1 package FS::contact::Import;
4 use vars qw( $DEBUG ); #$conf );
8 use FS::Misc::DateTime qw( parse_datetime );
9 use FS::Record qw( qsearchs );
17 FS::contact::Import - Batch contact importing
21 use FS::contact::Import;
24 FS::contact::Import::batch_import( {
25 file => $file, #filename
26 type => $type, #csv or xls
27 format => $format, #default
28 agentnum => $agentnum,
29 job => $job, #optional job queue job, for progressbar updates
30 pkgbatch => $pkgbatch, #optional batch unique identifier
35 use FS::UI::Web::JSRPC;
37 new FS::UI::Web::JSRPC 'FS::contact::Import::process_batch_import', $cgi;
38 print $server->process;
42 Batch contact importing.
46 =item process_batch_import
48 Load a batch import as a queued JSRPC job
52 sub process_batch_import {
55 my $param = thaw(decode_base64(shift));
56 warn Dumper($param) if $DEBUG;
58 my $files = $param->{'uploaded_files'}
59 or die "No files provided.\n";
61 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
63 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
64 #my $dir = '/usr/local/etc/freeside/cache.'. $FS::UID::datasrc. '/';
65 my $file = $dir. $files{'file'};
68 if ( $file =~ /\.(\w+)$/i ) {
72 warn "can't parse file type from filename $file; defaulting to CSV";
77 FS::contact::Import::batch_import( {
81 agentnum => $param->{'agentnum'},
82 'format' => $param->{'format'},
87 die "$error\n" if $error;
96 'default' => [ qw( custnum last first title comment selfservice_access emailaddress phonetypenum1 phonetypenum3 phonetypenum2 ) ],
103 ## not tested but maybe allow 2nd format to attach location in the future
104 my %import_options = (
105 'table' => 'contact',
107 'preinsert_callback' => sub {
108 my($record, $param) = @_;
109 my @location_params = grep /^location\./, keys %$param;
110 if (@location_params) {
111 my $cust_location = FS::cust_location->new({
112 'custnum' => $record->custnum,
114 foreach my $p (@location_params) {
115 $p =~ /^location.(\w+)$/;
116 $cust_location->set($1, $param->{$p});
119 my $error = $cust_location->find_or_insert; # this avoids duplicates
120 return "error creating location: $error" if $error;
121 $record->set('locationnum', $cust_location->locationnum);
128 sub _import_options {
135 my $iopt = _import_options;
136 $opt->{$_} = $iopt->{$_} foreach keys %$iopt;
138 my $format = delete $opt->{'format'};
140 my $formatfields = _formatfields();
141 die "unknown format $format" unless $formatfields->{$format};
144 foreach my $field ( @{ $formatfields->{$format} } ) {
145 push @fields, $field;
148 $opt->{'fields'} = \@fields;
150 FS::Record::batch_import( $opt );
156 Not enough documentation.