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