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'};
132 if ( $format eq 'simple' ) {
133 @fields = qw( cust_pkg.setup dayphone first last
134 address1 address2 city state zip comments );
135 } elsif ( $format eq 'extended' ) {
136 @fields = qw( agent_custid refnum
137 last first address1 address2 city state zip country
139 ship_last ship_first ship_address1 ship_address2
140 ship_city ship_state ship_zip ship_country
141 payinfo paycvv paydate
144 svc_acct.username svc_acct._password
146 } elsif ( $format eq 'extended-plus_options' ) {
147 @fields = qw( agent_custid refnum
148 last first address1 address2 city state zip country
150 ship_last ship_first ship_address1 ship_address2
151 ship_city ship_state ship_zip ship_country
152 payinfo paycvv paydate
155 svc_acct.username svc_acct._password
158 } elsif ( $format eq 'extended-plus_company' ) {
159 @fields = qw( agent_custid refnum
160 last first company address1 address2 city state zip country
162 ship_last ship_first ship_company ship_address1 ship_address2
163 ship_city ship_state ship_zip ship_country
164 payinfo paycvv paydate
167 svc_acct.username svc_acct._password
169 } elsif ( $format eq 'extended-plus_company_and_options' ) {
170 @fields = qw( agent_custid refnum
171 last first company address1 address2 city state zip country
173 ship_last ship_first ship_company ship_address1 ship_address2
174 ship_city ship_state ship_zip ship_country
175 payinfo paycvv paydate
178 svc_acct.username svc_acct._password
181 } elsif ( $format =~ /^svc_broadband/ ) {
182 @fields = qw( agent_custid refnum
183 last first company address1 address2 city state zip country
185 ship_last ship_first ship_company ship_address1 ship_address2
186 ship_city ship_state ship_zip ship_country
187 payinfo paycvv paydate
189 cust_pkg.pkgpart cust_pkg.bill
191 push @fields, map "svc_broadband.$_", qw( ip_addr mac_addr sectornum );
192 } elsif ( $format =~ /^svc_external/ ) {
193 @fields = qw( agent_custid refnum
194 last first company address1 address2 city state zip country
196 ship_last ship_first ship_company ship_address1 ship_address2
197 ship_city ship_state ship_zip ship_country
198 payinfo paycvv paydate
200 cust_pkg.pkgpart cust_pkg.bill
201 svc_external.id svc_external.title
203 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
204 if $format eq 'svc_external_svc_phone';
205 } elsif ( $format eq 'birthdates-acct_phone_hardware') {
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 birthdate spouse_birthdate
212 payinfo paycvv paydate
214 cust_pkg.pkgpart cust_pkg.bill
215 svc_acct.username svc_acct._password
217 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
218 push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
220 } elsif ( $format eq 'national_id-acct_phone') {
221 @fields = qw( agent_custid refnum
222 last first company address1 address2 city state zip country
224 ship_last ship_first ship_company ship_address1 ship_address2
225 ship_city ship_state ship_zip ship_country
227 payinfo paycvv paydate
229 cust_pkg.pkgpart cust_pkg.bill
230 svc_acct.username svc_acct._password svc_acct.slipip
232 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 paydate => '12/2037', #default
324 my %cust_pkg = ( pkgpart => $pkgpart );
326 my %bill_location = ();
327 my %ship_location = ();
328 foreach my $field ( @fields ) {
330 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
332 #$cust_pkg{$1} = parse_datetime( shift @$columns );
333 if ( $1 eq 'pkgpart' ) {
334 $cust_pkg{$1} = shift @columns;
335 } elsif ( $1 eq 'setup' ) {
336 $billtime = parse_datetime(shift @columns);
338 $cust_pkg{$1} = parse_datetime( shift @columns );
341 } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
343 $svc_x{$1} = shift @columns;
345 } elsif ( $field =~ /^svc_broadband\.(ip_addr|mac_addr|sectornum)$/ ) {
347 $svc_x{$1} = shift @columns;
349 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
351 $svc_x{$1} = shift @columns;
353 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
354 $svc_x{$1} = shift @columns;
356 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
358 $svc_x{$1} = shift @columns;
360 } elsif ( $is_location{$field} ) {
362 $bill_location{$field} = shift @columns;
364 } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
366 $ship_location{$1} = shift @columns;
371 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
373 my $referral = $columns[0];
374 my %hash = ( 'referral' => $referral,
375 'agentnum' => $agentnum,
379 my $part_referral = qsearchs('part_referral', \%hash )
380 || new FS::part_referral \%hash;
382 unless ( $part_referral->refnum ) {
383 my $error = $part_referral->insert;
385 $dbh->rollback if $oldAutoCommit;
386 return "can't auto-insert advertising source: $referral: $error";
390 $columns[0] = $part_referral->refnum;
393 my $value = shift @columns;
394 $cust_main{$field} = $value if length($value);
396 } # foreach my $field
397 # finished importing columns
399 $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
400 $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
401 if ( grep $_, values(%ship_location) ) {
402 $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
403 $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
405 $cust_main{'ship_location'} = $cust_main{'bill_location'};
408 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
410 if ( $cust_main{'payinfo'} =~ /^\s*(\d+\@[\d\.]+)\s*$/ ) {
412 $cust_main{'payby'} = 'CHEK';
413 $cust_main{'payinfo'} = $1;
417 $cust_main{'payby'} = 'CARD';
419 if ($cust_main{'payinfo'} =~ /^\s*([AD]?)(.*)\s*$/) {
420 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
421 $cust_main{'payinfo'} = $2;
428 $cust_main{$_} = parse_datetime($cust_main{$_})
429 foreach grep $cust_main{$_},
430 qw( birthdate spouse_birthdate anniversary_date );
432 my $invoicing_list = $cust_main{'invoicing_list'}
433 ? [ delete $cust_main{'invoicing_list'} ]
436 my $customer_options = delete $cust_main{customer_options};
437 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
438 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
440 my $cust_main = new FS::cust_main ( \%cust_main );
443 tie my %hash, 'Tie::RefHash'; #this part is important
445 if ( $cust_pkg{'pkgpart'} ) {
447 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
448 $dbh->rollback if $oldAutoCommit;
449 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
452 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
456 if ( $svc_x{'username'} ) {
458 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
459 $svcdb = 'svc_external';
460 } elsif ( $svc_x{ip_addr} || $svc_x{mac_addr} ) {
461 $svcdb = 'svc_broadband';
465 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
466 $svc_phone = FS::svc_phone->new( {
467 map { $_ => delete($svc_x{$_}) }
468 qw( countrycode phonenum sip_password pin )
472 my $svc_hardware = '';
473 if ( $svc_x{'typenum'} ) {
474 $svc_hardware = FS::svc_hardware->new( {
475 map { $_ => delete($svc_x{$_}) }
476 qw( typenum ip_addr hw_addr serial )
480 if ( $svcdb || $svc_phone || $svc_hardware ) {
481 my $part_pkg = $cust_pkg->part_pkg;
482 unless ( $part_pkg ) {
483 $dbh->rollback if $oldAutoCommit;
484 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
487 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
488 my $class = "FS::$svcdb";
489 push @svc_x, $class->new( \%svc_x );
492 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
493 push @svc_x, $svc_phone;
495 if ( $svc_hardware ) {
496 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
497 push @svc_x, $svc_hardware;
502 $hash{$cust_pkg} = \@svc_x;
505 my $error = $cust_main->insert( \%hash, $invoicing_list );
508 $dbh->rollback if $oldAutoCommit;
509 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
512 if ( $format eq 'simple' ) {
514 #false laziness w/bill.cgi
515 $error = $cust_main->bill( 'time' => $billtime );
517 $dbh->rollback if $oldAutoCommit;
518 return "can't bill customer for $line: $error";
521 $error = $cust_main->apply_payments_and_credits;
523 $dbh->rollback if $oldAutoCommit;
524 return "can't bill customer for $line: $error";
527 $error = $cust_main->collect();
529 $dbh->rollback if $oldAutoCommit;
530 return "can't collect customer for $line: $error";
537 if ( $job && time - $min_sec > $last ) { #progress bar
538 $job->update_statustext( int(100 * $row / $count) );
544 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
546 return "Empty file!" unless $row;
554 Not enough documentation.
558 L<FS::cust_main>, L<FS::cust_pkg>,
559 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>