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 => 32;
21 use DateTime::Duration;
24 use t::lib::TestBuilder;
37 require_ok('C4::Circulation');
57 my $schema = Koha::Database->schema;
58 $schema->storage->txn_begin;
59 my $dbh = C4::Context->dbh;
61 my $builder = t::lib::TestBuilder->new();
63 $dbh->do(q|DELETE FROM issues|);
64 $dbh->do(q|DELETE FROM items|);
65 $dbh->do(q|DELETE FROM borrowers|);
66 $dbh->do(q|DELETE FROM branches|);
67 $dbh->do(q|DELETE FROM categories|);
68 $dbh->do(q|DELETE FROM accountlines|);
69 $dbh->do(q|DELETE FROM issuingrules|);
71 # Generate sample datas
72 my $itemtype = $builder->build(
73 { source => 'Itemtype',
74 value => { notforloan => undef, rentalcharge => 0 }
77 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
78 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
79 my $categorycode = $builder->build({
81 value => { enrolmentfee => undef }
85 my $dt_today = dt_from_string;
86 my $today = output_pref(
94 my $dt_today2 = dt_from_string;
95 my $dur10 = DateTime::Duration->new( days => -10 );
96 $dt_today2->add_duration($dur10);
97 my $daysago10 = output_pref(
100 timeformat => '24hr',
105 # Add biblio and item
106 my $record = MARC::Record->new();
107 $record->append_fields(
108 MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
110 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
112 my $barcode_1 = 'barcode_1';
113 my $barcode_2 = 'barcode_2';
114 my @sampleitem1 = C4::Items::AddItem(
116 barcode => $barcode_1,
117 itemcallnumber => 'callnumber1',
118 homebranch => $branchcode_1,
119 holdingbranch => $branchcode_1,
126 my $item_id1 = $sampleitem1[2];
127 my @sampleitem2 = C4::Items::AddItem(
129 barcode => $barcode_2,
130 itemcallnumber => 'callnumber2',
131 homebranch => $branchcode_2,
132 holdingbranch => $branchcode_2,
139 my $item_id2 = $sampleitem2[2];
142 my $borrower_id1 = C4::Members::AddMember(
143 firstname => 'firstname1',
144 surname => 'surname1 ',
145 categorycode => $categorycode,
146 branchcode => $branchcode_1
148 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
149 my $borrower_id2 = C4::Members::AddMember(
150 firstname => 'firstname2',
151 surname => 'surname2 ',
152 categorycode => $categorycode,
153 branchcode => $branchcode_2,
155 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
158 $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
159 $branchcode_1, 'email@example.org'
163 C4::Context->_new_userenv('DUMMY_SESSION_ID');
164 C4::Context->set_userenv(@USERENV);
166 my $userenv = C4::Context->userenv
167 or BAIL_OUT("No userenv");
172 my $query = " SELECT count(*) FROM issues";
173 my $sth = $dbh->prepare($query);
175 my $countissue = $sth -> fetchrow_array;
176 is ($countissue ,0, "there is no issue");
177 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
178 is( ref $issue1, 'Koha::Schema::Result::Issue',
179 'AddIssue returns a Koha::Schema::Result::Issue object' );
180 my $datedue1 = dt_from_string( $issue1->date_due() );
183 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
184 "Koha::Schema::Result::Issue->date_due() returns a date"
186 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
188 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
189 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
190 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
193 $countissue = $sth -> fetchrow_array;
194 is ($countissue,1,"1 issues have been added");
196 #Test AddIssuingCharge
197 $query = " SELECT count(*) FROM accountlines";
198 $sth = $dbh->prepare($query);
200 my $countaccount = $sth -> fetchrow_array;
201 is ($countaccount,0,"0 accountline exists");
202 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
203 1, "An issuing charge has been added" );
204 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
206 $countaccount = $sth -> fetchrow_array;
207 is ($countaccount,1,"1 accountline has been added");
211 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
212 $datedue1, $daysago10 );
215 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
216 "AddRenewal returns a date"
219 #Test GetBiblioIssues
220 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
223 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
224 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
225 #is(GetItemIssue($item_id1),{},"Item1's issues");
228 #FIXME: this routine currently doesn't work be
229 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
232 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
233 is( GetOpenIssue(-1), undef,
234 "With wrong parameter GetOpenIssue returns undef" );
235 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
239 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
240 #Without anything in DB
241 @renewcount = C4::Circulation::GetRenewCount();
244 [ 0, undef, 0 ], # FIXME Need to be fixed, see FIXME in GetRenewCount
245 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
247 @renewcount = C4::Circulation::GetRenewCount(-1);
250 [ 0, undef, 0 ], # FIXME Need to be fixed
251 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
253 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
257 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
260 #With something in DB
261 # Add a default rule: No renewal allowed
263 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
264 VALUES ( '*', '*', '*', 10, 0 )
266 @renewcount = C4::Circulation::GetRenewCount();
270 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
272 @renewcount = C4::Circulation::GetRenewCount(-1);
276 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
278 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
282 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
285 # Add a default rule: renewal is allowed
287 UPDATE issuingrules SET renewalsallowed = 3
289 @renewcount = C4::Circulation::GetRenewCount();
293 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
295 @renewcount = C4::Circulation::GetRenewCount(-1);
299 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
301 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
305 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
308 AddRenewal( $borrower_id1, $item_id1, $branchcode_1,
309 $datedue3, $daysago10 );
310 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
314 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
317 $dbh->do("DELETE FROM old_issues");
318 AddReturn($barcode_1);
319 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
320 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
322 $dbh->do("DELETE FROM old_issues");
323 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
324 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
325 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
326 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" );
329 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
331 barcode => 'barcode_3',
332 itemcallnumber => 'callnumber3',
333 homebranch => $branchcode_1,
334 holdingbranch => $branchcode_1,
341 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
342 AddReturn( 'barcode_3', $branchcode_1 );
343 my $item = GetItem( $itemnumber );
344 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
346 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
347 AddReturn( 'barcode_3', $branchcode_1 );
348 $item = GetItem( $itemnumber );
349 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
351 AddReturn( 'barcode_3', $branchcode_1 );
352 $item = GetItem( $itemnumber );
353 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
355 # Bug 14640 - Cancel the hold on checking out if asked
356 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
357 undef, 1, undef, undef, "a note", "a title", undef, '');
358 ok( $reserve_id, 'The reserve should have been inserted' );
359 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
360 my $reserve = GetReserve( $reserve_id );
361 is( $reserve, undef, 'The reserve should have been correctly cancelled' );
364 $schema->storage->txn_rollback;