add table cust_main_invoice
[freeside.git] / bin / fs-setup
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: fs-setup,v 1.10 1998-12-16 06:05:38 ivan Exp $
4 #
5 # ivan@sisd.com 97-nov-8,9
6 #
7 # agent_type and type_pkgs added.
8 # (index need to be declared, & primary keys shoudln't have mysql syntax)
9 # ivan@sisd.com 97-nov-13
10 #
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
12 #
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
14 #
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
17 #
18 # finished up ivan@sisd.com 98-jun-1
19 #
20 # part_svc fields are all forced NULL, not the opposite
21 # hmm: also are forced varchar($char_d) as fixed '0' for things like
22 # uid is Not Good.  will this break anything else?
23 # ivan@sisd.com 98-jun-29
24 #
25 # ss is 11 chars ivan@sisd.com 98-jul-20
26 #
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
28 #
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
31 #
32 # fix radius attributes ivan@sisd.com 98-sep-27
33 #
34 # $Log: fs-setup,v $
35 # Revision 1.10  1998-12-16 06:05:38  ivan
36 # add table cust_main_invoice
37 #
38 # Revision 1.9  1998/12/15 04:36:29  ivan
39 # s/croak/die/; #oops
40 #
41 # Revision 1.8  1998/12/15 04:33:27  ivan
42 # dies if it isn't running as the freeside user
43 #
44 # Revision 1.7  1998/11/18 09:01:31  ivan
45 # i18n! i18n!
46 #
47 # Revision 1.6  1998/11/15 13:18:02  ivan
48 # remove debugging
49 #
50 # Revision 1.5  1998/11/15 09:43:03  ivan
51 # update for new config file syntax, new adminsuidsetup
52 #
53 # Revision 1.4  1998/10/22 15:51:23  ivan
54 # also varchar with no length specified - postgresql fix broke mysql.
55 #
56 # Revision 1.3  1998/10/22 15:46:28  ivan
57 # now smallint is illegal, so remove that too.
58 #
59
60 #to delay loading dbdef until we're ready
61 BEGIN { $FS::Record::setup_hack = 1; }
62
63 use strict;
64 use DBI;
65 use FS::dbdef;
66 use FS::UID qw(adminsuidsetup datasrc checkeuid);
67 use FS::Record;
68 use FS::cust_main_county;
69
70 die "Not running uid freeside!" unless checkeuid();
71
72 my $user = shift or die &usage;
73 FS::UID::getsecrets $user;
74
75 #needs to match FS::Record
76 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
77
78 ###
79
80 print "\nEnter the maximum username length: ";
81 my($username_len)=&getvalue;
82
83 print "\n\n", <<END, ":";
84 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
85 for each user.  Enter any additional RADIUS attributes you need to track for
86 each user, separated by whitespace.
87 END
88 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
89
90 sub getvalue {
91   my($x)=scalar(<STDIN>);
92   chop $x;
93   $x;
94 }
95
96 ###
97
98 my($char_d) = 80; #default maxlength for text fields
99
100 #my(@date_type)  = ( 'timestamp', '', ''     );
101 my(@date_type)  = ( 'int', 'NULL', ''     );
102 my(@perl_type) = ( 'varchar', 'NULL', 255  ); 
103 my(@money_type);
104 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
105   @money_type = ( 'money',   '', '' );
106 } else {
107   @money_type = ( 'decimal',   '', '10,2' );
108 }
109
110 ###
111 # create a dbdef object from the old data structure
112 ###
113
114 my(%tables)=&tables_hash_hack;
115
116 #turn it into objects
117 my($dbdef) = new FS::dbdef ( map {  
118   my(@columns);
119   while (@{$tables{$_}{'columns'}}) {
120     my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
121     push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
122   }
123   FS::dbdef_table->new(
124     $_,
125     $tables{$_}{'primary_key'},
126     #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
127     #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
128     FS::dbdef_unique->new($tables{$_}{'unique'}),
129     FS::dbdef_index->new($tables{$_}{'index'}),
130     @columns,
131   );
132 } (keys %tables) );
133
134 #add radius attributes to svc_acct
135
136 my($svc_acct)=$dbdef->table('svc_acct');
137
138 my($attribute);
139 foreach $attribute (@attributes) {
140   $svc_acct->addcolumn ( new FS::dbdef_column (
141     'radius_'. $attribute,
142     'varchar',
143     'NULL',
144     $char_d,
145   ));
146 }
147
148 #make part_svc table (but now as object)
149
150 my($part_svc)=$dbdef->table('part_svc');
151
152 #because of svc_acct_pop
153 #foreach (grep /^svc_/, $dbdef->tables) { 
154 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
155 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
156   my($table)=$dbdef->table($_);
157   my($col);
158   foreach $col ( $table->columns ) {
159     next if $col =~ /^svcnum$/;
160     $part_svc->addcolumn( new FS::dbdef_column (
161       $table->name. '__' . $table->column($col)->name,
162       'varchar', #$table->column($col)->type, 
163       'NULL',
164       $char_d, #$table->column($col)->length,
165     ));
166     $part_svc->addcolumn ( new FS::dbdef_column (
167       $table->name. '__'. $table->column($col)->name . "_flag",
168       'char',
169       'NULL',
170       1,
171     ));
172   }
173 }
174
175 #important
176 $dbdef->save($dbdef_file);
177 FS::Record::reload_dbdef($dbdef_file);
178
179 ###
180 # create 'em
181 ###
182
183 my($dbh)=adminsuidsetup $user;
184
185 #create tables
186 $|=1;
187
188 my($table);
189 foreach  ($dbdef->tables) {
190   my($table)=$dbdef->table($_);
191   print "Creating $_...";
192
193   my($statement);
194
195   #create table
196   foreach $statement ($table->sql_create_table(datasrc)) {
197     #print $statement, "\n"; 
198     $dbh->do( $statement )
199       or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
200   }
201
202   print "\n";
203 }
204
205 #not really sample data (and shouldn't default to US)
206
207 #cust_main_county
208
209 #USPS state codes
210 foreach ( qw(
211 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
212 ME MH MD MA MI MN MS MO MT NC ND NE NH NJ NM NV NY MP OH OK OR PA PW PR RI 
213 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
214 ) ) {
215   my($cust_main_county)=create FS::cust_main_county({
216     'state' => $_,
217     'tax'   => 0,
218     'country' => 'US',
219   });  
220   my($error);
221   $error=$cust_main_county->insert;
222   die $error if $error;
223 }
224
225 #ISO 2-letter country codes (same as country TLDs) except US
226 foreach ( qw(
227 AF AL DZ AS AD AO AI AQ AG AR AM AW AU AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
228 BA BW BV BR IO BN BG BF BI KH CM CA CV KY CF TD CL CN CX CC CO KM CG CK CR CI
229 HR CU CY CZ DK DJ DM DO TP EC EG SV GQ ER EE ET FK FO FJ FI FR FX GF PF TF GA
230 GM GE DE GH GI GR GL GD GP GU GT GN GW GY HT HM HN HK HU IS IN ID IR IQ IE IL
231 IT JM JP JO KZ KE KI KP KR KW KG LA LV LB LS LR LY LI LT LU MO MK MG MW MY MV
232 ML MT MH MQ MR MU YT MX FM MD MC MN MS MA MZ MM NA NR NP NL AN NC NZ NI NE NG
233 NU NF MP NO OM PK PW PA PG PY PE PH PN PL PT PR QA RE RO RU RW KN LC VC WS SM
234 ST SA SN SC SL SG SK SI SB SO ZA GS ES LK SH PM SD SR SJ SZ SE CH SY TW TJ TZ
235 TH TG TK TO TT TN TR TM TC TV UG UA AE GB UM UY UZ VU VA VE VN VG VI WF EH
236 YE YU ZR ZM ZW
237 ) ) {
238   my($cust_main_county)=create FS::cust_main_county({
239     'tax'   => 0,
240     'country' => $_,
241   });  
242   my($error);
243   $error=$cust_main_county->insert;
244   die $error if $error;
245 }
246
247 $dbh->disconnect or die $dbh->errstr;
248
249 sub usage {
250   die "Usage:\n  fs-setup user\n"; 
251 }
252
253 ###
254 # Now it becomes an object.  much better.
255 ###
256 sub tables_hash_hack {
257
258   #note that s/(date|change)/_$1/; to avoid keyword conflict.
259   #put a kludge in FS::Record to catch this or? (pry need some date-handling
260   #stuff anyway also)
261
262   my(%tables)=( #yech.}
263
264     'agent' => {
265       'columns' => [
266         'agentnum', 'int',            '',     '',
267         'agent',    'varchar',           '',     $char_d,
268         'typenum',  'int',            '',     '',
269         'freq',     'int',       'NULL', '',
270         'prog',     @perl_type,
271       ],
272       'primary_key' => 'agentnum',
273       'unique' => [ [] ],
274       'index' => [ ['typenum'] ],
275     },
276
277     'agent_type' => {
278       'columns' => [
279         'typenum',   'int',  '', '',
280         'atype',     'varchar', '', $char_d,
281       ],
282       'primary_key' => 'typenum',
283       'unique' => [ [] ],
284       'index' => [ [] ],
285     },
286
287     'type_pkgs' => {
288       'columns' => [
289         'typenum',   'int',  '', '',
290         'pkgpart',   'int',  '', '',
291       ],
292       'primary_key' => '',
293       'unique' => [ ['typenum', 'pkgpart'] ],
294       'index' => [ ['typenum'] ],
295     },
296
297     'cust_bill' => {
298       'columns' => [
299         'invnum',    'int',  '', '',
300         'custnum',   'int',  '', '',
301         '_date',     @date_type,
302         'charged',   @money_type,
303         'owed',      @money_type,
304         'printed',   'int',  '', '',
305       ],
306       'primary_key' => 'invnum',
307       'unique' => [ [] ],
308       'index' => [ ['custnum'] ],
309     },
310
311     'cust_bill_pkg' => {
312       'columns' => [
313         'pkgnum',  'int', '', '',
314         'invnum',  'int', '', '',
315         'setup',   @money_type,
316         'recur',   @money_type,
317         'sdate',   @date_type,
318         'edate',   @date_type,
319       ],
320       'primary_key' => '',
321       'unique' => [ ['pkgnum', 'invnum'] ],
322       'index' => [ ['invnum'] ],
323     },
324
325     'cust_credit' => {
326       'columns' => [
327         'crednum',  'int', '', '',
328         'custnum',  'int', '', '',
329         '_date',    @date_type,
330         'amount',   @money_type,
331         'credited', @money_type,
332         'otaker',   'varchar', '', 8,
333         'reason',   'varchar', '', 255,
334       ],
335       'primary_key' => 'crednum',
336       'unique' => [ [] ],
337       'index' => [ ['custnum'] ],
338     },
339
340     'cust_main' => {
341       'columns' => [
342         'custnum',  'int',  '',     '',
343         'agentnum', 'int',  '',     '',
344         'titlenum', 'int',  'NULL',   '',
345         'last',     'varchar', '',     $char_d,
346         'middle',   'varchar', 'NULL', $char_d,
347         'first',    'varchar', '',     $char_d,
348         'ss',       'char', 'NULL', 11,
349         'company',  'varchar', 'NULL', $char_d,
350         'address1', 'varchar', '',     $char_d,
351         'address2', 'varchar', 'NULL', $char_d,
352         'city',     'varchar', '',     $char_d,
353         'county',   'varchar', 'NULL', $char_d,
354         'state',    'varchar', '',     $char_d,
355         'zip',      'varchar', '',     10,
356         'country',  'char', '',     2,
357         'daytime',  'varchar', 'NULL', 20,
358         'night',    'varchar', 'NULL', 20,
359         'fax',      'varchar', 'NULL', 12,
360         'payby',    'char', '',     4,
361         'payinfo',  'varchar', 'NULL', 16,
362         'paydate',  @date_type,
363         'payname',  'varchar', 'NULL', $char_d,
364         'tax',      'char', 'NULL', 1,
365         'otaker',   'varchar', '',     8,
366         'refnum',   'int',  '',     '',
367       ],
368       'primary_key' => 'custnum',
369       'unique' => [ [] ],
370       #'index' => [ ['last'], ['company'] ],
371       'index' => [ ['last'], ],
372     },
373
374     'cust_main_invoice' => {
375       'columns' => [
376         'destnum',  'int',  '',     '',
377         'custnum',  'int',  '',     '',
378         'dest',     'varchar', '',  $char_d,
379       ],
380       'primary_key' => 'destnum',
381       'unique' => [ [] ],
382       'index' => [ ['custnum'], ],
383     },
384
385     'cust_main_county' => { #county+state+country are checked off the
386                             #cust_main_county for validation and to provide
387                             # a tax rate.
388       'columns' => [
389         'taxnum',   'int',   '',    '',
390         'state',    'char',  'NULL',    $char_d,
391         'county',   'varchar',  'NULL',    $char_d,
392         'country',  'char',  '', 2, 
393         'tax',      'real',  '',    '', #tax %
394       ],
395       'primary_key' => 'taxnum',
396       'unique' => [ [] ],
397   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
398       'index' => [ [] ],
399     },
400
401     'cust_pay' => {
402       'columns' => [
403         'paynum',   'int',    '',   '',
404         'invnum',   'int',    '',   '',
405         'paid',     @money_type,
406         '_date',    @date_type,
407         'payby',    'char',   '',     4, # CARD/BILL/COMP, should be index into
408                                          # payment type table.
409         'payinfo',  'varchar',   'NULL', 16,  #see cust_main above
410         'paybatch', 'varchar',   'NULL', $char_d, #for auditing purposes.
411       ],
412       'primary_key' => 'paynum',
413       'unique' => [ [] ],
414       'index' => [ ['invnum'] ],
415     },
416
417     'cust_pay_batch' => { #what's this used for again?  list of customers
418                           #in current CARD batch? (necessarily CARD?)
419       'columns' => [
420         'invnum',   'int',    '',   '',
421         'custnum',   'int',    '',   '',
422         'last',     'varchar', '',     $char_d,
423         'first',    'varchar', '',     $char_d,
424         'address1', 'varchar', '',     $char_d,
425         'address2', 'varchar', 'NULL', $char_d,
426         'city',     'varchar', '',     $char_d,
427         'state',    'char', '',     2,
428         'zip',      'varchar', '',     10,
429         'country',  'char', '',     2,
430         'trancode', 'int', '', '',
431         'cardnum',  'varchar', '',     16,
432         'exp',      @date_type,
433         'payname',  'varchar', 'NULL', $char_d,
434         'amount',   @money_type,
435       ],
436       'primary_key' => '',
437       'unique' => [ [] ],
438       'index' => [ ['invnum'], ['custnum'] ],
439     },
440
441     'cust_pkg' => {
442       'columns' => [
443         'pkgnum',    'int',    '',   '',
444         'custnum',   'int',    '',   '',
445         'pkgpart',   'int',    '',   '',
446         'otaker',    'varchar', '', 8,
447         'setup',     @date_type,
448         'bill',      @date_type,
449         'susp',      @date_type,
450         'cancel',    @date_type,
451         'expire',    @date_type,
452       ],
453       'primary_key' => 'pkgnum',
454       'unique' => [ [] ],
455       'index' => [ ['custnum'] ],
456     },
457
458     'cust_refund' => {
459       'columns' => [
460         'refundnum',    'int',    '',   '',
461         'crednum',      'int',    '',   '',
462         '_date',        @date_type,
463         'refund',       @money_type,
464         'otaker',       'varchar',   '',   8,
465         'reason',       'varchar',   '',   $char_d,
466         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
467                                              # into payment type table.
468         'payinfo',      'varchar',   'NULL', 16,  #see cust_main above
469       ],
470       'primary_key' => 'refundnum',
471       'unique' => [ [] ],
472       'index' => [ ['crednum'] ],
473     },
474
475     'cust_svc' => {
476       'columns' => [
477         'svcnum',    'int',    '',   '',
478         'pkgnum',    'int',    '',   '',
479         'svcpart',   'int',    '',   '',
480       ],
481       'primary_key' => 'svcnum',
482       'unique' => [ [] ],
483       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
484     },
485
486     'part_pkg' => {
487       'columns' => [
488         'pkgpart',    'int',    '',   '',
489         'pkg',        'varchar',   '',   $char_d,
490         'comment',    'varchar',   '',   $char_d,
491         'setup',      @perl_type,
492         'freq',       'int', '', '',  #billing frequency (months)
493         'recur',      @perl_type,
494       ],
495       'primary_key' => 'pkgpart',
496       'unique' => [ [] ],
497       'index' => [ [] ],
498     },
499
500     'part_title' => {
501       'columns' => [
502         'titlenum',   'int',    '',   '',
503         'title',      'varchar',   '',   $char_d,
504       ],
505       'primary_key' => 'titlenum',
506       'unique' => [ [] ],
507       'index' => [ [] ],
508     },
509
510     'pkg_svc' => {
511       'columns' => [
512         'pkgpart',    'int',    '',   '',
513         'svcpart',    'int',    '',   '',
514         'quantity',   'int',    '',   '',
515       ],
516       'primary_key' => '',
517       'unique' => [ ['pkgpart', 'svcpart'] ],
518       'index' => [ ['pkgpart'] ],
519     },
520
521     'part_referral' => {
522       'columns' => [
523         'refnum',   'int',    '',   '',
524         'referral', 'varchar',   '',   $char_d,
525       ],
526       'primary_key' => 'refnum',
527       'unique' => [ [] ],
528       'index' => [ [] ],
529     },
530
531     'part_svc' => {
532       'columns' => [
533         'svcpart',    'int',    '',   '',
534         'svc',        'varchar',   '',   $char_d,
535         'svcdb',      'varchar',   '',   $char_d,
536       ],
537       'primary_key' => 'svcpart',
538       'unique' => [ [] ],
539       'index' => [ [] ],
540     },
541
542     #(this should be renamed to part_pop)
543     'svc_acct_pop' => {
544       'columns' => [
545         'popnum',    'int',    '',   '',
546         'city',      'varchar',   '',   $char_d,
547         'state',     'char',   '',   2,
548         'ac',        'char',   '',   3,
549         'exch',      'char',   '',   3,
550         #rest o' number?
551       ],
552       'primary_key' => 'popnum',
553       'unique' => [ [] ],
554       'index' => [ [] ],
555     },
556
557     'svc_acct' => {
558       'columns' => [
559         'svcnum',    'int',    '',   '',
560         'username',  'varchar',   '',   $username_len, #unique (& remove dup code)
561         '_password', 'varchar',   '',   25, #13 for encryped pw's plus ' *SUSPENDED*
562         'popnum',    'int',    'NULL',   '',
563         'uid',       'int', 'NULL',   '',
564         'gid',       'int', 'NULL',   '',
565         'finger',    'varchar',   'NULL',   $char_d,
566         'dir',       'varchar',   'NULL',   $char_d,
567         'shell',     'varchar',   'NULL',   $char_d,
568         'quota',     'varchar',   'NULL',   $char_d,
569         'slipip',    'varchar',   'NULL',   15, #four TINYINTs, bah.
570       ],
571       'primary_key' => 'svcnum',
572       'unique' => [ [] ],
573       'index' => [ ['username'] ],
574     },
575
576     'svc_acct_sm' => {
577       'columns' => [
578         'svcnum',    'int',    '',   '',
579         'domsvc',    'int',    '',   '',
580         'domuid',    'int', '',   '',
581         'domuser',   'varchar',   '',   $char_d,
582       ],
583       'primary_key' => 'svcnum',
584       'unique' => [ [] ],
585       'index' => [ ['domsvc'], ['domuid'] ], 
586     },
587
588     #'svc_charge' => {
589     #  'columns' => [
590     #    'svcnum',    'int',    '',   '',
591     #    'amount',    @money_type,
592     #  ],
593     #  'primary_key' => 'svcnum',
594     #  'unique' => [ [] ],
595     #  'index' => [ [] ],
596     #},
597
598     'svc_domain' => {
599       'columns' => [
600         'svcnum',    'int',    '',   '',
601         'domain',    'varchar',    '',   $char_d,
602       ],
603       'primary_key' => 'svcnum',
604       'unique' => [ ['domain'] ],
605       'index' => [ [] ],
606     },
607
608     #'svc_wo' => {
609     #  'columns' => [
610     #    'svcnum',    'int',    '',   '',
611     #    'svcnum',    'int',    '',   '',
612     #    'svcnum',    'int',    '',   '',
613     #    'worker',    'varchar',   '',   $char_d,
614     #    '_date',     @date_type,
615     #  ],
616     #  'primary_key' => 'svcnum',
617     #  'unique' => [ [] ],
618     #  'index' => [ [] ],
619     #},
620
621   );
622
623   %tables;
624
625 }
626