6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearch qsearchs dbh);
10 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
12 print "started time=".time."\n";
14 # INSTRUCTIONS: save the spreadsheet as CSV (in ASCII), set the
15 # below variables, and run this script, passing in a fs username as an arg.
18 my $file = '/home/levinse/prepaidcards.csv';
26 local $SIG{HUP} = 'IGNORE';
27 local $SIG{INT} = 'IGNORE';
28 local $SIG{QUIT} = 'IGNORE';
29 local $SIG{TERM} = 'IGNORE';
30 local $SIG{TSTP} = 'IGNORE';
31 local $SIG{PIPE} = 'IGNORE';
33 my $oldAutoCommit = $FS::UID::AutoCommit;
34 local $FS::UID::AutoCommit = 0;
44 $str =~ s/^\s+|\s+$//g;
52 my $cardnum = trim($columns[0]);
54 my $prepay_credit = new FS::prepay_credit { 'identifier' => $cardnum,
57 my $error = $prepay_credit->insert;
58 fatal("error inserting card: $error") if $error;
60 warn "Pass $linenum\n" if $debug;
62 print "Done $linenum time=$time\n" if ($linenum % 1000 == 0);
67 $dbh->rollback if $oldAutoCommit;
71 my $csv = new Text::CSV;
72 open (CSV, "<", $file) or die $!;
73 print "Starting main loop time=".time."\n";
75 if ( $linenum == 1 ) { # skip header
80 if( $skipto > $linenum ) { # debug stuff
85 last if $limit > 0 && $limit <= $linenum;
87 # kept getting these errors for many lines:
88 # "EIQ - Binary character inside quoted field, binary off"
89 $_ =~ s/[^[:ascii:]]//g;
91 if ($csv->parse($_)) {
92 my @columns = $csv->fields();
93 suffer($linenum,@columns);
95 my $err = $csv->error_diag . "(" . $csv->error_input . ")";
96 print "WARNING: failed to parse line $linenum: " . $csv->error_diag
97 . " (" . $csv->error_input . ")\n";
103 fatal("COMMIT ABORTED DUE TO DRY RUN BEING ON") if $dry;
104 $dbh->commit or die $dbh->errstr if $oldAutoCommit;