3 # This script includes tests for GetReserveFee and ChargeReserveFee
5 # Copyright 2015 Rijksmuseum
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Test::More tests => 3;
26 use t::lib::TestBuilder;
29 use C4::Circulation qw( AddIssue );
30 use C4::Reserves qw( GetReserveFee ChargeReserveFee AddReserve );
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new();
37 my $library = $builder->build({
40 my $mContext = Test::MockModule->new('C4::Context');
41 $mContext->mock( 'userenv', sub {
42 return { branch => $library->{branchcode} };
45 my $dbh = C4::Context->dbh; # after start transaction of testbuilder
47 # Category with hold fee, two patrons
51 categorycode => 'XYZ1',
58 categorycode => 'XYZ2',
62 my $patron1 = $builder->build({
65 categorycode => 'XYZ1',
68 my $patron2 = $builder->build({
71 categorycode => 'XYZ1',
74 my $patron3 = $builder->build({
77 my $patron4 = $builder->build({
80 categorycode => 'XYZ2',
84 # One biblio and two items
85 my $biblio = $builder->build_sample_biblio;
86 my $item1 = $builder->build_sample_item(
88 biblionumber => $biblio->biblionumber,
92 my $item2 = $builder->build_sample_item(
94 biblionumber => $biblio->biblionumber,
99 subtest 'GetReserveFee' => sub {
102 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
103 C4::Circulation::AddIssue( $patron3, $item2->barcode, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
104 my $acc2 = acctlines( $patron2->{borrowernumber} );
105 my $res1 = addreserve( $patron1->{borrowernumber} );
107 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
108 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
109 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
110 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->title );
111 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
113 # If we delete the reserve, there should be no charge
114 $dbh->do( "DELETE FROM reserves WHERE borrowernumber = ?", undef, ( $patron1->{borrowernumber}) );
115 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
116 is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
118 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
119 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
120 is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
122 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
123 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
124 is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
126 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
127 $fee = C4::Reserves::GetReserveFee( $patron4->{borrowernumber}, $biblio->biblionumber );
128 is( $fee, 0, 'HoldFeeMode=any_time_is_placed ; fee == 0, Patron 4 should not be charged' );
131 subtest 'Integration with AddReserve' => sub {
134 my $dbh = C4::Context->dbh;
136 subtest 'Items are not issued' => sub {
139 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
140 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
141 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
142 addreserve( $patron1->{borrowernumber} );
143 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - No fee charged for patron 1 if not issued' );
145 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
146 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
147 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
148 addreserve( $patron1->{borrowernumber} );
149 is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_placed - Patron should be always charged' );
151 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
152 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
153 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
154 addreserve( $patron1->{borrowernumber} );
155 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when placing a hold' );
158 subtest 'Items are issued' => sub {
161 $dbh->do( "DELETE FROM issues WHERE itemnumber=?", undef, $item1->itemnumber);
162 $dbh->do( "DELETE FROM issues WHERE itemnumber=?", undef, $item2->itemnumber);
163 C4::Circulation::AddIssue( $patron2, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
165 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
166 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
167 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
168 addreserve( $patron1->{borrowernumber} );
169 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if items are not all checked out' );
171 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
172 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
173 addreserve( $patron3->{borrowernumber} );
174 addreserve( $patron1->{borrowernumber} );
175 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if all the items are not checked out, even if 1 hold is already placed' );
177 C4::Circulation::AddIssue( $patron3, $item2->barcode, '2015-12-31', 0, undef, 0, {} );
178 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
179 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
180 addreserve( $patron1->{borrowernumber} );
181 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if all items are checked out but no holds are placed' );
183 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
184 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
185 addreserve( $patron3->{borrowernumber} );
186 addreserve( $patron1->{borrowernumber} );
187 is( acctlines( $patron1->{borrowernumber} ), 1, 'not_always - Patron should only be charged if all items are checked out and at least 1 hold is already placed' );
191 subtest 'Integration with AddIssue' => sub {
194 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
195 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
196 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
198 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
199 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
200 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
202 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
203 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
204 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
205 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
207 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
208 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
209 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
210 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when checking out an item which was not placed hold for him' );
212 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
213 my $id = addreserve( $patron1->{borrowernumber} );
214 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged yet (just checking to make sure)');
215 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
216 is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_collected - Patron should not be charged when checking out an item which was not placed hold for him' );
219 sub acctlines { #calculate number of accountlines for a patron
220 my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
227 branchcode => $library->{branchcode},
228 borrowernumber => $_[0],
229 biblionumber => $biblio->biblionumber,
231 title => $biblio->title,
236 $schema->storage->txn_rollback;