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 $conf->set('address_standardize_method', 'usps')
90 if $conf->exists('usps_webtools-userid')
91 && length($conf->config('usps_webtools-userid')) > 0
92 && ! $conf->exists('address_standardize_method');
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 local $FS::cust_pkg::upgrade = 1; #go away after setup+start dates cleaned up for old customers
138 foreach my $table ( keys %$data ) {
140 my $class = "FS::$table";
144 if ( $class->can('_upgrade_data') ) {
145 warn "Upgrading $table...\n";
149 $class->_upgrade_data(%opt);
151 if ( $oldAutoCommit ) {
152 warn " committing\n";
153 dbh->commit or die dbh->errstr;
156 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
157 warn " done in ". (time-$start). " seconds\n";
160 warn "WARNING: asked for upgrade of $table,".
161 " but FS::$table has no _upgrade_data method\n";
164 # my @records = @{ $data->{$table} };
166 # foreach my $record ( @records ) {
167 # my $args = delete($record->{'_upgrade_args'}) || [];
168 # my $object = $class->new( $record );
169 # my $error = $object->insert( @$args );
170 # die "error inserting record into $table: $error\n"
176 local($FS::cust_main::ignore_expired_card) = 1;
177 local($FS::cust_main::ignore_illegal_zip) = 1;
178 local($FS::cust_main::ignore_banned_card) = 1;
179 local($FS::cust_main::skip_fuzzyfiles) = 1;
181 local($FS::cust_payby::ignore_expired_card) = 1;
182 local($FS::cust_payby::ignore_banned_card) = 1;
184 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
185 # kind of a weird spot for this, but it's better than duplicating
186 # all this code in each class...
187 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
188 foreach my $table ( @decrypt_tables ) {
189 my @objects = qsearch({
192 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
193 " AND LENGTH(payinfo) > 100",
195 foreach my $object ( @objects ) {
196 my $payinfo = $object->decrypt($object->payinfo);
197 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
198 $object->payinfo($payinfo);
199 my $error = $object->replace;
200 die $error if $error;
213 tie my %hash, 'Tie::IxHash',
215 #cust_main (remove paycvv from history)
221 #reason type and reasons
223 'cust_pkg_reason' => [],
225 #need part_pkg before cust_credit...
231 #duplicate history records
234 #populate cust_pay.otaker
237 #populate part_pkg_taxclass for starters
238 'part_pkg_taxclass' => [],
240 #remove bad pending records
241 'cust_pay_pending' => [],
243 #replace invnum and pkgnum with billpkgnum
244 'cust_bill_pkg_detail' => [],
246 #usage_classes if we have none
249 #phone_type if we have none
253 'access_right' => [],
255 #change recur_flat and enable_prorate
256 'part_pkg_option' => [],
258 #add weights to pkg_category
259 'pkg_category' => [],
265 'cust_attachment' => [],
266 #'cust_credit' => [],
268 'cust_main_note' => [],
270 'cust_pay_void' => [],
272 #'cust_pkg_reason' => [],
273 'cust_pkg_discount' => [],
278 'payment_gateway' => [],
280 #migrate to templates
281 'msg_template' => [],
283 #return unprovisioned numbers to availability
286 #insert scripcondition
287 'TicketSystem' => [],
289 #insert LATA data if not already present
292 #insert MSA data if not already present
295 # migrate to radius_group and groupnum instead of groupname
296 'radius_usergroup' => [],
300 #insert default tower_sector if not present
303 #repair improperly deleted services
307 'svc_broadband' => [],
309 #set up payment gateways if needed
312 #flag monthly tax exemptions
313 'cust_tax_exempt_pkg' => [],
315 #kick off tax location history upgrade
316 'cust_bill_pkg' => [],
318 #fix taxable line item links
319 'cust_bill_pkg_tax_location' => [],
333 my $data = upgrade_schema_data(%opt);
335 my $oldAutoCommit = $FS::UID::AutoCommit;
336 local $FS::UID::AutoCommit = 0;
337 local $FS::UID::AutoCommit = 0;
339 foreach my $table ( keys %$data ) {
341 my $class = "FS::$table";
345 if ( $class->can('_upgrade_schema') ) {
346 warn "Upgrading $table schema...\n";
350 $class->_upgrade_schema(%opt);
352 if ( $oldAutoCommit ) {
353 warn " committing\n";
354 dbh->commit or die dbh->errstr;
357 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
358 warn " done in ". (time-$start). " seconds\n";
361 warn "WARNING: asked for schema upgrade of $table,".
362 " but FS::$table has no _upgrade_schema method\n";
369 =item upgrade_schema_data
373 sub upgrade_schema_data {
376 tie my %hash, 'Tie::IxHash',
378 #fix classnum character(1)
379 'cust_bill_pkg_detail' => [],
380 #add necessary columns to RT schema
381 'TicketSystem' => [],
389 sub upgrade_sqlradius {
392 my $conf = new FS::Conf;
394 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
396 foreach my $part_export ( @part_export ) {
398 my $errmsg = 'Error adding FreesideStatus to '.
399 $part_export->option('datasrc'). ': ';
401 my $dbh = DBI->connect(
402 ( map $part_export->option($_), qw ( datasrc username password ) ),
403 { PrintError => 0, PrintWarn => 0 }
405 warn $errmsg.$DBI::errstr;
409 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
410 my $group = "UserName";
412 if ref($part_export) =~ /withdomain/
413 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
415 my $sth_alter = $dbh->prepare(
416 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
419 if ( $sth_alter->execute ) {
420 my $sth_update = $dbh->prepare(
421 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
422 ) or die $errmsg.$dbh->errstr;
423 $sth_update->execute or die $errmsg.$sth_update->errstr;
425 my $error = $sth_alter->errstr;
427 unless $error =~ /Duplicate column name/i #mysql
428 || $error =~ /already exists/i; #Pg
432 my $error = $dbh->errstr;
433 warn $errmsg.$error; #unless $error =~ /exists/i;
436 my $sth_index = $dbh->prepare(
437 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
440 unless ( $sth_index->execute ) {
441 my $error = $sth_index->errstr;
443 unless $error =~ /Duplicate key name/i #mysql
444 || $error =~ /already exists/i; #Pg
447 my $error = $dbh->errstr;
448 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
451 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
452 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
453 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
455 my $sth = $dbh->prepare("SELECT UserName,
457 $str2time max(AcctStartTime)),
458 $str2time max(AcctStopTime))
460 WHERE FreesideStatus = 'done'
464 or die $errmsg.$dbh->errstr;
465 $sth->execute() or die $errmsg.$sth->errstr;
467 while (my $row = $sth->fetchrow_arrayref ) {
468 my ($username, $realm, $start, $stop) = @$row;
470 $username = lc($username) unless $conf->exists('username-uppercase');
472 my $exportnum = $part_export->exportnum;
473 my $extra_sql = " AND exportnum = $exportnum ".
474 " AND exportsvcnum IS NOT NULL ";
476 if ( ref($part_export) =~ /withdomain/ ) {
477 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
478 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
481 my $svc_acct = qsearchs({
482 'select' => 'svc_acct.*',
483 'table' => 'svc_acct',
484 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
485 'LEFT JOIN export_svc USING ( svcpart )',
486 'hashref' => { 'username' => $username },
487 'extra_sql' => $extra_sql,
491 $svc_acct->last_login($start)
492 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
493 $svc_acct->last_logout($stop)
494 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);