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