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