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