communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[freeside.git] / bin / import-tax-rates
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use vars qw($opt_c $opt_p $opt_t $opt_d $opt_z $opt_f);
5 use vars qw($DEBUG);
6 use Getopt::Std;
7 use FS::UID qw(adminsuidsetup);
8 use FS::Conf;
9 use FS::tax_rate;
10 use FS::cust_tax_location;
11
12 getopts('c:p:t:d:z:f:');
13
14 my $user = shift or die &usage;
15 my $dbh = adminsuidsetup $user;
16
17 my ($format) = $opt_f =~ /^([-\w]+)$/;
18
19 my @list = (
20   'CODE',     $opt_c, \&FS::tax_class::batch_import,
21   'PLUS4',    $opt_p, \&FS::cust_tax_location::batch_import,
22   'ZIP',      $opt_z, \&FS::cust_tax_location::batch_import,
23   'TXMATRIX', $opt_t, \&FS::part_pkg_taxrate::batch_import,
24   'DETAIL',   $opt_d, \&FS::tax_rate::batch_import,
25 );
26
27 my $oldAutoCommit = $FS::UID::AutoCommit;
28 local $FS::UID::AutoCommit = 0;
29
30 my $error = '';
31
32 while(@list) {
33   my ($name, $file, $method) = splice(@list, 0, 3);
34
35   my $fh;
36
37   $file =~ /^([\s\d\w.]+)$/ or die "Illegal filename: $file\n";
38   $file = $1;
39
40   my $f = $format;
41   $f .= '-zip' if $name eq 'ZIP';
42
43   open $fh, '<', $file or die "can't open $name file: $!\n";
44   $error ||= &{$method}( { filehandle => $fh, 'format' => $f, } );
45
46   die "error while processing $file: $error" if $error;
47   close $fh;
48 }
49
50 if ($error) {
51   $dbh->rollback or die $dbh->errstr if $oldAutoCommit;
52 }else{
53   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
54 }
55
56 sub usage { die "Usage:\nimport-tax-rates f FORMAT -c CODEFILE -p PLUS4FILE -z ZIPFILE -t TXMATRIXFILE -d DETAILFILE  user\n\n"; }