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