3 # $Id: fs-setup,v 1.26 2000-07-06 08:57:27 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.26 2000-07-06 08:57:27 ivan
36 # support for radius check attributes (except importing). poorly documented.
38 # Revision 1.25 2000/06/29 12:00:49 ivan
39 # support for pre-encrypted md5 passwords.
41 # Revision 1.24 2000/03/02 07:44:07 ivan
42 # typo forgot closing '
44 # Revision 1.23 2000/02/03 05:16:52 ivan
45 # beginning of DNS and Apache support
47 # Revision 1.22 2000/01/31 05:22:23 ivan
48 # prepaid "internet cards"
50 # Revision 1.21 2000/01/30 06:03:26 ivan
51 # postgres 6.5 finally supports decimal(10,2)
53 # Revision 1.20 2000/01/28 22:53:33 ivan
54 # track full phone number
56 # Revision 1.19 1999/07/29 08:50:35 ivan
57 # wrong type for cust_pay_batch.exp
59 # Revision 1.18 1999/04/15 22:46:30 ivan
62 # Revision 1.17 1999/04/14 07:58:39 ivan
63 # export getsecrets from FS::UID instead of calling it explicitly
65 # Revision 1.16 1999/02/28 19:44:16 ivan
66 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
68 # Revision 1.15 1999/02/27 21:06:21 ivan
69 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
70 # by Ben Leibig <leibig@colorado.edu>
72 # Revision 1.14 1999/02/07 09:59:14 ivan
73 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
75 # Revision 1.13 1999/02/04 06:09:23 ivan
78 # Revision 1.12 1999/02/03 10:42:27 ivan
79 # *** empty log message ***
81 # Revision 1.11 1999/01/17 03:11:52 ivan
82 # remove preliminary completehost changes
84 # Revision 1.10 1998/12/16 06:05:38 ivan
85 # add table cust_main_invoice
87 # Revision 1.9 1998/12/15 04:36:29 ivan
90 # Revision 1.8 1998/12/15 04:33:27 ivan
91 # dies if it isn't running as the freeside user
93 # Revision 1.7 1998/11/18 09:01:31 ivan
96 # Revision 1.6 1998/11/15 13:18:02 ivan
99 # Revision 1.5 1998/11/15 09:43:03 ivan
100 # update for new config file syntax, new adminsuidsetup
102 # Revision 1.4 1998/10/22 15:51:23 ivan
103 # also varchar with no length specified - postgresql fix broke mysql.
105 # Revision 1.3 1998/10/22 15:46:28 ivan
106 # now smallint is illegal, so remove that too.
109 #to delay loading dbdef until we're ready
110 BEGIN { $FS::Record::setup_hack = 1; }
115 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
117 use FS::cust_main_county;
119 die "Not running uid freeside!" unless checkeuid();
121 my $user = shift or die &usage;
124 #needs to match FS::Record
125 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
129 print "\nEnter the maximum username length: ";
130 my($username_len)=&getvalue;
132 print "\n\n", <<END, ":";
133 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
134 reply attribute Framed-IP-Address for each user. You can specify additional
135 check and reply attributes. First enter any additional RADIUS check attributes
136 you need to track for each user, separated by whitespace.
138 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
140 print "\n\n", <<END, ":";
141 Now enter any additional reply attributes you need to track for each user,
142 separated by whitespace.
144 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
147 my($x)=scalar(<STDIN>);
154 my($char_d) = 80; #default maxlength for text fields
156 #my(@date_type) = ( 'timestamp', '', '' );
157 my(@date_type) = ( 'int', 'NULL', '' );
158 my(@perl_type) = ( 'varchar', 'NULL', 255 );
159 my @money_type = ( 'decimal', '', '10,2' );
162 # create a dbdef object from the old data structure
165 my(%tables)=&tables_hash_hack;
167 #turn it into objects
168 my($dbdef) = new FS::dbdef ( map {
170 while (@{$tables{$_}{'columns'}}) {
171 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
172 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
174 FS::dbdef_table->new(
176 $tables{$_}{'primary_key'},
177 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
178 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
179 FS::dbdef_unique->new($tables{$_}{'unique'}),
180 FS::dbdef_index->new($tables{$_}{'index'}),
185 #add radius attributes to svc_acct
187 my($svc_acct)=$dbdef->table('svc_acct');
190 foreach $attribute (@attributes) {
191 $svc_acct->addcolumn ( new FS::dbdef_column (
192 'radius_'. $attribute,
199 foreach $attribute (@check_attributes) {
200 $svc_acct->addcolumn( new FS::dbdef_column (
208 #make part_svc table (but now as object)
210 my($part_svc)=$dbdef->table('part_svc');
212 #because of svc_acct_pop
213 #foreach (grep /^svc_/, $dbdef->tables) {
214 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
215 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
216 my($table)=$dbdef->table($_);
218 foreach $col ( $table->columns ) {
219 next if $col =~ /^svcnum$/;
220 $part_svc->addcolumn( new FS::dbdef_column (
221 $table->name. '__' . $table->column($col)->name,
222 'varchar', #$table->column($col)->type,
224 $char_d, #$table->column($col)->length,
226 $part_svc->addcolumn ( new FS::dbdef_column (
227 $table->name. '__'. $table->column($col)->name . "_flag",
236 $dbdef->save($dbdef_file);
237 &FS::Record::reload_dbdef($dbdef_file);
243 my($dbh)=adminsuidsetup $user;
249 foreach ($dbdef->tables) {
250 my($table)=$dbdef->table($_);
251 print "Creating $_...";
256 foreach $statement ($table->sql_create_table(datasrc)) {
257 #print $statement, "\n";
258 $dbh->do( $statement )
259 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
265 #not really sample data (and shouldn't default to US)
271 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
272 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
273 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
275 my($cust_main_county)=new FS::cust_main_county({
281 $error=$cust_main_county->insert;
282 die $error if $error;
285 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
287 VIC NSW NT QLD TAS ACT WA SA
289 my($cust_main_county)=new FS::cust_main_county({
295 $error=$cust_main_county->insert;
296 die $error if $error;
299 #ISO 2-letter country codes (same as country TLDs) except US and AU
301 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
302 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
303 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
304 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
305 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
306 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
307 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
308 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
309 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
312 my($cust_main_county)=new FS::cust_main_county({
317 $error=$cust_main_county->insert;
318 die $error if $error;
321 $dbh->disconnect or die $dbh->errstr;
324 die "Usage:\n fs-setup user\n";
328 # Now it becomes an object. much better.
330 sub tables_hash_hack {
332 #note that s/(date|change)/_$1/; to avoid keyword conflict.
333 #put a kludge in FS::Record to catch this or? (pry need some date-handling
336 my(%tables)=( #yech.}
340 'agentnum', 'int', '', '',
341 'agent', 'varchar', '', $char_d,
342 'typenum', 'int', '', '',
343 'freq', 'int', 'NULL', '',
346 'primary_key' => 'agentnum',
348 'index' => [ ['typenum'] ],
353 'typenum', 'int', '', '',
354 'atype', 'varchar', '', $char_d,
356 'primary_key' => 'typenum',
363 'typenum', 'int', '', '',
364 'pkgpart', 'int', '', '',
367 'unique' => [ ['typenum', 'pkgpart'] ],
368 'index' => [ ['typenum'] ],
373 'invnum', 'int', '', '',
374 'custnum', 'int', '', '',
376 'charged', @money_type,
378 'printed', 'int', '', '',
380 'primary_key' => 'invnum',
382 'index' => [ ['custnum'] ],
387 'pkgnum', 'int', '', '',
388 'invnum', 'int', '', '',
389 'setup', @money_type,
390 'recur', @money_type,
395 'unique' => [ ['pkgnum', 'invnum'] ],
396 'index' => [ ['invnum'] ],
401 'crednum', 'int', '', '',
402 'custnum', 'int', '', '',
404 'amount', @money_type,
405 'credited', @money_type,
406 'otaker', 'varchar', '', 8,
407 'reason', 'varchar', '', 255,
409 'primary_key' => 'crednum',
411 'index' => [ ['custnum'] ],
416 'custnum', 'int', '', '',
417 'agentnum', 'int', '', '',
418 # 'titlenum', 'int', 'NULL', '',
419 'last', 'varchar', '', $char_d,
420 # 'middle', 'varchar', 'NULL', $char_d,
421 'first', 'varchar', '', $char_d,
422 'ss', 'char', 'NULL', 11,
423 'company', 'varchar', 'NULL', $char_d,
424 'address1', 'varchar', '', $char_d,
425 'address2', 'varchar', 'NULL', $char_d,
426 'city', 'varchar', '', $char_d,
427 'county', 'varchar', 'NULL', $char_d,
428 'state', 'varchar', 'NULL', $char_d,
429 'zip', 'varchar', '', 10,
430 'country', 'char', '', 2,
431 'daytime', 'varchar', 'NULL', 20,
432 'night', 'varchar', 'NULL', 20,
433 'fax', 'varchar', 'NULL', 12,
434 'payby', 'char', '', 4,
435 'payinfo', 'varchar', 'NULL', 16,
436 #'paydate', @date_type,
437 'paydate', 'varchar', 'NULL', 10,
438 'payname', 'varchar', 'NULL', $char_d,
439 'tax', 'char', 'NULL', 1,
440 'otaker', 'varchar', '', 8,
441 'refnum', 'int', '', '',
443 'primary_key' => 'custnum',
445 #'index' => [ ['last'], ['company'] ],
446 'index' => [ ['last'], ],
449 'cust_main_invoice' => {
451 'destnum', 'int', '', '',
452 'custnum', 'int', '', '',
453 'dest', 'varchar', '', $char_d,
455 'primary_key' => 'destnum',
457 'index' => [ ['custnum'], ],
460 'cust_main_county' => { #county+state+country are checked off the
461 #cust_main_county for validation and to provide
464 'taxnum', 'int', '', '',
465 'state', 'varchar', 'NULL', $char_d,
466 'county', 'varchar', 'NULL', $char_d,
467 'country', 'char', '', 2,
468 'tax', 'real', '', '', #tax %
470 'primary_key' => 'taxnum',
472 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
478 'paynum', 'int', '', '',
479 'invnum', 'int', '', '',
482 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
483 # payment type table.
484 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
485 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
487 'primary_key' => 'paynum',
489 'index' => [ ['invnum'] ],
492 'cust_pay_batch' => { #what's this used for again? list of customers
493 #in current CARD batch? (necessarily CARD?)
495 'invnum', 'int', '', '',
496 'custnum', 'int', '', '',
497 'last', 'varchar', '', $char_d,
498 'first', 'varchar', '', $char_d,
499 'address1', 'varchar', '', $char_d,
500 'address2', 'varchar', 'NULL', $char_d,
501 'city', 'varchar', '', $char_d,
502 'state', 'varchar', '', $char_d,
503 'zip', 'varchar', '', 10,
504 'country', 'char', '', 2,
505 'trancode', 'int', '', '',
506 'cardnum', 'varchar', '', 16,
508 'exp', 'varchar', '', 11,
509 'payname', 'varchar', 'NULL', $char_d,
510 'amount', @money_type,
514 'index' => [ ['invnum'], ['custnum'] ],
519 'pkgnum', 'int', '', '',
520 'custnum', 'int', '', '',
521 'pkgpart', 'int', '', '',
522 'otaker', 'varchar', '', 8,
526 'cancel', @date_type,
527 'expire', @date_type,
529 'primary_key' => 'pkgnum',
531 'index' => [ ['custnum'] ],
536 'refundnum', 'int', '', '',
537 'crednum', 'int', '', '',
539 'refund', @money_type,
540 'otaker', 'varchar', '', 8,
541 'reason', 'varchar', '', $char_d,
542 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
543 # into payment type table.
544 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
546 'primary_key' => 'refundnum',
548 'index' => [ ['crednum'] ],
553 'svcnum', 'int', '', '',
554 'pkgnum', 'int', '', '',
555 'svcpart', 'int', '', '',
557 'primary_key' => 'svcnum',
559 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
564 'pkgpart', 'int', '', '',
565 'pkg', 'varchar', '', $char_d,
566 'comment', 'varchar', '', $char_d,
568 'freq', 'int', '', '', #billing frequency (months)
571 'primary_key' => 'pkgpart',
578 # 'titlenum', 'int', '', '',
579 # 'title', 'varchar', '', $char_d,
581 # 'primary_key' => 'titlenum',
582 # 'unique' => [ [] ],
588 'pkgpart', 'int', '', '',
589 'svcpart', 'int', '', '',
590 'quantity', 'int', '', '',
593 'unique' => [ ['pkgpart', 'svcpart'] ],
594 'index' => [ ['pkgpart'] ],
599 'refnum', 'int', '', '',
600 'referral', 'varchar', '', $char_d,
602 'primary_key' => 'refnum',
609 'svcpart', 'int', '', '',
610 'svc', 'varchar', '', $char_d,
611 'svcdb', 'varchar', '', $char_d,
613 'primary_key' => 'svcpart',
618 #(this should be renamed to part_pop)
621 'popnum', 'int', '', '',
622 'city', 'varchar', '', $char_d,
623 'state', 'varchar', '', $char_d,
625 'exch', 'char', '', 3,
626 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
628 'primary_key' => 'popnum',
635 'svcnum', 'int', '', '',
636 'username', 'varchar', '', $username_len, #unique (& remove dup code)
637 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
638 'popnum', 'int', 'NULL', '',
639 'uid', 'int', 'NULL', '',
640 'gid', 'int', 'NULL', '',
641 'finger', 'varchar', 'NULL', $char_d,
642 'dir', 'varchar', 'NULL', $char_d,
643 'shell', 'varchar', 'NULL', $char_d,
644 'quota', 'varchar', 'NULL', $char_d,
645 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
647 'primary_key' => 'svcnum',
649 'index' => [ ['username'] ],
654 'svcnum', 'int', '', '',
655 'domsvc', 'int', '', '',
656 'domuid', 'int', '', '',
657 'domuser', 'varchar', '', $char_d,
659 'primary_key' => 'svcnum',
661 'index' => [ ['domsvc'], ['domuid'] ],
666 # 'svcnum', 'int', '', '',
667 # 'amount', @money_type,
669 # 'primary_key' => 'svcnum',
670 # 'unique' => [ [] ],
676 'svcnum', 'int', '', '',
677 'domain', 'varchar', '', $char_d,
679 'primary_key' => 'svcnum',
680 'unique' => [ ['domain'] ],
686 'recnum', 'int', '', '',
687 'svcnum', 'int', '', '',
688 'reczone', 'varchar', '', $char_d,
689 'recaf', 'char', '', 2,
690 'rectype', 'char', '', 5,
691 'recdata', 'varchar', '', $char_d,
693 'primary_key' => 'recnum',
695 'index' => [ ['svcnum'] ],
700 'svcnum', 'int', '', '',
701 'recnum', 'int', '', '',
702 'usersvc', 'int', '', '',
704 'primary_key' => 'svcnum',
711 # 'svcnum', 'int', '', '',
712 # 'svcnum', 'int', '', '',
713 # 'svcnum', 'int', '', '',
714 # 'worker', 'varchar', '', $char_d,
715 # '_date', @date_type,
717 # 'primary_key' => 'svcnum',
718 # 'unique' => [ [] ],
724 'prepaynum', 'int', '', '',
725 'identifier', 'varchar', '', $char_d,
726 'amount', @money_type,
728 'primary_key' => 'prepaynum',
729 'unique' => [ ['identifier'] ],