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