tax engine refactoring for Avalara and Billsoft tax vendors, #25718
[freeside.git] / FS / FS / cust_main / Import.pm
1 package FS::cust_main::Import;
2
3 use strict;
4 use vars qw( $DEBUG $conf );
5 use Data::Dumper;
6 use File::Slurp qw( slurp );
7 use FS::Misc::DateTime qw( parse_datetime );
8 use FS::UID qw( dbh );
9 use FS::Record qw( qsearchs );
10 use FS::cust_main;
11 use FS::svc_acct;
12 use FS::svc_external;
13 use FS::svc_phone;
14 use FS::svc_hardware;
15 use FS::part_referral;
16
17 $DEBUG = 0;
18
19 install_callback FS::UID sub {
20   $conf = new FS::Conf;
21 };
22
23 my %is_location = map { $_ => 1 } FS::cust_main::Location->location_fields;
24
25 =head1 NAME
26
27 FS::cust_main::Import - Batch customer importing
28
29 =head1 SYNOPSIS
30
31   use FS::cust_main::Import;
32
33   #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,
41     refnum    => $refnum,
42     pkgpart   => $pkgpart,
43     job       => $job,       #optional job queue job, for progressbar updates
44     custbatch => $custbatch, #optional batch unique identifier
45   } );
46   die $error if $error;
47
48   #ajax helper
49   use FS::UI::Web::JSRPC;
50   my $server =
51     new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
52   print $server->process;
53
54 =head1 DESCRIPTION
55
56 Batch customer importing.
57
58 =head1 SUBROUTINES
59
60 =item process_batch_import
61
62 Load a batch import as a queued JSRPC job
63
64 =cut
65
66 sub process_batch_import {
67   my $job = shift;
68   my $param = shift;
69   warn Dumper($param) if $DEBUG;
70   
71   my $files = $param->{'uploaded_files'}
72     or die "No files provided.\n";
73
74   my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
75
76   my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
77   my $file = $dir. $files{'file'};
78
79   my $type;
80   if ( $file =~ /\.(\w+)$/i ) {
81     $type = lc($1);
82   } else {
83     #or error out???
84     warn "can't parse file type from filename $file; defaulting to CSV";
85     $type = 'csv';
86   }
87
88   my $error =
89     FS::cust_main::Import::batch_import( {
90       job       => $job,
91       file      => $file,
92       type      => $type,
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'},
100     } );
101
102   unlink $file;
103
104   die "$error\n" if $error;
105
106 }
107
108 =item batch_import
109
110 =cut
111
112
113 #some false laziness w/cdr.pm now
114 sub batch_import {
115   my $param = shift;
116
117   my $job       = $param->{job};
118
119   my $filename  = $param->{file};
120   my $type      = $param->{type} || 'csv';
121
122   my $custbatch = $param->{custbatch};
123
124   my $agentnum  = $param->{agentnum};
125   my $refnum    = $param->{refnum};
126   my $pkgpart   = $param->{pkgpart};
127
128   my $format    = $param->{'format'};
129
130   my @fields;
131   my $payby;
132   if ( $format eq 'simple' ) {
133     @fields = qw( cust_pkg.setup dayphone first last
134                   address1 address2 city state zip comments );
135     $payby = 'BILL';
136   } elsif ( $format eq 'extended' ) {
137     @fields = qw( agent_custid refnum
138                   last first address1 address2 city state zip country
139                   daytime night
140                   ship_last ship_first ship_address1 ship_address2
141                   ship_city ship_state ship_zip ship_country
142                   payinfo paycvv paydate
143                   invoicing_list
144                   cust_pkg.pkgpart
145                   svc_acct.username svc_acct._password 
146                 );
147     $payby = 'BILL';
148  } elsif ( $format eq 'extended-plus_options' ) {
149     @fields = qw( agent_custid refnum
150                   last first address1 address2 city state zip country
151                   daytime night
152                   ship_last ship_first ship_address1 ship_address2
153                   ship_city ship_state ship_zip ship_country
154                   payinfo paycvv paydate
155                   invoicing_list
156                   cust_pkg.pkgpart
157                   svc_acct.username svc_acct._password 
158                   customer_options
159                 );
160     $payby = 'BILL';
161  } elsif ( $format eq 'extended-plus_company' ) {
162     @fields = qw( agent_custid refnum
163                   last first company address1 address2 city state zip country
164                   daytime night
165                   ship_last ship_first ship_company ship_address1 ship_address2
166                   ship_city ship_state ship_zip ship_country
167                   payinfo paycvv paydate
168                   invoicing_list
169                   cust_pkg.pkgpart
170                   svc_acct.username svc_acct._password 
171                 );
172     $payby = 'BILL';
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
176                   daytime night
177                   ship_last ship_first ship_company ship_address1 ship_address2
178                   ship_city ship_state ship_zip ship_country
179                   payinfo paycvv paydate
180                   invoicing_list
181                   cust_pkg.pkgpart
182                   svc_acct.username svc_acct._password 
183                   customer_options
184                 );
185     $payby = 'BILL';
186  } elsif ( $format =~ /^svc_external/ ) {
187     @fields = qw( agent_custid refnum
188                   last first company address1 address2 city state zip country
189                   daytime night
190                   ship_last ship_first ship_company ship_address1 ship_address2
191                   ship_city ship_state ship_zip ship_country
192                   payinfo paycvv paydate
193                   invoicing_list
194                   cust_pkg.pkgpart cust_pkg.bill
195                   svc_external.id svc_external.title
196                 );
197     push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
198       if $format eq 'svc_external_svc_phone';
199     $payby = 'BILL';
200   } elsif ( $format eq 'birthdates-acct_phone_hardware') {
201     @fields = qw( agent_custid refnum
202                   last first company address1 address2 city state zip country
203                   daytime night
204                   ship_last ship_first ship_company ship_address1 ship_address2
205                   ship_city ship_state ship_zip ship_country
206                   birthdate spouse_birthdate
207                   payinfo paycvv paydate
208                   invoicing_list
209                   cust_pkg.pkgpart cust_pkg.bill
210                   svc_acct.username svc_acct._password 
211                 );
212     push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
213     push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
214
215     $payby = 'BILL';
216   } elsif ( $format eq 'national_id-acct_phone') {
217     @fields = qw( agent_custid refnum
218                   last first company address1 address2 city state zip country
219                   daytime night
220                   ship_last ship_first ship_company ship_address1 ship_address2
221                   ship_city ship_state ship_zip ship_country
222                   national_id
223                   payinfo paycvv paydate
224                   invoicing_list
225                   cust_pkg.pkgpart cust_pkg.bill
226                   svc_acct.username svc_acct._password svc_acct.slipip
227                 );
228     push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
229
230     $payby = 'BILL';
231   } else {
232     die "unknown format $format";
233   }
234
235   my $count;
236   my $parser;
237   my @buffer = ();
238   if ( $type eq 'csv' ) {
239
240     eval "use Text::CSV_XS;";
241     die $@ if $@;
242
243     $parser = new Text::CSV_XS;
244
245     @buffer = split(/\r?\n/, slurp($filename) );
246     $count = scalar(@buffer);
247
248   } elsif ( $type eq 'xls' ) {
249
250     eval "use Spreadsheet::ParseExcel;";
251     die $@ if $@;
252
253     my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
254     $parser = $excel->{Worksheet}[0]; #first sheet
255
256     $count = $parser->{MaxRow} || $parser->{MinRow};
257     $count++;
258
259   } else {
260     die "Unknown file type $type\n";
261   }
262
263   #my $columns;
264
265   local $SIG{HUP} = 'IGNORE';
266   local $SIG{INT} = 'IGNORE';
267   local $SIG{QUIT} = 'IGNORE';
268   local $SIG{TERM} = 'IGNORE';
269   local $SIG{TSTP} = 'IGNORE';
270   local $SIG{PIPE} = 'IGNORE';
271
272   my $oldAutoCommit = $FS::UID::AutoCommit;
273   local $FS::UID::AutoCommit = 0;
274   my $dbh = dbh;
275
276   #implies ignore_expired_card
277   local($FS::cust_main::import) = 1;
278   local($FS::cust_main::import) = 1;
279   
280   my $line;
281   my $row = 0;
282   my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
283   while (1) {
284
285     my @columns = ();
286     if ( $type eq 'csv' ) {
287
288       last unless scalar(@buffer);
289       $line = shift(@buffer);
290
291       $parser->parse($line) or do {
292         $dbh->rollback if $oldAutoCommit;
293         return "can't parse: ". $parser->error_input();
294       };
295       @columns = $parser->fields();
296
297     } elsif ( $type eq 'xls' ) {
298
299       last if $row > ($parser->{MaxRow} || $parser->{MinRow})
300            || ! $parser->{Cells}[$row];
301
302       my @row = @{ $parser->{Cells}[$row] };
303       @columns = map $_->{Val}, @row;
304
305       #my $z = 'A';
306       #warn $z++. ": $_\n" for @columns;
307
308     } else {
309       die "Unknown file type $type\n";
310     }
311
312     #warn join('-',@columns);
313
314     my %cust_main = (
315       custbatch => $custbatch,
316       agentnum  => $agentnum,
317       refnum    => $refnum,
318       payby     => $payby, #default
319       paydate   => '12/2037', #default
320     );
321     my $billtime = time;
322     my %cust_pkg = ( pkgpart => $pkgpart );
323     my %svc_x = ();
324     my %bill_location = ();
325     my %ship_location = ();
326     foreach my $field ( @fields ) {
327
328       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
329
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);
335         } else {
336           $cust_pkg{$1} = parse_datetime( shift @columns );
337         } 
338
339       } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
340
341         $svc_x{$1} = shift @columns;
342
343       } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
344
345         $svc_x{$1} = shift @columns;
346
347       } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
348         $svc_x{$1} = shift @columns;
349       
350       } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
351
352         $svc_x{$1} = shift @columns;
353
354       } elsif ( $is_location{$field} ) {
355
356         $bill_location{$field} = shift @columns;
357
358       } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
359
360         $ship_location{$1} = shift @columns;
361       
362       } else {
363
364         #refnum interception
365         if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
366
367           my $referral = $columns[0];
368           my %hash = ( 'referral' => $referral,
369                        'agentnum' => $agentnum,
370                        'disabled' => '',
371                      );
372
373           my $part_referral = qsearchs('part_referral', \%hash )
374                               || new FS::part_referral \%hash;
375
376           unless ( $part_referral->refnum ) {
377             my $error = $part_referral->insert;
378             if ( $error ) {
379               $dbh->rollback if $oldAutoCommit;
380               return "can't auto-insert advertising source: $referral: $error";
381             }
382           }
383
384           $columns[0] = $part_referral->refnum;
385         }
386
387         my $value = shift @columns;
388         $cust_main{$field} = $value if length($value);
389       }
390     } # foreach my $field
391     # finished importing columns
392
393     $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
394     $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
395     if ( grep $_, values(%ship_location) ) {
396       $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
397       $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
398     } else {
399       $cust_main{'ship_location'} = $cust_main{'bill_location'};
400     }
401
402     if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
403       $cust_main{'payby'} = 'CARD';
404       if ($cust_main{'payinfo'} =~ /\s*([AD]?)(.*)\s*$/) {
405         $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
406         $cust_main{'payinfo'} = $2;
407       }
408     }
409
410     $cust_main{$_} = parse_datetime($cust_main{$_})
411       foreach grep $cust_main{$_},
412         qw( birthdate spouse_birthdate anniversary_date );
413
414     my $invoicing_list = $cust_main{'invoicing_list'}
415                            ? [ delete $cust_main{'invoicing_list'} ]
416                            : [];
417
418     my $customer_options = delete $cust_main{customer_options};
419     $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
420     push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
421
422     my $cust_main = new FS::cust_main ( \%cust_main );
423
424     use Tie::RefHash;
425     tie my %hash, 'Tie::RefHash'; #this part is important
426
427     if ( $cust_pkg{'pkgpart'} ) {
428
429       unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
430         $dbh->rollback if $oldAutoCommit;
431         return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
432       }
433
434       my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
435
436       my @svc_x = ();
437       my $svcdb = '';
438       if ( $svc_x{'username'} ) {
439         $svcdb = 'svc_acct';
440       } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
441         $svcdb = 'svc_external';
442       }
443
444       my $svc_phone = '';
445       if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
446         $svc_phone = FS::svc_phone->new( {
447           map { $_ => delete($svc_x{$_}) }
448               qw( countrycode phonenum sip_password pin )
449         } );
450       }
451
452       my $svc_hardware = '';
453       if ( $svc_x{'typenum'} ) {
454         $svc_hardware = FS::svc_hardware->new( {
455           map { $_ => delete($svc_x{$_}) }
456             qw( typenum ip_addr hw_addr serial )
457         } );
458       }
459
460       if ( $svcdb || $svc_phone || $svc_hardware ) {
461         my $part_pkg = $cust_pkg->part_pkg;
462         unless ( $part_pkg ) {
463           $dbh->rollback if $oldAutoCommit;
464           return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
465         } 
466         if ( $svcdb ) {
467           $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
468           my $class = "FS::$svcdb";
469           push @svc_x, $class->new( \%svc_x );
470         }
471         if ( $svc_phone ) {
472           $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
473           push @svc_x, $svc_phone;
474         }
475         if ( $svc_hardware ) {
476           $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
477           push @svc_x, $svc_hardware;
478         }
479
480       }
481
482       $hash{$cust_pkg} = \@svc_x;
483     }
484
485     my $error = $cust_main->insert( \%hash, $invoicing_list );
486
487     if ( $error ) {
488       $dbh->rollback if $oldAutoCommit;
489       return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
490     }
491
492     if ( $format eq 'simple' ) {
493
494       #false laziness w/bill.cgi
495       $error = $cust_main->bill( 'time' => $billtime );
496       if ( $error ) {
497         $dbh->rollback if $oldAutoCommit;
498         return "can't bill customer for $line: $error";
499       }
500   
501       $error = $cust_main->apply_payments_and_credits;
502       if ( $error ) {
503         $dbh->rollback if $oldAutoCommit;
504         return "can't bill customer for $line: $error";
505       }
506
507       $error = $cust_main->collect();
508       if ( $error ) {
509         $dbh->rollback if $oldAutoCommit;
510         return "can't collect customer for $line: $error";
511       }
512
513     }
514
515     $row++;
516
517     if ( $job && time - $min_sec > $last ) { #progress bar
518       $job->update_statustext( int(100 * $row / $count) );
519       $last = time;
520     }
521
522   }
523
524   $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
525
526   return "Empty file!" unless $row;
527
528   ''; #no error
529
530 }
531
532 =head1 BUGS
533
534 Not enough documentation.
535
536 =head1 SEE ALSO
537
538 L<FS::cust_main>, L<FS::cust_pkg>,
539 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>
540
541 =cut
542
543 1;