3 # $Id: fs-setup,v 1.13 1999-02-04 06:09:23 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.13 1999-02-04 06:09:23 ivan
38 # Revision 1.12 1999/02/03 10:42:27 ivan
39 # *** empty log message ***
41 # Revision 1.11 1999/01/17 03:11:52 ivan
42 # remove preliminary completehost changes
44 # Revision 1.10 1998/12/16 06:05:38 ivan
45 # add table cust_main_invoice
47 # Revision 1.9 1998/12/15 04:36:29 ivan
50 # Revision 1.8 1998/12/15 04:33:27 ivan
51 # dies if it isn't running as the freeside user
53 # Revision 1.7 1998/11/18 09:01:31 ivan
56 # Revision 1.6 1998/11/15 13:18:02 ivan
59 # Revision 1.5 1998/11/15 09:43:03 ivan
60 # update for new config file syntax, new adminsuidsetup
62 # Revision 1.4 1998/10/22 15:51:23 ivan
63 # also varchar with no length specified - postgresql fix broke mysql.
65 # Revision 1.3 1998/10/22 15:46:28 ivan
66 # now smallint is illegal, so remove that too.
69 #to delay loading dbdef until we're ready
70 BEGIN { $FS::Record::setup_hack = 1; }
75 use FS::UID qw(adminsuidsetup datasrc checkeuid);
77 use FS::cust_main_county;
79 die "Not running uid freeside!" unless checkeuid();
81 my $user = shift or die &usage;
82 FS::UID::getsecrets $user;
84 #needs to match FS::Record
85 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
89 print "\nEnter the maximum username length: ";
90 my($username_len)=&getvalue;
92 print "\n\n", <<END, ":";
93 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
94 for each user. Enter any additional RADIUS attributes you need to track for
95 each user, separated by whitespace.
97 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
100 my($x)=scalar(<STDIN>);
107 my($char_d) = 80; #default maxlength for text fields
109 #my(@date_type) = ( 'timestamp', '', '' );
110 my(@date_type) = ( 'int', 'NULL', '' );
111 my(@perl_type) = ( 'varchar', 'NULL', 255 );
113 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
114 @money_type = ( 'money', '', '' );
116 @money_type = ( 'decimal', '', '10,2' );
120 # create a dbdef object from the old data structure
123 my(%tables)=&tables_hash_hack;
125 #turn it into objects
126 my($dbdef) = new FS::dbdef ( map {
128 while (@{$tables{$_}{'columns'}}) {
129 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
130 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
132 FS::dbdef_table->new(
134 $tables{$_}{'primary_key'},
135 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
136 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
137 FS::dbdef_unique->new($tables{$_}{'unique'}),
138 FS::dbdef_index->new($tables{$_}{'index'}),
143 #add radius attributes to svc_acct
145 my($svc_acct)=$dbdef->table('svc_acct');
148 foreach $attribute (@attributes) {
149 $svc_acct->addcolumn ( new FS::dbdef_column (
150 'radius_'. $attribute,
157 #make part_svc table (but now as object)
159 my($part_svc)=$dbdef->table('part_svc');
161 #because of svc_acct_pop
162 #foreach (grep /^svc_/, $dbdef->tables) {
163 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
164 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
165 my($table)=$dbdef->table($_);
167 foreach $col ( $table->columns ) {
168 next if $col =~ /^svcnum$/;
169 $part_svc->addcolumn( new FS::dbdef_column (
170 $table->name. '__' . $table->column($col)->name,
171 'varchar', #$table->column($col)->type,
173 $char_d, #$table->column($col)->length,
175 $part_svc->addcolumn ( new FS::dbdef_column (
176 $table->name. '__'. $table->column($col)->name . "_flag",
185 $dbdef->save($dbdef_file);
186 &FS::Record::reload_dbdef($dbdef_file);
192 my($dbh)=adminsuidsetup $user;
198 foreach ($dbdef->tables) {
199 my($table)=$dbdef->table($_);
200 print "Creating $_...";
205 foreach $statement ($table->sql_create_table(datasrc)) {
206 #print $statement, "\n";
207 $dbh->do( $statement )
208 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
214 #not really sample data (and shouldn't default to US)
220 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
221 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
222 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
224 my($cust_main_county)=create FS::cust_main_county({
230 $error=$cust_main_county->insert;
231 die $error if $error;
234 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
236 VIC NSW NT QLD TAS ACT WA SA
238 my($cust_main_county)=create FS::cust_main_county({
244 $error=$cust_main_county->insert;
245 die $error if $error;
248 #ISO 2-letter country codes (same as country TLDs) except US and AU
250 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
251 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
252 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
253 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
254 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
255 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
256 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
257 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
258 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
261 my($cust_main_county)=create FS::cust_main_county({
266 $error=$cust_main_county->insert;
267 die $error if $error;
270 $dbh->disconnect or die $dbh->errstr;
273 die "Usage:\n fs-setup user\n";
277 # Now it becomes an object. much better.
279 sub tables_hash_hack {
281 #note that s/(date|change)/_$1/; to avoid keyword conflict.
282 #put a kludge in FS::Record to catch this or? (pry need some date-handling
285 my(%tables)=( #yech.}
289 'agentnum', 'int', '', '',
290 'agent', 'varchar', '', $char_d,
291 'typenum', 'int', '', '',
292 'freq', 'int', 'NULL', '',
295 'primary_key' => 'agentnum',
297 'index' => [ ['typenum'] ],
302 'typenum', 'int', '', '',
303 'atype', 'varchar', '', $char_d,
305 'primary_key' => 'typenum',
312 'typenum', 'int', '', '',
313 'pkgpart', 'int', '', '',
316 'unique' => [ ['typenum', 'pkgpart'] ],
317 'index' => [ ['typenum'] ],
322 'invnum', 'int', '', '',
323 'custnum', 'int', '', '',
325 'charged', @money_type,
327 'printed', 'int', '', '',
329 'primary_key' => 'invnum',
331 'index' => [ ['custnum'] ],
336 'pkgnum', 'int', '', '',
337 'invnum', 'int', '', '',
338 'setup', @money_type,
339 'recur', @money_type,
344 'unique' => [ ['pkgnum', 'invnum'] ],
345 'index' => [ ['invnum'] ],
350 'crednum', 'int', '', '',
351 'custnum', 'int', '', '',
353 'amount', @money_type,
354 'credited', @money_type,
355 'otaker', 'varchar', '', 8,
356 'reason', 'varchar', '', 255,
358 'primary_key' => 'crednum',
360 'index' => [ ['custnum'] ],
365 'custnum', 'int', '', '',
366 'agentnum', 'int', '', '',
367 # 'titlenum', 'int', 'NULL', '',
368 'last', 'varchar', '', $char_d,
369 # 'middle', 'varchar', 'NULL', $char_d,
370 'first', 'varchar', '', $char_d,
371 'ss', 'char', 'NULL', 11,
372 'company', 'varchar', 'NULL', $char_d,
373 'address1', 'varchar', '', $char_d,
374 'address2', 'varchar', 'NULL', $char_d,
375 'city', 'varchar', '', $char_d,
376 'county', 'varchar', 'NULL', $char_d,
377 'state', 'varchar', '', $char_d,
378 'zip', 'varchar', '', 10,
379 'country', 'char', '', 2,
380 'daytime', 'varchar', 'NULL', 20,
381 'night', 'varchar', 'NULL', 20,
382 'fax', 'varchar', 'NULL', 12,
383 'payby', 'char', '', 4,
384 'payinfo', 'varchar', 'NULL', 16,
385 'paydate', @date_type,
386 'payname', 'varchar', 'NULL', $char_d,
387 'tax', 'char', 'NULL', 1,
388 'otaker', 'varchar', '', 8,
389 'refnum', 'int', '', '',
391 'primary_key' => 'custnum',
393 #'index' => [ ['last'], ['company'] ],
394 'index' => [ ['last'], ],
397 'cust_main_invoice' => {
399 'destnum', 'int', '', '',
400 'custnum', 'int', '', '',
401 'dest', 'varchar', '', $char_d,
403 'primary_key' => 'destnum',
405 'index' => [ ['custnum'], ],
408 'cust_main_county' => { #county+state+country are checked off the
409 #cust_main_county for validation and to provide
412 'taxnum', 'int', '', '',
413 'state', 'char', 'NULL', $char_d,
414 'county', 'varchar', 'NULL', $char_d,
415 'country', 'char', '', 2,
416 'tax', 'real', '', '', #tax %
418 'primary_key' => 'taxnum',
420 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
426 'paynum', 'int', '', '',
427 'invnum', 'int', '', '',
430 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
431 # payment type table.
432 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
433 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
435 'primary_key' => 'paynum',
437 'index' => [ ['invnum'] ],
440 'cust_pay_batch' => { #what's this used for again? list of customers
441 #in current CARD batch? (necessarily CARD?)
443 'invnum', 'int', '', '',
444 'custnum', 'int', '', '',
445 'last', 'varchar', '', $char_d,
446 'first', 'varchar', '', $char_d,
447 'address1', 'varchar', '', $char_d,
448 'address2', 'varchar', 'NULL', $char_d,
449 'city', 'varchar', '', $char_d,
450 'state', 'char', '', 2,
451 'zip', 'varchar', '', 10,
452 'country', 'char', '', 2,
453 'trancode', 'int', '', '',
454 'cardnum', 'varchar', '', 16,
456 'payname', 'varchar', 'NULL', $char_d,
457 'amount', @money_type,
461 'index' => [ ['invnum'], ['custnum'] ],
466 'pkgnum', 'int', '', '',
467 'custnum', 'int', '', '',
468 'pkgpart', 'int', '', '',
469 'otaker', 'varchar', '', 8,
473 'cancel', @date_type,
474 'expire', @date_type,
476 'primary_key' => 'pkgnum',
478 'index' => [ ['custnum'] ],
483 'refundnum', 'int', '', '',
484 'crednum', 'int', '', '',
486 'refund', @money_type,
487 'otaker', 'varchar', '', 8,
488 'reason', 'varchar', '', $char_d,
489 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
490 # into payment type table.
491 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
493 'primary_key' => 'refundnum',
495 'index' => [ ['crednum'] ],
500 'svcnum', 'int', '', '',
501 'pkgnum', 'int', '', '',
502 'svcpart', 'int', '', '',
504 'primary_key' => 'svcnum',
506 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
511 'pkgpart', 'int', '', '',
512 'pkg', 'varchar', '', $char_d,
513 'comment', 'varchar', '', $char_d,
515 'freq', 'int', '', '', #billing frequency (months)
518 'primary_key' => 'pkgpart',
525 # 'titlenum', 'int', '', '',
526 # 'title', 'varchar', '', $char_d,
528 # 'primary_key' => 'titlenum',
529 # 'unique' => [ [] ],
535 'pkgpart', 'int', '', '',
536 'svcpart', 'int', '', '',
537 'quantity', 'int', '', '',
540 'unique' => [ ['pkgpart', 'svcpart'] ],
541 'index' => [ ['pkgpart'] ],
546 'refnum', 'int', '', '',
547 'referral', 'varchar', '', $char_d,
549 'primary_key' => 'refnum',
556 'svcpart', 'int', '', '',
557 'svc', 'varchar', '', $char_d,
558 'svcdb', 'varchar', '', $char_d,
560 'primary_key' => 'svcpart',
565 #(this should be renamed to part_pop)
568 'popnum', 'int', '', '',
569 'city', 'varchar', '', $char_d,
570 'state', 'char', '', 2,
572 'exch', 'char', '', 3,
575 'primary_key' => 'popnum',
582 'svcnum', 'int', '', '',
583 'username', 'varchar', '', $username_len, #unique (& remove dup code)
584 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
585 'popnum', 'int', 'NULL', '',
586 'uid', 'int', 'NULL', '',
587 'gid', 'int', 'NULL', '',
588 'finger', 'varchar', 'NULL', $char_d,
589 'dir', 'varchar', 'NULL', $char_d,
590 'shell', 'varchar', 'NULL', $char_d,
591 'quota', 'varchar', 'NULL', $char_d,
592 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
594 'primary_key' => 'svcnum',
596 'index' => [ ['username'] ],
601 'svcnum', 'int', '', '',
602 'domsvc', 'int', '', '',
603 'domuid', 'int', '', '',
604 'domuser', 'varchar', '', $char_d,
606 'primary_key' => 'svcnum',
608 'index' => [ ['domsvc'], ['domuid'] ],
613 # 'svcnum', 'int', '', '',
614 # 'amount', @money_type,
616 # 'primary_key' => 'svcnum',
617 # 'unique' => [ [] ],
623 'svcnum', 'int', '', '',
624 'domain', 'varchar', '', $char_d,
626 'primary_key' => 'svcnum',
627 'unique' => [ ['domain'] ],
633 # 'svcnum', 'int', '', '',
634 # 'svcnum', 'int', '', '',
635 # 'svcnum', 'int', '', '',
636 # 'worker', 'varchar', '', $char_d,
637 # '_date', @date_type,
639 # 'primary_key' => 'svcnum',
640 # 'unique' => [ [] ],