1 package FS::contact::Import;
4 use vars qw( $DEBUG ); #$conf );
6 use FS::Misc::DateTime qw( parse_datetime );
7 use FS::Record qw( qsearchs );
15 FS::contact::Import - Batch contact importing
19 use FS::contact::Import;
22 FS::contact::Import::batch_import( {
23 file => $file, #filename
24 type => $type, #csv or xls
25 format => $format, #default
26 agentnum => $agentnum,
27 job => $job, #optional job queue job, for progressbar updates
28 pkgbatch => $pkgbatch, #optional batch unique identifier
33 use FS::UI::Web::JSRPC;
35 new FS::UI::Web::JSRPC 'FS::contact::Import::process_batch_import', $cgi;
36 print $server->process;
40 Batch contact importing.
44 =item process_batch_import
46 Load a batch import as a queued JSRPC job
50 sub process_batch_import {
53 warn Dumper($param) if $DEBUG;
55 my $files = $param->{'uploaded_files'}
56 or die "No files provided.\n";
58 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
60 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
61 #my $dir = '/usr/local/etc/freeside/cache.'. $FS::UID::datasrc. '/';
62 my $file = $dir. $files{'file'};
65 if ( $file =~ /\.(\w+)$/i ) {
69 warn "can't parse file type from filename $file; defaulting to CSV";
74 FS::contact::Import::batch_import( {
78 agentnum => $param->{'agentnum'},
79 'format' => $param->{'format'},
84 die "$error\n" if $error;
93 'default' => [ qw( custnum last first title comment selfservice_access emailaddress phonetypenum1 phonetypenum3 phonetypenum2 ) ],
100 ## not tested but maybe allow 2nd format to attach location in the future
101 my %import_options = (
102 'table' => 'contact',
104 'preinsert_callback' => sub {
105 my($record, $param) = @_;
106 my @location_params = grep /^location\./, keys %$param;
107 if (@location_params) {
108 my $cust_location = FS::cust_location->new({
109 'custnum' => $record->custnum,
111 foreach my $p (@location_params) {
112 $p =~ /^location.(\w+)$/;
113 $cust_location->set($1, $param->{$p});
116 my $error = $cust_location->find_or_insert; # this avoids duplicates
117 return "error creating location: $error" if $error;
118 $record->set('locationnum', $cust_location->locationnum);
125 sub _import_options {
132 my $iopt = _import_options;
133 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
135 my $format = delete $opt->{'format'};
137 my $formatfields = _formatfields();
138 die "unknown format $format" unless $formatfields->{$format};
141 foreach my $field ( @{ $formatfields->{$format} } ) {
142 push @fields, $field;
145 $opt->{'fields'} = \@fields;
147 FS::Record::batch_import( $opt );
153 Not enough documentation.