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 = ();
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_broadband\.(ip_addr|mac_addr|sectornum)$/ ) {
348 $svc_x{$1} = shift @columns;
350 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
352 $svc_x{$1} = shift @columns;
354 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
355 $svc_x{$1} = shift @columns;
357 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
359 $svc_x{$1} = shift @columns;
361 } elsif ( $is_location{$field} ) {
363 $bill_location{$field} = shift @columns;
365 } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
367 $ship_location{$1} = shift @columns;
372 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
374 my $referral = $columns[0];
375 my %hash = ( 'referral' => $referral,
376 'agentnum' => $agentnum,
380 my $part_referral = qsearchs('part_referral', \%hash )
381 || new FS::part_referral \%hash;
383 unless ( $part_referral->refnum ) {
384 my $error = $part_referral->insert;
386 $dbh->rollback if $oldAutoCommit;
387 return "can't auto-insert advertising source: $referral: $error";
391 $columns[0] = $part_referral->refnum;
394 my $value = shift @columns;
395 $cust_main{$field} = $value if length($value);
397 } # foreach my $field
398 # finished importing columns
400 $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
401 $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
402 if ( grep $_, values(%ship_location) ) {
403 $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
404 $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
406 $cust_main{'ship_location'} = $cust_main{'bill_location'};
409 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
411 if ( $cust_main{'payinfo'} =~ /^\s*(\d+\@[\d\.]+)\s*$/ ) {
413 delete $cust_main{'payinfo'};
415 $cust_payby = new FS::cust_payby {
420 } elsif ($cust_main{'payinfo'} =~ /^\s*([AD]?)(.*)\s*$/) {
422 delete $cust_main{'payinfo'};
424 $cust_payby = new FS::cust_payby {
425 'payby' => ($1 eq 'D') ? 'DCRD' : 'CARD',
427 'paycvv' => delete $cust_main{'paycvv'},
428 'paydate' => delete $cust_main{'paydate'},
429 'payname' => $cust_main{'first'}. ' '. $cust_main{'last'},
436 $cust_main{$_} = parse_datetime($cust_main{$_})
437 foreach grep $cust_main{$_},
438 qw( birthdate spouse_birthdate anniversary_date );
440 my $invoicing_list = $cust_main{'invoicing_list'}
441 ? [ delete $cust_main{'invoicing_list'} ]
444 my $customer_options = delete $cust_main{customer_options};
445 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
446 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
448 my $cust_main = new FS::cust_main ( \%cust_main );
451 tie my %hash, 'Tie::RefHash'; #this part is important
453 if ( $cust_pkg{'pkgpart'} ) {
455 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
456 $dbh->rollback if $oldAutoCommit;
457 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
460 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
464 if ( $svc_x{'username'} ) {
466 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
467 $svcdb = 'svc_external';
468 } elsif ( $svc_x{ip_addr} || $svc_x{mac_addr} ) {
469 $svcdb = 'svc_broadband';
473 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
474 $svc_phone = FS::svc_phone->new( {
475 map { $_ => delete($svc_x{$_}) }
476 qw( countrycode phonenum sip_password pin )
480 my $svc_hardware = '';
481 if ( $svc_x{'typenum'} ) {
482 $svc_hardware = FS::svc_hardware->new( {
483 map { $_ => delete($svc_x{$_}) }
484 qw( typenum ip_addr hw_addr serial )
488 if ( $svcdb || $svc_phone || $svc_hardware ) {
489 my $part_pkg = $cust_pkg->part_pkg;
490 unless ( $part_pkg ) {
491 $dbh->rollback if $oldAutoCommit;
492 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
495 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
496 my $class = "FS::$svcdb";
497 push @svc_x, $class->new( \%svc_x );
500 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
501 push @svc_x, $svc_phone;
503 if ( $svc_hardware ) {
504 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
505 push @svc_x, $svc_hardware;
510 $hash{$cust_pkg} = \@svc_x;
513 my %options = ('invoicing_list' => $invoicing_list);
514 $options{'cust_payby'} = [ $cust_payby ] if $cust_payby;
516 my $error = $cust_main->insert( \%hash, %options );
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>