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 {
27 FS::cust_main::Import - Batch customer importing
31 use FS::cust_main::Import;
34 FS::cust_main::Import::batch_import( {
35 file => $file, #filename
36 type => $type, #csv or xls
37 format => $format, #extended, extended-plus_company, svc_external,
38 #extended-plus_company_and_options
39 #extended-plus_options, or svc_external_svc_phone
40 agentnum => $agentnum,
43 job => $job, #optional job queue job, for progressbar updates
44 custbatch => $custbatch, #optional batch unique identifier
49 use FS::UI::Web::JSRPC;
51 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
52 print $server->process;
56 Batch customer importing.
60 =item process_batch_import
62 Load a batch import as a queued JSRPC job
66 sub process_batch_import {
69 my $param = thaw(decode_base64(shift));
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_company 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_external/ ) {
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
196 svc_external.id svc_external.title
198 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
199 if $format eq 'svc_external_svc_phone';
201 } elsif ( $format eq 'birthdates-acct_phone_hardware') {
202 @fields = qw( agent_custid refnum
203 last first company address1 address2 city state zip country
205 ship_last ship_first ship_company ship_address1 ship_address2
206 ship_city ship_state ship_zip ship_country
207 birthdate spouse_birthdate
208 payinfo paycvv paydate
210 cust_pkg.pkgpart cust_pkg.bill
211 svc_acct.username svc_acct._password
213 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
214 push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
217 } elsif ( $format eq 'national_id-acct_phone') {
218 @fields = qw( agent_custid refnum
219 last first company address1 address2 city state zip country
221 ship_last ship_first ship_company ship_address1 ship_address2
222 ship_city ship_state ship_zip ship_country
224 payinfo paycvv paydate
226 cust_pkg.pkgpart cust_pkg.bill
227 svc_acct.username svc_acct._password svc_acct.slipip
229 push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
233 die "unknown format $format";
239 if ( $type eq 'csv' ) {
241 eval "use Text::CSV_XS;";
244 $parser = new Text::CSV_XS;
246 @buffer = split(/\r?\n/, slurp($filename) );
247 $count = scalar(@buffer);
249 } elsif ( $type eq 'xls' ) {
251 eval "use Spreadsheet::ParseExcel;";
254 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
255 $parser = $excel->{Worksheet}[0]; #first sheet
257 $count = $parser->{MaxRow} || $parser->{MinRow};
261 die "Unknown file type $type\n";
266 local $SIG{HUP} = 'IGNORE';
267 local $SIG{INT} = 'IGNORE';
268 local $SIG{QUIT} = 'IGNORE';
269 local $SIG{TERM} = 'IGNORE';
270 local $SIG{TSTP} = 'IGNORE';
271 local $SIG{PIPE} = 'IGNORE';
273 my $oldAutoCommit = $FS::UID::AutoCommit;
274 local $FS::UID::AutoCommit = 0;
277 #implies ignore_expired_card
278 local($FS::cust_main::import) = 1;
279 local($FS::cust_main::import) = 1;
283 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
287 if ( $type eq 'csv' ) {
289 last unless scalar(@buffer);
290 $line = shift(@buffer);
292 $parser->parse($line) or do {
293 $dbh->rollback if $oldAutoCommit;
294 return "can't parse: ". $parser->error_input();
296 @columns = $parser->fields();
298 } elsif ( $type eq 'xls' ) {
300 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
301 || ! $parser->{Cells}[$row];
303 my @row = @{ $parser->{Cells}[$row] };
304 @columns = map $_->{Val}, @row;
307 #warn $z++. ": $_\n" for @columns;
310 die "Unknown file type $type\n";
313 #warn join('-',@columns);
316 custbatch => $custbatch,
317 agentnum => $agentnum,
319 country => $conf->config('countrydefault') || 'US',
320 payby => $payby, #default
321 paydate => '12/2037', #default
324 my %cust_pkg = ( pkgpart => $pkgpart );
326 foreach my $field ( @fields ) {
328 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
330 #$cust_pkg{$1} = parse_datetime( shift @$columns );
331 if ( $1 eq 'pkgpart' ) {
332 $cust_pkg{$1} = shift @columns;
333 } elsif ( $1 eq 'setup' ) {
334 $billtime = parse_datetime(shift @columns);
336 $cust_pkg{$1} = parse_datetime( shift @columns );
339 } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
341 $svc_x{$1} = shift @columns;
343 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
345 $svc_x{$1} = shift @columns;
347 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
348 $svc_x{$1} = shift @columns;
350 } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
352 $svc_x{$1} = shift @columns;
357 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
359 my $referral = $columns[0];
360 my %hash = ( 'referral' => $referral,
361 'agentnum' => $agentnum,
365 my $part_referral = qsearchs('part_referral', \%hash )
366 || new FS::part_referral \%hash;
368 unless ( $part_referral->refnum ) {
369 my $error = $part_referral->insert;
371 $dbh->rollback if $oldAutoCommit;
372 return "can't auto-insert advertising source: $referral: $error";
376 $columns[0] = $part_referral->refnum;
379 my $value = shift @columns;
380 $cust_main{$field} = $value if length($value);
384 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
385 $cust_main{'payby'} = 'CARD';
386 if ($cust_main{'payinfo'} =~ /\s*([AD]?)(.*)\s*$/) {
387 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
388 $cust_main{'payinfo'} = $2;
392 $cust_main{$_} = parse_datetime($cust_main{$_})
393 foreach grep $cust_main{$_},
394 qw( birthdate spouse_birthdate anniversary_date );
396 my $invoicing_list = $cust_main{'invoicing_list'}
397 ? [ delete $cust_main{'invoicing_list'} ]
400 my $customer_options = delete $cust_main{customer_options};
401 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
402 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
404 my $cust_main = new FS::cust_main ( \%cust_main );
407 tie my %hash, 'Tie::RefHash'; #this part is important
409 if ( $cust_pkg{'pkgpart'} ) {
411 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
412 $dbh->rollback if $oldAutoCommit;
413 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
416 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
420 if ( $svc_x{'username'} ) {
422 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
423 $svcdb = 'svc_external';
427 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
428 $svc_phone = FS::svc_phone->new( {
429 map { $_ => delete($svc_x{$_}) }
430 qw( countrycode phonenum sip_password pin )
434 my $svc_hardware = '';
435 if ( $svc_x{'typenum'} ) {
436 $svc_hardware = FS::svc_hardware->new( {
437 map { $_ => delete($svc_x{$_}) }
438 qw( typenum ip_addr hw_addr serial )
442 if ( $svcdb || $svc_phone || $svc_hardware ) {
443 my $part_pkg = $cust_pkg->part_pkg;
444 unless ( $part_pkg ) {
445 $dbh->rollback if $oldAutoCommit;
446 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
449 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
450 my $class = "FS::$svcdb";
451 push @svc_x, $class->new( \%svc_x );
454 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
455 push @svc_x, $svc_phone;
457 if ( $svc_hardware ) {
458 $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
459 push @svc_x, $svc_hardware;
464 $hash{$cust_pkg} = \@svc_x;
467 my $error = $cust_main->insert( \%hash, $invoicing_list );
470 $dbh->rollback if $oldAutoCommit;
471 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
474 if ( $format eq 'simple' ) {
476 #false laziness w/bill.cgi
477 $error = $cust_main->bill( 'time' => $billtime );
479 $dbh->rollback if $oldAutoCommit;
480 return "can't bill customer for $line: $error";
483 $error = $cust_main->apply_payments_and_credits;
485 $dbh->rollback if $oldAutoCommit;
486 return "can't bill customer for $line: $error";
489 $error = $cust_main->collect();
491 $dbh->rollback if $oldAutoCommit;
492 return "can't collect customer for $line: $error";
499 if ( $job && time - $min_sec > $last ) { #progress bar
500 $job->update_statustext( int(100 * $row / $count) );
506 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
508 return "Empty file!" unless $row;
516 Not enough documentation.
520 L<FS::cust_main>, L<FS::cust_pkg>,
521 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>