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>.
21 use DateTime::Duration;
31 use Test::More tests => 32;
34 use_ok('C4::Circulation');
54 my $dbh = C4::Context->dbh;
55 $dbh->{RaiseError} = 1;
56 $dbh->{AutoCommit} = 0;
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|);
70 my $dt_today = dt_from_string;
71 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
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 });
78 #Add branch and category
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',
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',
115 Koha::Library->new($samplebranch1)->store;
116 Koha::Library->new($samplebranch2)->store;
119 categorycode => 'CAT1',
120 description => 'Description1',
121 enrolmentperiod => 'Null',
122 enrolmentperioddate => 'Null',
123 dateofbirthrequired => 'Null',
126 enrolmentfee => 'Null',
127 overduenoticerequired => 'Null',
128 issuelimit => 'Null',
129 reservefee => 'Null',
131 category_type => 'Null'
134 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
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}
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, '' );
152 my $barcode_1 = 'barcode_1';
153 my $barcode_2 = 'barcode_2';
154 my @sampleitem1 = C4::Items::AddItem(
156 barcode => $barcode_1,
157 itemcallnumber => 'callnumber1',
158 homebranch => $samplebranch1->{branchcode},
159 holdingbranch => $samplebranch1->{branchcode},
165 my $item_id1 = $sampleitem1[2];
166 my @sampleitem2 = C4::Items::AddItem(
168 barcode => $barcode_2,
169 itemcallnumber => 'callnumber2',
170 homebranch => $samplebranch2->{branchcode},
171 holdingbranch => $samplebranch2->{branchcode},
177 my $item_id2 = $sampleitem2[2];
180 my $borrower_id1 = C4::Members::AddMember(
181 firstname => 'firstname1',
182 surname => 'surname1 ',
183 categorycode => $samplecat->{categorycode},
184 branchcode => $samplebranch1->{branchcode},
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},
193 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
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' );
200 C4::Context->_new_userenv('DUMMY_SESSION_ID');
201 C4::Context->set_userenv(@USERENV);
203 my $userenv = C4::Context->userenv
204 or BAIL_OUT("No userenv");
209 $query = " SELECT count(*) FROM issues";
210 my $sth = $dbh->prepare($query);
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() );
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"
223 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
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 );
230 $countissue = $sth -> fetchrow_array;
231 is ($countissue,1,"1 issues have been added");
233 #Test AddIssuingCharge
234 $query = " SELECT count(*) FROM accountlines";
235 $sth = $dbh->prepare($query);
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 );
243 $countaccount = $sth -> fetchrow_array;
244 is ($countaccount,1,"1 accountline has been added");
248 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
249 $datedue1, $daysago10 );
252 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
253 "AddRenewal returns a date"
256 #Test GetBiblioIssues
257 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
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");
265 #FIXME: this routine currently doesn't work be
266 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
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);
276 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
277 #Without anything in DB
278 @renewcount = C4::Circulation::GetRenewCount();
281 [ 0, undef, 0 ], # FIXME Need to be fixed
282 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
284 @renewcount = C4::Circulation::GetRenewCount(-1);
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"
290 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
294 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
297 #With something in DB
298 # Add a default rule: No renewal allowed
300 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
301 VALUES ( '*', '*', '*', 10, 0 )
303 @renewcount = C4::Circulation::GetRenewCount();
307 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
309 @renewcount = C4::Circulation::GetRenewCount(-1);
313 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
315 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
319 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
322 # Add a default rule: renewal is allowed
324 UPDATE issuingrules SET renewalsallowed = 3
326 @renewcount = C4::Circulation::GetRenewCount();
330 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
332 @renewcount = C4::Circulation::GetRenewCount(-1);
336 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
338 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
342 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
345 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
346 $datedue3, $daysago10 );
347 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
351 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
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" );
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" );
366 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
368 barcode => 'barcode_3',
369 itemcallnumber => 'callnumber3',
370 homebranch => $samplebranch1->{branchcode},
371 holdingbranch => $samplebranch1->{branchcode},
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' );
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"} );
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"} );
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' );