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