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;
13 use FS::Setup qw( enable_banned_pay_pad );
17 $FS::svc_domain::whois_hack = 1;
19 @ISA = qw( Exporter );
20 @EXPORT_OK = qw( upgrade_schema upgrade_config upgrade upgrade_sqlradius );
26 FS::Upgrade - Database upgrade routines
34 Currently this module simply provides a place to store common subroutines for
49 my $conf = new FS::Conf;
51 # to simplify tokenization upgrades
52 die "Conf selfservice-payment_gateway no longer supported"
53 if $conf->config('selfservice-payment_gateway');
55 $conf->touch('payment_receipt')
56 if $conf->exists('payment_receipt_email')
57 || $conf->config('payment_receipt_msgnum');
59 $conf->touch('geocode-require_nw_coordinates')
60 if $conf->exists('svc_broadband-require-nw-coordinates');
62 unless ( $conf->config('echeck-country') ) {
63 if ( $conf->exists('cust_main-require-bank-branch') ) {
64 $conf->set('echeck-country', 'CA');
65 } elsif ( $conf->exists('echeck-nonus') ) {
66 $conf->set('echeck-country', 'XX');
68 $conf->set('echeck-country', 'US');
72 my @agents = qsearch('agent', {});
74 upgrade_overlimit_groups($conf);
75 map { upgrade_overlimit_groups($conf,$_->agentnum) } @agents;
77 upgrade_invoice_from($conf);
78 foreach my $agent (@agents) {
79 upgrade_invoice_from($conf,$agent->agentnum,1);
82 my $DIST_CONF = '/usr/local/etc/freeside/default_conf/';#DIST_CONF in Makefile
83 $conf->set($_, scalar(read_file( "$DIST_CONF/$_" )) )
84 foreach grep { ! $conf->exists($_) && -s "$DIST_CONF/$_" }
85 qw( quotation_html quotation_latex quotation_latexnotes );
87 # change 'fslongtable' to 'longtable'
88 # in invoice and quotation main templates, and also in all secondary
91 qsearch('conf', { 'name' => {op=>'LIKE', value=>'%latex%'} });
93 foreach my $c (@latex_confs) {
94 my $value = $c->value;
95 if (length($value) and $value =~ /fslongtable/) {
96 $value =~ s/fslongtable/longtable/g;
97 $conf->set($c->name, $value, $c->agentnum);
101 # if there's a USPS tools login, assume that's the standardization method
103 $conf->set('address_standardize_method', 'usps')
104 if $conf->exists('usps_webtools-userid')
105 && length($conf->config('usps_webtools-userid')) > 0
106 && ! $conf->exists('address_standardize_method');
108 # this option has been renamed/expanded
109 if ( $conf->exists('cust_main-enable_spouse_birthdate') ) {
110 $conf->touch('cust_main-enable_spouse');
111 $conf->delete('cust_main-enable_spouse_birthdate');
115 if ( $conf->exists('cust_pkg-show_fcc_voice_grade_equivalent') ) {
116 $conf->touch('part_pkg-show_fcc_options');
117 $conf->delete('cust_pkg-show_fcc_voice_grade_equivalent');
119 You have FCC Form 477 package options enabled.
121 Starting with the October 2014 filing date, the FCC has redesigned
122 Form 477 and introduced new service categories. See bin/convert-477-options
123 to update your package configuration for the new report.
125 If you need to continue using the old Form 477 report, turn on the
126 'old_fcc_report' configuration option.
130 # boolean invoice_sections_by_location option is now
131 # invoice_sections_method = 'location'
132 my @invoice_sections_confs =
133 qsearch('conf', { 'name' => { op=>'LIKE', value=>'%sections_by_location' } });
134 foreach my $c (@invoice_sections_confs) {
135 $c->name =~ /^(\w+)sections_by_location$/;
136 $conf->delete($c->name);
137 my $newname = $1.'sections_method';
138 $conf->set($newname, 'location');
141 # boolean enable_taxproducts is now tax_data_vendor = 'cch'
142 if ( $conf->exists('enable_taxproducts') ) {
144 $conf->delete('enable_taxproducts');
145 $conf->set('tax_data_vendor', 'cch');
149 # boolean tax-cust_exempt-groups-require_individual_nums is now -num_req all
150 if ( $conf->exists('tax-cust_exempt-groups-require_individual_nums') ) {
151 $conf->set('tax-cust_exempt-groups-num_req', 'all');
152 $conf->delete('tax-cust_exempt-groups-require_individual_nums');
155 # boolean+text previous_balance-exclude_from_total is now two separate options
156 my $total_new_charges = $conf->config('previous_balance-exclude_from_total');
157 if ( defined $total_new_charges && length($total_new_charges) > 0 ) {
158 $conf->set('previous_balance-text-total_new_charges', $total_new_charges);
159 $conf->set('previous_balance-exclude_from_total', '');
162 # switch from specifying an email address to boolean check
163 if ( $conf->exists('batch-errors_to') ) {
164 $conf->touch('batch-errors_not_fatal');
165 $conf->delete('batch-errors_to');
168 if ( $conf->exists('voip-cust_email_csv_cdr') ) {
169 $conf->set('voip_cdr_email_attach', 'csv');
170 $conf->delete('voip-cust_email_csv_cdr') ;
173 if ($conf->exists('unsuspendauto') && !$conf->config('unsuspend_balance')) {
174 $conf->set('unsuspend_balance','Zero');
175 $conf->delete('unsuspendauto');
178 my $cust_fields = $conf->config('cust-fields');
179 if ( defined $cust_fields && $cust_fields =~ / \| Payment Type/ ) {
180 # so we can potentially use 'Payment Types' or somesuch in the future
181 $cust_fields =~ s/ \| Payment Type( \|)/$1/;
182 $cust_fields =~ s/ \| Payment Type$//;
183 $conf->set('cust-fields',$cust_fields);
186 enable_banned_pay_pad() unless length($conf->config('banned_pay-pad'));
188 # if translate-auto-insert is enabled for a locale, ensure that invoice
189 # terms are in the msgcat (is there a better place for this?)
190 if (my $auto_locale = $conf->config('translate-auto-insert')) {
191 my $lh = FS::L10N->get_handle($auto_locale);
192 foreach (@FS::Conf::invoice_terms) {
193 $lh->maketext($_) if length($_);
197 unless ( FS::upgrade_journal->is_done('deprecate_unmask_ss') ) {
198 if ( $conf->config_bool( 'unmask_ss' )) {
199 warn "'unmask_ssn' deprecated from global configuration\n";
200 for my $access_group ( qsearch( access_group => {} )) {
201 $access_group->grant_access_right( 'Unmask customer SSN' );
202 warn " - 'Unmask customer SSN' access right granted to '" .
203 $access_group->groupname . "' employee group\n";
206 FS::upgrade_journal->set_done('deprecate_unmask_ss');
209 # Rename agent-disable_counts as config-disable_counts, flag now
210 # affects several configuration pages
211 for my $row ( qsearch( conf => { name => 'agent-disable_counts' } )) {
212 $row->name('config-disable_counts');
218 sub upgrade_overlimit_groups {
220 my $agentnum = shift;
221 my @groups = $conf->config('overlimit_groups',$agentnum);
222 if(scalar(@groups)) {
223 my $groups = join(',',@groups);
226 if ( $groups !~ /^[\d,]+$/ ) {
227 foreach my $groupname ( @groups ) {
228 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
230 $g = new FS::radius_group {
231 'groupname' => $groupname,
232 'description' => $groupname,
235 die $error if $error;
237 push @groupnums, $g->groupnum;
239 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
244 sub upgrade_invoice_from {
245 my ($conf, $agentnum, $agentonly) = @_;
247 ! $conf->exists('invoice_from_name',$agentnum,$agentonly)
248 && $conf->exists('invoice_from',$agentnum,$agentonly)
249 && $conf->config('invoice_from',$agentnum,$agentonly) =~ /\<(.*)\>/
252 $realemail =~ s/^\s*//; # remove leading spaces
253 $realemail =~ s/\s*$//; # remove trailing spaces
254 my $realname = $conf->config('invoice_from',$agentnum);
255 $realname =~ s/\<.*\>//; # remove email address
256 $realname =~ s/^\s*//; # remove leading spaces
257 $realname =~ s/\s*$//; # remove trailing spaces
258 # properly quote names that contain punctuation
259 if (($realname =~ /[^[:alnum:][:space:]]/) && ($realname !~ /^\".*\"$/)) {
260 $realname = '"' . $realname . '"';
262 $conf->set('invoice_from_name', $realname, $agentnum);
263 $conf->set('invoice_from', $realemail, $agentnum);
274 my $data = upgrade_data(%opt);
276 my $oldAutoCommit = $FS::UID::AutoCommit;
277 local $FS::UID::AutoCommit = 0;
278 local $FS::UID::AutoCommit = 0;
280 local $FS::cust_pkg::upgrade = 1; #go away after setup+start dates cleaned up for old customers
283 foreach my $table ( keys %$data ) {
285 my $class = "FS::$table";
289 if ( $class->can('_upgrade_data') ) {
290 warn "Upgrading $table...\n";
294 $class->_upgrade_data(%opt);
296 # New interface for async upgrades: a class can declare a
297 # "queueable_upgrade" method, which will run as part of the normal
298 # upgrade, but if the -j option is passed, will instead be run from
300 if ( $class->can('queueable_upgrade') ) {
301 my $jobname = $class . '::queueable_upgrade';
302 my $num_jobs = FS::queue->count("job = '$jobname' and status != 'failed'");
304 warn "$class upgrade already scheduled.\n";
306 if ( $opt{'queue'} ) {
307 warn "Scheduling $class upgrade.\n";
308 my $job = FS::queue->new({ job => $jobname });
309 $job->insert($class, %opt);
311 $class->queueable_upgrade(%opt);
316 if ( $oldAutoCommit ) {
317 warn " committing\n";
318 dbh->commit or die dbh->errstr;
321 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
322 warn " done in ". (time-$start). " seconds\n";
325 warn "WARNING: asked for upgrade of $table,".
326 " but FS::$table has no _upgrade_data method\n";
329 # my @records = @{ $data->{$table} };
331 # foreach my $record ( @records ) {
332 # my $args = delete($record->{'_upgrade_args'}) || [];
333 # my $object = $class->new( $record );
334 # my $error = $object->insert( @$args );
335 # die "error inserting record into $table: $error\n"
341 local($FS::cust_main::ignore_expired_card) = 1;
342 #this is long-gone... would need to set an equivalent in cust_location #local($FS::cust_main::ignore_illegal_zip) = 1;
343 local($FS::cust_main::ignore_banned_card) = 1;
344 local($FS::cust_main::skip_fuzzyfiles) = 1;
346 local($FS::cust_payby::ignore_expired_card) = 1;
347 local($FS::cust_payby::ignore_banned_card) = 1;
349 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
350 # kind of a weird spot for this, but it's better than duplicating
351 # all this code in each class...
352 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
353 foreach my $table ( @decrypt_tables ) {
354 my @objects = qsearch({
357 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
358 " AND LENGTH(payinfo) > 100",
360 foreach my $object ( @objects ) {
361 my $payinfo = $object->decrypt($object->payinfo);
362 if ( $payinfo eq $object->payinfo ) {
363 warn "error decrypting payinfo for $table: $payinfo\n";
366 $object->payinfo($payinfo);
367 my $error = $object->replace;
368 die $error if $error;
381 tie my %hash, 'Tie::IxHash',
386 #payby conditions to new ones
387 'part_event_condition' => [],
389 #payby actions to new ones
392 #fix whitespace - before cust_main
393 'cust_location' => [],
395 # need before cust_main tokenization upgrade,
396 # blocks tokenization upgrade if deprecated features still in use
397 'agent_payment_gateway' => [],
399 #remove bad source_paynum before cust_main
402 #cust_main (tokenizes cards, remove paycvv from history, locations, cust_payby, etc)
403 # (handles payinfo encryption/tokenization across all relevant tables)
406 #contact -> cust_contact / prospect_contact
412 #reason type and reasons
414 'cust_pkg_reason' => [],
416 #need part_pkg before cust_credit...
422 # reason / void_reason migration to reasonnum / void_reasonnum
423 'cust_credit_void' => [],
424 'cust_bill_void' => [],
425 # also fix some tax allocation records
426 'cust_bill_pkg_void' => [],
428 #duplicate history records
431 #populate cust_pay.otaker
434 #populate part_pkg_taxclass for starters
435 'part_pkg_taxclass' => [],
437 #remove bad pending records
438 'cust_pay_pending' => [],
440 #replace invnum and pkgnum with billpkgnum
441 'cust_bill_pkg_detail' => [],
443 #usage_classes if we have none
446 #phone_type if we have none
450 'access_right' => [],
452 #change recur_flat and enable_prorate
453 'part_pkg_option' => [],
455 #add weights to pkg_category
456 'pkg_category' => [],
462 'cust_attachment' => [],
463 #'cust_credit' => [],
465 'cust_main_note' => [],
467 'cust_pay_void' => [],
469 #'cust_pkg_reason' => [],
470 'cust_pkg_discount' => [],
471 #'cust_refund' => [],
478 'payment_gateway' => [],
480 #migrate to templates
481 'msg_template' => [],
483 #return unprovisioned numbers to availability
486 #insert scripcondition
487 'TicketSystem' => [],
489 #insert LATA data if not already present
492 #insert MSA data if not already present
495 # migrate to radius_group and groupnum instead of groupname
496 'radius_usergroup' => [],
500 #insert default tower_sector if not present
503 #repair improperly deleted services
507 'svc_broadband' => [],
509 #set up payment gateways if needed
512 #flag monthly tax exemptions
513 'cust_tax_exempt_pkg' => [],
515 #kick off tax location history upgrade
516 'cust_bill_pkg' => [],
518 #fix taxable line item links
519 'cust_bill_pkg_tax_location' => [],
521 #populate state FIPS codes if not already done
524 #set default locations on quoted packages
525 'quotation_pkg' => [],
527 #populate tax statuses
530 #mark certain taxes as system-maintained,
532 'cust_main_county' => [],
534 #'compliance solutions' -> 'compliance_solutions'
536 'tax_rate_location' => [],
538 #upgrade part_event_condition_option agentnum to a multiple hash value
539 'part_event_condition_option' =>[],
545 'svc_hardware' => [],
551 'tower_sector' => [],
567 my $data = upgrade_schema_data(%opt);
569 my $oldAutoCommit = $FS::UID::AutoCommit;
570 local $FS::UID::AutoCommit = 0;
571 local $FS::UID::AutoCommit = 0;
573 foreach my $table ( keys %$data ) {
575 my $class = "FS::$table";
579 if ( $class->can('_upgrade_schema') ) {
580 warn "Upgrading $table schema...\n";
584 $class->_upgrade_schema(%opt);
586 if ( $oldAutoCommit ) {
587 warn " committing\n";
588 dbh->commit or die dbh->errstr;
591 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
592 warn " done in ". (time-$start). " seconds\n";
595 warn "WARNING: asked for schema upgrade of $table,".
596 " but FS::$table has no _upgrade_schema method\n";
603 =item upgrade_schema_data
607 sub upgrade_schema_data {
610 #auto-find tables/classes with an _update_schema method?
612 tie my %hash, 'Tie::IxHash',
614 #fix classnum character(1)
615 'cust_bill_pkg_detail' => [],
616 #add necessary columns to RT schema
617 'TicketSystem' => [],
618 #remove h_access_user_log if it exists (since our regular auto schema
619 # upgrade doesn't have the drop tables flag turned on)
620 'access_user_log' => [],
621 #remove possible dangling records
622 'password_history' => [],
623 'cust_pay_pending' => [],
624 #remove records referencing removed things with their FKs
625 'pkg_referral' => [],
626 'cust_bill_pkg_discount' => [],
628 'cust_bill_pay_batch' => [],
629 'cust_event_fee' => [],
631 'queue_depend' => [],
632 'cust_main_invoice' => [],
633 #update records referencing removed things with their FKs
641 sub upgrade_sqlradius {
644 my $conf = new FS::Conf;
646 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
648 foreach my $part_export ( @part_export ) {
650 my $errmsg = 'Error adding FreesideStatus to '.
651 $part_export->option('datasrc'). ': ';
653 my $dbh = FS::DBI->connect(
654 ( map $part_export->option($_), qw ( datasrc username password ) ),
655 { PrintError => 0, PrintWarn => 0 }
657 warn $errmsg.$FS::DBI::errstr;
661 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
662 my $group = "UserName";
664 if ref($part_export) =~ /withdomain/
665 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
667 my $sth_alter = $dbh->prepare(
668 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
671 if ( $sth_alter->execute ) {
672 my $sth_update = $dbh->prepare(
673 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
674 ) or die $errmsg.$dbh->errstr;
675 $sth_update->execute or die $errmsg.$sth_update->errstr;
677 my $error = $sth_alter->errstr;
679 unless $error =~ /Duplicate column name/i #mysql
680 || $error =~ /already exists/i; #Pg
684 my $error = $dbh->errstr;
685 warn $errmsg.$error; #unless $error =~ /exists/i;
688 my $sth_index = $dbh->prepare(
689 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
692 unless ( $sth_index->execute ) {
693 my $error = $sth_index->errstr;
695 unless $error =~ /Duplicate key name/i #mysql
696 || $error =~ /already exists/i; #Pg
699 my $error = $dbh->errstr;
700 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
703 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
704 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
705 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
707 my $sth = $dbh->prepare("SELECT UserName,
709 $str2time max(AcctStartTime)),
710 $str2time max(AcctStopTime))
712 WHERE FreesideStatus = 'done'
716 or die $errmsg.$dbh->errstr;
717 $sth->execute() or die $errmsg.$sth->errstr;
719 while (my $row = $sth->fetchrow_arrayref ) {
720 my ($username, $realm, $start, $stop) = @$row;
722 $username = lc($username) unless $conf->exists('username-uppercase');
724 my $exportnum = $part_export->exportnum;
725 my $extra_sql = " AND exportnum = $exportnum ".
726 " AND exportsvcnum IS NOT NULL ";
728 if ( ref($part_export) =~ /withdomain/ ) {
729 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
730 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
733 my $svc_acct = qsearchs({
734 'select' => 'svc_acct.*',
735 'table' => 'svc_acct',
736 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
737 'LEFT JOIN export_svc USING ( svcpart )',
738 'hashref' => { 'username' => $username },
739 'extra_sql' => $extra_sql,
743 $svc_acct->last_login($start)
744 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
745 $svc_acct->last_logout($stop)
746 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);