ignore expired cards on customer import
[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::part_referral;
17
18 $DEBUG = 0;
19
20 install_callback FS::UID sub {
21   $conf = new FS::Conf;
22 };
23
24 =head1 NAME
25
26 FS::cust_main::Import - Batch customer importing
27
28 =head1 SYNOPSIS
29
30   use FS::cust_main::Import;
31
32   #import
33   FS::cust_main::Import::batch_import( {
34     file      => $file,      #filename
35     type      => $type,      #csv or xls
36     format    => $format,    #extended, extended-plus_company, svc_external,
37                              # or svc_external_svc_phone
38     agentnum  => $agentnum,
39     refnum    => $refnum,
40     pkgpart   => $pkgpart,
41     job       => $job,       #optional job queue job, for progressbar updates
42     custbatch => $custbatch, #optional batch unique identifier
43   } );
44   die $error if $error;
45
46   #ajax helper
47   use FS::UI::Web::JSRPC;
48   my $server =
49     new FS::UI::Web::JSRPC 'FS::cust_main::Import::process_batch_import', $cgi;
50   print $server->process;
51
52 =head1 DESCRIPTION
53
54 Batch customer importing.
55
56 =head1 SUBROUTINES
57
58 =item process_batch_import
59
60 Load a batch import as a queued JSRPC job
61
62 =cut
63
64 sub process_batch_import {
65   my $job = shift;
66
67   my $param = thaw(decode_base64(shift));
68   warn Dumper($param) if $DEBUG;
69   
70   my $files = $param->{'uploaded_files'}
71     or die "No files provided.\n";
72
73   my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
74
75   my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
76   my $file = $dir. $files{'file'};
77
78   my $type;
79   if ( $file =~ /\.(\w+)$/i ) {
80     $type = lc($1);
81   } else {
82     #or error out???
83     warn "can't parse file type from filename $file; defaulting to CSV";
84     $type = 'csv';
85   }
86
87   my $error =
88     FS::cust_main::Import::batch_import( {
89       job       => $job,
90       file      => $file,
91       type      => $type,
92       custbatch => $param->{custbatch},
93       agentnum  => $param->{'agentnum'},
94       refnum    => $param->{'refnum'},
95       pkgpart   => $param->{'pkgpart'},
96       #'fields'  => [qw( cust_pkg.setup dayphone first last address1 address2
97       #                 city state zip comments                          )],
98       'format'  => $param->{'format'},
99     } );
100
101   unlink $file;
102
103   die "$error\n" if $error;
104
105 }
106
107 =item batch_import
108
109 =cut
110
111
112 #some false laziness w/cdr.pm now
113 sub batch_import {
114   my $param = shift;
115
116   my $job       = $param->{job};
117
118   my $filename  = $param->{file};
119   my $type      = $param->{type} || 'csv';
120
121   my $custbatch = $param->{custbatch};
122
123   my $agentnum  = $param->{agentnum};
124   my $refnum    = $param->{refnum};
125   my $pkgpart   = $param->{pkgpart};
126
127   my $format    = $param->{'format'};
128
129   my @fields;
130   my $payby;
131   if ( $format eq 'simple' ) {
132     @fields = qw( cust_pkg.setup dayphone first last
133                   address1 address2 city state zip comments );
134     $payby = 'BILL';
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     $payby = 'BILL';
147  } elsif ( $format eq 'extended-plus_company' ) {
148     @fields = qw( agent_custid refnum
149                   last first company address1 address2 city state zip country
150                   daytime night
151                   ship_last ship_first ship_company ship_address1 ship_address2
152                   ship_city ship_state ship_zip ship_country
153                   payinfo paycvv paydate
154                   invoicing_list
155                   cust_pkg.pkgpart
156                   svc_acct.username svc_acct._password 
157                 );
158     $payby = 'BILL';
159  } elsif ( $format =~ /^svc_external/ ) {
160     @fields = qw( agent_custid refnum
161                   last first company address1 address2 city state zip country
162                   daytime night
163                   ship_last ship_first ship_company ship_address1 ship_address2
164                   ship_city ship_state ship_zip ship_country
165                   payinfo paycvv paydate
166                   invoicing_list
167                   cust_pkg.pkgpart cust_pkg.bill
168                   svc_external.id svc_external.title
169                 );
170     push @fields, map "svc_phone.$_", qw( countrycode phonenum sip_password pin)
171       if $format eq 'svc_external_svc_phone';
172     $payby = 'BILL';
173   } else {
174     die "unknown format $format";
175   }
176
177   my $count;
178   my $parser;
179   my @buffer = ();
180   if ( $type eq 'csv' ) {
181
182     eval "use Text::CSV_XS;";
183     die $@ if $@;
184
185     $parser = new Text::CSV_XS;
186
187     @buffer = split(/\r?\n/, slurp($filename) );
188     $count = scalar(@buffer);
189
190   } elsif ( $type eq 'xls' ) {
191
192     eval "use Spreadsheet::ParseExcel;";
193     die $@ if $@;
194
195     my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($filename);
196     $parser = $excel->{Worksheet}[0]; #first sheet
197
198     $count = $parser->{MaxRow} || $parser->{MinRow};
199     $count++;
200
201   } else {
202     die "Unknown file type $type\n";
203   }
204
205   #my $columns;
206
207   local $SIG{HUP} = 'IGNORE';
208   local $SIG{INT} = 'IGNORE';
209   local $SIG{QUIT} = 'IGNORE';
210   local $SIG{TERM} = 'IGNORE';
211   local $SIG{TSTP} = 'IGNORE';
212   local $SIG{PIPE} = 'IGNORE';
213
214   my $oldAutoCommit = $FS::UID::AutoCommit;
215   local $FS::UID::AutoCommit = 0;
216   my $dbh = dbh;
217
218   #implies ignore_expired_card
219   local($FS::cust_main::import) = 1;
220   local($FS::cust_main::import) = 1;
221   
222   my $line;
223   my $row = 0;
224   my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
225   while (1) {
226
227     my @columns = ();
228     if ( $type eq 'csv' ) {
229
230       last unless scalar(@buffer);
231       $line = shift(@buffer);
232
233       $parser->parse($line) or do {
234         $dbh->rollback if $oldAutoCommit;
235         return "can't parse: ". $parser->error_input();
236       };
237       @columns = $parser->fields();
238
239     } elsif ( $type eq 'xls' ) {
240
241       last if $row > ($parser->{MaxRow} || $parser->{MinRow})
242            || ! $parser->{Cells}[$row];
243
244       my @row = @{ $parser->{Cells}[$row] };
245       @columns = map $_->{Val}, @row;
246
247       #my $z = 'A';
248       #warn $z++. ": $_\n" for @columns;
249
250     } else {
251       die "Unknown file type $type\n";
252     }
253
254     #warn join('-',@columns);
255
256     my %cust_main = (
257       custbatch => $custbatch,
258       agentnum  => $agentnum,
259       refnum    => $refnum,
260       country   => $conf->config('countrydefault') || 'US',
261       payby     => $payby, #default
262       paydate   => '12/2037', #default
263     );
264     my $billtime = time;
265     my %cust_pkg = ( pkgpart => $pkgpart );
266     my %svc_x = ();
267     foreach my $field ( @fields ) {
268
269       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
270
271         #$cust_pkg{$1} = parse_datetime( shift @$columns );
272         if ( $1 eq 'pkgpart' ) {
273           $cust_pkg{$1} = shift @columns;
274         } elsif ( $1 eq 'setup' ) {
275           $billtime = parse_datetime(shift @columns);
276         } else {
277           $cust_pkg{$1} = parse_datetime( shift @columns );
278         } 
279
280       } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
281
282         $svc_x{$1} = shift @columns;
283
284       } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
285
286         $svc_x{$1} = shift @columns;
287
288       } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
289         $svc_x{$1} = shift @columns;
290        
291       } else {
292
293         #refnum interception
294         if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
295
296           my $referral = $columns[0];
297           my %hash = ( 'referral' => $referral,
298                        'agentnum' => $agentnum,
299                        'disabled' => '',
300                      );
301
302           my $part_referral = qsearchs('part_referral', \%hash )
303                               || new FS::part_referral \%hash;
304
305           unless ( $part_referral->refnum ) {
306             my $error = $part_referral->insert;
307             if ( $error ) {
308               $dbh->rollback if $oldAutoCommit;
309               return "can't auto-insert advertising source: $referral: $error";
310             }
311           }
312
313           $columns[0] = $part_referral->refnum;
314         }
315
316         my $value = shift @columns;
317         $cust_main{$field} = $value if length($value);
318       }
319     }
320
321     $cust_main{'payby'} = 'CARD'
322       if defined $cust_main{'payinfo'}
323       && length  $cust_main{'payinfo'};
324
325     my $invoicing_list = $cust_main{'invoicing_list'}
326                            ? [ delete $cust_main{'invoicing_list'} ]
327                            : [];
328
329     my $cust_main = new FS::cust_main ( \%cust_main );
330
331     use Tie::RefHash;
332     tie my %hash, 'Tie::RefHash'; #this part is important
333
334     if ( $cust_pkg{'pkgpart'} ) {
335
336       unless ( $cust_pkg{'pkgpart'} =~ /^\d+$/ ) {
337         $dbh->rollback if $oldAutoCommit;
338         return 'illegal pkgpart: '. $cust_pkg{'pkgpart'};
339       }
340
341       my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
342
343       my @svc_x = ();
344       my $svcdb = '';
345       if ( $svc_x{'username'} ) {
346         $svcdb = 'svc_acct';
347       } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
348         $svcdb = 'svc_external';
349       }
350
351       my $svc_phone = '';
352       if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
353         $svc_phone = FS::svc_phone->new( {
354           map { $_ => delete($svc_x{$_}) }
355               qw( countrycode phonenum sip_password pin)
356         } );
357       }
358
359       if ( $svcdb || $svc_phone ) {
360         my $part_pkg = $cust_pkg->part_pkg;
361         unless ( $part_pkg ) {
362           $dbh->rollback if $oldAutoCommit;
363           return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
364         } 
365         if ( $svcdb ) {
366           $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
367           my $class = "FS::$svcdb";
368           push @svc_x, $class->new( \%svc_x );
369         }
370         if ( $svc_phone ) {
371           $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
372           push @svc_x, $svc_phone;
373         }
374       }
375
376       $hash{$cust_pkg} = \@svc_x;
377     }
378
379     my $error = $cust_main->insert( \%hash, $invoicing_list );
380
381     if ( $error ) {
382       $dbh->rollback if $oldAutoCommit;
383       return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
384     }
385
386     if ( $format eq 'simple' ) {
387
388       #false laziness w/bill.cgi
389       $error = $cust_main->bill( 'time' => $billtime );
390       if ( $error ) {
391         $dbh->rollback if $oldAutoCommit;
392         return "can't bill customer for $line: $error";
393       }
394   
395       $error = $cust_main->apply_payments_and_credits;
396       if ( $error ) {
397         $dbh->rollback if $oldAutoCommit;
398         return "can't bill customer for $line: $error";
399       }
400
401       $error = $cust_main->collect();
402       if ( $error ) {
403         $dbh->rollback if $oldAutoCommit;
404         return "can't collect customer for $line: $error";
405       }
406
407     }
408
409     $row++;
410
411     if ( $job && time - $min_sec > $last ) { #progress bar
412       $job->update_statustext( int(100 * $row / $count) );
413       $last = time;
414     }
415
416   }
417
418   $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
419
420   return "Empty file!" unless $row;
421
422   ''; #no error
423
424 }
425
426 =head1 BUGS
427
428 Not enough documentation.
429
430 =head1 SEE ALSO
431
432 L<FS::cust_main>, L<FS::cust_pkg>,
433 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>
434
435 =cut
436
437 1;