Bug 18936: Convert issuingrules fields to circulation_rules
[koha.git] / t / db_dependent / Circulation / issue.t
blob7d391a42808f5ba4a6ef3e043e817c817dc0862c
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 45;
21 use DateTime::Duration;
23 use t::lib::Mocks;
24 use t::lib::TestBuilder;
26 use C4::Circulation;
27 use C4::Items;
28 use C4::Biblio;
29 use C4::Context;
30 use C4::Reserves;
31 use Koha::Checkouts;
32 use Koha::Database;
33 use Koha::DateUtils;
34 use Koha::Holds;
35 use Koha::Items;
36 use Koha::Library;
37 use Koha::Patrons;
38 use Koha::CirculationRules;
40 BEGIN {
41 require_ok('C4::Circulation');
44 can_ok(
45 'C4::Circulation',
46 qw(AddIssue
47 AddIssuingCharge
48 AddRenewal
49 AddReturn
50 GetBiblioIssues
51 GetIssuingCharges
52 GetOpenIssue
53 GetRenewCount
54 GetUpcomingDueIssues
58 #Start transaction
59 my $schema = Koha::Database->schema;
60 $schema->storage->txn_begin;
61 my $dbh = C4::Context->dbh;
63 my $builder = t::lib::TestBuilder->new();
65 $dbh->do(q|DELETE FROM issues|);
66 $dbh->do(q|DELETE FROM items|);
67 $dbh->do(q|DELETE FROM borrowers|);
68 $dbh->do(q|DELETE FROM categories|);
69 $dbh->do(q|DELETE FROM accountlines|);
70 $dbh->do(q|DELETE FROM circulation_rules|);
71 $dbh->do(q|DELETE FROM reserves|);
72 $dbh->do(q|DELETE FROM old_reserves|);
73 $dbh->do(q|DELETE FROM statistics|);
75 # Generate sample datas
76 my $itemtype = $builder->build(
77 { source => 'Itemtype',
78 value => { notforloan => undef, rentalcharge => 0 }
80 )->{itemtype};
81 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
82 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
83 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
84 my $categorycode = $builder->build({
85 source => 'Category',
86 value => { enrolmentfee => undef }
87 })->{categorycode};
89 # A default issuingrule should always be present
90 my $issuingrule = $builder->build(
92 source => 'Issuingrule',
93 value => {
94 itemtype => '*',
95 categorycode => '*',
96 branchcode => '*',
97 lengthunit => 'days',
98 issuelength => 0,
99 renewalperiod => 0,
100 renewalsallowed => 0
105 # Add Dates
106 my $dt_today = dt_from_string;
107 my $today = output_pref(
108 { dt => $dt_today,
109 dateformat => 'iso',
110 timeformat => '24hr',
111 dateonly => 1
115 my $dt_today2 = dt_from_string;
116 my $dur10 = DateTime::Duration->new( days => -10 );
117 $dt_today2->add_duration($dur10);
118 my $daysago10 = output_pref(
119 { dt => $dt_today2,
120 dateformat => 'iso',
121 timeformat => '24hr',
122 dateonly => 1
126 # Add biblio and item
127 my $record = MARC::Record->new();
128 $record->append_fields(
129 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
131 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
133 my $barcode_1 = 'barcode_1';
134 my $barcode_2 = 'barcode_2';
135 my @sampleitem1 = C4::Items::AddItem(
137 barcode => $barcode_1,
138 itemcallnumber => 'callnumber1',
139 homebranch => $branchcode_1,
140 holdingbranch => $branchcode_1,
141 issue => 1,
142 reserve => 1,
143 itype => $itemtype
145 $biblionumber
147 my $item_id1 = $sampleitem1[2];
148 my @sampleitem2 = C4::Items::AddItem(
150 barcode => $barcode_2,
151 itemcallnumber => 'callnumber2',
152 homebranch => $branchcode_2,
153 holdingbranch => $branchcode_2,
154 notforloan => 1,
155 issue => 1,
156 itype => $itemtype
158 $biblionumber
160 my $item_id2 = $sampleitem2[2];
162 #Add borrower
163 my $borrower_id1 = Koha::Patron->new({
164 firstname => 'firstname1',
165 surname => 'surname1 ',
166 categorycode => $categorycode,
167 branchcode => $branchcode_1
168 })->store->borrowernumber;
169 my $patron_1 = Koha::Patrons->find( $borrower_id1 );
170 my $borrower_1 = $patron_1->unblessed;
171 my $borrower_id2 = Koha::Patron->new({
172 firstname => 'firstname2',
173 surname => 'surname2 ',
174 categorycode => $categorycode,
175 branchcode => $branchcode_2,
176 })->store->borrowernumber;
177 my $patron_2 = Koha::Patrons->find( $borrower_id2 );
178 my $borrower_2 = $patron_2->unblessed;
180 t::lib::Mocks::mock_userenv({ patron => $patron_1 });
182 #Begin Tests
184 #Test AddIssue
185 my $query = " SELECT count(*) FROM issues";
186 my $sth = $dbh->prepare($query);
187 $sth->execute;
188 my $countissue = $sth -> fetchrow_array;
189 is ($countissue ,0, "there is no issue");
190 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
191 is( ref $issue1, 'Koha::Checkout',
192 'AddIssue returns a Koha::Checkout object' );
193 my $datedue1 = dt_from_string( $issue1->date_due() );
194 like(
195 $datedue1,
196 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
197 "Koha::Checkout->date_due() returns a date"
199 my $issue_id1 = $issue1->issue_id;
201 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
202 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
204 $sth->execute;
205 $countissue = $sth -> fetchrow_array;
206 is ($countissue,1,"1 issues have been added");
208 #Test AddIssuingCharge
209 $query = " SELECT count(*) FROM accountlines";
210 $sth = $dbh->prepare($query);
211 $sth->execute;
212 my $countaccount = $sth->fetchrow_array;
213 is ($countaccount,0,"0 accountline exists");
214 my $checkout = Koha::Checkouts->find( $issue_id1 );
215 my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10, 'RENT' );
216 is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
217 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
218 my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
219 is( $offset->credit_id, undef, 'Offset was created');
220 $sth->execute;
221 $countaccount = $sth->fetchrow_array;
222 is ($countaccount,1,"1 accountline has been added");
224 # Test AddRenewal
226 my $se = Test::MockModule->new( 'C4::Context' );
227 $se->mock( 'interface', sub {return 'intranet'});
229 # Let's renew this one at a different library for statistical purposes to test Bug 17781
230 # Mocking userenv with a different branchcode
231 t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
233 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
235 # Restoring the userenv with the original branchcode
236 t::lib::Mocks::mock_userenv({ patron => $patron_1});
238 like(
239 $datedue3,
240 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
241 "AddRenewal returns a date"
244 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
245 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
247 $se->mock( 'interface', sub {return 'opac'});
249 #Let's do an opac renewal - whatever branchcode we send should be used
250 my $opac_renew_issue = $builder->build({
251 source=>"Issue",
252 value=>{
253 date_due => '2017-01-01',
254 branch => $branchcode_1,
255 itype => $itemtype,
256 borrowernumber => $borrower_id1
260 my $datedue4 = AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula", $datedue1, $daysago10 );
262 $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula" );
263 ok( $stat, "Bug 18572 - 'Bug 18572 - OpacRenewalBranch is now respected" );
267 #Test GetBiblioIssues
268 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
270 #Test GetOpenIssue
271 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
272 is( GetOpenIssue(-1), undef,
273 "With wrong parameter GetOpenIssue returns undef" );
274 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
276 my @renewcount;
277 #Test GetRenewCount
278 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
279 #Without anything in DB
280 @renewcount = C4::Circulation::GetRenewCount();
281 is_deeply(
282 \@renewcount,
283 [ 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
284 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
286 @renewcount = C4::Circulation::GetRenewCount(-1);
287 is_deeply(
288 \@renewcount,
289 [ 0, 0, 0 ], # FIXME Need to be fixed
290 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
292 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
293 is_deeply(
294 \@renewcount,
295 [ 2, 0, 0 ],
296 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
299 #With something in DB
300 @renewcount = C4::Circulation::GetRenewCount();
301 is_deeply(
302 \@renewcount,
303 [ 0, 0, 0 ],
304 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
306 @renewcount = C4::Circulation::GetRenewCount(-1);
307 is_deeply(
308 \@renewcount,
309 [ 0, 0, 0 ],
310 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
312 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
313 is_deeply(
314 \@renewcount,
315 [ 2, 0, 0 ],
316 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
319 # Add a default rule: renewal is allowed
320 Koha::CirculationRules->set_rules(
322 categorycode => '*',
323 itemtype => '*',
324 branchcode => '*',
325 rules => {
326 renewalsallowed => 3,
330 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
331 is_deeply(
332 \@renewcount,
333 [ 2, 3, 1 ],
334 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
337 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
338 $datedue3, $daysago10 );
339 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
340 is_deeply(
341 \@renewcount,
342 [ 3, 3, 0 ],
343 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
346 $dbh->do("DELETE FROM old_issues");
347 AddReturn($barcode_1);
348 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
349 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
351 $dbh->do("DELETE FROM old_issues");
352 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
353 AddReturn($barcode_1, undef, undef, dt_from_string('2014-04-01 23:42'));
354 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
355 ok( $return->{returndate} eq '2014-04-01 23:42:00', "Item returned with a return date of '2014-04-01 23:42' has that return date" );
357 my $itemnumber;
358 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
360 barcode => 'barcode_3',
361 itemcallnumber => 'callnumber3',
362 homebranch => $branchcode_1,
363 holdingbranch => $branchcode_1,
364 notforloan => 1,
365 itype => $itemtype
367 $biblionumber
370 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
371 AddReturn( 'barcode_3', $branchcode_1 );
372 my $item = Koha::Items->find( $itemnumber );
373 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
375 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
376 AddReturn( 'barcode_3', $branchcode_1 );
377 $item = Koha::Items->find( $itemnumber );
378 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
380 AddReturn( 'barcode_3', $branchcode_1 );
381 $item = Koha::Items->find( $itemnumber );
382 ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
384 my $itemnumber2;
385 ($biblionumber, $biblioitemnumber, $itemnumber2) = C4::Items::AddItem(
387 barcode => 'barcode_4',
388 itemcallnumber => 'callnumber4',
389 homebranch => $branchcode_1,
390 holdingbranch => $branchcode_1,
391 location => 'FIC',
392 itype => $itemtype
394 $biblionumber
397 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', q{} );
398 AddReturn( 'barcode_4', $branchcode_1 );
399 my $item2 = Koha::Items->find( $itemnumber2 );
400 ok( $item2->location eq 'FIC', 'UpdateItemLocationOnCheckin does not modify value when not enabled' );
402 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', 'FIC: GEN' );
403 AddReturn( 'barcode_4', $branchcode_1 );
404 $item2 = Koha::Items->find( $itemnumber2 );
405 ok( $item2->location eq 'GEN', q{UpdateItemLocationOnCheckin updates location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
406 ok( $item2->permanent_location eq 'GEN', q{UpdateItemLocationOnCheckin updates permanent_location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
407 AddReturn( 'barcode_4', $branchcode_1 );
408 $item2 = Koha::Items->find( $itemnumber2 );
409 ok( $item2->location eq 'GEN', q{UpdateItemLocationOnCheckin does not update location value from 'GEN' with setting "FIC: GEN"} );
411 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
412 AddReturn( 'barcode_4', $branchcode_1 );
413 $item2 = Koha::Items->find( $itemnumber2 );
414 ok( $item2->location eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART"} );
415 ok( $item2->permanent_location eq 'GEN', q{UpdateItemLocationOnCheckin does not update permanent_location value from 'GEN' with setting "_ALL_: CART"} );
416 AddIssue( $borrower_1, 'barcode_4', $daysago10,0, $today, '' );
417 $item2 = Koha::Items->find( $itemnumber2 );
418 ok( $item2->location eq 'GEN', q{Location updates from 'CART' to permanent location on issue} );
420 t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', "GEN: _BLANK_\n_BLANK_: PROC\nPROC: _PERM_" );
421 AddReturn( 'barcode_4', $branchcode_1 );
422 $item2 = Koha::Items->find( $itemnumber2 );
423 ok( $item2->location eq '', q{UpdateItemLocationOnCheckin updates location value from 'GEN' to '' with setting "GEN: _BLANK_"} );
424 AddReturn( 'barcode_4', $branchcode_1 );
425 $item2 = Koha::Items->find( $itemnumber2 );
426 ok( $item2->location eq 'PROC' , q{UpdateItemLocationOnCheckin updates location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
427 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
428 AddReturn( 'barcode_4', $branchcode_1 );
429 $item2 = Koha::Items->find( $itemnumber2 );
430 ok( $item2->location eq '' , q{UpdateItemLocationOnCheckin updates location value from 'PROC' to '' with setting "PROC: _PERM_" } );
431 ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location from '' with setting "PROC: _PERM_" } );
436 # Bug 14640 - Cancel the hold on checking out if asked
437 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
438 undef, 1, undef, undef, "a note", "a title", undef, '');
439 ok( $reserve_id, 'The reserve should have been inserted' );
440 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
441 my $hold = Koha::Holds->find( $reserve_id );
442 is( $hold, undef, 'The reserve should have been correctly cancelled' );
444 #End transaction
445 $schema->storage->txn_rollback;