summaryrefslogtreecommitdiff
path: root/bin/tax_rate_location.import
blob: 439d27cc90ab685c62b738457401cc857187a76b (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
#!/usr/bin/perl -Tw

use strict;
use vars qw($opt_g $opt_f);
use vars qw($DEBUG);
use Getopt::Std;
use FS::UID qw(adminsuidsetup);
use FS::Conf;
use FS::tax_rate_location;

getopts('f:g:');

my $user = shift or die &usage;
my $dbh = adminsuidsetup $user;

my ($format) = $opt_f =~ /^([-\w]+)$/;

my @list = (
  'GEOCODE',      $opt_g, \&FS::tax_rate_location::batch_import,
);

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

my $error = '';

while(@list) {
  my ($name, $file, $method) = splice(@list, 0, 3);

  my $fh;

  $file =~ /^([\s\d\w.]+)$/ or die "Illegal filename: $file\n";
  $file = $1;

  open $fh, '<', $file or die "can't open $name file: $!\n";
  $error ||= &{$method}( { filehandle => $fh, 'format' => $format, } );

  die "error while processing $file: $error" if $error;
  close $fh;
}

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

sub usage { die "Usage:\ntax_rates_location.import -f FORMAT -g GEOCODEFILE user\n\n"; }