Bug 26734: (follow-up) Replace db update with backup and default

The inline update of variable for the notice template was proving
troublesome.. this patch replaces it was a backup of the existing notice
followed by replacing the notice with the default.

Test plan
1/ Run the database update
2/ Check that the templates have been replaced by the defaults (for all
languages enabled)
3/ Check that a backup of the original notices was made by naviagating
to view the action log (/tools/viewlog.pl) and selecting 'all modules' +
'all actions'

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Martin Renvoize 2021-02-03 14:49:04 +00:00 committed by Jonathan Druart
parent e6ed0c7151
commit 0bdfedc13e

View file

@ -1,45 +1,151 @@
$DBversion = 'XXX';
if( CheckVersion( $DBversion ) ) {
if ( CheckVersion($DBversion) ) {
# ACCOUNT_CREDIT UPDATES
# replace patron variable with credit.patron
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% patron', '[% credit.patron') WHERE code = 'ACCOUNT_CREDIT' ");
# replace library variable with credit.library.branchname
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% library', '[% credit.library.branchname') WHERE code = 'ACCOUNT_CREDIT' ");
# backup existing notice to action_logs
my $credit_arr = $dbh->selectall_arrayref(
"SELECT lang FROM letter WHERE code = 'ACCOUNT_CREDIT'", { Slice => {} });
my $c_sth = $dbh->prepare(q{
INSERT INTO action_logs ( timestamp, module, action, object, info, interface )
SELECT NOW(), 'NOTICES', 'UPGRADE', id, content, 'cli'
FROM letter
WHERE lang = ? AND code = 'ACCOUNT_CREDIT'
});
# replace offsets variable with credit.offsets
$dbh->do("UPDATE letter SET content = REPLACE(content, ' offsets %]', ' credit.offsets %]') WHERE code = 'ACCOUNT_CREDIT' ");
# replace change_given variable with change
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% change_given', '[% change') WHERE code = 'ACCOUNT_CREDIT' ");
for my $c ( @{$credit_arr} ) {
$c_sth->execute( $c->{lang} );
}
# replace accounts foreach with basic check
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% FOREACH account IN accounts %]', '[% IF credit %]') WHERE code = 'ACCOUNT_CREDIT' ");
# replace account with credit
$dbh->do("UPDATE letter SET content = REPLACE(content, 'account.', 'credit.') WHERE code = 'ACCOUNT_CREDIT' ");
# replace amountcredit with amount >= 0
$dbh->do("UPDATE letter SET content = REPLACE(content, '( credit.amountcredit )', '( credit.amount <= 0 )') WHERE code = 'ACCOUNT_CREDIT' ");
# replace notice with default
my $c_notice = q{
[% USE Price %]
[% PROCESS 'accounts.inc' %]
<table>
[% IF ( LibraryName ) %]
<tr>
<th colspan="4" class="centerednames">
<h3>[% LibraryName | html %]</h3>
</th>
</tr>
[% END %]
<tr>
<th colspan="4" class="centerednames">
<h2><u>Fee receipt</u></h2>
</th>
</tr>
<tr>
<th colspan="4" class="centerednames">
<h2>[% Branches.GetName( credit.patron.branchcode ) | html %]</h2>
</th>
</tr>
<tr>
<th colspan="4">
Received with thanks from [% credit.patron.firstname | html %] [% credit.patron.surname | html %] <br />
Card number: [% credit.patron.cardnumber | html %]<br />
</th>
</tr>
<tr>
<th>Date</th>
<th>Description of charges</th>
<th>Note</th>
<th>Amount</th>
</tr>
<tr class="highlight">
<td>[% credit.date | $KohaDates %]</td>
<td>
[% PROCESS account_type_description account=credit %]
[%- IF credit.description %], [% credit.description | html %][% END %]
</td>
<td>[% credit.note | html %]</td>
<td class="credit">[% credit.amount | $Price %]</td>
</tr>
<tfoot>
<tr>
<td colspan="3">Total outstanding dues as on date: </td>
[% IF ( credit.patron.account.balance >= 0 ) %]<td class="credit">[% ELSE %]<td class="debit">[% END %][% credit.patron.account.balance | $Price %]</td>
</tr>
</tfoot>
</table>
};
my $c_insert = $dbh->prepare("UPDATE letter SET content = ?, is_html = 1 WHERE code = 'ACCOUNT_CREDIT'");
$c_insert->execute($c_notice);
# ACCOUNT_DEBIT UPDATES
# replace patron variable with debit.patron
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% patron', '[% debit.patron') WHERE code = 'ACCOUNT_DEBIT' ");
# replace library variable with debit.library.branchname
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% library', '[% debit.library.branchname') WHERE code = 'ACCOUNT_DEBIT' ");
# replace offsets variable with debit.offsets
$dbh->do("UPDATE letter SET content = REPLACE(content, ' offsets %]', ' debit.offsets %]') WHERE code = 'ACCOUNT_DEBIT' ");
# backup existing notice to action_logs
my $debit_arr = $dbh->selectall_arrayref(
"SELECT lang FROM letter WHERE code = 'ACCOUNT_DEBIT'", { Slice => {} });
my $d_sth = $dbh->prepare(q{
INSERT INTO action_logs ( timestamp, module, action, object, info, interface )
SELECT NOW(), 'NOTICES', 'UPGRADE', id, content, 'cli'
FROM letter
WHERE lang = ? AND code = 'ACCOUNT_DEBIT'
});
# replace accounts foreach with basic check
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% FOREACH account IN accounts %]', '[% IF debit %]') WHERE code = 'ACCOUNT_DEBIT' ");
# replace account with debit
$dbh->do("UPDATE letter SET content = REPLACE(content, 'account.', 'debit.') WHERE code = 'ACCOUNT_DEBIT' ");
# replace amountcredit with amount >= 0
$dbh->do("UPDATE letter SET content = REPLACE(content, '( debit.amountcredit )', '( debit.amount <= 0 )') WHERE code = 'ACCOUNT_DEBIT' ");
# replace amountoutstandingcredit with amount >= 0
$dbh->do("UPDATE letter SET content = REPLACE(content, '( debit.amountoutstandingcredit )', '( debit.amountoutstanding <= 0 )') WHERE code = 'ACCOUNT_DEBIT' ");
for my $d ( @{$debit_arr} ) {
$d_sth->execute( $d->{lang} );
}
# replace total variable with debit.patron.account.balance
$dbh->do("UPDATE letter SET content = REPLACE(content, '[% total ', '[% debit.patron.account.balance ') WHERE code = 'ACCOUNT_DEBIT' ");
# replace totalcredit variable with debit.patron.account.balance <= 0
$dbh->do("UPDATE letter SET content = REPLACE(content, 'totalcredit', 'debit.patron.account.balance <= 0') WHERE code = 'ACCOUNT_DEBIT' ");
# replace notice with default
my $d_notice = q{
[% USE Price %]
[% PROCESS 'accounts.inc' %]
<table>
[% IF ( LibraryName ) %]
<tr>
<th colspan="5" class="centerednames">
<h3>[% LibraryName | html %]</h3>
</th>
</tr>
[% END %]
NewVersion( $DBversion, 26734, "Update notices to use standard variables");
<tr>
<th colspan="5" class="centerednames">
<h2><u>INVOICE</u></h2>
</th>
</tr>
<tr>
<th colspan="5" class="centerednames">
<h2>[% Branches.GetName( debit.patron.branchcode ) | html %]</h2>
</th>
</tr>
<tr>
<th colspan="5" >
Bill to: [% debit.patron.firstname | html %] [% debit.patron.surname | html %] <br />
Card number: [% debit.patron.cardnumber | html %]<br />
</th>
</tr>
<tr>
<th>Date</th>
<th>Description of charges</th>
<th>Note</th>
<th style="text-align:right;">Amount</th>
<th style="text-align:right;">Amount outstanding</th>
</tr>
<tr class="highlight">
<td>[% debit.date | $KohaDates%]</td>
<td>
[% PROCESS account_type_description account=debit %]
[%- IF debit.description %], [% debit.description | html %][% END %]
</td>
<td>[% debit.note | html %]</td>
<td class="debit">[% debit.amount | $Price %]</td>
<td class="debit">[% debit.amountoutstanding | $Price %]</td>
</tr>
<tfoot>
<tr>
<td colspan="4">Total outstanding dues as on date: </td>
[% IF ( debit.patron.account.balance <= 0 ) %]<td class="credit">[% ELSE %]<td class="debit">[% END %][% debit.patron.account.balance | $Price %]</td>
</tr>
</tfoot>
</table>
};
my $d_insert = $dbh->prepare("UPDATE letter SET content = ?, is_html = 1 WHERE code = 'ACCOUNT_DEBIT'");
$d_insert->execute($d_notice);
NewVersion( $DBversion, 26734, "Update notices to use defaults" );
}