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