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