Bug 17680: C4::Circulation - Remove GetItemIssue, simple calls
[koha.git] / t / db_dependent / Circulation / SwitchOnSiteCheckouts.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
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>.
16
17 use Modern::Perl;
18 use Test::More tests => 10;
19 use C4::Context;
20
21 use C4::Biblio;
22 use C4::Members;
23 use C4::Circulation;
24 use C4::Items;
25 use C4::Context;
26
27 use Koha::DateUtils qw( dt_from_string );
28 use Koha::Database;
29 use Koha::Checkouts;
30
31 use t::lib::TestBuilder;
32 use t::lib::Mocks;
33
34 my $schema = Koha::Database->new->schema;
35 $schema->storage->txn_begin;
36
37 our $dbh = C4::Context->dbh;
38
39 $dbh->do(q|DELETE FROM branch_item_rules|);
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
42 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
43 $dbh->do(q|DELETE FROM default_circ_rules|);
44 $dbh->do(q|DELETE FROM default_branch_item_rules|);
45 $dbh->do(q|DELETE FROM issuingrules|);
46
47 my $builder = t::lib::TestBuilder->new();
48
49 my $branch = $builder->build({
50     source => 'Branch',
51 });
52
53 my $patron = $builder->build({
54     source => 'Borrower',
55     value => {
56         branchcode => $branch->{branchcode},
57         debarred => undef,
58     },
59 });
60
61 my $biblio = $builder->build({
62     source => 'Biblio',
63     value => {
64         branchcode => $branch->{branchcode},
65     },
66 });
67 my $item = $builder->build({
68     source => 'Item',
69     value => {
70         biblionumber => $biblio->{biblionumber},
71         homebranch => $branch->{branchcode},
72         holdingbranch => $branch->{branchcode},
73         notforloan => 0,
74         withdrawn => 0,
75         lost => 0,
76     },
77 });
78
79 my $issuingrule = $builder->build({
80     source => 'Issuingrule',
81     value => {
82         branchcode         => $branch->{branchcode},
83         categorycode       => '*',
84         itemtype           => '*',
85         maxissueqty        => 2,
86         maxonsiteissueqty  => 1,
87         lengthunit         => 'days',
88         issuelength        => 5,
89     },
90 });
91
92 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
93 C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
94
95 t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
96
97 # Add onsite checkout
98 C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
99
100 my ( $impossible, $messages );
101 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 0);
102 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
103 is( $impossible->{NO_RENEWAL_FOR_ONSITE_CHECKOUTS}, 1, 'Do not renew on-site checkouts' );
104
105 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 1);
106 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
107 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
108 is( exists $impossible->{TOO_MANY}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
109 C4::Circulation::AddIssue( $patron, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
110 my $issue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
111 is( $issue->onsite_checkout, 0, 'The issue should have been switched to a regular checkout' );
112 my $five_days_after = dt_from_string->add( days => 5 )->set( hour => 23, minute => 59, second => 0 );
113 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' );
114
115 # Specific case
116 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
117 my $another_item = $builder->build({
118     source => 'Item',
119     value => {
120         biblionumber => $biblio->{biblionumber},
121         homebranch => $branch->{branchcode},
122         holdingbranch => $branch->{branchcode},
123         notforloan => 0,
124         withdrawn => 0,
125         lost => 0,
126     },
127 });
128
129 C4::Circulation::AddIssue( $patron, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
130 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
131 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 1 - Switch is allowed' );
132 is( exists $impossible->{TOO_MANY}, '', 'Specific case 1 - Switch is allowed' );
133
134 my $yet_another_item = $builder->build({
135     source => 'Item',
136     value => {
137         biblionumber => $biblio->{biblionumber},
138         homebranch => $branch->{branchcode},
139         holdingbranch => $branch->{branchcode},
140         notforloan => 0,
141         withdrawn => 0,
142         lost => 0,
143     },
144 });
145 ( $impossible, undef, undef, undef ) = C4::Circulation::CanBookBeIssued( $patron, $yet_another_item->{barcode} );
146 is( $impossible->{TOO_MANY}, 'TOO_MANY_CHECKOUTS', 'Not a specific case, $delta should not be incremented' );
147
148 $dbh->do(q|DELETE FROM issuingrules|);
149 my $borrower_circ_rule = $builder->build({
150     source => 'DefaultCircRule',
151     value => {
152         branchcode         => $branch->{branchcode},
153         categorycode       => '*',
154         maxissueqty        => 2,
155         maxonsiteissueqty  => 1,
156     },
157 });
158 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
159 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 2 - Switch is allowed' );
160 is( exists $impossible->{TOO_MANY}, '', 'Specific case 2 - Switch is allowed' );
161
162 $schema->storage->txn_rollback;
163
164 1;