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 )],
115 my %import_options = (
116 'table' => 'cust_pkg',
118 'postinsert_callback' => sub {
119 my( $record, $param ) = @_;
121 my $formatfields = _formatfields;
122 foreach my $svc_x ( grep { $_ ne 'default' } keys %$formatfields ) {
124 my $ff = $formatfields->{$svc_x};
126 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
127 my $svc = "FS::$svc_x"->new( {
128 'pkgnum' => $record->pkgnum,
129 'svcpart' => $record->part_pkg->svcpart($svc_x),
130 map { $_ => $param->{"$svc_x.$_"} } @$ff
133 #this whole thing should be turned into a callback or config to turn on
134 if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
135 my($username, $domain) = split(/\@/, $svc->username);
136 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
137 || new FS::svc_domain { 'svcpart' => 1,
138 'domain' => $domain, };
139 unless ( $svc_domain->svcnum ) {
140 my $error = $svc_domain->insert;
141 return "error auto-inserting domain: $error" if $error;
143 $svc->username($username);
144 $svc->domsvc($svc_domain->svcnum);
147 my $error = $svc->insert;
148 return "error inserting service: $error" if $error;
158 sub _import_options {
165 my $iopt = _import_options;
166 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
168 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
170 my $format = delete $opt->{'format'};
173 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
175 my $agent_pkgid = $2;
178 my( $self, $value ) = @_; # $conf, $param
179 my $cust_main = qsearchs('cust_main', {
180 'agentnum' => $agentnum,
181 'agent_custid' => $value,
183 $self->custnum($cust_main->custnum) if $cust_main;
186 push @fields, 'agent_pkgid' if $agent_pkgid;
188 @fields = ( 'custnum' );
191 push @fields, ( 'pkgpart', 'discountnum' );
194 qw( start_date setup bill last_bill susp adjourn cancel expire )
197 my( $self, $value ) = @_; # $conf, $param
198 #->$field has undesirable effects
199 $self->set($field, parse_datetime($value) ); #$field closure
203 my $formatfields = _formatfields();
205 die "unknown format $format" unless $formatfields->{$format};
207 foreach my $field ( @{ $formatfields->{$format} } ) {
210 my( $self, $value, $conf, $param ) = @_;
211 $param->{"$format.$field"} = $value;
216 $opt->{'fields'} = \@fields;
218 FS::Record::batch_import( $opt );
225 my %cust_pkg = ( pkgpart => $pkgpart );
227 foreach my $field ( @fields ) {
229 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
231 #$cust_pkg{$1} = parse_datetime( shift @$columns );
232 if ( $1 eq 'pkgpart' ) {
233 $cust_pkg{$1} = shift @columns;
234 } elsif ( $1 eq 'setup' ) {
235 $billtime = parse_datetime(shift @columns);
237 $cust_pkg{$1} = parse_datetime( shift @columns );
240 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
242 $svc_x{$1} = shift @columns;
244 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
246 $svc_x{$1} = shift @columns;
248 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
249 $svc_x{$1} = shift @columns;
254 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
256 my $referral = $columns[0];
257 my %hash = ( 'referral' => $referral,
258 'agentnum' => $agentnum,
262 my $part_referral = qsearchs('part_referral', \%hash )
263 || new FS::part_referral \%hash;
265 unless ( $part_referral->refnum ) {
266 my $error = $part_referral->insert;
268 $dbh->rollback if $oldAutoCommit;
269 return "can't auto-insert advertising source: $referral: $error";
273 $columns[0] = $part_referral->refnum;
276 my $value = shift @columns;
277 $cust_main{$field} = $value if length($value);
281 $cust_main{'payby'} = 'CARD'
282 if defined $cust_main{'payinfo'}
283 && length $cust_main{'payinfo'};
285 my $invoicing_list = $cust_main{'invoicing_list'}
286 ? [ delete $cust_main{'invoicing_list'} ]
289 my $cust_main = new FS::cust_main ( \%cust_main );
292 tie my %hash, 'Tie::RefHash'; #this part is important
294 if ( $cust_pkg{'pkgpart'} ) {
295 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
299 if ( $svc_x{'username'} ) {
301 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
302 $svcdb = 'svc_external';
306 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
307 $svc_phone = FS::svc_phone->new( {
308 map { $_ => delete($svc_x{$_}) }
309 qw( countrycode phonenum sip_password pin)
313 if ( $svcdb || $svc_phone ) {
314 my $part_pkg = $cust_pkg->part_pkg;
315 unless ( $part_pkg ) {
316 $dbh->rollback if $oldAutoCommit;
317 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
320 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
321 my $class = "FS::$svcdb";
322 push @svc_x, $class->new( \%svc_x );
325 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
326 push @svc_x, $svc_phone;
330 $hash{$cust_pkg} = \@svc_x;
333 my $error = $cust_main->insert( \%hash, $invoicing_list );
336 $dbh->rollback if $oldAutoCommit;
337 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
340 if ( $format eq 'simple' ) {
342 #false laziness w/bill.cgi
343 $error = $cust_main->bill( 'time' => $billtime );
345 $dbh->rollback if $oldAutoCommit;
346 return "can't bill customer for $line: $error";
349 $error = $cust_main->apply_payments_and_credits;
351 $dbh->rollback if $oldAutoCommit;
352 return "can't bill customer for $line: $error";
355 $error = $cust_main->collect();
357 $dbh->rollback if $oldAutoCommit;
358 return "can't collect customer for $line: $error";
365 if ( $job && time - $min_sec > $last ) { #progress bar
366 $job->update_statustext( int(100 * $row / $count) );
372 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
374 return "Empty file!" unless $row;
382 Not enough documentation.
386 L<FS::cust_main>, L<FS::cust_pkg>,
387 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>