Custom Ticket::NumberGenerator not selectable

Moderator: crythias

Post Reply
billyboy88
OTRS newbie
Posts: 1
Joined: 15 May 2018, 15:20
OTRS Version?: 6.0.6

Custom Ticket::NumberGenerator not selectable

Post by billyboy88 » 04 Jun 2018, 09:45

Hello,
I've created a custom NumberGenerator by copying /opt/otrs/Kernel/System/Ticket/Number/AutoIncrement.pm to PrefixBasedAutoIncrement.pm. I've only changed the package name and the return variable of the TicketNumberBuild function.

The problem I have is that the new NumberGenerator is shown in the Administration-Menu Dropdownbox under Core->Ticket->Ticket::NumberGenerator but if I select it and try to save, OTRS told me that "The setting value is invalid".

Full code of the package in /opt/otrs/Kernel/System/Ticket/Number/PrefixBasedAutoIncrement.pm:

Code: Select all

package Kernel::System::Ticket::Number::PrefixBasedAutoIncrement;

use strict;
use warnings;

use parent qw(Kernel::System::Ticket::NumberBase);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::DB',
    'Kernel::System::Ticket',
);

sub IsDateBased {
    return 0;
}

sub TicketNumberBuild {
    my ( $Self, $Offset ) = @_;

    $Offset ||= 0;

    my $BaseCounter = 1;
    if ( $Self->TicketNumberCounterIsEmpty() ) {
        $BaseCounter = $Self->InitialCounterOffsetCalculate();
    }

    my $Counter = $Self->TicketNumberCounterAdd(
        Offset => $BaseCounter + $Offset,
    );

    return if !$Counter;

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    my $Prefix = $ConfigObject->Get('Ticket::NumberGenerator::PrefixBasedAutoIncrement::Prefix');
    my $MinSize  = $ConfigObject->Get('Ticket::NumberGenerator::AutoIncrement::MinCounterSize')
        || $ConfigObject->Get('Ticket::NumberGenerator::MinCounterSize')
        || 5;

    # Pad ticket number with leading '0' to length $MinSize (config option).
    $Counter = sprintf "%.*u", $MinSize, $Counter;

    my $TicketNumber = $Prefix . $Counter;

    return $TicketNumber;
}

sub GetTNByString {
    my ( $Self, $String ) = @_;

    if ( !$String ) {
        return;
    }

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    my $CheckPrefix = $ConfigObject->Get('Ticket::NumberGenerator::PrefixBasedAutoIncrement::CheckPrefix');
    my $Prefix      = '';

    if ($CheckPrefix) {
        $Prefix = $ConfigObject->Get('Ticket::NumberGenerator::PrefixBasedAutoIncrement::Prefix');
    }

    my $TicketHook        = $ConfigObject->Get('Ticket::Hook');
    my $TicketHookDivider = $ConfigObject->Get('Ticket::HookDivider');
    my $MinSize           = $ConfigObject->Get('Ticket::NumberGenerator::AutoIncrement::MinCounterSize')
        || $ConfigObject->Get('Ticket::NumberGenerator::MinCounterSize')
        || 5;
    my $MaxSize = $MinSize + 5;

    # Check ticket number.
    if ( $String =~ /\Q$TicketHook$TicketHookDivider\E($Prefix\d{$MinSize,$MaxSize})/i ) {
        return $1;
    }

    if ( $String =~ /\Q$TicketHook\E:\s{0,2}($SPrefix\d{$MinSize,$MaxSize})/i ) {
        return $1;
    }

    return;
}

#
# Calculate initial counter value on (migrated) systems that already have tickets,
#   but no counter entries yet.
#
sub InitialCounterOffsetCalculate {
    my ( $Self, %Param ) = @_;

    my $LastTicketNumber = $Self->_GetLastTicketNumber();
    return 1 if !$LastTicketNumber;

    # If the ticket number was created by a date based generator, ticket counter needs to start from 1
    return 1 if $Self->_LooksLikeDateBasedTicketNumber($LastTicketNumber);

    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    my $CheckPrefix = $ConfigObject->Get('Ticket::NumberGenerator::PrefixBasedAutoIncrement::CheckPrefix');
    my $SystemID      = '';

    if ($CheckPrefix) {
        $Prefix = $ConfigObject->Get('Ticket::NumberGenerator::PrefixBasedAutoIncrement::Prefix');
    }

    # Remove SystemID and leading zeros
    $LastTicketNumber =~ s{\A $Prefix 0* }{}msx;

    return 1 if !$LastTicketNumber;

    return $LastTicketNumber + 1;
}

sub _GetLastTicketNumber {
    my ( $Self, %Param ) = @_;

    my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

    return if !$DBObject->Prepare(
        SQL => 'SELECT MAX(id) FROM ticket',
    );

    my $TicketID;
    while ( my @Data = $DBObject->FetchrowArray() ) {
        $TicketID = $Data[0];
    }

    return if $TicketID && $TicketID == 1;

    my %Ticket = $Kernel::OM->Get('Kernel::System::Ticket')->TicketGet(
        TicketID      => $TicketID,
        DynamicFields => 0,
        UserID        => 1,
        Silent        => 1,
    );

    return if !%Ticket;
    return if !$Ticket{TicketNumber};

    return $Ticket{TicketNumber};
}

sub _LooksLikeDateBasedTicketNumber {
    my ( $Self, $TicketNumber ) = @_;

    return if !$TicketNumber;

    my $PossibleDate = substr $TicketNumber, 0, 8;
    return if length $PossibleDate != 8;

    # Format possible date as a date string
    $PossibleDate =~ s{\A (\d{4}) (\d{2}) (\d{2}) \z}{$1-$2-$3 00:00:00}gsmx;

    my $DateTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');

    my $Result = $DateTimeObject->Set( String => $PossibleDate );

    return if !$Result;

    return 1;
}

1;
I also added a specific config file in /opt/otrs/Kernel/Config/Files/XML/PrefixBasedAutoincrement.xml (and recreated the global config cache file) of course:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<otrs_config version="2.0" init="Change">
    <Setting Name="Ticket::NumberGenerator::PrefixBasedAutoIncrement::CheckPrefix">
       <Description Translatable="1">Checks the Prefix in ticket number detection for follow-ups. If not enabled, Prefix will be changed after using the system.</Description>
       <Navigation>Core::Ticket</Navigation>
       <Value>
           <Item ValueType="Checkbox">0</Item>
       </Value>
   </Setting>
   <Setting Name="Ticket::NumberGenerator::PrefixBasedAutoIncrement::Prefix">
       <Description Translatable="1">The prefix used in the PrefixBasedAutoIncrement module (e.g. 'MM_').</Description>
       <Navigation>Core::Ticket</Navigation>
       <Value>
           <Item ValueType="String" ValueRegex=""></Item>
       </Value>
    </Setting>
</otrs_config>
These settings of the XML file are shown in the web frontend and can be changed and saved.

I hope you can help me a little bit where to search. Does the package/NumberGenerator need to be registered somewhere?

Post Reply