4 use vars qw( @ISA @EXPORT_OK $DEBUG );
8 use FS::UID qw( dbh driver_name );
10 use FS::Record qw(qsearchs qsearch str2time_sql);
11 use FS::upgrade_journal;
14 $FS::svc_domain::whois_hack = 1;
16 @ISA = qw( Exporter );
17 @EXPORT_OK = qw( upgrade_schema upgrade_config upgrade upgrade_sqlradius );
23 FS::Upgrade - Database upgrade routines
31 Currently this module simply provides a place to store common subroutines for
46 my $conf = new FS::Conf;
48 $conf->touch('payment_receipt')
49 if $conf->exists('payment_receipt_email')
50 || $conf->config('payment_receipt_msgnum');
52 $conf->touch('geocode-require_nw_coordinates')
53 if $conf->exists('svc_broadband-require-nw-coordinates');
55 unless ( $conf->config('echeck-country') ) {
56 if ( $conf->exists('cust_main-require-bank-branch') ) {
57 $conf->set('echeck-country', 'CA');
58 } elsif ( $conf->exists('echeck-nonus') ) {
59 $conf->set('echeck-country', 'XX');
61 $conf->set('echeck-country', 'US');
65 upgrade_overlimit_groups($conf);
66 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
68 my $DIST_CONF = '/usr/local/etc/freeside/default_conf/';#DIST_CONF in Makefile
69 $conf->set($_, scalar(read_file( "$DIST_CONF/$_" )) )
70 foreach grep { ! $conf->exists($_) && -s "$DIST_CONF/$_" }
71 qw( quotation_html quotation_latex quotation_latexnotes );
73 # change 'fslongtable' to 'longtable'
74 foreach my $name (qw(invoice_latex quotation_latex)) {
75 my $value = join("\n",$conf->config($name));
77 $value =~ s/fslongtable/longtable/g;
78 $conf->set($name, $value);
84 sub upgrade_overlimit_groups {
87 my @groups = $conf->config('overlimit_groups',$agentnum);
89 my $groups = join(',',@groups);
92 if ( $groups !~ /^[\d,]+$/ ) {
93 foreach my $groupname ( @groups ) {
94 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
96 $g = new FS::radius_group {
97 'groupname' => $groupname,
98 'description' => $groupname,
101 die $error if $error;
103 push @groupnums, $g->groupnum;
105 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
117 my $data = upgrade_data(%opt);
119 my $oldAutoCommit = $FS::UID::AutoCommit;
120 local $FS::UID::AutoCommit = 0;
121 local $FS::UID::AutoCommit = 0;
123 foreach my $table ( keys %$data ) {
125 my $class = "FS::$table";
129 if ( $class->can('_upgrade_data') ) {
130 warn "Upgrading $table...\n";
134 $class->_upgrade_data(%opt);
136 if ( $oldAutoCommit ) {
137 warn " committing\n";
138 dbh->commit or die dbh->errstr;
141 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
142 warn " done in ". (time-$start). " seconds\n";
145 warn "WARNING: asked for upgrade of $table,".
146 " but FS::$table has no _upgrade_data method\n";
149 # my @records = @{ $data->{$table} };
151 # foreach my $record ( @records ) {
152 # my $args = delete($record->{'_upgrade_args'}) || [];
153 # my $object = $class->new( $record );
154 # my $error = $object->insert( @$args );
155 # die "error inserting record into $table: $error\n"
161 local($FS::cust_main::ignore_expired_card) = 1;
162 local($FS::cust_main::ignore_illegal_zip) = 1;
163 local($FS::cust_main::ignore_banned_card) = 1;
164 local($FS::cust_main::skip_fuzzyfiles) = 1;
166 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
167 # kind of a weird spot for this, but it's better than duplicating
168 # all this code in each class...
169 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
170 foreach my $table ( @decrypt_tables ) {
171 my @objects = qsearch({
174 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
175 " AND LENGTH(payinfo) > 100",
177 foreach my $object ( @objects ) {
178 my $payinfo = $object->decrypt($object->payinfo);
179 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
180 $object->payinfo($payinfo);
181 my $error = $object->replace;
182 die $error if $error;
195 tie my %hash, 'Tie::IxHash',
197 #cust_main (remove paycvv from history)
203 #reason type and reasons
205 'cust_pkg_reason' => [],
207 #need part_pkg before cust_credit...
213 #duplicate history records
216 #populate cust_pay.otaker
219 #populate part_pkg_taxclass for starters
220 'part_pkg_taxclass' => [],
222 #remove bad pending records
223 'cust_pay_pending' => [],
225 #replace invnum and pkgnum with billpkgnum
226 'cust_bill_pkg_detail' => [],
228 #usage_classes if we have none
231 #phone_type if we have none
235 'access_right' => [],
237 #change recur_flat and enable_prorate
238 'part_pkg_option' => [],
240 #add weights to pkg_category
241 'pkg_category' => [],
247 'cust_attachment' => [],
248 #'cust_credit' => [],
250 'cust_main_note' => [],
252 'cust_pay_void' => [],
254 #'cust_pkg_reason' => [],
255 'cust_pkg_discount' => [],
260 'payment_gateway' => [],
262 #migrate to templates
263 'msg_template' => [],
265 #return unprovisioned numbers to availability
268 #insert scripcondition
269 'TicketSystem' => [],
271 #insert LATA data if not already present
274 #insert MSA data if not already present
277 # migrate to radius_group and groupnum instead of groupname
278 'radius_usergroup' => [],
282 #insert default tower_sector if not present
286 'svc_broadband' => [],
288 #set up payment gateways if needed
291 #flag monthly tax exemptions
292 'cust_tax_exempt_pkg' => [],
294 #kick off tax location history upgrade
295 'cust_bill_pkg' => [],
309 my $data = upgrade_schema_data(%opt);
311 my $oldAutoCommit = $FS::UID::AutoCommit;
312 local $FS::UID::AutoCommit = 0;
313 local $FS::UID::AutoCommit = 0;
315 foreach my $table ( keys %$data ) {
317 my $class = "FS::$table";
321 if ( $class->can('_upgrade_schema') ) {
322 warn "Upgrading $table schema...\n";
326 $class->_upgrade_schema(%opt);
328 if ( $oldAutoCommit ) {
329 warn " committing\n";
330 dbh->commit or die dbh->errstr;
333 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
334 warn " done in ". (time-$start). " seconds\n";
337 warn "WARNING: asked for schema upgrade of $table,".
338 " but FS::$table has no _upgrade_schema method\n";
345 =item upgrade_schema_data
349 sub upgrade_schema_data {
352 tie my %hash, 'Tie::IxHash',
354 #fix classnum character(1)
355 'cust_bill_pkg_detail' => [],
356 #add necessary columns to RT schema
357 'TicketSystem' => [],
365 sub upgrade_sqlradius {
368 my $conf = new FS::Conf;
370 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
372 foreach my $part_export ( @part_export ) {
374 my $errmsg = 'Error adding FreesideStatus to '.
375 $part_export->option('datasrc'). ': ';
377 my $dbh = DBI->connect(
378 ( map $part_export->option($_), qw ( datasrc username password ) ),
379 { PrintError => 0, PrintWarn => 0 }
381 warn $errmsg.$DBI::errstr;
385 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
386 my $group = "UserName";
388 if ref($part_export) =~ /withdomain/
389 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
391 my $sth_alter = $dbh->prepare(
392 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
395 if ( $sth_alter->execute ) {
396 my $sth_update = $dbh->prepare(
397 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
398 ) or die $errmsg.$dbh->errstr;
399 $sth_update->execute or die $errmsg.$sth_update->errstr;
401 my $error = $sth_alter->errstr;
403 unless $error =~ /Duplicate column name/i #mysql
404 || $error =~ /already exists/i; #Pg
408 my $error = $dbh->errstr;
409 warn $errmsg.$error; #unless $error =~ /exists/i;
412 my $sth_index = $dbh->prepare(
413 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
416 unless ( $sth_index->execute ) {
417 my $error = $sth_index->errstr;
419 unless $error =~ /Duplicate key name/i #mysql
420 || $error =~ /already exists/i; #Pg
423 my $error = $dbh->errstr;
424 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
427 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
428 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
429 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
431 my $sth = $dbh->prepare("SELECT UserName,
433 $str2time max(AcctStartTime)),
434 $str2time max(AcctStopTime))
436 WHERE FreesideStatus = 'done'
440 or die $errmsg.$dbh->errstr;
441 $sth->execute() or die $errmsg.$sth->errstr;
443 while (my $row = $sth->fetchrow_arrayref ) {
444 my ($username, $realm, $start, $stop) = @$row;
446 $username = lc($username) unless $conf->exists('username-uppercase');
448 my $exportnum = $part_export->exportnum;
449 my $extra_sql = " AND exportnum = $exportnum ".
450 " AND exportsvcnum IS NOT NULL ";
452 if ( ref($part_export) =~ /withdomain/ ) {
453 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
454 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
457 my $svc_acct = qsearchs({
458 'select' => 'svc_acct.*',
459 'table' => 'svc_acct',
460 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
461 'LEFT JOIN export_svc USING ( svcpart )',
462 'hashref' => { 'username' => $username },
463 'extra_sql' => $extra_sql,
467 $svc_acct->last_login($start)
468 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
469 $svc_acct->last_logout($stop)
470 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);