system only reason update routine
[freeside.git] / FS / FS / Upgrade.pm
1 package FS::Upgrade;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK );
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 =head1 NAME
18
19 FS::Upgrade - Database upgrade routines
20
21 =head1 SYNOPSIS
22
23   use FS::Upgrade;
24
25 =head1 DESCRIPTION
26
27 Currently this module simply provides a place to store common subroutines for
28 database upgrades.
29
30 =head1 SUBROUTINES
31
32 =over 4
33
34 =item
35
36 =cut
37
38 sub upgrade {
39   my %opt = @_;
40
41   my $oldAutoCommit = $FS::UID::AutoCommit;
42   local $FS::UID::AutoCommit = 0;
43   $FS::UID::AutoCommit = 0;
44
45   my $data = upgrade_data(%opt);
46
47   foreach my $table ( keys %$data ) {
48
49     my $class = "FS::$table";
50     eval "use $class;";
51     die $@ if $@;
52
53     if ( $class->can('_upgrade_data') ) {
54       $class->_upgrade_data(%opt);
55     } else {
56       warn "WARNING: asked for upgrade of $table,".
57            " but FS::$table has no _upgrade_data method\n";
58     }
59
60 #    my @records = @{ $data->{$table} };
61 #
62 #    foreach my $record ( @records ) {
63 #      my $args = delete($record->{'_upgrade_args'}) || [];
64 #      my $object = $class->new( $record );
65 #      my $error = $object->insert( @$args );
66 #      die "error inserting record into $table: $error\n"
67 #        if $error;
68 #    }
69
70   }
71
72   if ( $oldAutoCommit ) {
73     dbh->commit or die dbh->errstr;
74   }
75
76 }
77
78
79 sub upgrade_data {
80   my %opt = @_;
81
82   tie my %hash, 'Tie::IxHash', 
83
84     #reason type and reasons
85     'reason_type'     => [],
86     'reason'          => [],
87     'cust_pkg_reason' => [],
88
89     #need part_pkg before cust_credit...
90     'part_pkg' => [],
91
92     #customer credits
93     'cust_credit' => [],
94
95     #duplicate history records
96     'h_cust_svc'  => [],
97
98     #populate cust_pay.otaker
99     'cust_pay'    => [],
100
101     #populate part_pkg_taxclass for starters
102     'part_pkg_taxclass' => [],
103
104     #remove bad pending records
105     'cust_pay_pending' => [],
106
107     #replace invnum and pkgnum with billpkgnum
108     'cust_bill_pkg_detail' => [],
109
110     #usage_classes if we have none
111     'usage_class' => [],
112
113   ;
114
115   \%hash;
116
117 }
118
119 sub upgrade_sqlradius {
120   #my %opt = @_;
121
122   my $conf = new FS::Conf;
123
124   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
125
126   foreach my $part_export ( @part_export ) {
127
128     my $errmsg = 'Error adding FreesideStatus to '.
129                  $part_export->option('datasrc'). ': ';
130
131     my $dbh = DBI->connect(
132       ( map $part_export->option($_), qw ( datasrc username password ) ),
133       { PrintError => 0, PrintWarn => 0 }
134     ) or do {
135       warn $errmsg.$DBI::errstr;
136       next;
137     };
138
139     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
140     my $group = "UserName";
141     $group .= ",Realm"
142       if ( ref($part_export) =~ /withdomain/ );
143
144     my $sth_alter = $dbh->prepare(
145       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
146     );
147     if ( $sth_alter ) {
148       if ( $sth_alter->execute ) {
149         my $sth_update = $dbh->prepare(
150          "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
151         ) or die $errmsg.$dbh->errstr;
152         $sth_update->execute or die $errmsg.$sth_update->errstr;
153       } else {
154         my $error = $sth_alter->errstr;
155         warn $errmsg.$error unless $error =~ /Duplicate column name/i;
156       }
157     } else {
158       my $error = $dbh->errstr;
159       warn $errmsg.$error; #unless $error =~ /exists/i;
160     }
161
162     my $sth_index = $dbh->prepare(
163       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
164     );
165     if ( $sth_index ) {
166       unless ( $sth_index->execute ) {
167         my $error = $sth_index->errstr;
168         warn $errmsg.$error unless $error =~ /Duplicate key name/i;
169       }
170     } else {
171       my $error = $dbh->errstr;
172       warn $errmsg.$error; #unless $error =~ /exists/i;
173     }
174
175     my $sth = $dbh->prepare("SELECT UserName,
176                                     Realm,
177                                     $str2time max(AcctStartTime)),
178                                     $str2time max(AcctStopTime))
179                               FROM radacct
180                               WHERE FreesideStatus = 'done'
181                                 AND AcctStartTime != 0
182                                 AND AcctStopTime  != 0
183                               GROUP BY $group
184                             ")
185       or die $errmsg.$dbh->errstr;
186     $sth->execute() or die $errmsg.$sth->errstr;
187   
188     while (my $row = $sth->fetchrow_arrayref ) {
189       my ($username, $realm, $start, $stop) = @$row;
190   
191       $username = lc($username) unless $conf->exists('username-uppercase');
192       my $extra_sql = '';
193       if ( ref($part_export) =~ /withdomain/ ) {
194         $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
195                          WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
196       }
197   
198       my $svc_acct = qsearchs( 'svc_acct',
199                                { 'username' => $username },
200                                '',
201                                $extra_sql,
202                              );
203       if ($svc_acct) {
204         $svc_acct->last_login($start)
205           if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
206         $svc_acct->last_logout($stop)
207           if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
208       }
209     }
210   }
211
212 }
213
214 =back
215
216 =head1 BUGS
217
218 Sure.
219
220 =head1 SEE ALSO
221
222 =cut
223
224 1;
225