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