Bug 26518: Use Koha::Biblio[item] in AddBiblio
[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 => 15;
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 my $query;
48
49 $dbh->do(q|DELETE FROM issues|);
50 $dbh->do(q|DELETE FROM items|);
51 $dbh->do(q|DELETE FROM borrowers|);
52 $dbh->do(q|DELETE FROM clubs|);
53 $dbh->do(q|DELETE FROM branches|);
54 $dbh->do(q|DELETE FROM categories|);
55 $dbh->do(q|DELETE FROM accountlines|);
56 $dbh->do(q|DELETE FROM itemtypes|);
57 $dbh->do(q|DELETE FROM circulation_rules|);
58
59 my $builder = t::lib::TestBuilder->new();
60
61 # Add branch
62 my $samplebranch1 = $builder->build({ source => 'Branch' });
63 my $samplebranch2 = $builder->build({ source => 'Branch' });
64 # Add itemtypes
65 my $no_circ_itemtype = $builder->build({
66     source => 'Itemtype',
67     value => {
68         rentalcharge => '0',
69         notforloan   => 0
70     }
71 });
72 my $sampleitemtype1 = $builder->build({
73     source => 'Itemtype',
74     value => {
75         rentalcharge => '10.0',
76         notforloan   => 1
77     }
78 });
79 my $sampleitemtype2 = $builder->build({
80     source => 'Itemtype',
81     value => {
82         rentalcharge => '5.0',
83         notforloan   => 0
84     }
85 });
86 # Add Category
87 my $samplecat     = $builder->build({
88     source => 'Category',
89     value => {
90         hidelostitems => 0
91     }
92 });
93
94 #Add biblio and item
95 my $record = MARC::Record->new();
96 $record->append_fields(
97     MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
98 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
99
100 # item 1 has home branch and holding branch samplebranch1
101 my @sampleitem1 = C4::Items::AddItem(
102     {
103         barcode        => 'barcode_1',
104         itemcallnumber => 'callnumber1',
105         homebranch     => $samplebranch1->{branchcode},
106         holdingbranch  => $samplebranch1->{branchcode},
107         itype          => $no_circ_itemtype->{ itemtype }
108     },
109     $biblionumber
110 );
111 my $item_id1    = $sampleitem1[2];
112
113 # item 2 has holding branch samplebranch2
114 my @sampleitem2 = C4::Items::AddItem(
115     {
116         barcode        => 'barcode_2',
117         itemcallnumber => 'callnumber2',
118         homebranch     => $samplebranch2->{branchcode},
119         holdingbranch  => $samplebranch1->{branchcode},
120         itype          => $no_circ_itemtype->{ itemtype }
121     },
122     $biblionumber
123 );
124 my $item_id2 = $sampleitem2[2];
125
126 # item 3 has item type sampleitemtype2 with noreturn policy
127 my @sampleitem3 = C4::Items::AddItem(
128     {
129         barcode        => 'barcode_3',
130         itemcallnumber => 'callnumber3',
131         homebranch     => $samplebranch2->{branchcode},
132         holdingbranch  => $samplebranch2->{branchcode},
133         itype          => $sampleitemtype2->{itemtype}
134     },
135     $biblionumber
136 );
137 my $item_id3 = $sampleitem3[2];
138
139 #Add borrower
140 my $borrower_id1 = Koha::Patron->new({
141     firstname    => 'firstname1',
142     surname      => 'surname1 ',
143     categorycode => $samplecat->{categorycode},
144     branchcode   => $samplebranch1->{branchcode},
145 })->store->borrowernumber;
146
147 is_deeply(
148     GetBranchBorrowerCircRule(),
149     { patron_maxissueqty => undef, patron_maxonsiteissueqty => undef },
150 "Without parameter, GetBranchBorrower returns undef (unilimited) for patron_maxissueqty and patron_maxonsiteissueqty if no rules defined"
151 );
152
153 Koha::CirculationRules->set_rules(
154     {
155         branchcode   => $samplebranch1->{branchcode},
156         categorycode => $samplecat->{categorycode},
157         itemtype     => undef,
158         rules        => {
159             patron_maxissueqty       => 5,
160             patron_maxonsiteissueqty => 6,
161         }
162     }
163 );
164
165 Koha::CirculationRules->set_rules(
166     {
167         branchcode   => $samplebranch2->{branchcode},
168         categorycode => undef,
169         itemtype     => undef,
170         rules        => {
171             patron_maxissueqty       => 3,
172             patron_maxonsiteissueqty => 2,
173             holdallowed              => 1,
174             returnbranch             => 'holdingbranch',
175         }
176     }
177 );
178
179 Koha::CirculationRules->set_rules(
180     {
181         branchcode   => undef,
182         categorycode => undef,
183         itemtype     => undef,
184         rules        => {
185             patron_maxissueqty       => 4,
186             patron_maxonsiteissueqty => 5,
187             holdallowed              => 3,
188             returnbranch             => 'homebranch',
189         }
190     }
191 );
192
193 Koha::CirculationRules->set_rules(
194     {
195         branchcode   => $samplebranch1->{branchcode},
196         categorycode => undef,
197         itemtype     => $sampleitemtype1->{itemtype},
198         rules        => {
199             holdallowed       => 5,
200             returnbranch      => 'homebranch',
201         }
202     }
203 );
204 Koha::CirculationRules->set_rules(
205     {
206         branchcode   => $samplebranch2->{branchcode},
207         categorycode => undef,
208         itemtype     => $sampleitemtype1->{itemtype},
209         rules        => {
210             holdallowed       => 5,
211             returnbranch      => 'holdingbranch',
212         }
213     }
214 );
215 Koha::CirculationRules->set_rules(
216     {
217         branchcode   => $samplebranch2->{branchcode},
218         categorycode => undef,
219         itemtype     => $sampleitemtype2->{itemtype},
220         rules        => {
221             holdallowed       => 5,
222             returnbranch      => 'noreturn',
223         }
224     }
225 );
226
227 #Test GetBranchBorrowerCircRule
228 is_deeply(
229     GetBranchBorrowerCircRule(),
230     { patron_maxissueqty => 4, patron_maxonsiteissueqty => 5 },
231 "Without parameter, GetBranchBorrower returns the patron_maxissueqty and patron_maxonsiteissueqty of default_circ_rules"
232 );
233 is_deeply(
234     GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
235     { patron_maxissueqty => 3, patron_maxonsiteissueqty => 2 },
236 "Without only the branchcode specified, GetBranchBorrower returns the patron_maxissueqty and patron_maxonsiteissueqty corresponding"
237 );
238 is_deeply(
239     GetBranchBorrowerCircRule(
240         $samplebranch1->{branchcode},
241         $samplecat->{categorycode}
242     ),
243     { patron_maxissueqty => 5, patron_maxonsiteissueqty => 6 },
244     "GetBranchBorrower returns the patron_maxissueqty and patron_maxonsiteissueqty of the branch1 and the category1"
245 );
246 is_deeply(
247     GetBranchBorrowerCircRule( -1, -1 ),
248     { patron_maxissueqty => 4, patron_maxonsiteissueqty => 5 },
249 "GetBranchBorrower with wrong parameters returns the patron_maxissueqty and patron_maxonsiteissueqty of default_circ_rules"
250 );
251
252 #Test GetBranchItemRule
253 my @lazy_any = ( 'hold_fulfillment_policy' => 'any' );
254 is_deeply(
255     GetBranchItemRule(
256         $samplebranch1->{branchcode},
257         $sampleitemtype1->{itemtype},
258     ),
259     { returnbranch => 'homebranch', holdallowed => 5, @lazy_any },
260     "GetBranchitem returns holdallowed and return branch"
261 );
262 is_deeply(
263     GetBranchItemRule(),
264     { returnbranch => 'homebranch', holdallowed => 3, @lazy_any },
265 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
266 );
267 is_deeply(
268     GetBranchItemRule( $samplebranch2->{branchcode} ),
269     { returnbranch => 'holdingbranch', holdallowed => 1, @lazy_any },
270 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
271 );
272 is_deeply(
273     GetBranchItemRule( -1, -1 ),
274     { returnbranch => 'homebranch', holdallowed => 3, @lazy_any },
275     "With only one parametern GetBranchItemRule returns default values"
276 );
277
278 # Test return policies
279 t::lib::Mocks::mock_preference('AutomaticItemReturn','0');
280
281 # item1 returned at branch2 should trigger transfer to homebranch
282 $query =
283 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
284 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
285
286 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
287     $samplebranch2->{branchcode});
288 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
289
290 # item2 returned at branch2 should trigger transfer to holding branch
291 $query =
292 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
293 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
294 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
295     $samplebranch2->{branchcode});
296 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
297
298 # Generate the transfer from above
299 ModItemTransfer($item_id2, $samplebranch2->{branchcode}, $samplebranch1->{branchcode}, "ReturnToHolding");
300 # Fulfill it
301 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',$samplebranch1->{branchcode});
302 is( $messages->{NeedsTransfer}, undef, "AddReturn does not generate a new transfer for return policy when resolving an existing transfer" );
303
304 # item3 should not trigger transfer - floating collection
305 $query =
306 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
307 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
308 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
309 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
310     $samplebranch1->{branchcode});
311 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");
312 t::lib::Mocks::mock_preference( 'item-level_itypes', 0 );
313
314 $schema->storage->txn_rollback;
315