RT# 78356 - created import format for broadband service
[freeside.git] / FS / FS / cust_pkg / Import.pm
1 package FS::cust_pkg::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 FS::Misc::DateTime qw( parse_datetime );
9 use FS::Record qw( qsearchs );
10 use FS::cust_pkg;
11 use FS::cust_main;
12 use FS::svc_acct;
13 use FS::svc_external;
14 use FS::svc_phone;
15 use FS::svc_domain;
16
17 $DEBUG = 0;
18
19 #install_callback FS::UID sub {
20 #  $conf = new FS::Conf;
21 #};
22
23 =head1 NAME
24
25 FS::cust_pkg::Import - Batch customer importing
26
27 =head1 SYNOPSIS
28
29   use FS::cust_pkg::Import;
30
31   #import
32   FS::cust_pkg::Import::batch_import( {
33     file      => $file,      #filename
34     type      => $type,      #csv or xls
35     format    => $format,    #extended, extended-plus_company, svc_external,
36                              # or svc_external_svc_phone
37     agentnum  => $agentnum,
38     job       => $job,       #optional job queue job, for progressbar updates
39     pkgbatch  => $pkgbatch, #optional batch unique identifier
40   } );
41   die $error if $error;
42
43   #ajax helper
44   use FS::UI::Web::JSRPC;
45   my $server =
46     new FS::UI::Web::JSRPC 'FS::cust_pkg::Import::process_batch_import', $cgi;
47   print $server->process;
48
49 =head1 DESCRIPTION
50
51 Batch package importing.
52
53 =head1 SUBROUTINES
54
55 =item process_batch_import
56
57 Load a batch import as a queued JSRPC job
58
59 =cut
60
61 sub process_batch_import {
62   my $job = shift;
63
64   my $param = thaw(decode_base64(shift));
65   warn Dumper($param) if $DEBUG;
66   
67   my $files = $param->{'uploaded_files'}
68     or die "No files provided.\n";
69
70   my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
71
72   my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
73   my $file = $dir. $files{'file'};
74
75   my $type;
76   if ( $file =~ /\.(\w+)$/i ) {
77     $type = lc($1);
78   } else {
79     #or error out???
80     warn "can't parse file type from filename $file; defaulting to CSV";
81     $type = 'csv';
82   }
83
84   my $error =
85     FS::cust_pkg::Import::batch_import( {
86       job      => $job,
87       file     => $file,
88       type     => $type,
89       'params' => { pkgbatch => $param->{pkgbatch} },
90       agentnum => $param->{'agentnum'},
91       'format' => $param->{'format'},
92     } );
93
94   unlink $file;
95
96   die "$error\n" if $error;
97
98 }
99
100 =item batch_import
101
102 =cut
103
104 my %formatfields = (
105   'default'      => [],
106   'all_dates'    => [],
107   'svc_acct'     => [qw( username _password domsvc )],
108   'svc_broadband' => [qw( ip_addr description routernum blocknum sectornum speed_up speed_down )],
109   'svc_phone'    => [qw( countrycode phonenum sip_password pin )],
110   'svc_external' => [qw( id title )],
111   'location'     => [qw( address1 address2 city state zip country )],
112 );
113
114 sub _formatfields {
115   \%formatfields;
116 }
117
118 my %import_options = (
119   'table'         => 'cust_pkg',
120
121   'preinsert_callback'  => sub {
122     my($record, $param) = @_;
123     my @location_params = grep /^location\./, keys %$param;
124     if (@location_params) {
125       my $cust_location = FS::cust_location->new({
126           'custnum' => $record->custnum,
127       });
128       foreach my $p (@location_params) {
129         $p =~ /^location.(\w+)$/;
130         $cust_location->set($1, $param->{$p});
131       }
132
133       my $error = $cust_location->find_or_insert; # this avoids duplicates
134       return "error creating location: $error" if $error;
135       $record->set('locationnum', $cust_location->locationnum);
136     }
137     '';
138   },
139
140   'postinsert_callback' => sub {
141     my( $record, $param ) = @_;
142
143     my $formatfields = _formatfields;
144     foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
145
146       my $ff = $formatfields->{$svc_x};
147
148       if ( grep $param->{"$svc_x.$_"}, @$ff ) {
149         my $svc = "FS::$svc_x"->new( {
150           'pkgnum'  => $record->pkgnum,
151           'svcpart' => $record->part_pkg->svcpart($svc_x),
152           map { $_ => $param->{"$svc_x.$_"} } @$ff
153         } );
154
155         #this whole thing should be turned into a callback or config to turn on
156         if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
157           my($username, $domain) = split(/\@/, $svc->username);
158           my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
159                          || new FS::svc_domain { 'svcpart' => 1,
160                                                  'domain'  => $domain, };
161           unless ( $svc_domain->svcnum ) {
162             my $error = $svc_domain->insert;
163             return "error auto-inserting domain: $error" if $error;
164           }
165           $svc->username($username);
166           $svc->domsvc($svc_domain->svcnum);
167         }
168
169         my $error = $svc->insert;
170         return "error inserting service: $error" if $error;
171       }
172
173     }
174
175     return ''; #no error
176
177   },
178 );
179
180 sub _import_options {
181   \%import_options;
182 }
183
184 sub batch_import {
185   my $opt = shift;
186
187   my $iopt = _import_options;
188   $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
189
190   my $agentnum  = delete $opt->{agentnum}; # i like closures (delete though?)
191
192   my $format = delete $opt->{'format'};
193   my @fields = ();
194
195   if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
196     $format = $1;
197     my $agent_pkgid = $2;
198     @fields = (
199       sub {
200         my( $self, $value ) = @_; # $conf, $param
201         my $cust_main = qsearchs('cust_main', {
202           'agentnum'     => $agentnum,
203           'agent_custid' => $value,
204         });
205         $self->custnum($cust_main->custnum) if $cust_main;
206       },
207     );
208     push @fields, 'agent_pkgid' if $agent_pkgid;
209   } else {
210     @fields = ( 'custnum' );
211   }
212
213   if ( $format =~ /^(.*)-locationnum$/ ) {
214     $format = $1;
215     push @fields, 'locationnum';
216   }
217
218   if ( $format =~ /^bulk_(.*)$/ ) {
219
220     $format = $1;
221
222     $opt->{'postinsert_callback'} = sub {
223       my( $record, $param ) = @_;
224
225       my $formatfields = _formatfields;
226       foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
227
228         my $ff = $formatfields->{$svc_x};
229
230         if ( grep $param->{"$svc_x.$_"}, @$ff ) {
231
232           $param->{'svc_phone.phonenum'} =~ /^\s*(\d+)\s*\-\s*(\d+)\s*$/
233             or return 'Enter a phone number range, with dash as the separator';
234           my($start, $end) = ($1, $2);
235           if ( length($end) < length($start) ) {
236             $end = substr($start, 0, length($start) - length($end) ). $end;
237           }
238
239           foreach my $phonenum ( "$start" .. "$end" ) {
240
241             my $svc = "FS::$svc_x"->new( {
242               'pkgnum'  => $record->pkgnum,
243               'svcpart' => $record->part_pkg->svcpart($svc_x),
244               map { $_ => $param->{"$svc_x.$_"} } @$ff
245             } );
246
247             $svc->phonenum($phonenum);
248             #$svc->set_default_and_fixed;
249             my $error = $svc->insert;
250             return "error inserting service: $error" if $error;
251
252           }
253
254         }
255
256       }
257
258       return ''; #no error
259
260     };
261
262   }
263
264   push @fields, ( 'pkgpart', 'discountnum' );
265
266   my @date_fields = ();
267   if ( $format =~ /all_dates/ ) {
268     @date_fields = qw(
269       order_date
270       start_date setup bill last_bill susp adjourn
271       resume
272       cancel expire
273       contract_end dundate
274     );
275   } else {
276     @date_fields = qw(
277       start_date setup bill last_bill susp adjourn
278       cancel expire
279     );
280   }
281
282   foreach my $field (@date_fields) { 
283     push @fields, sub {
284       my( $self, $value ) = @_; # $conf, $param
285       #->$field has undesirable effects
286       $self->set($field, parse_datetime($value) ); #$field closure
287     };
288   }
289
290   my @formats = split /-/, $format;
291   foreach my $f (@formats){
292
293     my $formatfields = _formatfields();
294     die "unknown format $format" unless $formatfields->{$f};
295
296     foreach my $field ( @{ $formatfields->{$f} } ) {
297
298       push @fields, sub {
299         my( $self, $value, $conf, $param ) = @_;
300         $param->{"$f.$field"} = $value;
301       };
302
303     }
304   }
305
306   $opt->{'fields'} = \@fields;
307
308   FS::Record::batch_import( $opt );
309
310 }
311
312 =for comment
313
314     my $billtime = time;
315     my %cust_pkg = ( pkgpart => $pkgpart );
316     my %svc_x = ();
317     foreach my $field ( @fields ) {
318
319       if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
320
321         #$cust_pkg{$1} = parse_datetime( shift @$columns );
322         if ( $1 eq 'pkgpart' ) {
323           $cust_pkg{$1} = shift @columns;
324         } elsif ( $1 eq 'setup' ) {
325           $billtime = parse_datetime(shift @columns);
326         } else {
327           $cust_pkg{$1} = parse_datetime( shift @columns );
328         } 
329
330       } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
331
332         $svc_x{$1} = shift @columns;
333
334       } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
335
336         $svc_x{$1} = shift @columns;
337
338       } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
339         $svc_x{$1} = shift @columns;
340        
341       } else {
342
343         #refnum interception
344         if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
345
346           my $referral = $columns[0];
347           my %hash = ( 'referral' => $referral,
348                        'agentnum' => $agentnum,
349                        'disabled' => '',
350                      );
351
352           my $part_referral = qsearchs('part_referral', \%hash )
353                               || new FS::part_referral \%hash;
354
355           unless ( $part_referral->refnum ) {
356             my $error = $part_referral->insert;
357             if ( $error ) {
358               $dbh->rollback if $oldAutoCommit;
359               return "can't auto-insert advertising source: $referral: $error";
360             }
361           }
362
363           $columns[0] = $part_referral->refnum;
364         }
365
366         my $value = shift @columns;
367         $cust_main{$field} = $value if length($value);
368       }
369     }
370
371     $cust_main{'payby'} = 'CARD'
372       if defined $cust_main{'payinfo'}
373       && length  $cust_main{'payinfo'};
374
375     my $invoicing_list = $cust_main{'invoicing_list'}
376                            ? [ delete $cust_main{'invoicing_list'} ]
377                            : [];
378
379     my $cust_main = new FS::cust_main ( \%cust_main );
380
381     use Tie::RefHash;
382     tie my %hash, 'Tie::RefHash'; #this part is important
383
384     if ( $cust_pkg{'pkgpart'} ) {
385       my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
386
387       my @svc_x = ();
388       my $svcdb = '';
389       if ( $svc_x{'username'} ) {
390         $svcdb = 'svc_acct';
391       } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
392         $svcdb = 'svc_external';
393       }
394
395       my $svc_phone = '';
396       if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
397         $svc_phone = FS::svc_phone->new( {
398           map { $_ => delete($svc_x{$_}) }
399               qw( countrycode phonenum sip_password pin)
400         } );
401       }
402
403       if ( $svcdb || $svc_phone ) {
404         my $part_pkg = $cust_pkg->part_pkg;
405         unless ( $part_pkg ) {
406           $dbh->rollback if $oldAutoCommit;
407           return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
408         } 
409         if ( $svcdb ) {
410           $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
411           my $class = "FS::$svcdb";
412           push @svc_x, $class->new( \%svc_x );
413         }
414         if ( $svc_phone ) {
415           $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
416           push @svc_x, $svc_phone;
417         }
418       }
419
420       $hash{$cust_pkg} = \@svc_x;
421     }
422
423     my $error = $cust_main->insert( \%hash, $invoicing_list );
424
425     if ( $error ) {
426       $dbh->rollback if $oldAutoCommit;
427       return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
428     }
429
430     if ( $format eq 'simple' ) {
431
432       #false laziness w/bill.cgi
433       $error = $cust_main->bill( 'time' => $billtime );
434       if ( $error ) {
435         $dbh->rollback if $oldAutoCommit;
436         return "can't bill customer for $line: $error";
437       }
438   
439       $error = $cust_main->apply_payments_and_credits;
440       if ( $error ) {
441         $dbh->rollback if $oldAutoCommit;
442         return "can't bill customer for $line: $error";
443       }
444
445       $error = $cust_main->collect();
446       if ( $error ) {
447         $dbh->rollback if $oldAutoCommit;
448         return "can't collect customer for $line: $error";
449       }
450
451     }
452
453     $row++;
454
455     if ( $job && time - $min_sec > $last ) { #progress bar
456       $job->update_statustext( int(100 * $row / $count) );
457       $last = time;
458     }
459
460   }
461
462   $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
463
464   return "Empty file!" unless $row;
465
466   ''; #no error
467
468 }
469
470 =head1 BUGS
471
472 Not enough documentation.
473
474 =head1 SEE ALSO
475
476 L<FS::cust_main>, L<FS::cust_pkg>,
477 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>
478
479 =cut
480
481 1;