Bug 18925: Move maxissueqty and maxonsiteissueqty to circulation_rules
[koha.git] / t / db_dependent / Circulation / Branch.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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use C4::Circulation;
21 use C4::Items;
22 use C4::Biblio;
23 use C4::Context;
24 use Koha::CirculationRules;
25
26 use Koha::Patrons;
27
28 use Test::More tests => 14;
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 BEGIN {
33     use_ok('C4::Circulation');
34 }
35
36 can_ok( 'C4::Circulation', qw(
37     AddIssue
38     AddReturn
39     GetBranchBorrowerCircRule
40     GetBranchItemRule
41     )
42 );
43
44 my $schema = Koha::Database->schema;
45 $schema->storage->txn_begin;
46 my $dbh = C4::Context->dbh;
47
48 $dbh->do(q|DELETE FROM issues|);
49 $dbh->do(q|DELETE FROM items|);
50 $dbh->do(q|DELETE FROM borrowers|);
51 $dbh->do(q|DELETE FROM clubs|);
52 $dbh->do(q|DELETE FROM branches|);
53 $dbh->do(q|DELETE FROM categories|);
54 $dbh->do(q|DELETE FROM accountlines|);
55 $dbh->do(q|DELETE FROM itemtypes|);
56 $dbh->do(q|DELETE FROM branch_item_rules|);
57 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
58 $dbh->do(q|DELETE FROM default_circ_rules|);
59 $dbh->do(q|DELETE FROM default_branch_item_rules|);
60
61 my $builder = t::lib::TestBuilder->new();
62
63 # Add branch
64 my $samplebranch1 = $builder->build({ source => 'Branch' });
65 my $samplebranch2 = $builder->build({ source => 'Branch' });
66 # Add itemtypes
67 my $no_circ_itemtype = $builder->build({
68     source => 'Itemtype',
69     value => {
70         rentalcharge => '0',
71         notforloan   => 0
72     }
73 });
74 my $sampleitemtype1 = $builder->build({
75     source => 'Itemtype',
76     value => {
77         rentalcharge => '10.0',
78         notforloan   => 1
79     }
80 });
81 my $sampleitemtype2 = $builder->build({
82     source => 'Itemtype',
83     value => {
84         rentalcharge => '5.0',
85         notforloan   => 0
86     }
87 });
88 # Add Category
89 my $samplecat     = $builder->build({
90     source => 'Category',
91     value => {
92         hidelostitems => 0
93     }
94 });
95
96 #Add biblio and item
97 my $record = MARC::Record->new();
98 $record->append_fields(
99     MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
100 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
101
102 # item 1 has home branch and holding branch samplebranch1
103 my @sampleitem1 = C4::Items::AddItem(
104     {
105         barcode        => 'barcode_1',
106         itemcallnumber => 'callnumber1',
107         homebranch     => $samplebranch1->{branchcode},
108         holdingbranch  => $samplebranch1->{branchcode},
109         itype          => $no_circ_itemtype->{ itemtype }
110     },
111     $biblionumber
112 );
113 my $item_id1    = $sampleitem1[2];
114
115 # item 2 has holding branch samplebranch2
116 my @sampleitem2 = C4::Items::AddItem(
117     {
118         barcode        => 'barcode_2',
119         itemcallnumber => 'callnumber2',
120         homebranch     => $samplebranch2->{branchcode},
121         holdingbranch  => $samplebranch1->{branchcode},
122         itype          => $no_circ_itemtype->{ itemtype }
123     },
124     $biblionumber
125 );
126 my $item_id2 = $sampleitem2[2];
127
128 # item 3 has item type sampleitemtype2 with noreturn policy
129 my @sampleitem3 = C4::Items::AddItem(
130     {
131         barcode        => 'barcode_3',
132         itemcallnumber => 'callnumber3',
133         homebranch     => $samplebranch2->{branchcode},
134         holdingbranch  => $samplebranch2->{branchcode},
135         itype          => $sampleitemtype2->{itemtype}
136     },
137     $biblionumber
138 );
139 my $item_id3 = $sampleitem3[2];
140
141 #Add borrower
142 my $borrower_id1 = Koha::Patron->new({
143     firstname    => 'firstname1',
144     surname      => 'surname1 ',
145     categorycode => $samplecat->{categorycode},
146     branchcode   => $samplebranch1->{branchcode},
147 })->store->borrowernumber;
148
149 is_deeply(
150     GetBranchBorrowerCircRule(),
151     { maxissueqty => undef, maxonsiteissueqty => undef },
152 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
153 );
154
155 Koha::CirculationRules->set_rules(
156     {
157         branchcode   => $samplebranch1->{branchcode},
158         categorycode => $samplecat->{categorycode},
159         itemtype     => undef,
160         rules        => {
161             maxissueqty       => 5,
162             maxonsiteissueqty => 6,
163         }
164     }
165 );
166
167 my $query = q|
168     INSERT INTO default_branch_circ_rules
169     (branchcode, holdallowed, returnbranch)
170     VALUES( ?, ?, ? )
171 |;
172 $dbh->do( $query, {}, $samplebranch2->{branchcode}, 1, 'holdingbranch' );
173 Koha::CirculationRules->set_rules(
174     {
175         branchcode   => $samplebranch2->{branchcode},
176         categorycode => undef,
177         itemtype     => undef,
178         rules        => {
179             maxissueqty       => 3,
180             maxonsiteissueqty => 2,
181         }
182     }
183 );
184
185 $query = q|
186     INSERT INTO default_circ_rules
187     (singleton, holdallowed, returnbranch)
188     VALUES( ?, ?, ? )
189 |;
190 $dbh->do( $query, {}, 'singleton', 3, 'homebranch' );
191 Koha::CirculationRules->set_rules(
192     {
193         branchcode   => undef,
194         categorycode => undef,
195         itemtype     => undef,
196         rules        => {
197             maxissueqty       => 4,
198             maxonsiteissueqty => 5,
199         }
200     }
201 );
202
203 $query =
204 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
205 my $sth = $dbh->prepare($query);
206 $sth->execute(
207     $samplebranch1->{branchcode},
208     $sampleitemtype1->{itemtype},
209     5, 'homebranch'
210 );
211 $sth->execute(
212     $samplebranch2->{branchcode},
213     $sampleitemtype1->{itemtype},
214     5, 'holdingbranch'
215 );
216 $sth->execute(
217     $samplebranch2->{branchcode},
218     $sampleitemtype2->{itemtype},
219     5, 'noreturn'
220 );
221
222 #Test GetBranchBorrowerCircRule
223 is_deeply(
224     GetBranchBorrowerCircRule(),
225     { maxissueqty => 4, maxonsiteissueqty => 5 },
226 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
227 );
228 is_deeply(
229     GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
230     { maxissueqty => 3, maxonsiteissueqty => 2 },
231 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
232 );
233 is_deeply(
234     GetBranchBorrowerCircRule(
235         $samplebranch1->{branchcode},
236         $samplecat->{categorycode}
237     ),
238     { maxissueqty => 5, maxonsiteissueqty => 6 },
239     "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
240 );
241 is_deeply(
242     GetBranchBorrowerCircRule( -1, -1 ),
243     { maxissueqty => 4, maxonsiteissueqty => 5 },
244 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
245 );
246
247 #Test GetBranchItemRule
248 my @lazy_any = ( 'hold_fulfillment_policy' => 'any' );
249 is_deeply(
250     GetBranchItemRule(
251         $samplebranch1->{branchcode},
252         $sampleitemtype1->{itemtype},
253     ),
254     { returnbranch => 'homebranch', holdallowed => 5, @lazy_any },
255     "GetBranchitem returns holdallowed and return branch"
256 );
257 is_deeply(
258     GetBranchItemRule(),
259     { returnbranch => 'homebranch', holdallowed => 3, @lazy_any },
260 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
261 );
262 is_deeply(
263     GetBranchItemRule( $samplebranch2->{branchcode} ),
264     { returnbranch => 'holdingbranch', holdallowed => 1, @lazy_any },
265 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
266 );
267 is_deeply(
268     GetBranchItemRule( -1, -1 ),
269     { returnbranch => 'homebranch', holdallowed => 3, @lazy_any },
270     "With only one parametern GetBranchItemRule returns default values"
271 );
272
273 # Test return policies
274 t::lib::Mocks::mock_preference('AutomaticItemReturn','0');
275
276 # item1 returned at branch2 should trigger transfer to homebranch
277 $query =
278 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
279 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
280
281 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
282     $samplebranch2->{branchcode});
283 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
284
285 # item2 returned at branch2 should trigger transfer to holding branch
286 $query =
287 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
288 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
289 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
290     $samplebranch2->{branchcode});
291 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
292
293 # item3 should not trigger transfer - floating collection
294 $query =
295 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
296 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
297 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
298 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
299     $samplebranch1->{branchcode});
300 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");
301 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
302
303 $schema->storage->txn_rollback;
304