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