Bug 31051: (follow-up) Tests for get_savings and more

- Added tests in t/db_dependent/Koha/Patron.t
- Added wording to OPACShowSavings syspref about anonymised checkout
history
- Added IDs to the savings messages on the OPAC
- Prevent explosion if a checked out item has been deleted

Sponsored-by: Horowhenua Libraries Trust
Signed-off-by: Hammat Wele <hammat.wele@inlibro.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Aleisha Amohia 2022-08-29 16:30:01 +12:00 committed by Tomas Cohen Arazi
parent 4bd0cb39ab
commit f177d30ab4
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
5 changed files with 45 additions and 17 deletions

View file

@ -2441,22 +2441,20 @@ sub get_savings {
my $savings = 0;
# get old issues
my $old_issues_rs = $self->_result->old_issues;
my @old_itemnumbers = $old_issues_rs->get_column('itemnumber')->all;
foreach my $itemnumber ( @old_itemnumbers ) {
my $item = Koha::Items->find( $itemnumber );
$savings += $item->replacementprice;
# get old checkouts
my @old_checkouts = $self->old_checkouts->as_list;
foreach my $old_checkout ( @old_checkouts ) {
if ( $old_checkout->item ) {
$savings += $old_checkout->item->replacementprice;
}
}
# get current issues
my $issues_rs = $self->_result->issues;
my @itemnumbers = $issues_rs->get_column('itemnumber')->all;
foreach my $itemnumber ( @itemnumbers ) {
my $item = Koha::Items->find( $itemnumber );
$savings += $item->replacementprice;
# get current checkouts
my @checkouts = $self->checkouts->as_list;
foreach my $checkout ( @checkouts ) {
if ( $checkout->item ) {
$savings += $checkout->item->replacementprice;
}
}
return $savings;

View file

@ -537,6 +537,7 @@ OPAC:
checkouthistory: "on patron's checkout history page (the system preference <em>opacreadinghistory</em> must be enabled)"
summary: "in user summary box on OPAC homepage (the system preference <em>OPACUserSummary</em> must be enabled)"
user: "on patron's 'your summary' page"
- ". Note that displayed savings may be inaccurate if checkout history is anonymized."
OpenURL:
-
- 'Complete URL of OpenURL resolver (starting with <code>http://</code> or <code>https://</code>):'

View file

@ -56,7 +56,7 @@
<div id="opac-user-readingrec">
[% IF savings %]
<div class="alert alert-info">
<div class="alert alert-info" id="savings">
Congratulations, you have saved a total of [% savings | $Price with_symbol => 1 %] by using the library.
</div>
[% END %]

View file

@ -163,7 +163,7 @@
[% END # / IF patron_flagged %]
[% IF savings %]
<div class="alert alert-info">
<div class="alert alert-info" id="savings">
Congratulations, you have saved a total of [% savings | $Price with_symbol => 1 %] by using the library.
</div>
[% END %]

View file

@ -19,7 +19,7 @@
use Modern::Perl;
use Test::More tests => 19;
use Test::More tests => 20;
use Test::Exception;
use Test::Warn;
@ -29,6 +29,7 @@ use Koha::DateUtils qw(dt_from_string);
use Koha::ArticleRequests;
use Koha::Patrons;
use Koha::Patron::Relationships;
use C4::Circulation qw( AddIssue AddReturn );
use t::lib::TestBuilder;
use t::lib::Mocks;
@ -1349,3 +1350,31 @@ subtest 'notify_library_of_registration()' => sub {
$schema->storage->txn_rollback;
};
subtest 'get_savings tests' => sub {
plan tests => 2;
$schema->storage->txn_begin;
my $library = $builder->build_object({ class => 'Koha::Libraries' });
my $patron = $builder->build_object({ class => 'Koha::Patrons' }, { value => { branchcode => $library->branchcode } });
t::lib::Mocks::mock_userenv({ patron => $patron, branchcode => $library->branchcode });
my $biblio1 = $builder->build_object({ class => 'Koha::Biblios' });
my $item1 = $builder->build_object({ class => 'Koha::Items' }, { value => { biblionumber => $biblio1->biblionumber, replacementprice => '5.00', holdingbranch => $library->branchcode, homebranch => $library->branchcode } });
my $item2 = $builder->build_object({ class => 'Koha::Items' }, { value => { biblionumber => $biblio1->biblionumber, replacementprice => '5.00', holdingbranch => $library->branchcode, homebranch => $library->branchcode } });
AddIssue( $patron->unblessed, $item1->barcode );
AddIssue( $patron->unblessed, $item2->barcode );
my $savings = $patron->get_savings;
is( $savings, $item1->replacementprice + $item2->replacementprice, "Savings correctly calculated from current issues" );
AddReturn( $item2->barcode, $item2->homebranch );
$savings = $patron->get_savings;
is( $savings, $item1->replacementprice + $item2->replacementprice, "Savings correctly calculated from current and old issues" );
$schema->storage->txn_rollback;
};