3 # $Id: fs-setup,v 1.24 2000-03-02 07:44:07 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.24 2000-03-02 07:44:07 ivan
36 # typo forgot closing '
38 # Revision 1.23 2000/02/03 05:16:52 ivan
39 # beginning of DNS and Apache support
41 # Revision 1.22 2000/01/31 05:22:23 ivan
42 # prepaid "internet cards"
44 # Revision 1.21 2000/01/30 06:03:26 ivan
45 # postgres 6.5 finally supports decimal(10,2)
47 # Revision 1.20 2000/01/28 22:53:33 ivan
48 # track full phone number
50 # Revision 1.19 1999/07/29 08:50:35 ivan
51 # wrong type for cust_pay_batch.exp
53 # Revision 1.18 1999/04/15 22:46:30 ivan
56 # Revision 1.17 1999/04/14 07:58:39 ivan
57 # export getsecrets from FS::UID instead of calling it explicitly
59 # Revision 1.16 1999/02/28 19:44:16 ivan
60 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
62 # Revision 1.15 1999/02/27 21:06:21 ivan
63 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
64 # by Ben Leibig <leibig@colorado.edu>
66 # Revision 1.14 1999/02/07 09:59:14 ivan
67 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
69 # Revision 1.13 1999/02/04 06:09:23 ivan
72 # Revision 1.12 1999/02/03 10:42:27 ivan
73 # *** empty log message ***
75 # Revision 1.11 1999/01/17 03:11:52 ivan
76 # remove preliminary completehost changes
78 # Revision 1.10 1998/12/16 06:05:38 ivan
79 # add table cust_main_invoice
81 # Revision 1.9 1998/12/15 04:36:29 ivan
84 # Revision 1.8 1998/12/15 04:33:27 ivan
85 # dies if it isn't running as the freeside user
87 # Revision 1.7 1998/11/18 09:01:31 ivan
90 # Revision 1.6 1998/11/15 13:18:02 ivan
93 # Revision 1.5 1998/11/15 09:43:03 ivan
94 # update for new config file syntax, new adminsuidsetup
96 # Revision 1.4 1998/10/22 15:51:23 ivan
97 # also varchar with no length specified - postgresql fix broke mysql.
99 # Revision 1.3 1998/10/22 15:46:28 ivan
100 # now smallint is illegal, so remove that too.
103 #to delay loading dbdef until we're ready
104 BEGIN { $FS::Record::setup_hack = 1; }
109 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
111 use FS::cust_main_county;
113 die "Not running uid freeside!" unless checkeuid();
115 my $user = shift or die &usage;
118 #needs to match FS::Record
119 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
123 print "\nEnter the maximum username length: ";
124 my($username_len)=&getvalue;
126 print "\n\n", <<END, ":";
127 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
128 for each user. Enter any additional RADIUS attributes you need to track for
129 each user, separated by whitespace.
131 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
134 my($x)=scalar(<STDIN>);
141 my($char_d) = 80; #default maxlength for text fields
143 #my(@date_type) = ( 'timestamp', '', '' );
144 my(@date_type) = ( 'int', 'NULL', '' );
145 my(@perl_type) = ( 'varchar', 'NULL', 255 );
146 my @money_type = ( 'decimal', '', '10,2' );
149 # create a dbdef object from the old data structure
152 my(%tables)=&tables_hash_hack;
154 #turn it into objects
155 my($dbdef) = new FS::dbdef ( map {
157 while (@{$tables{$_}{'columns'}}) {
158 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
159 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
161 FS::dbdef_table->new(
163 $tables{$_}{'primary_key'},
164 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
165 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
166 FS::dbdef_unique->new($tables{$_}{'unique'}),
167 FS::dbdef_index->new($tables{$_}{'index'}),
172 #add radius attributes to svc_acct
174 my($svc_acct)=$dbdef->table('svc_acct');
177 foreach $attribute (@attributes) {
178 $svc_acct->addcolumn ( new FS::dbdef_column (
179 'radius_'. $attribute,
186 #make part_svc table (but now as object)
188 my($part_svc)=$dbdef->table('part_svc');
190 #because of svc_acct_pop
191 #foreach (grep /^svc_/, $dbdef->tables) {
192 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
193 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
194 my($table)=$dbdef->table($_);
196 foreach $col ( $table->columns ) {
197 next if $col =~ /^svcnum$/;
198 $part_svc->addcolumn( new FS::dbdef_column (
199 $table->name. '__' . $table->column($col)->name,
200 'varchar', #$table->column($col)->type,
202 $char_d, #$table->column($col)->length,
204 $part_svc->addcolumn ( new FS::dbdef_column (
205 $table->name. '__'. $table->column($col)->name . "_flag",
214 $dbdef->save($dbdef_file);
215 &FS::Record::reload_dbdef($dbdef_file);
221 my($dbh)=adminsuidsetup $user;
227 foreach ($dbdef->tables) {
228 my($table)=$dbdef->table($_);
229 print "Creating $_...";
234 foreach $statement ($table->sql_create_table(datasrc)) {
235 #print $statement, "\n";
236 $dbh->do( $statement )
237 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
243 #not really sample data (and shouldn't default to US)
249 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
250 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
251 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
253 my($cust_main_county)=new FS::cust_main_county({
259 $error=$cust_main_county->insert;
260 die $error if $error;
263 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
265 VIC NSW NT QLD TAS ACT WA SA
267 my($cust_main_county)=new FS::cust_main_county({
273 $error=$cust_main_county->insert;
274 die $error if $error;
277 #ISO 2-letter country codes (same as country TLDs) except US and AU
279 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
280 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
281 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
282 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
283 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
284 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
285 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
286 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
287 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
290 my($cust_main_county)=new FS::cust_main_county({
295 $error=$cust_main_county->insert;
296 die $error if $error;
299 $dbh->disconnect or die $dbh->errstr;
302 die "Usage:\n fs-setup user\n";
306 # Now it becomes an object. much better.
308 sub tables_hash_hack {
310 #note that s/(date|change)/_$1/; to avoid keyword conflict.
311 #put a kludge in FS::Record to catch this or? (pry need some date-handling
314 my(%tables)=( #yech.}
318 'agentnum', 'int', '', '',
319 'agent', 'varchar', '', $char_d,
320 'typenum', 'int', '', '',
321 'freq', 'int', 'NULL', '',
324 'primary_key' => 'agentnum',
326 'index' => [ ['typenum'] ],
331 'typenum', 'int', '', '',
332 'atype', 'varchar', '', $char_d,
334 'primary_key' => 'typenum',
341 'typenum', 'int', '', '',
342 'pkgpart', 'int', '', '',
345 'unique' => [ ['typenum', 'pkgpart'] ],
346 'index' => [ ['typenum'] ],
351 'invnum', 'int', '', '',
352 'custnum', 'int', '', '',
354 'charged', @money_type,
356 'printed', 'int', '', '',
358 'primary_key' => 'invnum',
360 'index' => [ ['custnum'] ],
365 'pkgnum', 'int', '', '',
366 'invnum', 'int', '', '',
367 'setup', @money_type,
368 'recur', @money_type,
373 'unique' => [ ['pkgnum', 'invnum'] ],
374 'index' => [ ['invnum'] ],
379 'crednum', 'int', '', '',
380 'custnum', 'int', '', '',
382 'amount', @money_type,
383 'credited', @money_type,
384 'otaker', 'varchar', '', 8,
385 'reason', 'varchar', '', 255,
387 'primary_key' => 'crednum',
389 'index' => [ ['custnum'] ],
394 'custnum', 'int', '', '',
395 'agentnum', 'int', '', '',
396 # 'titlenum', 'int', 'NULL', '',
397 'last', 'varchar', '', $char_d,
398 # 'middle', 'varchar', 'NULL', $char_d,
399 'first', 'varchar', '', $char_d,
400 'ss', 'char', 'NULL', 11,
401 'company', 'varchar', 'NULL', $char_d,
402 'address1', 'varchar', '', $char_d,
403 'address2', 'varchar', 'NULL', $char_d,
404 'city', 'varchar', '', $char_d,
405 'county', 'varchar', 'NULL', $char_d,
406 'state', 'varchar', 'NULL', $char_d,
407 'zip', 'varchar', '', 10,
408 'country', 'char', '', 2,
409 'daytime', 'varchar', 'NULL', 20,
410 'night', 'varchar', 'NULL', 20,
411 'fax', 'varchar', 'NULL', 12,
412 'payby', 'char', '', 4,
413 'payinfo', 'varchar', 'NULL', 16,
414 #'paydate', @date_type,
415 'paydate', 'varchar', 'NULL', 10,
416 'payname', 'varchar', 'NULL', $char_d,
417 'tax', 'char', 'NULL', 1,
418 'otaker', 'varchar', '', 8,
419 'refnum', 'int', '', '',
421 'primary_key' => 'custnum',
423 #'index' => [ ['last'], ['company'] ],
424 'index' => [ ['last'], ],
427 'cust_main_invoice' => {
429 'destnum', 'int', '', '',
430 'custnum', 'int', '', '',
431 'dest', 'varchar', '', $char_d,
433 'primary_key' => 'destnum',
435 'index' => [ ['custnum'], ],
438 'cust_main_county' => { #county+state+country are checked off the
439 #cust_main_county for validation and to provide
442 'taxnum', 'int', '', '',
443 'state', 'varchar', 'NULL', $char_d,
444 'county', 'varchar', 'NULL', $char_d,
445 'country', 'char', '', 2,
446 'tax', 'real', '', '', #tax %
448 'primary_key' => 'taxnum',
450 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
456 'paynum', 'int', '', '',
457 'invnum', 'int', '', '',
460 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
461 # payment type table.
462 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
463 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
465 'primary_key' => 'paynum',
467 'index' => [ ['invnum'] ],
470 'cust_pay_batch' => { #what's this used for again? list of customers
471 #in current CARD batch? (necessarily CARD?)
473 'invnum', 'int', '', '',
474 'custnum', 'int', '', '',
475 'last', 'varchar', '', $char_d,
476 'first', 'varchar', '', $char_d,
477 'address1', 'varchar', '', $char_d,
478 'address2', 'varchar', 'NULL', $char_d,
479 'city', 'varchar', '', $char_d,
480 'state', 'varchar', '', $char_d,
481 'zip', 'varchar', '', 10,
482 'country', 'char', '', 2,
483 'trancode', 'int', '', '',
484 'cardnum', 'varchar', '', 16,
486 'exp', 'varchar', '', 11,
487 'payname', 'varchar', 'NULL', $char_d,
488 'amount', @money_type,
492 'index' => [ ['invnum'], ['custnum'] ],
497 'pkgnum', 'int', '', '',
498 'custnum', 'int', '', '',
499 'pkgpart', 'int', '', '',
500 'otaker', 'varchar', '', 8,
504 'cancel', @date_type,
505 'expire', @date_type,
507 'primary_key' => 'pkgnum',
509 'index' => [ ['custnum'] ],
514 'refundnum', 'int', '', '',
515 'crednum', 'int', '', '',
517 'refund', @money_type,
518 'otaker', 'varchar', '', 8,
519 'reason', 'varchar', '', $char_d,
520 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
521 # into payment type table.
522 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
524 'primary_key' => 'refundnum',
526 'index' => [ ['crednum'] ],
531 'svcnum', 'int', '', '',
532 'pkgnum', 'int', '', '',
533 'svcpart', 'int', '', '',
535 'primary_key' => 'svcnum',
537 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
542 'pkgpart', 'int', '', '',
543 'pkg', 'varchar', '', $char_d,
544 'comment', 'varchar', '', $char_d,
546 'freq', 'int', '', '', #billing frequency (months)
549 'primary_key' => 'pkgpart',
556 # 'titlenum', 'int', '', '',
557 # 'title', 'varchar', '', $char_d,
559 # 'primary_key' => 'titlenum',
560 # 'unique' => [ [] ],
566 'pkgpart', 'int', '', '',
567 'svcpart', 'int', '', '',
568 'quantity', 'int', '', '',
571 'unique' => [ ['pkgpart', 'svcpart'] ],
572 'index' => [ ['pkgpart'] ],
577 'refnum', 'int', '', '',
578 'referral', 'varchar', '', $char_d,
580 'primary_key' => 'refnum',
587 'svcpart', 'int', '', '',
588 'svc', 'varchar', '', $char_d,
589 'svcdb', 'varchar', '', $char_d,
591 'primary_key' => 'svcpart',
596 #(this should be renamed to part_pop)
599 'popnum', 'int', '', '',
600 'city', 'varchar', '', $char_d,
601 'state', 'varchar', '', $char_d,
603 'exch', 'char', '', 3,
604 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
606 'primary_key' => 'popnum',
613 'svcnum', 'int', '', '',
614 'username', 'varchar', '', $username_len, #unique (& remove dup code)
615 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
616 'popnum', 'int', 'NULL', '',
617 'uid', 'int', 'NULL', '',
618 'gid', 'int', 'NULL', '',
619 'finger', 'varchar', 'NULL', $char_d,
620 'dir', 'varchar', 'NULL', $char_d,
621 'shell', 'varchar', 'NULL', $char_d,
622 'quota', 'varchar', 'NULL', $char_d,
623 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
625 'primary_key' => 'svcnum',
627 'index' => [ ['username'] ],
632 'svcnum', 'int', '', '',
633 'domsvc', 'int', '', '',
634 'domuid', 'int', '', '',
635 'domuser', 'varchar', '', $char_d,
637 'primary_key' => 'svcnum',
639 'index' => [ ['domsvc'], ['domuid'] ],
644 # 'svcnum', 'int', '', '',
645 # 'amount', @money_type,
647 # 'primary_key' => 'svcnum',
648 # 'unique' => [ [] ],
654 'svcnum', 'int', '', '',
655 'domain', 'varchar', '', $char_d,
657 'primary_key' => 'svcnum',
658 'unique' => [ ['domain'] ],
664 'recnum', 'int', '', '',
665 'svcnum', 'int', '', '',
666 'reczone', 'varchar', '', $char_d,
667 'recaf', 'char', '', 2,
668 'rectype', 'char', '', 5,
669 'recdata', 'varchar', '', $char_d,
671 'primary_key' => 'recnum',
673 'index' => [ ['svcnum'] ],
678 'svcnum', 'int', '', '',
679 'recnum', 'int', '', '',
680 'usersvc', 'int', '', '',
682 'primary_key' => 'svcnum',
689 # 'svcnum', 'int', '', '',
690 # 'svcnum', 'int', '', '',
691 # 'svcnum', 'int', '', '',
692 # 'worker', 'varchar', '', $char_d,
693 # '_date', @date_type,
695 # 'primary_key' => 'svcnum',
696 # 'unique' => [ [] ],
702 'prepaynum', 'int', '', '',
703 'identifier', 'varchar', '', $char_d,
704 'amount', @money_type,
706 'primary_key' => 'prepaynum',
707 'unique' => [ ['identifier'] ],