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