=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.44 retrieving revision 1.55 diff -u -r1.44 -r1.55 --- RT/Invoicing/rt_invoices.pl 2011/12/30 05:01:41 1.44 +++ RT/Invoicing/rt_invoices.pl 2020/08/02 18:52:40 1.55 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.43 2011/12/30 03:30:51 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.54 2015/12/08 02:10:43 andrew 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; @@ -28,9 +31,12 @@ use File::Path; use DateTime; +use List::Util qw/ sum /; + use lib './lib'; # XXX This is fragile, there are better ways use RTI::Config; use RTI::State; +use RTI::Util qw/ round ymd_to_DateTime /; my $config = RTI::Config->new(); my $state = RTI::State->new( $config->get('state') ); @@ -134,8 +140,10 @@ if ( my $unpaid_invoices = $state->unpaid_invoices() ) { foreach my $custid ( keys %{$unpaid_invoices} ) { - my %project = ( title => 'Unpaid Invoices', fees => [], ); + my %project + = ( title => 'Unpaid Invoices', fees => [], no_total => 1 ); my $past_due = 0; + my $unpaid = 0; my $cust; foreach ( @{$customers} ) { @@ -146,41 +154,76 @@ } $cust ||= fake_customer($custid); - foreach my $id ( sort { $a <=> $b } - keys %{ $unpaid_invoices->{$custid} } ) + foreach my $id ( + sort { $a <=> $b } + keys %{ $unpaid_invoices->{$custid} } + ) { my $unpaid = $state->get_invoice($id); my $invdate = ymd_to_DateTime( $unpaid->{invdate} ); - next - if $cust->{duedate} - && DateTime->compare( $invdate, $cust->{duedate} ) > 0; + my $content + = sprintf( "Invoice %06d from %s", $id, $invdate->ymd ); + if ( $cust->{duedate} && $invdate < $cust->{duedate}) { + $content = "PAST DUE: $content"; + $past_due += $unpaid_invoices->{$custid}->{$id}; + } + else { + $unpaid += $unpaid_invoices->{$custid}->{$id}; + } - $past_due += $unpaid_invoices->{$custid}->{$id}; push @{ $project{fees} }, { - id => $id, - contents => - sprintf( "Invoice %06d from %s", $id, $invdate->ymd ), - count => 1, - rate => $unpaid_invoices->{$custid}->{$id}, + id => $id, + contents => $content, + count => 1, + rate => $unpaid_invoices->{$custid}->{$id}, }; } if ($past_due) { - $cust->{invoice} ||= make_invoice(); + $cust->{invoice} ||= make_invoice($cust); $cust->{invoice}->{past_due} = $past_due; - $cust->{invoice}->{total_due} - = $cust->{invoice}->{total} + $past_due; + $cust->{invoice}->{unpaid} = $unpaid; unshift @{ $cust->{invoice}->{projects} }, \%project; } } } +if ( my $credits = $state->credits ) { + foreach my $custid ( keys %{$credits} ) { + + my $cust; + foreach ( @{$customers} ) { + if ( $_->{id} eq $custid ) { + $cust = $_; + last; + } + } + + next unless $cust; + next unless $cust->{invoice}; + next unless $credits->{$custid} < 0; + + $cust->{invoice}->{credit} = $credits->{$custid}; + + unshift @{ $cust->{invoice}->{projects} }, { + title => 'Credits', + no_total => 1, + fees => [ + { contents => 'Available Credit', + count => 1, + rate => -$credits->{$custid}, + } + ], + }; + } +} + 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}; @@ -200,6 +243,8 @@ $subtotal += round( $expense->{amount} ); } $project->{total} = $subtotal; + + next if $project->{no_total}; $invoice->{total} += $subtotal; } @{ $invoice->{transactions} } = sort { $a <=> $b } keys %transactions; @@ -218,6 +263,11 @@ $invoice->{total} -= round( $invoice->{discount}{amount} ); } + if ($invoice->{past_due}) { + $invoice->{total_due} + = sum( @{ $invoice }{ qw/ total past_due unpaid / } ); + } + next unless $invoice->{total} > 0 || $invoice->{total_due}; $invoice->{info} = $config->get('info'); @@ -234,12 +284,6 @@ $state->add_invoice($invoice); - foreach my $key (qw/ start end /) { - if ( exists $invoice->{$key} ) { - $invoice->{$key} = $invoice->{$key}->strftime('%B %d, %Y'); - } - } - my $invoice_dir = $config->get('invoice_dir'); File::Path::make_path($invoice_dir); my $file = join '/', $invoice_dir, $invoice->{file}; @@ -257,14 +301,6 @@ $state->save; -sub round { - my ($amount) = @_; - - #$amount =~ s/\.\d\d\K.*$//; - #return $amount; - return sprintf "%.02f", $amount; -} - sub find_customer_for_ticket { my ( $ticket, $customers ) = @_; @@ -272,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; + } + } } } @@ -343,8 +385,10 @@ sub make_invoice { my ($cust) = @_; - my %invoice - = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); + my %invoice = ( + end => $cust->{billend}->clone->subtract( seconds => 1 ), + total => 0, + ); $invoice{start} = $cust->{startinvoicedate}->clone if $cust->{startinvoicedate}; @@ -387,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 ); @@ -503,7 +547,7 @@ # XXX Only need $ticket for the alternate subject my $work_time = sprintf "%.03f", $txn->time_taken / 60; - my $work_type = $txn->cf('WorkType'); + my $work_type = $txn->cf('WorkType') || ''; if ( $work_type =~ s/\s*Onsite//i ) { @@ -566,36 +610,17 @@ return $hours; } -sub ymd_to_DateTime { - my ($ymd) = @_; - my ( $date, $time ) = split /[\sT]/, $ymd; - my ( $year, $month, $day ) = split '-', $date; - my ( $hour, $minute, $second ) = split ':', $time if $time; - - return DateTime->new( - year => $year, - month => $month, - day => $day, - hour => $hour || 0, - minute => $minute || 0, - second => $second || 0, - time_zone => 'local', - ); -} - sub get_billing_frequency { my ($cust) = @_; 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' ? $day_method = 'dow' + : $per eq 'month' ? $day_method = '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 { @@ -619,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 ); } @@ -632,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; @@ -648,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; }