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