=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- RT/Invoicing/rt_invoices.pl 2011/03/21 21:49:44 1.6 +++ RT/Invoicing/rt_invoices.pl 2011/03/21 23:29:22 1.7 @@ -38,8 +38,8 @@ my $day = $cust->{day} || 1; my $freq = $cust->{frequency} || 1; - my $day_method; my $per; + my $day_method; given ( $cust->{per} ) { when ('week') { $per = 'weeks'; $day_method = 'dow' } when ('month') { $per = 'months'; $day_method = 'day' } @@ -48,23 +48,18 @@ my $billends = DateTime->now->set( hour => 0, minute => 0, second => 0 ); - while ( $billends->$day_method != $day ) { - $billends->subtract( days => 1 ); - } + $billends->subtract( days => 1 ) while $billends->$day_method != $day; my $date = $billends->clone->subtract( $per => $freq ); my $lastinvoice = $state->last_invoice($custid); if ( $lastinvoice->{date} ) { my $last_invoice_date = ymd_to_DateTime( $lastinvoice->{date} ); - - warn "HAVE LASTINVOICE DATE\n"; - next CUSTOMER - if DateTime->compare( $billends, $last_invoice_date ) < 1; - $date = $last_invoice_date->clone->add( days => 1 ); } + next CUSTOMER if $billends <= $date; + my $title = $freq == 1 ? ucfirst( $cust->{per} . 'ly' ) @@ -78,12 +73,14 @@ $date->add( $per => $freq ); $date = $billends->clone if $date > $billends; - if ( my $diff = $date->$day_method - $day ) { - $date->subtract( days => $diff ); - } + # XXX This is helpful, but monthly and billday > 28 == !!! + $date->subtract( days => 1 ) while $date->$day_method != $day; + my $end = $date->clone->subtract( seconds => 1 ); + next if $end < $start; + $startdate = $start->clone if !$startdate || $startdate > $start; $invoice{start} ||= $start->clone; $invoice{end} = $end->clone; @@ -143,10 +140,12 @@ my $cust = find_customer_for_ticket( $customers, $ticket ); if ( !$cust ) { say "No customer found for ticket " . $ticket->id; - - # XXX should construct a "new" invoice to pop onto the list next; } + if ( !$cust->{invoice} ) { + say "Customer has no open invoices for ticket " . $ticket->id; + next; + } my $invoice = $cust->{invoice}; #foreach my $r ($ticket->requestors) { @@ -154,7 +153,8 @@ # $users->retrieve; # print Dump $users; #} - my $project = make_project( $invoice, $ticket ); + + my $project = make_project( $ticket, $cust ); next unless @{ $project->{fees} } || @{ $project->{expenses} }; foreach my $fee ( @{ $project->{fees} } ) { @@ -194,10 +194,12 @@ my $invoice = $cust->{invoice}; next unless $invoice->{projects} && @{ $invoice->{projects} }; + my %li = ( custid => $custid, ); + foreach my $project ( @{ $invoice->{projects} } ) { print "$project->{title}\n"; if ( $project->{transactions} ) { - push @{ $cust->{transactions} }, @{ $project->{transactions} }; + push @{ $li{transactions} }, @{ $project->{transactions} }; } my $subtotal = 0; foreach my $fee ( @{ $project->{fees} } ) { @@ -239,8 +241,6 @@ $invoice->{id} = $state->{lastinvoice}; $invoice->{file} = 'invoice_' . $state->{lastinvoice} . '.pdf'; - my %li = ( custid => $custid, ); - foreach my $k (qw/ transactions /) { if ( $invoice->{$k} ) { $li{$k} = $invoice->{$k} } elsif ( $cust->{$k} ) { $li{$k} = $cust->{$k} } @@ -250,7 +250,7 @@ = \%li; print "Created Invoice\n"; - print Dump $invoice; + print Dump $cust; foreach my $key (qw/ start end /) { if ( exists $invoice->{$key} ) { @@ -312,7 +312,7 @@ } sub make_project { - my ( $invoice, $ticket ) = @_; + my ( $ticket, $cust ) = @_; my %project = ( id => $ticket->id, @@ -335,13 +335,17 @@ next unless $txn->time_taken; next if $state->txn_is_invoiced( $txn->id ); - my $fee = make_fee( $ticket, $txn, $invoice->{rates} ); + my $fee = make_fee( $txn, $cust->{rates}, $ticket ); + if ( !( $fee->{rate} && $fee->{count} ) ) { warn "Invalid Fee, no rate or count"; next; } - next if $invoice->{start} && $invoice->{start} > $fee->{date}; + my $invoice = $cust->{invoice}; + next + if $invoice->{start} + && $invoice->{start} > $fee->{date}; next if $invoice->{end} < $fee->{date}; push @{ $project{fees} }, $fee; @@ -352,11 +356,12 @@ } sub make_fee { - my ( $ticket, $txn, $rates ) = @_; + my ( $txn, $rates, $ticket ) = @_; + # 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_rate = $rates->{$work_type} || $rates->{default} || 0; my %fee = ( id => $txn->id, @@ -364,9 +369,9 @@ . $txn->id . ')' . "\n\n" . ( $txn->data || $ticket->subject ), count => $work_time, - rate => $work_rate, type => $work_type, date => ymd_to_DateTime( $txn->created ), + rate => $rates->{$work_type} || $rates->{default} || 0, ); if ( $work_type && $work_type ne 'Normal' ) {