fix customer importing vs v4 schema, RT#75964
[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     foreach my $field ( @fields ) {
329
330       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
331
332         #$cust_pkg{$1} = parse_datetime( shift @$columns );
333         if ( $1 eq 'pkgpart' ) {
334           $cust_pkg{$1} = shift @columns;
335         } elsif ( $1 eq 'setup' ) {
336           $billtime = parse_datetime(shift @columns);
337         } else {
338           $cust_pkg{$1} = parse_datetime( shift @columns );
339         } 
340
341       } elsif ( $field =~ /^svc_acct\.(username|_password|slipip)$/ ) {
342
343         $svc_x{$1} = shift @columns;
344
345       } elsif ( $field =~ /^svc_broadband\.(ip_addr|mac_addr|sectornum)$/ ) {
346
347         $svc_x{$1} = shift @columns;
348
349       } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
350
351         $svc_x{$1} = shift @columns;
352
353       } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
354         $svc_x{$1} = shift @columns;
355       
356       } elsif ( $field =~ /^svc_hardware\.(typenum|ip_addr|hw_addr|serial)$/ ) {
357
358         $svc_x{$1} = shift @columns;
359
360       } elsif ( $is_location{$field} ) {
361
362         $bill_location{$field} = shift @columns;
363
364       } elsif ( $field =~ /^ship_(.*)$/ and $is_location{$1} ) {
365
366         $ship_location{$1} = shift @columns;
367       
368       } else {
369
370         #refnum interception
371         if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
372
373           my $referral = $columns[0];
374           my %hash = ( 'referral' => $referral,
375                        'agentnum' => $agentnum,
376                        'disabled' => '',
377                      );
378
379           my $part_referral = qsearchs('part_referral', \%hash )
380                               || new FS::part_referral \%hash;
381
382           unless ( $part_referral->refnum ) {
383             my $error = $part_referral->insert;
384             if ( $error ) {
385               $dbh->rollback if $oldAutoCommit;
386               return "can't auto-insert advertising source: $referral: $error";
387             }
388           }
389
390           $columns[0] = $part_referral->refnum;
391         }
392
393         my $value = shift @columns;
394         $cust_main{$field} = $value if length($value);
395       }
396     } # foreach my $field
397     # finished importing columns
398
399     $bill_location{'country'} ||= $conf->config('countrydefault') || 'US';
400     $cust_main{'bill_location'} = FS::cust_location->new(\%bill_location);
401     if ( grep $_, values(%ship_location) ) {
402       $ship_location{'country'} ||= $conf->config('countrydefault') || 'US';
403       $cust_main{'ship_location'} = FS::cust_location->new(\%ship_location);
404     } else {
405       $cust_main{'ship_location'} = $cust_main{'bill_location'};
406     }
407
408     if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
409
410       if ( $cust_main{'payinfo'} =~ /^\s*(\d+\@[\d\.]+)\s*$/ ) {
411
412         $cust_main{'payby'}   = 'CHEK';
413         $cust_main{'payinfo'} = $1;
414
415       } else {
416
417         $cust_main{'payby'} = 'CARD';
418
419         if ($cust_main{'payinfo'} =~ /^\s*([AD]?)(.*)\s*$/) {
420           $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
421           $cust_main{'payinfo'} = $2;
422         }
423
424       }
425
426     }
427
428     $cust_main{$_} = parse_datetime($cust_main{$_})
429       foreach grep $cust_main{$_},
430         qw( birthdate spouse_birthdate anniversary_date );
431
432     my $invoicing_list = $cust_main{'invoicing_list'}
433                            ? [ delete $cust_main{'invoicing_list'} ]
434                            : [];
435
436     my $customer_options = delete $cust_main{customer_options};
437     $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
438     push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
439
440     my $cust_main = new FS::cust_main ( \%cust_main );
441
442     use Tie::RefHash;
443     tie my %hash, 'Tie::RefHash'; #this part is important
444
445     if ( $cust_pkg{'pkgpart'} ) {
446
447       unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
448         $dbh->rollback if $oldAutoCommit;
449         return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
450       }
451
452       my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
453
454       my @svc_x = ();
455       my $svcdb = '';
456       if ( $svc_x{'username'} ) {
457         $svcdb = 'svc_acct';
458       } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
459         $svcdb = 'svc_external';
460       } elsif ( $svc_x{ip_addr} || $svc_x{mac_addr} ) {
461         $svcdb = 'svc_broadband';
462       }
463
464       my $svc_phone = '';
465       if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
466         $svc_phone = FS::svc_phone->new( {
467           map { $_ => delete($svc_x{$_}) }
468               qw( countrycode phonenum sip_password pin )
469         } );
470       }
471
472       my $svc_hardware = '';
473       if ( $svc_x{'typenum'} ) {
474         $svc_hardware = FS::svc_hardware->new( {
475           map { $_ => delete($svc_x{$_}) }
476             qw( typenum ip_addr hw_addr serial )
477         } );
478       }
479
480       if ( $svcdb || $svc_phone || $svc_hardware ) {
481         my $part_pkg = $cust_pkg->part_pkg;
482         unless ( $part_pkg ) {
483           $dbh->rollback if $oldAutoCommit;
484           return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
485         } 
486         if ( $svcdb ) {
487           $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
488           my $class = "FS::$svcdb";
489           push @svc_x, $class->new( \%svc_x );
490         }
491         if ( $svc_phone ) {
492           $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
493           push @svc_x, $svc_phone;
494         }
495         if ( $svc_hardware ) {
496           $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
497           push @svc_x, $svc_hardware;
498         }
499
500       }
501
502       $hash{$cust_pkg} = \@svc_x;
503     }
504
505     my $error = $cust_main->insert( \%hash, $invoicing_list );
506
507     if ( $error ) {
508       $dbh->rollback if $oldAutoCommit;
509       return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
510     }
511
512     if ( $format eq 'simple' ) {
513
514       #false laziness w/bill.cgi
515       $error = $cust_main->bill( 'time' => $billtime );
516       if ( $error ) {
517         $dbh->rollback if $oldAutoCommit;
518         return "can't bill customer for $line: $error";
519       }
520   
521       $error = $cust_main->apply_payments_and_credits;
522       if ( $error ) {
523         $dbh->rollback if $oldAutoCommit;
524         return "can't bill customer for $line: $error";
525       }
526
527       $error = $cust_main->collect();
528       if ( $error ) {
529         $dbh->rollback if $oldAutoCommit;
530         return "can't collect customer for $line: $error";
531       }
532
533     }
534
535     $row++;
536
537     if ( $job && time - $min_sec > $last ) { #progress bar
538       $job->update_statustext( int(100 * $row / $count) );
539       $last = time;
540     }
541
542   }
543
544   $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
545
546   return "Empty file!" unless $row;
547
548   ''; #no error
549
550 }
551
552 =head1 BUGS
553
554 Not enough documentation.
555
556 =head1 SEE ALSO
557
558 L<FS::cust_main>, L<FS::cust_pkg>,
559 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>
560
561 =cut
562
563 1;