3 # Template for importing legacy customer data
5 # $Id: TEMPLATE_cust_main.import,v 1.4 2001-08-21 02:44:47 ivan Exp $
9 use FS::UID qw(adminsuidsetup datasrc);
10 use FS::Record qw(fields qsearch qsearchs);
17 my $user = shift or die &usage;
20 # use these for the imported cust_main records (unless you have these in legacy
25 # map from legacy billing data to pkgpart, maps imported field
26 # LegacyBillingData to pkgpart. your names and pkgparts will be different
33 'Co-Located Server' => 15,
39 my($file)="legacy_file";
42 or die "Can't open $file: $!";
44 # put a tab-separated header atop the file, or define @fields
45 # (use these names or change them below)
61 # payby - CARD, BILL or COMP
62 # payinfo - Credit card #, P.O. # or COMP authorization
63 # paydate - Expiration
64 # tax - 'Y' for tax exempt
66 # LegacyBillingData - maps via %pkgpart above to a pkgpart
73 my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
74 #print join("\n",@fields);
77 my($link,$line)=(0,0);
80 next if /^[\s\t]*$/; #skip any blank lines
82 #define %svc hash for this record
83 my(@record)=split(/\t/);
86 $svc{$_}=shift @record;
89 # might need to massage some data like this
90 $svc{'payby'} =~ s/^Credit Card$/CARD/io;
91 $svc{'payby'} =~ s/^Check$/BILL/io;
92 $svc{'payby'} =~ s/^Cash$/BILL/io;
93 $svc{'payby'} =~ s/^$/BILL/o;
94 $svc{'First'} =~ s/&/and/go;
95 $svc{'Zip'} =~ s/\s+$//go;
97 my($cust_main) = new FS::cust_main ( {
98 'custnum' => $svc{'custnum'},
99 'agentnum' => $agentnum,
100 'last' => $svc{'last'},
101 'first' => $svc{'first'},
102 'company' => $svc{'company'},
103 'address1' => $svc{'address1'},
104 'address2' => $svc{'address2'},
105 'city' => $svc{'city'},
106 'state' => $svc{'state'},
107 'zip' => $svc{'zip'},
108 'country' => $svc{'country'},
109 'daytime' => $svc{'daytime'},
110 'night' => $svc{'night'},
111 'fax' => $svc{'fax'},
112 'payby' => $svc{'payby'},
113 'payinfo' => $svc{'payinfo'},
114 'paydate' => $svc{'paydate'},
115 'payname' => $svc{'payname'},
116 'tax' => $svc{'tax'},
120 $error=$cust_main->insert;
123 warn $cust_main->_dump;
124 warn map "$_: ". $svc{$_}. "|\n", keys %svc;
128 my($cust_pkg)=new FS::cust_pkg ( {
129 'custnum' => $svc{'custnum'},
130 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
138 $error=$cust_pkg->insert;
140 warn $svc{'LegacyBillingData'};
144 unless ( $svc{'username'} ) {
147 #find svc_acct record (imported with bin/svc_acct.import) for this username
148 my($svc_acct)=qsearchs('svc_acct',{'username'=>$svc{'username'}});
149 unless ( $svc_acct ) {
150 warn "username ", $svc{'username'}, " not found\n";
152 #link to the cust_pkg record we created above
154 #find cust_svc record for this svc_acct record
155 my($o_cust_svc)=qsearchs('cust_svc',{
156 'svcnum' => $svc_acct->svcnum,
159 unless ( $o_cust_svc ) {
160 warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
163 #make sure this svcpart is in pkgpart
164 my($pkg_svc)=qsearchs('pkg_svc',{
165 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
166 'svcpart' => $o_cust_svc->svcpart,
169 unless ( $pkg_svc ) {
170 warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
171 " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
174 #create new cust_svc record linked to cust_pkg record
175 my($n_cust_svc) = new FS::cust_svc ({
176 'svcnum' => $o_cust_svc->svcnum,
177 'pkgnum' => $cust_pkg->pkgnum,
178 'svcpart' => $pkg_svc->svcpart,
180 my($error) = $n_cust_svc->replace($o_cust_svc);
181 die $error if $error;
192 warn "\n$link of $line lines linked\n";
197 die "Usage:\n\n cust_main.import user\n";