=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.24 retrieving revision 1.25 diff -u -r1.24 -r1.25 --- RT/Invoicing/rt_invoices.pl 2011/04/12 20:32:15 1.24 +++ RT/Invoicing/rt_invoices.pl 2011/04/19 04:18:13 1.25 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $AFresh1: rt_invoices.pl,v 1.23 2011/04/08 18:00:16 andrew Exp $ +# $AFresh1: rt_invoices.pl,v 1.24 2011/04/12 19:32:15 andrew Exp $ ######################################################################## # Copyright (c) 2011 Andrew Fresh # @@ -46,14 +46,7 @@ my $startdate; my $customers = $config->get('customers'); -while ( my ( $custid, $cust ) = each %{$customers} ) { - $cust->{id} = $custid; - if ( my $invoice = make_invoice($cust) ) { - $cust->{invoice} = $invoice; - } -} - my @limits = map +{ attribute => 'status', operator => '=', @@ -83,23 +76,25 @@ my $iterator = $results->get_iterator; while ( my $ticket = &$iterator ) { - my $cust = find_customer_for_ticket( $customers, $ticket ); + my $cust = find_customer_for_ticket( $ticket ); if ( !$cust ) { warn "No customer found for ticket " . $ticket->id; next; } - if ( !$cust->{invoice} ) { - say "$cust->{id} has no open invoices [" . $ticket->id . ']'; + + say 'Ticket ' . $ticket->id . " belongs to $cust->{id}"; + + $cust->{invoice} ||= make_invoice($cust); + if ( !$cust->{invoice}->{end} ) { + say " $cust->{id} has no open invoices [" . $ticket->id . ']'; next; } - say 'Giving ticket ' . $ticket->id . " to $cust->{id}"; - my $invoice = $cust->{invoice}; my $project = make_project( $ticket, $cust ); next unless @{ $project->{fees} } || @{ $project->{expenses} }; foreach my $fee ( @{ $project->{fees} } ) { - my $hours = hours_for_date( $invoice, $fee->{date} ); + my $hours = hours_for_date( $cust->{invoice}, $fee->{date} ); my $h_type = exists $hours->{ $fee->{type} } @@ -119,23 +114,23 @@ } if ($discount_time) { - $invoice->{discount}{amount} + $cust->{invoice}->{discount}->{amount} += round( $discount_time * $fee->{rate} ); - $invoice->{discount}{hours}{$h_type} += $discount_time; + $cust->{invoice}->{discount}->{hours}{$h_type} += $discount_time; $h_type = '' if $h_type eq 'default'; $fee->{detail} = "$discount_time $h_type Hours Discounted"; } } - push @{ $invoice->{projects} }, $project; + push @{ $cust->{invoice}->{projects} }, $project; } -while ( my ( $custid, $cust ) = each %{$customers} ) { +foreach my $cust ( @{$customers} ) { my $invoice = $cust->{invoice}; - next unless $invoice->{projects} && @{ $invoice->{projects} }; + next unless $invoice && $invoice->{projects} && @{ $invoice->{projects} }; - my %li = ( custid => $custid, invdate => DateTime->now->ymd, ); + my %li = ( custid => $cust->{id}, invdate => DateTime->now->ymd, ); foreach my $project ( @{ $invoice->{projects} } ) { if ( $project->{transactions} ) { @@ -178,7 +173,7 @@ $invoice->{organization} = $from->{organization} || $from->{name}; $invoice->{from} = make_address($from); - $invoice->{to} = make_address( $cust->{address} || $custid ); + $invoice->{to} = make_address( $cust->{address} || $cust->{id} ); $state->{lastinvoice}++; $invoice->{id} = $state->{lastinvoice}; @@ -218,7 +213,7 @@ $tt->process( $config->get('invoice_template'), $invoice, $file ) or die $tt->error . "\n"; - printf "Generated %s for %s: \$%.02f\n", $invoice->{file}, $custid, + printf "Generated %s for %s: \$%.02f\n", $invoice->{file}, $cust->{id}, $invoice->{total}; } @@ -233,9 +228,9 @@ } sub find_customer_for_ticket { - my ( $customers, $ticket ) = @_; + my ($ticket) = @_; - foreach my $cust ( values %{$customers} ) { + foreach my $cust ( @{$customers} ) { next unless $cust->{match}; foreach my $m ( @{ $cust->{match} } ) { my $type = $m->{type}; @@ -250,16 +245,12 @@ } } - return fake_customer( $customers, $ticket ); -} - -sub fake_customer { - my ( $customers, $ticket ) = @_; - + # Fake customer if we didn't find one my $cust = $config->get('default') || {}; ( $cust->{id} ) = $ticket->requestors; return unless $cust->{id}; + push @{$customers}, $cust; $cust->{match} = [ { type => 'requestors', @@ -267,11 +258,6 @@ } ]; - if ( my $invoice = make_invoice($cust) ) { - $cust->{invoice} = $invoice; - } - - $customers->{ $cust->{id} } = $cust; return $cust; } @@ -348,7 +334,7 @@ # Is the start date more than $freq $per before the end date? my $diff = $billends - $date; - return if $diff->in_units($per) < 1; + return {} if $diff->in_units($per) < 1; if ( $cust->{base_rate} ) { my ( $project, $hours ) = make_base_project( @@ -376,7 +362,7 @@ ]; } - return if $invoice{start} && $invoice{end} < $invoice{start}; + return {} if $invoice{start} && $invoice{end} < $invoice{start}; return \%invoice; } @@ -608,9 +594,9 @@ my $c = LoadFile($file) or die "Unable to load $file\n"; - $c->{customers} ||= {}; + $c->{customers} ||= []; if ( $c->{default} ) { - foreach my $cust ( values %{ $c->{customers} } ) { + foreach my $cust ( @{ $c->{customers} } ) { foreach my $k ( keys %{ $c->{default} } ) { $cust->{$k} //= Load( Dump( $c->{default}->{$k} ) ); }