Initial revision
[freeside.git] / eg / TEMPLATE_cust_main.import
1 #!/usr/bin/perl -w
2
3 # Template for importing legacy customer data
4 #
5 # ivan@sisd.com 98-aug-17 - 20
6
7 use strict;
8 use FS::UID qw(adminsuidsetup datasrc);
9 use FS::Record qw(fields qsearch qsearchs);
10 use FS::cust_main;
11 use FS::cust_pkg;
12 use Date::Parse;
13
14 adminsuidsetup;
15
16 # use these for the imported cust_main records (unless you have these in legacy
17 # data)
18 my($agentnum)=4;
19 my($refnum)=5;
20
21 # map from legacy billing data to pkgpart, maps imported field
22 # LegacyBillingData to pkgpart.  your names and pkgparts will be different
23 my(%pkgpart)=(
24   'Employee'          => 10,
25   'Business'          => 11,
26   'Individual'        => 12,
27   'Basic PPP'         => 13,
28   'Slave'             => 14,
29   'Co-Located Server' => 15,
30   'Virtual Web'       => 16,
31   'Perk Mail'         => 17,
32   'Credit Hold'       => 18,
33 );
34
35 my($file)="legacy_file";
36
37 open(CLIENT,$file) 
38   or die "Can't open $file: $!"; 
39
40 # put a tab-separated header atop the file, or define @fields
41 #   (use these names or change them below)
42 #
43 # for cust_main
44 #   custnum - unique
45 #   last - (name)
46 #   first - (name)
47 #   company
48 #   address1
49 #   address2
50 #   city
51 #   state
52 #   zip
53 #   country
54 #   daytime - (phone)
55 #   night - (phone)
56 #   fax
57 #   payby - CARD, BILL or COMP
58 #   payinfo - Credit card #, P.O. # or COMP authorization
59 #   paydate - Expiration
60 #   tax - 'Y' for tax exempt
61 # for cust_pkg
62 #   LegacyBillingData - maps via %pkgpart above to a pkgpart
63 # for svc_acct
64 #   username
65
66 my($header);
67 $header=<CLIENT>;
68 chop $header;
69 my(@fields)=map { /^\s*(.*[^\s]+)\s*$/; $1 } split(/\t/,$header);
70 #print join("\n",@fields);
71
72 my($error);
73 my($link,$line)=(0,0);
74 while (<CLIENT>) {
75   chop; 
76   next if /^[\s\t]*$/; #skip any blank lines
77
78   #define %svc hash for this record
79   my(@record)=split(/\t/);
80   my(%svc);
81   foreach (@fields) {
82     $svc{$_}=shift @record;   
83   }
84
85   # might need to massage some data like this
86   $svc{'payby'} =~ s/^Credit Card$/CARD/io;
87   $svc{'payby'} =~ s/^Check$/BILL/io;
88   $svc{'payby'} =~ s/^Cash$/BILL/io;
89   $svc{'payby'} =~ s/^$/BILL/o;
90   $svc{'First'} =~ s/&/and/go; 
91   $svc{'Zip'} =~ s/\s+$//go;
92
93   my($cust_main) = create FS::cust_main ( {
94     'custnum'  => $svc{'custnum'},
95     'agentnum' => $agentnum,
96     'last'     => $svc{'last'},
97     'first'    => $svc{'first'},
98     'company'  => $svc{'company'},
99     'address1' => $svc{'address1'},
100     'address2' => $svc{'address2'},
101     'city'     => $svc{'city'},
102     'state'    => $svc{'state'},
103     'zip'      => $svc{'zip'},
104     'country'  => $svc{'country'},
105     'daytime'  => $svc{'daytime'},
106     'night'    => $svc{'night'},
107     'fax'      => $svc{'fax'},
108     'payby'    => $svc{'payby'},
109     'payinfo'  => $svc{'payinfo'},
110     'paydate'  => $svc{'paydate'},
111     'payname'  => $svc{'payname'},
112     'tax'      => $svc{'tax'},
113     'refnum'   => $refnum,
114   } );
115
116   $error=$cust_main->insert;
117
118   if ( $error ) {
119     warn $cust_main->_dump;
120     warn map "$_: ". $svc{$_}. "|\n", keys %svc;
121     die $error;
122   }
123
124   my($cust_pkg)=create FS::cust_pkg ( {
125     'custnum' => $svc{'custnum'},
126     'pkgpart' => $pkgpart{$svc{'LegacyBillingData'}},
127     'setup'   => '', 
128     'bill'    => '',
129     'susp'    => '',
130     'expire'  => '',
131     'cancel'  => '',
132   } );
133
134   $error=$cust_pkg->insert;
135   if ( $error ) {
136     warn $svc{'LegacyBillingData'};
137     die $error;
138   }
139
140   unless ( $svc{'username'} ) {
141     warn "Empty login";
142   } else {
143     #find svc_acct record (imported with bin/svc_acct.import) for this username
144     my($svc_acct)=qsearchs('svc_acct',{'username'=>$svc{'username'}});
145     unless ( $svc_acct ) {
146       warn "username ", $svc{'username'}, " not found\n";
147     } else {
148       #link to the cust_pkg record we created above
149
150       #find cust_svc record for this svc_acct record
151       my($o_cust_svc)=qsearchs('cust_svc',{
152         'svcnum' => $svc_acct->svcnum,
153         'pkgnum' => '',
154       } );
155       unless ( $o_cust_svc ) {
156         warn "No unlinked cust_svc for svcnum ", $svc_acct->svcnum;
157       } else {
158
159         #make sure this svcpart is in pkgpart
160         my($pkg_svc)=qsearchs('pkg_svc',{
161           'pkgpart'  => $pkgpart{$svc{'LegacyBillingData'}},
162           'svcpart'  => $o_cust_svc->svcpart,
163           'quantity' => 1,
164         });
165         unless ( $pkg_svc ) {
166           warn "login ", $svc{'username'}, ": No svcpart ", $o_cust_svc->svcpart,
167                " for pkgpart ", $pkgpart{$svc{'Acct. Type'}}, "\n" ;
168         } else {
169
170           #create new cust_svc record linked to cust_pkg record 
171           my($n_cust_svc) = create FS::cust_svc ({
172             'svcnum'  => $o_cust_svc->svcnum,
173             'pkgnum'  => $cust_pkg->pkgnum,
174             'svcpart' => $pkg_svc->svcpart,
175           });
176           my($error) = $n_cust_svc->replace($o_cust_svc);
177           die $error if $error;
178           $link++;
179         }
180       }
181     }
182   }
183
184   $line++;
185
186 }
187
188 warn "\n$link of $line lines linked\n";
189