3 # $Id: fs-setup,v 1.10 1998-12-16 06:05:38 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.10 1998-12-16 06:05:38 ivan
36 # add table cust_main_invoice
38 # Revision 1.9 1998/12/15 04:36:29 ivan
41 # Revision 1.8 1998/12/15 04:33:27 ivan
42 # dies if it isn't running as the freeside user
44 # Revision 1.7 1998/11/18 09:01:31 ivan
47 # Revision 1.6 1998/11/15 13:18:02 ivan
50 # Revision 1.5 1998/11/15 09:43:03 ivan
51 # update for new config file syntax, new adminsuidsetup
53 # Revision 1.4 1998/10/22 15:51:23 ivan
54 # also varchar with no length specified - postgresql fix broke mysql.
56 # Revision 1.3 1998/10/22 15:46:28 ivan
57 # now smallint is illegal, so remove that too.
60 #to delay loading dbdef until we're ready
61 BEGIN { $FS::Record::setup_hack = 1; }
66 use FS::UID qw(adminsuidsetup datasrc checkeuid);
68 use FS::cust_main_county;
70 die "Not running uid freeside!" unless checkeuid();
72 my $user = shift or die &usage;
73 FS::UID::getsecrets $user;
75 #needs to match FS::Record
76 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
80 print "\nEnter the maximum username length: ";
81 my($username_len)=&getvalue;
83 print "\n\n", <<END, ":";
84 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
85 for each user. Enter any additional RADIUS attributes you need to track for
86 each user, separated by whitespace.
88 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
91 my($x)=scalar(<STDIN>);
98 my($char_d) = 80; #default maxlength for text fields
100 #my(@date_type) = ( 'timestamp', '', '' );
101 my(@date_type) = ( 'int', 'NULL', '' );
102 my(@perl_type) = ( 'varchar', 'NULL', 255 );
104 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
105 @money_type = ( 'money', '', '' );
107 @money_type = ( 'decimal', '', '10,2' );
111 # create a dbdef object from the old data structure
114 my(%tables)=&tables_hash_hack;
116 #turn it into objects
117 my($dbdef) = new FS::dbdef ( map {
119 while (@{$tables{$_}{'columns'}}) {
120 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
121 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
123 FS::dbdef_table->new(
125 $tables{$_}{'primary_key'},
126 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
127 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
128 FS::dbdef_unique->new($tables{$_}{'unique'}),
129 FS::dbdef_index->new($tables{$_}{'index'}),
134 #add radius attributes to svc_acct
136 my($svc_acct)=$dbdef->table('svc_acct');
139 foreach $attribute (@attributes) {
140 $svc_acct->addcolumn ( new FS::dbdef_column (
141 'radius_'. $attribute,
148 #make part_svc table (but now as object)
150 my($part_svc)=$dbdef->table('part_svc');
152 #because of svc_acct_pop
153 #foreach (grep /^svc_/, $dbdef->tables) {
154 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
155 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
156 my($table)=$dbdef->table($_);
158 foreach $col ( $table->columns ) {
159 next if $col =~ /^svcnum$/;
160 $part_svc->addcolumn( new FS::dbdef_column (
161 $table->name. '__' . $table->column($col)->name,
162 'varchar', #$table->column($col)->type,
164 $char_d, #$table->column($col)->length,
166 $part_svc->addcolumn ( new FS::dbdef_column (
167 $table->name. '__'. $table->column($col)->name . "_flag",
176 $dbdef->save($dbdef_file);
177 FS::Record::reload_dbdef($dbdef_file);
183 my($dbh)=adminsuidsetup $user;
189 foreach ($dbdef->tables) {
190 my($table)=$dbdef->table($_);
191 print "Creating $_...";
196 foreach $statement ($table->sql_create_table(datasrc)) {
197 #print $statement, "\n";
198 $dbh->do( $statement )
199 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
205 #not really sample data (and shouldn't default to US)
211 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
212 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
213 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
215 my($cust_main_county)=create FS::cust_main_county({
221 $error=$cust_main_county->insert;
222 die $error if $error;
225 #ISO 2-letter country codes (same as country TLDs) except US
227 AF AL DZ AS AD AO AI AQ AG AR AM AW AU AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
228 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
229 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
230 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
231 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
232 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
233 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
234 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
235 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
238 my($cust_main_county)=create FS::cust_main_county({
243 $error=$cust_main_county->insert;
244 die $error if $error;
247 $dbh->disconnect or die $dbh->errstr;
250 die "Usage:\n fs-setup user\n";
254 # Now it becomes an object. much better.
256 sub tables_hash_hack {
258 #note that s/(date|change)/_$1/; to avoid keyword conflict.
259 #put a kludge in FS::Record to catch this or? (pry need some date-handling
262 my(%tables)=( #yech.}
266 'agentnum', 'int', '', '',
267 'agent', 'varchar', '', $char_d,
268 'typenum', 'int', '', '',
269 'freq', 'int', 'NULL', '',
272 'primary_key' => 'agentnum',
274 'index' => [ ['typenum'] ],
279 'typenum', 'int', '', '',
280 'atype', 'varchar', '', $char_d,
282 'primary_key' => 'typenum',
289 'typenum', 'int', '', '',
290 'pkgpart', 'int', '', '',
293 'unique' => [ ['typenum', 'pkgpart'] ],
294 'index' => [ ['typenum'] ],
299 'invnum', 'int', '', '',
300 'custnum', 'int', '', '',
302 'charged', @money_type,
304 'printed', 'int', '', '',
306 'primary_key' => 'invnum',
308 'index' => [ ['custnum'] ],
313 'pkgnum', 'int', '', '',
314 'invnum', 'int', '', '',
315 'setup', @money_type,
316 'recur', @money_type,
321 'unique' => [ ['pkgnum', 'invnum'] ],
322 'index' => [ ['invnum'] ],
327 'crednum', 'int', '', '',
328 'custnum', 'int', '', '',
330 'amount', @money_type,
331 'credited', @money_type,
332 'otaker', 'varchar', '', 8,
333 'reason', 'varchar', '', 255,
335 'primary_key' => 'crednum',
337 'index' => [ ['custnum'] ],
342 'custnum', 'int', '', '',
343 'agentnum', 'int', '', '',
344 'titlenum', 'int', 'NULL', '',
345 'last', 'varchar', '', $char_d,
346 'middle', 'varchar', 'NULL', $char_d,
347 'first', 'varchar', '', $char_d,
348 'ss', 'char', 'NULL', 11,
349 'company', 'varchar', 'NULL', $char_d,
350 'address1', 'varchar', '', $char_d,
351 'address2', 'varchar', 'NULL', $char_d,
352 'city', 'varchar', '', $char_d,
353 'county', 'varchar', 'NULL', $char_d,
354 'state', 'varchar', '', $char_d,
355 'zip', 'varchar', '', 10,
356 'country', 'char', '', 2,
357 'daytime', 'varchar', 'NULL', 20,
358 'night', 'varchar', 'NULL', 20,
359 'fax', 'varchar', 'NULL', 12,
360 'payby', 'char', '', 4,
361 'payinfo', 'varchar', 'NULL', 16,
362 'paydate', @date_type,
363 'payname', 'varchar', 'NULL', $char_d,
364 'tax', 'char', 'NULL', 1,
365 'otaker', 'varchar', '', 8,
366 'refnum', 'int', '', '',
368 'primary_key' => 'custnum',
370 #'index' => [ ['last'], ['company'] ],
371 'index' => [ ['last'], ],
374 'cust_main_invoice' => {
376 'destnum', 'int', '', '',
377 'custnum', 'int', '', '',
378 'dest', 'varchar', '', $char_d,
380 'primary_key' => 'destnum',
382 'index' => [ ['custnum'], ],
385 'cust_main_county' => { #county+state+country are checked off the
386 #cust_main_county for validation and to provide
389 'taxnum', 'int', '', '',
390 'state', 'char', 'NULL', $char_d,
391 'county', 'varchar', 'NULL', $char_d,
392 'country', 'char', '', 2,
393 'tax', 'real', '', '', #tax %
395 'primary_key' => 'taxnum',
397 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
403 'paynum', 'int', '', '',
404 'invnum', 'int', '', '',
407 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
408 # payment type table.
409 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
410 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
412 'primary_key' => 'paynum',
414 'index' => [ ['invnum'] ],
417 'cust_pay_batch' => { #what's this used for again? list of customers
418 #in current CARD batch? (necessarily CARD?)
420 'invnum', 'int', '', '',
421 'custnum', 'int', '', '',
422 'last', 'varchar', '', $char_d,
423 'first', 'varchar', '', $char_d,
424 'address1', 'varchar', '', $char_d,
425 'address2', 'varchar', 'NULL', $char_d,
426 'city', 'varchar', '', $char_d,
427 'state', 'char', '', 2,
428 'zip', 'varchar', '', 10,
429 'country', 'char', '', 2,
430 'trancode', 'int', '', '',
431 'cardnum', 'varchar', '', 16,
433 'payname', 'varchar', 'NULL', $char_d,
434 'amount', @money_type,
438 'index' => [ ['invnum'], ['custnum'] ],
443 'pkgnum', 'int', '', '',
444 'custnum', 'int', '', '',
445 'pkgpart', 'int', '', '',
446 'otaker', 'varchar', '', 8,
450 'cancel', @date_type,
451 'expire', @date_type,
453 'primary_key' => 'pkgnum',
455 'index' => [ ['custnum'] ],
460 'refundnum', 'int', '', '',
461 'crednum', 'int', '', '',
463 'refund', @money_type,
464 'otaker', 'varchar', '', 8,
465 'reason', 'varchar', '', $char_d,
466 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
467 # into payment type table.
468 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
470 'primary_key' => 'refundnum',
472 'index' => [ ['crednum'] ],
477 'svcnum', 'int', '', '',
478 'pkgnum', 'int', '', '',
479 'svcpart', 'int', '', '',
481 'primary_key' => 'svcnum',
483 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
488 'pkgpart', 'int', '', '',
489 'pkg', 'varchar', '', $char_d,
490 'comment', 'varchar', '', $char_d,
492 'freq', 'int', '', '', #billing frequency (months)
495 'primary_key' => 'pkgpart',
502 'titlenum', 'int', '', '',
503 'title', 'varchar', '', $char_d,
505 'primary_key' => 'titlenum',
512 'pkgpart', 'int', '', '',
513 'svcpart', 'int', '', '',
514 'quantity', 'int', '', '',
517 'unique' => [ ['pkgpart', 'svcpart'] ],
518 'index' => [ ['pkgpart'] ],
523 'refnum', 'int', '', '',
524 'referral', 'varchar', '', $char_d,
526 'primary_key' => 'refnum',
533 'svcpart', 'int', '', '',
534 'svc', 'varchar', '', $char_d,
535 'svcdb', 'varchar', '', $char_d,
537 'primary_key' => 'svcpart',
542 #(this should be renamed to part_pop)
545 'popnum', 'int', '', '',
546 'city', 'varchar', '', $char_d,
547 'state', 'char', '', 2,
549 'exch', 'char', '', 3,
552 'primary_key' => 'popnum',
559 'svcnum', 'int', '', '',
560 'username', 'varchar', '', $username_len, #unique (& remove dup code)
561 '_password', 'varchar', '', 25, #13 for encryped pw's plus ' *SUSPENDED*
562 'popnum', 'int', 'NULL', '',
563 'uid', 'int', 'NULL', '',
564 'gid', 'int', 'NULL', '',
565 'finger', 'varchar', 'NULL', $char_d,
566 'dir', 'varchar', 'NULL', $char_d,
567 'shell', 'varchar', 'NULL', $char_d,
568 'quota', 'varchar', 'NULL', $char_d,
569 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
571 'primary_key' => 'svcnum',
573 'index' => [ ['username'] ],
578 'svcnum', 'int', '', '',
579 'domsvc', 'int', '', '',
580 'domuid', 'int', '', '',
581 'domuser', 'varchar', '', $char_d,
583 'primary_key' => 'svcnum',
585 'index' => [ ['domsvc'], ['domuid'] ],
590 # 'svcnum', 'int', '', '',
591 # 'amount', @money_type,
593 # 'primary_key' => 'svcnum',
594 # 'unique' => [ [] ],
600 'svcnum', 'int', '', '',
601 'domain', 'varchar', '', $char_d,
603 'primary_key' => 'svcnum',
604 'unique' => [ ['domain'] ],
610 # 'svcnum', 'int', '', '',
611 # 'svcnum', 'int', '', '',
612 # 'svcnum', 'int', '', '',
613 # 'worker', 'varchar', '', $char_d,
614 # '_date', @date_type,
616 # 'primary_key' => 'svcnum',
617 # 'unique' => [ [] ],