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 => 5;
25 use t::lib::TestBuilder;
32 use Koha::Account::Lines;
40 # Mock userenv, used by AddIssue
43 my $context = Test::MockModule->new('C4::Context');
49 number => $manager_id,
56 my $schema = Koha::Database->schema;
57 $schema->storage->txn_begin;
59 my $builder = t::lib::TestBuilder->new();
60 Koha::IssuingRules->search->delete;
61 my $rule = Koha::IssuingRule->new(
71 my $manager = $builder->build({source => 'Borrower'});
72 $manager_id = $manager->{borrowernumber};
74 subtest "InProcessingToShelvingCart tests" => sub {
78 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
79 my $permanent_location = 'TEST';
80 my $location = 'PROC';
82 # Create a biblio record with biblio-level itemtype
83 my $record = MARC::Record->new();
84 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
85 my $built_item = $builder->build({
88 biblionumber => $biblionumber,
89 homebranch => $branch,
90 holdingbranch => $branch,
91 location => $location,
92 permanent_location => $permanent_location
95 my $barcode = $built_item->{ barcode };
96 my $itemnumber = $built_item->{ itemnumber };
99 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
100 AddReturn( $barcode, $branch );
101 $item = Koha::Items->find( $itemnumber );
102 is( $item->location, 'CART',
103 "InProcessingToShelvingCart functions as intended" );
105 ModItem( {location => $location}, undef, $itemnumber );
107 t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
108 AddReturn( $barcode, $branch );
109 $item = Koha::Items->find( $itemnumber );
110 is( $item->location, $permanent_location,
111 "InProcessingToShelvingCart functions as intended" );
115 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
119 # Set item-level item types
120 t::lib::Mocks::mock_preference( "item-level_itypes", 1 );
122 # Make sure logging is enabled
123 t::lib::Mocks::mock_preference( "IssueLog", 1 );
124 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
126 # Create an itemtype for biblio-level item type
127 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
128 # Create an itemtype for item-level item type
129 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
131 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
133 my $borrowernumber = $builder->build({
134 source => 'Borrower',
135 value => { branchcode => $branch }
136 })->{ borrowernumber };
137 # Look for the defined MARC field for biblio-level itemtype
138 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
140 kohafield => 'biblioitems.itemtype'
142 my $tagfield = $rs->first->tagfield;
143 my $tagsubfield = $rs->first->tagsubfield;
145 # Create a biblio record with biblio-level itemtype
146 my $record = MARC::Record->new();
147 $record->append_fields(
148 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
150 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
151 my $item_with_itemtype = $builder->build(
155 biblionumber => $biblionumber,
156 biblioitemnumber => $biblioitemnumber,
157 homebranch => $branch,
158 holdingbranch => $branch,
159 itype => $ilevel_itemtype
163 my $item_without_itemtype = $builder->build(
167 biblionumber => $biblionumber,
168 biblioitemnumber => $biblioitemnumber,
169 homebranch => $branch,
170 holdingbranch => $branch,
176 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
177 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
178 AddReturn( $item_with_itemtype->{ barcode }, $branch );
179 # Test item-level itemtype was recorded on the 'statistics' table
180 my $stat = $schema->resultset('Statistic')->search({
183 itemnumber => $item_with_itemtype->{ itemnumber }
184 }, { order_by => { -asc => 'datetime' } })->next();
186 is( $stat->itemtype, $ilevel_itemtype,
187 "item-level itype recorded on statistics for return");
188 warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
189 [qr/^item-level_itypes set but no itemtype set for item/,
190 qr/^item-level_itypes set but no itemtype set for item/,
191 qr/^item-level_itypes set but no itemtype set for item/,
192 qr/^item-level_itypes set but no itemtype set for item/,
193 qr/^item-level_itypes set but no itemtype set for item/,
194 qr/^item-level_itypes set but no itemtype set for item/],
195 'Item without itemtype set raises warning on AddIssue';
196 warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
197 qr/^item-level_itypes set but no itemtype set for item/,
198 'Item without itemtype set raises warning on AddReturn';
199 # Test biblio-level itemtype was recorded on the 'statistics' table
200 $stat = $schema->resultset('Statistic')->search({
203 itemnumber => $item_without_itemtype->{ itemnumber }
204 }, { order_by => { -asc => 'datetime' } })->next();
206 is( $stat->itemtype, $blevel_itemtype,
207 "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
211 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
215 # Make sure logging is enabled
216 t::lib::Mocks::mock_preference( "IssueLog", 1 );
217 t::lib::Mocks::mock_preference( "ReturnLog", 1 );
219 # Set biblio level item types
220 t::lib::Mocks::mock_preference( "item-level_itypes", 0 );
222 # Create an itemtype for biblio-level item type
223 my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
224 # Create an itemtype for item-level item type
225 my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
227 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
229 my $borrowernumber = $builder->build({
230 source => 'Borrower',
231 value => { branchcode => $branch }
232 })->{ borrowernumber };
233 # Look for the defined MARC field for biblio-level itemtype
234 my $rs = $schema->resultset('MarcSubfieldStructure')->search({
236 kohafield => 'biblioitems.itemtype'
238 my $tagfield = $rs->first->tagfield;
239 my $tagsubfield = $rs->first->tagsubfield;
241 # Create a biblio record with biblio-level itemtype
242 my $record = MARC::Record->new();
243 $record->append_fields(
244 MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
246 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
247 my $item_with_itemtype = $builder->build({
250 biblionumber => $biblionumber,
251 biblioitemnumber => $biblioitemnumber,
252 homebranch => $branch,
253 holdingbranch => $branch,
254 itype => $ilevel_itemtype
257 my $item_without_itemtype = $builder->build({
260 biblionumber => $biblionumber,
261 biblioitemnumber => $biblioitemnumber,
262 homebranch => $branch,
263 holdingbranch => $branch,
268 my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
270 AddIssue( $borrower, $item_with_itemtype->{ barcode } );
271 AddReturn( $item_with_itemtype->{ barcode }, $branch );
272 # Test item-level itemtype was recorded on the 'statistics' table
273 my $stat = $schema->resultset('Statistic')->search({
276 itemnumber => $item_with_itemtype->{ itemnumber }
277 }, { order_by => { -asc => 'datetime' } })->next();
279 is( $stat->itemtype, $blevel_itemtype,
280 "biblio-level itype recorded on statistics for return");
282 AddIssue( $borrower, $item_without_itemtype->{ barcode } );
283 AddReturn( $item_without_itemtype->{ barcode }, $branch );
284 # Test biblio-level itemtype was recorded on the 'statistics' table
285 $stat = $schema->resultset('Statistic')->search({
288 itemnumber => $item_without_itemtype->{ itemnumber }
289 }, { order_by => { -asc => 'datetime' } })->next();
291 is( $stat->itemtype, $blevel_itemtype,
292 "biblio-level itype recorded on statistics for return");
295 subtest 'Handle ids duplication' => sub {
298 t::lib::Mocks::mock_preference( 'item-level_itypes', 1 );
299 t::lib::Mocks::mock_preference( 'CalculateFinesOnReturn', 1 );
300 t::lib::Mocks::mock_preference( 'finesMode', 'production' );
301 Koha::IssuingRules->search->update({ chargeperiod => 1, fine => 1, firstremind => 1, });
303 my $biblio = $builder->build( { source => 'Biblio' } );
304 my $itemtype = $builder->build( { source => 'Itemtype', value => { rentalcharge => 5 } } );
305 my $item = $builder->build(
309 biblionumber => $biblio->{biblionumber},
313 itype => $itemtype->{itemtype},
317 my $patron = $builder->build({source => 'Borrower'});
318 $patron = Koha::Patrons->find( $patron->{borrowernumber} );
320 my $original_checkout = AddIssue( $patron->unblessed, $item->{barcode}, dt_from_string->subtract( days => 50 ) );
321 my $issue_id = $original_checkout->issue_id;
322 my $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
323 is( $account_lines->count, 1, '1 account line should exist for this issue_id' );
324 is( $account_lines->next->description, 'Rental', 'patron has been charged the rentalcharge' );
325 $account_lines->delete;
327 # Create an existing entry in old_issue
328 $builder->build({ source => 'OldIssue', value => { issue_id => $issue_id } });
330 my $old_checkout = Koha::Old::Checkouts->find( $issue_id );
332 my ($doreturn, $messages, $new_checkout, $borrower);
334 ( $doreturn, $messages, $new_checkout, $borrower ) =
335 AddReturn( $item->{barcode}, undef, undef, undef, dt_from_string );
338 qr{.*DBD::mysql::st execute failed: Duplicate entry.*},
339 { carped => qr{The checkin for the following issue failed.*Duplicate ID.*} }
341 'DBD should have raised an error about dup primary key';
343 is( $doreturn, 0, 'Return should not have been done' );
344 is( $messages->{WasReturned}, 0, 'messages should have the WasReturned flag set to 0' );
345 is( $messages->{DataCorrupted}, 1, 'messages should have the DataCorrupted flag set to 1' );
347 $account_lines = Koha::Account::Lines->search({ borrowernumber => $patron->borrowernumber, issue_id => $issue_id });
348 is( $account_lines->count, 0, 'No account lines should exist for this issue_id, patron should not have been charged' );
350 is( Koha::Checkouts->find( $issue_id )->issue_id, $issue_id, 'The issues entry should not have been removed' );
353 subtest 'BlockReturnOfLostItems' => sub {
355 my $biblio = $builder->build_object( { class => 'Koha::Biblios' } );
356 my $item = $builder->build_object(
358 class => 'Koha::Items',
360 biblionumber => $biblio->biblionumber,
367 my $patron = $builder->build_object({class => 'Koha::Patrons'});
368 my $checkout = AddIssue( $patron->unblessed, $item->barcode );
370 # Mark the item as lost
371 ModItem({itemlost => 1}, $biblio->biblionumber, $item->itemnumber);
373 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 1);
374 my ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
375 is( $doreturn, 0, "With BlockReturnOfLostItems, a checkin of a lost item should be blocked");
376 is( $messages->{WasLost}, 1, "... and the WasLost flag should be set");
378 $item->discard_changes;
379 is( $item->itemlost, 1, "Item remains lost" );
381 t::lib::Mocks::mock_preference('BlockReturnOfLostItems', 0);
382 ( $doreturn, $messages, $issue ) = AddReturn($item->barcode);
383 is( $doreturn, 1, "Without BlockReturnOfLostItems, a checkin of a lost item should not be blocked");