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