=================================================================== RCS file: /cvs/RT/Invoicing/lib/RTI/State.pm,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- RT/Invoicing/lib/RTI/State.pm 2011/12/30 05:01:56 1.2 +++ RT/Invoicing/lib/RTI/State.pm 2011/12/31 02:14:48 1.3 @@ -2,13 +2,13 @@ use strict; use warnings; -use Carp; +use 5.010; use DateTime; +use Carp; -use 5.010; - use YAML::Any qw/ LoadFile DumpFile /; +use RTI::Util qw/ ymd_to_DateTime /; my $file = ''; @@ -23,7 +23,10 @@ $self->{lastinvoice} ||= 0; while ( my ( $id, $invoice ) = each %{ $self->{invoice} } ) { $self->{lastinvoice} = $id if $self->{lastinvoice} < $id; + $invoice->{id} = $id; + $invoice->{$_} = ymd_to_DateTime( $invoice->{$_} ) + for qw/ invdate start end /; } } @@ -48,39 +51,17 @@ if exists $self->{invoice}->{$id}; $invoice->{id} ||= $id; - $invoice->{invdate} ||= DateTime->now( time_zone => 'local' )->ymd, + $invoice->{invdate} ||= DateTime->now( time_zone => 'local' ), $self->{lastinvoice} = $id if $self->{lastinvoice} < $id; - my %li; - foreach my $k ( - qw/ - id custid - file transactions - invdate start end - total past_due total_due - / - ) - { - my $v = $invoice->{$k}; - - if ( defined $v && length $v ) { - if ( ref $v eq 'DateTime' ) { - $li{$k} = $v->ymd; - } - else { - $li{$k} = $v; - } - } - } - - $self->{invoice}->{ $self->{lastinvoice} } = \%li; + $self->{invoice}->{$id} = $invoice; delete $self->{_tables}; return $self->{lastinvoice}; } -sub get_invoice{ +sub get_invoice { my ( $self, $id ) = @_; return $self->{invoice}->{$id}; } @@ -102,7 +83,7 @@ sub txn_is_invoiced { my ( $self, $txn ) = @_; - + if ( !$self->{_table}->{txn} ) { my $invoices = $self->{invoice}; foreach my $id ( sort { $a <=> $b } keys %{$invoices} ) { @@ -116,7 +97,7 @@ } sub unpaid_invoices { - my ($self, $custid) = @_; + my ( $self, $custid ) = @_; $self->_match_payments; return defined $custid @@ -130,7 +111,29 @@ delete $self->{_table}; delete $self->{lastinvoice}; foreach my $invoice ( values %{ $self->{invoice} } ) { - delete $invoice->{id}; + delete $invoice->{$_} for qw/ + id + from + to + info + logo + projects + expenses + discounts + /; + + foreach my $k ( keys %{$invoice} ) { + my $v = $invoice->{$k}; + + if ( defined $v && length $v ) { + if ( ref $v eq 'DateTime' ) { + $invoice->{$k} = $v->ymd; + } + } + else { + delete $invoice->{$k}; + } + } } DumpFile( $file, {%$self} ) or die "Unable to save state: $!"; } @@ -141,7 +144,7 @@ return if $self->{_table}{credit} && $self->{_table}{unpaid}; my $invoices = $self->{invoice}; - my %owes = map { $_ => $invoices->{$_}->{total} } keys %{ $invoices }; + my %owes = map { $_ => $invoices->{$_}->{total} } keys %{$invoices}; my %credit; @@ -177,7 +180,7 @@ } foreach my $id ( sort { $b <=> $a } keys %owes ) { - my $i = $invoices->{$id}; + my $i = $invoices->{$id}; my $custid = $i->{custid} or next; my $owes = sprintf "%0.2f", $owes{$id} || 0; @@ -201,7 +204,7 @@ if ($paid) { $credit{$custid} = $paid; } - elsif (exists $credit{$custid}) { + elsif ( exists $credit{$custid} ) { delete $credit{$custid}; } }