communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[freeside.git] / FS / FS / Upgrade.pm
1 package FS::Upgrade;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
5 use Exporter;
6 use Tie::IxHash;
7 use FS::UID qw( dbh driver_name );
8 use FS::Conf;
9 use FS::Record qw(qsearchs str2time_sql);
10
11 use FS::svc_domain;
12 $FS::svc_domain::whois_hack = 1;
13
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( upgrade upgrade_sqlradius );
16
17 $DEBUG = 1;
18
19 =head1 NAME
20
21 FS::Upgrade - Database upgrade routines
22
23 =head1 SYNOPSIS
24
25   use FS::Upgrade;
26
27 =head1 DESCRIPTION
28
29 Currently this module simply provides a place to store common subroutines for
30 database upgrades.
31
32 =head1 SUBROUTINES
33
34 =over 4
35
36 =item
37
38 =cut
39
40 sub upgrade {
41   my %opt = @_;
42
43   my $data = upgrade_data(%opt);
44
45   foreach my $table ( keys %$data ) {
46
47     my $class = "FS::$table";
48     eval "use $class;";
49     die $@ if $@;
50
51     if ( $class->can('_upgrade_data') ) {
52       warn "Upgrading $table...\n";
53
54       my $start = time;
55
56       my $oldAutoCommit = $FS::UID::AutoCommit;
57       local $FS::UID::AutoCommit = 0;
58       $FS::UID::AutoCommit = 0;
59
60       $class->_upgrade_data(%opt);
61
62       if ( $oldAutoCommit ) {
63         dbh->commit or die dbh->errstr;
64       }
65       
66       #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
67       warn "  done in ". (time-$start). " seconds\n";
68
69     } else {
70       warn "WARNING: asked for upgrade of $table,".
71            " but FS::$table has no _upgrade_data method\n";
72     }
73
74 #    my @records = @{ $data->{$table} };
75 #
76 #    foreach my $record ( @records ) {
77 #      my $args = delete($record->{'_upgrade_args'}) || [];
78 #      my $object = $class->new( $record );
79 #      my $error = $object->insert( @$args );
80 #      die "error inserting record into $table: $error\n"
81 #        if $error;
82 #    }
83
84   }
85
86 }
87
88
89 sub upgrade_data {
90   my %opt = @_;
91
92   tie my %hash, 'Tie::IxHash', 
93
94     #cust_main (remove paycvv from history)
95     'cust_main' => [],
96
97     #msgcat
98     'msgcat' => [],
99
100     #reason type and reasons
101     'reason_type'     => [],
102     'cust_pkg_reason' => [],
103
104     #need part_pkg before cust_credit...
105     'part_pkg' => [],
106
107     #customer credits
108     'cust_credit' => [],
109
110     #duplicate history records
111     'h_cust_svc'  => [],
112
113     #populate cust_pay.otaker
114     'cust_pay'    => [],
115
116     #populate part_pkg_taxclass for starters
117     'part_pkg_taxclass' => [],
118
119     #remove bad pending records
120     'cust_pay_pending' => [],
121
122     #replace invnum and pkgnum with billpkgnum
123     'cust_bill_pkg_detail' => [],
124
125     #usage_classes if we have none
126     'usage_class' => [],
127
128     #phone_type if we have none
129     'phone_type' => [],
130
131     #fixup access rights
132     'access_right' => [],
133
134     #change recur_flat and enable_prorate
135     'part_pkg_option' => [],
136
137     #add weights to pkg_category
138     'pkg_category' => [],
139
140     #cdrbatch fixes
141     'cdr' => [],
142
143     #otaker->usernum
144     'cust_attachment' => [],
145     #'cust_credit' => [],
146     #'cust_main' => [],
147     'cust_main_note' => [],
148     #'cust_pay' => [],
149     'cust_pay_void' => [],
150     'cust_pkg' => [],
151     #'cust_pkg_reason' => [],
152     'cust_pkg_discount' => [],
153     'cust_refund' => [],
154     'banned_pay' => [],
155
156   ;
157
158   \%hash;
159
160 }
161
162 sub upgrade_sqlradius {
163   #my %opt = @_;
164
165   my $conf = new FS::Conf;
166
167   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
168
169   foreach my $part_export ( @part_export ) {
170
171     my $errmsg = 'Error adding FreesideStatus to '.
172                  $part_export->option('datasrc'). ': ';
173
174     my $dbh = DBI->connect(
175       ( map $part_export->option($_), qw ( datasrc username password ) ),
176       { PrintError => 0, PrintWarn => 0 }
177     ) or do {
178       warn $errmsg.$DBI::errstr;
179       next;
180     };
181
182     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
183     my $group = "UserName";
184     $group .= ",Realm"
185       if ( ref($part_export) =~ /withdomain/ );
186
187     my $sth_alter = $dbh->prepare(
188       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
189     );
190     if ( $sth_alter ) {
191       if ( $sth_alter->execute ) {
192         my $sth_update = $dbh->prepare(
193          "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
194         ) or die $errmsg.$dbh->errstr;
195         $sth_update->execute or die $errmsg.$sth_update->errstr;
196       } else {
197         my $error = $sth_alter->errstr;
198         warn $errmsg.$error unless $error =~ /Duplicate column name/i;
199       }
200     } else {
201       my $error = $dbh->errstr;
202       warn $errmsg.$error; #unless $error =~ /exists/i;
203     }
204
205     my $sth_index = $dbh->prepare(
206       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
207     );
208     if ( $sth_index ) {
209       unless ( $sth_index->execute ) {
210         my $error = $sth_index->errstr;
211         warn $errmsg.$error unless $error =~ /Duplicate key name/i;
212       }
213     } else {
214       my $error = $dbh->errstr;
215       warn $errmsg.$error; #unless $error =~ /exists/i;
216     }
217
218     my $sth = $dbh->prepare("SELECT UserName,
219                                     Realm,
220                                     $str2time max(AcctStartTime)),
221                                     $str2time max(AcctStopTime))
222                               FROM radacct
223                               WHERE FreesideStatus = 'done'
224                                 AND AcctStartTime != 0
225                                 AND AcctStopTime  != 0
226                               GROUP BY $group
227                             ")
228       or die $errmsg.$dbh->errstr;
229     $sth->execute() or die $errmsg.$sth->errstr;
230   
231     while (my $row = $sth->fetchrow_arrayref ) {
232       my ($username, $realm, $start, $stop) = @$row;
233   
234       $username = lc($username) unless $conf->exists('username-uppercase');
235
236       my $exportnum = $part_export->exportnum;
237       my $extra_sql = " AND exportnum = $exportnum ".
238                       " AND exportsvcnum IS NOT NULL ";
239
240       if ( ref($part_export) =~ /withdomain/ ) {
241         $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
242                          WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
243       }
244   
245       my $svc_acct = qsearchs({
246         'select'    => 'svc_acct.*',
247         'table'     => 'svc_acct',
248         'addl_from' => 'LEFT JOIN cust_svc   USING ( svcnum )'.
249                        'LEFT JOIN export_svc USING ( svcpart )',
250         'hashref'   => { 'username' => $username },
251         'extra_sql' => $extra_sql,
252       });
253
254       if ($svc_acct) {
255         $svc_acct->last_login($start)
256           if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
257         $svc_acct->last_logout($stop)
258           if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
259       }
260     }
261   }
262
263 }
264
265 =back
266
267 =head1 BUGS
268
269 Sure.
270
271 =head1 SEE ALSO
272
273 =cut
274
275 1;
276