File: [local] / RT / Invoicing / fix_state (download)
Revision 1.2, Tue Jan 10 03:52:11 2012 UTC (12 years, 6 months ago) by andrew
Branch: MAIN
CVS Tags: HEAD Changes since 1.1: +8 -2 lines
reverse sort payments
|
#!/usr/bin/perl
# $AFresh1: fix_state,v 1.2 2012/01/10 03:52:11 andrew Exp $
use strict;
use warnings;
# Converts a rt_invoices.state v1.38 or earler to a newer file
use YAML::Any qw/ LoadFile DumpFile Dump /;
my $state = LoadFile('rt_invoice.state');
my %payments;
my %invoices;
while (my ($date, $invoices) = each %{ $state->{invoice } }) {
while (my ($id, $invoice) = each %{ $invoices }) {
$invoice->{invdate} ||= $date;
update_payment($id, $invoice) if $invoice->{paid};
$invoices{$id} = $invoice;
}
}
$state->{invoice} = \%invoices;
foreach my $custid (keys %payments) {
my $old = $payments{$custid};
my @new = sort { $b->{date} cmp $a->{date} } @{ $old };
$state->{payment}->{$custid} = \@new;
}
#print Dump $state;
DumpFile('rt_invoice.state', $state);
sub update_payment{
my ($id, $i) = @_;
$payments{$i->{custid}} ||= [];
my $payments = $payments{ $i->{custid} };
my $p = { invoices => [] };
my $new_payment = 1;
if ($i->{check}) {
foreach my $payment (@{ $payments }) {
if ($payment->{number} && $i->{check} == $payment->{number}) {
$new_payment = 0;
$p = $payment;
last;
}
}
$p->{type} = 'check';
$p->{number} = delete $i->{check};
}
if ($new_payment) {
$p->{paid} = 0;
push @{ $payments }, $p;
}
$p->{date} = delete $i->{pmt_date};
$p->{paid} += delete $i->{paid};
push @{ $p->{invoices} }, $id;
}