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 my $error = $cust_location->find_or_insert; # this avoids duplicates
129 return "error creating location: $error" if $error;
130 $record->set('locationnum', $cust_location->locationnum);
135 'postinsert_callback' => sub {
136 my( $record, $param ) = @_;
138 my $formatfields = _formatfields;
139 foreach my $svc_x ( grep /^svc/, keys %$formatfields ) {
141 my $ff = $formatfields->{$svc_x};
143 if ( grep $param->{"$svc_x.$_"}, @$ff ) {
144 my $svc = "FS::$svc_x"->new( {
145 'pkgnum' => $record->pkgnum,
146 'svcpart' => $record->part_pkg->svcpart($svc_x),
147 map { $_ => $param->{"$svc_x.$_"} } @$ff
150 #this whole thing should be turned into a callback or config to turn on
151 if ( $svc_x eq 'svc_acct' && $svc->username =~ /\@/ ) {
152 my($username, $domain) = split(/\@/, $svc->username);
153 my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
154 || new FS::svc_domain { 'svcpart' => 1,
155 'domain' => $domain, };
156 unless ( $svc_domain->svcnum ) {
157 my $error = $svc_domain->insert;
158 return "error auto-inserting domain: $error" if $error;
160 $svc->username($username);
161 $svc->domsvc($svc_domain->svcnum);
164 my $error = $svc->insert;
165 return "error inserting service: $error" if $error;
175 sub _import_options {
182 my $iopt = _import_options;
183 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
185 my $agentnum = delete $opt->{agentnum}; # i like closures (delete though?)
187 my $format = delete $opt->{'format'};
190 if ( $format =~ /^(.*)-agent_custid(-agent_pkgid)?$/ ) {
192 my $agent_pkgid = $2;
195 my( $self, $value ) = @_; # $conf, $param
196 my $cust_main = qsearchs('cust_main', {
197 'agentnum' => $agentnum,
198 'agent_custid' => $value,
200 $self->custnum($cust_main->custnum) if $cust_main;
203 push @fields, 'agent_pkgid' if $agent_pkgid;
205 @fields = ( 'custnum' );
208 push @fields, ( 'pkgpart', 'discountnum' );
210 my @date_fields = ();
211 if ( $format =~ /all_dates/ ) {
214 start_date setup bill last_bill susp adjourn
221 start_date setup bill last_bill susp adjourn
226 foreach my $field (@date_fields) {
228 my( $self, $value ) = @_; # $conf, $param
229 #->$field has undesirable effects
230 $self->set($field, parse_datetime($value) ); #$field closure
234 my $formatfields = _formatfields();
236 die "unknown format $format" unless $formatfields->{$format};
238 foreach my $field ( @{ $formatfields->{$format} } ) {
241 my( $self, $value, $conf, $param ) = @_;
242 $param->{"$format.$field"} = $value;
247 $opt->{'fields'} = \@fields;
249 FS::Record::batch_import( $opt );
255 Not enough documentation.
259 L<FS::cust_main>, L<FS::cust_pkg>,
260 L<FS::svc_acct>, L<FS::svc_external>, L<FS::svc_phone>