1 package FS::cust_main::Import;
4 use vars qw( $DEBUG $conf );
9 use File::Slurp qw( slurp );
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;
220 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
224 if ( $type eq 'csv' ) {
226 last unless scalar(@buffer);
227 $line = shift(@buffer);
229 $parser->parse($line) or do {
230 $dbh->rollback if $oldAutoCommit;
231 return "can't parse: ". $parser->error_input();
233 @columns = $parser->fields();
235 } elsif ( $type eq 'xls' ) {
237 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
238 || ! $parser->{Cells}[$row];
240 my @row = @{ $parser->{Cells}[$row] };
241 @columns = map $_->{Val}, @row;
244 #warn $z++. ": $_\n" for @columns;
247 die "Unknown file type $type\n";
250 #warn join('-',@columns);
253 custbatch => $custbatch,
254 agentnum => $agentnum,
256 country => $conf->config('countrydefault') || 'US',
257 payby => $payby, #default
258 paydate => '12/2037', #default
261 my %cust_pkg = ( pkgpart => $pkgpart );
263 foreach my $field ( @fields ) {
265 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
267 #$cust_pkg{$1} = str2time( shift @$columns );
268 if ( $1 eq 'pkgpart' ) {
269 $cust_pkg{$1} = shift @columns;
270 } elsif ( $1 eq 'setup' ) {
271 $billtime = str2time(shift @columns);
273 $cust_pkg{$1} = str2time( shift @columns );
276 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
278 $svc_x{$1} = shift @columns;
280 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
282 $svc_x{$1} = shift @columns;
284 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
285 $svc_x{$1} = shift @columns;
290 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
292 my $referral = $columns[0];
293 my %hash = ( 'referral' => $referral,
294 'agentnum' => $agentnum,
298 my $part_referral = qsearchs('part_referral', \%hash )
299 || new FS::part_referral \%hash;
301 unless ( $part_referral->refnum ) {
302 my $error = $part_referral->insert;
304 $dbh->rollback if $oldAutoCommit;
305 return "can't auto-insert advertising source: $referral: $error";
309 $columns[0] = $part_referral->refnum;
312 my $value = shift @columns;
313 $cust_main{$field} = $value if length($value);
317 $cust_main{'payby'} = 'CARD'
318 if defined $cust_main{'payinfo'}
319 && length $cust_main{'payinfo'};
321 my $invoicing_list = $cust_main{'invoicing_list'}
322 ? [ delete $cust_main{'invoicing_list'} ]
325 my $cust_main = new FS::cust_main ( \%cust_main );
328 tie my %hash, 'Tie::RefHash'; #this part is important
330 if ( $cust_pkg{'pkgpart'} ) {
331 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
335 if ( $svc_x{'username'} ) {
337 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
338 $svcdb = 'svc_external';
342 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
343 $svc_phone = FS::svc_phone->new( {
344 map { $_ => delete($svc_x{$_}) }
345 qw( countrycode phonenum sip_password pin)
349 if ( $svcdb || $svc_phone ) {
350 my $part_pkg = $cust_pkg->part_pkg;
351 unless ( $part_pkg ) {
352 $dbh->rollback if $oldAutoCommit;
353 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
356 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
357 my $class = "FS::$svcdb";
358 push @svc_x, $class->new( \%svc_x );
361 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
362 push @svc_x, $svc_phone;
366 $hash{$cust_pkg} = \@svc_x;
369 my $error = $cust_main->insert( \%hash, $invoicing_list );
372 $dbh->rollback if $oldAutoCommit;
373 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
376 if ( $format eq 'simple' ) {
378 #false laziness w/bill.cgi
379 $error = $cust_main->bill( 'time' => $billtime );
381 $dbh->rollback if $oldAutoCommit;
382 return "can't bill customer for $line: $error";
385 $error = $cust_main->apply_payments_and_credits;
387 $dbh->rollback if $oldAutoCommit;
388 return "can't bill customer for $line: $error";
391 $error = $cust_main->collect();
393 $dbh->rollback if $oldAutoCommit;
394 return "can't collect customer for $line: $error";
401 if ( $job && time - $min_sec > $last ) { #progress bar
402 $job->update_statustext( int(100 * $row / $count) );
408 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
410 return "Empty file!" unless $row;
418 Not enough documentation.
422 L<FS::cust_main>, L<FS::cust_pkg>,
423 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>