1cb1a65d1d4949b06de096d505e83394bdb9d76f
[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_category = $builder->build({ source => 'Category', value => { category_type => 'P', enrolmentfee => 0 } });
54 my $patron = $builder->build({
55     source => 'Borrower',
56     value => {
57         branchcode => $branch->{branchcode},
58         debarred => undef,
59         categorycode => $patron_category->{categorycode},
60     },
61 });
62
63 my $biblio = $builder->build({
64     source => 'Biblio',
65     value => {
66         branchcode => $branch->{branchcode},
67     },
68 });
69 my $item = $builder->build({
70     source => 'Item',
71     value => {
72         biblionumber => $biblio->{biblionumber},
73         homebranch => $branch->{branchcode},
74         holdingbranch => $branch->{branchcode},
75         notforloan => 0,
76         withdrawn => 0,
77         lost => 0,
78     },
79 });
80
81 my $issuingrule = $builder->build({
82     source => 'Issuingrule',
83     value => {
84         branchcode         => $branch->{branchcode},
85         categorycode       => '*',
86         itemtype           => '*',
87         maxissueqty        => 2,
88         maxonsiteissueqty  => 1,
89         lengthunit         => 'days',
90         issuelength        => 5,
91     },
92 });
93
94 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
95 C4::Context->set_userenv($patron->{borrowernumber}, $patron->{userid}, 'usercnum', 'First name', 'Surname', $branch->{branchcode}, 'My Library', 0);
96
97 t::lib::Mocks::mock_preference('AllowTooManyOverride', 0);
98
99 # Add onsite checkout
100 C4::Circulation::AddIssue( $patron, $item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
101
102 my ( $impossible, $messages );
103 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 0);
104 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
105 is( $impossible->{NO_RENEWAL_FOR_ONSITE_CHECKOUTS}, 1, 'Do not renew on-site checkouts' );
106
107 t::lib::Mocks::mock_preference('SwitchOnSiteCheckouts', 1);
108 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $item->{barcode} );
109 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
110 is( exists $impossible->{TOO_MANY}, '', 'If SwitchOnSiteCheckouts, switch the on-site checkout' );
111 C4::Circulation::AddIssue( $patron, $item->{barcode}, undef, undef, undef, undef, { switch_onsite_checkout => 1 } );
112 my $issue = Koha::Checkouts->find( { itemnumber => $item->{itemnumber} } );
113 is( $issue->onsite_checkout, 0, 'The issue should have been switched to a regular checkout' );
114 my $five_days_after = dt_from_string->add( days => 5 )->set( hour => 23, minute => 59, second => 0 );
115 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' );
116
117 # Specific case
118 t::lib::Mocks::mock_preference('ConsiderOnSiteCheckoutsAsNormalCheckouts', 1);
119 my $another_item = $builder->build({
120     source => 'Item',
121     value => {
122         biblionumber => $biblio->{biblionumber},
123         homebranch => $branch->{branchcode},
124         holdingbranch => $branch->{branchcode},
125         notforloan => 0,
126         withdrawn => 0,
127         lost => 0,
128     },
129 });
130
131 C4::Circulation::AddIssue( $patron, $another_item->{barcode}, dt_from_string, undef, dt_from_string, undef, { onsite_checkout => 1 } );
132 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
133 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 1 - Switch is allowed' );
134 is( exists $impossible->{TOO_MANY}, '', 'Specific case 1 - Switch is allowed' );
135
136 my $yet_another_item = $builder->build({
137     source => 'Item',
138     value => {
139         biblionumber => $biblio->{biblionumber},
140         homebranch => $branch->{branchcode},
141         holdingbranch => $branch->{branchcode},
142         notforloan => 0,
143         withdrawn => 0,
144         lost => 0,
145     },
146 });
147 ( $impossible, undef, undef, undef ) = C4::Circulation::CanBookBeIssued( $patron, $yet_another_item->{barcode} );
148 is( $impossible->{TOO_MANY}, 'TOO_MANY_CHECKOUTS', 'Not a specific case, $delta should not be incremented' );
149
150 $dbh->do(q|DELETE FROM issuingrules|);
151 my $borrower_circ_rule = $builder->build({
152     source => 'DefaultCircRule',
153     value => {
154         branchcode         => $branch->{branchcode},
155         categorycode       => '*',
156         maxissueqty        => 2,
157         maxonsiteissueqty  => 1,
158     },
159 });
160 ( $impossible, undef, undef, $messages ) = C4::Circulation::CanBookBeIssued( $patron, $another_item->{barcode} );
161 is( $messages->{ONSITE_CHECKOUT_WILL_BE_SWITCHED}, 1, 'Specific case 2 - Switch is allowed' );
162 is( exists $impossible->{TOO_MANY}, '', 'Specific case 2 - Switch is allowed' );
163
164 $schema->storage->txn_rollback;
165