1 package FS::cust_main::Import;
4 use vars qw( $DEBUG $conf );
6 use File::Slurp qw( slurp );
7 use FS::Misc::DateTime qw( parse_datetime );
9 use FS::Record qw( qsearchs );
15 use FS::part_referral;
19 install_callback FS::UID sub {
23 my %is_location = map { $_ => 1 } FS::cust_main::Location->location_fields;
27 FS::cust_main::Import - Batch customer importing
31 use FS::cust_main::Import;
34 FS::cust_main::Import::batch_import( {
35 file => $file, #filename
36 type => $type, #csv or xls
37 format => $format, #extended, extended-plus_company, svc_external,
38 #extended-plus_company_and_options
39 #extended-plus_options, or svc_external_svc_phone
40 agentnum => $agentnum,
43 job => $job, #optional job queue job, for progressbar updates
44 custbatch => $custbatch, #optional batch unique identifier
49 use FS::UI::Web::JSRPC;
51 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
52 print $server->process;
56 Batch customer importing.
60 =item process_batch_import
62 Load a batch import as a queued JSRPC job
66 sub process_batch_import {
69 warn Dumper($param) if $DEBUG;
71 my $files = $param->{'uploaded_files'}
72 or die "No files provided.\n";
74 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
76 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
77 my $file = $dir. $files{'file'};
80 if ( $file =~ /\.(\w+)$/i ) {
84 warn "can't parse file type from filename $file; defaulting to CSV";
89 FS::cust_main::Import::batch_import( {
93 custbatch => $param->{custbatch},
94 agentnum => $param->{'agentnum'},
95 refnum => $param->{'refnum'},
96 pkgpart => $param->{'pkgpart'},
97 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
98 # city state zip comments )],
99 'format' => $param->{'format'},
104 die "$error\n" if $error;
113 #some false laziness w/cdr.pm now
117 my $job = $param->{job};
119 my $filename = $param->{file};
120 my $type = $param->{type} || 'csv';
122 my $custbatch = $param->{custbatch};
124 my $agentnum = $param->{agentnum};
125 my $refnum = $param->{refnum};
126 my $pkgpart = $param->{pkgpart};
128 my $format = $param->{'format'};
132 if ( $format eq 'simple' ) {
133 @fields = qw( cust_pkg.setup dayphone first last
134 address1 address2 city state zip comments );
136 } elsif ( $format eq 'extended' ) {
137 @fields = qw( agent_custid refnum
138 last first address1 address2 city state zip country
140 ship_last ship_first ship_address1 ship_address2
141 ship_city ship_state ship_zip ship_country
142 payinfo paycvv paydate
145 svc_acct.username svc_acct._password
148 } elsif ( $format eq 'extended-plus_options' ) {
149 @fields = qw( agent_custid refnum
150 last first address1 address2 city state zip country
152 ship_last ship_first ship_address1 ship_address2
153 ship_city ship_state ship_zip ship_country
154 payinfo paycvv paydate
157 svc_acct.username svc_acct._password
161 } elsif ( $format eq 'extended-plus_company' ) {
162 @fields = qw( agent_custid refnum
163 last first company address1 address2 city state zip country
165 ship_last ship_first ship_company ship_address1 ship_address2
166 ship_city ship_state ship_zip ship_country
167 payinfo paycvv paydate
170 svc_acct.username svc_acct._password
173 } elsif ( $format eq 'extended-plus_company_and_options' ) {
174 @fields = qw( agent_custid refnum
175 last first company address1 address2 city state zip country
177 ship_last ship_first ship_company ship_address1 ship_address2
178 ship_city ship_state ship_zip ship_country
179 payinfo paycvv paydate
182 svc_acct.username svc_acct._password
186 } elsif ( $format =~ /^svc_external/ ) {
187 @fields = qw( agent_custid refnum
188 last first company address1 address2 city state zip country
190 ship_last ship_first ship_company ship_address1 ship_address2
191 ship_city ship_state ship_zip ship_country
192 payinfo paycvv paydate
194 cust_pkg.pkgpart cust_pkg.bill
195 svc_external.id svc_external.title
197 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
198 if $format eq 'svc_external_svc_phone';
200 } elsif ( $format eq 'birthdates-acct_phone_hardware') {
201 @fields = qw( agent_custid refnum
202 last first company address1 address2 city state zip country
204 ship_last ship_first ship_company ship_address1 ship_address2
205 ship_city ship_state ship_zip ship_country
206 birthdate spouse_birthdate
207 payinfo paycvv paydate
209 cust_pkg.pkgpart cust_pkg.bill
210 svc_acct.username svc_acct._password
212 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
213 push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
216 } elsif ( $format eq 'national_id-acct_phone') {
217 @fields = qw( agent_custid refnum
218 last first company address1 address2 city state zip country
220 ship_last ship_first ship_company ship_address1 ship_address2
221 ship_city ship_state ship_zip ship_country
223 payinfo paycvv paydate
225 cust_pkg.pkgpart cust_pkg.bill
226 svc_acct.username svc_acct._password svc_acct.slipip
228 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
232 die "unknown format $format";
238 if ( $type eq 'csv' ) {
240 eval "use Text::CSV_XS;";
243 $parser = new Text::CSV_XS;
245 @buffer = split(/\r?\n/, slurp($filename) );
246 $count = scalar(@buffer);
248 } elsif ( $type eq 'xls' ) {
250 eval "use Spreadsheet::ParseExcel;";
253 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
254 $parser = $excel->{Worksheet}[0]; #first sheet
256 $count = $parser->{MaxRow} || $parser->{MinRow};
260 die "Unknown file type $type\n";
265 local $SIG{HUP} = 'IGNORE';
266 local $SIG{INT} = 'IGNORE';
267 local $SIG{QUIT} = 'IGNORE';
268 local $SIG{TERM} = 'IGNORE';
269 local $SIG{TSTP} = 'IGNORE';
270 local $SIG{PIPE} = 'IGNORE';
272 my $oldAutoCommit = $FS::UID::AutoCommit;
273 local $FS::UID::AutoCommit = 0;
276 #implies ignore_expired_card
277 local($FS::cust_main::import) = 1;
278 local($FS::cust_main::import) = 1;
282 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
286 if ( $type eq 'csv' ) {
288 last unless scalar(@buffer);
289 $line = shift(@buffer);
291 $parser->parse($line) or do {
292 $dbh->rollback if $oldAutoCommit;
293 return "can't parse: ". $parser->error_input();
295 @columns = $parser->fields();
297 } elsif ( $type eq 'xls' ) {
299 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
300 || ! $parser->{Cells}[$row];
302 my @row = @{ $parser->{Cells}[$row] };
303 @columns = map $_->{Val}, @row;
306 #warn $z++. ": $_\n" for @columns;
309 die "Unknown file type $type\n";
312 #warn join('-',@columns);
315 custbatch => $custbatch,
316 agentnum => $agentnum,
318 payby => $payby, #default
319 paydate => '12/2037', #default
322 my %cust_pkg = ( pkgpart => $pkgpart );
324 my %bill_location = ();
325 my %ship_location = ();
326 foreach my $field ( @fields ) {
328 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
330 #$cust_pkg{$1} = parse_datetime( shift @$columns );
331 if ( $1 eq 'pkgpart' ) {
332 $cust_pkg{$1} = shift @columns;
333 } elsif ( $1 eq 'setup' ) {
334 $billtime = parse_datetime(shift @columns);
336 $cust_pkg{$1} = parse_datetime( shift @columns );
339 } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
341 $svc_x{$1} = shift @columns;
343 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
345 $svc_x{$1} = shift @columns;
347 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
348 $svc_x{$1} = shift @columns;
350 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
352 $svc_x{$1} = shift @columns;
354 } elsif ( $is_location{$field} ) {
356 $bill_location{$field} = shift @columns;
358 } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
360 $ship_location{$1} = shift @columns;
365 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
367 my $referral = $columns[0];
368 my %hash = ( 'referral' => $referral,
369 'agentnum' => $agentnum,
373 my $part_referral = qsearchs('part_referral', \%hash )
374 || new FS::part_referral \%hash;
376 unless ( $part_referral->refnum ) {
377 my $error = $part_referral->insert;
379 $dbh->rollback if $oldAutoCommit;
380 return "can't auto-insert advertising source: $referral: $error";
384 $columns[0] = $part_referral->refnum;
387 my $value = shift @columns;
388 $cust_main{$field} = $value if length($value);
390 } # foreach my $field
391 # finished importing columns
393 $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
394 $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
395 if ( grep $_, values(%ship_location) ) {
396 $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
397 $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
399 $cust_main{'ship_location'} = $cust_main{'bill_location'};
402 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
403 $cust_main{'payby'} = 'CARD';
404 if ($cust_main{'payinfo'} =~ /\s*([AD]?)(.*)\s*$/) {
405 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
406 $cust_main{'payinfo'} = $2;
410 $cust_main{$_} = parse_datetime($cust_main{$_})
411 foreach grep $cust_main{$_},
412 qw( birthdate spouse_birthdate anniversary_date );
414 my $invoicing_list = $cust_main{'invoicing_list'}
415 ? [ delete $cust_main{'invoicing_list'} ]
418 my $customer_options = delete $cust_main{customer_options};
419 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
420 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
422 my $cust_main = new FS::cust_main ( \%cust_main );
425 tie my %hash, 'Tie::RefHash'; #this part is important
427 if ( $cust_pkg{'pkgpart'} ) {
429 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
430 $dbh->rollback if $oldAutoCommit;
431 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
434 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
438 if ( $svc_x{'username'} ) {
440 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
441 $svcdb = 'svc_external';
445 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
446 $svc_phone = FS::svc_phone->new( {
447 map { $_ => delete($svc_x{$_}) }
448 qw( countrycode phonenum sip_password pin )
452 my $svc_hardware = '';
453 if ( $svc_x{'typenum'} ) {
454 $svc_hardware = FS::svc_hardware->new( {
455 map { $_ => delete($svc_x{$_}) }
456 qw( typenum ip_addr hw_addr serial )
460 if ( $svcdb || $svc_phone || $svc_hardware ) {
461 my $part_pkg = $cust_pkg->part_pkg;
462 unless ( $part_pkg ) {
463 $dbh->rollback if $oldAutoCommit;
464 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
467 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
468 my $class = "FS::$svcdb";
469 push @svc_x, $class->new( \%svc_x );
472 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
473 push @svc_x, $svc_phone;
475 if ( $svc_hardware ) {
476 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
477 push @svc_x, $svc_hardware;
482 $hash{$cust_pkg} = \@svc_x;
485 my $error = $cust_main->insert( \%hash, $invoicing_list );
488 $dbh->rollback if $oldAutoCommit;
489 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
492 if ( $format eq 'simple' ) {
494 #false laziness w/bill.cgi
495 $error = $cust_main->bill( 'time' => $billtime );
497 $dbh->rollback if $oldAutoCommit;
498 return "can't bill customer for $line: $error";
501 $error = $cust_main->apply_payments_and_credits;
503 $dbh->rollback if $oldAutoCommit;
504 return "can't bill customer for $line: $error";
507 $error = $cust_main->collect();
509 $dbh->rollback if $oldAutoCommit;
510 return "can't collect customer for $line: $error";
517 if ( $job && time - $min_sec > $last ) { #progress bar
518 $job->update_statustext( int(100 * $row / $count) );
524 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
526 return "Empty file!" unless $row;
534 Not enough documentation.
538 L<FS::cust_main>, L<FS::cust_pkg>,
539 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>