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 = new Test::MockModule('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({
78 my $item1 = $builder->build({
81 biblionumber => $biblio->{biblionumber},
85 my $item2 = $builder->build({
88 biblionumber => $biblio->{biblionumber},
93 subtest 'GetReserveFee' => sub {
96 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} ); # the date does not really matter
97 my $acc2 = acctlines( $patron2->{borrowernumber} );
98 my $res1 = addreserve( $patron1->{borrowernumber} );
100 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
101 my $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
102 is( $fee > 0, 1, 'Patron 2 should be charged cf GetReserveFee' );
103 C4::Reserves::ChargeReserveFee( $patron2->{borrowernumber}, $fee, $biblio->{title} );
104 is( acctlines( $patron2->{borrowernumber} ), $acc2 + 1, 'Patron 2 has been charged by ChargeReserveFee' );
106 # If we delete the reserve, there should be no charge
107 $dbh->do( "DELETE FROM reserves WHERE borrowernumber = ?", undef, ( $patron1->{borrowernumber}) );
108 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
109 is( $fee, 0, 'HoldFeeMode=not_always, Patron 2 should not be charged' );
111 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
112 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
113 is( int($fee), 2, 'HoldFeeMode=any_time_is_placed, Patron 2 should be charged' );
115 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
116 $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblionumber} );
117 is( int($fee), 2, 'HoldFeeMode=any_time_is_collected, Patron 2 should be charged' );
120 subtest 'Integration with AddReserve' => sub {
123 my $dbh = C4::Context->dbh;
125 subtest 'Items are not issued' => sub {
128 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
129 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
130 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
131 addreserve( $patron1->{borrowernumber} );
132 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - No fee charged for patron 1 if not issued' );
134 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
135 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
136 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
137 addreserve( $patron1->{borrowernumber} );
138 is( acctlines( $patron1->{borrowernumber} ), 1, 'any_time_is_placed - Patron should be always charged' );
140 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
141 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
142 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
143 addreserve( $patron1->{borrowernumber} );
144 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged when placing a hold' );
147 subtest 'Items are issued' => sub {
150 C4::Circulation::AddIssue( $patron2, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
152 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
153 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
154 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
155 addreserve( $patron1->{borrowernumber} );
156 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged if items are not all checked out' );
158 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
159 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
160 addreserve( $patron3->{borrowernumber} );
161 addreserve( $patron1->{borrowernumber} );
162 # FIXME Are we sure it's the expected behavior?
163 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' );
165 C4::Circulation::AddIssue( $patron3, $item2->{barcode}, '2015-12-31', 0, undef, 0, {} );
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} ), 1, 'not_always - Patron should be charged if all items are checked out' );
173 subtest 'Integration with AddIssue' => sub {
176 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
177 $dbh->do( "DELETE FROM reserves WHERE biblionumber=?", undef, $biblio->{biblionumber} );
178 $dbh->do( "DELETE FROM accountlines WHERE borrowernumber=?", undef, $patron1->{borrowernumber} );
180 t::lib::Mocks::mock_preference('HoldFeeMode', 'not_always');
181 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
182 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
184 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_placed');
185 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
186 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
187 is( acctlines( $patron1->{borrowernumber} ), 0, 'not_always - Patron should not be charged' );
189 t::lib::Mocks::mock_preference('HoldFeeMode', 'any_time_is_collected');
190 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
191 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
192 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' );
194 $dbh->do( "DELETE FROM issues WHERE borrowernumber = ?", undef, $patron1->{borrowernumber} );
195 my $id = addreserve( $patron1->{borrowernumber} );
196 is( acctlines( $patron1->{borrowernumber} ), 0, 'any_time_is_collected - Patron should not be charged yet (just checking to make sure)');
197 C4::Circulation::AddIssue( $patron1, $item1->{barcode}, '2015-12-31', 0, undef, 0, {} );
198 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' );
201 sub acctlines { #calculate number of accountlines for a patron
202 my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) );
208 $library->{branchcode},
210 $biblio->{biblionumber},
222 $schema->storage->txn_rollback;