add "National ID, plus account and phone services" import format, RT#18946
[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_external;
15 use FS::svc_phone;
16 use FS::svc_hardware;
17 use FS::part_referral;
18
19 $DEBUG = 0;
20
21 install_callback FS::UID sub {
22   $conf = new FS::Conf;
23 };
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
69   my $param = thaw(decode_base64(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   my $payby;
133   if ( $format eq 'simple' ) {
134     @fields = qw( cust_pkg.setup dayphone first last
135                   address1 address2 city state zip comments );
136     $payby = 'BILL';
137   } elsif ( $format eq 'extended' ) {
138     @fields = qw( agent_custid refnum
139                   last first address1 address2 city state zip country
140                   daytime night
141                   ship_last ship_first ship_address1 ship_address2
142                   ship_city ship_state ship_zip ship_country
143                   payinfo paycvv paydate
144                   invoicing_list
145                   cust_pkg.pkgpart
146                   svc_acct.username svc_acct._password 
147                 );
148     $payby = 'BILL';
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_company 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     $payby = 'BILL';
162  } elsif ( $format eq 'extended-plus_company' ) {
163     @fields = qw( agent_custid refnum
164                   last first company address1 address2 city state zip country
165                   daytime night
166                   ship_last ship_first ship_company ship_address1 ship_address2
167                   ship_city ship_state ship_zip ship_country
168                   payinfo paycvv paydate
169                   invoicing_list
170                   cust_pkg.pkgpart
171                   svc_acct.username svc_acct._password 
172                 );
173     $payby = 'BILL';
174  } elsif ( $format eq 'extended-plus_company_and_options' ) {
175     @fields = qw( agent_custid refnum
176                   last first company address1 address2 city state zip country
177                   daytime night
178                   ship_last ship_first ship_company ship_address1 ship_address2
179                   ship_city ship_state ship_zip ship_country
180                   payinfo paycvv paydate
181                   invoicing_list
182                   cust_pkg.pkgpart
183                   svc_acct.username svc_acct._password 
184                   customer_options
185                 );
186     $payby = 'BILL';
187  } elsif ( $format =~ /^svc_external/ ) {
188     @fields = qw( agent_custid refnum
189                   last first company address1 address2 city state zip country
190                   daytime night
191                   ship_last ship_first ship_company ship_address1 ship_address2
192                   ship_city ship_state ship_zip ship_country
193                   payinfo paycvv paydate
194                   invoicing_list
195                   cust_pkg.pkgpart cust_pkg.bill
196                   svc_external.id svc_external.title
197                 );
198     push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
199       if $format eq 'svc_external_svc_phone';
200     $payby = 'BILL';
201   } elsif ( $format eq 'birthdates-acct_phone_hardware') {
202     @fields = qw( agent_custid refnum
203                   last first company address1 address2 city state zip country
204                   daytime night
205                   ship_last ship_first ship_company ship_address1 ship_address2
206                   ship_city ship_state ship_zip ship_country
207                   birthdate spouse_birthdate
208                   payinfo paycvv paydate
209                   invoicing_list
210                   cust_pkg.pkgpart cust_pkg.bill
211                   svc_acct.username svc_acct._password 
212                 );
213     push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
214     push @fields, map "svc_hardware.$_", qw(typenum ip_addr hw_addr serial);
215
216     $payby = 'BILL';
217   } elsif ( $format eq 'national_id-acct_phone') {
218     @fields = qw( agent_custid refnum
219                   last first company address1 address2 city state zip country
220                   daytime night
221                   ship_last ship_first ship_company ship_address1 ship_address2
222                   ship_city ship_state ship_zip ship_country
223                   national_id
224                   payinfo paycvv paydate
225                   invoicing_list
226                   cust_pkg.pkgpart cust_pkg.bill
227                   svc_acct.username svc_acct._password 
228                 );
229     push @fields, map "svc_phone.$_", qw(countrycode phonenum sip_password pin);
230
231     $payby = 'BILL';
232   } else {
233     die "unknown format $format";
234   }
235
236   my $count;
237   my $parser;
238   my @buffer = ();
239   if ( $type eq 'csv' ) {
240
241     eval "use Text::CSV_XS;";
242     die $@ if $@;
243
244     $parser = new Text::CSV_XS;
245
246     @buffer = split(/\r?\n/, slurp($filename) );
247     $count = scalar(@buffer);
248
249   } elsif ( $type eq 'xls' ) {
250
251     eval "use Spreadsheet::ParseExcel;";
252     die $@ if $@;
253
254     my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
255     $parser = $excel->{Worksheet}[0]; #first sheet
256
257     $count = $parser->{MaxRow} || $parser->{MinRow};
258     $count++;
259
260   } else {
261     die "Unknown file type $type\n";
262   }
263
264   #my $columns;
265
266   local $SIG{HUP} = 'IGNORE';
267   local $SIG{INT} = 'IGNORE';
268   local $SIG{QUIT} = 'IGNORE';
269   local $SIG{TERM} = 'IGNORE';
270   local $SIG{TSTP} = 'IGNORE';
271   local $SIG{PIPE} = 'IGNORE';
272
273   my $oldAutoCommit = $FS::UID::AutoCommit;
274   local $FS::UID::AutoCommit = 0;
275   my $dbh = dbh;
276
277   #implies ignore_expired_card
278   local($FS::cust_main::import) = 1;
279   local($FS::cust_main::import) = 1;
280   
281   my $line;
282   my $row = 0;
283   my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
284   while (1) {
285
286     my @columns = ();
287     if ( $type eq 'csv' ) {
288
289       last unless scalar(@buffer);
290       $line = shift(@buffer);
291
292       $parser->parse($line) or do {
293         $dbh->rollback if $oldAutoCommit;
294         return "can't parse: ". $parser->error_input();
295       };
296       @columns = $parser->fields();
297
298     } elsif ( $type eq 'xls' ) {
299
300       last if $row > ($parser->{MaxRow} || $parser->{MinRow})
301            || ! $parser->{Cells}[$row];
302
303       my @row = @{ $parser->{Cells}[$row] };
304       @columns = map $_->{Val}, @row;
305
306       #my $z = 'A';
307       #warn $z++. ": $_\n" for @columns;
308
309     } else {
310       die "Unknown file type $type\n";
311     }
312
313     #warn join('-',@columns);
314
315     my %cust_main = (
316       custbatch => $custbatch,
317       agentnum  => $agentnum,
318       refnum    => $refnum,
319       country   => $conf->config('countrydefault') || 'US',
320       payby     => $payby, #default
321       paydate   => '12/2037', #default
322     );
323     my $billtime = time;
324     my %cust_pkg = ( pkgpart => $pkgpart );
325     my %svc_x = ();
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)$/ ) {
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       } else {
355
356         #refnum interception
357         if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
358
359           my $referral = $columns[0];
360           my %hash = ( 'referral' => $referral,
361                        'agentnum' => $agentnum,
362                        'disabled' => '',
363                      );
364
365           my $part_referral = qsearchs('part_referral', \%hash )
366                               || new FS::part_referral \%hash;
367
368           unless ( $part_referral->refnum ) {
369             my $error = $part_referral->insert;
370             if ( $error ) {
371               $dbh->rollback if $oldAutoCommit;
372               return "can't auto-insert advertising source: $referral: $error";
373             }
374           }
375
376           $columns[0] = $part_referral->refnum;
377         }
378
379         my $value = shift @columns;
380         $cust_main{$field} = $value if length($value);
381       }
382     }
383
384     if ( defined $cust_main{'payinfo'} && length $cust_main{'payinfo'} ) {
385       $cust_main{'payby'} = 'CARD';
386       if ($cust_main{'payinfo'} =~ /\s*([AD]?)(.*)\s*$/) {
387         $cust_main{'payby'} = 'DCRD' if $1 eq 'D';
388         $cust_main{'payinfo'} = $2;
389       }
390     }
391
392     $cust_main{$_} = parse_datetime($cust_main{$_})
393       foreach grep $cust_main{$_},
394         qw( birthdate spouse_birthdate anniversary_date );
395
396     my $invoicing_list = $cust_main{'invoicing_list'}
397                            ? [ delete $cust_main{'invoicing_list'} ]
398                            : [];
399
400     my $customer_options = delete $cust_main{customer_options};
401     $cust_main{tax} = 'Y' if $customer_options =~ /taxexempt/i;
402     push @$invoicing_list, 'POST' if $customer_options =~ /postalinvoice/i;
403
404     my $cust_main = new FS::cust_main ( \%cust_main );
405
406     use Tie::RefHash;
407     tie my %hash, 'Tie::RefHash'; #this part is important
408
409     if ( $cust_pkg{'pkgpart'} ) {
410
411       unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
412         $dbh->rollback if $oldAutoCommit;
413         return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
414       }
415
416       my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
417
418       my @svc_x = ();
419       my $svcdb = '';
420       if ( $svc_x{'username'} ) {
421         $svcdb = 'svc_acct';
422       } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
423         $svcdb = 'svc_external';
424       }
425
426       my $svc_phone = '';
427       if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
428         $svc_phone = FS::svc_phone->new( {
429           map { $_ => delete($svc_x{$_}) }
430               qw( countrycode phonenum sip_password pin )
431         } );
432       }
433
434       my $svc_hardware = '';
435       if ( $svc_x{'typenum'} ) {
436         $svc_hardware = FS::svc_hardware->new( {
437           map { $_ => delete($svc_x{$_}) }
438             qw( typenum ip_addr hw_addr serial )
439         } );
440       }
441
442       if ( $svcdb || $svc_phone || $svc_hardware ) {
443         my $part_pkg = $cust_pkg->part_pkg;
444         unless ( $part_pkg ) {
445           $dbh->rollback if $oldAutoCommit;
446           return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
447         } 
448         if ( $svcdb ) {
449           $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
450           my $class = "FS::$svcdb";
451           push @svc_x, $class->new( \%svc_x );
452         }
453         if ( $svc_phone ) {
454           $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
455           push @svc_x, $svc_phone;
456         }
457         if ( $svc_hardware ) {
458           $svc_hardware->svcpart( $part_pkg->svcpart_unique_svcdb('svc_hardware') );
459           push @svc_x, $svc_hardware;
460         }
461
462       }
463
464       $hash{$cust_pkg} = \@svc_x;
465     }
466
467     my $error = $cust_main->insert( \%hash, $invoicing_list );
468
469     if ( $error ) {
470       $dbh->rollback if $oldAutoCommit;
471       return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
472     }
473
474     if ( $format eq 'simple' ) {
475
476       #false laziness w/bill.cgi
477       $error = $cust_main->bill( 'time' => $billtime );
478       if ( $error ) {
479         $dbh->rollback if $oldAutoCommit;
480         return "can't bill customer for $line: $error";
481       }
482   
483       $error = $cust_main->apply_payments_and_credits;
484       if ( $error ) {
485         $dbh->rollback if $oldAutoCommit;
486         return "can't bill customer for $line: $error";
487       }
488
489       $error = $cust_main->collect();
490       if ( $error ) {
491         $dbh->rollback if $oldAutoCommit;
492         return "can't collect customer for $line: $error";
493       }
494
495     }
496
497     $row++;
498
499     if ( $job && time - $min_sec > $last ) { #progress bar
500       $job->update_statustext( int(100 * $row / $count) );
501       $last = time;
502     }
503
504   }
505
506   $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
507
508   return "Empty file!" unless $row;
509
510   ''; #no error
511
512 }
513
514 =head1 BUGS
515
516 Not enough documentation.
517
518 =head1 SEE ALSO
519
520 L<FS::cust_main>, L<FS::cust_pkg>,
521 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>
522
523 =cut
524
525 1;