From ddebdd435447f869fbb73ab710e5652233421d98 Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Mon, 29 Aug 2022 16:30:01 +1200 Subject: [PATCH] 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 --- Koha/Patron.pm | 26 +++++++--------- .../en/modules/admin/preferences/opac.pref | 1 + .../en/modules/opac-readingrecord.tt | 2 +- .../bootstrap/en/modules/opac-user.tt | 2 +- t/db_dependent/Koha/Patron.t | 31 ++++++++++++++++++- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index a82fe54ea11..5966164bcb2 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -2294,22 +2294,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; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref index 0a8c0528ed7..0950db81217 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/opac.pref @@ -536,6 +536,7 @@ OPAC: checkouthistory: "on patron's checkout history page (the system preference opacreadinghistory must be enabled)" summary: "in user summary box on OPAC homepage (the system preference OPACUserSummary 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 http:// or https://):' diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt index 65a62334d86..1e80809a8aa 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-readingrecord.tt @@ -56,7 +56,7 @@
[% IF savings %] -
+
Congratulations, you have saved a total of [% savings | $Price with_symbol => 1 %] by using the library.
[% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt index ca07ce0832b..d29163d0258 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-user.tt @@ -163,7 +163,7 @@ [% END # / IF patron_flagged %] [% IF savings %] -
+
Congratulations, you have saved a total of [% savings | $Price with_symbol => 1 %] by using the library.
[% END %] diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index ae8a7eed466..eafc731ca0e 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -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; +}; -- 2.20.1