constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
[freeside.git] / bin / fs-setup
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: fs-setup,v 1.16 1999-02-28 19:44:16 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.16  1999-02-28 19:44:16  ivan
36 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
37 #
38 # Revision 1.15  1999/02/27 21:06:21  ivan
39 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
40 # by Ben Leibig <leibig@colorado.edu>
41 #
42 # Revision 1.14  1999/02/07 09:59:14  ivan
43 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
44 #
45 # Revision 1.13  1999/02/04 06:09:23  ivan
46 # add AU provences
47 #
48 # Revision 1.12  1999/02/03 10:42:27  ivan
49 # *** empty log message ***
50 #
51 # Revision 1.11  1999/01/17 03:11:52  ivan
52 # remove preliminary completehost changes
53 #
54 # Revision 1.10  1998/12/16 06:05:38  ivan
55 # add table cust_main_invoice
56 #
57 # Revision 1.9  1998/12/15 04:36:29  ivan
58 # s/croak/die/; #oops
59 #
60 # Revision 1.8  1998/12/15 04:33:27  ivan
61 # dies if it isn't running as the freeside user
62 #
63 # Revision 1.7  1998/11/18 09:01:31  ivan
64 # i18n! i18n!
65 #
66 # Revision 1.6  1998/11/15 13:18:02  ivan
67 # remove debugging
68 #
69 # Revision 1.5  1998/11/15 09:43:03  ivan
70 # update for new config file syntax, new adminsuidsetup
71 #
72 # Revision 1.4  1998/10/22 15:51:23  ivan
73 # also varchar with no length specified - postgresql fix broke mysql.
74 #
75 # Revision 1.3  1998/10/22 15:46:28  ivan
76 # now smallint is illegal, so remove that too.
77 #
78
79 #to delay loading dbdef until we're ready
80 BEGIN { $FS::Record::setup_hack = 1; }
81
82 use strict;
83 use DBI;
84 use FS::dbdef;
85 use FS::UID qw(adminsuidsetup datasrc checkeuid);
86 use FS::Record;
87 use FS::cust_main_county;
88
89 die "Not running uid freeside!" unless checkeuid();
90
91 my $user = shift or die &usage;
92 FS::UID::getsecrets $user;
93
94 #needs to match FS::Record
95 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
96
97 ###
98
99 print "\nEnter the maximum username length: ";
100 my($username_len)=&getvalue;
101
102 print "\n\n", <<END, ":";
103 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
104 for each user.  Enter any additional RADIUS attributes you need to track for
105 each user, separated by whitespace.
106 END
107 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
108
109 sub getvalue {
110   my($x)=scalar(<STDIN>);
111   chop $x;
112   $x;
113 }
114
115 ###
116
117 my($char_d) = 80; #default maxlength for text fields
118
119 #my(@date_type)  = ( 'timestamp', '', ''     );
120 my(@date_type)  = ( 'int', 'NULL', ''     );
121 my(@perl_type) = ( 'varchar', 'NULL', 255  ); 
122 my(@money_type);
123 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
124   @money_type = ( 'money',   '', '' );
125 } else {
126   @money_type = ( 'decimal',   '', '10,2' );
127 }
128
129 ###
130 # create a dbdef object from the old data structure
131 ###
132
133 my(%tables)=&tables_hash_hack;
134
135 #turn it into objects
136 my($dbdef) = new FS::dbdef ( map {  
137   my(@columns);
138   while (@{$tables{$_}{'columns'}}) {
139     my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
140     push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
141   }
142   FS::dbdef_table->new(
143     $_,
144     $tables{$_}{'primary_key'},
145     #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
146     #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
147     FS::dbdef_unique->new($tables{$_}{'unique'}),
148     FS::dbdef_index->new($tables{$_}{'index'}),
149     @columns,
150   );
151 } (keys %tables) );
152
153 #add radius attributes to svc_acct
154
155 my($svc_acct)=$dbdef->table('svc_acct');
156
157 my($attribute);
158 foreach $attribute (@attributes) {
159   $svc_acct->addcolumn ( new FS::dbdef_column (
160     'radius_'. $attribute,
161     'varchar',
162     'NULL',
163     $char_d,
164   ));
165 }
166
167 #make part_svc table (but now as object)
168
169 my($part_svc)=$dbdef->table('part_svc');
170
171 #because of svc_acct_pop
172 #foreach (grep /^svc_/, $dbdef->tables) { 
173 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
174 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
175   my($table)=$dbdef->table($_);
176   my($col);
177   foreach $col ( $table->columns ) {
178     next if $col =~ /^svcnum$/;
179     $part_svc->addcolumn( new FS::dbdef_column (
180       $table->name. '__' . $table->column($col)->name,
181       'varchar', #$table->column($col)->type, 
182       'NULL',
183       $char_d, #$table->column($col)->length,
184     ));
185     $part_svc->addcolumn ( new FS::dbdef_column (
186       $table->name. '__'. $table->column($col)->name . "_flag",
187       'char',
188       'NULL',
189       1,
190     ));
191   }
192 }
193
194 #important
195 $dbdef->save($dbdef_file);
196 &FS::Record::reload_dbdef($dbdef_file);
197
198 ###
199 # create 'em
200 ###
201
202 my($dbh)=adminsuidsetup $user;
203
204 #create tables
205 $|=1;
206
207 my($table);
208 foreach  ($dbdef->tables) {
209   my($table)=$dbdef->table($_);
210   print "Creating $_...";
211
212   my($statement);
213
214   #create table
215   foreach $statement ($table->sql_create_table(datasrc)) {
216     #print $statement, "\n"; 
217     $dbh->do( $statement )
218       or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
219   }
220
221   print "\n";
222 }
223
224 #not really sample data (and shouldn't default to US)
225
226 #cust_main_county
227
228 #USPS state codes
229 foreach ( qw(
230 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
231 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 
232 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
233 ) ) {
234   my($cust_main_county)=new FS::cust_main_county({
235     'state' => $_,
236     'tax'   => 0,
237     'country' => 'US',
238   });  
239   my($error);
240   $error=$cust_main_county->insert;
241   die $error if $error;
242 }
243
244 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
245 foreach ( qw(
246 VIC NSW NT QLD TAS ACT WA SA
247 ) ) {
248   my($cust_main_county)=new FS::cust_main_county({
249     'state' => $_,
250     'tax'   => 0,
251     'country' => 'AU',
252   });  
253   my($error);
254   $error=$cust_main_county->insert;
255   die $error if $error;
256 }
257
258 #ISO 2-letter country codes (same as country TLDs) except US and AU
259 foreach ( qw(
260 AF AL DZ AS AD AO AI AQ AG AR AM AW AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269 YE YU ZR ZM ZW
270 ) ) {
271   my($cust_main_county)=new FS::cust_main_county({
272     'tax'   => 0,
273     'country' => $_,
274   });  
275   my($error);
276   $error=$cust_main_county->insert;
277   die $error if $error;
278 }
279
280 $dbh->disconnect or die $dbh->errstr;
281
282 sub usage {
283   die "Usage:\n  fs-setup user\n"; 
284 }
285
286 ###
287 # Now it becomes an object.  much better.
288 ###
289 sub tables_hash_hack {
290
291   #note that s/(date|change)/_$1/; to avoid keyword conflict.
292   #put a kludge in FS::Record to catch this or? (pry need some date-handling
293   #stuff anyway also)
294
295   my(%tables)=( #yech.}
296
297     'agent' => {
298       'columns' => [
299         'agentnum', 'int',            '',     '',
300         'agent',    'varchar',           '',     $char_d,
301         'typenum',  'int',            '',     '',
302         'freq',     'int',       'NULL', '',
303         'prog',     @perl_type,
304       ],
305       'primary_key' => 'agentnum',
306       'unique' => [ [] ],
307       'index' => [ ['typenum'] ],
308     },
309
310     'agent_type' => {
311       'columns' => [
312         'typenum',   'int',  '', '',
313         'atype',     'varchar', '', $char_d,
314       ],
315       'primary_key' => 'typenum',
316       'unique' => [ [] ],
317       'index' => [ [] ],
318     },
319
320     'type_pkgs' => {
321       'columns' => [
322         'typenum',   'int',  '', '',
323         'pkgpart',   'int',  '', '',
324       ],
325       'primary_key' => '',
326       'unique' => [ ['typenum', 'pkgpart'] ],
327       'index' => [ ['typenum'] ],
328     },
329
330     'cust_bill' => {
331       'columns' => [
332         'invnum',    'int',  '', '',
333         'custnum',   'int',  '', '',
334         '_date',     @date_type,
335         'charged',   @money_type,
336         'owed',      @money_type,
337         'printed',   'int',  '', '',
338       ],
339       'primary_key' => 'invnum',
340       'unique' => [ [] ],
341       'index' => [ ['custnum'] ],
342     },
343
344     'cust_bill_pkg' => {
345       'columns' => [
346         'pkgnum',  'int', '', '',
347         'invnum',  'int', '', '',
348         'setup',   @money_type,
349         'recur',   @money_type,
350         'sdate',   @date_type,
351         'edate',   @date_type,
352       ],
353       'primary_key' => '',
354       'unique' => [ ['pkgnum', 'invnum'] ],
355       'index' => [ ['invnum'] ],
356     },
357
358     'cust_credit' => {
359       'columns' => [
360         'crednum',  'int', '', '',
361         'custnum',  'int', '', '',
362         '_date',    @date_type,
363         'amount',   @money_type,
364         'credited', @money_type,
365         'otaker',   'varchar', '', 8,
366         'reason',   'varchar', '', 255,
367       ],
368       'primary_key' => 'crednum',
369       'unique' => [ [] ],
370       'index' => [ ['custnum'] ],
371     },
372
373     'cust_main' => {
374       'columns' => [
375         'custnum',  'int',  '',     '',
376         'agentnum', 'int',  '',     '',
377 #        'titlenum', 'int',  'NULL',   '',
378         'last',     'varchar', '',     $char_d,
379 #        'middle',   'varchar', 'NULL', $char_d,
380         'first',    'varchar', '',     $char_d,
381         'ss',       'char', 'NULL', 11,
382         'company',  'varchar', 'NULL', $char_d,
383         'address1', 'varchar', '',     $char_d,
384         'address2', 'varchar', 'NULL', $char_d,
385         'city',     'varchar', '',     $char_d,
386         'county',   'varchar', 'NULL', $char_d,
387         'state',    'varchar', 'NULL', $char_d,
388         'zip',      'varchar', '',     10,
389         'country',  'char', '',     2,
390         'daytime',  'varchar', 'NULL', 20,
391         'night',    'varchar', 'NULL', 20,
392         'fax',      'varchar', 'NULL', 12,
393         'payby',    'char', '',     4,
394         'payinfo',  'varchar', 'NULL', 16,
395         #'paydate',  @date_type,
396         'paydate',  'varchar', 'NULL', 10,
397         'payname',  'varchar', 'NULL', $char_d,
398         'tax',      'char', 'NULL', 1,
399         'otaker',   'varchar', '',     8,
400         'refnum',   'int',  '',     '',
401       ],
402       'primary_key' => 'custnum',
403       'unique' => [ [] ],
404       #'index' => [ ['last'], ['company'] ],
405       'index' => [ ['last'], ],
406     },
407
408     'cust_main_invoice' => {
409       'columns' => [
410         'destnum',  'int',  '',     '',
411         'custnum',  'int',  '',     '',
412         'dest',     'varchar', '',  $char_d,
413       ],
414       'primary_key' => 'destnum',
415       'unique' => [ [] ],
416       'index' => [ ['custnum'], ],
417     },
418
419     'cust_main_county' => { #county+state+country are checked off the
420                             #cust_main_county for validation and to provide
421                             # a tax rate.
422       'columns' => [
423         'taxnum',   'int',   '',    '',
424         'state',    'varchar',  'NULL',    $char_d,
425         'county',   'varchar',  'NULL',    $char_d,
426         'country',  'char',  '', 2, 
427         'tax',      'real',  '',    '', #tax %
428       ],
429       'primary_key' => 'taxnum',
430       'unique' => [ [] ],
431   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
432       'index' => [ [] ],
433     },
434
435     'cust_pay' => {
436       'columns' => [
437         'paynum',   'int',    '',   '',
438         'invnum',   'int',    '',   '',
439         'paid',     @money_type,
440         '_date',    @date_type,
441         'payby',    'char',   '',     4, # CARD/BILL/COMP, should be index into
442                                          # payment type table.
443         'payinfo',  'varchar',   'NULL', 16,  #see cust_main above
444         'paybatch', 'varchar',   'NULL', $char_d, #for auditing purposes.
445       ],
446       'primary_key' => 'paynum',
447       'unique' => [ [] ],
448       'index' => [ ['invnum'] ],
449     },
450
451     'cust_pay_batch' => { #what's this used for again?  list of customers
452                           #in current CARD batch? (necessarily CARD?)
453       'columns' => [
454         'invnum',   'int',    '',   '',
455         'custnum',   'int',    '',   '',
456         'last',     'varchar', '',     $char_d,
457         'first',    'varchar', '',     $char_d,
458         'address1', 'varchar', '',     $char_d,
459         'address2', 'varchar', 'NULL', $char_d,
460         'city',     'varchar', '',     $char_d,
461         'state',    'varchar', '',     $char_d,
462         'zip',      'varchar', '',     10,
463         'country',  'char', '',     2,
464         'trancode', 'int', '', '',
465         'cardnum',  'varchar', '',     16,
466         'exp',      @date_type,
467         'payname',  'varchar', 'NULL', $char_d,
468         'amount',   @money_type,
469       ],
470       'primary_key' => '',
471       'unique' => [ [] ],
472       'index' => [ ['invnum'], ['custnum'] ],
473     },
474
475     'cust_pkg' => {
476       'columns' => [
477         'pkgnum',    'int',    '',   '',
478         'custnum',   'int',    '',   '',
479         'pkgpart',   'int',    '',   '',
480         'otaker',    'varchar', '', 8,
481         'setup',     @date_type,
482         'bill',      @date_type,
483         'susp',      @date_type,
484         'cancel',    @date_type,
485         'expire',    @date_type,
486       ],
487       'primary_key' => 'pkgnum',
488       'unique' => [ [] ],
489       'index' => [ ['custnum'] ],
490     },
491
492     'cust_refund' => {
493       'columns' => [
494         'refundnum',    'int',    '',   '',
495         'crednum',      'int',    '',   '',
496         '_date',        @date_type,
497         'refund',       @money_type,
498         'otaker',       'varchar',   '',   8,
499         'reason',       'varchar',   '',   $char_d,
500         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
501                                              # into payment type table.
502         'payinfo',      'varchar',   'NULL', 16,  #see cust_main above
503       ],
504       'primary_key' => 'refundnum',
505       'unique' => [ [] ],
506       'index' => [ ['crednum'] ],
507     },
508
509     'cust_svc' => {
510       'columns' => [
511         'svcnum',    'int',    '',   '',
512         'pkgnum',    'int',    '',   '',
513         'svcpart',   'int',    '',   '',
514       ],
515       'primary_key' => 'svcnum',
516       'unique' => [ [] ],
517       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
518     },
519
520     'part_pkg' => {
521       'columns' => [
522         'pkgpart',    'int',    '',   '',
523         'pkg',        'varchar',   '',   $char_d,
524         'comment',    'varchar',   '',   $char_d,
525         'setup',      @perl_type,
526         'freq',       'int', '', '',  #billing frequency (months)
527         'recur',      @perl_type,
528       ],
529       'primary_key' => 'pkgpart',
530       'unique' => [ [] ],
531       'index' => [ [] ],
532     },
533
534 #    'part_title' => {
535 #      'columns' => [
536 #        'titlenum',   'int',    '',   '',
537 #        'title',      'varchar',   '',   $char_d,
538 #      ],
539 #      'primary_key' => 'titlenum',
540 #      'unique' => [ [] ],
541 #      'index' => [ [] ],
542 #    },
543
544     'pkg_svc' => {
545       'columns' => [
546         'pkgpart',    'int',    '',   '',
547         'svcpart',    'int',    '',   '',
548         'quantity',   'int',    '',   '',
549       ],
550       'primary_key' => '',
551       'unique' => [ ['pkgpart', 'svcpart'] ],
552       'index' => [ ['pkgpart'] ],
553     },
554
555     'part_referral' => {
556       'columns' => [
557         'refnum',   'int',    '',   '',
558         'referral', 'varchar',   '',   $char_d,
559       ],
560       'primary_key' => 'refnum',
561       'unique' => [ [] ],
562       'index' => [ [] ],
563     },
564
565     'part_svc' => {
566       'columns' => [
567         'svcpart',    'int',    '',   '',
568         'svc',        'varchar',   '',   $char_d,
569         'svcdb',      'varchar',   '',   $char_d,
570       ],
571       'primary_key' => 'svcpart',
572       'unique' => [ [] ],
573       'index' => [ [] ],
574     },
575
576     #(this should be renamed to part_pop)
577     'svc_acct_pop' => {
578       'columns' => [
579         'popnum',    'int',    '',   '',
580         'city',      'varchar',   '',   $char_d,
581         'state',     'varchar',   '',   $char_d,
582         'ac',        'char',   '',   3,
583         'exch',      'char',   '',   3,
584         #rest o' number?
585       ],
586       'primary_key' => 'popnum',
587       'unique' => [ [] ],
588       'index' => [ [] ],
589     },
590
591     'svc_acct' => {
592       'columns' => [
593         'svcnum',    'int',    '',   '',
594         'username',  'varchar',   '',   $username_len, #unique (& remove dup code)
595         '_password', 'varchar',   '',   25, #13 for encryped pw's plus ' *SUSPENDED*
596         'popnum',    'int',    'NULL',   '',
597         'uid',       'int', 'NULL',   '',
598         'gid',       'int', 'NULL',   '',
599         'finger',    'varchar',   'NULL',   $char_d,
600         'dir',       'varchar',   'NULL',   $char_d,
601         'shell',     'varchar',   'NULL',   $char_d,
602         'quota',     'varchar',   'NULL',   $char_d,
603         'slipip',    'varchar',   'NULL',   15, #four TINYINTs, bah.
604       ],
605       'primary_key' => 'svcnum',
606       'unique' => [ [] ],
607       'index' => [ ['username'] ],
608     },
609
610     'svc_acct_sm' => {
611       'columns' => [
612         'svcnum',    'int',    '',   '',
613         'domsvc',    'int',    '',   '',
614         'domuid',    'int', '',   '',
615         'domuser',   'varchar',   '',   $char_d,
616       ],
617       'primary_key' => 'svcnum',
618       'unique' => [ [] ],
619       'index' => [ ['domsvc'], ['domuid'] ], 
620     },
621
622     #'svc_charge' => {
623     #  'columns' => [
624     #    'svcnum',    'int',    '',   '',
625     #    'amount',    @money_type,
626     #  ],
627     #  'primary_key' => 'svcnum',
628     #  'unique' => [ [] ],
629     #  'index' => [ [] ],
630     #},
631
632     'svc_domain' => {
633       'columns' => [
634         'svcnum',    'int',    '',   '',
635         'domain',    'varchar',    '',   $char_d,
636       ],
637       'primary_key' => 'svcnum',
638       'unique' => [ ['domain'] ],
639       'index' => [ [] ],
640     },
641
642     #'svc_wo' => {
643     #  'columns' => [
644     #    'svcnum',    'int',    '',   '',
645     #    'svcnum',    'int',    '',   '',
646     #    'svcnum',    'int',    '',   '',
647     #    'worker',    'varchar',   '',   $char_d,
648     #    '_date',     @date_type,
649     #  ],
650     #  'primary_key' => 'svcnum',
651     #  'unique' => [ [] ],
652     #  'index' => [ [] ],
653     #},
654
655   );
656
657   %tables;
658
659 }
660