3 # $Id: fs-setup,v 1.20 2000-01-28 22:53:33 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.20 2000-01-28 22:53:33 ivan
36 # track full phone number
38 # Revision 1.19 1999/07/29 08:50:35 ivan
39 # wrong type for cust_pay_batch.exp
41 # Revision 1.18 1999/04/15 22:46:30 ivan
44 # Revision 1.17 1999/04/14 07:58:39 ivan
45 # export getsecrets from FS::UID instead of calling it explicitly
47 # Revision 1.16 1999/02/28 19:44:16 ivan
48 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
50 # Revision 1.15 1999/02/27 21:06:21 ivan
51 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
52 # by Ben Leibig <leibig@colorado.edu>
54 # Revision 1.14 1999/02/07 09:59:14 ivan
55 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
57 # Revision 1.13 1999/02/04 06:09:23 ivan
60 # Revision 1.12 1999/02/03 10:42:27 ivan
61 # *** empty log message ***
63 # Revision 1.11 1999/01/17 03:11:52 ivan
64 # remove preliminary completehost changes
66 # Revision 1.10 1998/12/16 06:05:38 ivan
67 # add table cust_main_invoice
69 # Revision 1.9 1998/12/15 04:36:29 ivan
72 # Revision 1.8 1998/12/15 04:33:27 ivan
73 # dies if it isn't running as the freeside user
75 # Revision 1.7 1998/11/18 09:01:31 ivan
78 # Revision 1.6 1998/11/15 13:18:02 ivan
81 # Revision 1.5 1998/11/15 09:43:03 ivan
82 # update for new config file syntax, new adminsuidsetup
84 # Revision 1.4 1998/10/22 15:51:23 ivan
85 # also varchar with no length specified - postgresql fix broke mysql.
87 # Revision 1.3 1998/10/22 15:46:28 ivan
88 # now smallint is illegal, so remove that too.
91 #to delay loading dbdef until we're ready
92 BEGIN { $FS::Record::setup_hack = 1; }
97 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
99 use FS::cust_main_county;
101 die "Not running uid freeside!" unless checkeuid();
103 my $user = shift or die &usage;
106 #needs to match FS::Record
107 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
111 print "\nEnter the maximum username length: ";
112 my($username_len)=&getvalue;
114 print "\n\n", <<END, ":";
115 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
116 for each user. Enter any additional RADIUS attributes you need to track for
117 each user, separated by whitespace.
119 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
122 my($x)=scalar(<STDIN>);
129 my($char_d) = 80; #default maxlength for text fields
131 #my(@date_type) = ( 'timestamp', '', '' );
132 my(@date_type) = ( 'int', 'NULL', '' );
133 my(@perl_type) = ( 'varchar', 'NULL', 255 );
135 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
136 @money_type = ( 'money', '', '' );
138 @money_type = ( 'decimal', '', '10,2' );
142 # create a dbdef object from the old data structure
145 my(%tables)=&tables_hash_hack;
147 #turn it into objects
148 my($dbdef) = new FS::dbdef ( map {
150 while (@{$tables{$_}{'columns'}}) {
151 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
152 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
154 FS::dbdef_table->new(
156 $tables{$_}{'primary_key'},
157 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
158 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
159 FS::dbdef_unique->new($tables{$_}{'unique'}),
160 FS::dbdef_index->new($tables{$_}{'index'}),
165 #add radius attributes to svc_acct
167 my($svc_acct)=$dbdef->table('svc_acct');
170 foreach $attribute (@attributes) {
171 $svc_acct->addcolumn ( new FS::dbdef_column (
172 'radius_'. $attribute,
179 #make part_svc table (but now as object)
181 my($part_svc)=$dbdef->table('part_svc');
183 #because of svc_acct_pop
184 #foreach (grep /^svc_/, $dbdef->tables) {
185 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
186 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
187 my($table)=$dbdef->table($_);
189 foreach $col ( $table->columns ) {
190 next if $col =~ /^svcnum$/;
191 $part_svc->addcolumn( new FS::dbdef_column (
192 $table->name. '__' . $table->column($col)->name,
193 'varchar', #$table->column($col)->type,
195 $char_d, #$table->column($col)->length,
197 $part_svc->addcolumn ( new FS::dbdef_column (
198 $table->name. '__'. $table->column($col)->name . "_flag",
207 $dbdef->save($dbdef_file);
208 &FS::Record::reload_dbdef($dbdef_file);
214 my($dbh)=adminsuidsetup $user;
220 foreach ($dbdef->tables) {
221 my($table)=$dbdef->table($_);
222 print "Creating $_...";
227 foreach $statement ($table->sql_create_table(datasrc)) {
228 #print $statement, "\n";
229 $dbh->do( $statement )
230 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
236 #not really sample data (and shouldn't default to US)
242 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
243 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
244 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
246 my($cust_main_county)=new FS::cust_main_county({
252 $error=$cust_main_county->insert;
253 die $error if $error;
256 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
258 VIC NSW NT QLD TAS ACT WA SA
260 my($cust_main_county)=new FS::cust_main_county({
266 $error=$cust_main_county->insert;
267 die $error if $error;
270 #ISO 2-letter country codes (same as country TLDs) except US and AU
272 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
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 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
283 my($cust_main_county)=new FS::cust_main_county({
288 $error=$cust_main_county->insert;
289 die $error if $error;
292 $dbh->disconnect or die $dbh->errstr;
295 die "Usage:\n fs-setup user\n";
299 # Now it becomes an object. much better.
301 sub tables_hash_hack {
303 #note that s/(date|change)/_$1/; to avoid keyword conflict.
304 #put a kludge in FS::Record to catch this or? (pry need some date-handling
307 my(%tables)=( #yech.}
311 'agentnum', 'int', '', '',
312 'agent', 'varchar', '', $char_d,
313 'typenum', 'int', '', '',
314 'freq', 'int', 'NULL', '',
317 'primary_key' => 'agentnum',
319 'index' => [ ['typenum'] ],
324 'typenum', 'int', '', '',
325 'atype', 'varchar', '', $char_d,
327 'primary_key' => 'typenum',
334 'typenum', 'int', '', '',
335 'pkgpart', 'int', '', '',
338 'unique' => [ ['typenum', 'pkgpart'] ],
339 'index' => [ ['typenum'] ],
344 'invnum', 'int', '', '',
345 'custnum', 'int', '', '',
347 'charged', @money_type,
349 'printed', 'int', '', '',
351 'primary_key' => 'invnum',
353 'index' => [ ['custnum'] ],
358 'pkgnum', 'int', '', '',
359 'invnum', 'int', '', '',
360 'setup', @money_type,
361 'recur', @money_type,
366 'unique' => [ ['pkgnum', 'invnum'] ],
367 'index' => [ ['invnum'] ],
372 'crednum', 'int', '', '',
373 'custnum', 'int', '', '',
375 'amount', @money_type,
376 'credited', @money_type,
377 'otaker', 'varchar', '', 8,
378 'reason', 'varchar', '', 255,
380 'primary_key' => 'crednum',
382 'index' => [ ['custnum'] ],
387 'custnum', 'int', '', '',
388 'agentnum', 'int', '', '',
389 # 'titlenum', 'int', 'NULL', '',
390 'last', 'varchar', '', $char_d,
391 # 'middle', 'varchar', 'NULL', $char_d,
392 'first', 'varchar', '', $char_d,
393 'ss', 'char', 'NULL', 11,
394 'company', 'varchar', 'NULL', $char_d,
395 'address1', 'varchar', '', $char_d,
396 'address2', 'varchar', 'NULL', $char_d,
397 'city', 'varchar', '', $char_d,
398 'county', 'varchar', 'NULL', $char_d,
399 'state', 'varchar', 'NULL', $char_d,
400 'zip', 'varchar', '', 10,
401 'country', 'char', '', 2,
402 'daytime', 'varchar', 'NULL', 20,
403 'night', 'varchar', 'NULL', 20,
404 'fax', 'varchar', 'NULL', 12,
405 'payby', 'char', '', 4,
406 'payinfo', 'varchar', 'NULL', 16,
407 #'paydate', @date_type,
408 'paydate', 'varchar', 'NULL', 10,
409 'payname', 'varchar', 'NULL', $char_d,
410 'tax', 'char', 'NULL', 1,
411 'otaker', 'varchar', '', 8,
412 'refnum', 'int', '', '',
414 'primary_key' => 'custnum',
416 #'index' => [ ['last'], ['company'] ],
417 'index' => [ ['last'], ],
420 'cust_main_invoice' => {
422 'destnum', 'int', '', '',
423 'custnum', 'int', '', '',
424 'dest', 'varchar', '', $char_d,
426 'primary_key' => 'destnum',
428 'index' => [ ['custnum'], ],
431 'cust_main_county' => { #county+state+country are checked off the
432 #cust_main_county for validation and to provide
435 'taxnum', 'int', '', '',
436 'state', 'varchar', 'NULL', $char_d,
437 'county', 'varchar', 'NULL', $char_d,
438 'country', 'char', '', 2,
439 'tax', 'real', '', '', #tax %
441 'primary_key' => 'taxnum',
443 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
449 'paynum', 'int', '', '',
450 'invnum', 'int', '', '',
453 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
454 # payment type table.
455 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
456 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
458 'primary_key' => 'paynum',
460 'index' => [ ['invnum'] ],
463 'cust_pay_batch' => { #what's this used for again? list of customers
464 #in current CARD batch? (necessarily CARD?)
466 'invnum', 'int', '', '',
467 'custnum', 'int', '', '',
468 'last', 'varchar', '', $char_d,
469 'first', 'varchar', '', $char_d,
470 'address1', 'varchar', '', $char_d,
471 'address2', 'varchar', 'NULL', $char_d,
472 'city', 'varchar', '', $char_d,
473 'state', 'varchar', '', $char_d,
474 'zip', 'varchar', '', 10,
475 'country', 'char', '', 2,
476 'trancode', 'int', '', '',
477 'cardnum', 'varchar', '', 16,
479 'exp', 'varchar', '', 11,
480 'payname', 'varchar', 'NULL', $char_d,
481 'amount', @money_type,
485 'index' => [ ['invnum'], ['custnum'] ],
490 'pkgnum', 'int', '', '',
491 'custnum', 'int', '', '',
492 'pkgpart', 'int', '', '',
493 'otaker', 'varchar', '', 8,
497 'cancel', @date_type,
498 'expire', @date_type,
500 'primary_key' => 'pkgnum',
502 'index' => [ ['custnum'] ],
507 'refundnum', 'int', '', '',
508 'crednum', 'int', '', '',
510 'refund', @money_type,
511 'otaker', 'varchar', '', 8,
512 'reason', 'varchar', '', $char_d,
513 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
514 # into payment type table.
515 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
517 'primary_key' => 'refundnum',
519 'index' => [ ['crednum'] ],
524 'svcnum', 'int', '', '',
525 'pkgnum', 'int', '', '',
526 'svcpart', 'int', '', '',
528 'primary_key' => 'svcnum',
530 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
535 'pkgpart', 'int', '', '',
536 'pkg', 'varchar', '', $char_d,
537 'comment', 'varchar', '', $char_d,
539 'freq', 'int', '', '', #billing frequency (months)
542 'primary_key' => 'pkgpart',
549 # 'titlenum', 'int', '', '',
550 # 'title', 'varchar', '', $char_d,
552 # 'primary_key' => 'titlenum',
553 # 'unique' => [ [] ],
559 'pkgpart', 'int', '', '',
560 'svcpart', 'int', '', '',
561 'quantity', 'int', '', '',
564 'unique' => [ ['pkgpart', 'svcpart'] ],
565 'index' => [ ['pkgpart'] ],
570 'refnum', 'int', '', '',
571 'referral', 'varchar', '', $char_d,
573 'primary_key' => 'refnum',
580 'svcpart', 'int', '', '',
581 'svc', 'varchar', '', $char_d,
582 'svcdb', 'varchar', '', $char_d,
584 'primary_key' => 'svcpart',
589 #(this should be renamed to part_pop)
592 'popnum', 'int', '', '',
593 'city', 'varchar', '', $char_d,
594 'state', 'varchar', '', $char_d,
596 'exch', 'char', '', 3,
597 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
599 'primary_key' => 'popnum',
606 'svcnum', 'int', '', '',
607 'username', 'varchar', '', $username_len, #unique (& remove dup code)
608 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
609 'popnum', 'int', 'NULL', '',
610 'uid', 'int', 'NULL', '',
611 'gid', 'int', 'NULL', '',
612 'finger', 'varchar', 'NULL', $char_d,
613 'dir', 'varchar', 'NULL', $char_d,
614 'shell', 'varchar', 'NULL', $char_d,
615 'quota', 'varchar', 'NULL', $char_d,
616 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
618 'primary_key' => 'svcnum',
620 'index' => [ ['username'] ],
625 'svcnum', 'int', '', '',
626 'domsvc', 'int', '', '',
627 'domuid', 'int', '', '',
628 'domuser', 'varchar', '', $char_d,
630 'primary_key' => 'svcnum',
632 'index' => [ ['domsvc'], ['domuid'] ],
637 # 'svcnum', 'int', '', '',
638 # 'amount', @money_type,
640 # 'primary_key' => 'svcnum',
641 # 'unique' => [ [] ],
647 'svcnum', 'int', '', '',
648 'domain', 'varchar', '', $char_d,
650 'primary_key' => 'svcnum',
651 'unique' => [ ['domain'] ],
657 # 'svcnum', 'int', '', '',
658 # 'svcnum', 'int', '', '',
659 # 'svcnum', 'int', '', '',
660 # 'worker', 'varchar', '', $char_d,
661 # '_date', @date_type,
663 # 'primary_key' => 'svcnum',
664 # 'unique' => [ [] ],