=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.42 retrieving revision 1.44 diff -u -r1.42 -r1.44 --- RT/Invoicing/rt_invoices.pl 2011/12/30 03:20:45 1.42 +++ RT/Invoicing/rt_invoices.pl 2011/12/30 05:01:41 1.44 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.41 2011/12/22 05:21:56 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.43 2011/12/30 03:30:51 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -47,7 +47,6 @@ #use YAML; #print Dump $config, $state; exit; - my $customers = $config->get('customers'); my $startdate = set_dates($customers); @@ -78,7 +77,7 @@ my $iterator = $results->get_iterator; while ( my $ticket = &$iterator ) { - my $cust = find_customer_for_ticket($ticket, $customers); + my $cust = find_customer_for_ticket( $ticket, $customers ); if ( !$cust ) { warn "No customer found for ticket " . $ticket->id; next; @@ -86,12 +85,10 @@ next if $cust->{no_invoice}; $cust->{invoice} ||= make_invoice($cust); - if ( !$cust->{invoice} ) { - $cust->{no_invoice} = 1; - say "$cust->{id} has no open invoices [" . $ticket->id . ']'; - next; - } + die "$cust->{id} has no open invoices [" . $ticket->id . ']' + unless $cust->{invoice}; + say 'Ticket ' . $ticket->id . " belongs to $cust->{id}"; my $project = make_project( $ticket, $cust ); @@ -135,11 +132,58 @@ } +if ( my $unpaid_invoices = $state->unpaid_invoices() ) { + foreach my $custid ( keys %{$unpaid_invoices} ) { + my %project = ( title => 'Unpaid Invoices', fees => [], ); + my $past_due = 0; + + my $cust; + foreach ( @{$customers} ) { + if ( $_->{id} eq $custid ) { + $cust = $_; + last; + } + } + $cust ||= fake_customer($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; + + $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}, + }; + } + + if ($past_due) { + $cust->{invoice} ||= make_invoice(); + + $cust->{invoice}->{past_due} = $past_due; + $cust->{invoice}->{total_due} + = $cust->{invoice}->{total} + $past_due; + + unshift @{ $cust->{invoice}->{projects} }, \%project; + } + } +} + foreach my $cust ( @{$customers} ) { my $invoice = $cust->{invoice}; next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; - $invoice->{custid} = $cust->{id}; + $invoice->{custid} = $cust->{id}; $invoice->{transactions} = []; my %transactions; @@ -174,38 +218,6 @@ $invoice->{total} -= round( $invoice->{discount}{amount} ); } - if (my $unpaid_invoices = $state->unpaid_invoices($cust->{id})) { - my %project = ( title => 'Unpaid Invoices', fees => [], ); - my $past_due = 0; - - foreach my $id ( sort { $a <=> $b } keys %{$unpaid_invoices} ) { - my $unpaid = $state->get_invoice($id); - my $invdate = ymd_to_DateTime( $unpaid->{invdate} ); - - next - if $cust->{duedate} - && DateTime->compare( $invdate, $cust->{duedate} ) > 0; - - $past_due += $unpaid_invoices->{$id}; - push @{ $project{fees} }, { - id => $id, - contents => sprintf( - "Invoice %06d from %s", - $id, $invdate->ymd - ), - count => 1, - rate => $unpaid_invoices->{$id}, - }; - } - - if ($past_due) { - $invoice->{past_due} = $past_due; - $invoice->{total_due} = $invoice->{total} + $invoice->{past_due}; - - unshift @{ $invoice->{projects} }, \%project; - } - } - next unless $invoice->{total} > 0 || $invoice->{total_due}; $invoice->{info} = $config->get('info'); @@ -220,9 +232,8 @@ $invoice->{to} = make_address( $cust->{address} || $cust->{id} ); $invoice->{logo} = $config->get('logo'); - $state->add_invoice( $invoice ); + $state->add_invoice($invoice); - foreach my $key (qw/ start end /) { if ( exists $invoice->{$key} ) { $invoice->{$key} = $invoice->{$key}->strftime('%B %d, %Y'); @@ -255,7 +266,7 @@ } sub find_customer_for_ticket { - my ($ticket, $customers) = @_; + my ( $ticket, $customers ) = @_; foreach my $cust ( @{$customers} ) { next unless $cust->{match}; @@ -275,13 +286,17 @@ } } - # Fake customer if we didn't find one - my $cust = $config->new_customer; + return fake_customer($ticket->requestors); +} - ( $cust->{id} ) = $ticket->requestors; - return unless $cust->{id}; +sub fake_customer { + my ($custid) = @_; + return unless $custid; + + my $cust = $config->new_customer; push @{$customers}, $cust; + $cust->{id} = $custid; $cust->{match} = [ { type => 'requestors', regex => $cust->{id}, @@ -328,17 +343,13 @@ sub make_invoice { my ($cust) = @_; - return if $cust->{no_invoice}; - return unless $cust->{billstart}; - - my %invoice = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); - $invoice{start} = $cust->{startinvoicedate}->clone + my %invoice + = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); + $invoice{start} = $cust->{startinvoicedate}->clone if $cust->{startinvoicedate}; - return if $invoice{start} && $invoice{start} > $invoice{end}; - if ( $cust->{base_rate} ) { - my ( $project, $hours ) = make_base_project( $cust ); + my ( $project, $hours ) = make_base_project($cust); if ( @{ $project->{fees} } ) { $invoice{end} = $project->{end}; @@ -358,11 +369,11 @@ } sub make_base_project { - my ( $cust ) = @_; + my ($cust) = @_; my $date = $cust->{billstart}->clone; my $billend = $cust->{billend}->clone; - my ($freq) = get_billing_frequency($cust); + my ($freq) = get_billing_frequency($cust); my $title = $cust->{frequency} == 1 @@ -376,7 +387,7 @@ while ( $date < $billend ) { my $start = $date->clone; - $date->add( $freq ); + $date->add($freq); my $end = $date > $billend ? $billend->clone : $date->clone; $end->subtract( seconds => 1 ); @@ -584,12 +595,12 @@ default { die "Unknown per [$per]\n" } } - return DateTime::Duration->new($per => $freq), $day_method; + return DateTime::Duration->new( $per => $freq ), $day_method; } sub set_dates { my ($customers) = @_; - + my $newest_invoice; my $max_duration; @@ -624,8 +635,7 @@ my $start = $end->clone->subtract($freq); # XXX This is helpful, but monthly and billday > 28 == !!! - $end->subtract( days => 1 ) - while $day && $end->$day_method != $day; + $end->subtract( days => 1 ) while $day && $end->$day_method != $day; my $lastinvoice = $state->last_invoice( $cust->{id} ); if ( $lastinvoice && $lastinvoice->{end} ) {