Optimize "Customer has a referring customer" condition, RT#74452
[freeside.git] / httemplate / misc / process / tax-upgrade.cgi
1 % if ( $error ) {
2 %   warn $error;
3 %   errorpage($error);
4 %  } else {
5     <% include('/elements/header.html','Import successful') %> 
6     <% include('/elements/footer.html') %> 
7 %  }
8 <%init>
9
10 die "access denied"
11   unless $FS::CurrentUser::CurrentUser->access_right('Import');
12
13 my $cfh = $cgi->upload('codefile');
14 my $zfh = $cgi->upload('plus4file');
15 my $tfh = $cgi->upload('txmatrix');
16 my $dfh = $cgi->upload('detail');
17 #warn $cgi;
18 #warn $fh;
19
20 my $oldAutoCommit = $FS::UID::AutoCommit;
21 local $FS::UID::AutoCommit = 0;
22 my $dbh = dbh;
23
24 my $error = '';
25
26 my ($cifh, $cdfh, $zifh, $zdfh, $tifh, $tdfh);
27
28 if (defined($cfh)) {
29   $cifh = new File::Temp( TEMPLATE => 'code.insert.XXXXXXXX',
30                           DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
31                         ) or die "can't open temp file: $!\n";
32
33   $cdfh = new File::Temp( TEMPLATE => 'code.insert.XXXXXXXX',
34                           DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
35                         ) or die "can't open temp file: $!\n";
36
37   while(<$cfh>) {
38     my $fh = '';
39     $fh = $cifh if $_ =~ /"I"\s*$/;
40     $fh = $cdfh if $_ =~ /"D"\s*$/;
41     die "bad input line: $_" unless $fh;
42     print $fh $_;
43   }
44   seek $cifh, 0, 0;
45   seek $cdfh, 0, 0;
46
47 }else{
48   $error = 'No code file';
49 }
50
51 $error ||= FS::tax_class::batch_import( {
52              filehandle => $cifh,
53              'format'   => scalar($cgi->param('format')),
54            } );
55
56 close $cifh if $cifh;
57
58 if (defined($zfh)) {
59   $zifh = new File::Temp( TEMPLATE => 'plus4.insert.XXXXXXXX',
60                           DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
61                         ) or die "can't open temp file: $!\n";
62
63   $zdfh = new File::Temp( TEMPLATE => 'plus4.insert.XXXXXXXX',
64                           DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
65                         ) or die "can't open temp file: $!\n";
66
67   while(<$zfh>) {
68     my $fh = '';
69     $fh = $zifh if $_ =~ /"I"\s*$/;
70     $fh = $zdfh if $_ =~ /"D"\s*$/;
71     die "bad input line: $_" unless $fh;
72     print $fh $_;
73   }
74   seek $zifh, 0, 0;
75   seek $zdfh, 0, 0;
76
77 }else{
78   $error = 'No plus4 file';
79 }
80
81 $error ||= FS::cust_tax_location::batch_import( {
82              filehandle => $zifh,
83              'format'   => scalar($cgi->param('format')),
84            } );
85 close $zifh if $zifh;
86
87 if (defined($tfh)) {
88   $tifh = new File::Temp( TEMPLATE => 'txmatrix.insert.XXXXXXXX',
89                           DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
90                         ) or die "can't open temp file: $!\n";
91
92   $tdfh = new File::Temp( TEMPLATE => 'txmatrix.insert.XXXXXXXX',
93                           DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
94                         ) or die "can't open temp file: $!\n";
95
96   while(<$tfh>) {
97     my $fh = '';
98     $fh = $tifh if $_ =~ /"I"\s*$/;
99     $fh = $tdfh if $_ =~ /"D"\s*$/;
100     die "bad input line: $_" unless $fh;
101     print $fh $_;
102   }
103   seek $tifh, 0, 0;
104   seek $tdfh, 0, 0;
105
106 }else{
107   $error = 'No tax matrix file';
108 }
109
110 $error ||= FS::part_pkg_taxrate::batch_import( {
111              filehandle => $tifh,
112              'format'   => scalar($cgi->param('format')),
113            } );
114 close $tifh if $tifh;
115
116 $error ||= defined($dfh)
117   ? FS::tax_rate::batch_update( {
118       filehandle => $dfh,
119       'format'   => scalar($cgi->param('format')),
120     } )
121   : 'No tax detail file';
122
123 $error ||= FS::part_pkg_taxrate::batch_import( {
124              filehandle => $tdfh,
125              'format'   => scalar($cgi->param('format')),
126            } );
127 close $tdfh if $tdfh;
128
129 $error ||= FS::cust_tax_location::batch_import( {
130              filehandle => $zdfh,
131              'format'   => scalar($cgi->param('format')),
132            } );
133 close $zdfh if $zdfh;
134
135 $error ||= FS::tax_class::batch_import( {
136              filehandle => $cdfh,
137              'format'   => scalar($cgi->param('format')),
138            } );
139 close $cdfh if $cdfh;
140
141 if ($error) {
142   $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
143 }else{
144   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
145 }
146
147 </%init>