Bug 17829: Move GetMember to Koha::Patron
[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 branches|);
50 $dbh->do(q|DELETE FROM categories|);
51 $dbh->do(q|DELETE FROM accountlines|);
52 $dbh->do(q|DELETE FROM itemtypes|);
53 $dbh->do(q|DELETE FROM branch_item_rules|);
54 $dbh->do(q|DELETE FROM branch_borrower_circ_rules|);
55 $dbh->do(q|DELETE FROM default_branch_circ_rules|);
56 $dbh->do(q|DELETE FROM default_circ_rules|);
57 $dbh->do(q|DELETE FROM default_branch_item_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     values => {
68         rentalcharge => '0',
69         notforloan   => 0
70     }
71 });
72 my $sampleitemtype1 = $builder->build({
73     source => 'Itemtype',
74     values => {
75         rentalcharge => '10.0',
76         notforloan   => 1
77     }
78 });
79 my $sampleitemtype2 = $builder->build({
80     source => 'Itemtype',
81     values => {
82         rentalcharge => '5.0',
83         notforloan   => 0
84     }
85 });
86 # Add Category
87 my $samplecat     = $builder->build({
88     source => 'Category',
89     values => {
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 = C4::Members::AddMember(
141     firstname    => 'firstname1',
142     surname      => 'surname1 ',
143     categorycode => $samplecat->{categorycode},
144     branchcode   => $samplebranch1->{branchcode},
145 );
146
147 is_deeply(
148     GetBranchBorrowerCircRule(),
149     { maxissueqty => undef, maxonsiteissueqty => undef },
150 "Without parameter, GetBranchBorrower returns undef (unilimited) for maxissueqty and maxonsiteissueqty if no rules defined"
151 );
152
153 my $query = q|
154     INSERT INTO branch_borrower_circ_rules
155     (branchcode, categorycode, maxissueqty, maxonsiteissueqty)
156     VALUES( ?, ?, ?, ? )
157 |;
158
159 $dbh->do(
160     $query, {},
161     $samplebranch1->{branchcode},
162     $samplecat->{categorycode}, 5, 6
163 );
164
165 $query = q|
166     INSERT INTO default_branch_circ_rules
167     (branchcode, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
168     VALUES( ?, ?, ?, ?, ? )
169 |;
170 $dbh->do( $query, {}, $samplebranch2->{branchcode},
171     3, 2, 1, 'holdingbranch' );
172 $query = q|
173     INSERT INTO default_circ_rules
174     (singleton, maxissueqty, maxonsiteissueqty, holdallowed, returnbranch)
175     VALUES( ?, ?, ?, ?, ? )
176 |;
177 $dbh->do( $query, {}, 'singleton', 4, 5, 3, 'homebranch' );
178
179 $query =
180 "INSERT INTO branch_item_rules (branchcode,itemtype,holdallowed,returnbranch) VALUES( ?,?,?,?)";
181 my $sth = $dbh->prepare($query);
182 $sth->execute(
183     $samplebranch1->{branchcode},
184     $sampleitemtype1->{itemtype},
185     5, 'homebranch'
186 );
187 $sth->execute(
188     $samplebranch2->{branchcode},
189     $sampleitemtype1->{itemtype},
190     5, 'holdingbranch'
191 );
192 $sth->execute(
193     $samplebranch2->{branchcode},
194     $sampleitemtype2->{itemtype},
195     5, 'noreturn'
196 );
197
198 #Test GetBranchBorrowerCircRule
199 is_deeply(
200     GetBranchBorrowerCircRule(),
201     { maxissueqty => 4, maxonsiteissueqty => 5 },
202 "Without parameter, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
203 );
204 is_deeply(
205     GetBranchBorrowerCircRule( $samplebranch2->{branchcode} ),
206     { maxissueqty => 3, maxonsiteissueqty => 2 },
207 "Without only the branchcode specified, GetBranchBorrower returns the maxissueqty and maxonsiteissueqty corresponding"
208 );
209 is_deeply(
210     GetBranchBorrowerCircRule(
211         $samplebranch1->{branchcode},
212         $samplecat->{categorycode}
213     ),
214     { maxissueqty => 5, maxonsiteissueqty => 6 },
215     "GetBranchBorrower returns the maxissueqty and maxonsiteissueqty of the branch1 and the category1"
216 );
217 is_deeply(
218     GetBranchBorrowerCircRule( -1, -1 ),
219     { maxissueqty => 4, maxonsiteissueqty => 5 },
220 "GetBranchBorrower with wrong parameters returns the maxissueqty and maxonsiteissueqty of default_circ_rules"
221 );
222
223 #Test GetBranchItemRule
224 my @lazy_any = ( 'hold_fulfillment_policy' => 'any' );
225 is_deeply(
226     GetBranchItemRule(
227         $samplebranch1->{branchcode},
228         $sampleitemtype1->{itemtype},
229     ),
230     { returnbranch => 'homebranch', holdallowed => 5, @lazy_any },
231     "GetBranchitem returns holdallowed and return branch"
232 );
233 is_deeply(
234     GetBranchItemRule(),
235     { returnbranch => 'homebranch', holdallowed => 3, @lazy_any },
236 "Without parameters GetBranchItemRule returns the values in default_circ_rules"
237 );
238 is_deeply(
239     GetBranchItemRule( $samplebranch2->{branchcode} ),
240     { returnbranch => 'holdingbranch', holdallowed => 1, @lazy_any },
241 "With only a branchcode GetBranchItemRule returns values in default_branch_circ_rules"
242 );
243 is_deeply(
244     GetBranchItemRule( -1, -1 ),
245     { returnbranch => 'homebranch', holdallowed => 3, @lazy_any },
246     "With only one parametern GetBranchItemRule returns default values"
247 );
248
249 # Test return policies
250 t::lib::Mocks::mock_preference('AutomaticItemReturn','0');
251
252 # item1 returned at branch2 should trigger transfer to homebranch
253 $query =
254 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
255 $dbh->do( $query, {}, $borrower_id1, $item_id1, $samplebranch1->{branchcode} );
256
257 my ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_1',
258     $samplebranch2->{branchcode});
259 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects default return policy - return to homebranch" );
260
261 # item2 returned at branch2 should trigger transfer to holding branch
262 $query =
263 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
264 $dbh->do( $query, {}, $borrower_id1, $item_id2, $samplebranch2->{branchcode} );
265 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_2',
266     $samplebranch2->{branchcode});
267 is( $messages->{NeedsTransfer}, $samplebranch1->{branchcode}, "AddReturn respects branch return policy - item2->homebranch policy = 'holdingbranch'" );
268
269 # item3 should not trigger transfer - floating collection
270 $query =
271 "INSERT INTO issues (borrowernumber,itemnumber,branchcode) VALUES( ?,?,? )";
272 $dbh->do( $query, {}, $borrower_id1, $item_id3, $samplebranch1->{branchcode} );
273 ($doreturn, $messages, $iteminformation, $borrower) = AddReturn('barcode_3',
274     $samplebranch1->{branchcode});
275 is($messages->{NeedsTransfer},undef,"AddReturn respects branch item return policy - noreturn");
276
277 $schema->storage->txn_rollback;
278
279 1;