summaryrefslogtreecommitdiff
path: root/FS/bin/freeside-setup
blob: a16e517490f26c1cb8056d2ff542bb3ec1b0b3c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl -Tw

#to delay loading dbdef until we're ready
BEGIN { $FS::Schema::setup_hack = 1; }

use strict;
use vars qw($opt_s);
use Getopt::Std;
use Locale::Country;
use Locale::SubCountry;
use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
use FS::Schema qw( dbdef_dist reload_dbdef );
use FS::Record;
use FS::cust_main_county;
#use FS::raddb;
use FS::part_bill_event;

die "Not running uid freeside!" unless checkeuid();

#my %attrib2db =
#  map { lc($FS::raddb::attrib{$_}) => $_ } keys %FS::raddb::attrib;

getopts("s");
my $user = shift or die &usage;
getsecrets($user);

#needs to match FS::Record
my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;

###

#print "\nEnter the maximum username length: ";
#my($username_len)=&getvalue;
my $username_len = 32; #usernamemax config file

#print "\n\n", <<END, ":";
#Freeside tracks the RADIUS User-Name, check attribute Password and
#reply attribute Framed-IP-Address for each user.  You can specify additional
#check and reply attributes (or you can add them later with the
#fs-radius-add-check and fs-radius-add-reply programs).
#
#First enter any additional RADIUS check attributes you need to track for each 
#user, separated by whitespace.
#END
#my @check_attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
#                         split(" ",&getvalue);
#
#print "\n\n", <<END, ":";
#Now enter any additional reply attributes you need to track for each user,
#separated by whitespace.
#END
#my @attributes = map { $attrib2db{lc($_)} or die "unknown attribute $_"; }
#                   split(" ",&getvalue);
#
#print "\n\n", <<END, ":";
#Do you wish to enable the tracking of a second, separate shipping/service
#address?
#END
#my $ship = &_yesno;
#
#sub getvalue {
#  my($x)=scalar(<STDIN>);
#  chop $x;
#  $x;
#}
#
#sub _yesno {
#  print " [y/N]:";
#  my $x = scalar(<STDIN>);
#  $x =~ /^y/i;
#}

#my @check_attributes = (); #add later
#my @attributes = (); #add later
#my $ship = $opt_s;

###
# create a dbdef object from the old data structure
###

my $dbdef = dbdef_dist;

#important
$dbdef->save($dbdef_file);
&FS::Schema::reload_dbdef($dbdef_file);

###
# create 'em
###

my $dbh = adminsuidsetup $user;

#create tables
$|=1;

foreach my $statement ( $dbdef->sql($dbh) ) {
  $dbh->do( $statement )
    or die "CREATE error: ". $dbh->errstr. "\ndoing statement: $statement";
}

#now go back and reverse engineer the db
#so we pick up the correct column DEFAULTs for #oidless inserts
dbdef_create($dbh, $dbdef_file);
delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
reload_dbdef($dbdef_file);

#cust_main_county
foreach my $country ( sort map uc($_), all_country_codes ) {

  my $subcountry = eval { new Locale::SubCountry($country) };
  my @states = $subcountry ? $subcountry->all_codes : undef;

  if ( !scalar(@states) || ( scalar(@states) == 1 && !defined($states[0]) ) ) {

    my $cust_main_county = new FS::cust_main_county({
      'tax'   => 0,
      'country' => $country,
    });  
    my $error = $cust_main_county->insert;
    die $error if $error;

  } else {

    if ( $states[0] =~ /^(\d+|\w)$/ ) {
      @states = map $subcountry->full_name($_), @states
    }

    foreach my $state ( @states ) {

      my $cust_main_county = new FS::cust_main_county({
        'state' => $state,
        'tax'   => 0,
        'country' => $country,
      });  
      my $error = $cust_main_county->insert;
      die $error if $error;

    }
  
  }
}

#billing events
foreach my $aref ( 
  #[ 'COMP', 'Comp invoice', '$cust_bill->comp();', 30, 'comp' ],
  [ 'CARD', 'Batch card', '$cust_bill->batch_card();', 40, 'batch-card' ],
  [ 'BILL', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
  [ 'DCRD', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
  [ 'DCHK', 'Send invoice', '$cust_bill->send();', 50, 'send' ],
) {

  my $part_bill_event = new FS::part_bill_event({
    'payby' => $aref->[0],
    'event' => $aref->[1],
    'eventcode' => $aref->[2],
    'seconds' => 0,
    'weight' => $aref->[3],
    'plan' => $aref->[4],
  });
  my($error);
  $error=$part_bill_event->insert;
  die $error if $error;

}

$dbh->commit or die $dbh->errstr;
$dbh->disconnect or die $dbh->errstr;

#print "Freeside database initialized sucessfully\n";

sub dbdef_create { # reverse engineer the schema from the DB and save to file
  my( $dbh, $file ) = @_;
  my $dbdef = new_native DBIx::DBSchema $dbh;
  $dbdef->save($file);
}

sub usage {
  die "Usage:\n  freeside-setup user\n"; 
}

1;