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$/ ) {
177 my( $self, $value ) = @_; # $conf, $param
178 my $cust_main = qsearchs('cust_main', {
179 'agentnum' => $agentnum,
180 'agent_custid' => $value,
182 $self->custnum($cust_main->custnum) if $cust_main;
186 @fields = ( 'custnum' );
189 push @fields, ( 'pkgpart', 'discountnum' );
192 qw( start_date setup bill last_bill susp adjourn cancel expire )
195 my( $self, $value ) = @_; # $conf, $param
196 #->$field has undesirable effects
197 $self->set($field, parse_datetime($value) ); #$field closure
201 my $formatfields = _formatfields();
203 die "unknown format $format" unless $formatfields->{$format};
205 foreach my $field ( @{ $formatfields->{$format} } ) {
208 my( $self, $value, $conf, $param ) = @_;
209 $param->{"$format.$field"} = $value;
214 $opt->{'fields'} = \@fields;
216 FS::Record::batch_import( $opt );
223 my %cust_pkg = ( pkgpart => $pkgpart );
225 foreach my $field ( @fields ) {
227 if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) {
229 #$cust_pkg{$1} = parse_datetime( shift @$columns );
230 if ( $1 eq 'pkgpart' ) {
231 $cust_pkg{$1} = shift @columns;
232 } elsif ( $1 eq 'setup' ) {
233 $billtime = parse_datetime(shift @columns);
235 $cust_pkg{$1} = parse_datetime( shift @columns );
238 } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) {
240 $svc_x{$1} = shift @columns;
242 } elsif ( $field =~ /^svc_external\.(id|title)$/ ) {
244 $svc_x{$1} = shift @columns;
246 } elsif ( $field =~ /^svc_phone\.(countrycode|phonenum|sip_password|pin)$/ ) {
247 $svc_x{$1} = shift @columns;
252 if ( $field eq 'refnum' && $columns[0] !~ /^\s*(\d+)\s*$/ ) {
254 my $referral = $columns[0];
255 my %hash = ( 'referral' => $referral,
256 'agentnum' => $agentnum,
260 my $part_referral = qsearchs('part_referral', \%hash )
261 || new FS::part_referral \%hash;
263 unless ( $part_referral->refnum ) {
264 my $error = $part_referral->insert;
266 $dbh->rollback if $oldAutoCommit;
267 return "can't auto-insert advertising source: $referral: $error";
271 $columns[0] = $part_referral->refnum;
274 my $value = shift @columns;
275 $cust_main{$field} = $value if length($value);
279 $cust_main{'payby'} = 'CARD'
280 if defined $cust_main{'payinfo'}
281 && length $cust_main{'payinfo'};
283 my $invoicing_list = $cust_main{'invoicing_list'}
284 ? [ delete $cust_main{'invoicing_list'} ]
287 my $cust_main = new FS::cust_main ( \%cust_main );
290 tie my %hash, 'Tie::RefHash'; #this part is important
292 if ( $cust_pkg{'pkgpart'} ) {
293 my $cust_pkg = new FS::cust_pkg ( \%cust_pkg );
297 if ( $svc_x{'username'} ) {
299 } elsif ( $svc_x{'id'} || $svc_x{'title'} ) {
300 $svcdb = 'svc_external';
304 if ( $svc_x{'countrycode'} || $svc_x{'phonenum'} ) {
305 $svc_phone = FS::svc_phone->new( {
306 map { $_ => delete($svc_x{$_}) }
307 qw( countrycode phonenum sip_password pin)
311 if ( $svcdb || $svc_phone ) {
312 my $part_pkg = $cust_pkg->part_pkg;
313 unless ( $part_pkg ) {
314 $dbh->rollback if $oldAutoCommit;
315 return "unknown pkgpart: ". $cust_pkg{'pkgpart'};
318 $svc_x{svcpart} = $part_pkg->svcpart_unique_svcdb( $svcdb );
319 my $class = "FS::$svcdb";
320 push @svc_x, $class->new( \%svc_x );
323 $svc_phone->svcpart( $part_pkg->svcpart_unique_svcdb('svc_phone') );
324 push @svc_x, $svc_phone;
328 $hash{$cust_pkg} = \@svc_x;
331 my $error = $cust_main->insert( \%hash, $invoicing_list );
334 $dbh->rollback if $oldAutoCommit;
335 return "can't insert customer". ( $line ? " for $line" : '' ). ": $error";
338 if ( $format eq 'simple' ) {
340 #false laziness w/bill.cgi
341 $error = $cust_main->bill( 'time' => $billtime );
343 $dbh->rollback if $oldAutoCommit;
344 return "can't bill customer for $line: $error";
347 $error = $cust_main->apply_payments_and_credits;
349 $dbh->rollback if $oldAutoCommit;
350 return "can't bill customer for $line: $error";
353 $error = $cust_main->collect();
355 $dbh->rollback if $oldAutoCommit;
356 return "can't collect customer for $line: $error";
363 if ( $job && time - $min_sec > $last ) { #progress bar
364 $job->update_statustext( int(100 * $row / $count) );
370 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
372 return "Empty file!" unless $row;
380 Not enough documentation.
384 L<FS::cust_main>, L<FS::cust_pkg>,
385 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>