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