3 # $Id: fs-setup,v 1.14 1999-02-07 09:59:14 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.14 1999-02-07 09:59:14 ivan
36 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
38 # Revision 1.13 1999/02/04 06:09:23 ivan
41 # Revision 1.12 1999/02/03 10:42:27 ivan
42 # *** empty log message ***
44 # Revision 1.11 1999/01/17 03:11:52 ivan
45 # remove preliminary completehost changes
47 # Revision 1.10 1998/12/16 06:05:38 ivan
48 # add table cust_main_invoice
50 # Revision 1.9 1998/12/15 04:36:29 ivan
53 # Revision 1.8 1998/12/15 04:33:27 ivan
54 # dies if it isn't running as the freeside user
56 # Revision 1.7 1998/11/18 09:01:31 ivan
59 # Revision 1.6 1998/11/15 13:18:02 ivan
62 # Revision 1.5 1998/11/15 09:43:03 ivan
63 # update for new config file syntax, new adminsuidsetup
65 # Revision 1.4 1998/10/22 15:51:23 ivan
66 # also varchar with no length specified - postgresql fix broke mysql.
68 # Revision 1.3 1998/10/22 15:46:28 ivan
69 # now smallint is illegal, so remove that too.
72 #to delay loading dbdef until we're ready
73 BEGIN { $FS::Record::setup_hack = 1; }
78 use FS::UID qw(adminsuidsetup datasrc checkeuid);
80 use FS::cust_main_county;
82 die "Not running uid freeside!" unless checkeuid();
84 my $user = shift or die &usage;
85 FS::UID::getsecrets $user;
87 #needs to match FS::Record
88 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
92 print "\nEnter the maximum username length: ";
93 my($username_len)=&getvalue;
95 print "\n\n", <<END, ":";
96 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
97 for each user. Enter any additional RADIUS attributes you need to track for
98 each user, separated by whitespace.
100 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
103 my($x)=scalar(<STDIN>);
110 my($char_d) = 80; #default maxlength for text fields
112 #my(@date_type) = ( 'timestamp', '', '' );
113 my(@date_type) = ( 'int', 'NULL', '' );
114 my(@perl_type) = ( 'varchar', 'NULL', 255 );
116 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
117 @money_type = ( 'money', '', '' );
119 @money_type = ( 'decimal', '', '10,2' );
123 # create a dbdef object from the old data structure
126 my(%tables)=&tables_hash_hack;
128 #turn it into objects
129 my($dbdef) = new FS::dbdef ( map {
131 while (@{$tables{$_}{'columns'}}) {
132 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
133 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
135 FS::dbdef_table->new(
137 $tables{$_}{'primary_key'},
138 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
139 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
140 FS::dbdef_unique->new($tables{$_}{'unique'}),
141 FS::dbdef_index->new($tables{$_}{'index'}),
146 #add radius attributes to svc_acct
148 my($svc_acct)=$dbdef->table('svc_acct');
151 foreach $attribute (@attributes) {
152 $svc_acct->addcolumn ( new FS::dbdef_column (
153 'radius_'. $attribute,
160 #make part_svc table (but now as object)
162 my($part_svc)=$dbdef->table('part_svc');
164 #because of svc_acct_pop
165 #foreach (grep /^svc_/, $dbdef->tables) {
166 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
167 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
168 my($table)=$dbdef->table($_);
170 foreach $col ( $table->columns ) {
171 next if $col =~ /^svcnum$/;
172 $part_svc->addcolumn( new FS::dbdef_column (
173 $table->name. '__' . $table->column($col)->name,
174 'varchar', #$table->column($col)->type,
176 $char_d, #$table->column($col)->length,
178 $part_svc->addcolumn ( new FS::dbdef_column (
179 $table->name. '__'. $table->column($col)->name . "_flag",
188 $dbdef->save($dbdef_file);
189 &FS::Record::reload_dbdef($dbdef_file);
195 my($dbh)=adminsuidsetup $user;
201 foreach ($dbdef->tables) {
202 my($table)=$dbdef->table($_);
203 print "Creating $_...";
208 foreach $statement ($table->sql_create_table(datasrc)) {
209 #print $statement, "\n";
210 $dbh->do( $statement )
211 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
217 #not really sample data (and shouldn't default to US)
223 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
224 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
225 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
227 my($cust_main_county)=create FS::cust_main_county({
233 $error=$cust_main_county->insert;
234 die $error if $error;
237 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
239 VIC NSW NT QLD TAS ACT WA SA
241 my($cust_main_county)=create FS::cust_main_county({
247 $error=$cust_main_county->insert;
248 die $error if $error;
251 #ISO 2-letter country codes (same as country TLDs) except US and AU
253 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
254 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
255 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
256 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
257 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
258 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
259 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
260 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
261 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
264 my($cust_main_county)=create FS::cust_main_county({
269 $error=$cust_main_county->insert;
270 die $error if $error;
273 $dbh->disconnect or die $dbh->errstr;
276 die "Usage:\n fs-setup user\n";
280 # Now it becomes an object. much better.
282 sub tables_hash_hack {
284 #note that s/(date|change)/_$1/; to avoid keyword conflict.
285 #put a kludge in FS::Record to catch this or? (pry need some date-handling
288 my(%tables)=( #yech.}
292 'agentnum', 'int', '', '',
293 'agent', 'varchar', '', $char_d,
294 'typenum', 'int', '', '',
295 'freq', 'int', 'NULL', '',
298 'primary_key' => 'agentnum',
300 'index' => [ ['typenum'] ],
305 'typenum', 'int', '', '',
306 'atype', 'varchar', '', $char_d,
308 'primary_key' => 'typenum',
315 'typenum', 'int', '', '',
316 'pkgpart', 'int', '', '',
319 'unique' => [ ['typenum', 'pkgpart'] ],
320 'index' => [ ['typenum'] ],
325 'invnum', 'int', '', '',
326 'custnum', 'int', '', '',
328 'charged', @money_type,
330 'printed', 'int', '', '',
332 'primary_key' => 'invnum',
334 'index' => [ ['custnum'] ],
339 'pkgnum', 'int', '', '',
340 'invnum', 'int', '', '',
341 'setup', @money_type,
342 'recur', @money_type,
347 'unique' => [ ['pkgnum', 'invnum'] ],
348 'index' => [ ['invnum'] ],
353 'crednum', 'int', '', '',
354 'custnum', 'int', '', '',
356 'amount', @money_type,
357 'credited', @money_type,
358 'otaker', 'varchar', '', 8,
359 'reason', 'varchar', '', 255,
361 'primary_key' => 'crednum',
363 'index' => [ ['custnum'] ],
368 'custnum', 'int', '', '',
369 'agentnum', 'int', '', '',
370 # 'titlenum', 'int', 'NULL', '',
371 'last', 'varchar', '', $char_d,
372 # 'middle', 'varchar', 'NULL', $char_d,
373 'first', 'varchar', '', $char_d,
374 'ss', 'char', 'NULL', 11,
375 'company', 'varchar', 'NULL', $char_d,
376 'address1', 'varchar', '', $char_d,
377 'address2', 'varchar', 'NULL', $char_d,
378 'city', 'varchar', '', $char_d,
379 'county', 'varchar', 'NULL', $char_d,
380 'state', 'varchar', 'NULL', $char_d,
381 'zip', 'varchar', '', 10,
382 'country', 'char', '', 2,
383 'daytime', 'varchar', 'NULL', 20,
384 'night', 'varchar', 'NULL', 20,
385 'fax', 'varchar', 'NULL', 12,
386 'payby', 'char', '', 4,
387 'payinfo', 'varchar', 'NULL', 16,
388 'paydate', @date_type,
389 'payname', 'varchar', 'NULL', $char_d,
390 'tax', 'char', 'NULL', 1,
391 'otaker', 'varchar', '', 8,
392 'refnum', 'int', '', '',
394 'primary_key' => 'custnum',
396 #'index' => [ ['last'], ['company'] ],
397 'index' => [ ['last'], ],
400 'cust_main_invoice' => {
402 'destnum', 'int', '', '',
403 'custnum', 'int', '', '',
404 'dest', 'varchar', '', $char_d,
406 'primary_key' => 'destnum',
408 'index' => [ ['custnum'], ],
411 'cust_main_county' => { #county+state+country are checked off the
412 #cust_main_county for validation and to provide
415 'taxnum', 'int', '', '',
416 'state', 'varchar', 'NULL', $char_d,
417 'county', 'varchar', 'NULL', $char_d,
418 'country', 'char', '', 2,
419 'tax', 'real', '', '', #tax %
421 'primary_key' => 'taxnum',
423 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
429 'paynum', 'int', '', '',
430 'invnum', 'int', '', '',
433 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
434 # payment type table.
435 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
436 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
438 'primary_key' => 'paynum',
440 'index' => [ ['invnum'] ],
443 'cust_pay_batch' => { #what's this used for again? list of customers
444 #in current CARD batch? (necessarily CARD?)
446 'invnum', 'int', '', '',
447 'custnum', 'int', '', '',
448 'last', 'varchar', '', $char_d,
449 'first', 'varchar', '', $char_d,
450 'address1', 'varchar', '', $char_d,
451 'address2', 'varchar', 'NULL', $char_d,
452 'city', 'varchar', '', $char_d,
453 'state', 'varchar', '', $char_d,
454 'zip', 'varchar', '', 10,
455 'country', 'char', '', 2,
456 'trancode', 'int', '', '',
457 'cardnum', 'varchar', '', 16,
459 'payname', 'varchar', 'NULL', $char_d,
460 'amount', @money_type,
464 'index' => [ ['invnum'], ['custnum'] ],
469 'pkgnum', 'int', '', '',
470 'custnum', 'int', '', '',
471 'pkgpart', 'int', '', '',
472 'otaker', 'varchar', '', 8,
476 'cancel', @date_type,
477 'expire', @date_type,
479 'primary_key' => 'pkgnum',
481 'index' => [ ['custnum'] ],
486 'refundnum', 'int', '', '',
487 'crednum', 'int', '', '',
489 'refund', @money_type,
490 'otaker', 'varchar', '', 8,
491 'reason', 'varchar', '', $char_d,
492 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
493 # into payment type table.
494 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
496 'primary_key' => 'refundnum',
498 'index' => [ ['crednum'] ],
503 'svcnum', 'int', '', '',
504 'pkgnum', 'int', '', '',
505 'svcpart', 'int', '', '',
507 'primary_key' => 'svcnum',
509 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
514 'pkgpart', 'int', '', '',
515 'pkg', 'varchar', '', $char_d,
516 'comment', 'varchar', '', $char_d,
518 'freq', 'int', '', '', #billing frequency (months)
521 'primary_key' => 'pkgpart',
528 # 'titlenum', 'int', '', '',
529 # 'title', 'varchar', '', $char_d,
531 # 'primary_key' => 'titlenum',
532 # 'unique' => [ [] ],
538 'pkgpart', 'int', '', '',
539 'svcpart', 'int', '', '',
540 'quantity', 'int', '', '',
543 'unique' => [ ['pkgpart', 'svcpart'] ],
544 'index' => [ ['pkgpart'] ],
549 'refnum', 'int', '', '',
550 'referral', 'varchar', '', $char_d,
552 'primary_key' => 'refnum',
559 'svcpart', 'int', '', '',
560 'svc', 'varchar', '', $char_d,
561 'svcdb', 'varchar', '', $char_d,
563 'primary_key' => 'svcpart',
568 #(this should be renamed to part_pop)
571 'popnum', 'int', '', '',
572 'city', 'varchar', '', $char_d,
573 'state', 'varchar', '', $char_d,
575 'exch', 'char', '', 3,
578 'primary_key' => 'popnum',
585 'svcnum', 'int', '', '',
586 'username', 'varchar', '', $username_len, #unique (& remove dup code)
587 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
588 'popnum', 'int', 'NULL', '',
589 'uid', 'int', 'NULL', '',
590 'gid', 'int', 'NULL', '',
591 'finger', 'varchar', 'NULL', $char_d,
592 'dir', 'varchar', 'NULL', $char_d,
593 'shell', 'varchar', 'NULL', $char_d,
594 'quota', 'varchar', 'NULL', $char_d,
595 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
597 'primary_key' => 'svcnum',
599 'index' => [ ['username'] ],
604 'svcnum', 'int', '', '',
605 'domsvc', 'int', '', '',
606 'domuid', 'int', '', '',
607 'domuser', 'varchar', '', $char_d,
609 'primary_key' => 'svcnum',
611 'index' => [ ['domsvc'], ['domuid'] ],
616 # 'svcnum', 'int', '', '',
617 # 'amount', @money_type,
619 # 'primary_key' => 'svcnum',
620 # 'unique' => [ [] ],
626 'svcnum', 'int', '', '',
627 'domain', 'varchar', '', $char_d,
629 'primary_key' => 'svcnum',
630 'unique' => [ ['domain'] ],
636 # 'svcnum', 'int', '', '',
637 # 'svcnum', 'int', '', '',
638 # 'svcnum', 'int', '', '',
639 # 'worker', 'varchar', '', $char_d,
640 # '_date', @date_type,
642 # 'primary_key' => 'svcnum',
643 # 'unique' => [ [] ],