3 # $Id: fs-setup,v 1.35 2001-04-15 09:36:43 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.35 2001-04-15 09:36:43 ivan
36 # http://www.sisd.com/freeside/list-archive/msg01450.html
38 # Revision 1.34 2001/04/09 23:05:16 ivan
39 # Transactions Part I!!!
41 # Revision 1.33 2001/02/03 14:03:50 ivan
42 # time-based prepaid cards, session monitor. woop!
44 # Revision 1.32 2000/12/04 00:13:02 ivan
47 # Revision 1.31 2000/12/01 18:34:53 ivan
50 # Revision 1.30 2000/12/01 18:33:32 ivan
53 # Revision 1.29 2000/11/07 15:00:37 ivan
56 # Revision 1.28 2000/10/30 10:47:26 ivan
57 # nas.last can't be defined NULL if indexed
59 # Revision 1.26 2000/07/06 08:57:27 ivan
60 # support for radius check attributes (except importing). poorly documented.
62 # Revision 1.25 2000/06/29 12:00:49 ivan
63 # support for pre-encrypted md5 passwords.
65 # Revision 1.24 2000/03/02 07:44:07 ivan
66 # typo forgot closing '
68 # Revision 1.23 2000/02/03 05:16:52 ivan
69 # beginning of DNS and Apache support
71 # Revision 1.22 2000/01/31 05:22:23 ivan
72 # prepaid "internet cards"
74 # Revision 1.21 2000/01/30 06:03:26 ivan
75 # postgres 6.5 finally supports decimal(10,2)
77 # Revision 1.20 2000/01/28 22:53:33 ivan
78 # track full phone number
80 # Revision 1.19 1999/07/29 08:50:35 ivan
81 # wrong type for cust_pay_batch.exp
83 # Revision 1.18 1999/04/15 22:46:30 ivan
86 # Revision 1.17 1999/04/14 07:58:39 ivan
87 # export getsecrets from FS::UID instead of calling it explicitly
89 # Revision 1.16 1999/02/28 19:44:16 ivan
90 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
92 # Revision 1.15 1999/02/27 21:06:21 ivan
93 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
94 # by Ben Leibig <leibig@colorado.edu>
96 # Revision 1.14 1999/02/07 09:59:14 ivan
97 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
99 # Revision 1.13 1999/02/04 06:09:23 ivan
102 # Revision 1.12 1999/02/03 10:42:27 ivan
103 # *** empty log message ***
105 # Revision 1.11 1999/01/17 03:11:52 ivan
106 # remove preliminary completehost changes
108 # Revision 1.10 1998/12/16 06:05:38 ivan
109 # add table cust_main_invoice
111 # Revision 1.9 1998/12/15 04:36:29 ivan
112 # s/croak/die/; #oops
114 # Revision 1.8 1998/12/15 04:33:27 ivan
115 # dies if it isn't running as the freeside user
117 # Revision 1.7 1998/11/18 09:01:31 ivan
120 # Revision 1.6 1998/11/15 13:18:02 ivan
123 # Revision 1.5 1998/11/15 09:43:03 ivan
124 # update for new config file syntax, new adminsuidsetup
126 # Revision 1.4 1998/10/22 15:51:23 ivan
127 # also varchar with no length specified - postgresql fix broke mysql.
129 # Revision 1.3 1998/10/22 15:46:28 ivan
130 # now smallint is illegal, so remove that too.
133 #to delay loading dbdef until we're ready
134 BEGIN { $FS::Record::setup_hack = 1; }
139 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
141 use FS::cust_main_county;
143 die "Not running uid freeside!" unless checkeuid();
145 my $user = shift or die &usage;
148 #needs to match FS::Record
149 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
153 print "\nEnter the maximum username length: ";
154 my($username_len)=&getvalue;
156 print "\n\n", <<END, ":";
157 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
158 reply attribute Framed-IP-Address for each user. You can specify additional
159 check and reply attributes. First enter any additional RADIUS check attributes
160 you need to track for each user, separated by whitespace.
162 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
164 print "\n\n", <<END, ":";
165 Now enter any additional reply attributes you need to track for each user,
166 separated by whitespace.
168 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
171 my($x)=scalar(<STDIN>);
178 my($char_d) = 80; #default maxlength for text fields
180 #my(@date_type) = ( 'timestamp', '', '' );
181 my(@date_type) = ( 'int', 'NULL', '' );
182 my(@perl_type) = ( 'varchar', 'NULL', 255 );
183 my @money_type = ( 'decimal', '', '10,2' );
186 # create a dbdef object from the old data structure
189 my(%tables)=&tables_hash_hack;
191 #turn it into objects
192 my($dbdef) = new FS::dbdef ( map {
194 while (@{$tables{$_}{'columns'}}) {
195 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
196 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
198 FS::dbdef_table->new(
200 $tables{$_}{'primary_key'},
201 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
202 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
203 FS::dbdef_unique->new($tables{$_}{'unique'}),
204 FS::dbdef_index->new($tables{$_}{'index'}),
209 #add radius attributes to svc_acct
211 my($svc_acct)=$dbdef->table('svc_acct');
214 foreach $attribute (@attributes) {
215 $svc_acct->addcolumn ( new FS::dbdef_column (
216 'radius_'. $attribute,
223 foreach $attribute (@check_attributes) {
224 $svc_acct->addcolumn( new FS::dbdef_column (
232 #make part_svc table (but now as object)
234 my($part_svc)=$dbdef->table('part_svc');
236 #because of svc_acct_pop
237 #foreach (grep /^svc_/, $dbdef->tables) {
238 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
239 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
240 my($table)=$dbdef->table($_);
242 foreach $col ( $table->columns ) {
243 next if $col =~ /^svcnum$/;
244 $part_svc->addcolumn( new FS::dbdef_column (
245 $table->name. '__' . $table->column($col)->name,
246 'varchar', #$table->column($col)->type,
248 $char_d, #$table->column($col)->length,
250 $part_svc->addcolumn ( new FS::dbdef_column (
251 $table->name. '__'. $table->column($col)->name . "_flag",
260 $dbdef->save($dbdef_file);
261 &FS::Record::reload_dbdef($dbdef_file);
267 my($dbh)=adminsuidsetup $user;
273 foreach ($dbdef->tables) {
274 my($table)=$dbdef->table($_);
275 print "Creating $_...";
280 foreach $statement ($table->sql_create_table(datasrc)) {
281 #print $statement, "\n";
282 $dbh->do( $statement )
283 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
289 #not really sample data (and shouldn't default to US)
295 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
296 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
297 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
299 my($cust_main_county)=new FS::cust_main_county({
305 $error=$cust_main_county->insert;
306 die $error if $error;
309 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
311 VIC NSW NT QLD TAS ACT WA SA
313 my($cust_main_county)=new FS::cust_main_county({
319 $error=$cust_main_county->insert;
320 die $error if $error;
323 #ISO 2-letter country codes (same as country TLDs) except US and AU
325 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
326 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
327 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
328 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
329 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
330 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
331 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
332 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
333 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
336 my($cust_main_county)=new FS::cust_main_county({
341 $error=$cust_main_county->insert;
342 die $error if $error;
345 $dbh->disconnect or die $dbh->errstr;
348 die "Usage:\n fs-setup user\n";
352 # Now it becomes an object. much better.
354 sub tables_hash_hack {
356 #note that s/(date|change)/_$1/; to avoid keyword conflict.
357 #put a kludge in FS::Record to catch this or? (pry need some date-handling
360 my(%tables)=( #yech.}
364 'agentnum', 'int', '', '',
365 'agent', 'varchar', '', $char_d,
366 'typenum', 'int', '', '',
367 'freq', 'int', 'NULL', '',
370 'primary_key' => 'agentnum',
372 'index' => [ ['typenum'] ],
377 'typenum', 'int', '', '',
378 'atype', 'varchar', '', $char_d,
380 'primary_key' => 'typenum',
387 'typenum', 'int', '', '',
388 'pkgpart', 'int', '', '',
391 'unique' => [ ['typenum', 'pkgpart'] ],
392 'index' => [ ['typenum'] ],
397 'invnum', 'int', '', '',
398 'custnum', 'int', '', '',
400 'charged', @money_type,
401 'printed', 'int', '', '',
403 'primary_key' => 'invnum',
405 'index' => [ ['custnum'] ],
410 'pkgnum', 'int', '', '',
411 'invnum', 'int', '', '',
412 'setup', @money_type,
413 'recur', @money_type,
418 'unique' => [ ['pkgnum', 'invnum'] ],
419 'index' => [ ['invnum'] ],
424 'crednum', 'int', '', '',
425 'custnum', 'int', '', '',
427 'amount', @money_type,
428 'otaker', 'varchar', '', 8,
429 'reason', 'varchar', '', 255,
431 'primary_key' => 'crednum',
433 'index' => [ ['custnum'] ],
438 'custnum', 'int', '', '',
439 'agentnum', 'int', '', '',
440 # 'titlenum', 'int', 'NULL', '',
441 'last', 'varchar', '', $char_d,
442 # 'middle', 'varchar', 'NULL', $char_d,
443 'first', 'varchar', '', $char_d,
444 'ss', 'char', 'NULL', 11,
445 'company', 'varchar', 'NULL', $char_d,
446 'address1', 'varchar', '', $char_d,
447 'address2', 'varchar', 'NULL', $char_d,
448 'city', 'varchar', '', $char_d,
449 'county', 'varchar', 'NULL', $char_d,
450 'state', 'varchar', 'NULL', $char_d,
451 'zip', 'varchar', '', 10,
452 'country', 'char', '', 2,
453 'daytime', 'varchar', 'NULL', 20,
454 'night', 'varchar', 'NULL', 20,
455 'fax', 'varchar', 'NULL', 12,
456 'payby', 'char', '', 4,
457 'payinfo', 'varchar', 'NULL', 16,
458 #'paydate', @date_type,
459 'paydate', 'varchar', 'NULL', 10,
460 'payname', 'varchar', 'NULL', $char_d,
461 'tax', 'char', 'NULL', 1,
462 'otaker', 'varchar', '', 8,
463 'refnum', 'int', '', '',
465 'primary_key' => 'custnum',
467 #'index' => [ ['last'], ['company'] ],
468 'index' => [ ['last'], ],
471 'cust_main_invoice' => {
473 'destnum', 'int', '', '',
474 'custnum', 'int', '', '',
475 'dest', 'varchar', '', $char_d,
477 'primary_key' => 'destnum',
479 'index' => [ ['custnum'], ],
482 'cust_main_county' => { #county+state+country are checked off the
483 #cust_main_county for validation and to provide
486 'taxnum', 'int', '', '',
487 'state', 'varchar', 'NULL', $char_d,
488 'county', 'varchar', 'NULL', $char_d,
489 'country', 'char', '', 2,
490 'tax', 'real', '', '', #tax %
492 'primary_key' => 'taxnum',
494 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
500 'paynum', 'int', '', '',
501 'invnum', 'int', '', '',
504 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
505 # payment type table.
506 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
507 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
509 'primary_key' => 'paynum',
511 'index' => [ ['invnum'] ],
514 'cust_pay_batch' => { #what's this used for again? list of customers
515 #in current CARD batch? (necessarily CARD?)
517 'invnum', 'int', '', '',
518 'custnum', 'int', '', '',
519 'last', 'varchar', '', $char_d,
520 'first', 'varchar', '', $char_d,
521 'address1', 'varchar', '', $char_d,
522 'address2', 'varchar', 'NULL', $char_d,
523 'city', 'varchar', '', $char_d,
524 'state', 'varchar', '', $char_d,
525 'zip', 'varchar', '', 10,
526 'country', 'char', '', 2,
527 'trancode', 'int', '', '',
528 'cardnum', 'varchar', '', 16,
530 'exp', 'varchar', '', 11,
531 'payname', 'varchar', 'NULL', $char_d,
532 'amount', @money_type,
536 'index' => [ ['invnum'], ['custnum'] ],
541 'pkgnum', 'int', '', '',
542 'custnum', 'int', '', '',
543 'pkgpart', 'int', '', '',
544 'otaker', 'varchar', '', 8,
548 'cancel', @date_type,
549 'expire', @date_type,
551 'primary_key' => 'pkgnum',
553 'index' => [ ['custnum'] ],
558 'refundnum', 'int', '', '',
559 'crednum', 'int', '', '',
561 'refund', @money_type,
562 'otaker', 'varchar', '', 8,
563 'reason', 'varchar', '', $char_d,
564 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
565 # into payment type table.
566 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
568 'primary_key' => 'refundnum',
570 'index' => [ ['crednum'] ],
575 'svcnum', 'int', '', '',
576 'pkgnum', 'int', 'NULL', '',
577 'svcpart', 'int', '', '',
579 'primary_key' => 'svcnum',
581 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
586 'pkgpart', 'int', '', '',
587 'pkg', 'varchar', '', $char_d,
588 'comment', 'varchar', '', $char_d,
590 'freq', 'int', '', '', #billing frequency (months)
593 'primary_key' => 'pkgpart',
600 # 'titlenum', 'int', '', '',
601 # 'title', 'varchar', '', $char_d,
603 # 'primary_key' => 'titlenum',
604 # 'unique' => [ [] ],
610 'pkgpart', 'int', '', '',
611 'svcpart', 'int', '', '',
612 'quantity', 'int', '', '',
615 'unique' => [ ['pkgpart', 'svcpart'] ],
616 'index' => [ ['pkgpart'] ],
621 'refnum', 'int', '', '',
622 'referral', 'varchar', '', $char_d,
624 'primary_key' => 'refnum',
631 'svcpart', 'int', '', '',
632 'svc', 'varchar', '', $char_d,
633 'svcdb', 'varchar', '', $char_d,
635 'primary_key' => 'svcpart',
640 #(this should be renamed to part_pop)
643 'popnum', 'int', '', '',
644 'city', 'varchar', '', $char_d,
645 'state', 'varchar', '', $char_d,
647 'exch', 'char', '', 3,
648 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
650 'primary_key' => 'popnum',
657 'svcnum', 'int', '', '',
658 'username', 'varchar', '', $username_len, #unique (& remove dup code)
659 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
660 'popnum', 'int', 'NULL', '',
661 'uid', 'int', 'NULL', '',
662 'gid', 'int', 'NULL', '',
663 'finger', 'varchar', 'NULL', $char_d,
664 'dir', 'varchar', 'NULL', $char_d,
665 'shell', 'varchar', 'NULL', $char_d,
666 'quota', 'varchar', 'NULL', $char_d,
667 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
668 'seconds', 'int', 'NULL', '', #uhhhh
670 'primary_key' => 'svcnum',
672 'index' => [ ['username'] ],
677 'svcnum', 'int', '', '',
678 'domsvc', 'int', '', '',
679 'domuid', 'int', '', '',
680 'domuser', 'varchar', '', $char_d,
682 'primary_key' => 'svcnum',
684 'index' => [ ['domsvc'], ['domuid'] ],
689 # 'svcnum', 'int', '', '',
690 # 'amount', @money_type,
692 # 'primary_key' => 'svcnum',
693 # 'unique' => [ [] ],
699 'svcnum', 'int', '', '',
700 'domain', 'varchar', '', $char_d,
702 'primary_key' => 'svcnum',
703 'unique' => [ ['domain'] ],
709 'recnum', 'int', '', '',
710 'svcnum', 'int', '', '',
711 'reczone', 'varchar', '', $char_d,
712 'recaf', 'char', '', 2,
713 'rectype', 'char', '', 5,
714 'recdata', 'varchar', '', $char_d,
716 'primary_key' => 'recnum',
718 'index' => [ ['svcnum'] ],
723 'svcnum', 'int', '', '',
724 'recnum', 'int', '', '',
725 'usersvc', 'int', '', '',
727 'primary_key' => 'svcnum',
734 # 'svcnum', 'int', '', '',
735 # 'svcnum', 'int', '', '',
736 # 'svcnum', 'int', '', '',
737 # 'worker', 'varchar', '', $char_d,
738 # '_date', @date_type,
740 # 'primary_key' => 'svcnum',
741 # 'unique' => [ [] ],
747 'prepaynum', 'int', '', '',
748 'identifier', 'varchar', '', $char_d,
749 'amount', @money_type,
750 'seconds', 'int', 'NULL', '',
752 'primary_key' => 'prepaynum',
753 'unique' => [ ['identifier'] ],
759 'portnum', 'int', '', '',
760 'ip', 'varchar', 'NULL', 15,
761 'nasport', 'int', 'NULL', '',
762 'nasnum', 'int', '', '',
764 'primary_key' => 'portnum',
771 'nasnum', 'int', '', '',
772 'nas', 'varchar', '', $char_d,
773 'nasip', 'varchar', '', 15,
774 'nasfqdn', 'varchar', '', $char_d,
775 'last', 'int', '', '',
777 'primary_key' => 'nasnum',
778 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
779 'index' => [ [ 'last' ] ],
784 'sessionnum', 'int', '', '',
785 'portnum', 'int', '', '',
786 'svcnum', 'int', '', '',
788 'logout', @date_type,
790 'primary_key' => 'sessionnum',
792 'index' => [ [ 'portnum' ] ],