adminsuidsetup now need user
[freeside.git] / eg / TEMPLATE_cust_main.import
1 #!/usr/bin/perl -w
2
3 # Template for importing legacy customer data
4 #
5 # $Id: TEMPLATE_cust_main.import,v 1.2 1998-12-16 05:29:45 ivan Exp $
6 #
7 # ivan@sisd.com 98-aug-17 - 20
8 #
9 # $Log: TEMPLATE_cust_main.import,v $
10 # Revision 1.2  1998-12-16 05:29:45  ivan
11 # adminsuidsetup now need user
12 #
13
14 use strict;
15 use FS::UID qw(adminsuidsetup datasrc);
16 use FS::Record qw(fields qsearch qsearchs);
17 use FS::cust_main;
18 use FS::cust_pkg;
19 use Date::Parse;
20
21 my $user = shift or die &usage;
22 adminsuidsetup $user;
23
24 # use these for the imported cust_main records (unless you have these in legacy
25 # data)
26 my($agentnum)=4;
27 my($refnum)=5;
28
29 # map from legacy billing data to pkgpart, maps imported field
30 # LegacyBillingData to pkgpart.  your names and pkgparts will be different
31 my(%pkgpart)=(
32   'Employee'          => 10,
33   'Business'          => 11,
34   'Individual'        => 12,
35   'Basic PPP'         => 13,
36   'Slave'             => 14,
37   'Co-Located Server' => 15,
38   'Virtual Web'       => 16,
39   'Perk Mail'         => 17,
40   'Credit Hold'       => 18,
41 );
42
43 my($file)="legacy_file";
44
45 open(CLIENT,$file) 
46   or die "Can't open $file: $!"; 
47
48 # put a tab-separated header atop the file, or define @fields
49 #   (use these names or change them below)
50 #
51 # for cust_main
52 #   custnum - unique
53 #   last - (name)
54 #   first - (name)
55 #   company
56 #   address1
57 #   address2
58 #   city
59 #   state
60 #   zip
61 #   country
62 #   daytime - (phone)
63 #   night - (phone)
64 #   fax
65 #   payby - CARD, BILL or COMP
66 #   payinfo - Credit card #, P.O. # or COMP authorization
67 #   paydate - Expiration
68 #   tax - 'Y' for tax exempt
69 # for cust_pkg
70 #   LegacyBillingData - maps via %pkgpart above to a pkgpart
71 # for svc_acct
72 #   username
73
74 my($header);
75 $header=<CLIENT>;
76 chop $header;
77 my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
78 #print join("\n",@fields);
79
80 my($error);
81 my($link,$line)=(0,0);
82 while (<CLIENT>) {
83   chop; 
84   next if /^[\s\t]*$/; #skip any blank lines
85
86   #define %svc hash for this record
87   my(@record)=split(/\t/);
88   my(%svc);
89   foreach (@fields) {
90     $svc{$_}=shift @record;   
91   }
92
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;
100
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'},
121     'refnum'   => $refnum,
122   } );
123
124   $error=$cust_main->insert;
125
126   if ( $error ) {
127     warn $cust_main->_dump;
128     warn map "$_: ". $svc{$_}. "|\n", keys %svc;
129     die $error;
130   }
131
132   my($cust_pkg)=create FS::cust_pkg ( {
133     'custnum' => $svc{'custnum'},
134     'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
135     'setup'   => '', 
136     'bill'    => '',
137     'susp'    => '',
138     'expire'  => '',
139     'cancel'  => '',
140   } );
141
142   $error=$cust_pkg->insert;
143   if ( $error ) {
144     warn $svc{'LegacyBillingData'};
145     die $error;
146   }
147
148   unless ( $svc{'username'} ) {
149     warn "Empty login";
150   } else {
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";
155     } else {
156       #link to the cust_pkg record we created above
157
158       #find cust_svc record for this svc_acct record
159       my($o_cust_svc)=qsearchs('cust_svc',{
160         'svcnum' => $svc_acct->svcnum,
161         'pkgnum' => '',
162       } );
163       unless ( $o_cust_svc ) {
164         warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
165       } else {
166
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,
171           'quantity' => 1,
172         });
173         unless ( $pkg_svc ) {
174           warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
175                " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
176         } else {
177
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,
183           });
184           my($error) = $n_cust_svc->replace($o_cust_svc);
185           die $error if $error;
186           $link++;
187         }
188       }
189     }
190   }
191
192   $line++;
193
194 }
195
196 warn "\n$link of $line lines linked\n";
197
198 # ---
199
200 sub usage {
201   die "Usage:\n\n  cust_main.import user\n";
202 }