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_phone' => [qw( countrycode phonenum sip_password pin )],
109 'svc_external' => [qw( id title )],
110 'location' => [qw( address1 address2 city state zip country )],
117 my %import_options = (
118 'table' => 'cust_pkg',
120 'preinsert_callback' => sub {
121 my($record, $param) = @_;
122 my @location_params = grep /^location\./, keys %$param;
123 if (@location_params) {
124 my $cust_location = FS::cust_location->new({
125 'custnum' => $record->custnum,
127 foreach my $p (@location_params) {
128 $p =~ /^location.(\w+)$/;
129 $cust_location->set($1, $param->{$p});
132 my $error = $cust_location->find_or_insert; # this avoids duplicates
133 return "error creating location: $error" if $error;
134 $record->set('locationnum', $cust_location->locationnum);
139 'postinsert_callback' => sub {
140 my( $record, $param ) = @_;
142 my $formatfields = _formatfields;
143 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
145 my $ff = $formatfields->{$svc_x};
147 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
148 my $svc = "FS::$svc_x"->new( {
149 'pkgnum' => $record->pkgnum,
150 'svcpart' => $record->part_pkg->svcpart($svc_x),
151 map { $_ => $param->{"$svc_x.$_"} } @$ff
154 #this whole thing should be turned into a callback or config to turn on
155 if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
156 my($username, $domain) = split(/\@/, $svc->username);
157 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
158 || new FS::svc_domain { 'svcpart' => 1,
159 'domain' => $domain, };
160 unless ( $svc_domain->svcnum ) {
161 my $error = $svc_domain->insert;
162 return "error auto-inserting domain: $error" if $error;
164 $svc->username($username);
165 $svc->domsvc($svc_domain->svcnum);
168 my $error = $svc->insert;
169 return "error inserting service: $error" if $error;
179 sub _import_options {
186 my $iopt = _import_options;
187 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
189 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
191 my $format = delete $opt->{'format'};
194 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
196 my $agent_pkgid = $2;
199 my( $self, $value ) = @_; # $conf, $param
200 my $cust_main = qsearchs('cust_main', {
201 'agentnum' => $agentnum,
202 'agent_custid' => $value,
204 $self->custnum($cust_main->custnum) if $cust_main;
207 push @fields, 'agent_pkgid' if $agent_pkgid;
209 @fields = ( 'custnum' );
212 if ( $format =~ /^(.*)-locationnum$/ ) {
214 push @fields, 'locationnum';
217 if ( $format =~ /^bulk_(.*)$/ ) {
221 $opt->{'postinsert_callback'} = sub {
222 my( $record, $param ) = @_;
224 my $formatfields = _formatfields;
225 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
227 my $ff = $formatfields->{$svc_x};
229 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
231 $param->{'svc_phone.phonenum'} =~ /^\s*(\d+)\s*\-\s*(\d+)\s*$/
232 or return 'Enter a phone number range, with dash as the separator';
233 my($start, $end) = ($1, $2);
234 if ( length($end) < length($start) ) {
235 $end = substr($start, 0, length($start) - length($end) ). $end;
238 foreach my $phonenum ( "$start" .. "$end" ) {
240 my $svc = "FS::$svc_x"->new( {
241 'pkgnum' => $record->pkgnum,
242 'svcpart' => $record->part_pkg->svcpart($svc_x),
243 map { $_ => $param->{"$svc_x.$_"} } @$ff
246 $svc->phonenum($phonenum);
247 #$svc->set_default_and_fixed;
248 my $error = $svc->insert;
249 return "error inserting service: $error" if $error;
263 push @fields, ( 'pkgpart', 'discountnum' );
265 my @date_fields = ();
266 if ( $format =~ /all_dates/ ) {
269 start_date setup bill last_bill susp adjourn
276 start_date setup bill last_bill susp adjourn
281 foreach my $field (@date_fields) {
283 my( $self, $value ) = @_; # $conf, $param
284 #->$field has undesirable effects
285 $self->set($field, parse_datetime($value) ); #$field closure
289 my $formatfields = _formatfields();
291 die "unknown format $format" unless $formatfields->{$format};
293 foreach my $field ( @{ $formatfields->{$format} } ) {
296 my( $self, $value, $conf, $param ) = @_;
297 $param->{"$format.$field"} = $value;
302 $opt->{'fields'} = \@fields;
304 FS::Record::batch_import( $opt );
311 my %cust_pkg = ( pkgpart => $pkgpart );
313 foreach my $field ( @fields ) {
315 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
317 #$cust_pkg{$1} = parse_datetime( shift @$columns );
318 if ( $1 eq 'pkgpart' ) {
319 $cust_pkg{$1} = shift @columns;
320 } elsif ( $1 eq 'setup' ) {
321 $billtime = parse_datetime(shift @columns);
323 $cust_pkg{$1} = parse_datetime( shift @columns );
326 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
328 $svc_x{$1} = shift @columns;
330 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
332 $svc_x{$1} = shift @columns;
334 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
335 $svc_x{$1} = shift @columns;
340 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
342 my $referral = $columns[0];
343 my %hash = ( 'referral' => $referral,
344 'agentnum' => $agentnum,
348 my $part_referral = qsearchs('part_referral', \%hash )
349 || new FS::part_referral \%hash;
351 unless ( $part_referral->refnum ) {
352 my $error = $part_referral->insert;
354 $dbh->rollback if $oldAutoCommit;
355 return "can't auto-insert advertising source: $referral: $error";
359 $columns[0] = $part_referral->refnum;
362 my $value = shift @columns;
363 $cust_main{$field} = $value if length($value);
367 $cust_main{'payby'} = 'CARD'
368 if defined $cust_main{'payinfo'}
369 && length $cust_main{'payinfo'};
371 my $invoicing_list = $cust_main{'invoicing_list'}
372 ? [ delete $cust_main{'invoicing_list'} ]
375 my $cust_main = new FS::cust_main ( \%cust_main );
378 tie my %hash, 'Tie::RefHash'; #this part is important
380 if ( $cust_pkg{'pkgpart'} ) {
381 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
385 if ( $svc_x{'username'} ) {
387 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
388 $svcdb = 'svc_external';
392 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
393 $svc_phone = FS::svc_phone->new( {
394 map { $_ => delete($svc_x{$_}) }
395 qw( countrycode phonenum sip_password pin)
399 if ( $svcdb || $svc_phone ) {
400 my $part_pkg = $cust_pkg->part_pkg;
401 unless ( $part_pkg ) {
402 $dbh->rollback if $oldAutoCommit;
403 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
406 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
407 my $class = "FS::$svcdb";
408 push @svc_x, $class->new( \%svc_x );
411 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
412 push @svc_x, $svc_phone;
416 $hash{$cust_pkg} = \@svc_x;
419 my $error = $cust_main->insert( \%hash, $invoicing_list );
422 $dbh->rollback if $oldAutoCommit;
423 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
426 if ( $format eq 'simple' ) {
428 #false laziness w/bill.cgi
429 $error = $cust_main->bill( 'time' => $billtime );
431 $dbh->rollback if $oldAutoCommit;
432 return "can't bill customer for $line: $error";
435 $error = $cust_main->apply_payments_and_credits;
437 $dbh->rollback if $oldAutoCommit;
438 return "can't bill customer for $line: $error";
441 $error = $cust_main->collect();
443 $dbh->rollback if $oldAutoCommit;
444 return "can't collect customer for $line: $error";
451 if ( $job && time - $min_sec > $last ) { #progress bar
452 $job->update_statustext( int(100 * $row / $count) );
458 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
460 return "Empty file!" unless $row;
468 Not enough documentation.
472 L<FS::cust_main>, L<FS::cust_pkg>,
473 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>