summaryrefslogtreecommitdiff
path: root/httemplate/misc/process/tax-upgrade.cgi
blob: 8782282bda3d607079faea9afd4bc103e2866999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
% if ( $error ) {
%   warn $error;
%   errorpage($error);
%  } else {
    <% include('/elements/header.html','Import successful') %> 
    <% include('/elements/footer.html') %> 
%  }
<%init>

die "access denied"
  unless $FS::CurrentUser::CurrentUser->access_right('Import');

my $cfh = $cgi->upload('codefile');
my $zfh = $cgi->upload('plus4file');
my $tfh = $cgi->upload('txmatrix');
my $dfh = $cgi->upload('detail');
#warn $cgi;
#warn $fh;

my $oldAutoCommit = $FS::UID::AutoCommit;
local $FS::UID::AutoCommit = 0;
my $dbh = dbh;

my $error = '';

my ($cifh, $cdfh, $zifh, $zdfh, $tifh, $tdfh);

if (defined($cfh)) {
  $cifh = new File::Temp( TEMPLATE => 'code.insert.XXXXXXXX',
                          DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
                        ) or die "can't open temp file: $!\n";

  $cdfh = new File::Temp( TEMPLATE => 'code.insert.XXXXXXXX',
                          DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
                        ) or die "can't open temp file: $!\n";

  while(<$cfh>) {
    my $fh = '';
    $fh = $cifh if $_ =~ /"I"\s*$/;
    $fh = $cdfh if $_ =~ /"D"\s*$/;
    die "bad input line: $_" unless $fh;
    print $fh $_;
  }
  seek $cifh, 0, 0;
  seek $cdfh, 0, 0;

}else{
  $error = 'No code file';
}

$error ||= FS::tax_class::batch_import( {
             filehandle => $cifh,
             'format'   => scalar($cgi->param('format')),
           } );

close $cifh if $cifh;

if (defined($zfh)) {
  $zifh = new File::Temp( TEMPLATE => 'plus4.insert.XXXXXXXX',
                          DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
                        ) or die "can't open temp file: $!\n";

  $zdfh = new File::Temp( TEMPLATE => 'plus4.insert.XXXXXXXX',
                          DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
                        ) or die "can't open temp file: $!\n";

  while(<$zfh>) {
    my $fh = '';
    $fh = $zifh if $_ =~ /"I"\s*$/;
    $fh = $zdfh if $_ =~ /"D"\s*$/;
    die "bad input line: $_" unless $fh;
    print $fh $_;
  }
  seek $zifh, 0, 0;
  seek $zdfh, 0, 0;

}else{
  $error = 'No plus4 file';
}

$error ||= FS::cust_tax_location::batch_import( {
             filehandle => $zifh,
             'format'   => scalar($cgi->param('format')),
           } );
close $zifh if $zifh;

if (defined($tfh)) {
  $tifh = new File::Temp( TEMPLATE => 'txmatrix.insert.XXXXXXXX',
                          DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
                        ) or die "can't open temp file: $!\n";

  $tdfh = new File::Temp( TEMPLATE => 'txmatrix.insert.XXXXXXXX',
                          DIR      => $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc,
                        ) or die "can't open temp file: $!\n";

  while(<$tfh>) {
    my $fh = '';
    $fh = $tifh if $_ =~ /"I"\s*$/;
    $fh = $tdfh if $_ =~ /"D"\s*$/;
    die "bad input line: $_" unless $fh;
    print $fh $_;
  }
  seek $tifh, 0, 0;
  seek $tdfh, 0, 0;

}else{
  $error = 'No tax matrix file';
}

$error ||= FS::part_pkg_taxrate::batch_import( {
             filehandle => $tifh,
             'format'   => scalar($cgi->param('format')),
           } );
close $tifh if $tifh;

$error ||= defined($dfh)
  ? FS::tax_rate::batch_update( {
      filehandle => $dfh,
      'format'   => scalar($cgi->param('format')),
    } )
  : 'No tax detail file';

$error ||= FS::part_pkg_taxrate::batch_import( {
             filehandle => $tdfh,
             'format'   => scalar($cgi->param('format')),
           } );
close $tdfh if $tdfh;

$error ||= FS::cust_tax_location::batch_import( {
             filehandle => $zdfh,
             'format'   => scalar($cgi->param('format')),
           } );
close $zdfh if $zdfh;

$error ||= FS::tax_class::batch_import( {
             filehandle => $cdfh,
             'format'   => scalar($cgi->param('format')),
           } );
close $cdfh if $cdfh;

if ($error) {
  $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
}else{
  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
}

</%init>