stretch
[freeside.git] / bin / copy-cust_main_county
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use FS::UID qw( adminsuidsetup dbh );
6 use FS::Record qw( qsearch );
7 use FS::cust_main_county;
8
9 adminsuidsetup shift or die "Usage: copy-cust_main_county username\n";
10
11 my $from_state = 'XX'; #XXX
12
13 $FS::UID::AutoCommit = 0;
14
15 my @from_cust_main_county = qsearch({
16   'table'     => 'cust_main_county',
17   'hashref'   => { 'state'   => $from_state,
18                    'country' => 'US',
19                  },
20   'extra_sql' => "AND taxname LIKE 'FED%'",
21 });
22
23 #select distinct state from cust_main_county where country = 'US'
24 #and then exclude the one other state?
25 #my @to_states = ();
26 #no, just find the target records directly.  there should be one per state?
27 my @to_cust_main_county = qsearchs({
28   'table'   => 'cust_main_county',
29   'hashref' => { 'taxclass' => '',
30                  'country'  => 'US',
31                  'taxname'  => '',
32                  'tax'      => 0,
33                },
34 });
35
36 foreach my $to_cust_main_county (@to_cust_main_county) {
37
38   foreach my $from_cust_main_county (@from_cust_main_county) {
39
40     my $new = new FS::cust_main_county {
41                 'state'   => $to_cust_main_county->state,
42                 'country' => 'US',
43                 map { $_ => $from_cust_main_county->$_ }
44                   qw( tax taxclass taxname setuptax recurtax ),
45                  
46               };
47     my $error = $new->insert;
48     if ( $error ) {
49       dbh->rollback;
50       die $error;
51     }
52
53   }
54
55   my $error = $to_cust_main_county->delete;
56   if ( $error ) {
57     dbh->rollback;
58     die $error;
59   }
60
61 }
62
63 dbh->rollback or die dbh->errstr;
64 #dbh->commit or die dbh->errstr;
65
66 1;
67