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 );
16 use FS::part_referral;
20 install_callback FS::UID sub {
26 FS::cust_main::Import - Batch customer importing
30 use FS::cust_main::Import;
33 FS::cust_main::Import::batch_import( {
34 file => $file, #filename
35 type => $type, #csv or xls
36 format => $format, #extended, extended-plus_company, svc_external,
37 #extended-plus_company_and_options
38 #extended-plus_options, or svc_external_svc_phone
39 agentnum => $agentnum,
42 job => $job, #optional job queue job, for progressbar updates
43 custbatch => $custbatch, #optional batch unique identifier
48 use FS::UI::Web::JSRPC;
50 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
51 print $server->process;
55 Batch customer importing.
59 =item process_batch_import
61 Load a batch import as a queued JSRPC job
65 sub process_batch_import {
68 my $param = thaw(decode_base64(shift));
69 warn Dumper($param) if $DEBUG;
71 my $files = $param->{'uploaded_files'}
72 or die "No files provided.\n";
74 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
76 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
77 my $file = $dir. $files{'file'};
80 if ( $file =~ /\.(\w+)$/i ) {
84 warn "can't parse file type from filename $file; defaulting to CSV";
89 FS::cust_main::Import::batch_import( {
93 custbatch => $param->{custbatch},
94 agentnum => $param->{'agentnum'},
95 refnum => $param->{'refnum'},
96 pkgpart => $param->{'pkgpart'},
97 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
98 # city state zip comments )],
99 'format' => $param->{'format'},
104 die "$error\n" if $error;
113 #some false laziness w/cdr.pm now
117 my $job = $param->{job};
119 my $filename = $param->{file};
120 my $type = $param->{type} || 'csv';
122 my $custbatch = $param->{custbatch};
124 my $agentnum = $param->{agentnum};
125 my $refnum = $param->{refnum};
126 my $pkgpart = $param->{pkgpart};
128 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 );
136 } elsif ( $format eq 'extended' ) {
137 @fields = qw( agent_custid refnum
138 last first address1 address2 city state zip country
140 ship_last ship_first ship_address1 ship_address2
141 ship_city ship_state ship_zip ship_country
142 payinfo paycvv paydate
145 svc_acct.username svc_acct._password
148 } elsif ( $format eq 'extended-plus_options' ) {
149 @fields = qw( agent_custid refnum
150 last first address1 address2 city state zip country
152 ship_last ship_first ship_company ship_address1 ship_address2
153 ship_city ship_state ship_zip ship_country
154 payinfo paycvv paydate
157 svc_acct.username svc_acct._password
161 } elsif ( $format eq 'extended-plus_company' ) {
162 @fields = qw( agent_custid refnum
163 last first company address1 address2 city state zip country
165 ship_last ship_first ship_company ship_address1 ship_address2
166 ship_city ship_state ship_zip ship_country
167 payinfo paycvv paydate
170 svc_acct.username svc_acct._password
173 } elsif ( $format eq 'extended-plus_company_and_options' ) {
174 @fields = qw( agent_custid refnum
175 last first company address1 address2 city state zip country
177 ship_last ship_first ship_company ship_address1 ship_address2
178 ship_city ship_state ship_zip ship_country
179 payinfo paycvv paydate
182 svc_acct.username svc_acct._password
186 } elsif ( $format =~ /^svc_external/ ) {
187 @fields = qw( agent_custid refnum
188 last first company address1 address2 city state zip country
190 ship_last ship_first ship_company ship_address1 ship_address2
191 ship_city ship_state ship_zip ship_country
192 payinfo paycvv paydate
194 cust_pkg.pkgpart cust_pkg.bill
195 svc_external.id svc_external.title
197 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
198 if $format eq 'svc_external_svc_phone';
201 die "unknown format $format";
207 if ( $type eq 'csv' ) {
209 eval "use Text::CSV_XS;";
212 $parser = new Text::CSV_XS;
214 @buffer = split(/\r?\n/, slurp($filename) );
215 $count = scalar(@buffer);
217 } elsif ( $type eq 'xls' ) {
219 eval "use Spreadsheet::ParseExcel;";
222 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
223 $parser = $excel->{Worksheet}[0]; #first sheet
225 $count = $parser->{MaxRow} || $parser->{MinRow};
229 die "Unknown file type $type\n";
234 local $SIG{HUP} = 'IGNORE';
235 local $SIG{INT} = 'IGNORE';
236 local $SIG{QUIT} = 'IGNORE';
237 local $SIG{TERM} = 'IGNORE';
238 local $SIG{TSTP} = 'IGNORE';
239 local $SIG{PIPE} = 'IGNORE';
241 my $oldAutoCommit = $FS::UID::AutoCommit;
242 local $FS::UID::AutoCommit = 0;
245 #implies ignore_expired_card
246 local($FS::cust_main::import) = 1;
247 local($FS::cust_main::import) = 1;
251 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
255 if ( $type eq 'csv' ) {
257 last unless scalar(@buffer);
258 $line = shift(@buffer);
260 $parser->parse($line) or do {
261 $dbh->rollback if $oldAutoCommit;
262 return "can't parse: ". $parser->error_input();
264 @columns = $parser->fields();
266 } elsif ( $type eq 'xls' ) {
268 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
269 || ! $parser->{Cells}[$row];
271 my @row = @{ $parser->{Cells}[$row] };
272 @columns = map $_->{Val}, @row;
275 #warn $z++. ": $_\n" for @columns;
278 die "Unknown file type $type\n";
281 #warn join('-',@columns);
284 custbatch => $custbatch,
285 agentnum => $agentnum,
287 country => $conf->config('countrydefault') || 'US',
288 payby => $payby, #default
289 paydate => '12/2037', #default
292 my %cust_pkg = ( pkgpart => $pkgpart );
294 foreach my $field ( @fields ) {
296 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
298 #$cust_pkg{$1} = parse_datetime( shift @$columns );
299 if ( $1 eq 'pkgpart' ) {
300 $cust_pkg{$1} = shift @columns;
301 } elsif ( $1 eq 'setup' ) {
302 $billtime = parse_datetime(shift @columns);
304 $cust_pkg{$1} = parse_datetime( shift @columns );
307 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
309 $svc_x{$1} = shift @columns;
311 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
313 $svc_x{$1} = shift @columns;
315 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
316 $svc_x{$1} = shift @columns;
321 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
323 my $referral = $columns[0];
324 my %hash = ( 'referral' => $referral,
325 'agentnum' => $agentnum,
329 my $part_referral = qsearchs('part_referral', \%hash )
330 || new FS::part_referral \%hash;
332 unless ( $part_referral->refnum ) {
333 my $error = $part_referral->insert;
335 $dbh->rollback if $oldAutoCommit;
336 return "can't auto-insert advertising source: $referral: $error";
340 $columns[0] = $part_referral->refnum;
343 my $value = shift @columns;
344 $cust_main{$field} = $value if length($value);
348 if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
349 $cust_main{'payby'} = 'CARD';
350 if ($cust_main{'payinfo'} =~ /\s*([AD]?)(.*)\s*$/) {
351 $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
352 $cust_main{'payinfo'} = $2;
356 my $invoicing_list = $cust_main{'invoicing_list'}
357 ? [ delete $cust_main{'invoicing_list'} ]
360 my $customer_options = delete $cust_main{customer_options};
361 $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
362 push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
364 my $cust_main = new FS::cust_main ( \%cust_main );
367 tie my %hash, 'Tie::RefHash'; #this part is important
369 if ( $cust_pkg{'pkgpart'} ) {
371 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
372 $dbh->rollback if $oldAutoCommit;
373 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
376 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
380 if ( $svc_x{'username'} ) {
382 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
383 $svcdb = 'svc_external';
387 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
388 $svc_phone = FS::svc_phone->new( {
389 map { $_ => delete($svc_x{$_}) }
390 qw( countrycode phonenum sip_password pin)
394 if ( $svcdb || $svc_phone ) {
395 my $part_pkg = $cust_pkg->part_pkg;
396 unless ( $part_pkg ) {
397 $dbh->rollback if $oldAutoCommit;
398 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
401 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
402 my $class = "FS::$svcdb";
403 push @svc_x, $class->new( \%svc_x );
406 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
407 push @svc_x, $svc_phone;
411 $hash{$cust_pkg} = \@svc_x;
414 my $error = $cust_main->insert( \%hash, $invoicing_list );
417 $dbh->rollback if $oldAutoCommit;
418 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
421 if ( $format eq 'simple' ) {
423 #false laziness w/bill.cgi
424 $error = $cust_main->bill( 'time' => $billtime );
426 $dbh->rollback if $oldAutoCommit;
427 return "can't bill customer for $line: $error";
430 $error = $cust_main->apply_payments_and_credits;
432 $dbh->rollback if $oldAutoCommit;
433 return "can't bill customer for $line: $error";
436 $error = $cust_main->collect();
438 $dbh->rollback if $oldAutoCommit;
439 return "can't collect customer for $line: $error";
446 if ( $job && time - $min_sec > $last ) { #progress bar
447 $job->update_statustext( int(100 * $row / $count) );
453 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
455 return "Empty file!" unless $row;
463 Not enough documentation.
467 L<FS::cust_main>, L<FS::cust_pkg>,
468 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>