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 => 9;
27 use Koha::DateUtils qw( dt_from_string );
30 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 our $dbh = C4::Context->dbh;
38 $dbh->do(q|DELETE FROM branch_item_rules|);
39 $dbh->do(q|DELETE FROM issues|);
40 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
41 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
42 $dbh->do(q|DELETE FROM default_circ_rules|);
43 $dbh->do(q|DELETE FROM default_branch_item_rules|);
44 $dbh->do(q|DELETE FROM issuingrules|);
46 my $builder = t::lib::TestBuilder->new();
48 my $branch = $builder->build({
52 my $patron = $builder->build({
55 branchcode => $branch->{branchcode},
59 my $biblio = $builder->build({
62 branchcode => $branch->{branchcode},
65 my $item = $builder->build({
68 biblionumber => $biblio->{biblionumber},
69 homebranch => $branch->{branchcode},
70 holdingbranch => $branch->{branchcode},
74 my $issuingrule = $builder->build({
75 source => 'Issuingrule',
77 branchcode => $branch->{branchcode},
81 maxonsiteissueqty => 1,
87 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
88 C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
91 C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
93 my ( $impossible, $messages );
94 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 0);
95 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
96 is( $impossible->{NO_RENEWAL_FOR_ONSITE_CHECKOUTS}, 1, 'Do not renew on-site checkouts' );
98 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 1);
99 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
100 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
101 is( exists $impossible->{TOO_MANY}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
102 C4::Circulation::AddIssue( $patron, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
103 my $issue = C4::Circulation::GetItemIssue( $item->{itemnumber} );
104 is( $issue->{onsite_checkout}, 0, 'The issue should have been switched to a regular checkout' );
105 my $five_days_after = dt_from_string->add( days => 5 )->set( hour => 23, minute => 59, second => 0 );
106 is( $issue->{date_due}, $five_days_after, 'The date_due should have been set depending on the circ rules when the on-site checkout has been switched' );
109 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
110 my $another_item = $builder->build({
113 biblionumber => $biblio->{biblionumber},
114 homebranch => $branch->{branchcode},
115 holdingbranch => $branch->{branchcode},
119 C4::Circulation::AddIssue( $patron, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
120 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
121 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 1 - Switch is allowed' );
122 is( exists $impossible->{TOO_MANY}, '', 'Specific case 1 - Switch is allowed' );
124 $dbh->do(q|DELETE FROM issuingrules|);
125 my $borrower_circ_rule = $builder->build({
126 source => 'DefaultCircRule',
128 branchcode => $branch->{branchcode},
131 maxonsiteissueqty => 1,
134 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
135 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 2 - Switch is allowed' );
136 is( exists $impossible->{TOO_MANY}, '', 'Specific case 2 - Switch is allowed' );
138 $schema->storage->txn_rollback;