3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 34;
21 use DateTime::Duration;
24 use t::lib::TestBuilder;
37 require_ok('C4::Circulation');
58 my $schema = Koha::Database->schema;
59 $schema->storage->txn_begin;
60 my $dbh = C4::Context->dbh;
62 my $builder = t::lib::TestBuilder->new();
64 $dbh->do(q|DELETE FROM issues|);
65 $dbh->do(q|DELETE FROM items|);
66 $dbh->do(q|DELETE FROM borrowers|);
67 $dbh->do(q|DELETE FROM branches|);
68 $dbh->do(q|DELETE FROM categories|);
69 $dbh->do(q|DELETE FROM accountlines|);
70 $dbh->do(q|DELETE FROM issuingrules|);
71 $dbh->do(q|DELETE FROM reserves|);
72 $dbh->do(q|DELETE FROM old_reserves|);
73 $dbh->do(q|DELETE FROM statistics|);
75 # Generate sample datas
76 my $itemtype = $builder->build(
77 { source => 'Itemtype',
78 value => { notforloan => undef, rentalcharge => 0 }
81 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
82 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
83 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
84 my $categorycode = $builder->build({
86 value => { enrolmentfee => undef }
90 my $dt_today = dt_from_string;
91 my $today = output_pref(
99 my $dt_today2 = dt_from_string;
100 my $dur10 = DateTime::Duration->new( days => -10 );
101 $dt_today2->add_duration($dur10);
102 my $daysago10 = output_pref(
105 timeformat => '24hr',
110 # Add biblio and item
111 my $record = MARC::Record->new();
112 $record->append_fields(
113 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
115 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
117 my $barcode_1 = 'barcode_1';
118 my $barcode_2 = 'barcode_2';
119 my @sampleitem1 = C4::Items::AddItem(
121 barcode => $barcode_1,
122 itemcallnumber => 'callnumber1',
123 homebranch => $branchcode_1,
124 holdingbranch => $branchcode_1,
131 my $item_id1 = $sampleitem1[2];
132 my @sampleitem2 = C4::Items::AddItem(
134 barcode => $barcode_2,
135 itemcallnumber => 'callnumber2',
136 homebranch => $branchcode_2,
137 holdingbranch => $branchcode_2,
144 my $item_id2 = $sampleitem2[2];
147 my $borrower_id1 = C4::Members::AddMember(
148 firstname => 'firstname1',
149 surname => 'surname1 ',
150 categorycode => $categorycode,
151 branchcode => $branchcode_1
153 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
154 my $borrower_id2 = C4::Members::AddMember(
155 firstname => 'firstname2',
156 surname => 'surname2 ',
157 categorycode => $categorycode,
158 branchcode => $branchcode_2,
160 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
163 $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
164 $branchcode_1, 'email@example.org'
167 my @USERENV_DIFFERENT_LIBRARY = (
168 $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_3,
169 $branchcode_3, 'email@example.org'
173 C4::Context->_new_userenv('DUMMY_SESSION_ID');
174 C4::Context->set_userenv(@USERENV);
176 my $userenv = C4::Context->userenv
177 or BAIL_OUT("No userenv");
182 my $query = " SELECT count(*) FROM issues";
183 my $sth = $dbh->prepare($query);
185 my $countissue = $sth -> fetchrow_array;
186 is ($countissue ,0, "there is no issue");
187 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
188 is( ref $issue1, 'Koha::Schema::Result::Issue',
189 'AddIssue returns a Koha::Schema::Result::Issue object' );
190 my $datedue1 = dt_from_string( $issue1->date_due() );
193 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
194 "Koha::Schema::Result::Issue->date_due() returns a date"
196 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
198 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
199 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
200 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
203 $countissue = $sth -> fetchrow_array;
204 is ($countissue,1,"1 issues have been added");
206 #Test AddIssuingCharge
207 $query = " SELECT count(*) FROM accountlines";
208 $sth = $dbh->prepare($query);
210 my $countaccount = $sth -> fetchrow_array;
211 is ($countaccount,0,"0 accountline exists");
212 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
213 1, "An issuing charge has been added" );
214 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
216 $countaccount = $sth -> fetchrow_array;
217 is ($countaccount,1,"1 accountline has been added");
221 # Let's renew this one at a different library for statistical purposes to test Bug 17781
222 C4::Context->set_userenv(@USERENV_DIFFERENT_LIBRARY);
223 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
224 C4::Context->set_userenv(@USERENV);
228 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
229 "AddRenewal returns a date"
232 my $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $borrower_id1, $item_id1, $branchcode_3 );
233 ok( $stat, "Bug 17781 - 'Improper branchcode set during renewal' still fixed" );
235 #Let's do an opac renewal - whatever branchcode we send should be used
236 my $opac_renew_issue = $builder->build({ source=>"Issue",value=>{
237 date_due => '2017-01-01',
238 branch => $branchcode_1,
240 borrowernumber => $borrower_id1
244 my $datedue4 = AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula", $datedue1, $daysago10, 1 );
246 $stat = $dbh->selectrow_hashref("SELECT * FROM statistics WHERE type = 'renew' AND borrowernumber = ? AND itemnumber = ? AND branch = ?", undef, $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula" );
247 ok( $stat, "Bug 18572 - 'Bug 18572 - OpacRenewalBranch is now respected" );
251 #Test GetBiblioIssues
252 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
255 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
256 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
257 #is(GetItemIssue($item_id1),{},"Item1's issues");
260 #FIXME: this routine currently doesn't work be
261 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
264 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
265 is( GetOpenIssue(-1), undef,
266 "With wrong parameter GetOpenIssue returns undef" );
267 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
271 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
272 #Without anything in DB
273 @renewcount = C4::Circulation::GetRenewCount();
276 [ 0, undef, 0 ], # FIXME Need to be fixed
277 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
279 @renewcount = C4::Circulation::GetRenewCount(-1);
282 [ 0, undef, 0 ], # FIXME Need to be fixed
283 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
285 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
289 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
292 #With something in DB
293 # Add a default rule: No renewal allowed
295 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
296 VALUES ( '*', '*', '*', 10, 0 )
298 @renewcount = C4::Circulation::GetRenewCount();
302 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
304 @renewcount = C4::Circulation::GetRenewCount(-1);
308 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
310 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
314 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
317 # Add a default rule: renewal is allowed
319 UPDATE issuingrules SET renewalsallowed = 3
321 @renewcount = C4::Circulation::GetRenewCount();
325 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
327 @renewcount = C4::Circulation::GetRenewCount(-1);
331 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
333 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
337 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
340 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
341 $datedue3, $daysago10 );
342 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
346 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
349 $dbh->do("DELETE FROM old_issues");
350 AddReturn($barcode_1);
351 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
352 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
354 $dbh->do("DELETE FROM old_issues");
355 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
356 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
357 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
358 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" );
361 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
363 barcode => 'barcode_3',
364 itemcallnumber => 'callnumber3',
365 homebranch => $branchcode_1,
366 holdingbranch => $branchcode_1,
373 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
374 AddReturn( 'barcode_3', $branchcode_1 );
375 my $item = GetItem( $itemnumber );
376 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
378 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
379 AddReturn( 'barcode_3', $branchcode_1 );
380 $item = GetItem( $itemnumber );
381 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
383 AddReturn( 'barcode_3', $branchcode_1 );
384 $item = GetItem( $itemnumber );
385 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
387 # Bug 14640 - Cancel the hold on checking out if asked
388 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
389 undef, 1, undef, undef, "a note", "a title", undef, '');
390 ok( $reserve_id, 'The reserve should have been inserted' );
391 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
392 my $reserve = GetReserve( $reserve_id );
393 is( $reserve, undef, 'The reserve should have been correctly cancelled' );
396 $schema->storage->txn_rollback;