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