#!/usr/bin/perl # $AFresh1: fix_state,v 1.1 2011/12/21 02:21:54 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; $state->{payment} = \%payments; #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; }