3 # Template for importing legacy customer data
7 use FS::UID qw(adminsuidsetup datasrc);
8 use FS::Record qw(fields qsearch qsearchs);
15 my $user = shift or die &usage;
18 # use these for the imported cust_main records (unless you have these in legacy
23 # map from legacy billing data to pkgpart, maps imported field
24 # LegacyBillingData to pkgpart. your names and pkgparts will be different
31 'Co-Located Server' => 15,
37 my($file)="legacy_file";
40 or die "Can't open $file: $!";
42 # put a tab-separated header atop the file, or define @fields
43 # (use these names or change them below)
59 # payby - CARD, BILL or COMP
60 # payinfo - Credit card #, P.O. # or COMP authorization
61 # paydate - Expiration
62 # tax - 'Y' for tax exempt
64 # LegacyBillingData - maps via %pkgpart above to a pkgpart
71 my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
72 #print join("\n",@fields);
75 my($link,$line)=(0,0);
78 next if /^[\s\t]*$/; #skip any blank lines
80 #define %svc hash for this record
81 my(@record)=split(/\t/);
84 $svc{$_}=shift @record;
87 # might need to massage some data like this
88 $svc{'payby'} =~ s/^Credit Card$/CARD/io;
89 $svc{'payby'} =~ s/^Check$/BILL/io;
90 $svc{'payby'} =~ s/^Cash$/BILL/io;
91 $svc{'payby'} =~ s/^$/BILL/o;
92 $svc{'First'} =~ s/&/and/go;
93 $svc{'Zip'} =~ s/\s+$//go;
95 my($cust_main) = new FS::cust_main ( {
96 'custnum' => $svc{'custnum'},
97 'agentnum' => $agentnum,
98 'last' => $svc{'last'},
99 'first' => $svc{'first'},
100 'company' => $svc{'company'},
101 'address1' => $svc{'address1'},
102 'address2' => $svc{'address2'},
103 'city' => $svc{'city'},
104 'state' => $svc{'state'},
105 'zip' => $svc{'zip'},
106 'country' => $svc{'country'},
107 'daytime' => $svc{'daytime'},
108 'night' => $svc{'night'},
109 'fax' => $svc{'fax'},
110 'payby' => $svc{'payby'},
111 'payinfo' => $svc{'payinfo'},
112 'paydate' => $svc{'paydate'},
113 'payname' => $svc{'payname'},
114 'tax' => $svc{'tax'},
118 $error=$cust_main->insert;
121 warn $cust_main->_dump;
122 warn map "$_: ". $svc{$_}. "|\n", keys %svc;
126 my($cust_pkg)=new FS::cust_pkg ( {
127 'custnum' => $svc{'custnum'},
128 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
136 $error=$cust_pkg->insert;
138 warn $svc{'LegacyBillingData'};
142 unless ( $svc{'username'} ) {
145 #find svc_acct record (imported with bin/svc_acct.import) for this username
146 my($svc_acct)=qsearchs('svc_acct',{'username'=>$svc{'username'}});
147 unless ( $svc_acct ) {
148 warn "username ", $svc{'username'}, " not found\n";
150 #link to the cust_pkg record we created above
152 #find cust_svc record for this svc_acct record
153 my($o_cust_svc)=qsearchs('cust_svc',{
154 'svcnum' => $svc_acct->svcnum,
157 unless ( $o_cust_svc ) {
158 warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
161 #make sure this svcpart is in pkgpart
162 my($pkg_svc)=qsearchs('pkg_svc',{
163 'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
164 'svcpart' => $o_cust_svc->svcpart,
167 unless ( $pkg_svc ) {
168 warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
169 " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
172 #create new cust_svc record linked to cust_pkg record
173 my($n_cust_svc) = new FS::cust_svc ({
174 'svcnum' => $o_cust_svc->svcnum,
175 'pkgnum' => $cust_pkg->pkgnum,
176 'svcpart' => $pkg_svc->svcpart,
178 my($error) = $n_cust_svc->replace($o_cust_svc);
179 die $error if $error;
190 warn "\n$link of $line lines linked\n";
195 die "Usage:\n\n cust_main.import user\n";