1 package FS::cust_pkg::Import;
4 use vars qw( $DEBUG ); #$conf );
6 use FS::Misc::DateTime qw( parse_datetime );
7 use FS::Record qw( qsearchs );
17 #install_callback FS::UID sub {
18 # $conf = new FS::Conf;
23 FS::cust_pkg::Import - Batch customer importing
27 use FS::cust_pkg::Import;
30 FS::cust_pkg::Import::batch_import( {
31 file => $file, #filename
32 type => $type, #csv or xls
33 format => $format, #extended, extended-plus_company, svc_external,
34 # or svc_external_svc_phone
35 agentnum => $agentnum,
36 job => $job, #optional job queue job, for progressbar updates
37 pkgbatch => $pkgbatch, #optional batch unique identifier
42 use FS::UI::Web::JSRPC;
44 new FS::UI::Web::JSRPC 'FS::cust_pkg::Import::process_batch_import', $cgi;
45 print $server->process;
49 Batch package importing.
53 =item process_batch_import
55 Load a batch import as a queued JSRPC job
59 sub process_batch_import {
62 warn Dumper($param) if $DEBUG;
64 my $files = $param->{'uploaded_files'}
65 or die "No files provided.\n";
67 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
69 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
70 my $file = $dir. $files{'file'};
73 if ( $file =~ /\.(\w+)$/i ) {
77 warn "can't parse file type from filename $file; defaulting to CSV";
82 FS::cust_pkg::Import::batch_import( {
86 'params' => { pkgbatch => $param->{pkgbatch} },
87 agentnum => $param->{'agentnum'},
88 'format' => $param->{'format'},
93 die "$error\n" if $error;
103 'svc_acct' => [qw( username _password domsvc )],
104 'svc_phone' => [qw( countrycode phonenum sip_password pin )],
105 'svc_external' => [qw( id title )],
106 'location' => [qw( address1 address2 city state zip country )],
113 my %import_options = (
114 'table' => 'cust_pkg',
116 'preinsert_callback' => sub {
117 my($record, $param) = @_;
118 my @location_params = grep /^location\./, keys %$param;
119 if (@location_params) {
120 my $cust_location = FS::cust_location->new({
121 'custnum' => $record->custnum,
123 foreach my $p (@location_params) {
124 $p =~ /^location.(\w+)$/;
125 $cust_location->set($1, $param->{$p});
128 warn Dumper $cust_location; # XXX
129 my $error = $cust_location->find_or_insert; # this avoids duplicates
130 return "error creating location: $error" if $error;
131 $record->set('locationnum', $cust_location->locationnum);
136 'postinsert_callback' => sub {
137 my( $record, $param ) = @_;
139 my $formatfields = _formatfields;
140 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
142 my $ff = $formatfields->{$svc_x};
144 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
145 my $svc = "FS::$svc_x"->new( {
146 'pkgnum' => $record->pkgnum,
147 'svcpart' => $record->part_pkg->svcpart($svc_x),
148 map { $_ => $param->{"$svc_x.$_"} } @$ff
151 #this whole thing should be turned into a callback or config to turn on
152 if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
153 my($username, $domain) = split(/\@/, $svc->username);
154 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
155 || new FS::svc_domain { 'svcpart' => 1,
156 'domain' => $domain, };
157 unless ( $svc_domain->svcnum ) {
158 my $error = $svc_domain->insert;
159 return "error auto-inserting domain: $error" if $error;
161 $svc->username($username);
162 $svc->domsvc($svc_domain->svcnum);
165 my $error = $svc->insert;
166 return "error inserting service: $error" if $error;
176 sub _import_options {
183 my $iopt = _import_options;
184 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
186 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
188 my $format = delete $opt->{'format'};
191 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
193 my $agent_pkgid = $2;
196 my( $self, $value ) = @_; # $conf, $param
197 my $cust_main = qsearchs('cust_main', {
198 'agentnum' => $agentnum,
199 'agent_custid' => $value,
201 $self->custnum($cust_main->custnum) if $cust_main;
204 push @fields, 'agent_pkgid' if $agent_pkgid;
206 @fields = ( 'custnum' );
209 push @fields, ( 'pkgpart', 'discountnum' );
212 qw( start_date setup bill last_bill susp adjourn cancel expire )
215 my( $self, $value ) = @_; # $conf, $param
216 #->$field has undesirable effects
217 $self->set($field, parse_datetime($value) ); #$field closure
221 my $formatfields = _formatfields();
223 die "unknown format $format" unless $formatfields->{$format};
225 foreach my $field ( @{ $formatfields->{$format} } ) {
228 my( $self, $value, $conf, $param ) = @_;
229 $param->{"$format.$field"} = $value;
234 $opt->{'fields'} = \@fields;
236 FS::Record::batch_import( $opt );
243 my %cust_pkg = ( pkgpart => $pkgpart );
245 foreach my $field ( @fields ) {
247 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
249 #$cust_pkg{$1} = parse_datetime( shift @$columns );
250 if ( $1 eq 'pkgpart' ) {
251 $cust_pkg{$1} = shift @columns;
252 } elsif ( $1 eq 'setup' ) {
253 $billtime = parse_datetime(shift @columns);
255 $cust_pkg{$1} = parse_datetime( shift @columns );
258 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
260 $svc_x{$1} = shift @columns;
262 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
264 $svc_x{$1} = shift @columns;
266 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
267 $svc_x{$1} = shift @columns;
272 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
274 my $referral = $columns[0];
275 my %hash = ( 'referral' => $referral,
276 'agentnum' => $agentnum,
280 my $part_referral = qsearchs('part_referral', \%hash )
281 || new FS::part_referral \%hash;
283 unless ( $part_referral->refnum ) {
284 my $error = $part_referral->insert;
286 $dbh->rollback if $oldAutoCommit;
287 return "can't auto-insert advertising source: $referral: $error";
291 $columns[0] = $part_referral->refnum;
294 my $value = shift @columns;
295 $cust_main{$field} = $value if length($value);
299 $cust_main{'payby'} = 'CARD'
300 if defined $cust_main{'payinfo'}
301 && length $cust_main{'payinfo'};
303 my $invoicing_list = $cust_main{'invoicing_list'}
304 ? [ delete $cust_main{'invoicing_list'} ]
307 my $cust_main = new FS::cust_main ( \%cust_main );
310 tie my %hash, 'Tie::RefHash'; #this part is important
312 if ( $cust_pkg{'pkgpart'} ) {
313 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
317 if ( $svc_x{'username'} ) {
319 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
320 $svcdb = 'svc_external';
324 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
325 $svc_phone = FS::svc_phone->new( {
326 map { $_ => delete($svc_x{$_}) }
327 qw( countrycode phonenum sip_password pin)
331 if ( $svcdb || $svc_phone ) {
332 my $part_pkg = $cust_pkg->part_pkg;
333 unless ( $part_pkg ) {
334 $dbh->rollback if $oldAutoCommit;
335 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
338 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
339 my $class = "FS::$svcdb";
340 push @svc_x, $class->new( \%svc_x );
343 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
344 push @svc_x, $svc_phone;
348 $hash{$cust_pkg} = \@svc_x;
351 my $error = $cust_main->insert( \%hash, $invoicing_list );
354 $dbh->rollback if $oldAutoCommit;
355 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
358 if ( $format eq 'simple' ) {
360 #false laziness w/bill.cgi
361 $error = $cust_main->bill( 'time' => $billtime );
363 $dbh->rollback if $oldAutoCommit;
364 return "can't bill customer for $line: $error";
367 $error = $cust_main->apply_payments_and_credits;
369 $dbh->rollback if $oldAutoCommit;
370 return "can't bill customer for $line: $error";
373 $error = $cust_main->collect();
375 $dbh->rollback if $oldAutoCommit;
376 return "can't collect customer for $line: $error";
383 if ( $job && time - $min_sec > $last ) { #progress bar
384 $job->update_statustext( int(100 * $row / $count) );
390 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
392 return "Empty file!" unless $row;
400 Not enough documentation.
404 L<FS::cust_main>, L<FS::cust_pkg>,
405 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>