import torrus 1.0.9
[freeside.git] / bin / follow-tax-rename
1 #!/usr/bin/perl
2
3 use FS::UID qw( adminsuidsetup );
4 use FS::Record qw( qsearch qsearchs );
5 use FS::cust_bill_pkg;
6
7 $FS::Record::nowarn_classload = 1;
8 $FS::Record::nowarn_classload = 1;
9
10 adminsuidsetup shift;
11
12 my $begin = 1231876106;
13
14 my @old = qsearch('h_cust_main_county', {
15   'history_action' => 'replace_old',
16   'history_date'   => { op=>'>=', value=>$begin, },
17 } );
18
19 foreach my $old (@old) {
20
21   my $new = qsearchs('h_cust_main_county', {
22     'history_action' => 'replace_new',
23     'history_date'   => $old->history_date,
24   });
25
26   unless ( $new ) {
27     warn "huh?  no corresponding new record found?";
28     next;
29   }
30
31   my $old_taxname = $old->taxname;
32   my $new_taxname = $new->taxname;
33
34   my @cust_bill_pkg = qsearch('cust_bill_pkg', {
35    'pkgnum'   => 0,
36    'itemdesc' => $old->taxname,
37   });
38
39   next unless @cust_bill_pkg;
40
41   warn 'fixing '. scalar(@cust_bill_pkg).
42        " dangling line items for rename $old_taxname -> $new_taxname\n";
43
44   foreach my $cust_bill_pkg ( @cust_bill_pkg ) {
45
46     $cust_bill_pkg->itemdesc( $new->taxname );
47     my $error = $cust_bill_pkg->replace;
48     die $error if $error;
49
50   }
51
52 }