3 # $Id: fs-setup,v 1.21 2000-01-30 06:03:26 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.21 2000-01-30 06:03:26 ivan
36 # postgres 6.5 finally supports decimal(10,2)
38 # Revision 1.20 2000/01/28 22:53:33 ivan
39 # track full phone number
41 # Revision 1.19 1999/07/29 08:50:35 ivan
42 # wrong type for cust_pay_batch.exp
44 # Revision 1.18 1999/04/15 22:46:30 ivan
47 # Revision 1.17 1999/04/14 07:58:39 ivan
48 # export getsecrets from FS::UID instead of calling it explicitly
50 # Revision 1.16 1999/02/28 19:44:16 ivan
51 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
53 # Revision 1.15 1999/02/27 21:06:21 ivan
54 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
55 # by Ben Leibig <leibig@colorado.edu>
57 # Revision 1.14 1999/02/07 09:59:14 ivan
58 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
60 # Revision 1.13 1999/02/04 06:09:23 ivan
63 # Revision 1.12 1999/02/03 10:42:27 ivan
64 # *** empty log message ***
66 # Revision 1.11 1999/01/17 03:11:52 ivan
67 # remove preliminary completehost changes
69 # Revision 1.10 1998/12/16 06:05:38 ivan
70 # add table cust_main_invoice
72 # Revision 1.9 1998/12/15 04:36:29 ivan
75 # Revision 1.8 1998/12/15 04:33:27 ivan
76 # dies if it isn't running as the freeside user
78 # Revision 1.7 1998/11/18 09:01:31 ivan
81 # Revision 1.6 1998/11/15 13:18:02 ivan
84 # Revision 1.5 1998/11/15 09:43:03 ivan
85 # update for new config file syntax, new adminsuidsetup
87 # Revision 1.4 1998/10/22 15:51:23 ivan
88 # also varchar with no length specified - postgresql fix broke mysql.
90 # Revision 1.3 1998/10/22 15:46:28 ivan
91 # now smallint is illegal, so remove that too.
94 #to delay loading dbdef until we're ready
95 BEGIN { $FS::Record::setup_hack = 1; }
100 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
102 use FS::cust_main_county;
104 die "Not running uid freeside!" unless checkeuid();
106 my $user = shift or die &usage;
109 #needs to match FS::Record
110 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
114 print "\nEnter the maximum username length: ";
115 my($username_len)=&getvalue;
117 print "\n\n", <<END, ":";
118 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
119 for each user. Enter any additional RADIUS attributes you need to track for
120 each user, separated by whitespace.
122 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
125 my($x)=scalar(<STDIN>);
132 my($char_d) = 80; #default maxlength for text fields
134 #my(@date_type) = ( 'timestamp', '', '' );
135 my(@date_type) = ( 'int', 'NULL', '' );
136 my(@perl_type) = ( 'varchar', 'NULL', 255 );
137 my @money_type = ( 'decimal', '', '10,2' );
140 # create a dbdef object from the old data structure
143 my(%tables)=&tables_hash_hack;
145 #turn it into objects
146 my($dbdef) = new FS::dbdef ( map {
148 while (@{$tables{$_}{'columns'}}) {
149 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
150 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
152 FS::dbdef_table->new(
154 $tables{$_}{'primary_key'},
155 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
156 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
157 FS::dbdef_unique->new($tables{$_}{'unique'}),
158 FS::dbdef_index->new($tables{$_}{'index'}),
163 #add radius attributes to svc_acct
165 my($svc_acct)=$dbdef->table('svc_acct');
168 foreach $attribute (@attributes) {
169 $svc_acct->addcolumn ( new FS::dbdef_column (
170 'radius_'. $attribute,
177 #make part_svc table (but now as object)
179 my($part_svc)=$dbdef->table('part_svc');
181 #because of svc_acct_pop
182 #foreach (grep /^svc_/, $dbdef->tables) {
183 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
184 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
185 my($table)=$dbdef->table($_);
187 foreach $col ( $table->columns ) {
188 next if $col =~ /^svcnum$/;
189 $part_svc->addcolumn( new FS::dbdef_column (
190 $table->name. '__' . $table->column($col)->name,
191 'varchar', #$table->column($col)->type,
193 $char_d, #$table->column($col)->length,
195 $part_svc->addcolumn ( new FS::dbdef_column (
196 $table->name. '__'. $table->column($col)->name . "_flag",
205 $dbdef->save($dbdef_file);
206 &FS::Record::reload_dbdef($dbdef_file);
212 my($dbh)=adminsuidsetup $user;
218 foreach ($dbdef->tables) {
219 my($table)=$dbdef->table($_);
220 print "Creating $_...";
225 foreach $statement ($table->sql_create_table(datasrc)) {
226 #print $statement, "\n";
227 $dbh->do( $statement )
228 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
234 #not really sample data (and shouldn't default to US)
240 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
241 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
242 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
244 my($cust_main_county)=new FS::cust_main_county({
250 $error=$cust_main_county->insert;
251 die $error if $error;
254 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
256 VIC NSW NT QLD TAS ACT WA SA
258 my($cust_main_county)=new FS::cust_main_county({
264 $error=$cust_main_county->insert;
265 die $error if $error;
268 #ISO 2-letter country codes (same as country TLDs) except US and AU
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
278 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
281 my($cust_main_county)=new FS::cust_main_county({
286 $error=$cust_main_county->insert;
287 die $error if $error;
290 $dbh->disconnect or die $dbh->errstr;
293 die "Usage:\n fs-setup user\n";
297 # Now it becomes an object. much better.
299 sub tables_hash_hack {
301 #note that s/(date|change)/_$1/; to avoid keyword conflict.
302 #put a kludge in FS::Record to catch this or? (pry need some date-handling
305 my(%tables)=( #yech.}
309 'agentnum', 'int', '', '',
310 'agent', 'varchar', '', $char_d,
311 'typenum', 'int', '', '',
312 'freq', 'int', 'NULL', '',
315 'primary_key' => 'agentnum',
317 'index' => [ ['typenum'] ],
322 'typenum', 'int', '', '',
323 'atype', 'varchar', '', $char_d,
325 'primary_key' => 'typenum',
332 'typenum', 'int', '', '',
333 'pkgpart', 'int', '', '',
336 'unique' => [ ['typenum', 'pkgpart'] ],
337 'index' => [ ['typenum'] ],
342 'invnum', 'int', '', '',
343 'custnum', 'int', '', '',
345 'charged', @money_type,
347 'printed', 'int', '', '',
349 'primary_key' => 'invnum',
351 'index' => [ ['custnum'] ],
356 'pkgnum', 'int', '', '',
357 'invnum', 'int', '', '',
358 'setup', @money_type,
359 'recur', @money_type,
364 'unique' => [ ['pkgnum', 'invnum'] ],
365 'index' => [ ['invnum'] ],
370 'crednum', 'int', '', '',
371 'custnum', 'int', '', '',
373 'amount', @money_type,
374 'credited', @money_type,
375 'otaker', 'varchar', '', 8,
376 'reason', 'varchar', '', 255,
378 'primary_key' => 'crednum',
380 'index' => [ ['custnum'] ],
385 'custnum', 'int', '', '',
386 'agentnum', 'int', '', '',
387 # 'titlenum', 'int', 'NULL', '',
388 'last', 'varchar', '', $char_d,
389 # 'middle', 'varchar', 'NULL', $char_d,
390 'first', 'varchar', '', $char_d,
391 'ss', 'char', 'NULL', 11,
392 'company', 'varchar', 'NULL', $char_d,
393 'address1', 'varchar', '', $char_d,
394 'address2', 'varchar', 'NULL', $char_d,
395 'city', 'varchar', '', $char_d,
396 'county', 'varchar', 'NULL', $char_d,
397 'state', 'varchar', 'NULL', $char_d,
398 'zip', 'varchar', '', 10,
399 'country', 'char', '', 2,
400 'daytime', 'varchar', 'NULL', 20,
401 'night', 'varchar', 'NULL', 20,
402 'fax', 'varchar', 'NULL', 12,
403 'payby', 'char', '', 4,
404 'payinfo', 'varchar', 'NULL', 16,
405 #'paydate', @date_type,
406 'paydate', 'varchar', 'NULL', 10,
407 'payname', 'varchar', 'NULL', $char_d,
408 'tax', 'char', 'NULL', 1,
409 'otaker', 'varchar', '', 8,
410 'refnum', 'int', '', '',
412 'primary_key' => 'custnum',
414 #'index' => [ ['last'], ['company'] ],
415 'index' => [ ['last'], ],
418 'cust_main_invoice' => {
420 'destnum', 'int', '', '',
421 'custnum', 'int', '', '',
422 'dest', 'varchar', '', $char_d,
424 'primary_key' => 'destnum',
426 'index' => [ ['custnum'], ],
429 'cust_main_county' => { #county+state+country are checked off the
430 #cust_main_county for validation and to provide
433 'taxnum', 'int', '', '',
434 'state', 'varchar', 'NULL', $char_d,
435 'county', 'varchar', 'NULL', $char_d,
436 'country', 'char', '', 2,
437 'tax', 'real', '', '', #tax %
439 'primary_key' => 'taxnum',
441 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
447 'paynum', 'int', '', '',
448 'invnum', 'int', '', '',
451 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
452 # payment type table.
453 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
454 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
456 'primary_key' => 'paynum',
458 'index' => [ ['invnum'] ],
461 'cust_pay_batch' => { #what's this used for again? list of customers
462 #in current CARD batch? (necessarily CARD?)
464 'invnum', 'int', '', '',
465 'custnum', 'int', '', '',
466 'last', 'varchar', '', $char_d,
467 'first', 'varchar', '', $char_d,
468 'address1', 'varchar', '', $char_d,
469 'address2', 'varchar', 'NULL', $char_d,
470 'city', 'varchar', '', $char_d,
471 'state', 'varchar', '', $char_d,
472 'zip', 'varchar', '', 10,
473 'country', 'char', '', 2,
474 'trancode', 'int', '', '',
475 'cardnum', 'varchar', '', 16,
477 'exp', 'varchar', '', 11,
478 'payname', 'varchar', 'NULL', $char_d,
479 'amount', @money_type,
483 'index' => [ ['invnum'], ['custnum'] ],
488 'pkgnum', 'int', '', '',
489 'custnum', 'int', '', '',
490 'pkgpart', 'int', '', '',
491 'otaker', 'varchar', '', 8,
495 'cancel', @date_type,
496 'expire', @date_type,
498 'primary_key' => 'pkgnum',
500 'index' => [ ['custnum'] ],
505 'refundnum', 'int', '', '',
506 'crednum', 'int', '', '',
508 'refund', @money_type,
509 'otaker', 'varchar', '', 8,
510 'reason', 'varchar', '', $char_d,
511 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
512 # into payment type table.
513 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
515 'primary_key' => 'refundnum',
517 'index' => [ ['crednum'] ],
522 'svcnum', 'int', '', '',
523 'pkgnum', 'int', '', '',
524 'svcpart', 'int', '', '',
526 'primary_key' => 'svcnum',
528 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
533 'pkgpart', 'int', '', '',
534 'pkg', 'varchar', '', $char_d,
535 'comment', 'varchar', '', $char_d,
537 'freq', 'int', '', '', #billing frequency (months)
540 'primary_key' => 'pkgpart',
547 # 'titlenum', 'int', '', '',
548 # 'title', 'varchar', '', $char_d,
550 # 'primary_key' => 'titlenum',
551 # 'unique' => [ [] ],
557 'pkgpart', 'int', '', '',
558 'svcpart', 'int', '', '',
559 'quantity', 'int', '', '',
562 'unique' => [ ['pkgpart', 'svcpart'] ],
563 'index' => [ ['pkgpart'] ],
568 'refnum', 'int', '', '',
569 'referral', 'varchar', '', $char_d,
571 'primary_key' => 'refnum',
578 'svcpart', 'int', '', '',
579 'svc', 'varchar', '', $char_d,
580 'svcdb', 'varchar', '', $char_d,
582 'primary_key' => 'svcpart',
587 #(this should be renamed to part_pop)
590 'popnum', 'int', '', '',
591 'city', 'varchar', '', $char_d,
592 'state', 'varchar', '', $char_d,
594 'exch', 'char', '', 3,
595 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
597 'primary_key' => 'popnum',
604 'svcnum', 'int', '', '',
605 'username', 'varchar', '', $username_len, #unique (& remove dup code)
606 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
607 'popnum', 'int', 'NULL', '',
608 'uid', 'int', 'NULL', '',
609 'gid', 'int', 'NULL', '',
610 'finger', 'varchar', 'NULL', $char_d,
611 'dir', 'varchar', 'NULL', $char_d,
612 'shell', 'varchar', 'NULL', $char_d,
613 'quota', 'varchar', 'NULL', $char_d,
614 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
616 'primary_key' => 'svcnum',
618 'index' => [ ['username'] ],
623 'svcnum', 'int', '', '',
624 'domsvc', 'int', '', '',
625 'domuid', 'int', '', '',
626 'domuser', 'varchar', '', $char_d,
628 'primary_key' => 'svcnum',
630 'index' => [ ['domsvc'], ['domuid'] ],
635 # 'svcnum', 'int', '', '',
636 # 'amount', @money_type,
638 # 'primary_key' => 'svcnum',
639 # 'unique' => [ [] ],
645 'svcnum', 'int', '', '',
646 'domain', 'varchar', '', $char_d,
648 'primary_key' => 'svcnum',
649 'unique' => [ ['domain'] ],
655 # 'svcnum', 'int', '', '',
656 # 'svcnum', 'int', '', '',
657 # 'svcnum', 'int', '', '',
658 # 'worker', 'varchar', '', $char_d,
659 # '_date', @date_type,
661 # 'primary_key' => 'svcnum',
662 # 'unique' => [ [] ],