1 package FS::cust_pkg::Import;
4 use vars qw( $DEBUG ); #$conf );
8 use FS::Misc::DateTime qw( parse_datetime );
9 use FS::Record qw( qsearchs );
19 #install_callback FS::UID sub {
20 # $conf = new FS::Conf;
25 FS::cust_pkg::Import - Batch customer importing
29 use FS::cust_pkg::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
44 use FS::UI::Web::JSRPC;
46 new FS::UI::Web::JSRPC 'FS::cust_pkg::Import::process_batch_import', $cgi;
47 print $server->process;
51 Batch package importing.
55 =item process_batch_import
57 Load a batch import as a queued JSRPC job
61 sub process_batch_import {
64 my $param = thaw(decode_base64(shift));
65 warn Dumper($param) if $DEBUG;
67 my $files = $param->{'uploaded_files'}
68 or die "No files provided.\n";
70 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
72 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
73 my $file = $dir. $files{'file'};
76 if ( $file =~ /\.(\w+)$/i ) {
80 warn "can't parse file type from filename $file; defaulting to CSV";
85 FS::cust_pkg::Import::batch_import( {
89 'params' => { pkgbatch => $param->{pkgbatch} },
90 agentnum => $param->{'agentnum'},
91 'format' => $param->{'format'},
96 die "$error\n" if $error;
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 )],
118 my %import_options = (
119 'table' => 'cust_pkg',
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,
128 foreach my $p (@location_params) {
129 $p =~ /^location.(\w+)$/;
130 $cust_location->set($1, $param->{$p});
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);
140 'postinsert_callback' => sub {
141 my( $record, $param ) = @_;
143 my $formatfields = _formatfields;
144 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
146 my $ff = $formatfields->{$svc_x};
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
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;
165 $svc->username($username);
166 $svc->domsvc($svc_domain->svcnum);
169 my $error = $svc->insert;
170 return "error inserting service: $error" if $error;
180 sub _import_options {
187 my $iopt = _import_options;
188 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
190 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
192 my $format = delete $opt->{'format'};
195 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
197 my $agent_pkgid = $2;
200 my( $self, $value ) = @_; # $conf, $param
201 my $cust_main = qsearchs('cust_main', {
202 'agentnum' => $agentnum,
203 'agent_custid' => $value,
205 $self->custnum($cust_main->custnum) if $cust_main;
208 push @fields, 'agent_pkgid' if $agent_pkgid;
210 @fields = ( 'custnum' );
213 if ( $format =~ /^(.*)-locationnum$/ ) {
215 push @fields, 'locationnum';
218 if ( $format =~ /^bulk_(.*)$/ ) {
222 $opt->{'postinsert_callback'} = sub {
223 my( $record, $param ) = @_;
225 my $formatfields = _formatfields;
226 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
228 my $ff = $formatfields->{$svc_x};
230 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
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;
239 foreach my $phonenum ( "$start" .. "$end" ) {
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
247 $svc->phonenum($phonenum);
248 #$svc->set_default_and_fixed;
249 my $error = $svc->insert;
250 return "error inserting service: $error" if $error;
264 push @fields, ( 'pkgpart', 'discountnum' );
266 my @date_fields = ();
267 if ( $format =~ /all_dates/ ) {
270 start_date setup bill last_bill susp adjourn
277 start_date setup bill last_bill susp adjourn
282 foreach my $field (@date_fields) {
284 my( $self, $value ) = @_; # $conf, $param
285 #->$field has undesirable effects
286 $self->set($field, parse_datetime($value) ); #$field closure
290 my @formats = split /-/, $format;
291 foreach my $f (@formats){
293 my $formatfields = _formatfields();
294 die "unknown format $format" unless $formatfields->{$f};
296 foreach my $field ( @{ $formatfields->{$f} } ) {
299 my( $self, $value, $conf, $param ) = @_;
300 $param->{"$f.$field"} = $value;
306 $opt->{'fields'} = \@fields;
308 FS::Record::batch_import( $opt );
315 my %cust_pkg = ( pkgpart => $pkgpart );
317 foreach my $field ( @fields ) {
319 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
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);
327 $cust_pkg{$1} = parse_datetime( shift @columns );
330 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
332 $svc_x{$1} = shift @columns;
334 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
336 $svc_x{$1} = shift @columns;
338 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
339 $svc_x{$1} = shift @columns;
344 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
346 my $referral = $columns[0];
347 my %hash = ( 'referral' => $referral,
348 'agentnum' => $agentnum,
352 my $part_referral = qsearchs('part_referral', \%hash )
353 || new FS::part_referral \%hash;
355 unless ( $part_referral->refnum ) {
356 my $error = $part_referral->insert;
358 $dbh->rollback if $oldAutoCommit;
359 return "can't auto-insert advertising source: $referral: $error";
363 $columns[0] = $part_referral->refnum;
366 my $value = shift @columns;
367 $cust_main{$field} = $value if length($value);
371 $cust_main{'payby'} = 'CARD'
372 if defined $cust_main{'payinfo'}
373 && length $cust_main{'payinfo'};
375 my $invoicing_list = $cust_main{'invoicing_list'}
376 ? [ delete $cust_main{'invoicing_list'} ]
379 my $cust_main = new FS::cust_main ( \%cust_main );
382 tie my %hash, 'Tie::RefHash'; #this part is important
384 if ( $cust_pkg{'pkgpart'} ) {
385 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
389 if ( $svc_x{'username'} ) {
391 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
392 $svcdb = 'svc_external';
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)
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'};
410 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
411 my $class = "FS::$svcdb";
412 push @svc_x, $class->new( \%svc_x );
415 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
416 push @svc_x, $svc_phone;
420 $hash{$cust_pkg} = \@svc_x;
423 my $error = $cust_main->insert( \%hash, $invoicing_list );
426 $dbh->rollback if $oldAutoCommit;
427 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
430 if ( $format eq 'simple' ) {
432 #false laziness w/bill.cgi
433 $error = $cust_main->bill( 'time' => $billtime );
435 $dbh->rollback if $oldAutoCommit;
436 return "can't bill customer for $line: $error";
439 $error = $cust_main->apply_payments_and_credits;
441 $dbh->rollback if $oldAutoCommit;
442 return "can't bill customer for $line: $error";
445 $error = $cust_main->collect();
447 $dbh->rollback if $oldAutoCommit;
448 return "can't collect customer for $line: $error";
455 if ( $job && time - $min_sec > $last ) { #progress bar
456 $job->update_statustext( int(100 * $row / $count) );
462 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
464 return "Empty file!" unless $row;
472 Not enough documentation.
476 L<FS::cust_main>, L<FS::cust_pkg>,
477 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>