[Solved] email always creates a new ticket with custom ticketgenerator

Moderator: crythias

Post Reply
jjurkus
Znuny newbie
Posts: 54
Joined: 29 Jan 2016, 15:36
Znuny Version: 6.0.17

[Solved] email always creates a new ticket with custom ticketgenerator

Post by jjurkus »

Yes, this is one of those subjects that have been discussed before. I've searched and read quite a few topics, the developer manual, but I'm doing something wrong.

I have created a different ticketgenerator, to make the ticketnumbers take the form of $Year-$Count.
For example, this creates a ticket like 2016-0030, and it works fine.

However, when a customer replies to an existing ticket a new ticket is generated, while it should append that e-mail to the original ticket.

This is my TicketGenerator.pm

Code: Select all

package Kernel::System::Ticket::Number::TicketGenerator;

use strict;
use warnings;

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::Log',
    'Kernel::System::Main',
    'Kernel::System::Time',
);

sub TicketCreateNumber {
    my ( $Self, $JumpCounter ) = @_;

    if ( !$JumpCounter ) {
        $JumpCounter = 0;
    }

    # get needed objects
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');
    my $MainObject   = $Kernel::OM->Get('Kernel::System::Main');
    my $TimeObject   = $Kernel::OM->Get('Kernel::System::Time');

    # get needed config options
    my $CounterLog = $ConfigObject->Get('Ticket::CounterLog');
    my $SystemID   = $ConfigObject->Get('SystemID');

    # get current time
    my ( $Sec, $Min, $Hour, $Day, $Month, $Year ) = $TimeObject->SystemTime2Date(
        SystemTime => $TimeObject->SystemTime(),
    );

    # read count
    my $Count = 0;
    my $LastModify = '';
    if ( -f $CounterLog ) {

        my $ContentSCALARRef = $MainObject->FileRead(
            Location => $CounterLog,
        );

        if ( $ContentSCALARRef && ${$ContentSCALARRef} ) {

            ($Count) = split( /;/, ${$ContentSCALARRef} );

            # just debug
            if ( $Self->{Debug} > 0 ) {
                $Kernel::OM->Get('Kernel::System::Log')->Log(
                    Priority => 'debug',
                    Message  => "Read counter from $CounterLog: $Count",
                );
            }
        }
    }

    # check if we need to reset the counter, happens yearly
    if ( !$LastModify || $LastModify ne "$Year" ) {
        $Count = 0;
    }
    # count auto increment ($Count++)
    $Count++;
    $Count = $Count + $JumpCounter;

    # write new count
    my $Write = $MainObject->FileWrite(
        Location => $CounterLog,
        Content  => \$Count,
    );

    if ($Write) {

        if ( $Self->{Debug} > 0 ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'debug',
                Message  => "Write counter: $Count",
            );
        }
    }

    my $MinSize = $ConfigObject->Get('Ticket::NumberGenerator::MinCounterSize')
        || 4;
    $Count = sprintf "%.*u", $MinSize, $Count;

    # create new ticket number
    my $Tn = $Year . "-" . $Count;

    # Check ticket number. If exists generate new one!
    if ( $Self->TicketCheckNumber( Tn => $Tn ) ) {

        $Self->{LoopProtectionCounter}++;

        if ( $Self->{LoopProtectionCounter} >= 16000 ) {

            # loop protection
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "CounterLoopProtection is now $Self->{LoopProtectionCounter}!"
                    . " Stopped TicketCreateNumber()!",
            );
            return;
        }

        # create new ticket number again
        $Kernel::OM->Get('Kernel::System::Log')->Log(
            Priority => 'notice',
            Message  => "Tn ($Tn) exists! Creating a new one.",
        );

        $Tn = $Self->TicketCreateNumber( $Self->{LoopProtectionCounter} );
    }

    return $Tn;
}

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

    if ( !$String ) {
        return;
    }

    # get config object
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # get needed config options
    my $CheckSystemID = $ConfigObject->Get('Ticket::NumberGenerator::CheckSystemID');
    my $SystemID      = '';

    if ($CheckSystemID) {
        $SystemID = $ConfigObject->Get('SystemID');
    }

    my $TicketHook        = $ConfigObject->Get('Ticket::Hook');
    my $TicketHookDivider = $ConfigObject->Get('Ticket::HookDivider');

    # check current setting
    if ( $String =~ /\Q$TicketHook$TicketHookDivider\E(\d{1,4}-\d{1,40})/i ) {
        return $1;
    }

    # check default setting
    if ( $String =~ /\Q$TicketHook\E:\s{0,2}($SystemID\d{1,4}-\d{1,40})/i ) {
        return $1;
    }

    return;
}

