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;
30 use Test::More tests => 32;
33 use_ok('C4::Circulation');
53 my $dbh = C4::Context->dbh;
54 $dbh->{RaiseError} = 1;
55 $dbh->{AutoCommit} = 0;
57 $dbh->do(q|DELETE FROM issues|);
58 $dbh->do(q|DELETE FROM items|);
59 $dbh->do(q|DELETE FROM borrowers|);
60 $dbh->do(q|DELETE FROM branches|);
61 $dbh->do(q|DELETE FROM categories|);
62 $dbh->do(q|DELETE FROM accountlines|);
63 $dbh->do(q|DELETE FROM issuingrules|);
69 my $dt_today = dt_from_string;
70 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
72 my $dt_today2 = dt_from_string;
73 my $dur10 = DateTime::Duration->new( days => -10 );
74 $dt_today2->add_duration($dur10);
75 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
77 #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',
100 branchname => 'Sample Branch2',
101 branchaddress1 => 'sample adr1_2',
102 branchaddress2 => 'sample adr2_2',
103 branchaddress3 => 'sample adr3_2',
104 branchzip => 'sample zip2',
105 branchcity => 'sample city2',
106 branchstate => 'sample state2',
107 branchcountry => 'sample country2',
108 branchphone => 'sample phone2',
109 branchfax => 'sample fax2',
110 branchemail => 'sample email2',
111 branchurl => 'sample url2',
112 branchip => 'sample ip2',
113 branchprinter => undef,
114 opac_info => 'sample opac2',
116 ModBranch($samplebranch1);
117 ModBranch($samplebranch2);
120 categorycode => 'CAT1',
121 description => 'Description1',
122 enrolmentperiod => 'Null',
123 enrolmentperioddate => 'Null',
124 dateofbirthrequired => 'Null',
127 enrolmentfee => 'Null',
128 overduenoticerequired => 'Null',
129 issuelimit => 'Null',
130 reservefee => 'Null',
132 category_type => 'Null'
135 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
138 $samplecat->{categorycode}, $samplecat->{description},
139 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
140 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
141 $samplecat->{bulk}, $samplecat->{enrolmentfee},
142 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
143 $samplecat->{reservefee}, $samplecat->{hidelostitems},
144 $samplecat->{category_type}
148 my $record = MARC::Record->new();
149 $record->append_fields(
150 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
151 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
153 my $barcode_1 = 'barcode_1';
154 my $barcode_2 = 'barcode_2';
155 my @sampleitem1 = C4::Items::AddItem(
157 barcode => $barcode_1,
158 itemcallnumber => 'callnumber1',
159 homebranch => $samplebranch1->{branchcode},
160 holdingbranch => $samplebranch1->{branchcode},
166 my $item_id1 = $sampleitem1[2];
167 my @sampleitem2 = C4::Items::AddItem(
169 barcode => $barcode_2,
170 itemcallnumber => 'callnumber2',
171 homebranch => $samplebranch2->{branchcode},
172 holdingbranch => $samplebranch2->{branchcode},
178 my $item_id2 = $sampleitem2[2];
181 my $borrower_id1 = C4::Members::AddMember(
182 firstname => 'firstname1',
183 surname => 'surname1 ',
184 categorycode => $samplecat->{categorycode},
185 branchcode => $samplebranch1->{branchcode},
187 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
188 my $borrower_id2 = C4::Members::AddMember(
189 firstname => 'firstname2',
190 surname => 'surname2 ',
191 categorycode => $samplecat->{categorycode},
192 branchcode => $samplebranch2->{branchcode},
194 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
196 # NEED TO BE FIXED !!!
197 # The first parameter for set_userenv is the class ref
198 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
199 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
201 C4::Context->_new_userenv('DUMMY_SESSION_ID');
202 C4::Context->set_userenv(@USERENV);
204 my $userenv = C4::Context->userenv
205 or BAIL_OUT("No userenv");
210 $query = " SELECT count(*) FROM issues";
211 my $sth = $dbh->prepare($query);
213 my $countissue = $sth -> fetchrow_array;
214 is ($countissue ,0, "there is no issue");
215 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
216 is( ref $issue1, 'Koha::Schema::Result::Issue',
217 'AddIssue returns a Koha::Schema::Result::Issue object' );
218 my $datedue1 = dt_from_string( $issue1->date_due() );
221 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
222 "Koha::Schema::Result::Issue->date_due() returns a date"
224 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
226 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
227 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
228 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
231 $countissue = $sth -> fetchrow_array;
232 is ($countissue,1,"1 issues have been added");
234 #Test AddIssuingCharge
235 $query = " SELECT count(*) FROM accountlines";
236 $sth = $dbh->prepare($query);
238 my $countaccount = $sth -> fetchrow_array;
239 is ($countaccount,0,"0 accountline exists");
240 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
241 1, "An issuing charge has been added" );
242 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
244 $countaccount = $sth -> fetchrow_array;
245 is ($countaccount,1,"1 accountline has been added");
249 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
250 $datedue1, $daysago10 );
253 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
254 "AddRenewal returns a date"
257 #Test GetBiblioIssues
258 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
261 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
262 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
263 #is(GetItemIssue($item_id1),{},"Item1's issues");
266 #FIXME: this routine currently doesn't work be
267 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
270 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
271 is( GetOpenIssue(-1), undef,
272 "With wrong parameter GetOpenIssue returns undef" );
273 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
277 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
278 #Without anything in DB
279 @renewcount = C4::Circulation::GetRenewCount();
282 [ 0, undef, 0 ], # FIXME Need to be fixed
283 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
285 @renewcount = C4::Circulation::GetRenewCount(-1);
288 [ 0, undef, 0 ], # FIXME Need to be fixed
289 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
291 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
295 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
298 #With something in DB
299 # Add a default rule: No renewal allowed
301 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
302 VALUES ( '*', '*', '*', 10, 0 )
304 @renewcount = C4::Circulation::GetRenewCount();
308 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
310 @renewcount = C4::Circulation::GetRenewCount(-1);
314 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
316 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
320 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
323 # Add a default rule: renewal is allowed
325 UPDATE issuingrules SET renewalsallowed = 3
327 @renewcount = C4::Circulation::GetRenewCount();
331 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
333 @renewcount = C4::Circulation::GetRenewCount(-1);
337 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
339 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
343 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
346 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
347 $datedue3, $daysago10 );
348 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
352 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
355 $dbh->do("DELETE FROM old_issues");
356 AddReturn($barcode_1);
357 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
358 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
360 $dbh->do("DELETE FROM old_issues");
361 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
362 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
363 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
364 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" );
367 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
369 barcode => 'barcode_3',
370 itemcallnumber => 'callnumber3',
371 homebranch => $samplebranch1->{branchcode},
372 holdingbranch => $samplebranch1->{branchcode},
378 C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
379 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
380 my $item = GetItem( $itemnumber );
381 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
383 C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
384 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
385 $item = GetItem( $itemnumber );
386 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
388 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
389 $item = GetItem( $itemnumber );
390 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
392 # Bug 14640 - Cancel the hold on checking out if asked
393 my $reserve_id = AddReserve('CPL', $borrower_id1, $biblionumber,
394 undef, 1, undef, undef, "a note", "a title", undef, '');
395 ok( $reserve_id, 'The reserve should have been inserted' );
396 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
397 my $reserve = GetReserve( $reserve_id );
398 is( $reserve, undef, 'The reserve should have been correctly cancelled' );