=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- RT/Invoicing/rt_invoices.pl 2011/12/22 04:52:28 1.40 +++ RT/Invoicing/rt_invoices.pl 2011/12/22 05:21:56 1.41 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.38 2011/06/21 00:20:28 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.40 2011/12/22 04:52:28 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -84,10 +84,10 @@ next; } - next if $cust->{no_invoices}; + next if $cust->{no_invoice}; $cust->{invoice} ||= make_invoice($cust); if ( !$cust->{invoice} ) { - $cust->{no_invoices} = 1; + $cust->{no_invoice} = 1; say "$cust->{id} has no open invoices [" . $ticket->id . ']'; next; } @@ -320,11 +320,9 @@ sub make_invoice { my ($cust) = @_; + return if $cust->{no_invoice}; return unless $cust->{billstart}; - my ($freq) = get_billing_frequency($cust); - return if $cust->{billstart}->clone->add($freq) > $cust->{billend}; - my %invoice = ( end => $cust->{billend}->clone->subtract( seconds => 1 ) ); $invoice{start} = $cust->{startinvoicedate}->clone if $cust->{startinvoicedate}; @@ -612,22 +610,22 @@ my $day = $cust->{day} || 0; my ( $freq, $day_method ) = get_billing_frequency($cust); - my $billend = DateTime->now( time_zone => 'local' ) + my $end = DateTime->now( time_zone => 'local' ) ->set( hour => 0, minute => 0, second => 0 ); - my $date = $billend->clone->subtract($freq); + my $start = $end->clone->subtract($freq); # XXX This is helpful, but monthly and billday > 28 == !!! - $billend->subtract( days => 1 ) - while $day && $billend->$day_method != $day; + $end->subtract( days => 1 ) + while $day && $end->$day_method != $day; - $cust->{billend} = $billend; - my $lastinvoice = $state->last_invoice( $cust->{id} ); if ( $lastinvoice && $lastinvoice->{end} ) { - $date = ymd_to_DateTime( $lastinvoice->{end} )->add( days => 1 ); - $cust->{startinvoicedate} = $date->clone; + $start = ymd_to_DateTime( $lastinvoice->{end} )->add( days => 1 ); + $cust->{startinvoicedate} = $start->clone; } - $cust->{billstart} = $date; + $cust->{no_invoice} = 1 if $start->clone->add($freq) > $end; + $cust->{billend} = $end; + $cust->{billstart} = $start; }