leading summary page invoices #RT5086
[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     'reason'          => [],
103     'cust_pkg_reason' => [],
104
105     #need part_pkg before cust_credit...
106     'part_pkg' => [],
107
108     #customer credits
109     'cust_credit' => [],
110
111     #duplicate history records
112     'h_cust_svc'  => [],
113
114     #populate cust_pay.otaker
115     'cust_pay'    => [],
116
117     #populate part_pkg_taxclass for starters
118     'part_pkg_taxclass' => [],
119
120     #remove bad pending records
121     'cust_pay_pending' => [],
122
123     #replace invnum and pkgnum with billpkgnum
124     'cust_bill_pkg_detail' => [],
125
126     #usage_classes if we have none
127     'usage_class' => [],
128
129     #fixup access rights
130     'access_right' => [],
131
132     #change tax_rate column types
133     'tax_rate' => [],
134
135     #change recur_flat and enable_prorate
136     'part_pkg_option' => [],
137
138     #add weights to pkg_category
139     'pkg_category' => [],
140
141   ;
142
143   \%hash;
144
145 }
146
147 sub upgrade_sqlradius {
148   #my %opt = @_;
149
150   my $conf = new FS::Conf;
151
152   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
153
154   foreach my $part_export ( @part_export ) {
155
156     my $errmsg = 'Error adding FreesideStatus to '.
157                  $part_export->option('datasrc'). ': ';
158
159     my $dbh = DBI->connect(
160       ( map $part_export->option($_), qw ( datasrc username password ) ),
161       { PrintError => 0, PrintWarn => 0 }
162     ) or do {
163       warn $errmsg.$DBI::errstr;
164       next;
165     };
166
167     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
168     my $group = "UserName";
169     $group .= ",Realm"
170       if ( ref($part_export) =~ /withdomain/ );
171
172     my $sth_alter = $dbh->prepare(
173       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
174     );
175     if ( $sth_alter ) {
176       if ( $sth_alter->execute ) {
177         my $sth_update = $dbh->prepare(
178          "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
179         ) or die $errmsg.$dbh->errstr;
180         $sth_update->execute or die $errmsg.$sth_update->errstr;
181       } else {
182         my $error = $sth_alter->errstr;
183         warn $errmsg.$error unless $error =~ /Duplicate column name/i;
184       }
185     } else {
186       my $error = $dbh->errstr;
187       warn $errmsg.$error; #unless $error =~ /exists/i;
188     }
189
190     my $sth_index = $dbh->prepare(
191       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
192     );
193     if ( $sth_index ) {
194       unless ( $sth_index->execute ) {
195         my $error = $sth_index->errstr;
196         warn $errmsg.$error unless $error =~ /Duplicate key name/i;
197       }
198     } else {
199       my $error = $dbh->errstr;
200       warn $errmsg.$error; #unless $error =~ /exists/i;
201     }
202
203     my $sth = $dbh->prepare("SELECT UserName,
204                                     Realm,
205                                     $str2time max(AcctStartTime)),
206                                     $str2time max(AcctStopTime))
207                               FROM radacct
208                               WHERE FreesideStatus = 'done'
209                                 AND AcctStartTime != 0
210                                 AND AcctStopTime  != 0
211                               GROUP BY $group
212                             ")
213       or die $errmsg.$dbh->errstr;
214     $sth->execute() or die $errmsg.$sth->errstr;
215   
216     while (my $row = $sth->fetchrow_arrayref ) {
217       my ($username, $realm, $start, $stop) = @$row;
218   
219       $username = lc($username) unless $conf->exists('username-uppercase');
220
221       my $exportnum = $part_export->exportnum;
222       my $extra_sql = " AND exportnum = $exportnum ".
223                       " AND exportsvcnum IS NOT NULL ";
224
225       if ( ref($part_export) =~ /withdomain/ ) {
226         $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
227                          WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
228       }
229   
230       my $svc_acct = qsearchs({
231         'select'    => 'svc_acct.*',
232         'table'     => 'svc_acct',
233         'addl_from' => 'LEFT JOIN cust_svc   USING ( svcnum )'.
234                        'LEFT JOIN export_svc USING ( svcpart )',
235         'hashref'   => { 'username' => $username },
236         'extra_sql' => $extra_sql,
237       });
238
239       if ($svc_acct) {
240         $svc_acct->last_login($start)
241           if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
242         $svc_acct->last_logout($stop)
243           if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
244       }
245     }
246   }
247
248 }
249
250 =back
251
252 =head1 BUGS
253
254 Sure.
255
256 =head1 SEE ALSO
257
258 =cut
259
260 1;
261