=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.50 retrieving revision 1.56 diff -u -r1.50 -r1.56 --- RT/Invoicing/rt_invoices.pl 2013/06/20 05:36:06 1.50 +++ RT/Invoicing/rt_invoices.pl 2020/08/02 18:57:28 1.56 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.49 2013/02/05 03:41:58 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.55 2020/08/02 17:52:40 afresh1 Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -20,6 +20,9 @@ use 5.010; +# Because we don't have a real cert +$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; + use Template; use RT::Client::REST; use RT::Client::REST::Ticket; @@ -220,7 +223,7 @@ } foreach my $cust ( @{$customers} ) { - my $invoice = $cust->{invoice}; + my $invoice = $cust->{invoice} ||= make_invoice($cust); next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; $invoice->{custid} = $cust->{id}; @@ -305,17 +308,23 @@ next unless $cust->{match}; foreach my $m ( @{ $cust->{match} } ) { my $type = $m->{type}; - my $match - = exists $m->{$type} - ? lc( $m->{$type} ) - : qr/\Q$m->{regex}\E/; - my $thing = [ map {lc} $ticket->$type ]; - - if ( !$match ) { - warn "Invalid match!"; - next; + my @things = map {lc} $ticket->$type; + if ( exists $m->{$type} ) { + if ( !$m->{$type} ) { + warn "Invalid match!"; + next; + } + my $match = lc $m->{$type}; + for my $thing (@things) { + return $cust if $thing eq $match; + } } - return $cust if ( $match ~~ $thing ); + else { + my $match = qr/\Q$m->{regex}\E/; + for my $thing (@things) { + return $cust if $thing =~ $match; + } + } } } @@ -422,7 +431,7 @@ while ( $date < $billend ) { my $start = $date->clone; - $date->add($freq); + $date->add_duration($freq); my $end = $date > $billend ? $billend->clone : $date->clone; $end->subtract( seconds => 1 ); @@ -606,14 +615,12 @@ my $per = $cust->{per} || 'week'; my $freq = $cust->{frequency} || 1; - my $day_method; - given ($per) { - when ('week') { $per = 'weeks'; $day_method = 'dow' } - when ('month') { $per = 'months'; $day_method = 'day' } - default { die "Unknown per [$per]\n" } - } + my $day_method + = $per eq 'week' ? 'dow' + : $per eq 'month' ? 'day' + : die "Unknown per [$per]\n"; - return DateTime::Duration->new( $per => $freq ), $day_method; + return DateTime::Duration->new( "${per}s" => $freq ), $day_method; } sub set_dates { @@ -637,7 +644,9 @@ } } - return $newest_invoice->clone->subtract($max_duration) + $newest_invoice ||= DateTime->now; + + return $newest_invoice->clone->subtract_duration($max_duration) ->subtract( days => 1 ); } @@ -650,7 +659,7 @@ my $end = DateTime->now( time_zone => 'local' ) ->set( hour => 0, minute => 0, second => 0 ); - my $start = $end->clone->subtract($freq); + my $start = $end->clone->subtract_duration($freq); # XXX This is helpful, but monthly and billday > 28 == !!! $end->subtract( days => 1 ) while $day && $end->$day_method != $day; @@ -666,7 +675,7 @@ ? DateTime->now->subtract( days => $cust->{net} ) : 0; - $cust->{no_invoice} = 1 if $start->clone->add($freq) > $end; + $cust->{no_invoice} = 1 if $start->clone->add_duration($freq) > $end; $cust->{billend} = $end; $cust->{billstart} = $start; }