remove preliminary completehost changes
[freeside.git] / bin / fs-setup
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: fs-setup,v 1.11 1999-01-17 03:11:52 ivan Exp $
4 #
5 # ivan@sisd.com 97-nov-8,9
6 #
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
10 #
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
12 #
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
14 #
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
17 #
18 # finished up ivan@sisd.com 98-jun-1
19 #
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
24 #
25 # ss is 11 chars ivan@sisd.com 98-jul-20
26 #
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
28 #
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
31 #
32 # fix radius attributes ivan@sisd.com 98-sep-27
33 #
34 # $Log: fs-setup,v $
35 # Revision 1.11  1999-01-17 03:11:52  ivan
36 # remove preliminary completehost changes
37 #
38 # Revision 1.10  1998/12/16 06:05:38  ivan
39 # add table cust_main_invoice
40 #
41 # Revision 1.9  1998/12/15 04:36:29  ivan
42 # s/croak/die/; #oops
43 #
44 # Revision 1.8  1998/12/15 04:33:27  ivan
45 # dies if it isn't running as the freeside user
46 #
47 # Revision 1.7  1998/11/18 09:01:31  ivan
48 # i18n! i18n!
49 #
50 # Revision 1.6  1998/11/15 13:18:02  ivan
51 # remove debugging
52 #
53 # Revision 1.5  1998/11/15 09:43:03  ivan
54 # update for new config file syntax, new adminsuidsetup
55 #
56 # Revision 1.4  1998/10/22 15:51:23  ivan
57 # also varchar with no length specified - postgresql fix broke mysql.
58 #
59 # Revision 1.3  1998/10/22 15:46:28  ivan
60 # now smallint is illegal, so remove that too.
61 #
62
63 #to delay loading dbdef until we're ready
64 BEGIN { $FS::Record::setup_hack = 1; }
65
66 use strict;
67 use DBI;
68 use FS::dbdef;
69 use FS::UID qw(adminsuidsetup datasrc checkeuid);
70 use FS::Record;
71 use FS::cust_main_county;
72
73 die "Not running uid freeside!" unless checkeuid();
74
75 my $user = shift or die &usage;
76 FS::UID::getsecrets $user;
77
78 #needs to match FS::Record
79 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
80
81 ###
82
83 print "\nEnter the maximum username length: ";
84 my($username_len)=&getvalue;
85
86 print "\n\n", <<END, ":";
87 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
88 for each user.  Enter any additional RADIUS attributes you need to track for
89 each user, separated by whitespace.
90 END
91 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
92
93 sub getvalue {
94   my($x)=scalar(<STDIN>);
95   chop $x;
96   $x;
97 }
98
99 ###
100
101 my($char_d) = 80; #default maxlength for text fields
102
103 #my(@date_type)  = ( 'timestamp', '', ''     );
104 my(@date_type)  = ( 'int', 'NULL', ''     );
105 my(@perl_type) = ( 'varchar', 'NULL', 255  ); 
106 my(@money_type);
107 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
108   @money_type = ( 'money',   '', '' );
109 } else {
110   @money_type = ( 'decimal',   '', '10,2' );
111 }
112
113 ###
114 # create a dbdef object from the old data structure
115 ###
116
117 my(%tables)=&tables_hash_hack;
118
119 #turn it into objects
120 my($dbdef) = new FS::dbdef ( map {  
121   my(@columns);
122   while (@{$tables{$_}{'columns'}}) {
123     my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
124     push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
125   }
126   FS::dbdef_table->new(
127     $_,
128     $tables{$_}{'primary_key'},
129     #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
130     #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
131     FS::dbdef_unique->new($tables{$_}{'unique'}),
132     FS::dbdef_index->new($tables{$_}{'index'}),
133     @columns,
134   );
135 } (keys %tables) );
136
137 #add radius attributes to svc_acct
138
139 my($svc_acct)=$dbdef->table('svc_acct');
140
141 my($attribute);
142 foreach $attribute (@attributes) {
143   $svc_acct->addcolumn ( new FS::dbdef_column (
144     'radius_'. $attribute,
145     'varchar',
146     'NULL',
147     $char_d,
148   ));
149 }
150
151 #make part_svc table (but now as object)
152
153 my($part_svc)=$dbdef->table('part_svc');
154
155 #because of svc_acct_pop
156 #foreach (grep /^svc_/, $dbdef->tables) { 
157 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
158 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
159   my($table)=$dbdef->table($_);
160   my($col);
161   foreach $col ( $table->columns ) {
162     next if $col =~ /^svcnum$/;
163     $part_svc->addcolumn( new FS::dbdef_column (
164       $table->name. '__' . $table->column($col)->name,
165       'varchar', #$table->column($col)->type, 
166       'NULL',
167       $char_d, #$table->column($col)->length,
168     ));
169     $part_svc->addcolumn ( new FS::dbdef_column (
170       $table->name. '__'. $table->column($col)->name . "_flag",
171       'char',
172       'NULL',
173       1,
174     ));
175   }
176 }
177
178 #important
179 $dbdef->save($dbdef_file);
180 FS::Record::reload_dbdef($dbdef_file);
181
182 ###
183 # create 'em
184 ###
185
186 my($dbh)=adminsuidsetup $user;
187
188 #create tables
189 $|=1;
190
191 my($table);
192 foreach  ($dbdef->tables) {
193   my($table)=$dbdef->table($_);
194   print "Creating $_...";
195
196   my($statement);
197
198   #create table
199   foreach $statement ($table->sql_create_table(datasrc)) {
200     #print $statement, "\n"; 
201     $dbh->do( $statement )
202       or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
203   }
204
205   print "\n";
206 }
207
208 #not really sample data (and shouldn't default to US)
209
210 #cust_main_county
211
212 #USPS state codes
213 foreach ( qw(
214 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
215 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 
216 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
217 ) ) {
218   my($cust_main_county)=create FS::cust_main_county({
219     'state' => $_,
220     'tax'   => 0,
221     'country' => 'US',
222   });  
223   my($error);
224   $error=$cust_main_county->insert;
225   die $error if $error;
226 }
227
228 #ISO 2-letter country codes (same as country TLDs) except US
229 foreach ( qw(
230 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
231 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
232 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
233 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
234 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
235 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
236 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
237 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
238 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
239 YE YU ZR ZM ZW
240 ) ) {
241   my($cust_main_county)=create FS::cust_main_county({
242     'tax'   => 0,
243     'country' => $_,
244   });  
245   my($error);
246   $error=$cust_main_county->insert;
247   die $error if $error;
248 }
249
250 $dbh->disconnect or die $dbh->errstr;
251
252 sub usage {
253   die "Usage:\n  fs-setup user\n"; 
254 }
255
256 ###
257 # Now it becomes an object.  much better.
258 ###
259 sub tables_hash_hack {
260
261   #note that s/(date|change)/_$1/; to avoid keyword conflict.
262   #put a kludge in FS::Record to catch this or? (pry need some date-handling
263   #stuff anyway also)
264
265   my(%tables)=( #yech.}
266
267     'agent' => {
268       'columns' => [
269         'agentnum', 'int',            '',     '',
270         'agent',    'varchar',           '',     $char_d,
271         'typenum',  'int',            '',     '',
272         'freq',     'int',       'NULL', '',
273         'prog',     @perl_type,
274       ],
275       'primary_key' => 'agentnum',
276       'unique' => [ [] ],
277       'index' => [ ['typenum'] ],
278     },
279
280     'agent_type' => {
281       'columns' => [
282         'typenum',   'int',  '', '',
283         'atype',     'varchar', '', $char_d,
284       ],
285       'primary_key' => 'typenum',
286       'unique' => [ [] ],
287       'index' => [ [] ],
288     },
289
290     'type_pkgs' => {
291       'columns' => [
292         'typenum',   'int',  '', '',
293         'pkgpart',   'int',  '', '',
294       ],
295       'primary_key' => '',
296       'unique' => [ ['typenum', 'pkgpart'] ],
297       'index' => [ ['typenum'] ],
298     },
299
300     'cust_bill' => {
301       'columns' => [
302         'invnum',    'int',  '', '',
303         'custnum',   'int',  '', '',
304         '_date',     @date_type,
305         'charged',   @money_type,
306         'owed',      @money_type,
307         'printed',   'int',  '', '',
308       ],
309       'primary_key' => 'invnum',
310       'unique' => [ [] ],
311       'index' => [ ['custnum'] ],
312     },
313
314     'cust_bill_pkg' => {
315       'columns' => [
316         'pkgnum',  'int', '', '',
317         'invnum',  'int', '', '',
318         'setup',   @money_type,
319         'recur',   @money_type,
320         'sdate',   @date_type,
321         'edate',   @date_type,
322       ],
323       'primary_key' => '',
324       'unique' => [ ['pkgnum', 'invnum'] ],
325       'index' => [ ['invnum'] ],
326     },
327
328     'cust_credit' => {
329       'columns' => [
330         'crednum',  'int', '', '',
331         'custnum',  'int', '', '',
332         '_date',    @date_type,
333         'amount',   @money_type,
334         'credited', @money_type,
335         'otaker',   'varchar', '', 8,
336         'reason',   'varchar', '', 255,
337       ],
338       'primary_key' => 'crednum',
339       'unique' => [ [] ],
340       'index' => [ ['custnum'] ],
341     },
342
343     'cust_main' => {
344       'columns' => [
345         'custnum',  'int',  '',     '',
346         'agentnum', 'int',  '',     '',
347 #        'titlenum', 'int',  'NULL',   '',
348         'last',     'varchar', '',     $char_d,
349 #        'middle',   'varchar', 'NULL', $char_d,
350         'first',    'varchar', '',     $char_d,
351         'ss',       'char', 'NULL', 11,
352         'company',  'varchar', 'NULL', $char_d,
353         'address1', 'varchar', '',     $char_d,
354         'address2', 'varchar', 'NULL', $char_d,
355         'city',     'varchar', '',     $char_d,
356         'county',   'varchar', 'NULL', $char_d,
357         'state',    'varchar', '',     $char_d,
358         'zip',      'varchar', '',     10,
359         'country',  'char', '',     2,
360         'daytime',  'varchar', 'NULL', 20,
361         'night',    'varchar', 'NULL', 20,
362         'fax',      'varchar', 'NULL', 12,
363         'payby',    'char', '',     4,
364         'payinfo',  'varchar', 'NULL', 16,
365         'paydate',  @date_type,
366         'payname',  'varchar', 'NULL', $char_d,
367         'tax',      'char', 'NULL', 1,
368         'otaker',   'varchar', '',     8,
369         'refnum',   'int',  '',     '',
370       ],
371       'primary_key' => 'custnum',
372       'unique' => [ [] ],
373       #'index' => [ ['last'], ['company'] ],
374       'index' => [ ['last'], ],
375     },
376
377     'cust_main_invoice' => {
378       'columns' => [
379         'destnum',  'int',  '',     '',
380         'custnum',  'int',  '',     '',
381         'dest',     'varchar', '',  $char_d,
382       ],
383       'primary_key' => 'destnum',
384       'unique' => [ [] ],
385       'index' => [ ['custnum'], ],
386     },
387
388     'cust_main_county' => { #county+state+country are checked off the
389                             #cust_main_county for validation and to provide
390                             # a tax rate.
391       'columns' => [
392         'taxnum',   'int',   '',    '',
393         'state',    'char',  'NULL',    $char_d,
394         'county',   'varchar',  'NULL',    $char_d,
395         'country',  'char',  '', 2, 
396         'tax',      'real',  '',    '', #tax %
397       ],
398       'primary_key' => 'taxnum',
399       'unique' => [ [] ],
400   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
401       'index' => [ [] ],
402     },
403
404     'cust_pay' => {
405       'columns' => [
406         'paynum',   'int',    '',   '',
407         'invnum',   'int',    '',   '',
408         'paid',     @money_type,
409         '_date',    @date_type,
410         'payby',    'char',   '',     4, # CARD/BILL/COMP, should be index into
411                                          # payment type table.
412         'payinfo',  'varchar',   'NULL', 16,  #see cust_main above
413         'paybatch', 'varchar',   'NULL', $char_d, #for auditing purposes.
414       ],
415       'primary_key' => 'paynum',
416       'unique' => [ [] ],
417       'index' => [ ['invnum'] ],
418     },
419
420     'cust_pay_batch' => { #what's this used for again?  list of customers
421                           #in current CARD batch? (necessarily CARD?)
422       'columns' => [
423         'invnum',   'int',    '',   '',
424         'custnum',   'int',    '',   '',
425         'last',     'varchar', '',     $char_d,
426         'first',    'varchar', '',     $char_d,
427         'address1', 'varchar', '',     $char_d,
428         'address2', 'varchar', 'NULL', $char_d,
429         'city',     'varchar', '',     $char_d,
430         'state',    'char', '',     2,
431         'zip',      'varchar', '',     10,
432         'country',  'char', '',     2,
433         'trancode', 'int', '', '',
434         'cardnum',  'varchar', '',     16,
435         'exp',      @date_type,
436         'payname',  'varchar', 'NULL', $char_d,
437         'amount',   @money_type,
438       ],
439       'primary_key' => '',
440       'unique' => [ [] ],
441       'index' => [ ['invnum'], ['custnum'] ],
442     },
443
444     'cust_pkg' => {
445       'columns' => [
446         'pkgnum',    'int',    '',   '',
447         'custnum',   'int',    '',   '',
448         'pkgpart',   'int',    '',   '',
449         'otaker',    'varchar', '', 8,
450         'setup',     @date_type,
451         'bill',      @date_type,
452         'susp',      @date_type,
453         'cancel',    @date_type,
454         'expire',    @date_type,
455       ],
456       'primary_key' => 'pkgnum',
457       'unique' => [ [] ],
458       'index' => [ ['custnum'] ],
459     },
460
461     'cust_refund' => {
462       'columns' => [
463         'refundnum',    'int',    '',   '',
464         'crednum',      'int',    '',   '',
465         '_date',        @date_type,
466         'refund',       @money_type,
467         'otaker',       'varchar',   '',   8,
468         'reason',       'varchar',   '',   $char_d,
469         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
470                                              # into payment type table.
471         'payinfo',      'varchar',   'NULL', 16,  #see cust_main above
472       ],
473       'primary_key' => 'refundnum',
474       'unique' => [ [] ],
475       'index' => [ ['crednum'] ],
476     },
477
478     'cust_svc' => {
479       'columns' => [
480         'svcnum',    'int',    '',   '',
481         'pkgnum',    'int',    '',   '',
482         'svcpart',   'int',    '',   '',
483       ],
484       'primary_key' => 'svcnum',
485       'unique' => [ [] ],
486       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
487     },
488
489     'part_pkg' => {
490       'columns' => [
491         'pkgpart',    'int',    '',   '',
492         'pkg',        'varchar',   '',   $char_d,
493         'comment',    'varchar',   '',   $char_d,
494         'setup',      @perl_type,
495         'freq',       'int', '', '',  #billing frequency (months)
496         'recur',      @perl_type,
497       ],
498       'primary_key' => 'pkgpart',
499       'unique' => [ [] ],
500       'index' => [ [] ],
501     },
502
503 #    'part_title' => {
504 #      'columns' => [
505 #        'titlenum',   'int',    '',   '',
506 #        'title',      'varchar',   '',   $char_d,
507 #      ],
508 #      'primary_key' => 'titlenum',
509 #      'unique' => [ [] ],
510 #      'index' => [ [] ],
511 #    },
512
513     'pkg_svc' => {
514       'columns' => [
515         'pkgpart',    'int',    '',   '',
516         'svcpart',    'int',    '',   '',
517         'quantity',   'int',    '',   '',
518       ],
519       'primary_key' => '',
520       'unique' => [ ['pkgpart', 'svcpart'] ],
521       'index' => [ ['pkgpart'] ],
522     },
523
524     'part_referral' => {
525       'columns' => [
526         'refnum',   'int',    '',   '',
527         'referral', 'varchar',   '',   $char_d,
528       ],
529       'primary_key' => 'refnum',
530       'unique' => [ [] ],
531       'index' => [ [] ],
532     },
533
534     'part_svc' => {
535       'columns' => [
536         'svcpart',    'int',    '',   '',
537         'svc',        'varchar',   '',   $char_d,
538         'svcdb',      'varchar',   '',   $char_d,
539       ],
540       'primary_key' => 'svcpart',
541       'unique' => [ [] ],
542       'index' => [ [] ],
543     },
544
545     #(this should be renamed to part_pop)
546     'svc_acct_pop' => {
547       'columns' => [
548         'popnum',    'int',    '',   '',
549         'city',      'varchar',   '',   $char_d,
550         'state',     'char',   '',   2,
551         'ac',        'char',   '',   3,
552         'exch',      'char',   '',   3,
553         #rest o' number?
554       ],
555       'primary_key' => 'popnum',
556       'unique' => [ [] ],
557       'index' => [ [] ],
558     },
559
560     'svc_acct' => {
561       'columns' => [
562         'svcnum',    'int',    '',   '',
563         'username',  'varchar',   '',   $username_len, #unique (& remove dup code)
564         '_password', 'varchar',   '',   25, #13 for encryped pw's plus ' *SUSPENDED*
565         'popnum',    'int',    'NULL',   '',
566         'uid',       'int', 'NULL',   '',
567         'gid',       'int', 'NULL',   '',
568         'finger',    'varchar',   'NULL',   $char_d,
569         'dir',       'varchar',   'NULL',   $char_d,
570         'shell',     'varchar',   'NULL',   $char_d,
571         'quota',     'varchar',   'NULL',   $char_d,
572         'slipip',    'varchar',   'NULL',   15, #four TINYINTs, bah.
573       ],
574       'primary_key' => 'svcnum',
575       'unique' => [ [] ],
576       'index' => [ ['username'] ],
577     },
578
579     'svc_acct_sm' => {
580       'columns' => [
581         'svcnum',    'int',    '',   '',
582         'domsvc',    'int',    '',   '',
583         'domuid',    'int', '',   '',
584         'domuser',   'varchar',   '',   $char_d,
585       ],
586       'primary_key' => 'svcnum',
587       'unique' => [ [] ],
588       'index' => [ ['domsvc'], ['domuid'] ], 
589     },
590
591     #'svc_charge' => {
592     #  'columns' => [
593     #    'svcnum',    'int',    '',   '',
594     #    'amount',    @money_type,
595     #  ],
596     #  'primary_key' => 'svcnum',
597     #  'unique' => [ [] ],
598     #  'index' => [ [] ],
599     #},
600
601     'svc_domain' => {
602       'columns' => [
603         'svcnum',    'int',    '',   '',
604         'domain',    'varchar',    '',   $char_d,
605       ],
606       'primary_key' => 'svcnum',
607       'unique' => [ ['domain'] ],
608       'index' => [ [] ],
609     },
610
611     #'svc_wo' => {
612     #  'columns' => [
613     #    'svcnum',    'int',    '',   '',
614     #    'svcnum',    'int',    '',   '',
615     #    'svcnum',    'int',    '',   '',
616     #    'worker',    'varchar',   '',   $char_d,
617     #    '_date',     @date_type,
618     #  ],
619     #  'primary_key' => 'svcnum',
620     #  'unique' => [ [] ],
621     #  'index' => [ [] ],
622     #},
623
624   );
625
626   %tables;
627
628 }
629