3 # Template for importing legacy customer data
5 # $Id: TEMPLATE_cust_main.import,v 1.3 1999-03-26 13:15:56 ivan Exp $
7 # ivan@sisd.com 98-aug-17 - 20
9 # $Log: TEMPLATE_cust_main.import,v $
10 # Revision 1.3 1999-03-26 13:15:56 ivan
11 # s/create/new/, use all necessary FS::table_names to avoid warnings
13 # Revision 1.2 1998/12/16 05:29:45 ivan
14 # adminsuidsetup now need user
19 use FS::UID qw(adminsuidsetup datasrc);
20 use FS::Record qw(fields qsearch qsearchs);
27 my $user = shift or die &usage;
30 # use these for the imported cust_main records (unless you have these in legacy
35 # map from legacy billing data to pkgpart, maps imported field
36 # LegacyBillingData to pkgpart. your names and pkgparts will be different
43 'Co-Located Server' => 15,
49 my($file)="legacy_file";
52 or die "Can't open $file: $!";
54 # put a tab-separated header atop the file, or define @fields
55 # (use these names or change them below)
71 # payby - CARD, BILL or COMP
72 # payinfo - Credit card #, P.O. # or COMP authorization
73 # paydate - Expiration
74 # tax - 'Y' for tax exempt
76 # LegacyBillingData - maps via %pkgpart above to a pkgpart
83 my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
84 #print join("\n",@fields);
87 my($link,$line)=(0,0);
90 next if /^[\s\t]*$/; #skip any blank lines
92 #define %svc hash for this record
93 my(@record)=split(/\t/);
96 $svc{$_}=shift @record;
99 # might need to massage some data like this
100 $svc{'payby'} =~ s/^Credit Card$/CARD/io;
101 $svc{'payby'} =~ s/^Check$/BILL/io;
102 $svc{'payby'} =~ s/^Cash$/BILL/io;
103 $svc{'payby'} =~ s/^$/BILL/o;
104 $svc{'First'} =~ s/&/and/go;
105 $svc{'Zip'} =~ s/\s+$//go;
107 my($cust_main) = new FS::cust_main ( {
108 'custnum' => $svc{'custnum'},
109 'agentnum' => $agentnum,
110 'last' => $svc{'last'},
111 'first' => $svc{'first'},
112 'company' => $svc{'company'},
113 'address1' => $svc{'address1'},
114 'address2' => $svc{'address2'},
115 'city' => $svc{'city'},
116 'state' => $svc{'state'},
117 'zip' => $svc{'zip'},
118 'country' => $svc{'country'},
119 'daytime' => $svc{'daytime'},
120 'night' => $svc{'night'},
121 'fax' => $svc{'fax'},
122 'payby' => $svc{'payby'},
123 'payinfo' => $svc{'payinfo'},
124 'paydate' => $svc{'paydate'},
125 'payname' => $svc{'payname'},
126 'tax' => $svc{'tax'},
130 $error=$cust_main->insert;
133 warn $cust_main->_dump;
134 warn map "$_: ". $svc{$_}. "|\n", keys %svc;
138 my($cust_pkg)=new FS::cust_pkg ( {
139 'custnum' => $svc{'custnum'},
140 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
148 $error=$cust_pkg->insert;
150 warn $svc{'LegacyBillingData'};
154 unless ( $svc{'username'} ) {
157 #find svc_acct record (imported with bin/svc_acct.import) for this username
158 my($svc_acct)=qsearchs('svc_acct',{'username'=>$svc{'username'}});
159 unless ( $svc_acct ) {
160 warn "username ", $svc{'username'}, " not found\n";
162 #link to the cust_pkg record we created above
164 #find cust_svc record for this svc_acct record
165 my($o_cust_svc)=qsearchs('cust_svc',{
166 'svcnum' => $svc_acct->svcnum,
169 unless ( $o_cust_svc ) {
170 warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
173 #make sure this svcpart is in pkgpart
174 my($pkg_svc)=qsearchs('pkg_svc',{
175 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
176 'svcpart' => $o_cust_svc->svcpart,
179 unless ( $pkg_svc ) {
180 warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
181 " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
184 #create new cust_svc record linked to cust_pkg record
185 my($n_cust_svc) = new FS::cust_svc ({
186 'svcnum' => $o_cust_svc->svcnum,
187 'pkgnum' => $cust_pkg->pkgnum,
188 'svcpart' => $pkg_svc->svcpart,
190 my($error) = $n_cust_svc->replace($o_cust_svc);
191 die $error if $error;
202 warn "\n$link of $line lines linked\n";
207 die "Usage:\n\n cust_main.import user\n";