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 );
12 use FS::svc_broadband;
16 use FS::part_referral;
20 install_callback FS::UID sub {
24 my %is_location = map { $_ => 1 } FS::cust_main::Location->location_fields;
28 FS::cust_main::Import - Batch customer importing
32 use FS::cust_main::Import;
35 FS::cust_main::Import::batch_import( {
36 file => $file, #filename
37 type => $type, #csv or xls
38 format => $format, #extended, extended-plus_company, svc_external,
39 #extended-plus_company_and_options
40 #extended-plus_options, or svc_external_svc_phone
41 agentnum => $agentnum,
44 job => $job, #optional job queue job, for progressbar updates
45 custbatch => $custbatch, #optional batch unique identifier
50 use FS::UI::Web::JSRPC;
52 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
53 print $server->process;
57 Batch customer importing.
61 =item process_batch_import
63 Load a batch import as a queued JSRPC job
67 sub process_batch_import {
70 warn Dumper($param) if $DEBUG;
72 my $files = $param->{'uploaded_files'}
73 or die "No files provided.\n";
75 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
77 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
78 my $file = $dir. $files{'file'};
81 if ( $file =~ /\.(\w+)$/i ) {
85 warn "can't parse file type from filename $file; defaulting to CSV";
90 FS::cust_main::Import::batch_import( {
94 custbatch => $param->{custbatch},
95 agentnum => $param->{'agentnum'},
96 refnum => $param->{'refnum'},
97 pkgpart => $param->{'pkgpart'},
98 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
99 # city state zip comments )],
100 'format' => $param->{'format'},
105 die "$error\n" if $error;
114 #some false laziness w/cdr.pm now
118 my $job = $param->{job};
120 my $filename = $param->{file};
121 my $type = $param->{type} || 'csv';
123 my $custbatch = $param->{custbatch};
125 my $agentnum = $param->{agentnum};
126 my $refnum = $param->{refnum};
127 my $pkgpart = $param->{pkgpart};
129 my $format = $param->{'format'};
133 if ( $format eq 'simple' ) {
134 @fields = qw( cust_pkg.setup dayphone first last
135 address1 address2 city state zip comments );
137 } elsif ( $format eq 'extended' ) {
138 @fields = qw( agent_custid refnum
139 last first address1 address2 city state zip country
141 ship_last ship_first ship_address1 ship_address2
142 ship_city ship_state ship_zip ship_country
143 payinfo paycvv paydate
146 svc_acct.username svc_acct._password
149 } elsif ( $format eq 'extended-plus_options' ) {
150 @fields = qw( agent_custid refnum
151 last first address1 address2 city state zip country
153 ship_last ship_first ship_address1 ship_address2
154 ship_city ship_state ship_zip ship_country
155 payinfo paycvv paydate
158 svc_acct.username svc_acct._password
162 } elsif ( $format eq 'extended-plus_company' ) {
163 @fields = qw( agent_custid refnum
164 last first company address1 address2 city state zip country
166 ship_last ship_first ship_company ship_address1 ship_address2
167 ship_city ship_state ship_zip ship_country
168 payinfo paycvv paydate
171 svc_acct.username svc_acct._password
174 } elsif ( $format eq 'extended-plus_company_and_options' ) {
175 @fields = qw( agent_custid refnum
176 last first company address1 address2 city state zip country
178 ship_last ship_first ship_company ship_address1 ship_address2
179 ship_city ship_state ship_zip ship_country
180 payinfo paycvv paydate
183 svc_acct.username svc_acct._password
187 } elsif ( $format =~ /^svc_broadband/ ) {
188 @fields = qw( agent_custid refnum
189 last first company address1 address2 city state zip country
191 ship_last ship_first ship_company ship_address1 ship_address2
192 ship_city ship_state ship_zip ship_country
193 payinfo paycvv paydate
195 cust_pkg.pkgpart cust_pkg.bill
197 push @fields, map "svc_broadband.$_", qw( ip_addr mac_addr sectornum );
199 } elsif ( $format =~ /^svc_external/ ) {
200 @fields = qw( agent_custid refnum
201 last first company address1 address2 city state zip country
203 ship_last ship_first ship_company ship_address1 ship_address2
204 ship_city ship_state ship_zip ship_country
205 payinfo paycvv paydate
207 cust_pkg.pkgpart cust_pkg.bill
208 svc_external.id svc_external.title
210 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
211 if $format eq 'svc_external_svc_phone';
213 } elsif ( $format eq 'birthdates-acct_phone_hardware') {
214 @fields = qw( agent_custid refnum
215 last first company address1 address2 city state zip country
217 ship_last ship_first ship_company ship_address1 ship_address2
218 ship_city ship_state ship_zip ship_country
219 birthdate spouse_birthdate
220 payinfo paycvv paydate
222 cust_pkg.pkgpart cust_pkg.bill
223 svc_acct.username svc_acct._password
225 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
226 push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
229 } elsif ( $format eq 'national_id-acct_phone') {
230 @fields = qw( agent_custid refnum
231 last first company address1 address2 city state zip country
233 ship_last ship_first ship_company ship_address1 ship_address2
234 ship_city ship_state ship_zip ship_country
236 payinfo paycvv paydate
238 cust_pkg.pkgpart cust_pkg.bill
239 svc_acct.username svc_acct._password svc_acct.slipip
241 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
245 die "unknown format $format";
251 if ( $type eq 'csv' ) {
253 eval "use Text::CSV_XS;";
256 $parser = new Text::CSV_XS;
258 @buffer = split(/\r?\n/, slurp($filename) );
259 $count = scalar(@buffer);
261 } elsif ( $type eq 'xls' ) {
263 eval "use Spreadsheet::ParseExcel;";
266 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
267 $parser = $excel->{Worksheet}[0]; #first sheet
269 $count = $parser->{MaxRow} || $parser->{MinRow};
273 die "Unknown file type $type\n";
278 local $SIG{HUP} = 'IGNORE';
279 local $SIG{INT} = 'IGNORE';
280 local $SIG{QUIT} = 'IGNORE';
281 local $SIG{TERM} = 'IGNORE';
282 local $SIG{TSTP} = 'IGNORE';
283 local $SIG{PIPE} = 'IGNORE';
285 my $oldAutoCommit = $FS::UID::AutoCommit;
286 local $FS::UID::AutoCommit = 0;
289 #implies ignore_expired_card
290 local($FS::cust_main::import) = 1;
291 local($FS::cust_main::import) = 1;
295 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
299 if ( $type eq 'csv' ) {
301 last unless scalar(@buffer);
302 $line = shift(@buffer);
304 $parser->parse($line) or do {
305 $dbh->rollback if $oldAutoCommit;
306 return "can't parse: ". $parser->error_input();
308 @columns = $parser->fields();
310 } elsif ( $type eq 'xls' ) {
312 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
313 || ! $parser->{Cells}[$row];
315 my @row = @{ $parser->{Cells}[$row] };
316 @columns = map $_->{Val}, @row;
319 #warn $z++. ": $_\n" for @columns;
322 die "Unknown file type $type\n";
325 #warn join('-',@columns);
328 custbatch => $custbatch,
329 agentnum => $agentnum,
331 payby => $payby, #default
332 paydate => '12/2037', #default
335 my %cust_pkg = ( pkgpart => $pkgpart );
337 my %bill_location = ();
338 my %ship_location = ();
339 foreach my $field ( @fields ) {
341 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
343 #$cust_pkg{$1} = parse_datetime( shift @$columns );
344 if ( $1 eq 'pkgpart' ) {
345 $cust_pkg{$1} = shift @columns;
346 } elsif ( $1 eq 'setup' ) {
347 $billtime = parse_datetime(shift @columns);
349 $cust_pkg{$1} = parse_datetime( shift @columns );
352 } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
354 $svc_x{$1} = shift @columns;
356 } elsif ( $field =~ /^svc_broadband\.(ip_addr|mac_addr|sectornum)$/ ) {
358 $svc_x{$1} = shift @columns;
360 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
362 $svc_x{$1} = shift @columns;
364 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
365 $svc_x{$1} = shift @columns;
367 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
369 $svc_x{$1} = shift @columns;
371 } elsif ( $is_location{$field} ) {
373 $bill_location{$field} = shift @columns;
375 } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
377 $ship_location{$1} = shift @columns;
382 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
384 my $referral = $columns[0];
385 my %hash = ( 'referral' => $referral,
386 'agentnum' => $agentnum,
390 my $part_referral = qsearchs('part_referral', \%hash )
391 || new FS::part_referral \%hash;
393 unless ( $part_referral->refnum ) {
394 my $error = $part_referral->insert;
396 $dbh->rollback if $oldAutoCommit;
397 return "can't auto-insert advertising source: $referral: $error";
401 $columns[0] = $part_referral->refnum;
404 my $value = shift @columns;
405 $cust_main{$field} = $value if length($value);
407 } # foreach my $field
408 # finished importing columns
410 $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
411 $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
412 if ( grep $_, values(%ship_location) ) {
413 $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
414 $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
416 $cust_main{'ship_location'} = $cust_main{'bill_location'};
419 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
421 if ( $cust_main{'payinfo'} =~ /^\s*(\d+\@[\d\.]+)\s*$/ ) {
423 $cust_main{'payby'} = 'CHEK';
424 $cust_main{'payinfo'} = $1;
428 $cust_main{'payby'} = 'CARD';
430 if ($cust_main{'payinfo'} =~ /^\s*([AD]?)(.*)\s*$/) {
431 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
432 $cust_main{'payinfo'} = $2;
439 $cust_main{$_} = parse_datetime($cust_main{$_})
440 foreach grep $cust_main{$_},
441 qw( birthdate spouse_birthdate anniversary_date );
443 my $invoicing_list = $cust_main{'invoicing_list'}
444 ? [ delete $cust_main{'invoicing_list'} ]
447 my $customer_options = delete $cust_main{customer_options};
448 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
449 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
451 my $cust_main = new FS::cust_main ( \%cust_main );
454 tie my %hash, 'Tie::RefHash'; #this part is important
456 if ( $cust_pkg{'pkgpart'} ) {
458 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
459 $dbh->rollback if $oldAutoCommit;
460 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
463 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
467 if ( $svc_x{'username'} ) {
469 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
470 $svcdb = 'svc_external';
471 } elsif ( $svc_x{ip_addr} || $svc_x{mac_addr} ) {
472 $svcdb = 'svc_broadband';
476 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
477 $svc_phone = FS::svc_phone->new( {
478 map { $_ => delete($svc_x{$_}) }
479 qw( countrycode phonenum sip_password pin )
483 my $svc_hardware = '';
484 if ( $svc_x{'typenum'} ) {
485 $svc_hardware = FS::svc_hardware->new( {
486 map { $_ => delete($svc_x{$_}) }
487 qw( typenum ip_addr hw_addr serial )
491 if ( $svcdb || $svc_phone || $svc_hardware ) {
492 my $part_pkg = $cust_pkg->part_pkg;
493 unless ( $part_pkg ) {
494 $dbh->rollback if $oldAutoCommit;
495 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
498 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
499 my $class = "FS::$svcdb";
500 push @svc_x, $class->new( \%svc_x );
503 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
504 push @svc_x, $svc_phone;
506 if ( $svc_hardware ) {
507 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
508 push @svc_x, $svc_hardware;
513 $hash{$cust_pkg} = \@svc_x;
516 my $error = $cust_main->insert( \%hash, $invoicing_list );
519 $dbh->rollback if $oldAutoCommit;
520 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
523 if ( $format eq 'simple' ) {
525 #false laziness w/bill.cgi
526 $error = $cust_main->bill( 'time' => $billtime );
528 $dbh->rollback if $oldAutoCommit;
529 return "can't bill customer for $line: $error";
532 $error = $cust_main->apply_payments_and_credits;
534 $dbh->rollback if $oldAutoCommit;
535 return "can't bill customer for $line: $error";
538 $error = $cust_main->collect();
540 $dbh->rollback if $oldAutoCommit;
541 return "can't collect customer for $line: $error";
548 if ( $job && time - $min_sec > $last ) { #progress bar
549 $job->update_statustext( int(100 * $row / $count) );
555 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
557 return "Empty file!" unless $row;
565 Not enough documentation.
569 L<FS::cust_main>, L<FS::cust_pkg>,
570 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>