This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / FS / bin / freeside-setup
1 #!/usr/bin/perl -Tw
2
3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
5
6 use strict;
7 use vars qw($opt_s);
8 use Getopt::Std;
9 use Locale::Country;
10 use Locale::SubCountry;
11 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
12 use FS::Schema qw( dbdef_dist );
13 use FS::Record;
14 use FS::cust_main_county;
15 #use FS::raddb;
16 use FS::part_bill_event;
17
18 die "Not running uid freeside!" unless checkeuid();
19
20 #my %attrib2db =
21 #  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
22
23 getopts("s");
24 my $user = shift or die &usage;
25 getsecrets($user);
26
27 #needs to match FS::Record
28 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
29
30 ###
31
32 #print "\nEnter the maximum username length: ";
33 #my($username_len)=&getvalue;
34 my $username_len = 32; #usernamemax config file
35
36 #print "\n\n", <<END, ":";
37 #Freeside tracks the RADIUS User-Name, check attribute Password and
38 #reply attribute Framed-IP-Address for each user.  You can specify additional
39 #check and reply attributes (or you can add them later with the
40 #fs-radius-add-check and fs-radius-add-reply programs).
41 #
42 #First enter any additional RADIUS check attributes you need to track for each 
43 #user, separated by whitespace.
44 #END
45 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
46 #                         split(" ",&getvalue);
47 #
48 #print "\n\n", <<END, ":";
49 #Now enter any additional reply attributes you need to track for each user,
50 #separated by whitespace.
51 #END
52 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
53 #                   split(" ",&getvalue);
54 #
55 #print "\n\n", <<END, ":";
56 #Do you wish to enable the tracking of a second, separate shipping/service
57 #address?
58 #END
59 #my $ship = &_yesno;
60 #
61 #sub getvalue {
62 #  my($x)=scalar(<STDIN>);
63 #  chop $x;
64 #  $x;
65 #}
66 #
67 #sub _yesno {
68 #  print " [y/N]:";
69 #  my $x = scalar(<STDIN>);
70 #  $x =~ /^y/i;
71 #}
72
73 #my @check_attributes = (); #add later
74 #my @attributes = (); #add later
75 #my $ship = $opt_s;
76
77 ###
78 # create a dbdef object from the old data structure
79 ###
80
81 my $dbdef = dbdef_dist;
82
83 #important
84 $dbdef->save($dbdef_file);
85 &FS::Schema::reload_dbdef($dbdef_file);
86
87 ###
88 # create 'em
89 ###
90
91 my $dbh = adminsuidsetup $user;
92
93 #create tables
94 $|=1;
95
96 foreach my $statement ( $dbdef->sql($dbh) ) {
97   $dbh->do( $statement )
98     or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
99 }
100
101 #cust_main_county
102 foreach my $country ( sort map uc($_), all_country_codes ) {
103
104   my $subcountry = eval { new Locale::SubCountry($country) };
105   my @states = $subcountry ? $subcountry->all_codes : undef;
106
107   if ( !scalar(@states) || ( scalar(@states) == 1 && !defined($states[0]) ) ) {
108
109     my $cust_main_county = new FS::cust_main_county({
110       'tax'   => 0,
111       'country' => $country,
112     });  
113     my $error = $cust_main_county->insert;
114     die $error if $error;
115
116   } else {
117
118     if ( $states[0] =~ /^(\d+|\w)$/ ) {
119       @states = map $subcountry->full_name($_), @states
120     }
121
122     foreach my $state ( @states ) {
123
124       my $cust_main_county = new FS::cust_main_county({
125         'state' => $state,
126         'tax'   => 0,
127         'country' => $country,
128       });  
129       my $error = $cust_main_county->insert;
130       die $error if $error;
131
132     }
133   
134   }
135 }
136
137 #billing events
138 foreach my $aref ( 
139   #[ 'COMP', 'Comp invoice', '$cust_bill->comp();', 30, 'comp' ],
140   [ 'CARD', 'Batch card', '$cust_bill->batch_card();', 40, 'batch-card' ],
141   [ 'BILL', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
142   [ 'DCRD', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
143   [ 'DCHK', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
144 ) {
145
146   my $part_bill_event = new FS::part_bill_event({
147     'payby' => $aref->[0],
148     'event' => $aref->[1],
149     'eventcode' => $aref->[2],
150     'seconds' => 0,
151     'weight' => $aref->[3],
152     'plan' => $aref->[4],
153   });
154   my($error);
155   $error=$part_bill_event->insert;
156   die $error if $error;
157
158 }
159
160 $dbh->commit or die $dbh->errstr;
161 $dbh->disconnect or die $dbh->errstr;
162
163 #print "Freeside database initialized sucessfully\n";
164
165 sub usage {
166   die "Usage:\n  freeside-setup user\n"; 
167 }
168
169 1;
170