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 # or svc_external_svc_phone
38 agentnum => $agentnum,
41 job => $job, #optional job queue job, for progressbar updates
42 custbatch => $custbatch, #optional batch unique identifier
47 use FS::UI::Web::JSRPC;
49 new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
50 print $server->process;
54 Batch customer importing.
58 =item process_batch_import
60 Load a batch import as a queued JSRPC job
64 sub process_batch_import {
67 my $param = thaw(decode_base64(shift));
68 warn Dumper($param) if $DEBUG;
70 my $files = $param->{'uploaded_files'}
71 or die "No files provided.\n";
73 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
75 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
76 my $file = $dir. $files{'file'};
79 if ( $file =~ /\.(\w+)$/i ) {
83 warn "can't parse file type from filename $file; defaulting to CSV";
88 FS::cust_main::Import::batch_import( {
92 custbatch => $param->{custbatch},
93 agentnum => $param->{'agentnum'},
94 refnum => $param->{'refnum'},
95 pkgpart => $param->{'pkgpart'},
96 #'fields' => [qw( cust_pkg.setup dayphone first last address1 address2
97 # city state zip comments )],
98 'format' => $param->{'format'},
103 die "$error\n" if $error;
112 #some false laziness w/cdr.pm now
116 my $job = $param->{job};
118 my $filename = $param->{file};
119 my $type = $param->{type} || 'csv';
121 my $custbatch = $param->{custbatch};
123 my $agentnum = $param->{agentnum};
124 my $refnum = $param->{refnum};
125 my $pkgpart = $param->{pkgpart};
127 my $format = $param->{'format'};
131 if ( $format eq 'simple' ) {
132 @fields = qw( cust_pkg.setup dayphone first last
133 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
147 } elsif ( $format eq 'extended-plus_company' ) {
148 @fields = qw( agent_custid refnum
149 last first company address1 address2 city state zip country
151 ship_last ship_first ship_company ship_address1 ship_address2
152 ship_city ship_state ship_zip ship_country
153 payinfo paycvv paydate
156 svc_acct.username svc_acct._password
159 } elsif ( $format =~ /^svc_external/ ) {
160 @fields = qw( agent_custid refnum
161 last first company address1 address2 city state zip country
163 ship_last ship_first ship_company ship_address1 ship_address2
164 ship_city ship_state ship_zip ship_country
165 payinfo paycvv paydate
167 cust_pkg.pkgpart cust_pkg.bill
168 svc_external.id svc_external.title
170 push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
171 if $format eq 'svc_external_svc_phone';
174 die "unknown format $format";
180 if ( $type eq 'csv' ) {
182 eval "use Text::CSV_XS;";
185 $parser = new Text::CSV_XS;
187 @buffer = split(/\r?\n/, slurp($filename) );
188 $count = scalar(@buffer);
190 } elsif ( $type eq 'xls' ) {
192 eval "use Spreadsheet::ParseExcel;";
195 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
196 $parser = $excel->{Worksheet}[0]; #first sheet
198 $count = $parser->{MaxRow} || $parser->{MinRow};
202 die "Unknown file type $type\n";
207 local $SIG{HUP} = 'IGNORE';
208 local $SIG{INT} = 'IGNORE';
209 local $SIG{QUIT} = 'IGNORE';
210 local $SIG{TERM} = 'IGNORE';
211 local $SIG{TSTP} = 'IGNORE';
212 local $SIG{PIPE} = 'IGNORE';
214 my $oldAutoCommit = $FS::UID::AutoCommit;
215 local $FS::UID::AutoCommit = 0;
218 #implies ignore_expired_card
219 local($FS::cust_main::import) = 1;
220 local($FS::cust_main::import) = 1;
224 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
228 if ( $type eq 'csv' ) {
230 last unless scalar(@buffer);
231 $line = shift(@buffer);
233 $parser->parse($line) or do {
234 $dbh->rollback if $oldAutoCommit;
235 return "can't parse: ". $parser->error_input();
237 @columns = $parser->fields();
239 } elsif ( $type eq 'xls' ) {
241 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
242 || ! $parser->{Cells}[$row];
244 my @row = @{ $parser->{Cells}[$row] };
245 @columns = map $_->{Val}, @row;
248 #warn $z++. ": $_\n" for @columns;
251 die "Unknown file type $type\n";
254 #warn join('-',@columns);
257 custbatch => $custbatch,
258 agentnum => $agentnum,
260 country => $conf->config('countrydefault') || 'US',
261 payby => $payby, #default
262 paydate => '12/2037', #default
265 my %cust_pkg = ( pkgpart => $pkgpart );
267 foreach my $field ( @fields ) {
269 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
271 #$cust_pkg{$1} = parse_datetime( shift @$columns );
272 if ( $1 eq 'pkgpart' ) {
273 $cust_pkg{$1} = shift @columns;
274 } elsif ( $1 eq 'setup' ) {
275 $billtime = parse_datetime(shift @columns);
277 $cust_pkg{$1} = parse_datetime( shift @columns );
280 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
282 $svc_x{$1} = shift @columns;
284 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
286 $svc_x{$1} = shift @columns;
288 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
289 $svc_x{$1} = shift @columns;
294 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
296 my $referral = $columns[0];
297 my %hash = ( 'referral' => $referral,
298 'agentnum' => $agentnum,
302 my $part_referral = qsearchs('part_referral', \%hash )
303 || new FS::part_referral \%hash;
305 unless ( $part_referral->refnum ) {
306 my $error = $part_referral->insert;
308 $dbh->rollback if $oldAutoCommit;
309 return "can't auto-insert advertising source: $referral: $error";
313 $columns[0] = $part_referral->refnum;
316 my $value = shift @columns;
317 $cust_main{$field} = $value if length($value);
321 $cust_main{'payby'} = 'CARD'
322 if defined $cust_main{'payinfo'}
323 && length $cust_main{'payinfo'};
325 my $invoicing_list = $cust_main{'invoicing_list'}
326 ? [ delete $cust_main{'invoicing_list'} ]
329 my $cust_main = new FS::cust_main ( \%cust_main );
332 tie my %hash, 'Tie::RefHash'; #this part is important
334 if ( $cust_pkg{'pkgpart'} ) {
336 unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
337 $dbh->rollback if $oldAutoCommit;
338 return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
341 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
345 if ( $svc_x{'username'} ) {
347 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
348 $svcdb = 'svc_external';
352 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
353 $svc_phone = FS::svc_phone->new( {
354 map { $_ => delete($svc_x{$_}) }
355 qw( countrycode phonenum sip_password pin)
359 if ( $svcdb || $svc_phone ) {
360 my $part_pkg = $cust_pkg->part_pkg;
361 unless ( $part_pkg ) {
362 $dbh->rollback if $oldAutoCommit;
363 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
366 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
367 my $class = "FS::$svcdb";
368 push @svc_x, $class->new( \%svc_x );
371 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
372 push @svc_x, $svc_phone;
376 $hash{$cust_pkg} = \@svc_x;
379 my $error = $cust_main->insert( \%hash, $invoicing_list );
382 $dbh->rollback if $oldAutoCommit;
383 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
386 if ( $format eq 'simple' ) {
388 #false laziness w/bill.cgi
389 $error = $cust_main->bill( 'time' => $billtime );
391 $dbh->rollback if $oldAutoCommit;
392 return "can't bill customer for $line: $error";
395 $error = $cust_main->apply_payments_and_credits;
397 $dbh->rollback if $oldAutoCommit;
398 return "can't bill customer for $line: $error";
401 $error = $cust_main->collect();
403 $dbh->rollback if $oldAutoCommit;
404 return "can't collect customer for $line: $error";
411 if ( $job && time - $min_sec > $last ) { #progress bar
412 $job->update_statustext( int(100 * $row / $count) );
418 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
420 return "Empty file!" unless $row;
428 Not enough documentation.
432 L<FS::cust_main>, L<FS::cust_pkg>,
433 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>