=================================================================== RCS file: /cvs/RT/Invoicing/rt_invoices.pl,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- RT/Invoicing/rt_invoices.pl 2011/03/21 23:29:22 1.7 +++ RT/Invoicing/rt_invoices.pl 2011/03/22 00:22:27 1.8 @@ -148,12 +148,6 @@ } my $invoice = $cust->{invoice}; - #foreach my $r ($ticket->requestors) { - # $users->id( $r ); - # $users->retrieve; - # print Dump $users; - #} - my $project = make_project( $ticket, $cust ); next unless @{ $project->{fees} } || @{ $project->{expenses} }; @@ -301,14 +295,64 @@ return $cust; } - return fake_customer($ticket); + return fake_customer( $customers, $ticket ); } sub fake_customer { - my ($ticket) = @_; + my ( $customers, $ticket ) = @_; - # XXX eventually, will generate a customer we can bill! - return; + # make the custid the first requestor + my ($custid) = $ticket->requestors; + return unless $custid; + + my $cust = $config->get('default') || {}; + $cust->{to} = get_requestor($custid); + + my %invoice = ( end => DateTime->now ); + + my $lastinvoice = $state->last_invoice($custid); + if ( $lastinvoice->{date} ) { + my $last_invoice_date = ymd_to_DateTime( $lastinvoice->{date} ); + $invoice{start} = $last_invoice_date->clone->add( days => 1 ); + } + + if ( !( $invoice{start} && $invoice{start} < $invoice{end} ) ) { + $cust->{invoice} = \%invoice; + } + + $customers->{$custid} = $cust; + return $cust; +} + +sub get_requestor { + my ($id) = @_; + + state %requestors; + return $requestors{$id} if $requestors{$id}; + + my %map = ( + address_one => 'addr1', + address_two => 'addr2', + email_address => 'email', + ); + + $users->id($id); + $users->retrieve; + + my %requestor; + foreach my $m ( keys %{ $users->_attributes } ) { + next unless $users->can($m); + + my $v = $users->$m; + next unless $v; + + $m = $map{$m} if exists $map{$m}; + + $requestor{$m} = $v; + } + + $requestors{$id} = \%requestor; + return \%requestor; } sub make_project {