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;
106 'svc_acct' => [qw( username _password domsvc )],
107 'svc_phone' => [qw( countrycode phonenum sip_password pin )],
108 'svc_external' => [qw( id title )],
109 'location' => [qw( address1 address2 city state zip country )],
116 my %import_options = (
117 'table' => 'cust_pkg',
119 'preinsert_callback' => sub {
120 my($record, $param) = @_;
121 my @location_params = grep /^location\./, keys %$param;
122 if (@location_params) {
123 my $cust_location = FS::cust_location->new({
124 'custnum' => $record->custnum,
126 foreach my $p (@location_params) {
127 $p =~ /^location.(\w+)$/;
128 $cust_location->set($1, $param->{$p});
131 my $error = $cust_location->find_or_insert; # this avoids duplicates
132 return "error creating location: $error" if $error;
133 $record->set('locationnum', $cust_location->locationnum);
138 'postinsert_callback' => sub {
139 my( $record, $param ) = @_;
141 my $formatfields = _formatfields;
142 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
144 my $ff = $formatfields->{$svc_x};
146 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
147 my $svc = "FS::$svc_x"->new( {
148 'pkgnum' => $record->pkgnum,
149 'svcpart' => $record->part_pkg->svcpart($svc_x),
150 map { $_ => $param->{"$svc_x.$_"} } @$ff
153 #this whole thing should be turned into a callback or config to turn on
154 if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
155 my($username, $domain) = split(/\@/, $svc->username);
156 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
157 || new FS::svc_domain { 'svcpart' => 1,
158 'domain' => $domain, };
159 unless ( $svc_domain->svcnum ) {
160 my $error = $svc_domain->insert;
161 return "error auto-inserting domain: $error" if $error;
163 $svc->username($username);
164 $svc->domsvc($svc_domain->svcnum);
167 my $error = $svc->insert;
168 return "error inserting service: $error" if $error;
178 sub _import_options {
185 my $iopt = _import_options;
186 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
188 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
190 my $format = delete $opt->{'format'};
193 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
195 my $agent_pkgid = $2;
198 my( $self, $value ) = @_; # $conf, $param
199 my $cust_main = qsearchs('cust_main', {
200 'agentnum' => $agentnum,
201 'agent_custid' => $value,
203 $self->custnum($cust_main->custnum) if $cust_main;
206 push @fields, 'agent_pkgid' if $agent_pkgid;
208 @fields = ( 'custnum' );
211 push @fields, ( 'pkgpart', 'discountnum' );
213 my @date_fields = ();
214 if ( $format =~ /all_dates/ ) {
217 start_date setup bill last_bill susp adjourn
224 start_date setup bill last_bill susp adjourn
229 foreach my $field (@date_fields) {
231 my( $self, $value ) = @_; # $conf, $param
232 #->$field has undesirable effects
233 $self->set($field, parse_datetime($value) ); #$field closure
237 my $formatfields = _formatfields();
239 die "unknown format $format" unless $formatfields->{$format};
241 foreach my $field ( @{ $formatfields->{$format} } ) {
244 my( $self, $value, $conf, $param ) = @_;
245 $param->{"$format.$field"} = $value;
250 $opt->{'fields'} = \@fields;
252 FS::Record::batch_import( $opt );
259 my %cust_pkg = ( pkgpart => $pkgpart );
261 foreach my $field ( @fields ) {
263 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
265 #$cust_pkg{$1} = parse_datetime( shift @$columns );
266 if ( $1 eq 'pkgpart' ) {
267 $cust_pkg{$1} = shift @columns;
268 } elsif ( $1 eq 'setup' ) {
269 $billtime = parse_datetime(shift @columns);
271 $cust_pkg{$1} = parse_datetime( shift @columns );
274 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
276 $svc_x{$1} = shift @columns;
278 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
280 $svc_x{$1} = shift @columns;
282 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
283 $svc_x{$1} = shift @columns;
288 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
290 my $referral = $columns[0];
291 my %hash = ( 'referral' => $referral,
292 'agentnum' => $agentnum,
296 my $part_referral = qsearchs('part_referral', \%hash )
297 || new FS::part_referral \%hash;
299 unless ( $part_referral->refnum ) {
300 my $error = $part_referral->insert;
302 $dbh->rollback if $oldAutoCommit;
303 return "can't auto-insert advertising source: $referral: $error";
307 $columns[0] = $part_referral->refnum;
310 my $value = shift @columns;
311 $cust_main{$field} = $value if length($value);
315 $cust_main{'payby'} = 'CARD'
316 if defined $cust_main{'payinfo'}
317 && length $cust_main{'payinfo'};
319 my $invoicing_list = $cust_main{'invoicing_list'}
320 ? [ delete $cust_main{'invoicing_list'} ]
323 my $cust_main = new FS::cust_main ( \%cust_main );
326 tie my %hash, 'Tie::RefHash'; #this part is important
328 if ( $cust_pkg{'pkgpart'} ) {
329 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
333 if ( $svc_x{'username'} ) {
335 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
336 $svcdb = 'svc_external';
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)
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'};
354 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
355 my $class = "FS::$svcdb";
356 push @svc_x, $class->new( \%svc_x );
359 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
360 push @svc_x, $svc_phone;
364 $hash{$cust_pkg} = \@svc_x;
367 my $error = $cust_main->insert( \%hash, $invoicing_list );
370 $dbh->rollback if $oldAutoCommit;
371 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
374 if ( $format eq 'simple' ) {
376 #false laziness w/bill.cgi
377 $error = $cust_main->bill( 'time' => $billtime );
379 $dbh->rollback if $oldAutoCommit;
380 return "can't bill customer for $line: $error";
383 $error = $cust_main->apply_payments_and_credits;
385 $dbh->rollback if $oldAutoCommit;
386 return "can't bill customer for $line: $error";
389 $error = $cust_main->collect();
391 $dbh->rollback if $oldAutoCommit;
392 return "can't collect customer for $line: $error";
399 if ( $job && time - $min_sec > $last ) { #progress bar
400 $job->update_statustext( int(100 * $row / $count) );
406 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
408 return "Empty file!" unless $row;
416 Not enough documentation.
420 L<FS::cust_main>, L<FS::cust_pkg>,
421 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>