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