=================================================================== RCS file: /cvs/RT/Invoicing/lib/RTI/State.pm,v retrieving revision 1.2 retrieving revision 1.7 diff -u -r1.2 -r1.7 --- RT/Invoicing/lib/RTI/State.pm 2011/12/30 05:01:56 1.2 +++ RT/Invoicing/lib/RTI/State.pm 2012/11/28 02:24:04 1.7 @@ -2,14 +2,15 @@ use strict; use warnings; -use Carp; +use 5.010; use DateTime; +use Carp; -use 5.010; +use YAML::XS qw/ LoadFile DumpFile /; +$YAML::XS::QuoteNumericStrings = 0; +use RTI::Util qw/ ymd_to_DateTime /; -use YAML::Any qw/ LoadFile DumpFile /; - my $file = ''; sub new { @@ -23,8 +24,17 @@ $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 /; } + foreach my $custid (keys %{ $self->{payment} || {} }) { + foreach my $payment (@{ $self->{payment}->{$custid} || [] }) { + $payment->{date} = ymd_to_DateTime( $payment->{date} ) + if $payment->{date}; + } + } } bless $self, $class; @@ -48,39 +58,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 +90,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 +104,7 @@ } sub unpaid_invoices { - my ($self, $custid) = @_; + my ( $self, $custid ) = @_; $self->_match_payments; return defined $custid @@ -124,14 +112,53 @@ : $self->{_table}->{unpaid}; } +sub credits { + my ( $self, $custid ) = @_; + + $self->_match_payments; + return defined $custid + ? $self->{_table}->{credit}->{$custid} + : $self->{_table}->{credit}; +} + sub save { my ($self) = @_; 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 + discount + hours + organization + /; + + 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}; + } + } } + foreach my $custid (keys %{ $self->{payment} || {} }) { + foreach my $payment (@{ $self->{payment}->{$custid} || [] }) { + $payment->{date} = $payment->{date}->ymd + if ref $payment->{date} eq 'DateTime'; + } + } DumpFile( $file, {%$self} ) or die "Unable to save state: $!"; } @@ -141,7 +168,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 +204,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 +228,7 @@ if ($paid) { $credit{$custid} = $paid; } - elsif (exists $credit{$custid}) { + elsif ( exists $credit{$custid} ) { delete $credit{$custid}; } }