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