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;
104 'svc_acct' => [qw( username _password domsvc )],
105 'svc_phone' => [qw( countrycode phonenum sip_password pin )],
106 'svc_external' => [qw( id title )],
107 'location' => [qw( address1 address2 city state zip country )],
108 'quan_price' => [qw( quantity setup_fee recur_fee )],
115 my %import_options = (
116 'table' => 'cust_pkg',
118 'preinsert_callback' => sub {
119 my($record, $param) = @_;
121 my @location_params = grep { /^location\./ && length($param->{$_}) }
123 if (@location_params) {
124 warn join('-', @location_params);
125 my $cust_location = FS::cust_location->new({
126 'custnum' => $record->custnum,
128 foreach my $p (@location_params) {
129 $p =~ /^location.(\w+)$/;
130 $cust_location->set($1, $param->{$p});
133 my $error = $cust_location->find_or_insert; # this avoids duplicates
134 return "error creating location: $error" if $error;
135 $record->set('locationnum', $cust_location->locationnum);
138 $record->quantity( $param->{'quan_price.quantity'} )
139 if $param->{'quan_price.quantity'} > 0;
141 my $s = $param->{'quan_price.setup_fee'};
142 my $r = $param->{'quan_price.recur_fee'};
143 my $part_pkg = $record->part_pkg;
144 if ( ( $s && $s != $part_pkg->option('setup_fee') )
145 or ( $r && $r != $part_pkg->option('recur_fee') )
148 my $custom_part_pkg = $part_pkg->clone;
149 $custom_part_pkg->disabled('Y');
150 my %options = $part_pkg->options;
151 $options{'setup_fee'} = $s if $s;
152 $options{'recur_fee'} = $r if $r;
153 my $error = $custom_part_pkg->insert( options=>\%options );
154 return "error customizing package: $error" if $error;
155 $record->pkgpart( $custom_part_pkg->pkgpart );
162 'postinsert_callback' => sub {
163 my( $record, $param ) = @_;
165 my $formatfields = _formatfields;
166 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
168 my $ff = $formatfields->{$svc_x};
170 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
171 my $svc = "FS::$svc_x"->new( {
172 'pkgnum' => $record->pkgnum,
173 'svcpart' => $record->part_pkg->svcpart($svc_x),
174 map { $_ => $param->{"$svc_x.$_"} } @$ff
177 #this whole thing should be turned into a callback or config to turn on
178 if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
179 my($username, $domain) = split(/\@/, $svc->username);
180 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
181 || new FS::svc_domain { 'svcpart' => 1,
182 'domain' => $domain, };
183 unless ( $svc_domain->svcnum ) {
184 my $error = $svc_domain->insert;
185 return "error auto-inserting domain: $error" if $error;
187 $svc->username($username);
188 $svc->domsvc($svc_domain->svcnum);
191 my $error = $svc->insert;
192 return "error inserting service: $error" if $error;
202 sub _import_options {
209 my $iopt = _import_options;
210 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
212 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
214 my $format = delete $opt->{'format'};
217 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
219 my $agent_pkgid = $2;
222 my( $self, $value ) = @_; # $conf, $param
223 my $cust_main = qsearchs('cust_main', {
224 'agentnum' => $agentnum,
225 'agent_custid' => $value,
227 $self->custnum($cust_main->custnum) if $cust_main;
230 push @fields, 'agent_pkgid' if $agent_pkgid;
232 @fields = ( 'custnum' );
235 if ( $format =~ /^(.*)-locationnum$/ ) {
237 push @fields, 'locationnum';
240 if ( $format =~ /^bulk_(.*)$/ ) {
244 $opt->{'postinsert_callback'} = sub {
245 my( $record, $param ) = @_;
247 my $formatfields = _formatfields;
248 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
250 my $ff = $formatfields->{$svc_x};
252 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
254 $param->{'svc_phone.phonenum'} =~ /^\s*(\d+)\s*\-\s*(\d+)\s*$/
255 or return 'Enter a phone number range, with dash as the separator';
256 my($start, $end) = ($1, $2);
257 if ( length($end) < length($start) ) {
258 $end = substr($start, 0, length($start) - length($end) ). $end;
261 foreach my $phonenum ( "$start" .. "$end" ) {
263 my $svc = "FS::$svc_x"->new( {
264 'pkgnum' => $record->pkgnum,
265 'svcpart' => $record->part_pkg->svcpart($svc_x),
266 map { $_ => $param->{"$svc_x.$_"} } @$ff
269 $svc->phonenum($phonenum);
270 #$svc->set_default_and_fixed;
271 my $error = $svc->insert;
272 return "error inserting service: $error" if $error;
286 push @fields, ( 'pkgpart', 'discountnum' );
288 my @date_fields = ();
289 if ( $format =~ /all_dates/ ) {
292 start_date setup bill last_bill susp adjourn
299 start_date setup bill last_bill susp adjourn
304 foreach my $field (@date_fields) {
306 my( $self, $value ) = @_; # $conf, $param
307 #->$field has undesirable effects
308 $self->set($field, parse_datetime($value) ); #$field closure
312 my @formats = split /-/, $format;
313 foreach my $f (@formats){
315 my $formatfields = _formatfields();
316 die "unknown format $format" unless $formatfields->{$f};
318 foreach my $field ( @{ $formatfields->{$f} } ) {
321 my( $self, $value, $conf, $param ) = @_;
322 $param->{"$f.$field"} = $value;
328 $opt->{'fields'} = \@fields;
330 FS::Record::batch_import( $opt );
336 Not enough documentation.
340 L<FS::cust_main>, L<FS::cust_pkg>,
341 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>