1 package FS::cust_main::Import_Charges;
2 #actually no specific reason it lives under FS::cust_main:: othan than it calls
3 # a thing on cust_main objects. not part of the inheritence, just providess a
4 # subroutine for misc/process/cust_main-import_charges.cgi
10 use FS::Record qw( qsearchs );
15 FS::cust_main::Import_Charges - Batch charge importing
19 use FS::cust_main::Import_Charges;
22 FS::cust_main::Import_charges::batch_charge( {
24 'agentnum' => scalar($cgi->param('agentnum')),
25 'format' => scalar($cgi->param('format')),
30 Batch customer charging.
43 #warn join('-',keys %$param);
44 my $fh = $param->{filehandle};
45 my $agentnum = $param->{agentnum};
46 my $format = $param->{format};
48 my $extra_sql = ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
51 if ( $format eq 'simple' ) {
52 @fields = qw( custnum agent_custid amount pkg );
54 die "unknown format $format";
57 my $csv = new Text::CSV_XS;
64 local $SIG{HUP} = 'IGNORE';
65 local $SIG{INT} = 'IGNORE';
66 local $SIG{QUIT} = 'IGNORE';
67 local $SIG{TERM} = 'IGNORE';
68 local $SIG{TSTP} = 'IGNORE';
69 local $SIG{PIPE} = 'IGNORE';
71 my $oldAutoCommit = $FS::UID::AutoCommit;
72 local $FS::UID::AutoCommit = 0;
75 #while ( $columns = $csv->getline($fh) ) {
77 while ( defined($line=<$fh>) ) {
79 $csv->parse($line) or do {
80 $dbh->rollback if $oldAutoCommit;
81 return "can't parse: ". $csv->error_input();
84 my @columns = $csv->fields();
85 #warn join('-',@columns);
88 foreach my $field ( @fields ) {
89 $row{$field} = shift @columns;
92 if ( $row{custnum} && $row{agent_custid} ) {
93 dbh->rollback if $oldAutoCommit;
94 return "can't specify custnum with agent_custid $row{agent_custid}";
98 if ( $row{agent_custid} && $agentnum ) {
99 %hash = ( 'agent_custid' => $row{agent_custid},
100 'agentnum' => $agentnum,
104 if ( $row{custnum} ) {
105 %hash = ( 'custnum' => $row{custnum} );
108 unless ( scalar(keys %hash) ) {
109 $dbh->rollback if $oldAutoCommit;
110 return "can't find customer without custnum or agent_custid and agentnum";
113 my $cust_main = qsearchs('cust_main', { %hash } );
114 unless ( $cust_main ) {
115 $dbh->rollback if $oldAutoCommit;
116 my $custnum = $row{custnum} || $row{agent_custid};
117 return "unknown custnum $custnum";
120 if ( $row{'amount'} > 0 ) {
121 my $error = $cust_main->charge($row{'amount'}, $row{'pkg'});
123 $dbh->rollback if $oldAutoCommit;
127 } elsif ( $row{'amount'} < 0 ) {
128 my $error = $cust_main->credit( sprintf( "%.2f", 0-$row{'amount'} ),
131 $dbh->rollback if $oldAutoCommit;
141 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
143 return "Empty file!" unless $imported;