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 # in invoice and quotation main templates, and also in all secondary
77 qsearch('conf', { 'name' => {op=>'LIKE', value=>'%latex%'} });
79 foreach my $c (@latex_confs) {
80 my $value = $c->value;
81 if (length($value) and $value =~ /fslongtable/) {
82 $value =~ s/fslongtable/longtable/g;
83 $conf->set($c->name, $value, $c->agentnum);
87 # if there's a USPS tools login, assume that's the standardization method
89 if ( length($conf->config('usps_webtools-userid')) > 0 and
90 !$conf->exists('address_standardize_method') ) {
91 $conf->set('address_standardize_method', 'usps');
96 sub upgrade_overlimit_groups {
99 my @groups = $conf->config('overlimit_groups',$agentnum);
100 if(scalar(@groups)) {
101 my $groups = join(',',@groups);
104 if ( $groups !~ /^[\d,]+$/ ) {
105 foreach my $groupname ( @groups ) {
106 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
108 $g = new FS::radius_group {
109 'groupname' => $groupname,
110 'description' => $groupname,
113 die $error if $error;
115 push @groupnums, $g->groupnum;
117 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
129 my $data = upgrade_data(%opt);
131 my $oldAutoCommit = $FS::UID::AutoCommit;
132 local $FS::UID::AutoCommit = 0;
133 local $FS::UID::AutoCommit = 0;
135 foreach my $table ( keys %$data ) {
137 my $class = "FS::$table";
141 if ( $class->can('_upgrade_data') ) {
142 warn "Upgrading $table...\n";
146 $class->_upgrade_data(%opt);
148 if ( $oldAutoCommit ) {
149 warn " committing\n";
150 dbh->commit or die dbh->errstr;
153 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
154 warn " done in ". (time-$start). " seconds\n";
157 warn "WARNING: asked for upgrade of $table,".
158 " but FS::$table has no _upgrade_data method\n";
161 # my @records = @{ $data->{$table} };
163 # foreach my $record ( @records ) {
164 # my $args = delete($record->{'_upgrade_args'}) || [];
165 # my $object = $class->new( $record );
166 # my $error = $object->insert( @$args );
167 # die "error inserting record into $table: $error\n"
173 local($FS::cust_main::ignore_expired_card) = 1;
174 local($FS::cust_main::ignore_illegal_zip) = 1;
175 local($FS::cust_main::ignore_banned_card) = 1;
176 local($FS::cust_main::skip_fuzzyfiles) = 1;
178 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
179 # kind of a weird spot for this, but it's better than duplicating
180 # all this code in each class...
181 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
182 foreach my $table ( @decrypt_tables ) {
183 my @objects = qsearch({
186 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
187 " AND LENGTH(payinfo) > 100",
189 foreach my $object ( @objects ) {
190 my $payinfo = $object->decrypt($object->payinfo);
191 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
192 $object->payinfo($payinfo);
193 my $error = $object->replace;
194 die $error if $error;
207 tie my %hash, 'Tie::IxHash',
209 #cust_main (remove paycvv from history)
215 #reason type and reasons
217 'cust_pkg_reason' => [],
219 #need part_pkg before cust_credit...
225 #duplicate history records
228 #populate cust_pay.otaker
231 #populate part_pkg_taxclass for starters
232 'part_pkg_taxclass' => [],
234 #remove bad pending records
235 'cust_pay_pending' => [],
237 #replace invnum and pkgnum with billpkgnum
238 'cust_bill_pkg_detail' => [],
240 #usage_classes if we have none
243 #phone_type if we have none
247 'access_right' => [],
249 #change recur_flat and enable_prorate
250 'part_pkg_option' => [],
252 #add weights to pkg_category
253 'pkg_category' => [],
259 'cust_attachment' => [],
260 #'cust_credit' => [],
262 'cust_main_note' => [],
264 'cust_pay_void' => [],
266 #'cust_pkg_reason' => [],
267 'cust_pkg_discount' => [],
272 'payment_gateway' => [],
274 #migrate to templates
275 'msg_template' => [],
277 #return unprovisioned numbers to availability
280 #insert scripcondition
281 'TicketSystem' => [],
283 #insert LATA data if not already present
286 #insert MSA data if not already present
289 # migrate to radius_group and groupnum instead of groupname
290 'radius_usergroup' => [],
294 #insert default tower_sector if not present
297 #repair improperly deleted services
301 'svc_broadband' => [],
303 #set up payment gateways if needed
306 #flag monthly tax exemptions
307 'cust_tax_exempt_pkg' => [],
309 #kick off tax location history upgrade
310 'cust_bill_pkg' => [],
312 #fix taxable line item links
313 'cust_bill_pkg_tax_location' => [],
327 my $data = upgrade_schema_data(%opt);
329 my $oldAutoCommit = $FS::UID::AutoCommit;
330 local $FS::UID::AutoCommit = 0;
331 local $FS::UID::AutoCommit = 0;
333 foreach my $table ( keys %$data ) {
335 my $class = "FS::$table";
339 if ( $class->can('_upgrade_schema') ) {
340 warn "Upgrading $table schema...\n";
344 $class->_upgrade_schema(%opt);
346 if ( $oldAutoCommit ) {
347 warn " committing\n";
348 dbh->commit or die dbh->errstr;
351 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
352 warn " done in ". (time-$start). " seconds\n";
355 warn "WARNING: asked for schema upgrade of $table,".
356 " but FS::$table has no _upgrade_schema method\n";
363 =item upgrade_schema_data
367 sub upgrade_schema_data {
370 tie my %hash, 'Tie::IxHash',
372 #fix classnum character(1)
373 'cust_bill_pkg_detail' => [],
374 #add necessary columns to RT schema
375 'TicketSystem' => [],
383 sub upgrade_sqlradius {
386 my $conf = new FS::Conf;
388 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
390 foreach my $part_export ( @part_export ) {
392 my $errmsg = 'Error adding FreesideStatus to '.
393 $part_export->option('datasrc'). ': ';
395 my $dbh = DBI->connect(
396 ( map $part_export->option($_), qw ( datasrc username password ) ),
397 { PrintError => 0, PrintWarn => 0 }
399 warn $errmsg.$DBI::errstr;
403 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
404 my $group = "UserName";
406 if ref($part_export) =~ /withdomain/
407 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
409 my $sth_alter = $dbh->prepare(
410 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
413 if ( $sth_alter->execute ) {
414 my $sth_update = $dbh->prepare(
415 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
416 ) or die $errmsg.$dbh->errstr;
417 $sth_update->execute or die $errmsg.$sth_update->errstr;
419 my $error = $sth_alter->errstr;
421 unless $error =~ /Duplicate column name/i #mysql
422 || $error =~ /already exists/i; #Pg
426 my $error = $dbh->errstr;
427 warn $errmsg.$error; #unless $error =~ /exists/i;
430 my $sth_index = $dbh->prepare(
431 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
434 unless ( $sth_index->execute ) {
435 my $error = $sth_index->errstr;
437 unless $error =~ /Duplicate key name/i #mysql
438 || $error =~ /already exists/i; #Pg
441 my $error = $dbh->errstr;
442 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
445 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
446 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
447 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
449 my $sth = $dbh->prepare("SELECT UserName,
451 $str2time max(AcctStartTime)),
452 $str2time max(AcctStopTime))
454 WHERE FreesideStatus = 'done'
458 or die $errmsg.$dbh->errstr;
459 $sth->execute() or die $errmsg.$sth->errstr;
461 while (my $row = $sth->fetchrow_arrayref ) {
462 my ($username, $realm, $start, $stop) = @$row;
464 $username = lc($username) unless $conf->exists('username-uppercase');
466 my $exportnum = $part_export->exportnum;
467 my $extra_sql = " AND exportnum = $exportnum ".
468 " AND exportsvcnum IS NOT NULL ";
470 if ( ref($part_export) =~ /withdomain/ ) {
471 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
472 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
475 my $svc_acct = qsearchs({
476 'select' => 'svc_acct.*',
477 'table' => 'svc_acct',
478 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
479 'LEFT JOIN export_svc USING ( svcpart )',
480 'hashref' => { 'username' => $username },
481 'extra_sql' => $extra_sql,
485 $svc_acct->last_login($start)
486 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
487 $svc_acct->last_logout($stop)
488 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);