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
80 branchname => 'Sample Branch',
81 branchaddress1 => 'sample adr1',
82 branchaddress2 => 'sample adr2',
83 branchaddress3 => 'sample adr3',
84 branchzip => 'sample zip',
85 branchcity => 'sample city',
86 branchstate => 'sample state',
87 branchcountry => 'sample country',
88 branchphone => 'sample phone',
89 branchfax => 'sample fax',
90 branchemail => 'sample email',
91 branchurl => 'sample url',
92 branchip => 'sample ip',
93 branchprinter => undef,
94 opac_info => 'sample opac',
98 branchname => 'Sample Branch2',
99 branchaddress1 => 'sample adr1_2',
100 branchaddress2 => 'sample adr2_2',
101 branchaddress3 => 'sample adr3_2',
102 branchzip => 'sample zip2',
103 branchcity => 'sample city2',
104 branchstate => 'sample state2',
105 branchcountry => 'sample country2',
106 branchphone => 'sample phone2',
107 branchfax => 'sample fax2',
108 branchemail => 'sample email2',
109 branchurl => 'sample url2',
110 branchip => 'sample ip2',
111 branchprinter => undef,
112 opac_info => 'sample opac2',
114 Koha::Library->new($samplebranch1)->store;
115 Koha::Library->new($samplebranch2)->store;
118 categorycode => 'CAT1',
119 description => 'Description1',
120 enrolmentperiod => 'Null',
121 enrolmentperioddate => 'Null',
122 dateofbirthrequired => 'Null',
125 enrolmentfee => 'Null',
126 overduenoticerequired => 'Null',
127 issuelimit => 'Null',
128 reservefee => 'Null',
130 category_type => 'Null'
133 "INSERT INTO categories (categorycode,description,enrolmentperiod,enrolmentperioddate,dateofbirthrequired ,finetype,bulk,enrolmentfee,overduenoticerequired,issuelimit ,reservefee ,hidelostitems ,category_type) VALUES( ?,?,?,?,?,?,?,?,?,?,?,?,?)";
136 $samplecat->{categorycode}, $samplecat->{description},
137 $samplecat->{enrolmentperiod}, $samplecat->{enrolmentperioddate},
138 $samplecat->{dateofbirthrequired}, $samplecat->{finetype},
139 $samplecat->{bulk}, $samplecat->{enrolmentfee},
140 $samplecat->{overduenoticerequired}, $samplecat->{issuelimit},
141 $samplecat->{reservefee}, $samplecat->{hidelostitems},
142 $samplecat->{category_type}
146 my $record = MARC::Record->new();
147 $record->append_fields(
148 MARC::Field->new( '952', '0', '0', a => $samplebranch1->{branchcode} ) );
149 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
151 my $barcode_1 = 'barcode_1';
152 my $barcode_2 = 'barcode_2';
153 my @sampleitem1 = C4::Items::AddItem(
155 barcode => $barcode_1,
156 itemcallnumber => 'callnumber1',
157 homebranch => $samplebranch1->{branchcode},
158 holdingbranch => $samplebranch1->{branchcode},
164 my $item_id1 = $sampleitem1[2];
165 my @sampleitem2 = C4::Items::AddItem(
167 barcode => $barcode_2,
168 itemcallnumber => 'callnumber2',
169 homebranch => $samplebranch2->{branchcode},
170 holdingbranch => $samplebranch2->{branchcode},
176 my $item_id2 = $sampleitem2[2];
179 my $borrower_id1 = C4::Members::AddMember(
180 firstname => 'firstname1',
181 surname => 'surname1 ',
182 categorycode => $samplecat->{categorycode},
183 branchcode => $samplebranch1->{branchcode},
185 my $borrower_1 = C4::Members::GetMember(borrowernumber => $borrower_id1);
186 my $borrower_id2 = C4::Members::AddMember(
187 firstname => 'firstname2',
188 surname => 'surname2 ',
189 categorycode => $samplecat->{categorycode},
190 branchcode => $samplebranch2->{branchcode},
192 my $borrower_2 = C4::Members::GetMember(borrowernumber => $borrower_id2);
194 # NEED TO BE FIXED !!!
195 # The first parameter for set_userenv is the class ref
196 #my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'username', 'CPL', 'CPL', 'email@example.org' );
197 my @USERENV = ( $borrower_id1, 'test', 'MASTERTEST', 'firstname', 'CPL', 'CPL', 'email@example.org' );
199 C4::Context->_new_userenv('DUMMY_SESSION_ID');
200 C4::Context->set_userenv(@USERENV);
202 my $userenv = C4::Context->userenv
203 or BAIL_OUT("No userenv");
208 $query = " SELECT count(*) FROM issues";
209 my $sth = $dbh->prepare($query);
211 my $countissue = $sth -> fetchrow_array;
212 is ($countissue ,0, "there is no issue");
213 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
214 is( ref $issue1, 'Koha::Schema::Result::Issue',
215 'AddIssue returns a Koha::Schema::Result::Issue object' );
216 my $datedue1 = dt_from_string( $issue1->date_due() );
219 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
220 "Koha::Schema::Result::Issue->date_due() returns a date"
222 my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
224 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
225 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
226 my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
229 $countissue = $sth -> fetchrow_array;
230 is ($countissue,1,"1 issues have been added");
232 #Test AddIssuingCharge
233 $query = " SELECT count(*) FROM accountlines";
234 $sth = $dbh->prepare($query);
236 my $countaccount = $sth -> fetchrow_array;
237 is ($countaccount,0,"0 accountline exists");
238 is( C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, 10 ),
239 1, "An issuing charge has been added" );
240 my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
242 $countaccount = $sth -> fetchrow_array;
243 is ($countaccount,1,"1 accountline has been added");
247 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
248 $datedue1, $daysago10 );
251 qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
252 "AddRenewal returns a date"
255 #Test GetBiblioIssues
256 is( GetBiblioIssues(), undef, "GetBiblio Issues without parameters" );
259 #FIXME : As the issues are not correctly added in the database, these tests don't work correctly
260 is(GetItemIssue,undef,"Without parameter GetItemIssue returns undef");
261 #is(GetItemIssue($item_id1),{},"Item1's issues");
264 #FIXME: this routine currently doesn't work be
265 #is_deeply (GetItemIssues,{},"Without parameter, GetItemIssue returns all the issues");
268 is( GetOpenIssue(), undef, "Without parameter GetOpenIssue returns undef" );
269 is( GetOpenIssue(-1), undef,
270 "With wrong parameter GetOpenIssue returns undef" );
271 my $openissue = GetOpenIssue($borrower_id1, $item_id1);
275 my $issue3 = C4::Circulation::AddIssue( $borrower_1, $barcode_1 );
276 #Without anything in DB
277 @renewcount = C4::Circulation::GetRenewCount();
280 [ 0, undef, 0 ], # FIXME Need to be fixed
281 "Without issuing rules and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
283 @renewcount = C4::Circulation::GetRenewCount(-1);
286 [ 0, undef, 0 ], # FIXME Need to be fixed
287 "Without issuing rules and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = undef, renewsleft = 0"
289 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
293 "Without issuing rules and with a valid parameter, renewcount = 2, renewsallowed = undef, renewsleft = 0"
296 #With something in DB
297 # Add a default rule: No renewal allowed
299 INSERT INTO issuingrules( categorycode, itemtype, branchcode, issuelength, renewalsallowed )
300 VALUES ( '*', '*', '*', 10, 0 )
302 @renewcount = C4::Circulation::GetRenewCount();
306 "With issuing rules (renewal disallowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
308 @renewcount = C4::Circulation::GetRenewCount(-1);
312 "With issuing rules (renewal disallowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 0, renewsleft = 0"
314 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
318 "With issuing rules (renewal disallowed) and with a valid parameter, Getrenewcount returns renewcount = 2, renewsallowed = 0, renewsleft = 0"
321 # Add a default rule: renewal is allowed
323 UPDATE issuingrules SET renewalsallowed = 3
325 @renewcount = C4::Circulation::GetRenewCount();
329 "With issuing rules (renewal allowed) and without parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
331 @renewcount = C4::Circulation::GetRenewCount(-1);
335 "With issuing rules (renewal allowed) and without wrong parameter, GetRenewCount returns renewcount = 0, renewsallowed = 3, renewsleft = 3"
337 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
341 "With issuing rules (renewal allowed) and with a valid parameter, Getrenewcount of item1 returns 3 renews left"
344 AddRenewal( $borrower_id1, $item_id1, $samplebranch1->{branchcode},
345 $datedue3, $daysago10 );
346 @renewcount = C4::Circulation::GetRenewCount($borrower_id1, $item_id1);
350 "With issuing rules (renewal allowed, 1 remaining) and with a valid parameter, Getrenewcount of item1 returns 0 renews left"
353 $dbh->do("DELETE FROM old_issues");
354 AddReturn($barcode_1);
355 my $return = $dbh->selectrow_hashref("SELECT DATE(returndate) AS return_date, CURRENT_DATE() AS today FROM old_issues LIMIT 1" );
356 ok( $return->{return_date} eq $return->{today}, "Item returned with no return date specified has todays date" );
358 $dbh->do("DELETE FROM old_issues");
359 C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10, 0, $today );
360 AddReturn($barcode_1, undef, undef, undef, '2014-04-01 23:42');
361 $return = $dbh->selectrow_hashref("SELECT * FROM old_issues LIMIT 1" );
362 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" );
365 ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem(
367 barcode => 'barcode_3',
368 itemcallnumber => 'callnumber3',
369 homebranch => $samplebranch1->{branchcode},
370 holdingbranch => $samplebranch1->{branchcode},
376 C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
377 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
378 my $item = GetItem( $itemnumber );
379 ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
381 C4::Context->set_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
382 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
383 $item = GetItem( $itemnumber );
384 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
386 AddReturn( 'barcode_3', $samplebranch1->{branchcode} );
387 $item = GetItem( $itemnumber );
388 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
390 # Bug 14640 - Cancel the hold on checking out if asked
391 my $reserve_id = AddReserve('CPL', $borrower_id1, $biblionumber,
392 undef, 1, undef, undef, "a note", "a title", undef, '');
393 ok( $reserve_id, 'The reserve should have been inserted' );
394 AddIssue( $borrower_2, $barcode_1, dt_from_string, 'cancel' );
395 my $reserve = GetReserve( $reserve_id );
396 is( $reserve, undef, 'The reserve should have been correctly cancelled' );