3 use FS::UID qw(adminsuidsetup);
4 use FS::cust_main_invoice;
5 use FS::Record qw(qsearch qsearchs dbh);
7 use Date::Parse 'str2time';
10 my $usage = "usage: contact-upgrade-fix-multiple <user> <upgrade date>\n";
12 my $user = shift or die $usage;
13 adminsuidsetup($user);
14 local $FS::UID::AutoCommit = 0;
17 my $timestamp = str2time($date) or die $usage;
19 die "upgrade date is before the 4.0 release, must be incorrect.\n$usage"
20 if $timestamp < 1455609600;
23 'table' => 'h_cust_main_invoice',
25 'history_date' => { op => '>=', value => $timestamp },
26 'history_action' => 'delete',
27 'dest' => { op => '!=', value => 'POST' },
31 # find deleted cust_main_invoice records
33 foreach my $deleted (qsearch $search) {
34 my $custnum = $deleted->custnum;
35 push @{ $custnum_dest{$custnum} ||= [] }, $deleted->dest;
38 # find those customers
39 while (my ($custnum, $dests) = each(%custnum_dest)) {
40 my $cust_main = FS::cust_main->by_key($custnum);
41 # filter out the email(s) that the customer already has
42 my @curr_dest = $cust_main->invoicing_list_email;
43 my @new_dest = @curr_dest;
44 print "cust#$custnum\n";
45 foreach my $email ( @$dests ) {
47 if ( grep { $_ eq $email } @curr_dest ) {
52 push @new_dest, $email;
54 my $error = $cust_main->replace( invoicing_list => \@new_dest );