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 );
18 #install_callback FS::UID sub {
19 # $conf = new FS::Conf;
24 FS::cust_pkg::Import - Batch customer importing
28 use FS::cust_pkg::Import;
31 FS::cust_pkg::Import::batch_import( {
32 file => $file, #filename
33 type => $type, #csv or xls
34 format => $format, #extended, extended-plus_company, svc_external,
35 # or svc_external_svc_phone
36 agentnum => $agentnum,
37 job => $job, #optional job queue job, for progressbar updates
38 pkgbatch => $pkgbatch, #optional batch unique identifier
43 use FS::UI::Web::JSRPC;
45 new FS::UI::Web::JSRPC 'FS::cust_pkg::Import::process_batch_import', $cgi;
46 print $server->process;
50 Batch package importing.
54 =item process_batch_import
56 Load a batch import as a queued JSRPC job
60 sub process_batch_import {
63 my $param = thaw(decode_base64(shift));
64 warn Dumper($param) if $DEBUG;
66 my $files = $param->{'uploaded_files'}
67 or die "No files provided.\n";
69 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
71 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
72 my $file = $dir. $files{'file'};
75 if ( $file =~ /\.(\w+)$/i ) {
79 warn "can't parse file type from filename $file; defaulting to CSV";
84 FS::cust_pkg::Import::batch_import( {
88 'params' => { pkgbatch => $param->{pkgbatch} },
89 agentnum => $param->{'agentnum'},
90 'format' => $param->{'format'},
95 die "$error\n" if $error;
105 'svc_acct' => [qw( username _password domsvc )],
106 'svc_phone' => [qw( countrycode phonenum sip_password pin )],
107 'svc_external' => [qw( id title )],
114 my %import_options = (
115 'table' => 'cust_pkg',
117 'postinsert_callback' => sub {
118 my( $record, $param ) = @_;
120 my $formatfields = _formatfields;
121 foreach my $svc_x ( grep { $_ ne 'default' } keys %$formatfields ) {
123 my $ff = $formatfields->{$svc_x};
125 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
126 my $svc_x = "FS::$svc_x"->new( {
127 'pkgnum' => $record->pkgnum,
128 'svcpart' => $record->part_pkg->svcpart($svc_x),
129 map { $_ => $param->{"$svc_x.$_"} } @$ff
131 my $error = $svc_x->insert;
132 return $error if $error;
142 sub _import_options {
149 my $iopt = _import_options;
150 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
152 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
154 my $format = delete $opt->{'format'};
157 if ( $format =~ /^(.*)-agent_custid$/ ) {
161 my( $self, $value ) = @_; # $conf, $param
162 my $cust_main = qsearchs('cust_main', {
163 'agentnum' => $agentnum,
164 'agent_custid' => $value,
166 $self->custnum($cust_main->custnum) if $cust_main;
170 @fields = ( 'custnum' );
173 push @fields, ( 'pkgpart', 'discountnum' );
176 qw( start_date setup bill last_bill susp adjourn cancel expire )
179 my( $self, $value ) = @_; # $conf, $param
180 #->$field has undesirable effects
181 $self->set($field, parse_datetime($value) ); #$field closure
185 my $formatfields = _formatfields();
187 die "unknown format $format" unless $formatfields->{$format};
189 foreach my $field ( @{ $formatfields->{$format} } ) {
192 my( $self, $value, $conf, $param ) = @_;
193 $param->{"$format.$field"} = $value;
198 $opt->{'fields'} = \@fields;
200 FS::Record::batch_import( $opt );
207 my %cust_pkg = ( pkgpart => $pkgpart );
209 foreach my $field ( @fields ) {
211 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
213 #$cust_pkg{$1} = parse_datetime( shift @$columns );
214 if ( $1 eq 'pkgpart' ) {
215 $cust_pkg{$1} = shift @columns;
216 } elsif ( $1 eq 'setup' ) {
217 $billtime = parse_datetime(shift @columns);
219 $cust_pkg{$1} = parse_datetime( shift @columns );
222 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
224 $svc_x{$1} = shift @columns;
226 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
228 $svc_x{$1} = shift @columns;
230 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
231 $svc_x{$1} = shift @columns;
236 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
238 my $referral = $columns[0];
239 my %hash = ( 'referral' => $referral,
240 'agentnum' => $agentnum,
244 my $part_referral = qsearchs('part_referral', \%hash )
245 || new FS::part_referral \%hash;
247 unless ( $part_referral->refnum ) {
248 my $error = $part_referral->insert;
250 $dbh->rollback if $oldAutoCommit;
251 return "can't auto-insert advertising source: $referral: $error";
255 $columns[0] = $part_referral->refnum;
258 my $value = shift @columns;
259 $cust_main{$field} = $value if length($value);
263 $cust_main{'payby'} = 'CARD'
264 if defined $cust_main{'payinfo'}
265 && length $cust_main{'payinfo'};
267 my $invoicing_list = $cust_main{'invoicing_list'}
268 ? [ delete $cust_main{'invoicing_list'} ]
271 my $cust_main = new FS::cust_main ( \%cust_main );
274 tie my %hash, 'Tie::RefHash'; #this part is important
276 if ( $cust_pkg{'pkgpart'} ) {
277 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
281 if ( $svc_x{'username'} ) {
283 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
284 $svcdb = 'svc_external';
288 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
289 $svc_phone = FS::svc_phone->new( {
290 map { $_ => delete($svc_x{$_}) }
291 qw( countrycode phonenum sip_password pin)
295 if ( $svcdb || $svc_phone ) {
296 my $part_pkg = $cust_pkg->part_pkg;
297 unless ( $part_pkg ) {
298 $dbh->rollback if $oldAutoCommit;
299 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
302 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
303 my $class = "FS::$svcdb";
304 push @svc_x, $class->new( \%svc_x );
307 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
308 push @svc_x, $svc_phone;
312 $hash{$cust_pkg} = \@svc_x;
315 my $error = $cust_main->insert( \%hash, $invoicing_list );
318 $dbh->rollback if $oldAutoCommit;
319 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
322 if ( $format eq 'simple' ) {
324 #false laziness w/bill.cgi
325 $error = $cust_main->bill( 'time' => $billtime );
327 $dbh->rollback if $oldAutoCommit;
328 return "can't bill customer for $line: $error";
331 $error = $cust_main->apply_payments_and_credits;
333 $dbh->rollback if $oldAutoCommit;
334 return "can't bill customer for $line: $error";
337 $error = $cust_main->collect();
339 $dbh->rollback if $oldAutoCommit;
340 return "can't collect customer for $line: $error";
347 if ( $job && time - $min_sec > $last ) { #progress bar
348 $job->update_statustext( int(100 * $row / $count) );
354 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
356 return "Empty file!" unless $row;
364 Not enough documentation.
368 L<FS::cust_main>, L<FS::cust_pkg>,
369 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>