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