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