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