3 # $Id: fs-setup,v 1.19 1999-07-29 08:50:35 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.19 1999-07-29 08:50:35 ivan
36 # wrong type for cust_pay_batch.exp
38 # Revision 1.18 1999/04/15 22:46:30 ivan
41 # Revision 1.17 1999/04/14 07:58:39 ivan
42 # export getsecrets from FS::UID instead of calling it explicitly
44 # Revision 1.16 1999/02/28 19:44:16 ivan
45 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
47 # Revision 1.15 1999/02/27 21:06:21 ivan
48 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
49 # by Ben Leibig <leibig@colorado.edu>
51 # Revision 1.14 1999/02/07 09:59:14 ivan
52 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
54 # Revision 1.13 1999/02/04 06:09:23 ivan
57 # Revision 1.12 1999/02/03 10:42:27 ivan
58 # *** empty log message ***
60 # Revision 1.11 1999/01/17 03:11:52 ivan
61 # remove preliminary completehost changes
63 # Revision 1.10 1998/12/16 06:05:38 ivan
64 # add table cust_main_invoice
66 # Revision 1.9 1998/12/15 04:36:29 ivan
69 # Revision 1.8 1998/12/15 04:33:27 ivan
70 # dies if it isn't running as the freeside user
72 # Revision 1.7 1998/11/18 09:01:31 ivan
75 # Revision 1.6 1998/11/15 13:18:02 ivan
78 # Revision 1.5 1998/11/15 09:43:03 ivan
79 # update for new config file syntax, new adminsuidsetup
81 # Revision 1.4 1998/10/22 15:51:23 ivan
82 # also varchar with no length specified - postgresql fix broke mysql.
84 # Revision 1.3 1998/10/22 15:46:28 ivan
85 # now smallint is illegal, so remove that too.
88 #to delay loading dbdef until we're ready
89 BEGIN { $FS::Record::setup_hack = 1; }
94 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
96 use FS::cust_main_county;
98 die "Not running uid freeside!" unless checkeuid();
100 my $user = shift or die &usage;
103 #needs to match FS::Record
104 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
108 print "\nEnter the maximum username length: ";
109 my($username_len)=&getvalue;
111 print "\n\n", <<END, ":";
112 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
113 for each user. Enter any additional RADIUS attributes you need to track for
114 each user, separated by whitespace.
116 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
119 my($x)=scalar(<STDIN>);
126 my($char_d) = 80; #default maxlength for text fields
128 #my(@date_type) = ( 'timestamp', '', '' );
129 my(@date_type) = ( 'int', 'NULL', '' );
130 my(@perl_type) = ( 'varchar', 'NULL', 255 );
132 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
133 @money_type = ( 'money', '', '' );
135 @money_type = ( 'decimal', '', '10,2' );
139 # create a dbdef object from the old data structure
142 my(%tables)=&tables_hash_hack;
144 #turn it into objects
145 my($dbdef) = new FS::dbdef ( map {
147 while (@{$tables{$_}{'columns'}}) {
148 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
149 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
151 FS::dbdef_table->new(
153 $tables{$_}{'primary_key'},
154 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
155 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
156 FS::dbdef_unique->new($tables{$_}{'unique'}),
157 FS::dbdef_index->new($tables{$_}{'index'}),
162 #add radius attributes to svc_acct
164 my($svc_acct)=$dbdef->table('svc_acct');
167 foreach $attribute (@attributes) {
168 $svc_acct->addcolumn ( new FS::dbdef_column (
169 'radius_'. $attribute,
176 #make part_svc table (but now as object)
178 my($part_svc)=$dbdef->table('part_svc');
180 #because of svc_acct_pop
181 #foreach (grep /^svc_/, $dbdef->tables) {
182 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
183 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
184 my($table)=$dbdef->table($_);
186 foreach $col ( $table->columns ) {
187 next if $col =~ /^svcnum$/;
188 $part_svc->addcolumn( new FS::dbdef_column (
189 $table->name. '__' . $table->column($col)->name,
190 'varchar', #$table->column($col)->type,
192 $char_d, #$table->column($col)->length,
194 $part_svc->addcolumn ( new FS::dbdef_column (
195 $table->name. '__'. $table->column($col)->name . "_flag",
204 $dbdef->save($dbdef_file);
205 &FS::Record::reload_dbdef($dbdef_file);
211 my($dbh)=adminsuidsetup $user;
217 foreach ($dbdef->tables) {
218 my($table)=$dbdef->table($_);
219 print "Creating $_...";
224 foreach $statement ($table->sql_create_table(datasrc)) {
225 #print $statement, "\n";
226 $dbh->do( $statement )
227 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
233 #not really sample data (and shouldn't default to US)
239 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
240 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
241 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
243 my($cust_main_county)=new FS::cust_main_county({
249 $error=$cust_main_county->insert;
250 die $error if $error;
253 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
255 VIC NSW NT QLD TAS ACT WA SA
257 my($cust_main_county)=new FS::cust_main_county({
263 $error=$cust_main_county->insert;
264 die $error if $error;
267 #ISO 2-letter country codes (same as country TLDs) except US and AU
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
280 my($cust_main_county)=new FS::cust_main_county({
285 $error=$cust_main_county->insert;
286 die $error if $error;
289 $dbh->disconnect or die $dbh->errstr;
292 die "Usage:\n fs-setup user\n";
296 # Now it becomes an object. much better.
298 sub tables_hash_hack {
300 #note that s/(date|change)/_$1/; to avoid keyword conflict.
301 #put a kludge in FS::Record to catch this or? (pry need some date-handling
304 my(%tables)=( #yech.}
308 'agentnum', 'int', '', '',
309 'agent', 'varchar', '', $char_d,
310 'typenum', 'int', '', '',
311 'freq', 'int', 'NULL', '',
314 'primary_key' => 'agentnum',
316 'index' => [ ['typenum'] ],
321 'typenum', 'int', '', '',
322 'atype', 'varchar', '', $char_d,
324 'primary_key' => 'typenum',
331 'typenum', 'int', '', '',
332 'pkgpart', 'int', '', '',
335 'unique' => [ ['typenum', 'pkgpart'] ],
336 'index' => [ ['typenum'] ],
341 'invnum', 'int', '', '',
342 'custnum', 'int', '', '',
344 'charged', @money_type,
346 'printed', 'int', '', '',
348 'primary_key' => 'invnum',
350 'index' => [ ['custnum'] ],
355 'pkgnum', 'int', '', '',
356 'invnum', 'int', '', '',
357 'setup', @money_type,
358 'recur', @money_type,
363 'unique' => [ ['pkgnum', 'invnum'] ],
364 'index' => [ ['invnum'] ],
369 'crednum', 'int', '', '',
370 'custnum', 'int', '', '',
372 'amount', @money_type,
373 'credited', @money_type,
374 'otaker', 'varchar', '', 8,
375 'reason', 'varchar', '', 255,
377 'primary_key' => 'crednum',
379 'index' => [ ['custnum'] ],
384 'custnum', 'int', '', '',
385 'agentnum', 'int', '', '',
386 # 'titlenum', 'int', 'NULL', '',
387 'last', 'varchar', '', $char_d,
388 # 'middle', 'varchar', 'NULL', $char_d,
389 'first', 'varchar', '', $char_d,
390 'ss', 'char', 'NULL', 11,
391 'company', 'varchar', 'NULL', $char_d,
392 'address1', 'varchar', '', $char_d,
393 'address2', 'varchar', 'NULL', $char_d,
394 'city', 'varchar', '', $char_d,
395 'county', 'varchar', 'NULL', $char_d,
396 'state', 'varchar', 'NULL', $char_d,
397 'zip', 'varchar', '', 10,
398 'country', 'char', '', 2,
399 'daytime', 'varchar', 'NULL', 20,
400 'night', 'varchar', 'NULL', 20,
401 'fax', 'varchar', 'NULL', 12,
402 'payby', 'char', '', 4,
403 'payinfo', 'varchar', 'NULL', 16,
404 #'paydate', @date_type,
405 'paydate', 'varchar', 'NULL', 10,
406 'payname', 'varchar', 'NULL', $char_d,
407 'tax', 'char', 'NULL', 1,
408 'otaker', 'varchar', '', 8,
409 'refnum', 'int', '', '',
411 'primary_key' => 'custnum',
413 #'index' => [ ['last'], ['company'] ],
414 'index' => [ ['last'], ],
417 'cust_main_invoice' => {
419 'destnum', 'int', '', '',
420 'custnum', 'int', '', '',
421 'dest', 'varchar', '', $char_d,
423 'primary_key' => 'destnum',
425 'index' => [ ['custnum'], ],
428 'cust_main_county' => { #county+state+country are checked off the
429 #cust_main_county for validation and to provide
432 'taxnum', 'int', '', '',
433 'state', 'varchar', 'NULL', $char_d,
434 'county', 'varchar', 'NULL', $char_d,
435 'country', 'char', '', 2,
436 'tax', 'real', '', '', #tax %
438 'primary_key' => 'taxnum',
440 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
446 'paynum', 'int', '', '',
447 'invnum', 'int', '', '',
450 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
451 # payment type table.
452 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
453 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
455 'primary_key' => 'paynum',
457 'index' => [ ['invnum'] ],
460 'cust_pay_batch' => { #what's this used for again? list of customers
461 #in current CARD batch? (necessarily CARD?)
463 'invnum', 'int', '', '',
464 'custnum', 'int', '', '',
465 'last', 'varchar', '', $char_d,
466 'first', 'varchar', '', $char_d,
467 'address1', 'varchar', '', $char_d,
468 'address2', 'varchar', 'NULL', $char_d,
469 'city', 'varchar', '', $char_d,
470 'state', 'varchar', '', $char_d,
471 'zip', 'varchar', '', 10,
472 'country', 'char', '', 2,
473 'trancode', 'int', '', '',
474 'cardnum', 'varchar', '', 16,
476 'exp', 'varchar', '', 11,
477 'payname', 'varchar', 'NULL', $char_d,
478 'amount', @money_type,
482 'index' => [ ['invnum'], ['custnum'] ],
487 'pkgnum', 'int', '', '',
488 'custnum', 'int', '', '',
489 'pkgpart', 'int', '', '',
490 'otaker', 'varchar', '', 8,
494 'cancel', @date_type,
495 'expire', @date_type,
497 'primary_key' => 'pkgnum',
499 'index' => [ ['custnum'] ],
504 'refundnum', 'int', '', '',
505 'crednum', 'int', '', '',
507 'refund', @money_type,
508 'otaker', 'varchar', '', 8,
509 'reason', 'varchar', '', $char_d,
510 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
511 # into payment type table.
512 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
514 'primary_key' => 'refundnum',
516 'index' => [ ['crednum'] ],
521 'svcnum', 'int', '', '',
522 'pkgnum', 'int', '', '',
523 'svcpart', 'int', '', '',
525 'primary_key' => 'svcnum',
527 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
532 'pkgpart', 'int', '', '',
533 'pkg', 'varchar', '', $char_d,
534 'comment', 'varchar', '', $char_d,
536 'freq', 'int', '', '', #billing frequency (months)
539 'primary_key' => 'pkgpart',
546 # 'titlenum', 'int', '', '',
547 # 'title', 'varchar', '', $char_d,
549 # 'primary_key' => 'titlenum',
550 # 'unique' => [ [] ],
556 'pkgpart', 'int', '', '',
557 'svcpart', 'int', '', '',
558 'quantity', 'int', '', '',
561 'unique' => [ ['pkgpart', 'svcpart'] ],
562 'index' => [ ['pkgpart'] ],
567 'refnum', 'int', '', '',
568 'referral', 'varchar', '', $char_d,
570 'primary_key' => 'refnum',
577 'svcpart', 'int', '', '',
578 'svc', 'varchar', '', $char_d,
579 'svcdb', 'varchar', '', $char_d,
581 'primary_key' => 'svcpart',
586 #(this should be renamed to part_pop)
589 'popnum', 'int', '', '',
590 'city', 'varchar', '', $char_d,
591 'state', 'varchar', '', $char_d,
593 'exch', 'char', '', 3,
596 'primary_key' => 'popnum',
603 'svcnum', 'int', '', '',
604 'username', 'varchar', '', $username_len, #unique (& remove dup code)
605 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
606 'popnum', 'int', 'NULL', '',
607 'uid', 'int', 'NULL', '',
608 'gid', 'int', 'NULL', '',
609 'finger', 'varchar', 'NULL', $char_d,
610 'dir', 'varchar', 'NULL', $char_d,
611 'shell', 'varchar', 'NULL', $char_d,
612 'quota', 'varchar', 'NULL', $char_d,
613 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
615 'primary_key' => 'svcnum',
617 'index' => [ ['username'] ],
622 'svcnum', 'int', '', '',
623 'domsvc', 'int', '', '',
624 'domuid', 'int', '', '',
625 'domuser', 'varchar', '', $char_d,
627 'primary_key' => 'svcnum',
629 'index' => [ ['domsvc'], ['domuid'] ],
634 # 'svcnum', 'int', '', '',
635 # 'amount', @money_type,
637 # 'primary_key' => 'svcnum',
638 # 'unique' => [ [] ],
644 'svcnum', 'int', '', '',
645 'domain', 'varchar', '', $char_d,
647 'primary_key' => 'svcnum',
648 'unique' => [ ['domain'] ],
654 # 'svcnum', 'int', '', '',
655 # 'svcnum', 'int', '', '',
656 # 'svcnum', 'int', '', '',
657 # 'worker', 'varchar', '', $char_d,
658 # '_date', @date_type,
660 # 'primary_key' => 'svcnum',
661 # 'unique' => [ [] ],