1 package FS::cust_main::Import;
4 use vars qw( $DEBUG $conf );
8 use File::Slurp qw( slurp );
9 use FS::Misc::DateTime qw( parse_datetime );
10 use FS::UID qw( dbh );
11 use FS::Record qw( qsearchs );
14 use FS::svc_broadband;
18 use FS::part_referral;
19 use Business::CreditCard 0.35;
23 install_callback FS::UID sub {
27 my %is_location = map { $_ => 1 } FS::cust_main::Location->location_fields;
31 FS::cust_main::Import - Batch customer importing
35 use FS::cust_main::Import;
38 FS::cust_main::Import::batch_import( {
39 file => $file, #filename
40 type => $type, #csv or xls
41 format => $format, #extended, extended-plus_company, svc_external,
42 #extended-plus_company_and_options
43 #extended-plus_options, or svc_external_svc_phone
44 agentnum => $agentnum,
47 job => $job, #optional job queue job, for progressbar updates
48 custbatch => $custbatch, #optional batch unique identifier
53 use FS::UI::Web::JSRPC;
55 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
56 print $server->process;
60 Batch customer importing.
64 =item process_batch_import
66 Load a batch import as a queued JSRPC job
70 sub process_batch_import {
73 my $param = thaw(decode_base64(shift));
74 warn Dumper($param) if $DEBUG;
76 my $files = $param->{'uploaded_files'}
77 or die "No files provided.\n";
79 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
81 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
82 my $file = $dir. $files{'file'};
85 if ( $file =~ /\.(\w+)$/i ) {
89 warn "can't parse file type from filename $file; defaulting to CSV";
94 FS::cust_main::Import::batch_import( {
98 custbatch => $param->{custbatch},
99 agentnum => $param->{'agentnum'},
100 refnum => $param->{'refnum'},
101 pkgpart => $param->{'pkgpart'},
102 validate_cc => $param->{'validate_cc'},
103 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
104 # city state zip comments )],
105 'format' => $param->{'format'},
110 die "$error\n" if $error;
119 #some false laziness w/cdr.pm now
123 my $job = $param->{job};
125 my $filename = $param->{file};
126 my $type = $param->{type} || 'csv';
128 my $custbatch = $param->{custbatch};
130 my $agentnum = $param->{agentnum};
131 my $refnum = $param->{refnum};
132 my $pkgpart = $param->{pkgpart};
133 my $validate_cc = $param->{validate_cc};
135 my $format = $param->{'format'};
139 if ( $format eq 'simple' ) {
140 @fields = qw( cust_pkg.setup dayphone first last
141 address1 address2 city state zip comments );
143 } elsif ( $format eq 'extended' ) {
144 @fields = qw( agent_custid refnum
145 last first address1 address2 city state zip country
147 ship_last ship_first ship_address1 ship_address2
148 ship_city ship_state ship_zip ship_country
149 payinfo paycvv paydate
152 svc_acct.username svc_acct._password
155 } elsif ( $format eq 'extended-plus_options' ) {
156 @fields = qw( agent_custid refnum
157 last first address1 address2 city state zip country
159 ship_last ship_first ship_address1 ship_address2
160 ship_city ship_state ship_zip ship_country
161 payinfo paycvv paydate
164 svc_acct.username svc_acct._password
168 } elsif ( $format eq 'extended-plus_company' ) {
169 @fields = qw( agent_custid refnum
170 last first company address1 address2 city state zip country
172 ship_last ship_first ship_company ship_address1 ship_address2
173 ship_city ship_state ship_zip ship_country
174 payinfo paycvv paydate
177 svc_acct.username svc_acct._password
180 } elsif ( $format eq 'extended-plus_company_and_options' ) {
181 @fields = qw( agent_custid refnum
182 last first company address1 address2 city state zip country
184 ship_last ship_first ship_company ship_address1 ship_address2
185 ship_city ship_state ship_zip ship_country
186 payinfo paycvv paydate
189 svc_acct.username svc_acct._password
193 } elsif ( $format =~ /^svc_broadband/ ) {
194 @fields = qw( agent_custid refnum
195 last first company address1 address2 city state zip country
197 ship_last ship_first ship_company ship_address1 ship_address2
198 ship_city ship_state ship_zip ship_country
199 payinfo paycvv paydate
201 cust_pkg.pkgpart cust_pkg.bill
203 push @fields, map "svc_broadband.$_", qw( ip_addr mac_addr sectornum );
205 } elsif ( $format =~ /^svc_external/ ) {
206 @fields = qw( agent_custid refnum
207 last first company address1 address2 city state zip country
209 ship_last ship_first ship_company ship_address1 ship_address2
210 ship_city ship_state ship_zip ship_country
211 payinfo paycvv paydate
213 cust_pkg.pkgpart cust_pkg.bill
214 svc_external.id svc_external.title
216 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
217 if $format eq 'svc_external_svc_phone';
219 } elsif ( $format eq 'birthdates-acct_phone_hardware') {
220 @fields = qw( agent_custid refnum
221 last first company address1 address2 city state zip country
223 ship_last ship_first ship_company ship_address1 ship_address2
224 ship_city ship_state ship_zip ship_country
225 birthdate spouse_birthdate
226 payinfo paycvv paydate
228 cust_pkg.pkgpart cust_pkg.bill
229 svc_acct.username svc_acct._password
231 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
232 push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
235 } elsif ( $format eq 'national_id-acct_phone') {
236 @fields = qw( agent_custid refnum
237 last first company address1 address2 city state zip country
239 ship_last ship_first ship_company ship_address1 ship_address2
240 ship_city ship_state ship_zip ship_country
242 payinfo paycvv paydate
244 cust_pkg.pkgpart cust_pkg.bill
245 svc_acct.username svc_acct._password svc_acct.slipip
247 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
251 die "unknown format $format";
257 if ( $type eq 'csv' ) {
259 eval "use Text::CSV_XS;";
262 $parser = new Text::CSV_XS;
264 @buffer = split(/\r?\n/, slurp($filename) );
265 $count = scalar(@buffer);
267 } elsif ( $type eq 'xls' ) {
269 eval "use Spreadsheet::ParseExcel;";
272 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
273 $parser = $excel->{Worksheet}[0]; #first sheet
275 $count = $parser->{MaxRow} || $parser->{MinRow};
279 die "Unknown file type $type\n";
284 local $SIG{HUP} = 'IGNORE';
285 local $SIG{INT} = 'IGNORE';
286 local $SIG{QUIT} = 'IGNORE';
287 local $SIG{TERM} = 'IGNORE';
288 local $SIG{TSTP} = 'IGNORE';
289 local $SIG{PIPE} = 'IGNORE';
291 my $oldAutoCommit = $FS::UID::AutoCommit;
292 local $FS::UID::AutoCommit = 0;
295 #implies ignore_expired_card
296 local($FS::cust_main::import) = 1;
297 local($FS::cust_main::import) = 1;
301 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
305 if ( $type eq 'csv' ) {
307 last unless scalar(@buffer);
308 $line = shift(@buffer);
310 $parser->parse($line) or do {
311 $dbh->rollback if $oldAutoCommit;
312 return "can't parse: ". $parser->error_input();
314 @columns = $parser->fields();
316 } elsif ( $type eq 'xls' ) {
318 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
319 || ! $parser->{Cells}[$row];
321 my @row = @{ $parser->{Cells}[$row] };
322 @columns = map $_->{Val}, @row;
325 #warn $z++. ": $_\n" for @columns;
328 die "Unknown file type $type\n";
331 #warn join('-',@columns);
334 custbatch => $custbatch,
335 agentnum => $agentnum,
337 payby => $payby, #default
338 paydate => '12/2037', #default
341 my %cust_pkg = ( pkgpart => $pkgpart );
343 my %bill_location = ();
344 my %ship_location = ();
345 foreach my $field ( @fields ) {
347 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
349 #$cust_pkg{$1} = parse_datetime( shift @$columns );
350 if ( $1 eq 'pkgpart' ) {
351 $cust_pkg{$1} = shift @columns;
352 } elsif ( $1 eq 'setup' ) {
353 $billtime = parse_datetime(shift @columns);
355 $cust_pkg{$1} = parse_datetime( shift @columns );
358 } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
360 $svc_x{$1} = shift @columns;
362 } elsif ( $field =~ /^svc_broadband\.(ip_addr|mac_addr|sectornum)$/ ) {
364 $svc_x{$1} = shift @columns;
366 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
368 $svc_x{$1} = shift @columns;
370 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
371 $svc_x{$1} = shift @columns;
373 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
375 $svc_x{$1} = shift @columns;
377 } elsif ( $is_location{$field} ) {
379 $bill_location{$field} = shift @columns;
381 } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
383 $ship_location{$1} = shift @columns;
388 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
390 my $referral = $columns[0];
391 my %hash = ( 'referral' => $referral,
392 'agentnum' => $agentnum,
396 my $part_referral = qsearchs('part_referral', \%hash )
397 || new FS::part_referral \%hash;
399 unless ( $part_referral->refnum ) {
400 my $error = $part_referral->insert;
402 $dbh->rollback if $oldAutoCommit;
403 return "can't auto-insert advertising source: $referral: $error";
407 $columns[0] = $part_referral->refnum;
410 my $value = shift @columns;
411 $cust_main{$field} = $value if length($value);
413 } # foreach my $field
414 # finished importing columns
416 $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
417 $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
418 if ( grep $_, values(%ship_location) ) {
419 $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
420 $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
422 $cust_main{'ship_location'} = $cust_main{'bill_location'};
425 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
427 if ( $cust_main{'payinfo'} =~ /^\s*(\d+\@[\d\.]+)\s*$/ ) {
429 $cust_main{'payby'} = 'CHEK';
430 $cust_main{'payinfo'} = $1;
434 $cust_main{'payby'} = 'CARD';
436 if ($cust_main{'payinfo'} =~ /^\s*([AD]?)(.*)\s*$/) {
438 ## validate credit card if requested
441 or return "Invalid card($2) for customer ".$cust_main{'first'}." ".$cust_main{'last'};
442 return "Unknown card type for customer ".$cust_main{'first'}." ".$cust_main{'last'}
443 if cardtype($2) eq "Unknown";
446 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
447 $cust_main{'payinfo'} = $2;
454 $cust_main{$_} = parse_datetime($cust_main{$_})
455 foreach grep $cust_main{$_},
456 qw( birthdate spouse_birthdate anniversary_date );
458 my $invoicing_list = $cust_main{'invoicing_list'}
459 ? [ delete $cust_main{'invoicing_list'} ]
462 my $customer_options = delete $cust_main{customer_options};
463 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
464 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
466 my $cust_main = new FS::cust_main ( \%cust_main );
469 tie my %hash, 'Tie::RefHash'; #this part is important
471 if ( $cust_pkg{'pkgpart'} ) {
473 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
474 $dbh->rollback if $oldAutoCommit;
475 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
478 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
482 if ( $svc_x{'username'} ) {
484 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
485 $svcdb = 'svc_external';
486 } elsif ( $svc_x{ip_addr} || $svc_x{mac_addr} ) {
487 $svcdb = 'svc_broadband';
491 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
492 $svc_phone = FS::svc_phone->new( {
493 map { $_ => delete($svc_x{$_}) }
494 qw( countrycode phonenum sip_password pin )
498 my $svc_hardware = '';
499 if ( $svc_x{'typenum'} ) {
500 $svc_hardware = FS::svc_hardware->new( {
501 map { $_ => delete($svc_x{$_}) }
502 qw( typenum ip_addr hw_addr serial )
506 if ( $svcdb || $svc_phone || $svc_hardware ) {
507 my $part_pkg = $cust_pkg->part_pkg;
508 unless ( $part_pkg ) {
509 $dbh->rollback if $oldAutoCommit;
510 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
513 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
514 my $class = "FS::$svcdb";
515 push @svc_x, $class->new( \%svc_x );
518 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
519 push @svc_x, $svc_phone;
521 if ( $svc_hardware ) {
522 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
523 push @svc_x, $svc_hardware;
528 $hash{$cust_pkg} = \@svc_x;
531 my $error = $cust_main->insert( \%hash, $invoicing_list );
534 $dbh->rollback if $oldAutoCommit;
535 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
538 if ( $format eq 'simple' ) {
540 #false laziness w/bill.cgi
541 $error = $cust_main->bill( 'time' => $billtime );
543 $dbh->rollback if $oldAutoCommit;
544 return "can't bill customer for $line: $error";
547 $error = $cust_main->apply_payments_and_credits;
549 $dbh->rollback if $oldAutoCommit;
550 return "can't bill customer for $line: $error";
553 $error = $cust_main->collect();
555 $dbh->rollback if $oldAutoCommit;
556 return "can't collect customer for $line: $error";
563 if ( $job && time - $min_sec > $last ) { #progress bar
564 $job->update_statustext( int(100 * $row / $count) );
570 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
572 return "Empty file!" unless $row;
580 Not enough documentation.
584 L<FS::cust_main>, L<FS::cust_pkg>,
585 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>