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