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