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);
12 use FS::upgrade_journal;
15 $FS::svc_domain::whois_hack = 1;
17 @ISA = qw( Exporter );
18 @EXPORT_OK = qw( upgrade_schema upgrade_config upgrade upgrade_sqlradius );
24 FS::Upgrade - Database upgrade routines
32 Currently this module simply provides a place to store common subroutines for
47 my $conf = new FS::Conf;
49 $conf->touch('payment_receipt')
50 if $conf->exists('payment_receipt_email')
51 || $conf->config('payment_receipt_msgnum');
53 $conf->touch('geocode-require_nw_coordinates')
54 if $conf->exists('svc_broadband-require-nw-coordinates');
56 unless ( $conf->config('echeck-country') ) {
57 if ( $conf->exists('cust_main-require-bank-branch') ) {
58 $conf->set('echeck-country', 'CA');
59 } elsif ( $conf->exists('echeck-nonus') ) {
60 $conf->set('echeck-country', 'XX');
62 $conf->set('echeck-country', 'US');
66 upgrade_overlimit_groups($conf);
67 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
69 my $DIST_CONF = '/usr/local/etc/freeside/default_conf/';#DIST_CONF in Makefile
70 $conf->set($_, scalar(read_file( "$DIST_CONF/$_" )) )
71 foreach grep { ! $conf->exists($_) && -s "$DIST_CONF/$_" }
72 qw( quotation_html quotation_latex quotation_latexnotes );
74 # change 'fslongtable' to 'longtable'
75 # in invoice and quotation main templates, and also in all secondary
78 qsearch('conf', { 'name' => {op=>'LIKE', value=>'%latex%'} });
80 foreach my $c (@latex_confs) {
81 my $value = $c->value;
82 if (length($value) and $value =~ /fslongtable/) {
83 $value =~ s/fslongtable/longtable/g;
84 $conf->set($c->name, $value, $c->agentnum);
88 # if there's a USPS tools login, assume that's the standardization method
90 $conf->set('address_standardize_method', 'usps')
91 if $conf->exists('usps_webtools-userid')
92 && length($conf->config('usps_webtools-userid')) > 0
93 && ! $conf->exists('address_standardize_method');
95 # this option has been renamed/expanded
96 if ( $conf->exists('cust_main-enable_spouse_birthdate') ) {
97 $conf->touch('cust_main-enable_spouse');
98 $conf->delete('cust_main-enable_spouse_birthdate');
102 if ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent') ) {
103 $conf->touch('part_pkg-show_fcc_options');
104 $conf->delete('cust_pkg-show_fcc_voice_grade_equivalent');
106 You have FCC Form 477 package options enabled.
108 Starting with the October 2014 filing date, the FCC has redesigned
109 Form 477 and introduced new service categories. See bin/convert-477-options
110 to update your package configuration for the new report.
112 If you need to continue using the old Form 477 report, turn on the
113 'old_fcc_report' configuration option.
118 sub upgrade_overlimit_groups {
120 my $agentnum = shift;
121 my @groups = $conf->config('overlimit_groups',$agentnum);
122 if(scalar(@groups)) {
123 my $groups = join(',',@groups);
126 if ( $groups !~ /^[\d,]+$/ ) {
127 foreach my $groupname ( @groups ) {
128 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
130 $g = new FS::radius_group {
131 'groupname' => $groupname,
132 'description' => $groupname,
135 die $error if $error;
137 push @groupnums, $g->groupnum;
139 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
151 my $data = upgrade_data(%opt);
153 my $oldAutoCommit = $FS::UID::AutoCommit;
154 local $FS::UID::AutoCommit = 0;
155 local $FS::UID::AutoCommit = 0;
157 local $FS::cust_pkg::upgrade = 1; #go away after setup+start dates cleaned up for old customers
160 foreach my $table ( keys %$data ) {
162 my $class = "FS::$table";
166 if ( $class->can('_upgrade_data') ) {
167 warn "Upgrading $table...\n";
171 $class->_upgrade_data(%opt);
173 # New interface for async upgrades: a class can declare a
174 # "queueable_upgrade" method, which will run as part of the normal
175 # upgrade, but if the -j option is passed, will instead be run from
177 if ( $class->can('queueable_upgrade') ) {
178 my $jobname = $class . '::queueable_upgrade';
179 my $num_jobs = FS::queue->count("job = '$jobname' and status != 'failed'");
181 warn "$class upgrade already scheduled.\n";
183 if ( $opt{'queue'} ) {
184 warn "Scheduling $class upgrade.\n";
185 my $job = FS::queue->new({ job => $jobname });
186 $job->insert($class, %opt);
188 $class->queueable_upgrade(%opt);
193 if ( $oldAutoCommit ) {
194 warn " committing\n";
195 dbh->commit or die dbh->errstr;
198 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
199 warn " done in ". (time-$start). " seconds\n";
202 warn "WARNING: asked for upgrade of $table,".
203 " but FS::$table has no _upgrade_data method\n";
206 # my @records = @{ $data->{$table} };
208 # foreach my $record ( @records ) {
209 # my $args = delete($record->{'_upgrade_args'}) || [];
210 # my $object = $class->new( $record );
211 # my $error = $object->insert( @$args );
212 # die "error inserting record into $table: $error\n"
218 local($FS::cust_main::ignore_expired_card) = 1;
219 local($FS::cust_main::ignore_illegal_zip) = 1;
220 local($FS::cust_main::ignore_banned_card) = 1;
221 local($FS::cust_main::skip_fuzzyfiles) = 1;
223 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
224 # kind of a weird spot for this, but it's better than duplicating
225 # all this code in each class...
226 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
227 foreach my $table ( @decrypt_tables ) {
228 my @objects = qsearch({
231 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
232 " AND LENGTH(payinfo) > 100",
234 foreach my $object ( @objects ) {
235 my $payinfo = $object->decrypt($object->payinfo);
236 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
237 $object->payinfo($payinfo);
238 my $error = $object->replace;
239 die $error if $error;
252 tie my %hash, 'Tie::IxHash',
254 #cust_main (remove paycvv from history)
260 #reason type and reasons
262 'cust_pkg_reason' => [],
264 #need part_pkg before cust_credit...
270 #duplicate history records
273 #populate cust_pay.otaker
276 #populate part_pkg_taxclass for starters
277 'part_pkg_taxclass' => [],
279 #remove bad pending records
280 'cust_pay_pending' => [],
282 #replace invnum and pkgnum with billpkgnum
283 'cust_bill_pkg_detail' => [],
285 #usage_classes if we have none
288 #phone_type if we have none
292 'access_right' => [],
294 #change recur_flat and enable_prorate
295 'part_pkg_option' => [],
297 #add weights to pkg_category
298 'pkg_category' => [],
304 'cust_attachment' => [],
305 #'cust_credit' => [],
307 'cust_main_note' => [],
309 'cust_pay_void' => [],
311 #'cust_pkg_reason' => [],
312 'cust_pkg_discount' => [],
317 'payment_gateway' => [],
319 #migrate to templates
320 'msg_template' => [],
322 #return unprovisioned numbers to availability
325 #insert scripcondition
326 'TicketSystem' => [],
328 #insert LATA data if not already present
331 #insert MSA data if not already present
334 # migrate to radius_group and groupnum instead of groupname
335 'radius_usergroup' => [],
339 #insert default tower_sector if not present
342 #repair improperly deleted services
346 'svc_broadband' => [],
348 #set up payment gateways if needed
351 #flag monthly tax exemptions
352 'cust_tax_exempt_pkg' => [],
354 #kick off tax location history upgrade
355 'cust_bill_pkg' => [],
357 #fix taxable line item links
358 'cust_bill_pkg_tax_location' => [],
360 #populate state FIPS codes if not already done
375 my $data = upgrade_schema_data(%opt);
377 my $oldAutoCommit = $FS::UID::AutoCommit;
378 local $FS::UID::AutoCommit = 0;
379 local $FS::UID::AutoCommit = 0;
381 foreach my $table ( keys %$data ) {
383 my $class = "FS::$table";
387 if ( $class->can('_upgrade_schema') ) {
388 warn "Upgrading $table schema...\n";
392 $class->_upgrade_schema(%opt);
394 if ( $oldAutoCommit ) {
395 warn " committing\n";
396 dbh->commit or die dbh->errstr;
399 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
400 warn " done in ". (time-$start). " seconds\n";
403 warn "WARNING: asked for schema upgrade of $table,".
404 " but FS::$table has no _upgrade_schema method\n";
411 =item upgrade_schema_data
415 sub upgrade_schema_data {
418 tie my %hash, 'Tie::IxHash',
420 #fix classnum character(1)
421 'cust_bill_pkg_detail' => [],
422 #add necessary columns to RT schema
423 'TicketSystem' => [],
431 sub upgrade_sqlradius {
434 my $conf = new FS::Conf;
436 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
438 foreach my $part_export ( @part_export ) {
440 my $errmsg = 'Error adding FreesideStatus to '.
441 $part_export->option('datasrc'). ': ';
443 my $dbh = DBI->connect(
444 ( map $part_export->option($_), qw ( datasrc username password ) ),
445 { PrintError => 0, PrintWarn => 0 }
447 warn $errmsg.$DBI::errstr;
451 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
452 my $group = "UserName";
454 if ref($part_export) =~ /withdomain/
455 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
457 my $sth_alter = $dbh->prepare(
458 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
461 if ( $sth_alter->execute ) {
462 my $sth_update = $dbh->prepare(
463 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
464 ) or die $errmsg.$dbh->errstr;
465 $sth_update->execute or die $errmsg.$sth_update->errstr;
467 my $error = $sth_alter->errstr;
469 unless $error =~ /Duplicate column name/i #mysql
470 || $error =~ /already exists/i; #Pg
474 my $error = $dbh->errstr;
475 warn $errmsg.$error; #unless $error =~ /exists/i;
478 my $sth_index = $dbh->prepare(
479 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
482 unless ( $sth_index->execute ) {
483 my $error = $sth_index->errstr;
485 unless $error =~ /Duplicate key name/i #mysql
486 || $error =~ /already exists/i; #Pg
489 my $error = $dbh->errstr;
490 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
493 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
494 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
495 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
497 my $sth = $dbh->prepare("SELECT UserName,
499 $str2time max(AcctStartTime)),
500 $str2time max(AcctStopTime))
502 WHERE FreesideStatus = 'done'
506 or die $errmsg.$dbh->errstr;
507 $sth->execute() or die $errmsg.$sth->errstr;
509 while (my $row = $sth->fetchrow_arrayref ) {
510 my ($username, $realm, $start, $stop) = @$row;
512 $username = lc($username) unless $conf->exists('username-uppercase');
514 my $exportnum = $part_export->exportnum;
515 my $extra_sql = " AND exportnum = $exportnum ".
516 " AND exportsvcnum IS NOT NULL ";
518 if ( ref($part_export) =~ /withdomain/ ) {
519 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
520 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
523 my $svc_acct = qsearchs({
524 'select' => 'svc_acct.*',
525 'table' => 'svc_acct',
526 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
527 'LEFT JOIN export_svc USING ( svcpart )',
528 'hashref' => { 'username' => $username },
529 'extra_sql' => $extra_sql,
533 $svc_acct->last_login($start)
534 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
535 $svc_acct->last_logout($stop)
536 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);