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;
30 use C4::Reserves qw|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',
55 my $patron1 = $builder->build({
58 categorycode => 'XYZ1',
61 my $patron2 = $builder->build({
64 categorycode => 'XYZ1',
67 my $patron3 = $builder->build({
71 # One biblio and two items
72 my $biblio = $builder->build_sample_biblio;
73 my $item1 = $builder->build_sample_item(
75 biblionumber => $biblio->biblionumber,
79 my $item2 = $builder->build_sample_item(
81 biblionumber => $biblio->biblionumber,
86 subtest 'GetReserveFee' => sub {
89 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
90 my $acc2 = acctlines( $patron2->{borrowernumber} );
91 my $res1 = addreserve( $patron1->{borrowernumber} );
93 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
94 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
95 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
96 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->title );
97 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
99 # If we delete the reserve, there should be no charge
100 $dbh->do( "DELETE FROM reserves WHERE borrowernumber = ?", undef, ( $patron1->{borrowernumber}) );
101 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
102 is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
104 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
105 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
106 is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
108 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
109 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->biblionumber );
110 is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
113 subtest 'Integration with AddReserve' => sub {
116 my $dbh = C4::Context->dbh;
118 subtest 'Items are not issued' => sub {
121 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
122 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
123 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
124 addreserve( $patron1->{borrowernumber} );
125 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - No fee charged for patron 1 if not issued' );
127 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
128 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
129 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
130 addreserve( $patron1->{borrowernumber} );
131 is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_placed - Patron should be always charged' );
133 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
134 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
135 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
136 addreserve( $patron1->{borrowernumber} );
137 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when placing a hold' );
140 subtest 'Items are issued' => sub {
143 C4::Circulation::AddIssue( $patron2, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
145 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
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} ), 0, 'not_always - Patron should not be charged if items are not all checked out' );
151 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
152 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
153 addreserve( $patron3->{borrowernumber} );
154 addreserve( $patron1->{borrowernumber} );
155 # FIXME Are we sure it's the expected behavior?
156 is( acctlines( $patron1->{borrowernumber} ), 1, 'not_always - Patron should be charged if all the items are not checked out and at least 1 hold is already placed' );
158 C4::Circulation::AddIssue( $patron3, $item2->barcode, '2015-12-31', 0, undef, 0, {} );
159 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
160 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
161 addreserve( $patron1->{borrowernumber} );
162 is( acctlines( $patron1->{borrowernumber} ), 1, 'not_always - Patron should be charged if all items are checked out' );
166 subtest 'Integration with AddIssue' => sub {
169 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
170 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->biblionumber );
171 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
173 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
174 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
175 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
177 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
178 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
179 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
180 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
182 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
183 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
184 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
185 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' );
187 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
188 my $id = addreserve( $patron1->{borrowernumber} );
189 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged yet (just checking to make sure)');
190 C4::Circulation::AddIssue( $patron1, $item1->barcode, '2015-12-31', 0, undef, 0, {} );
191 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' );
194 sub acctlines { #calculate number of accountlines for a patron
195 my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
202 branchcode => $library->{branchcode},
203 borrowernumber => $_[0],
204 biblionumber => $biblio->biblionumber,
206 title => $biblio->title,
211 $schema->storage->txn_rollback;