3 #to delay loading dbdef until we're ready
4 BEGIN { $FS::Schema::setup_hack = 1; }
10 use Locale::SubCountry;
11 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
12 use FS::Schema qw( dbdef_dist reload_dbdef );
14 use FS::cust_main_county;
16 use FS::part_bill_event;
18 die "Not running uid freeside!" unless checkeuid();
21 # map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;
24 my $user = shift or die &usage;
27 #needs to match FS::Record
28 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
32 #print "\nEnter the maximum username length: ";
33 #my($username_len)=&getvalue;
34 my $username_len = 32; #usernamemax config file
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).
42 #First enter any additional RADIUS check attributes you need to track for each
43 #user, separated by whitespace.
45 #my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
46 # split(" ",&getvalue);
48 #print "\n\n", <<END, ":";
49 #Now enter any additional reply attributes you need to track for each user,
50 #separated by whitespace.
52 #my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
53 # split(" ",&getvalue);
55 #print "\n\n", <<END, ":";
56 #Do you wish to enable the tracking of a second, separate shipping/service
62 # my($x)=scalar(<STDIN>);
69 # my $x = scalar(<STDIN>);
73 #my @check_attributes = (); #add later
74 #my @attributes = (); #add later
78 # create a dbdef object from the old data structure
81 my $dbdef = dbdef_dist;
84 $dbdef->save($dbdef_file);
85 &FS::Schema::reload_dbdef($dbdef_file);
91 my $dbh = adminsuidsetup $user;
96 foreach my $statement ( $dbdef->sql($dbh) ) {
97 $dbh->do( $statement )
98 or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
101 #now go back and reverse engineer the db
102 #so we pick up the correct column DEFAULTs for #oidless inserts
103 dbdef_create($dbh, $dbdef_file);
104 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
105 reload_dbdef($dbdef_file);
108 foreach my $country ( sort map uc($_), all_country_codes ) {
110 my $subcountry = eval { new Locale::SubCountry($country) };
111 my @states = $subcountry ? $subcountry->all_codes : undef;
113 if ( !scalar(@states) || ( scalar(@states) == 1 && !defined($states[0]) ) ) {
115 my $cust_main_county = new FS::cust_main_county({
117 'country' => $country,
119 my $error = $cust_main_county->insert;
120 die $error if $error;
124 if ( $states[0] =~ /^(\d+|\w)$/ ) {
125 @states = map $subcountry->full_name($_), @states
128 foreach my $state ( @states ) {
130 my $cust_main_county = new FS::cust_main_county({
133 'country' => $country,
135 my $error = $cust_main_county->insert;
136 die $error if $error;
145 #[ 'COMP', 'Comp invoice', '$cust_bill->comp();', 30, 'comp' ],
146 [ 'CARD', 'Batch card', '$cust_bill->batch_card();', 40, 'batch-card' ],
147 [ 'BILL', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
148 [ 'DCRD', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
149 [ 'DCHK', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
152 my $part_bill_event = new FS::part_bill_event({
153 'payby' => $aref->[0],
154 'event' => $aref->[1],
155 'eventcode' => $aref->[2],
157 'weight' => $aref->[3],
158 'plan' => $aref->[4],
161 $error=$part_bill_event->insert;
162 die $error if $error;
166 $dbh->commit or die $dbh->errstr;
167 $dbh->disconnect or die $dbh->errstr;
169 #print "Freeside database initialized sucessfully\n";
171 sub dbdef_create { # reverse engineer the schema from the DB and save to file
172 my( $dbh, $file ) = @_;
173 my $dbdef = new_native DBIx::DBSchema $dbh;
178 die "Usage:\n freeside-setup user\n";