|
Line 0
Link Here
|
| 0 |
- |
1 |
use Modern::Perl; |
|
|
2 |
|
| 3 |
return { |
| 4 |
bug_number => "36846", |
| 5 |
description => "Fix 'collected' vs 'tendered' variables in notice templates", |
| 6 |
up => sub { |
| 7 |
my ($args) = @_; |
| 8 |
my ( $dbh, $out ) = @$args{qw(dbh out)}; |
| 9 |
|
| 10 |
# Templates that can receive 'tendered' and 'change' variables via substitute parameters |
| 11 |
# Based on analysis of pos/pay.pl, pos/printreceipt.pl, and members/paycollect.pl |
| 12 |
my @affected_templates = ( 'RECEIPT', 'ACCOUNT_CREDIT' ); |
| 13 |
|
| 14 |
# Replace 'collected' with 'tendered' in all affected templates |
| 15 |
# Handle various spacing and formatting variations |
| 16 |
my @patterns = ( |
| 17 |
[ '[% collected', '[% tendered' ], |
| 18 |
[ '[%collected', '[%tendered' ], |
| 19 |
[ '[% collected', '[% tendered' ], |
| 20 |
[ '[%\t+collected', '[%\t+tendered' ], |
| 21 |
[ 'collected | $Price', 'tendered | $Price' ], |
| 22 |
[ 'collected|$Price', 'tendered|$Price' ], |
| 23 |
[ 'collected | \$Price', 'tendered | \$Price' ], |
| 24 |
[ 'collected|\$Price', 'tendered|\$Price' ], |
| 25 |
); |
| 26 |
|
| 27 |
my $total_updated = 0; |
| 28 |
|
| 29 |
foreach my $template_code (@affected_templates) { |
| 30 |
my $template_updated = 0; |
| 31 |
|
| 32 |
foreach my $pattern (@patterns) { |
| 33 |
my ( $old, $new ) = @$pattern; |
| 34 |
|
| 35 |
my $sth = $dbh->prepare( |
| 36 |
"UPDATE letter SET content = REPLACE(content, ?, ?) WHERE code = ? AND content LIKE ?"); |
| 37 |
|
| 38 |
my $affected = $sth->execute( $old, $new, $template_code, "%$old%" ); |
| 39 |
if ( $affected && $affected > 0 ) { |
| 40 |
$template_updated += $affected; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
# Additional cleanup - handle any remaining 'collected' references |
| 45 |
# This covers edge cases where 'collected' might appear in other contexts |
| 46 |
my $cleanup_sth = $dbh->prepare( |
| 47 |
"UPDATE letter SET content = REPLACE(content, 'collected', 'tendered') |
| 48 |
WHERE code = ? AND content LIKE '%collected%' |
| 49 |
AND content NOT LIKE '%tendered%'" |
| 50 |
); |
| 51 |
my $cleanup_affected = $cleanup_sth->execute($template_code); |
| 52 |
if ( $cleanup_affected && $cleanup_affected > 0 ) { |
| 53 |
$template_updated += $cleanup_affected; |
| 54 |
} |
| 55 |
|
| 56 |
if ( $template_updated > 0 ) { |
| 57 |
say $out |
| 58 |
"Updated $template_updated $template_code notice templates to use 'tendered' instead of 'collected'"; |
| 59 |
$total_updated += $template_updated; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if ( $total_updated == 0 ) { |
| 64 |
say $out "No templates needed updating for 'collected' to 'tendered' conversion"; |
| 65 |
} |
| 66 |
|
| 67 |
# Verify the fix worked for all affected templates |
| 68 |
my $remaining_total = 0; |
| 69 |
foreach my $template_code (@affected_templates) { |
| 70 |
my $remaining_sth = |
| 71 |
$dbh->prepare("SELECT COUNT(*) FROM letter WHERE code = ? AND content LIKE '%collected%'"); |
| 72 |
$remaining_sth->execute($template_code); |
| 73 |
my ($remaining) = $remaining_sth->fetchrow_array(); |
| 74 |
|
| 75 |
if ( $remaining > 0 ) { |
| 76 |
say $out |
| 77 |
"Warning: $remaining $template_code templates still contain 'collected' references - manual review may be needed"; |
| 78 |
$remaining_total += $remaining; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ( $remaining_total == 0 ) { |
| 83 |
say $out "Successfully converted all 'collected' references to 'tendered' in payment notice templates"; |
| 84 |
} |
| 85 |
}, |
| 86 |
}; |