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 );
17 use FS::part_referral;
21 install_callback FS::UID sub {
25 my %is_location = map { $_ => 1 } FS::cust_main::Location->location_fields;
29 FS::cust_main::Import - Batch customer importing
33 use FS::cust_main::Import;
36 FS::cust_main::Import::batch_import( {
37 file => $file, #filename
38 type => $type, #csv or xls
39 format => $format, #extended, extended-plus_company, svc_external,
40 #extended-plus_company_and_options
41 #extended-plus_options, or svc_external_svc_phone
42 agentnum => $agentnum,
45 job => $job, #optional job queue job, for progressbar updates
46 custbatch => $custbatch, #optional batch unique identifier
51 use FS::UI::Web::JSRPC;
53 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
54 print $server->process;
58 Batch customer importing.
62 =item process_batch_import
64 Load a batch import as a queued JSRPC job
68 sub process_batch_import {
71 my $param = thaw(decode_base64(shift));
72 warn Dumper($param) if $DEBUG;
74 my $files = $param->{'uploaded_files'}
75 or die "No files provided.\n";
77 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
79 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
80 my $file = $dir. $files{'file'};
83 if ( $file =~ /\.(\w+)$/i ) {
87 warn "can't parse file type from filename $file; defaulting to CSV";
92 FS::cust_main::Import::batch_import( {
96 custbatch => $param->{custbatch},
97 agentnum => $param->{'agentnum'},
98 refnum => $param->{'refnum'},
99 pkgpart => $param->{'pkgpart'},
100 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
101 # city state zip comments )],
102 'format' => $param->{'format'},
107 die "$error\n" if $error;
116 #some false laziness w/cdr.pm now
120 my $job = $param->{job};
122 my $filename = $param->{file};
123 my $type = $param->{type} || 'csv';
125 my $custbatch = $param->{custbatch};
127 my $agentnum = $param->{agentnum};
128 my $refnum = $param->{refnum};
129 my $pkgpart = $param->{pkgpart};
131 my $format = $param->{'format'};
135 if ( $format eq 'simple' ) {
136 @fields = qw( cust_pkg.setup dayphone first last
137 address1 address2 city state zip comments );
139 } elsif ( $format eq 'extended' ) {
140 @fields = qw( agent_custid refnum
141 last first address1 address2 city state zip country
143 ship_last ship_first ship_address1 ship_address2
144 ship_city ship_state ship_zip ship_country
145 payinfo paycvv paydate
148 svc_acct.username svc_acct._password
151 } elsif ( $format eq 'extended-plus_options' ) {
152 @fields = qw( agent_custid refnum
153 last first address1 address2 city state zip country
155 ship_last ship_first ship_address1 ship_address2
156 ship_city ship_state ship_zip ship_country
157 payinfo paycvv paydate
160 svc_acct.username svc_acct._password
164 } elsif ( $format eq 'extended-plus_company' ) {
165 @fields = qw( agent_custid refnum
166 last first company address1 address2 city state zip country
168 ship_last ship_first ship_company ship_address1 ship_address2
169 ship_city ship_state ship_zip ship_country
170 payinfo paycvv paydate
173 svc_acct.username svc_acct._password
176 } elsif ( $format eq 'extended-plus_company_and_options' ) {
177 @fields = qw( agent_custid refnum
178 last first company address1 address2 city state zip country
180 ship_last ship_first ship_company ship_address1 ship_address2
181 ship_city ship_state ship_zip ship_country
182 payinfo paycvv paydate
185 svc_acct.username svc_acct._password
189 } elsif ( $format =~ /^svc_external/ ) {
190 @fields = qw( agent_custid refnum
191 last first company address1 address2 city state zip country
193 ship_last ship_first ship_company ship_address1 ship_address2
194 ship_city ship_state ship_zip ship_country
195 payinfo paycvv paydate
197 cust_pkg.pkgpart cust_pkg.bill
198 svc_external.id svc_external.title
200 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
201 if $format eq 'svc_external_svc_phone';
203 } elsif ( $format eq 'birthdates-acct_phone_hardware') {
204 @fields = qw( agent_custid refnum
205 last first company address1 address2 city state zip country
207 ship_last ship_first ship_company ship_address1 ship_address2
208 ship_city ship_state ship_zip ship_country
209 birthdate spouse_birthdate
210 payinfo paycvv paydate
212 cust_pkg.pkgpart cust_pkg.bill
213 svc_acct.username svc_acct._password
215 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
216 push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
219 } elsif ( $format eq 'national_id-acct_phone') {
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
226 payinfo paycvv paydate
228 cust_pkg.pkgpart cust_pkg.bill
229 svc_acct.username svc_acct._password svc_acct.slipip
231 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
235 die "unknown format $format";
241 if ( $type eq 'csv' ) {
243 eval "use Text::CSV_XS;";
246 $parser = new Text::CSV_XS;
248 @buffer = split(/\r?\n/, slurp($filename) );
249 $count = scalar(@buffer);
251 } elsif ( $type eq 'xls' ) {
253 eval "use Spreadsheet::ParseExcel;";
256 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
257 $parser = $excel->{Worksheet}[0]; #first sheet
259 $count = $parser->{MaxRow} || $parser->{MinRow};
263 die "Unknown file type $type\n";
268 local $SIG{HUP} = 'IGNORE';
269 local $SIG{INT} = 'IGNORE';
270 local $SIG{QUIT} = 'IGNORE';
271 local $SIG{TERM} = 'IGNORE';
272 local $SIG{TSTP} = 'IGNORE';
273 local $SIG{PIPE} = 'IGNORE';
275 my $oldAutoCommit = $FS::UID::AutoCommit;
276 local $FS::UID::AutoCommit = 0;
279 #implies ignore_expired_card
280 local($FS::cust_main::import) = 1;
281 local($FS::cust_main::import) = 1;
285 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
289 if ( $type eq 'csv' ) {
291 last unless scalar(@buffer);
292 $line = shift(@buffer);
294 $parser->parse($line) or do {
295 $dbh->rollback if $oldAutoCommit;
296 return "can't parse: ". $parser->error_input();
298 @columns = $parser->fields();
300 } elsif ( $type eq 'xls' ) {
302 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
303 || ! $parser->{Cells}[$row];
305 my @row = @{ $parser->{Cells}[$row] };
306 @columns = map $_->{Val}, @row;
309 #warn $z++. ": $_\n" for @columns;
312 die "Unknown file type $type\n";
315 #warn join('-',@columns);
318 custbatch => $custbatch,
319 agentnum => $agentnum,
321 payby => $payby, #default
322 paydate => '12/2037', #default
325 my %cust_pkg = ( pkgpart => $pkgpart );
327 my %bill_location = ();
328 my %ship_location = ();
329 foreach my $field ( @fields ) {
331 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
333 #$cust_pkg{$1} = parse_datetime( shift @$columns );
334 if ( $1 eq 'pkgpart' ) {
335 $cust_pkg{$1} = shift @columns;
336 } elsif ( $1 eq 'setup' ) {
337 $billtime = parse_datetime(shift @columns);
339 $cust_pkg{$1} = parse_datetime( shift @columns );
342 } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
344 $svc_x{$1} = shift @columns;
346 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
348 $svc_x{$1} = shift @columns;
350 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
351 $svc_x{$1} = shift @columns;
353 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
355 $svc_x{$1} = shift @columns;
357 } elsif ( $is_location{$field} ) {
359 $bill_location{$field} = shift @columns;
361 } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
363 $ship_location{$1} = shift @columns;
368 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
370 my $referral = $columns[0];
371 my %hash = ( 'referral' => $referral,
372 'agentnum' => $agentnum,
376 my $part_referral = qsearchs('part_referral', \%hash )
377 || new FS::part_referral \%hash;
379 unless ( $part_referral->refnum ) {
380 my $error = $part_referral->insert;
382 $dbh->rollback if $oldAutoCommit;
383 return "can't auto-insert advertising source: $referral: $error";
387 $columns[0] = $part_referral->refnum;
390 my $value = shift @columns;
391 $cust_main{$field} = $value if length($value);
393 } # foreach my $field
394 # finished importing columns
396 $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
397 $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
398 if ( grep $_, values(%ship_location) ) {
399 $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
400 $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
402 $cust_main{'ship_location'} = $cust_main{'bill_location'};
405 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
406 $cust_main{'payby'} = 'CARD';
407 if ($cust_main{'payinfo'} =~ /\s*([AD]?)(.*)\s*$/) {
408 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
409 $cust_main{'payinfo'} = $2;
413 $cust_main{$_} = parse_datetime($cust_main{$_})
414 foreach grep $cust_main{$_},
415 qw( birthdate spouse_birthdate anniversary_date );
417 my $invoicing_list = $cust_main{'invoicing_list'}
418 ? [ delete $cust_main{'invoicing_list'} ]
421 my $customer_options = delete $cust_main{customer_options};
422 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
423 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
425 my $cust_main = new FS::cust_main ( \%cust_main );
428 tie my %hash, 'Tie::RefHash'; #this part is important
430 if ( $cust_pkg{'pkgpart'} ) {
432 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
433 $dbh->rollback if $oldAutoCommit;
434 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
437 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
441 if ( $svc_x{'username'} ) {
443 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
444 $svcdb = 'svc_external';
448 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
449 $svc_phone = FS::svc_phone->new( {
450 map { $_ => delete($svc_x{$_}) }
451 qw( countrycode phonenum sip_password pin )
455 my $svc_hardware = '';
456 if ( $svc_x{'typenum'} ) {
457 $svc_hardware = FS::svc_hardware->new( {
458 map { $_ => delete($svc_x{$_}) }
459 qw( typenum ip_addr hw_addr serial )
463 if ( $svcdb || $svc_phone || $svc_hardware ) {
464 my $part_pkg = $cust_pkg->part_pkg;
465 unless ( $part_pkg ) {
466 $dbh->rollback if $oldAutoCommit;
467 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
470 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
471 my $class = "FS::$svcdb";
472 push @svc_x, $class->new( \%svc_x );
475 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
476 push @svc_x, $svc_phone;
478 if ( $svc_hardware ) {
479 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
480 push @svc_x, $svc_hardware;
485 $hash{$cust_pkg} = \@svc_x;
488 my $error = $cust_main->insert( \%hash, $invoicing_list );
491 $dbh->rollback if $oldAutoCommit;
492 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
495 if ( $format eq 'simple' ) {
497 #false laziness w/bill.cgi
498 $error = $cust_main->bill( 'time' => $billtime );
500 $dbh->rollback if $oldAutoCommit;
501 return "can't bill customer for $line: $error";
504 $error = $cust_main->apply_payments_and_credits;
506 $dbh->rollback if $oldAutoCommit;
507 return "can't bill customer for $line: $error";
510 $error = $cust_main->collect();
512 $dbh->rollback if $oldAutoCommit;
513 return "can't collect customer for $line: $error";
520 if ( $job && time - $min_sec > $last ) { #progress bar
521 $job->update_statustext( int(100 * $row / $count) );
527 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
529 return "Empty file!" unless $row;
537 Not enough documentation.
541 L<FS::cust_main>, L<FS::cust_pkg>,
542 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>