diff options
author | Mark Wells <mark@freeside.biz> | 2016-03-09 15:49:53 -0800 |
---|---|---|
committer | Mark Wells <mark@freeside.biz> | 2016-03-09 15:49:58 -0800 |
commit | b472074e9b8d1d9ca2e2e38e231bf1f209281286 (patch) | |
tree | 6a072ba0a92e92bcf8651e3e0379a8535c796be3 /bin/contact-upgrade-fix-multiple | |
parent | d9f0a98539cd8dd957ea9a4b5d77fbb739d43d2e (diff) |
fix contact upgrade for multiple email addresses, #40971, from #25536
Diffstat (limited to 'bin/contact-upgrade-fix-multiple')
-rwxr-xr-x | bin/contact-upgrade-fix-multiple | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/bin/contact-upgrade-fix-multiple b/bin/contact-upgrade-fix-multiple new file mode 100755 index 0000000..f5d68fc --- /dev/null +++ b/bin/contact-upgrade-fix-multiple @@ -0,0 +1,60 @@ +#!/usr/bin/perl + +use FS::UID qw(adminsuidsetup); +use FS::cust_main_invoice; +use FS::Record qw(qsearch qsearchs dbh); +use FS::cust_main; +use Date::Parse 'str2time'; +use strict; + +my $usage = "usage: contact-upgrade-fix-multiple <user> <upgrade date>\n"; + +my $user = shift or die $usage; +adminsuidsetup($user); +local $FS::UID::AutoCommit = 0; + +my $date = shift; +my $timestamp = str2time($date) or die $usage; +# safety +die "upgrade date is before the 4.0 release, must be incorrect.\n$usage" + if $timestamp < 1455609600; + +my $search = { + 'table' => 'h_cust_main_invoice', + 'hashref' => { + 'history_date' => { op => '>=', value => $timestamp }, + 'history_action' => 'delete', + 'dest' => { op => '!=', value => 'POST' }, + } +}; + +# find deleted cust_main_invoice records +my %custnum_dest; +foreach my $deleted (qsearch $search) { + my $custnum = $deleted->custnum; + push @{ $custnum_dest{$custnum} ||= [] }, $deleted->dest; +} + +# find those customers +while (my ($custnum, $dests) = each(%custnum_dest)) { + my $cust_main = FS::cust_main->by_key($custnum); + # filter out the email(s) that the customer already has + my @curr_dest = $cust_main->invoicing_list_email; + my @new_dest = @curr_dest; + print "cust#$custnum\n"; + foreach my $email ( @$dests ) { + print " $email: "; + if ( grep { $_ eq $email } @curr_dest ) { + print "skipped.\n"; + next; + } + print "appending.\n"; + push @new_dest, $email; + } + my $error = $cust_main->replace( invoicing_list => \@new_dest ); + die $error if $error; +} + +dbh->commit; +print "Done.\n"; + |