3 # $Id: fs-setup,v 1.22 2000-01-31 05:22:23 ivan Exp $
5 # ivan@sisd.com 97-nov-8,9
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
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
18 # finished up ivan@sisd.com 98-jun-1
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
25 # ss is 11 chars ivan@sisd.com 98-jul-20
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
32 # fix radius attributes ivan@sisd.com 98-sep-27
35 # Revision 1.22 2000-01-31 05:22:23 ivan
36 # prepaid "internet cards"
38 # Revision 1.21 2000/01/30 06:03:26 ivan
39 # postgres 6.5 finally supports decimal(10,2)
41 # Revision 1.20 2000/01/28 22:53:33 ivan
42 # track full phone number
44 # Revision 1.19 1999/07/29 08:50:35 ivan
45 # wrong type for cust_pay_batch.exp
47 # Revision 1.18 1999/04/15 22:46:30 ivan
50 # Revision 1.17 1999/04/14 07:58:39 ivan
51 # export getsecrets from FS::UID instead of calling it explicitly
53 # Revision 1.16 1999/02/28 19:44:16 ivan
54 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
56 # Revision 1.15 1999/02/27 21:06:21 ivan
57 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
58 # by Ben Leibig <leibig@colorado.edu>
60 # Revision 1.14 1999/02/07 09:59:14 ivan
61 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
63 # Revision 1.13 1999/02/04 06:09:23 ivan
66 # Revision 1.12 1999/02/03 10:42:27 ivan
67 # *** empty log message ***
69 # Revision 1.11 1999/01/17 03:11:52 ivan
70 # remove preliminary completehost changes
72 # Revision 1.10 1998/12/16 06:05:38 ivan
73 # add table cust_main_invoice
75 # Revision 1.9 1998/12/15 04:36:29 ivan
78 # Revision 1.8 1998/12/15 04:33:27 ivan
79 # dies if it isn't running as the freeside user
81 # Revision 1.7 1998/11/18 09:01:31 ivan
84 # Revision 1.6 1998/11/15 13:18:02 ivan
87 # Revision 1.5 1998/11/15 09:43:03 ivan
88 # update for new config file syntax, new adminsuidsetup
90 # Revision 1.4 1998/10/22 15:51:23 ivan
91 # also varchar with no length specified - postgresql fix broke mysql.
93 # Revision 1.3 1998/10/22 15:46:28 ivan
94 # now smallint is illegal, so remove that too.
97 #to delay loading dbdef until we're ready
98 BEGIN { $FS::Record::setup_hack = 1; }
103 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
105 use FS::cust_main_county;
107 die "Not running uid freeside!" unless checkeuid();
109 my $user = shift or die &usage;
112 #needs to match FS::Record
113 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
117 print "\nEnter the maximum username length: ";
118 my($username_len)=&getvalue;
120 print "\n\n", <<END, ":";
121 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
122 for each user. Enter any additional RADIUS attributes you need to track for
123 each user, separated by whitespace.
125 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
128 my($x)=scalar(<STDIN>);
135 my($char_d) = 80; #default maxlength for text fields
137 #my(@date_type) = ( 'timestamp', '', '' );
138 my(@date_type) = ( 'int', 'NULL', '' );
139 my(@perl_type) = ( 'varchar', 'NULL', 255 );
140 my @money_type = ( 'decimal', '', '10,2' );
143 # create a dbdef object from the old data structure
146 my(%tables)=&tables_hash_hack;
148 #turn it into objects
149 my($dbdef) = new FS::dbdef ( map {
151 while (@{$tables{$_}{'columns'}}) {
152 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
153 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
155 FS::dbdef_table->new(
157 $tables{$_}{'primary_key'},
158 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
159 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
160 FS::dbdef_unique->new($tables{$_}{'unique'}),
161 FS::dbdef_index->new($tables{$_}{'index'}),
166 #add radius attributes to svc_acct
168 my($svc_acct)=$dbdef->table('svc_acct');
171 foreach $attribute (@attributes) {
172 $svc_acct->addcolumn ( new FS::dbdef_column (
173 'radius_'. $attribute,
180 #make part_svc table (but now as object)
182 my($part_svc)=$dbdef->table('part_svc');
184 #because of svc_acct_pop
185 #foreach (grep /^svc_/, $dbdef->tables) {
186 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
187 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
188 my($table)=$dbdef->table($_);
190 foreach $col ( $table->columns ) {
191 next if $col =~ /^svcnum$/;
192 $part_svc->addcolumn( new FS::dbdef_column (
193 $table->name. '__' . $table->column($col)->name,
194 'varchar', #$table->column($col)->type,
196 $char_d, #$table->column($col)->length,
198 $part_svc->addcolumn ( new FS::dbdef_column (
199 $table->name. '__'. $table->column($col)->name . "_flag",
208 $dbdef->save($dbdef_file);
209 &FS::Record::reload_dbdef($dbdef_file);
215 my($dbh)=adminsuidsetup $user;
221 foreach ($dbdef->tables) {
222 my($table)=$dbdef->table($_);
223 print "Creating $_...";
228 foreach $statement ($table->sql_create_table(datasrc)) {
229 #print $statement, "\n";
230 $dbh->do( $statement )
231 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
237 #not really sample data (and shouldn't default to US)
243 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
244 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
245 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
247 my($cust_main_county)=new FS::cust_main_county({
253 $error=$cust_main_county->insert;
254 die $error if $error;
257 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
259 VIC NSW NT QLD TAS ACT WA SA
261 my($cust_main_county)=new FS::cust_main_county({
267 $error=$cust_main_county->insert;
268 die $error if $error;
271 #ISO 2-letter country codes (same as country TLDs) except US and AU
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 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
281 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
284 my($cust_main_county)=new FS::cust_main_county({
289 $error=$cust_main_county->insert;
290 die $error if $error;
293 $dbh->disconnect or die $dbh->errstr;
296 die "Usage:\n fs-setup user\n";
300 # Now it becomes an object. much better.
302 sub tables_hash_hack {
304 #note that s/(date|change)/_$1/; to avoid keyword conflict.
305 #put a kludge in FS::Record to catch this or? (pry need some date-handling
308 my(%tables)=( #yech.}
312 'agentnum', 'int', '', '',
313 'agent', 'varchar', '', $char_d,
314 'typenum', 'int', '', '',
315 'freq', 'int', 'NULL', '',
318 'primary_key' => 'agentnum',
320 'index' => [ ['typenum'] ],
325 'typenum', 'int', '', '',
326 'atype', 'varchar', '', $char_d,
328 'primary_key' => 'typenum',
335 'typenum', 'int', '', '',
336 'pkgpart', 'int', '', '',
339 'unique' => [ ['typenum', 'pkgpart'] ],
340 'index' => [ ['typenum'] ],
345 'invnum', 'int', '', '',
346 'custnum', 'int', '', '',
348 'charged', @money_type,
350 'printed', 'int', '', '',
352 'primary_key' => 'invnum',
354 'index' => [ ['custnum'] ],
359 'pkgnum', 'int', '', '',
360 'invnum', 'int', '', '',
361 'setup', @money_type,
362 'recur', @money_type,
367 'unique' => [ ['pkgnum', 'invnum'] ],
368 'index' => [ ['invnum'] ],
373 'crednum', 'int', '', '',
374 'custnum', 'int', '', '',
376 'amount', @money_type,
377 'credited', @money_type,
378 'otaker', 'varchar', '', 8,
379 'reason', 'varchar', '', 255,
381 'primary_key' => 'crednum',
383 'index' => [ ['custnum'] ],
388 'custnum', 'int', '', '',
389 'agentnum', 'int', '', '',
390 # 'titlenum', 'int', 'NULL', '',
391 'last', 'varchar', '', $char_d,
392 # 'middle', 'varchar', 'NULL', $char_d,
393 'first', 'varchar', '', $char_d,
394 'ss', 'char', 'NULL', 11,
395 'company', 'varchar', 'NULL', $char_d,
396 'address1', 'varchar', '', $char_d,
397 'address2', 'varchar', 'NULL', $char_d,
398 'city', 'varchar', '', $char_d,
399 'county', 'varchar', 'NULL', $char_d,
400 'state', 'varchar', 'NULL', $char_d,
401 'zip', 'varchar', '', 10,
402 'country', 'char', '', 2,
403 'daytime', 'varchar', 'NULL', 20,
404 'night', 'varchar', 'NULL', 20,
405 'fax', 'varchar', 'NULL', 12,
406 'payby', 'char', '', 4,
407 'payinfo', 'varchar', 'NULL', 16,
408 #'paydate', @date_type,
409 'paydate', 'varchar', 'NULL', 10,
410 'payname', 'varchar', 'NULL', $char_d,
411 'tax', 'char', 'NULL', 1,
412 'otaker', 'varchar', '', 8,
413 'refnum', 'int', '', '',
415 'primary_key' => 'custnum',
417 #'index' => [ ['last'], ['company'] ],
418 'index' => [ ['last'], ],
421 'cust_main_invoice' => {
423 'destnum', 'int', '', '',
424 'custnum', 'int', '', '',
425 'dest', 'varchar', '', $char_d,
427 'primary_key' => 'destnum',
429 'index' => [ ['custnum'], ],
432 'cust_main_county' => { #county+state+country are checked off the
433 #cust_main_county for validation and to provide
436 'taxnum', 'int', '', '',
437 'state', 'varchar', 'NULL', $char_d,
438 'county', 'varchar', 'NULL', $char_d,
439 'country', 'char', '', 2,
440 'tax', 'real', '', '', #tax %
442 'primary_key' => 'taxnum',
444 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
450 'paynum', 'int', '', '',
451 'invnum', 'int', '', '',
454 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
455 # payment type table.
456 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
457 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
459 'primary_key' => 'paynum',
461 'index' => [ ['invnum'] ],
464 'cust_pay_batch' => { #what's this used for again? list of customers
465 #in current CARD batch? (necessarily CARD?)
467 'invnum', 'int', '', '',
468 'custnum', 'int', '', '',
469 'last', 'varchar', '', $char_d,
470 'first', 'varchar', '', $char_d,
471 'address1', 'varchar', '', $char_d,
472 'address2', 'varchar', 'NULL', $char_d,
473 'city', 'varchar', '', $char_d,
474 'state', 'varchar', '', $char_d,
475 'zip', 'varchar', '', 10,
476 'country', 'char', '', 2,
477 'trancode', 'int', '', '',
478 'cardnum', 'varchar', '', 16,
480 'exp', 'varchar', '', 11,
481 'payname', 'varchar', 'NULL', $char_d,
482 'amount', @money_type,
486 'index' => [ ['invnum'], ['custnum'] ],
491 'pkgnum', 'int', '', '',
492 'custnum', 'int', '', '',
493 'pkgpart', 'int', '', '',
494 'otaker', 'varchar', '', 8,
498 'cancel', @date_type,
499 'expire', @date_type,
501 'primary_key' => 'pkgnum',
503 'index' => [ ['custnum'] ],
508 'refundnum', 'int', '', '',
509 'crednum', 'int', '', '',
511 'refund', @money_type,
512 'otaker', 'varchar', '', 8,
513 'reason', 'varchar', '', $char_d,
514 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
515 # into payment type table.
516 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
518 'primary_key' => 'refundnum',
520 'index' => [ ['crednum'] ],
525 'svcnum', 'int', '', '',
526 'pkgnum', 'int', '', '',
527 'svcpart', 'int', '', '',
529 'primary_key' => 'svcnum',
531 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
536 'pkgpart', 'int', '', '',
537 'pkg', 'varchar', '', $char_d,
538 'comment', 'varchar', '', $char_d,
540 'freq', 'int', '', '', #billing frequency (months)
543 'primary_key' => 'pkgpart',
550 # 'titlenum', 'int', '', '',
551 # 'title', 'varchar', '', $char_d,
553 # 'primary_key' => 'titlenum',
554 # 'unique' => [ [] ],
560 'pkgpart', 'int', '', '',
561 'svcpart', 'int', '', '',
562 'quantity', 'int', '', '',
565 'unique' => [ ['pkgpart', 'svcpart'] ],
566 'index' => [ ['pkgpart'] ],
571 'refnum', 'int', '', '',
572 'referral', 'varchar', '', $char_d,
574 'primary_key' => 'refnum',
581 'svcpart', 'int', '', '',
582 'svc', 'varchar', '', $char_d,
583 'svcdb', 'varchar', '', $char_d,
585 'primary_key' => 'svcpart',
590 #(this should be renamed to part_pop)
593 'popnum', 'int', '', '',
594 'city', 'varchar', '', $char_d,
595 'state', 'varchar', '', $char_d,
597 'exch', 'char', '', 3,
598 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
600 'primary_key' => 'popnum',
607 'svcnum', 'int', '', '',
608 'username', 'varchar', '', $username_len, #unique (& remove dup code)
609 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
610 'popnum', 'int', 'NULL', '',
611 'uid', 'int', 'NULL', '',
612 'gid', 'int', 'NULL', '',
613 'finger', 'varchar', 'NULL', $char_d,
614 'dir', 'varchar', 'NULL', $char_d,
615 'shell', 'varchar', 'NULL', $char_d,
616 'quota', 'varchar', 'NULL', $char_d,
617 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
619 'primary_key' => 'svcnum',
621 'index' => [ ['username'] ],
626 'svcnum', 'int', '', '',
627 'domsvc', 'int', '', '',
628 'domuid', 'int', '', '',
629 'domuser', 'varchar', '', $char_d,
631 'primary_key' => 'svcnum',
633 'index' => [ ['domsvc'], ['domuid'] ],
638 # 'svcnum', 'int', '', '',
639 # 'amount', @money_type,
641 # 'primary_key' => 'svcnum',
642 # 'unique' => [ [] ],
648 'svcnum', 'int', '', '',
649 'domain', 'varchar', '', $char_d,
651 'primary_key' => 'svcnum',
652 'unique' => [ ['domain'] ],
658 # 'svcnum', 'int', '', '',
659 # 'svcnum', 'int', '', '',
660 # 'svcnum', 'int', '', '',
661 # 'worker', 'varchar', '', $char_d,
662 # '_date', @date_type,
664 # 'primary_key' => 'svcnum',
665 # 'unique' => [ [] ],
671 'prepaynum', 'int', '', '',
672 'identifier', 'varchar', '', $char_d,
673 'amount', @money_type,
675 'primary_key' => 'prepaynum',
676 'unique' => [ ['identifier'] ],