1;
Of course I modified the check for the current ticket number layout in GetTNByString. Either it does not match, which is because of the way the regex is constructed. Or there are other settings not entirely correct.

I have been using the on-line regular expression 'calculators', but I can not get the subject to match without using a global modifier. However, it is easy to send an e-mail with something that matches the used regex.
I have replied to an e-mail sent by the otrs system, which has a ticketnumber in the subject. I've sent e-mails with these subjects:
Re: [Ticket#2016-0005]
[Ticket#2016-0005]
Ticket#2016-0005
2016-0005
[2016-0005]
Re: [2016-0005]
Neither works, so this would make me think something is amiss in the settings.

Ticket -> Core::PostMaster
PostMaster::PostFilterModule###000-FollowUpArticleTypeCheck
PostMaster::CheckFollowUpModule###0100-Subject
PostMaster::CheckFollowUpModule###0200-References
are all enabled, and not modified from the default

Ticket -> Core::Ticket
Ticket::NumberGenerator::CheckSystemID -> No

E-mail is sent and fetched with IMAPS and SMTPS.

I hope somebody can point me in the right direction, cause I'm out of options to check. :(

I also don't know how to get this stuff logged, the debug log only dumps all parameter settings, and I'm not aware of any logfile where I can see what it's doing and if something matches at all.
Last edited by jjurkus on 19 Feb 2016, 14:54, edited 1 time in total.
OTRS 6.0.x on CentOS 7 with a PostgreSQL database.
EXG133
Znuny expert
Posts: 217
Joined: 06 Aug 2012, 18:12
Znuny Version: 3.1.7 & 4.04

Re: email always creates a new ticket with custom ticketgenerator

Post by EXG133 »

What are you TicketHook settings in Core::Ticket?
jjurkus
Znuny newbie
Posts: 54
Joined: 29 Jan 2016, 15:36
Znuny Version: 6.0.17

Re: email always creates a new ticket with custom ticketgenerator

Post by jjurkus »

EXG133 wrote:What are you TicketHook settings in Core::Ticket?

Code: Select all

su -c "/opt/otrs/bin/otrs.Console.pl Maint::Config::Dump Ticket::Hook" -s /bin/bash otrs
Ticket#
Ticket::HookDivider is also unchanged from the default.
OTRS 6.0.x on CentOS 7 with a PostgreSQL database.
jjurkus
Znuny newbie
Posts: 54
Joined: 29 Jan 2016, 15:36
Znuny Version: 6.0.17

Re: email always creates a new ticket with custom ticketgenerator

Post by jjurkus »

Anybody?

Of course I have searched, and found topics like this: viewtopic.php?f=53&t=16812
As the parsing of the ticket number is in the same file I also have modified that (see first post).

Anyone else have modified the number generator and willing to share their file? Maybe that will help me to see the error.
OTRS 6.0.x on CentOS 7 with a PostgreSQL database.
jjurkus
Znuny newbie
Posts: 54
Joined: 29 Jan 2016, 15:36
Znuny Version: 6.0.17

Re: [Solved] email always creates a new ticket with custom ticketgenerator

Post by jjurkus »

I finally got it working!

I don't know why. :(
I've changed the regexp and it worked. I had a lot of if statements to test various cases, but it didn't respond to any of those. Maybe only the first if is evaluated?? :?: :-|
OTRS 6.0.x on CentOS 7 with a PostgreSQL database.
hrobayo
Znuny newbie
Posts: 8
Joined: 04 Feb 2019, 21:48
Znuny Version: 6.0.16
Real Name: Hugo Camilo Robayo Ayala
Company: Aduana

Re: [Solved] email always creates a new ticket with custom ticketgenerator

Post by hrobayo »

Please could you send the regexp expression that works for you...
jjurkus
Znuny newbie
Posts: 54
Joined: 29 Jan 2016, 15:36
Znuny Version: 6.0.17

Re: [Solved] email always creates a new ticket with custom ticketgenerator

Post by jjurkus »

This is my current TicketGenerator-v6.pm:

Code: Select all

package Kernel::System::Ticket::Number::TicketGenerator;

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 TicketCreateNumber {
	my ( $Self, $JumpCounter ) = @_;

	if ( !$JumpCounter ) {
		$JumpCounter = 0;
	}
	
	# get needed objects
	my $ConfigObject = $Kernel::OM->Get('Kernel::Config');    
    
	my $DateTimeObject = $Kernel::OM->Create('Kernel::System::DateTime');
	my $DateTimeSettings = $DateTimeObject->Get(); 
	my $CurrentYear = $DateTimeSettings->{Year};

	# als laatste ticketnummer niet bestaat moet er jaartal-0001 worden teruggeven of zo. -> huidig jaartal-0 laten ophogen
	my $LastTicketNumber = $Self->_GetLastTicketNumber();
	if ( !$LastTicketNumber ) {
		my $Count = 0;
		my $LastTicketNumber = $CurrentYear . "-" . $Count;	
    }
	
    # lastticketnumber splitsen in deel voor en na streep
    # jaartal van laatste ticket vergelijken met huidige jaar -> indien anders weer bij 1 beginnen
    # anders jaartal-count+1
	my ( $Year, $Count ) = split(/-/,$LastTicketNumber);  

	# check if we need to reset the counter, happens yearly
    if ( $Year ne "$CurrentYear" ) {
    	$Count = 0;
    	$Year = $CurrentYear;
	}
	
	# Counterdeel van ticketnummer ophogen
	$Count++;
	$Count = $Count + $JumpCounter;

	# Minimumlengte van Counter ophalen uit de config
	my $MinSize = $ConfigObject->Get('Ticket::NumberGenerator::MinCounterSize')
		|| 4;
	$Count = sprintf "%.*u", $MinSize, $Count;

	# create new ticket number
	my $Tn = $Year . "-" . $Count;
	
    return $Tn;
}

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

    if ( !$String ) {
        return;
    }

    # get config object
    my $ConfigObject = $Kernel::OM->Get('Kernel::Config');

    # get needed config options
    my $CheckSystemID = $ConfigObject->Get('Ticket::NumberGenerator::CheckSystemID');
    my $SystemID      = '';

    if ($CheckSystemID) {
        $SystemID = $ConfigObject->Get('SystemID');
    }

    my $TicketHook        = $ConfigObject->Get('Ticket::Hook');
    my $TicketHookDivider = $ConfigObject->Get('Ticket::HookDivider');

    # check current setting
    if ( $String =~ /\Q$TicketHook$TicketHookDivider\E(\d{1,4}-\d{1,40})/i ) {
        return $1;
    }

    # check default setting
    if ( $String =~ /\Q$TicketHook\E:\s{0,2}($SystemID\d{1,4}-\d{1,40})/i ) {
        return $1;
    }

    return;
}

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};
}

1;
The only thing I have not tested good is what happens when a new year starts. Something buggered up the beginning of this year. I think I have solved it now, but I'll find out at the end of this year. 8)

edit: I can't see what changed in the regexp. :-(
OTRS 6.0.x on CentOS 7 with a PostgreSQL database.
Post Reply