3 # Template for importing legacy customer data
5 # $Id: TEMPLATE_cust_main.import,v 1.2 1998-12-16 05:29:45 ivan Exp $
7 # ivan@sisd.com 98-aug-17 - 20
9 # $Log: TEMPLATE_cust_main.import,v $
10 # Revision 1.2 1998-12-16 05:29:45 ivan
11 # adminsuidsetup now need user
15 use FS::UID qw(adminsuidsetup datasrc);
16 use FS::Record qw(fields qsearch qsearchs);
21 my $user = shift or die &usage;
24 # use these for the imported cust_main records (unless you have these in legacy
29 # map from legacy billing data to pkgpart, maps imported field
30 # LegacyBillingData to pkgpart. your names and pkgparts will be different
37 'Co-Located Server' => 15,
43 my($file)="legacy_file";
46 or die "Can't open $file: $!";
48 # put a tab-separated header atop the file, or define @fields
49 # (use these names or change them below)
65 # payby - CARD, BILL or COMP
66 # payinfo - Credit card #, P.O. # or COMP authorization
67 # paydate - Expiration
68 # tax - 'Y' for tax exempt
70 # LegacyBillingData - maps via %pkgpart above to a pkgpart
77 my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
78 #print join("\n",@fields);
81 my($link,$line)=(0,0);
84 next if /^[\s\t]*$/; #skip any blank lines
86 #define %svc hash for this record
87 my(@record)=split(/\t/);
90 $svc{$_}=shift @record;
93 # might need to massage some data like this
94 $svc{'payby'} =~ s/^Credit Card$/CARD/io;
95 $svc{'payby'} =~ s/^Check$/BILL/io;
96 $svc{'payby'} =~ s/^Cash$/BILL/io;
97 $svc{'payby'} =~ s/^$/BILL/o;
98 $svc{'First'} =~ s/&/and/go;
99 $svc{'Zip'} =~ s/\s+$//go;
101 my($cust_main) = create FS::cust_main ( {
102 'custnum' => $svc{'custnum'},
103 'agentnum' => $agentnum,
104 'last' => $svc{'last'},
105 'first' => $svc{'first'},
106 'company' => $svc{'company'},
107 'address1' => $svc{'address1'},
108 'address2' => $svc{'address2'},
109 'city' => $svc{'city'},
110 'state' => $svc{'state'},
111 'zip' => $svc{'zip'},
112 'country' => $svc{'country'},
113 'daytime' => $svc{'daytime'},
114 'night' => $svc{'night'},
115 'fax' => $svc{'fax'},
116 'payby' => $svc{'payby'},
117 'payinfo' => $svc{'payinfo'},
118 'paydate' => $svc{'paydate'},
119 'payname' => $svc{'payname'},
120 'tax' => $svc{'tax'},
124 $error=$cust_main->insert;
127 warn $cust_main->_dump;
128 warn map "$_: ". $svc{$_}. "|\n", keys %svc;
132 my($cust_pkg)=create FS::cust_pkg ( {
133 'custnum' => $svc{'custnum'},
134 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
142 $error=$cust_pkg->insert;
144 warn $svc{'LegacyBillingData'};
148 unless ( $svc{'username'} ) {
151 #find svc_acct record (imported with bin/svc_acct.import) for this username
152 my($svc_acct)=qsearchs('svc_acct',{'username'=>$svc{'username'}});
153 unless ( $svc_acct ) {
154 warn "username ", $svc{'username'}, " not found\n";
156 #link to the cust_pkg record we created above
158 #find cust_svc record for this svc_acct record
159 my($o_cust_svc)=qsearchs('cust_svc',{
160 'svcnum' => $svc_acct->svcnum,
163 unless ( $o_cust_svc ) {
164 warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
167 #make sure this svcpart is in pkgpart
168 my($pkg_svc)=qsearchs('pkg_svc',{
169 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
170 'svcpart' => $o_cust_svc->svcpart,
173 unless ( $pkg_svc ) {
174 warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
175 " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
178 #create new cust_svc record linked to cust_pkg record
179 my($n_cust_svc) = create FS::cust_svc ({
180 'svcnum' => $o_cust_svc->svcnum,
181 'pkgnum' => $cust_pkg->pkgnum,
182 'svcpart' => $pkg_svc->svcpart,
184 my($error) = $n_cust_svc->replace($o_cust_svc);
185 die $error if $error;
196 warn "\n$link of $line lines linked\n";
201 die "Usage:\n\n cust_main.import user\n";