[BACK]Return to fix_state CVS log [TXT][DIR] Up to [local] / RT / Invoicing

Annotation of RT/Invoicing/fix_state, Revision 1.2

1.1       andrew      1: #!/usr/bin/perl
1.2     ! andrew      2: # $AFresh1: fix_state,v 1.1 2011/12/21 02:21:54 andrew Exp $
1.1       andrew      3: use strict;
                      4: use warnings;
                      5:
                      6: # Converts a rt_invoices.state v1.38 or earler to a newer file
                      7:
                      8: use YAML::Any qw/ LoadFile DumpFile Dump /;
                      9:
                     10: my $state = LoadFile('rt_invoice.state');
                     11:
                     12: my %payments;
                     13: my %invoices;
                     14: while (my ($date, $invoices) = each %{ $state->{invoice } }) {
                     15:        while (my ($id, $invoice) = each %{ $invoices }) {
                     16:                $invoice->{invdate} ||= $date;
                     17:                update_payment($id, $invoice) if $invoice->{paid};
                     18:                $invoices{$id} = $invoice;
                     19:        }
                     20: }
                     21:
                     22: $state->{invoice} = \%invoices;
1.2     ! andrew     23:
        !            24: foreach my $custid (keys %payments) {
        !            25:     my $old = $payments{$custid};
        !            26:     my @new = sort { $b->{date} cmp $a->{date} } @{ $old };
        !            27:     $state->{payment}->{$custid} = \@new;
        !            28: }
        !            29:
1.1       andrew     30: #print Dump $state;
                     31: DumpFile('rt_invoice.state', $state);
                     32:
                     33:
                     34: sub update_payment{
                     35:        my ($id, $i) = @_;
                     36:
                     37:        $payments{$i->{custid}} ||= [];
                     38:        my $payments = $payments{ $i->{custid} };
                     39:
                     40:        my $p = { invoices => [] };
                     41:        my $new_payment = 1;
                     42:        if ($i->{check}) {
                     43:                foreach my $payment (@{ $payments }) {
                     44:                        if ($payment->{number} && $i->{check} == $payment->{number}) {
                     45:                                $new_payment = 0;
                     46:                                $p = $payment;
                     47:                                last;
                     48:                        }
                     49:                }
                     50:                $p->{type} = 'check';
                     51:                $p->{number} = delete $i->{check};
                     52:        }
                     53:        if ($new_payment) {
                     54:                $p->{paid} = 0;
                     55:                push @{ $payments }, $p;
                     56:        }
                     57:        $p->{date} = delete $i->{pmt_date};
                     58:        $p->{paid} += delete $i->{paid};
                     59:        push @{ $p->{invoices} }, $id;
                     60: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>