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