3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Test
::More tests
=> 10;
27 use Koha
::DateUtils
qw( dt_from_string );
30 use Koha
::CirculationRules
;
32 use t
::lib
::TestBuilder
;
35 my $schema = Koha
::Database
->new->schema;
36 $schema->storage->txn_begin;
38 our $dbh = C4
::Context
->dbh;
40 $dbh->do(q
|DELETE FROM issues
|);
41 $dbh->do(q
|DELETE FROM circulation_rules
|);
43 my $builder = t
::lib
::TestBuilder
->new();
45 my $branch = $builder->build({
49 my $patron_category = $builder->build({ source
=> 'Category', value
=> { categorycode
=> 'NOT_X', category_type
=> 'P', enrolmentfee
=> 0 } });
50 my $patron = $builder->build_object({
51 class => 'Koha::Patrons',
53 branchcode
=> $branch->{branchcode
},
55 categorycode
=> $patron_category->{categorycode
},
58 my $patron_unblessed = $patron->unblessed;
60 my $item = $builder->build_sample_item(
62 library
=> $branch->{branchcode
},
66 Koha
::CirculationRules
->search()->delete();
67 Koha
::CirculationRules
->set_rules(
69 branchcode
=> $branch->{branchcode
},
70 categorycode
=> undef,
74 maxonsiteissueqty
=> 1,
78 hardduedatecompare
=> 0,
83 t
::lib
::Mocks
::mock_userenv
({ patron
=> $patron });
85 t
::lib
::Mocks
::mock_preference
('AllowTooManyOverride', 0);
88 C4
::Circulation
::AddIssue
( $patron_unblessed, $item->barcode, dt_from_string
, undef, dt_from_string
, undef, { onsite_checkout
=> 1 } );
90 my ( $impossible, $messages );
91 t
::lib
::Mocks
::mock_preference
('SwitchOnSiteCheckouts', 0);
92 ( $impossible, undef, undef, $messages ) = C4
::Circulation
::CanBookBeIssued
( $patron, $item->barcode );
93 is
( $impossible->{NO_RENEWAL_FOR_ONSITE_CHECKOUTS
}, 1, 'Do not renew on-site checkouts' );
95 t
::lib
::Mocks
::mock_preference
('SwitchOnSiteCheckouts', 1);
96 ( $impossible, undef, undef, $messages ) = C4
::Circulation
::CanBookBeIssued
( $patron, $item->barcode );
97 is
( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED
}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
98 is
( exists $impossible->{TOO_MANY
}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
99 C4
::Circulation
::AddIssue
( $patron_unblessed, $item->barcode, undef, undef, undef, undef, { switch_onsite_checkout
=> 1 } );
100 my $issue = Koha
::Checkouts
->find( { itemnumber
=> $item->itemnumber } );
101 is
( $issue->onsite_checkout, 0, 'The issue should have been switched to a regular checkout' );
102 my $five_days_after = dt_from_string
->add( days
=> 5 )->set( hour
=> 23, minute
=> 59, second
=> 0 );
103 is
( dt_from_string
($issue->date_due, 'sql'), $five_days_after, 'The date_due should have been set depending on the circ rules when the on-site checkout has been switched' );
106 t
::lib
::Mocks
::mock_preference
('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
107 my $another_item = $builder->build_sample_item(
109 biblionumber
=> $item->biblionumber,
110 library
=> $branch->{branchcode
},
114 C4
::Circulation
::AddIssue
( $patron_unblessed, $another_item->barcode, dt_from_string
, undef, dt_from_string
, undef, { onsite_checkout
=> 1 } );
115 ( $impossible, undef, undef, $messages ) = C4
::Circulation
::CanBookBeIssued
( $patron, $another_item->barcode );
116 is
( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED
}, 1, 'Specific case 1 - Switch is allowed' );
117 is
( exists $impossible->{TOO_MANY
}, '', 'Specific case 1 - Switch is allowed' );
119 my $yet_another_item = $builder->build_sample_item(
121 biblionumber
=> $item->biblionumber,
122 library
=> $branch->{branchcode
},
126 ( $impossible, undef, undef, undef ) = C4
::Circulation
::CanBookBeIssued
( $patron, $yet_another_item->barcode );
127 is
( $impossible->{TOO_MANY
}, 'TOO_MANY_CHECKOUTS', 'Not a specific case, $delta should not be incremented' );
129 Koha
::CirculationRules
->search()->delete();
130 Koha
::CirculationRules
->set_rules(
132 branchcode
=> $branch->{branchcode
},
133 categorycode
=> undef,
137 maxonsiteissueqty
=> 1,
141 ( $impossible, undef, undef, $messages ) = C4
::Circulation
::CanBookBeIssued
( $patron, $another_item->barcode );
142 is
( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED
}, 1, 'Specific case 2 - Switch is allowed' );
143 is
( exists $impossible->{TOO_MANY
}, '', 'Specific case 2 - Switch is allowed' );
145 $schema->storage->txn_rollback;