3 # $Id: fs-setup,v 1.15 1999-02-27 21:06:21 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.15 1999-02-27 21:06:21 ivan
36 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
37 # by Ben Leibig <leibig@colorado.edu>
39 # Revision 1.14 1999/02/07 09:59:14 ivan
40 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
42 # Revision 1.13 1999/02/04 06:09:23 ivan
45 # Revision 1.12 1999/02/03 10:42:27 ivan
46 # *** empty log message ***
48 # Revision 1.11 1999/01/17 03:11:52 ivan
49 # remove preliminary completehost changes
51 # Revision 1.10 1998/12/16 06:05:38 ivan
52 # add table cust_main_invoice
54 # Revision 1.9 1998/12/15 04:36:29 ivan
57 # Revision 1.8 1998/12/15 04:33:27 ivan
58 # dies if it isn't running as the freeside user
60 # Revision 1.7 1998/11/18 09:01:31 ivan
63 # Revision 1.6 1998/11/15 13:18:02 ivan
66 # Revision 1.5 1998/11/15 09:43:03 ivan
67 # update for new config file syntax, new adminsuidsetup
69 # Revision 1.4 1998/10/22 15:51:23 ivan
70 # also varchar with no length specified - postgresql fix broke mysql.
72 # Revision 1.3 1998/10/22 15:46:28 ivan
73 # now smallint is illegal, so remove that too.
76 #to delay loading dbdef until we're ready
77 BEGIN { $FS::Record::setup_hack = 1; }
82 use FS::UID qw(adminsuidsetup datasrc checkeuid);
84 use FS::cust_main_county;
86 die "Not running uid freeside!" unless checkeuid();
88 my $user = shift or die &usage;
89 FS::UID::getsecrets $user;
91 #needs to match FS::Record
92 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
96 print "\nEnter the maximum username length: ";
97 my($username_len)=&getvalue;
99 print "\n\n", <<END, ":";
100 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
101 for each user. Enter any additional RADIUS attributes you need to track for
102 each user, separated by whitespace.
104 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
107 my($x)=scalar(<STDIN>);
114 my($char_d) = 80; #default maxlength for text fields
116 #my(@date_type) = ( 'timestamp', '', '' );
117 my(@date_type) = ( 'int', 'NULL', '' );
118 my(@perl_type) = ( 'varchar', 'NULL', 255 );
120 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
121 @money_type = ( 'money', '', '' );
123 @money_type = ( 'decimal', '', '10,2' );
127 # create a dbdef object from the old data structure
130 my(%tables)=&tables_hash_hack;
132 #turn it into objects
133 my($dbdef) = new FS::dbdef ( map {
135 while (@{$tables{$_}{'columns'}}) {
136 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
137 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
139 FS::dbdef_table->new(
141 $tables{$_}{'primary_key'},
142 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
143 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
144 FS::dbdef_unique->new($tables{$_}{'unique'}),
145 FS::dbdef_index->new($tables{$_}{'index'}),
150 #add radius attributes to svc_acct
152 my($svc_acct)=$dbdef->table('svc_acct');
155 foreach $attribute (@attributes) {
156 $svc_acct->addcolumn ( new FS::dbdef_column (
157 'radius_'. $attribute,
164 #make part_svc table (but now as object)
166 my($part_svc)=$dbdef->table('part_svc');
168 #because of svc_acct_pop
169 #foreach (grep /^svc_/, $dbdef->tables) {
170 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
171 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
172 my($table)=$dbdef->table($_);
174 foreach $col ( $table->columns ) {
175 next if $col =~ /^svcnum$/;
176 $part_svc->addcolumn( new FS::dbdef_column (
177 $table->name. '__' . $table->column($col)->name,
178 'varchar', #$table->column($col)->type,
180 $char_d, #$table->column($col)->length,
182 $part_svc->addcolumn ( new FS::dbdef_column (
183 $table->name. '__'. $table->column($col)->name . "_flag",
192 $dbdef->save($dbdef_file);
193 &FS::Record::reload_dbdef($dbdef_file);
199 my($dbh)=adminsuidsetup $user;
205 foreach ($dbdef->tables) {
206 my($table)=$dbdef->table($_);
207 print "Creating $_...";
212 foreach $statement ($table->sql_create_table(datasrc)) {
213 #print $statement, "\n";
214 $dbh->do( $statement )
215 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
221 #not really sample data (and shouldn't default to US)
227 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
228 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
229 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
231 my($cust_main_county)=create FS::cust_main_county({
237 $error=$cust_main_county->insert;
238 die $error if $error;
241 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
243 VIC NSW NT QLD TAS ACT WA SA
245 my($cust_main_county)=create FS::cust_main_county({
251 $error=$cust_main_county->insert;
252 die $error if $error;
255 #ISO 2-letter country codes (same as country TLDs) except US and AU
257 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
258 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
259 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
260 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
261 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
262 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
263 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
264 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
265 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
268 my($cust_main_county)=create FS::cust_main_county({
273 $error=$cust_main_county->insert;
274 die $error if $error;
277 $dbh->disconnect or die $dbh->errstr;
280 die "Usage:\n fs-setup user\n";
284 # Now it becomes an object. much better.
286 sub tables_hash_hack {
288 #note that s/(date|change)/_$1/; to avoid keyword conflict.
289 #put a kludge in FS::Record to catch this or? (pry need some date-handling
292 my(%tables)=( #yech.}
296 'agentnum', 'int', '', '',
297 'agent', 'varchar', '', $char_d,
298 'typenum', 'int', '', '',
299 'freq', 'int', 'NULL', '',
302 'primary_key' => 'agentnum',
304 'index' => [ ['typenum'] ],
309 'typenum', 'int', '', '',
310 'atype', 'varchar', '', $char_d,
312 'primary_key' => 'typenum',
319 'typenum', 'int', '', '',
320 'pkgpart', 'int', '', '',
323 'unique' => [ ['typenum', 'pkgpart'] ],
324 'index' => [ ['typenum'] ],
329 'invnum', 'int', '', '',
330 'custnum', 'int', '', '',
332 'charged', @money_type,
334 'printed', 'int', '', '',
336 'primary_key' => 'invnum',
338 'index' => [ ['custnum'] ],
343 'pkgnum', 'int', '', '',
344 'invnum', 'int', '', '',
345 'setup', @money_type,
346 'recur', @money_type,
351 'unique' => [ ['pkgnum', 'invnum'] ],
352 'index' => [ ['invnum'] ],
357 'crednum', 'int', '', '',
358 'custnum', 'int', '', '',
360 'amount', @money_type,
361 'credited', @money_type,
362 'otaker', 'varchar', '', 8,
363 'reason', 'varchar', '', 255,
365 'primary_key' => 'crednum',
367 'index' => [ ['custnum'] ],
372 'custnum', 'int', '', '',
373 'agentnum', 'int', '', '',
374 # 'titlenum', 'int', 'NULL', '',
375 'last', 'varchar', '', $char_d,
376 # 'middle', 'varchar', 'NULL', $char_d,
377 'first', 'varchar', '', $char_d,
378 'ss', 'char', 'NULL', 11,
379 'company', 'varchar', 'NULL', $char_d,
380 'address1', 'varchar', '', $char_d,
381 'address2', 'varchar', 'NULL', $char_d,
382 'city', 'varchar', '', $char_d,
383 'county', 'varchar', 'NULL', $char_d,
384 'state', 'varchar', 'NULL', $char_d,
385 'zip', 'varchar', '', 10,
386 'country', 'char', '', 2,
387 'daytime', 'varchar', 'NULL', 20,
388 'night', 'varchar', 'NULL', 20,
389 'fax', 'varchar', 'NULL', 12,
390 'payby', 'char', '', 4,
391 'payinfo', 'varchar', 'NULL', 16,
392 #'paydate', @date_type,
393 'paydate', 'varchar', 'NULL', 10,
394 'payname', 'varchar', 'NULL', $char_d,
395 'tax', 'char', 'NULL', 1,
396 'otaker', 'varchar', '', 8,
397 'refnum', 'int', '', '',
399 'primary_key' => 'custnum',
401 #'index' => [ ['last'], ['company'] ],
402 'index' => [ ['last'], ],
405 'cust_main_invoice' => {
407 'destnum', 'int', '', '',
408 'custnum', 'int', '', '',
409 'dest', 'varchar', '', $char_d,
411 'primary_key' => 'destnum',
413 'index' => [ ['custnum'], ],
416 'cust_main_county' => { #county+state+country are checked off the
417 #cust_main_county for validation and to provide
420 'taxnum', 'int', '', '',
421 'state', 'varchar', 'NULL', $char_d,
422 'county', 'varchar', 'NULL', $char_d,
423 'country', 'char', '', 2,
424 'tax', 'real', '', '', #tax %
426 'primary_key' => 'taxnum',
428 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
434 'paynum', 'int', '', '',
435 'invnum', 'int', '', '',
438 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
439 # payment type table.
440 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
441 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
443 'primary_key' => 'paynum',
445 'index' => [ ['invnum'] ],
448 'cust_pay_batch' => { #what's this used for again? list of customers
449 #in current CARD batch? (necessarily CARD?)
451 'invnum', 'int', '', '',
452 'custnum', 'int', '', '',
453 'last', 'varchar', '', $char_d,
454 'first', 'varchar', '', $char_d,
455 'address1', 'varchar', '', $char_d,
456 'address2', 'varchar', 'NULL', $char_d,
457 'city', 'varchar', '', $char_d,
458 'state', 'varchar', '', $char_d,
459 'zip', 'varchar', '', 10,
460 'country', 'char', '', 2,
461 'trancode', 'int', '', '',
462 'cardnum', 'varchar', '', 16,
464 'payname', 'varchar', 'NULL', $char_d,
465 'amount', @money_type,
469 'index' => [ ['invnum'], ['custnum'] ],
474 'pkgnum', 'int', '', '',
475 'custnum', 'int', '', '',
476 'pkgpart', 'int', '', '',
477 'otaker', 'varchar', '', 8,
481 'cancel', @date_type,
482 'expire', @date_type,
484 'primary_key' => 'pkgnum',
486 'index' => [ ['custnum'] ],
491 'refundnum', 'int', '', '',
492 'crednum', 'int', '', '',
494 'refund', @money_type,
495 'otaker', 'varchar', '', 8,
496 'reason', 'varchar', '', $char_d,
497 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
498 # into payment type table.
499 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
501 'primary_key' => 'refundnum',
503 'index' => [ ['crednum'] ],
508 'svcnum', 'int', '', '',
509 'pkgnum', 'int', '', '',
510 'svcpart', 'int', '', '',
512 'primary_key' => 'svcnum',
514 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
519 'pkgpart', 'int', '', '',
520 'pkg', 'varchar', '', $char_d,
521 'comment', 'varchar', '', $char_d,
523 'freq', 'int', '', '', #billing frequency (months)
526 'primary_key' => 'pkgpart',
533 # 'titlenum', 'int', '', '',
534 # 'title', 'varchar', '', $char_d,
536 # 'primary_key' => 'titlenum',
537 # 'unique' => [ [] ],
543 'pkgpart', 'int', '', '',
544 'svcpart', 'int', '', '',
545 'quantity', 'int', '', '',
548 'unique' => [ ['pkgpart', 'svcpart'] ],
549 'index' => [ ['pkgpart'] ],
554 'refnum', 'int', '', '',
555 'referral', 'varchar', '', $char_d,
557 'primary_key' => 'refnum',
564 'svcpart', 'int', '', '',
565 'svc', 'varchar', '', $char_d,
566 'svcdb', 'varchar', '', $char_d,
568 'primary_key' => 'svcpart',
573 #(this should be renamed to part_pop)
576 'popnum', 'int', '', '',
577 'city', 'varchar', '', $char_d,
578 'state', 'varchar', '', $char_d,
580 'exch', 'char', '', 3,
583 'primary_key' => 'popnum',
590 'svcnum', 'int', '', '',
591 'username', 'varchar', '', $username_len, #unique (& remove dup code)
592 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
593 'popnum', 'int', 'NULL', '',
594 'uid', 'int', 'NULL', '',
595 'gid', 'int', 'NULL', '',
596 'finger', 'varchar', 'NULL', $char_d,
597 'dir', 'varchar', 'NULL', $char_d,
598 'shell', 'varchar', 'NULL', $char_d,
599 'quota', 'varchar', 'NULL', $char_d,
600 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
602 'primary_key' => 'svcnum',
604 'index' => [ ['username'] ],
609 'svcnum', 'int', '', '',
610 'domsvc', 'int', '', '',
611 'domuid', 'int', '', '',
612 'domuser', 'varchar', '', $char_d,
614 'primary_key' => 'svcnum',
616 'index' => [ ['domsvc'], ['domuid'] ],
621 # 'svcnum', 'int', '', '',
622 # 'amount', @money_type,
624 # 'primary_key' => 'svcnum',
625 # 'unique' => [ [] ],
631 'svcnum', 'int', '', '',
632 'domain', 'varchar', '', $char_d,
634 'primary_key' => 'svcnum',
635 'unique' => [ ['domain'] ],
641 # 'svcnum', 'int', '', '',
642 # 'svcnum', 'int', '', '',
643 # 'svcnum', 'int', '', '',
644 # 'worker', 'varchar', '', $char_d,
645 # '_date', @date_type,
647 # 'primary_key' => 'svcnum',
648 # 'unique' => [ [] ],