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