3 # $Id: fs-setup,v 1.23 2000-02-03 05:16:52 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.23 2000-02-03 05:16:52 ivan
36 # beginning of DNS and Apache support
38 # Revision 1.22 2000/01/31 05:22:23 ivan
39 # prepaid "internet cards"
41 # Revision 1.21 2000/01/30 06:03:26 ivan
42 # postgres 6.5 finally supports decimal(10,2)
44 # Revision 1.20 2000/01/28 22:53:33 ivan
45 # track full phone number
47 # Revision 1.19 1999/07/29 08:50:35 ivan
48 # wrong type for cust_pay_batch.exp
50 # Revision 1.18 1999/04/15 22:46:30 ivan
53 # Revision 1.17 1999/04/14 07:58:39 ivan
54 # export getsecrets from FS::UID instead of calling it explicitly
56 # Revision 1.16 1999/02/28 19:44:16 ivan
57 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
59 # Revision 1.15 1999/02/27 21:06:21 ivan
60 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
61 # by Ben Leibig <leibig@colorado.edu>
63 # Revision 1.14 1999/02/07 09:59:14 ivan
64 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
66 # Revision 1.13 1999/02/04 06:09:23 ivan
69 # Revision 1.12 1999/02/03 10:42:27 ivan
70 # *** empty log message ***
72 # Revision 1.11 1999/01/17 03:11:52 ivan
73 # remove preliminary completehost changes
75 # Revision 1.10 1998/12/16 06:05:38 ivan
76 # add table cust_main_invoice
78 # Revision 1.9 1998/12/15 04:36:29 ivan
81 # Revision 1.8 1998/12/15 04:33:27 ivan
82 # dies if it isn't running as the freeside user
84 # Revision 1.7 1998/11/18 09:01:31 ivan
87 # Revision 1.6 1998/11/15 13:18:02 ivan
90 # Revision 1.5 1998/11/15 09:43:03 ivan
91 # update for new config file syntax, new adminsuidsetup
93 # Revision 1.4 1998/10/22 15:51:23 ivan
94 # also varchar with no length specified - postgresql fix broke mysql.
96 # Revision 1.3 1998/10/22 15:46:28 ivan
97 # now smallint is illegal, so remove that too.
100 #to delay loading dbdef until we're ready
101 BEGIN { $FS::Record::setup_hack = 1; }
106 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
108 use FS::cust_main_county;
110 die "Not running uid freeside!" unless checkeuid();
112 my $user = shift or die &usage;
115 #needs to match FS::Record
116 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
120 print "\nEnter the maximum username length: ";
121 my($username_len)=&getvalue;
123 print "\n\n", <<END, ":";
124 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
125 for each user. Enter any additional RADIUS attributes you need to track for
126 each user, separated by whitespace.
128 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
131 my($x)=scalar(<STDIN>);
138 my($char_d) = 80; #default maxlength for text fields
140 #my(@date_type) = ( 'timestamp', '', '' );
141 my(@date_type) = ( 'int', 'NULL', '' );
142 my(@perl_type) = ( 'varchar', 'NULL', 255 );
143 my @money_type = ( 'decimal', '', '10,2' );
146 # create a dbdef object from the old data structure
149 my(%tables)=&tables_hash_hack;
151 #turn it into objects
152 my($dbdef) = new FS::dbdef ( map {
154 while (@{$tables{$_}{'columns'}}) {
155 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
156 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
158 FS::dbdef_table->new(
160 $tables{$_}{'primary_key'},
161 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
162 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
163 FS::dbdef_unique->new($tables{$_}{'unique'}),
164 FS::dbdef_index->new($tables{$_}{'index'}),
169 #add radius attributes to svc_acct
171 my($svc_acct)=$dbdef->table('svc_acct');
174 foreach $attribute (@attributes) {
175 $svc_acct->addcolumn ( new FS::dbdef_column (
176 'radius_'. $attribute,
183 #make part_svc table (but now as object)
185 my($part_svc)=$dbdef->table('part_svc');
187 #because of svc_acct_pop
188 #foreach (grep /^svc_/, $dbdef->tables) {
189 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
190 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
191 my($table)=$dbdef->table($_);
193 foreach $col ( $table->columns ) {
194 next if $col =~ /^svcnum$/;
195 $part_svc->addcolumn( new FS::dbdef_column (
196 $table->name. '__' . $table->column($col)->name,
197 'varchar', #$table->column($col)->type,
199 $char_d, #$table->column($col)->length,
201 $part_svc->addcolumn ( new FS::dbdef_column (
202 $table->name. '__'. $table->column($col)->name . "_flag",
211 $dbdef->save($dbdef_file);
212 &FS::Record::reload_dbdef($dbdef_file);
218 my($dbh)=adminsuidsetup $user;
224 foreach ($dbdef->tables) {
225 my($table)=$dbdef->table($_);
226 print "Creating $_...";
231 foreach $statement ($table->sql_create_table(datasrc)) {
232 #print $statement, "\n";
233 $dbh->do( $statement )
234 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
240 #not really sample data (and shouldn't default to US)
246 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
247 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
248 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
250 my($cust_main_county)=new FS::cust_main_county({
256 $error=$cust_main_county->insert;
257 die $error if $error;
260 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
262 VIC NSW NT QLD TAS ACT WA SA
264 my($cust_main_county)=new FS::cust_main_county({
270 $error=$cust_main_county->insert;
271 die $error if $error;
274 #ISO 2-letter country codes (same as country TLDs) except US and AU
276 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
277 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
278 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
279 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
280 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
281 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
282 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
283 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
284 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
287 my($cust_main_county)=new FS::cust_main_county({
292 $error=$cust_main_county->insert;
293 die $error if $error;
296 $dbh->disconnect or die $dbh->errstr;
299 die "Usage:\n fs-setup user\n";
303 # Now it becomes an object. much better.
305 sub tables_hash_hack {
307 #note that s/(date|change)/_$1/; to avoid keyword conflict.
308 #put a kludge in FS::Record to catch this or? (pry need some date-handling
311 my(%tables)=( #yech.}
315 'agentnum', 'int', '', '',
316 'agent', 'varchar', '', $char_d,
317 'typenum', 'int', '', '',
318 'freq', 'int', 'NULL', '',
321 'primary_key' => 'agentnum',
323 'index' => [ ['typenum'] ],
328 'typenum', 'int', '', '',
329 'atype', 'varchar', '', $char_d,
331 'primary_key' => 'typenum',
338 'typenum', 'int', '', '',
339 'pkgpart', 'int', '', '',
342 'unique' => [ ['typenum', 'pkgpart'] ],
343 'index' => [ ['typenum'] ],
348 'invnum', 'int', '', '',
349 'custnum', 'int', '', '',
351 'charged', @money_type,
353 'printed', 'int', '', '',
355 'primary_key' => 'invnum',
357 'index' => [ ['custnum'] ],
362 'pkgnum', 'int', '', '',
363 'invnum', 'int', '', '',
364 'setup', @money_type,
365 'recur', @money_type,
370 'unique' => [ ['pkgnum', 'invnum'] ],
371 'index' => [ ['invnum'] ],
376 'crednum', 'int', '', '',
377 'custnum', 'int', '', '',
379 'amount', @money_type,
380 'credited', @money_type,
381 'otaker', 'varchar', '', 8,
382 'reason', 'varchar', '', 255,
384 'primary_key' => 'crednum',
386 'index' => [ ['custnum'] ],
391 'custnum', 'int', '', '',
392 'agentnum', 'int', '', '',
393 # 'titlenum', 'int', 'NULL', '',
394 'last', 'varchar', '', $char_d,
395 # 'middle', 'varchar', 'NULL', $char_d,
396 'first', 'varchar', '', $char_d,
397 'ss', 'char', 'NULL', 11,
398 'company', 'varchar', 'NULL', $char_d,
399 'address1', 'varchar', '', $char_d,
400 'address2', 'varchar', 'NULL', $char_d,
401 'city', 'varchar', '', $char_d,
402 'county', 'varchar', 'NULL', $char_d,
403 'state', 'varchar', 'NULL', $char_d,
404 'zip', 'varchar', '', 10,
405 'country', 'char', '', 2,
406 'daytime', 'varchar', 'NULL', 20,
407 'night', 'varchar', 'NULL', 20,
408 'fax', 'varchar', 'NULL', 12,
409 'payby', 'char', '', 4,
410 'payinfo', 'varchar', 'NULL', 16,
411 #'paydate', @date_type,
412 'paydate', 'varchar', 'NULL', 10,
413 'payname', 'varchar', 'NULL', $char_d,
414 'tax', 'char', 'NULL', 1,
415 'otaker', 'varchar', '', 8,
416 'refnum', 'int', '', '',
418 'primary_key' => 'custnum',
420 #'index' => [ ['last'], ['company'] ],
421 'index' => [ ['last'], ],
424 'cust_main_invoice' => {
426 'destnum', 'int', '', '',
427 'custnum', 'int', '', '',
428 'dest', 'varchar', '', $char_d,
430 'primary_key' => 'destnum',
432 'index' => [ ['custnum'], ],
435 'cust_main_county' => { #county+state+country are checked off the
436 #cust_main_county for validation and to provide
439 'taxnum', 'int', '', '',
440 'state', 'varchar', 'NULL', $char_d,
441 'county', 'varchar', 'NULL', $char_d,
442 'country', 'char', '', 2,
443 'tax', 'real', '', '', #tax %
445 'primary_key' => 'taxnum',
447 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
453 'paynum', 'int', '', '',
454 'invnum', 'int', '', '',
457 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
458 # payment type table.
459 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
460 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
462 'primary_key' => 'paynum',
464 'index' => [ ['invnum'] ],
467 'cust_pay_batch' => { #what's this used for again? list of customers
468 #in current CARD batch? (necessarily CARD?)
470 'invnum', 'int', '', '',
471 'custnum', 'int', '', '',
472 'last', 'varchar', '', $char_d,
473 'first', 'varchar', '', $char_d,
474 'address1', 'varchar', '', $char_d,
475 'address2', 'varchar', 'NULL', $char_d,
476 'city', 'varchar', '', $char_d,
477 'state', 'varchar', '', $char_d,
478 'zip', 'varchar', '', 10,
479 'country', 'char', '', 2,
480 'trancode', 'int', '', '',
481 'cardnum', 'varchar', '', 16,
483 'exp', 'varchar', '', 11,
484 'payname', 'varchar', 'NULL', $char_d,
485 'amount', @money_type,
489 'index' => [ ['invnum'], ['custnum'] ],
494 'pkgnum', 'int', '', '',
495 'custnum', 'int', '', '',
496 'pkgpart', 'int', '', '',
497 'otaker', 'varchar', '', 8,
501 'cancel', @date_type,
502 'expire', @date_type,
504 'primary_key' => 'pkgnum',
506 'index' => [ ['custnum'] ],
511 'refundnum', 'int', '', '',
512 'crednum', 'int', '', '',
514 'refund', @money_type,
515 'otaker', 'varchar', '', 8,
516 'reason', 'varchar', '', $char_d,
517 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
518 # into payment type table.
519 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
521 'primary_key' => 'refundnum',
523 'index' => [ ['crednum'] ],
528 'svcnum', 'int', '', '',
529 'pkgnum', 'int', '', '',
530 'svcpart', 'int', '', '',
532 'primary_key' => 'svcnum',
534 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
539 'pkgpart', 'int', '', '',
540 'pkg', 'varchar', '', $char_d,
541 'comment', 'varchar', '', $char_d,
543 'freq', 'int', '', '', #billing frequency (months)
546 'primary_key' => 'pkgpart',
553 # 'titlenum', 'int', '', '',
554 # 'title', 'varchar', '', $char_d,
556 # 'primary_key' => 'titlenum',
557 # 'unique' => [ [] ],
563 'pkgpart', 'int', '', '',
564 'svcpart', 'int', '', '',
565 'quantity', 'int', '', '',
568 'unique' => [ ['pkgpart', 'svcpart'] ],
569 'index' => [ ['pkgpart'] ],
574 'refnum', 'int', '', '',
575 'referral', 'varchar', '', $char_d,
577 'primary_key' => 'refnum',
584 'svcpart', 'int', '', '',
585 'svc', 'varchar', '', $char_d,
586 'svcdb', 'varchar', '', $char_d,
588 'primary_key' => 'svcpart',
593 #(this should be renamed to part_pop)
596 'popnum', 'int', '', '',
597 'city', 'varchar', '', $char_d,
598 'state', 'varchar', '', $char_d,
600 'exch', 'char', '', 3,
601 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
603 'primary_key' => 'popnum',
610 'svcnum', 'int', '', '',
611 'username', 'varchar', '', $username_len, #unique (& remove dup code)
612 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
613 'popnum', 'int', 'NULL', '',
614 'uid', 'int', 'NULL', '',
615 'gid', 'int', 'NULL', '',
616 'finger', 'varchar', 'NULL', $char_d,
617 'dir', 'varchar', 'NULL', $char_d,
618 'shell', 'varchar', 'NULL', $char_d,
619 'quota', 'varchar', 'NULL', $char_d,
620 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
622 'primary_key' => 'svcnum',
624 'index' => [ ['username'] ],
629 'svcnum', 'int', '', '',
630 'domsvc', 'int', '', '',
631 'domuid', 'int', '', '',
632 'domuser', 'varchar', '', $char_d,
634 'primary_key' => 'svcnum',
636 'index' => [ ['domsvc'], ['domuid'] ],
641 # 'svcnum', 'int', '', '',
642 # 'amount', @money_type,
644 # 'primary_key' => 'svcnum',
645 # 'unique' => [ [] ],
651 'svcnum', 'int', '', '',
652 'domain', 'varchar', '', $char_d,
654 'primary_key' => 'svcnum',
655 'unique' => [ ['domain'] ],
661 'recnum', 'int', '', '',
662 'svcnum', 'int', '', '',
663 'reczone', 'varchar', '', $char_d,
664 'recaf', 'char', '', 2,
665 'rectype', 'char', '', 5,
666 'recdata', 'varchar', '', $char_d,
668 'primary_key' => 'recnum',
670 'index' => [ ['svcnum'] ],
675 'svcnum', 'int', '', '',
676 'recnum', 'int', '', '',
677 'usersvc', 'int', '', '',
679 'primary_key' => 'svcnum',
686 # 'svcnum', 'int', '', '',
687 # 'svcnum', 'int', '', '',
688 # 'svcnum', 'int', '', '',
689 # 'worker', 'varchar', '', $char_d,
690 # '_date', @date_type,
692 # 'primary_key' => 'svcnum',
693 # 'unique' => [ [] ],
699 'prepaynum', 'int', '', '',
700 'identifier', 'varchar', '', $char_d,
701 'amount', @money_type,
703 'primary_key' => 'prepaynum',
704 'unique' => [ ['identifier'] ],