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 => 31;
21 use DateTime::Duration;
24 use t::lib::TestBuilder;
38 require_ok('C4::Circulation');
56 my $schema = Koha::Database->schema;
57 $schema->storage->txn_begin;
58 my $dbh = C4::Context->dbh;
60 my $builder = t::lib::TestBuilder->new();
62 $dbh->do(q|DELETE FROM issues|);
63 $dbh->do(q|DELETE FROM items|);
64 $dbh->do(q|DELETE FROM borrowers|);
65 $dbh->do(q|DELETE FROM categories|);
66 $dbh->do(q|DELETE FROM accountlines|);
67 $dbh->do(q|DELETE FROM issuingrules|);
68 $dbh->do(q|DELETE FROM reserves|);
69 $dbh->do(q|DELETE FROM old_reserves|);
70 $dbh->do(q|DELETE FROM statistics|);
72 # Generate sample datas
73 my $itemtype = $builder->build(
74 { source => 'Itemtype',
75 value => { notforloan => undef, rentalcharge => 0 }
78 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
79 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
80 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
81 my $categorycode = $builder->build({
83 value => { enrolmentfee => undef }
87 my $dt_today = dt_from_string;
88 my $today = output_pref(
96 my $dt_today2 = dt_from_string;
97 my $dur10 = DateTime::Duration->new( days => -10 );
98 $dt_today2->add_duration($dur10);
99 my $daysago10 = output_pref(
102 timeformat => '24hr',
107 # Add biblio and item
108 my $record = MARC::Record->new();
109 $record->append_fields(
110 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
112 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
114 my $barcode_1 = 'barcode_1';
115 my $barcode_2 = 'barcode_2';
116 my @sampleitem1 = C4::Items::AddItem(
118 barcode => $barcode_1,
119 itemcallnumber => 'callnumber1',
120 homebranch => $branchcode_1,
121 holdingbranch => $branchcode_1,
128 my $item_id1 = $sampleitem1[2];
129 my @sampleitem2 = C4::Items::AddItem(
131 barcode => $barcode_2,
132 itemcallnumber => 'callnumber2',
133 homebranch => $branchcode_2,
134 holdingbranch => $branchcode_2,
141 my $item_id2 = $sampleitem2[2];
144 my $borrower_id1 = C4::Members::AddMember(
145 firstname => 'firstname1',
146 surname => 'surname1 ',
147 categorycode => $categorycode,
148 branchcode => $branchcode_1
150 my $borrower_1 = Koha::Patrons->find( $borrower_id1 )->unblessed;
151 my $borrower_id2 = C4::Members::AddMember(
152 firstname => 'firstname2',
153 surname => 'surname2 ',
154 categorycode => $categorycode,
155 branchcode => $branchcode_2,
157 my $borrower_2 = Koha::Patrons->find( $borrower_id2 )->unblessed;
160 $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
161 $branchcode_1, 'email@example.org'
164 my @USERENV_DIFFERENT_LIBRARY = (
165 $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_3,
166 $branchcode_3, 'email@example.org'
170 C4::Context->_new_userenv('DUMMY_SESSION_ID');
171 C4::Context->set_userenv(@USERENV);
173 my $userenv = C4::Context->userenv
174 or BAIL_OUT("No userenv");
179 my $query = " SELECT count(*) FROM issues";
180 my $sth = $dbh->prepare($query);
182 my $countissue = $sth -> fetchrow_array;
183 is ($countissue ,0, "there is no issue");
184 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
185 is( ref $issue1, 'Koha::Schema::Result::Issue',
186 'AddIssue returns a Koha::Schema::Result::Issue object' );
187 my $datedue1 = dt_from_string( $issue1->date_due() );
190 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
191 "Koha::Schema::Result::Issue->date_due() returns a date"
193 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
195 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
196 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
197 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
200 $countissue = $sth -> fetchrow_array;
201 is ($countissue,1,"1 issues have been added");
203 #Test AddIssuingCharge
204 $query = " SELECT count(*) FROM accountlines";
205 $sth = $dbh->prepare($query);
207 my $countaccount = $sth -> fetchrow_array;
208 is ($countaccount,0,"0 accountline exists");
209 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
210 1, "An issuing charge has been added" );
211 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
213 $countaccount = $sth -> fetchrow_array;
214 is ($countaccount,1,"1 accountline has been added");
218 my $se = Test::MockModule->new( 'C4::Context' );
219 $se->mock( 'interface', sub {return 'intranet'});
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 $se->mock( 'interface', sub {return 'opac'});
237 #Let's do an opac renewal - whatever branchcode we send should be used
238 my $opac_renew_issue = $builder->build({
241 date_due => '2017-01-01',
242 branch => $branchcode_1,
244 borrowernumber => $borrower_id1
248 my $datedue4 = AddRenewal( $opac_renew_issue->{borrowernumber}, $opac_renew_issue->{itemnumber}, "Stavromula", $datedue1, $daysago10 );
250 $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" );
251 ok( $stat, "Bug 18572 - 'Bug 18572 - OpacRenewalBranch is now respected" );
255 #Test GetBiblioIssues
256 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
259 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
260 is( GetOpenIssue(-1), undef,
261 "With wrong parameter GetOpenIssue returns undef" );
262 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
266 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
267 #Without anything in DB
268 @renewcount = C4::Circulation::GetRenewCount();
271 [ 0, 0, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
272 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
274 @renewcount = C4::Circulation::GetRenewCount(-1);
277 [ 0, 0, 0 ], # FIXME Need to be fixed
278 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
280 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
284 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
287 #With something in DB
288 # Add a default rule: No renewal allowed
290 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
291 VALUES ( '*', '*', '*', 10, 0 )
293 @renewcount = C4::Circulation::GetRenewCount();
297 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
299 @renewcount = C4::Circulation::GetRenewCount(-1);
303 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
305 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
309 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
312 # Add a default rule: renewal is allowed
314 UPDATE issuingrules SET renewalsallowed = 3
316 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
320 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
323 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
324 $datedue3, $daysago10 );
325 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
329 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
332 $dbh->do("DELETE FROM old_issues");
333 AddReturn($barcode_1);
334 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
335 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
337 $dbh->do("DELETE FROM old_issues");
338 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
339 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
340 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
341 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" );
344 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
346 barcode => 'barcode_3',
347 itemcallnumber => 'callnumber3',
348 homebranch => $branchcode_1,
349 holdingbranch => $branchcode_1,
356 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
357 AddReturn( 'barcode_3', $branchcode_1 );
358 my $item = GetItem( $itemnumber );
359 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
361 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
362 AddReturn( 'barcode_3', $branchcode_1 );
363 $item = GetItem( $itemnumber );
364 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
366 AddReturn( 'barcode_3', $branchcode_1 );
367 $item = GetItem( $itemnumber );
368 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
370 # Bug 14640 - Cancel the hold on checking out if asked
371 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
372 undef, 1, undef, undef, "a note", "a title", undef, '');
373 ok( $reserve_id, 'The reserve should have been inserted' );
374 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
375 my $reserve = GetReserve( $reserve_id );
376 is( $reserve, undef, 'The reserve should have been correctly cancelled' );
379 $schema->storage->txn_rollback;