3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 15;
28 use C4::Circulation qw( AddIssue LostItem AddReturn );
31 use Koha::Item::Transfer::Limits;
34 use Koha::DateUtils qw( dt_from_string );
36 use t::lib::TestBuilder;
40 my $schema = Koha::Database->new->schema;
41 $schema->storage->txn_begin;
43 my $dbh = C4::Context->dbh;
45 my $builder = t::lib::TestBuilder->new;
46 my $library = $builder->build( { source => 'Branch' } );
47 my $nb_of_items = Koha::Items->search->count;
48 my $biblio = $builder->build_sample_biblio();
49 my $new_item_1 = $builder->build_sample_item({
50 biblionumber => $biblio->biblionumber,
51 homebranch => $library->{branchcode},
52 holdingbranch => $library->{branchcode},
54 my $new_item_2 = $builder->build_sample_item({
55 biblionumber => $biblio->biblionumber,
56 homebranch => $library->{branchcode},
57 holdingbranch => $library->{branchcode},
61 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
63 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
64 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
66 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
67 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
69 subtest 'store' => sub {
72 my $biblio = $builder->build_sample_biblio;
73 my $today = dt_from_string->set( hour => 0, minute => 0, second => 0 );
74 my $item = Koha::Item->new(
76 homebranch => $library->{branchcode},
77 holdingbranch => $library->{branchcode},
78 biblionumber => $biblio->biblionumber,
81 )->store->get_from_storage;
83 is( t::lib::Dates::compare( $item->replacementpricedate, $today ),
84 0, 'replacementpricedate must have been set to today if not given' );
85 is( t::lib::Dates::compare( $item->datelastseen, $today ),
86 0, 'datelastseen must have been set to today if not given' );
89 $biblio->biblioitem->itemtype,
90 'items.itype must have been set to biblioitem.itemtype is not given'
94 subtest 'permanent_location' => sub {
97 subtest 'location passed to ->store' => sub {
100 my $location = 'my_loc';
102 homebranch => $library->{branchcode},
103 holdingbranch => $library->{branchcode},
104 biblionumber => $biblio->biblionumber,
105 location => $location,
109 # NewItemsDefaultLocation not set
110 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', '' );
112 # Not passing permanent_location on creating the item
113 my $item = Koha::Item->new($attributes)->store->get_from_storage;
114 is( $item->location, $location,
115 'location must have been set to location if given' );
116 is( $item->permanent_location, $item->location,
117 'permanent_location must have been set to location if not given' );
120 # Passing permanent_location on creating the item
121 $item = Koha::Item->new(
122 { %$attributes, permanent_location => 'perm_loc' } )
123 ->store->get_from_storage;
124 is( $item->permanent_location, 'perm_loc',
125 'permanent_location must have been kept if given' );
130 # NewItemsDefaultLocation set
131 my $default_location = 'default_location';
132 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', $default_location );
134 # Not passing permanent_location on creating the item
135 my $item = Koha::Item->new($attributes)->store->get_from_storage;
136 is( $item->location, $location,
137 'location must have been kept if given' );
138 is( $item->permanent_location, $location,
139 'permanent_location must have been set to the location given' );
142 # Passing permanent_location on creating the item
143 $item = Koha::Item->new(
144 { %$attributes, permanent_location => 'perm_loc' } )
145 ->store->get_from_storage;
146 is( $item->location, $location,
147 'location must have been kept if given' );
148 is( $item->permanent_location, 'perm_loc',
149 'permanent_location must have been kept if given' );
154 subtest 'location NOT passed to ->store' => sub {
158 homebranch => $library->{branchcode},
159 holdingbranch => $library->{branchcode},
160 biblionumber => $biblio->biblionumber,
164 # NewItemsDefaultLocation not set
165 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', '' );
167 # Not passing permanent_location on creating the item
168 my $item = Koha::Item->new($attributes)->store->get_from_storage;
169 is( $item->location, undef,
170 'location not passed and no default, it is undef' );
171 is( $item->permanent_location, $item->location,
172 'permanent_location must have been set to location if not given' );
175 # Passing permanent_location on creating the item
176 $item = Koha::Item->new(
177 { %$attributes, permanent_location => 'perm_loc' } )
178 ->store->get_from_storage;
179 is( $item->permanent_location, 'perm_loc',
180 'permanent_location must have been kept if given' );
185 # NewItemsDefaultLocation set
186 my $default_location = 'default_location';
187 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', $default_location );
189 # Not passing permanent_location on creating the item
190 my $item = Koha::Item->new($attributes)->store->get_from_storage;
191 is( $item->location, $default_location,
192 'location must have been set to default location if not given' );
193 is( $item->permanent_location, $default_location,
194 'permanent_location must have been set to the default location as well' );
197 # Passing permanent_location on creating the item
198 $item = Koha::Item->new(
199 { %$attributes, permanent_location => 'perm_loc' } )
200 ->store->get_from_storage;
201 is( $item->location, $default_location,
202 'location must have been set to default location if not given' );
203 is( $item->permanent_location, 'perm_loc',
204 'permanent_location must have been kept if given' );
211 subtest '*_on updates' => sub {
214 # Once the '_on' value is set (triggered by the related field turning from false to true)
215 # it should not be re-set for any changes outside of the related field being 'unset'.
217 my @fields = qw( itemlost withdrawn damaged );
218 my $today = dt_from_string();
219 my $yesterday = $today->clone()->subtract( days => 1 );
221 for my $field ( @fields ) {
222 my $item = $builder->build_sample_item(
225 itemlost_on => undef,
227 withdrawn_on => undef,
232 my $field_on = $field . '_on';
234 # Set field for the first time
235 Time::Fake->offset( $yesterday->epoch );
236 $item->$field(1)->store;
237 $item->get_from_storage;
238 is( t::lib::Dates::compare( $item->$field_on, $yesterday ),
239 0, $field_on . " was set upon first truthy setting" );
241 # Update the field to a new 'true' value
242 Time::Fake->offset( $today->epoch );
243 $item->$field(2)->store;
244 $item->get_from_storage;
245 is( t::lib::Dates::compare( $item->$field_on, $yesterday ),
246 0, $field_on . " was not updated upon second truthy setting" );
248 # Update the field to a new 'false' value
249 $item->$field(0)->store;
250 $item->get_from_storage;
251 is($item->$field_on, undef, $field_on . " was unset upon untruthy setting");
257 subtest '_lost_found_trigger' => sub {
260 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
261 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
263 my $processfee_amount = 20;
264 my $replacement_amount = 99.00;
265 my $item_type = $builder->build_object(
267 class => 'Koha::ItemTypes',
271 defaultreplacecost => undef,
272 processfee => $processfee_amount,
273 rentalcharge_daily => 0,
277 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
279 $biblio = $builder->build_sample_biblio( { author => 'Hall, Daria' } );
281 subtest 'Full write-off tests' => sub {
285 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
287 $builder->build_object( { class => "Koha::Patrons" } );
288 t::lib::Mocks::mock_userenv(
289 { patron => $manager, branchcode => $manager->branchcode } );
291 my $item = $builder->build_sample_item(
293 biblionumber => $biblio->biblionumber,
294 library => $library->branchcode,
295 replacementprice => $replacement_amount,
296 itype => $item_type->itemtype,
300 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
302 # Simulate item marked as lost
303 $item->itemlost(3)->store;
304 C4::Circulation::LostItem( $item->itemnumber, 1 );
306 my $processing_fee_lines = Koha::Account::Lines->search(
308 borrowernumber => $patron->id,
309 itemnumber => $item->itemnumber,
310 debit_type_code => 'PROCESSING'
313 is( $processing_fee_lines->count,
314 1, 'Only one processing fee produced' );
315 my $processing_fee_line = $processing_fee_lines->next;
316 is( $processing_fee_line->amount + 0,
318 'The right PROCESSING amount is generated' );
319 is( $processing_fee_line->amountoutstanding + 0,
321 'The right PROCESSING amountoutstanding is generated' );
323 my $lost_fee_lines = Koha::Account::Lines->search(
325 borrowernumber => $patron->id,
326 itemnumber => $item->itemnumber,
327 debit_type_code => 'LOST'
330 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
331 my $lost_fee_line = $lost_fee_lines->next;
332 is( $lost_fee_line->amount + 0,
333 $replacement_amount, 'The right LOST amount is generated' );
334 is( $lost_fee_line->amountoutstanding + 0,
336 'The right LOST amountoutstanding is generated' );
337 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
339 my $account = $patron->account;
340 my $debts = $account->outstanding_debits;
343 my $credit = $account->add_credit(
345 amount => $account->balance,
350 $credit->apply( { debits => [ $debts->as_list ] } );
352 # Simulate item marked as found
353 $item->itemlost(0)->store;
354 is( $item->{_refunded}, undef, 'No LOST_FOUND account line added' );
356 $lost_fee_line->discard_changes; # reload from DB
357 is( $lost_fee_line->amountoutstanding + 0,
358 0, 'Lost fee has no outstanding amount' );
359 is( $lost_fee_line->debit_type_code,
360 'LOST', 'Lost fee now still has account type of LOST' );
361 is( $lost_fee_line->status, 'FOUND',
362 "Lost fee now has account status of FOUND - No Refund" );
364 is( $patron->account->balance,
365 -0, 'The patron balance is 0, everything was written off' );
368 subtest 'Full payment tests' => sub {
372 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
374 my $item = $builder->build_sample_item(
376 biblionumber => $biblio->biblionumber,
377 library => $library->branchcode,
378 replacementprice => $replacement_amount,
379 itype => $item_type->itemtype
384 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
386 # Simulate item marked as lost
387 $item->itemlost(1)->store;
388 C4::Circulation::LostItem( $item->itemnumber, 1 );
390 my $processing_fee_lines = Koha::Account::Lines->search(
392 borrowernumber => $patron->id,
393 itemnumber => $item->itemnumber,
394 debit_type_code => 'PROCESSING'
397 is( $processing_fee_lines->count,
398 1, 'Only one processing fee produced' );
399 my $processing_fee_line = $processing_fee_lines->next;
400 is( $processing_fee_line->amount + 0,
402 'The right PROCESSING amount is generated' );
403 is( $processing_fee_line->amountoutstanding + 0,
405 'The right PROCESSING amountoutstanding is generated' );
407 my $lost_fee_lines = Koha::Account::Lines->search(
409 borrowernumber => $patron->id,
410 itemnumber => $item->itemnumber,
411 debit_type_code => 'LOST'
414 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
415 my $lost_fee_line = $lost_fee_lines->next;
416 is( $lost_fee_line->amount + 0,
417 $replacement_amount, 'The right LOST amount is generated' );
418 is( $lost_fee_line->amountoutstanding + 0,
420 'The right LOST amountountstanding is generated' );
422 my $account = $patron->account;
423 my $debts = $account->outstanding_debits;
426 my $credit = $account->add_credit(
428 amount => $account->balance,
433 $credit->apply( { debits => [ $debts->as_list ] } );
435 # Simulate item marked as found
436 $item->itemlost(0)->store;
437 is( $item->{_refunded}, 1, 'Refund triggered' );
439 my $credit_return = Koha::Account::Lines->search(
441 itemnumber => $item->itemnumber,
442 credit_type_code => 'LOST_FOUND'
447 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
448 is( $credit_return->amount + 0,
450 'The account line of type LOST_FOUND has an amount of -99' );
452 $credit_return->amountoutstanding + 0,
454 'The account line of type LOST_FOUND has an amountoutstanding of -99'
457 $lost_fee_line->discard_changes;
458 is( $lost_fee_line->amountoutstanding + 0,
459 0, 'Lost fee has no outstanding amount' );
460 is( $lost_fee_line->debit_type_code,
461 'LOST', 'Lost fee now still has account type of LOST' );
462 is( $lost_fee_line->status, 'FOUND',
463 "Lost fee now has account status of FOUND" );
465 is( $patron->account->balance, -99,
466 'The patron balance is -99, a credit that equals the lost fee payment'
470 subtest 'Test without payment or write off' => sub {
474 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
476 my $item = $builder->build_sample_item(
478 biblionumber => $biblio->biblionumber,
479 library => $library->branchcode,
480 replacementprice => 23.00,
481 replacementprice => $replacement_amount,
482 itype => $item_type->itemtype
487 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
489 # Simulate item marked as lost
490 $item->itemlost(3)->store;
491 C4::Circulation::LostItem( $item->itemnumber, 1 );
493 my $processing_fee_lines = Koha::Account::Lines->search(
495 borrowernumber => $patron->id,
496 itemnumber => $item->itemnumber,
497 debit_type_code => 'PROCESSING'
500 is( $processing_fee_lines->count,
501 1, 'Only one processing fee produced' );
502 my $processing_fee_line = $processing_fee_lines->next;
503 is( $processing_fee_line->amount + 0,
505 'The right PROCESSING amount is generated' );
506 is( $processing_fee_line->amountoutstanding + 0,
508 'The right PROCESSING amountoutstanding is generated' );
510 my $lost_fee_lines = Koha::Account::Lines->search(
512 borrowernumber => $patron->id,
513 itemnumber => $item->itemnumber,
514 debit_type_code => 'LOST'
517 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
518 my $lost_fee_line = $lost_fee_lines->next;
519 is( $lost_fee_line->amount + 0,
520 $replacement_amount, 'The right LOST amount is generated' );
521 is( $lost_fee_line->amountoutstanding + 0,
523 'The right LOST amountountstanding is generated' );
525 # Simulate item marked as found
526 $item->itemlost(0)->store;
527 is( $item->{_refunded}, 1, 'Refund triggered' );
529 my $credit_return = Koha::Account::Lines->search(
531 itemnumber => $item->itemnumber,
532 credit_type_code => 'LOST_FOUND'
537 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
538 is( $credit_return->amount + 0,
540 'The account line of type LOST_FOUND has an amount of -99' );
542 $credit_return->amountoutstanding + 0,
544 'The account line of type LOST_FOUND has an amountoutstanding of 0'
547 $lost_fee_line->discard_changes;
548 is( $lost_fee_line->amountoutstanding + 0,
549 0, 'Lost fee has no outstanding amount' );
550 is( $lost_fee_line->debit_type_code,
551 'LOST', 'Lost fee now still has account type of LOST' );
552 is( $lost_fee_line->status, 'FOUND',
553 "Lost fee now has account status of FOUND" );
555 is( $patron->account->balance,
556 20, 'The patron balance is 20, still owes the processing fee' );
560 'Test with partial payment and write off, and remaining debt' =>
565 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 0 );
567 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
568 my $item = $builder->build_sample_item(
570 biblionumber => $biblio->biblionumber,
571 library => $library->branchcode,
572 replacementprice => $replacement_amount,
573 itype => $item_type->itemtype
578 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
580 # Simulate item marked as lost
581 $item->itemlost(1)->store;
582 C4::Circulation::LostItem( $item->itemnumber, 1 );
584 my $processing_fee_lines = Koha::Account::Lines->search(
586 borrowernumber => $patron->id,
587 itemnumber => $item->itemnumber,
588 debit_type_code => 'PROCESSING'
591 is( $processing_fee_lines->count,
592 1, 'Only one processing fee produced' );
593 my $processing_fee_line = $processing_fee_lines->next;
594 is( $processing_fee_line->amount + 0,
596 'The right PROCESSING amount is generated' );
597 is( $processing_fee_line->amountoutstanding + 0,
599 'The right PROCESSING amountoutstanding is generated' );
601 my $lost_fee_lines = Koha::Account::Lines->search(
603 borrowernumber => $patron->id,
604 itemnumber => $item->itemnumber,
605 debit_type_code => 'LOST'
608 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
609 my $lost_fee_line = $lost_fee_lines->next;
610 is( $lost_fee_line->amount + 0,
611 $replacement_amount, 'The right LOST amount is generated' );
612 is( $lost_fee_line->amountoutstanding + 0,
614 'The right LOST amountountstanding is generated' );
616 my $account = $patron->account;
619 $processfee_amount + $replacement_amount,
620 'Balance is PROCESSING + LOST'
623 # Partially pay fee (99 - 27 = 72)
624 my $payment_amount = 24;
625 my $payment = $account->add_credit(
627 amount => $payment_amount,
633 $payment->apply( { debits => [$lost_fee_line] } );
635 # Partially write off fee (72 - 20 = 52)
636 my $write_off_amount = 20;
637 my $write_off = $account->add_credit(
639 amount => $write_off_amount,
644 $write_off->apply( { debits => [$lost_fee_line] } );
647 my $payment_amount_2 = 3;
648 my $payment_2 = $account->add_credit(
650 amount => $payment_amount_2,
657 { debits => [$lost_fee_line] } );
659 # Partially write off fee (52 - 5 = 47)
660 my $write_off_amount_2 = 5;
661 my $write_off_2 = $account->add_credit(
663 amount => $write_off_amount_2,
670 { debits => [$lost_fee_line] } );
675 $replacement_amount -
680 'Balance is PROCESSING + LOST - PAYMENT 1 - WRITEOFF - PAYMENT 2 - WRITEOFF 2'
683 # VOID payment_2 and writeoff_2
684 $payment_2->void({ interface => 'test' });
685 $write_off_2->void({ interface => 'test' });
690 $replacement_amount -
693 'Balance is PROCESSING + LOST - PAYMENT 1 - WRITEOFF (PAYMENT 2 and WRITEOFF 2 VOIDED)'
696 # Store the amountoutstanding value
697 $lost_fee_line->discard_changes;
698 my $outstanding = $lost_fee_line->amountoutstanding;
701 $replacement_amount - $payment_amount - $write_off_amount,
702 "Lost Fee Outstanding is LOST - PAYMENT 1 - WRITEOFF"
705 # Simulate item marked as found
706 $item->itemlost(0)->store;
707 is( $item->{_refunded}, 1, 'Refund triggered' );
709 my $credit_return = Koha::Account::Lines->search(
711 itemnumber => $item->itemnumber,
712 credit_type_code => 'LOST_FOUND'
717 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
721 $processfee_amount - $payment_amount,
722 'Balance is PROCESSING - PAYMENT (LOST_FOUND)'
725 $lost_fee_line->discard_changes;
726 is( $lost_fee_line->amountoutstanding + 0,
727 0, 'Lost fee has no outstanding amount' );
728 is( $lost_fee_line->debit_type_code,
729 'LOST', 'Lost fee now still has account type of LOST' );
730 is( $lost_fee_line->status, 'FOUND',
731 "Lost fee now has account status of FOUND" );
734 $credit_return->amount + 0,
735 ( $payment_amount + $outstanding ) * -1,
736 'The account line of type LOST_FOUND has an amount equal to the payment 1 + outstanding'
739 $credit_return->amountoutstanding + 0,
740 $payment_amount * -1,
741 'The account line of type LOST_FOUND has an amountoutstanding equal to the payment'
746 $processfee_amount - $payment_amount,
747 'The patron balance is the difference between the PROCESSING and the credit'
751 subtest 'Partial payment, existing debits and AccountAutoReconcile' =>
756 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
757 my $barcode = 'KD123456793';
758 my $replacement_amount = 100;
759 my $processfee_amount = 20;
761 my $item_type = $builder->build_object(
763 class => 'Koha::ItemTypes',
767 defaultreplacecost => undef,
769 rentalcharge_daily => 0,
773 my $item = Koha::Item->new(
775 biblionumber => $biblio->biblionumber,
776 homebranch => $library->branchcode,
777 holdingbranch => $library->branchcode,
779 replacementprice => $replacement_amount,
780 itype => $item_type->itemtype
785 C4::Circulation::AddIssue( $patron->unblessed, $barcode );
787 # Simulate item marked as lost
788 $item->itemlost(1)->store;
789 C4::Circulation::LostItem( $item->itemnumber, 1 );
791 my $lost_fee_lines = Koha::Account::Lines->search(
793 borrowernumber => $patron->id,
794 itemnumber => $item->itemnumber,
795 debit_type_code => 'LOST'
798 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
799 my $lost_fee_line = $lost_fee_lines->next;
800 is( $lost_fee_line->amount + 0,
801 $replacement_amount, 'The right LOST amount is generated' );
802 is( $lost_fee_line->amountoutstanding + 0,
804 'The right LOST amountountstanding is generated' );
806 my $account = $patron->account;
807 is( $account->balance, $replacement_amount, 'Balance is L' );
810 my $payment_amount = 27;
811 my $payment = $account->add_credit(
813 amount => $payment_amount,
818 $payment->apply( { debits => [$lost_fee_line] } );
822 $replacement_amount - $payment_amount,
826 my $manual_debit_amount = 80;
829 amount => $manual_debit_amount,
837 $manual_debit_amount + $replacement_amount - $payment_amount,
838 'Manual debit applied'
841 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
843 # Simulate item marked as found
844 $item->itemlost(0)->store;
845 is( $item->{_refunded}, 1, 'Refund triggered' );
847 my $credit_return = Koha::Account::Lines->search(
849 itemnumber => $item->itemnumber,
850 credit_type_code => 'LOST_FOUND'
855 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
859 $manual_debit_amount - $payment_amount,
860 'Balance is PROCESSING - payment (LOST_FOUND)'
863 my $manual_debit = Koha::Account::Lines->search(
865 borrowernumber => $patron->id,
866 debit_type_code => 'OVERDUE',
867 status => 'UNRETURNED'
871 $manual_debit->amountoutstanding + 0,
872 $manual_debit_amount - $payment_amount,
873 'reconcile_balance was called'
877 subtest 'Patron deleted' => sub {
880 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
881 my $barcode = 'KD123456794';
882 my $replacement_amount = 100;
883 my $processfee_amount = 20;
885 my $item_type = $builder->build_object(
887 class => 'Koha::ItemTypes',
891 defaultreplacecost => undef,
893 rentalcharge_daily => 0,
897 my $item = Koha::Item->new(
899 biblionumber => $biblio->biblionumber,
900 homebranch => $library->branchcode,
901 holdingbranch => $library->branchcode,
903 replacementprice => $replacement_amount,
904 itype => $item_type->itemtype
909 C4::Circulation::AddIssue( $patron->unblessed, $barcode );
911 # Simulate item marked as lost
912 $item->itemlost(1)->store;
913 C4::Circulation::LostItem( $item->itemnumber, 1 );
918 # Simulate item marked as found
919 $item->itemlost(0)->store;
920 is( $item->{_refunded}, undef, 'No refund triggered' );
924 subtest 'restore fine | no overdue' => sub {
929 $builder->build_object( { class => "Koha::Patrons" } );
930 t::lib::Mocks::mock_userenv(
931 { patron => $manager, branchcode => $manager->branchcode } );
933 # Set lostreturn_policy to 'restore' for tests
934 my $specific_rule_restore = $builder->build(
936 source => 'CirculationRule',
938 branchcode => $manager->branchcode,
939 categorycode => undef,
941 rule_name => 'lostreturn',
942 rule_value => 'restore'
947 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
949 my $item = $builder->build_sample_item(
951 biblionumber => $biblio->biblionumber,
952 library => $library->branchcode,
953 replacementprice => $replacement_amount,
954 itype => $item_type->itemtype
959 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
961 # Simulate item marked as lost
962 $item->itemlost(1)->store;
963 C4::Circulation::LostItem( $item->itemnumber, 1 );
965 my $processing_fee_lines = Koha::Account::Lines->search(
967 borrowernumber => $patron->id,
968 itemnumber => $item->itemnumber,
969 debit_type_code => 'PROCESSING'
972 is( $processing_fee_lines->count,
973 1, 'Only one processing fee produced' );
974 my $processing_fee_line = $processing_fee_lines->next;
975 is( $processing_fee_line->amount + 0,
977 'The right PROCESSING amount is generated' );
978 is( $processing_fee_line->amountoutstanding + 0,
980 'The right PROCESSING amountoutstanding is generated' );
982 my $lost_fee_lines = Koha::Account::Lines->search(
984 borrowernumber => $patron->id,
985 itemnumber => $item->itemnumber,
986 debit_type_code => 'LOST'
989 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
990 my $lost_fee_line = $lost_fee_lines->next;
991 is( $lost_fee_line->amount + 0,
992 $replacement_amount, 'The right LOST amount is generated' );
993 is( $lost_fee_line->amountoutstanding + 0,
995 'The right LOST amountountstanding is generated' );
997 my $account = $patron->account;
998 my $debts = $account->outstanding_debits;
1001 my $credit = $account->add_credit(
1003 amount => $account->balance,
1005 interface => 'test',
1008 $credit->apply( { debits => [ $debts->as_list ] } );
1010 # Simulate item marked as found
1011 $item->itemlost(0)->store;
1012 is( $item->{_refunded}, 1, 'Refund triggered' );
1013 is( $item->{_restored}, undef, 'Restore not triggered when there is no overdue fine found' );
1016 subtest 'restore fine | unforgiven overdue' => sub {
1020 # Set lostreturn_policy to 'restore' for tests
1022 $builder->build_object( { class => "Koha::Patrons" } );
1023 t::lib::Mocks::mock_userenv(
1024 { patron => $manager, branchcode => $manager->branchcode } );
1025 my $specific_rule_restore = $builder->build(
1027 source => 'CirculationRule',
1029 branchcode => $manager->branchcode,
1030 categorycode => undef,
1032 rule_name => 'lostreturn',
1033 rule_value => 'restore'
1038 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1040 my $item = $builder->build_sample_item(
1042 biblionumber => $biblio->biblionumber,
1043 library => $library->branchcode,
1044 replacementprice => $replacement_amount,
1045 itype => $item_type->itemtype
1050 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
1052 # Simulate item marked as lost
1053 $item->itemlost(1)->store;
1054 C4::Circulation::LostItem( $item->itemnumber, 1 );
1056 my $processing_fee_lines = Koha::Account::Lines->search(
1058 borrowernumber => $patron->id,
1059 itemnumber => $item->itemnumber,
1060 debit_type_code => 'PROCESSING'
1063 is( $processing_fee_lines->count,
1064 1, 'Only one processing fee produced' );
1065 my $processing_fee_line = $processing_fee_lines->next;
1066 is( $processing_fee_line->amount + 0,
1068 'The right PROCESSING amount is generated' );
1069 is( $processing_fee_line->amountoutstanding + 0,
1071 'The right PROCESSING amountoutstanding is generated' );
1073 my $lost_fee_lines = Koha::Account::Lines->search(
1075 borrowernumber => $patron->id,
1076 itemnumber => $item->itemnumber,
1077 debit_type_code => 'LOST'
1080 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1081 my $lost_fee_line = $lost_fee_lines->next;
1082 is( $lost_fee_line->amount + 0,
1083 $replacement_amount, 'The right LOST amount is generated' );
1084 is( $lost_fee_line->amountoutstanding + 0,
1085 $replacement_amount,
1086 'The right LOST amountountstanding is generated' );
1088 my $account = $patron->account;
1089 my $debts = $account->outstanding_debits;
1092 my $credit = $account->add_credit(
1094 amount => $account->balance,
1096 interface => 'test',
1099 $credit->apply( { debits => [ $debts->as_list ] } );
1102 my $overdue = $account->add_debit(
1105 user_id => $manager->borrowernumber,
1106 library_id => $library->branchcode,
1107 interface => 'test',
1108 item_id => $item->itemnumber,
1112 $overdue->status('LOST')->store();
1113 $overdue->discard_changes;
1114 is( $overdue->status, 'LOST',
1115 'Overdue status set to LOST' );
1117 # Simulate item marked as found
1118 $item->itemlost(0)->store;
1119 is( $item->{_refunded}, 1, 'Refund triggered' );
1120 is( $item->{_restored}, undef, 'Restore not triggered when overdue was not forgiven' );
1121 $overdue->discard_changes;
1122 is( $overdue->status, 'FOUND',
1123 'Overdue status updated to FOUND' );
1126 subtest 'restore fine | forgiven overdue' => sub {
1130 # Set lostreturn_policy to 'restore' for tests
1132 $builder->build_object( { class => "Koha::Patrons" } );
1133 t::lib::Mocks::mock_userenv(
1134 { patron => $manager, branchcode => $manager->branchcode } );
1135 my $specific_rule_restore = $builder->build(
1137 source => 'CirculationRule',
1139 branchcode => $manager->branchcode,
1140 categorycode => undef,
1142 rule_name => 'lostreturn',
1143 rule_value => 'restore'
1148 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1150 my $item = $builder->build_sample_item(
1152 biblionumber => $biblio->biblionumber,
1153 library => $library->branchcode,
1154 replacementprice => $replacement_amount,
1155 itype => $item_type->itemtype
1160 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
1162 # Simulate item marked as lost
1163 $item->itemlost(1)->store;
1164 C4::Circulation::LostItem( $item->itemnumber, 1 );
1166 my $processing_fee_lines = Koha::Account::Lines->search(
1168 borrowernumber => $patron->id,
1169 itemnumber => $item->itemnumber,
1170 debit_type_code => 'PROCESSING'
1173 is( $processing_fee_lines->count,
1174 1, 'Only one processing fee produced' );
1175 my $processing_fee_line = $processing_fee_lines->next;
1176 is( $processing_fee_line->amount + 0,
1178 'The right PROCESSING amount is generated' );
1179 is( $processing_fee_line->amountoutstanding + 0,
1181 'The right PROCESSING amountoutstanding is generated' );
1183 my $lost_fee_lines = Koha::Account::Lines->search(
1185 borrowernumber => $patron->id,
1186 itemnumber => $item->itemnumber,
1187 debit_type_code => 'LOST'
1190 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1191 my $lost_fee_line = $lost_fee_lines->next;
1192 is( $lost_fee_line->amount + 0,
1193 $replacement_amount, 'The right LOST amount is generated' );
1194 is( $lost_fee_line->amountoutstanding + 0,
1195 $replacement_amount,
1196 'The right LOST amountountstanding is generated' );
1198 my $account = $patron->account;
1199 my $debts = $account->outstanding_debits;
1202 my $credit = $account->add_credit(
1204 amount => $account->balance,
1206 interface => 'test',
1209 $credit->apply( { debits => [ $debts->as_list ] } );
1212 my $overdue = $account->add_debit(
1215 user_id => $manager->borrowernumber,
1216 library_id => $library->branchcode,
1217 interface => 'test',
1218 item_id => $item->itemnumber,
1222 $overdue->status('LOST')->store();
1223 is( $overdue->status, 'LOST',
1224 'Overdue status set to LOST' );
1226 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 0 );
1229 $credit = $account->add_credit(
1232 user_id => $manager->borrowernumber,
1233 library_id => $library->branchcode,
1234 interface => 'test',
1236 item_id => $item->itemnumber
1239 $credit->apply( { debits => [$overdue] } );
1241 # Simulate item marked as found
1242 $item->itemlost(0)->store;
1243 is( $item->{_refunded}, 1, 'Refund triggered' );
1244 is( $item->{_restored}, 1, 'Restore triggered when overdue was forgiven' );
1245 $overdue->discard_changes;
1246 is( $overdue->status, 'FOUND', 'Overdue status updated to FOUND' );
1247 is( $overdue->amountoutstanding, $overdue->amount, 'Overdue outstanding has been restored' );
1248 $credit->discard_changes;
1249 is( $credit->status, 'VOID', 'Overdue Forgival has been marked as VOID');
1252 subtest 'Continue when userenv is not set' => sub {
1255 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1256 my $barcode = 'KD123456795';
1257 my $replacement_amount = 100;
1258 my $processfee_amount = 20;
1260 my $item_type = $builder->build_object(
1262 class => 'Koha::ItemTypes',
1264 notforloan => undef,
1266 defaultreplacecost => undef,
1268 rentalcharge_daily => 0,
1272 my $item = $builder->build_sample_item(
1274 biblionumber => $biblio->biblionumber,
1275 homebranch => $library->branchcode,
1276 holdingbranch => $library->branchcode,
1277 barcode => $barcode,
1278 replacementprice => $replacement_amount,
1279 itype => $item_type->itemtype
1284 C4::Circulation::AddIssue( $patron->unblessed, $barcode );
1286 # Simulate item marked as lost
1287 $item->itemlost(1)->store;
1288 C4::Circulation::LostItem( $item->itemnumber, 1 );
1291 C4::Context->_new_userenv(undef);
1293 # Simluate item marked as found
1294 $item->itemlost(0)->store;
1295 is( $item->{_refunded}, 1, 'No refund triggered' );
1300 subtest 'log_action' => sub {
1302 t::lib::Mocks::mock_preference( 'CataloguingLog', 1 );
1304 my $item = Koha::Item->new(
1306 homebranch => $library->{branchcode},
1307 holdingbranch => $library->{branchcode},
1308 biblionumber => $biblio->biblionumber,
1309 location => 'my_loc',
1313 Koha::ActionLogs->search(
1315 module => 'CATALOGUING',
1317 object => $item->itemnumber,
1322 "Item creation logged"
1325 $item->location('another_loc')->store;
1327 Koha::ActionLogs->search(
1329 module => 'CATALOGUING',
1331 object => $item->itemnumber
1335 "Item modification logged"
1340 subtest 'get_transfer' => sub {
1343 my $transfer = $new_item_1->get_transfer();
1344 is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
1346 my $library_to = $builder->build( { source => 'Branch' } );
1348 my $transfer_1 = $builder->build_object(
1350 class => 'Koha::Item::Transfers',
1352 itemnumber => $new_item_1->itemnumber,
1353 frombranch => $new_item_1->holdingbranch,
1354 tobranch => $library_to->{branchcode},
1357 datearrived => undef,
1358 datecancelled => undef,
1359 daterequested => \'NOW()'
1364 $transfer = $new_item_1->get_transfer();
1365 is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1367 my $transfer_2 = $builder->build_object(
1369 class => 'Koha::Item::Transfers',
1371 itemnumber => $new_item_1->itemnumber,
1372 frombranch => $new_item_1->holdingbranch,
1373 tobranch => $library_to->{branchcode},
1376 datearrived => undef,
1377 datecancelled => undef,
1378 daterequested => \'NOW()'
1383 $transfer = $new_item_1->get_transfer();
1384 is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the oldest transfer request');
1386 $transfer_2->datesent(\'NOW()')->store;
1387 $transfer = $new_item_1->get_transfer();
1388 is( $transfer->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfer returns the in_transit transfer');
1390 my $transfer_3 = $builder->build_object(
1392 class => 'Koha::Item::Transfers',
1394 itemnumber => $new_item_1->itemnumber,
1395 frombranch => $new_item_1->holdingbranch,
1396 tobranch => $library_to->{branchcode},
1399 datearrived => undef,
1400 datecancelled => undef,
1401 daterequested => \'NOW()'
1406 $transfer_2->datearrived(\'NOW()')->store;
1407 $transfer = $new_item_1->get_transfer();
1408 is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the next queued transfer');
1409 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' );
1411 $transfer_1->datecancelled(\'NOW()')->store;
1412 $transfer = $new_item_1->get_transfer();
1413 is( $transfer->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfer ignores cancelled transfers');
1416 subtest 'holds' => sub {
1419 my $biblio = $builder->build_sample_biblio();
1420 my $item = $builder->build_sample_item({
1421 biblionumber => $biblio->biblionumber,
1423 is($item->holds->count, 0, "Nothing returned if no holds");
1424 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
1425 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
1426 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
1428 is($item->holds()->count,3,"Three holds found");
1429 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
1430 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
1431 is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
1434 subtest 'biblio' => sub {
1437 my $biblio = $retrieved_item_1->biblio;
1438 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
1439 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
1442 subtest 'biblioitem' => sub {
1445 my $biblioitem = $retrieved_item_1->biblioitem;
1446 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
1447 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
1451 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
1452 subtest 'checkout' => sub {
1454 my $item = Koha::Items->find( $new_item_1->itemnumber );
1456 my $checkout = $item->checkout;
1457 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
1460 my $patron = $builder->build({ source => 'Borrower' });
1461 C4::Circulation::AddIssue( $patron, $item->barcode );
1462 $checkout = $retrieved_item_1->checkout;
1463 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
1464 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
1465 is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
1468 C4::Circulation::AddReturn( $item->barcode );
1470 # There is no more checkout on this item, making sure it will not return old checkouts
1471 $checkout = $item->checkout;
1472 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
1475 subtest 'can_be_transferred' => sub {
1478 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
1479 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
1481 my $biblio = $builder->build_sample_biblio();
1482 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1483 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1484 my $item = $builder->build_sample_item({
1485 biblionumber => $biblio->biblionumber,
1486 homebranch => $library1->branchcode,
1487 holdingbranch => $library1->branchcode,
1490 is(Koha::Item::Transfer::Limits->search({
1491 fromBranch => $library1->branchcode,
1492 toBranch => $library2->branchcode,
1493 })->count, 0, 'There are no transfer limits between libraries.');
1494 ok($item->can_be_transferred({ to => $library2 }),
1495 'Item can be transferred between libraries.');
1497 my $limit = Koha::Item::Transfer::Limit->new({
1498 fromBranch => $library1->branchcode,
1499 toBranch => $library2->branchcode,
1500 itemtype => $item->effective_itemtype,
1502 is(Koha::Item::Transfer::Limits->search({
1503 fromBranch => $library1->branchcode,
1504 toBranch => $library2->branchcode,
1505 })->count, 1, 'Given we have added a transfer limit,');
1506 is($item->can_be_transferred({ to => $library2 }), 0,
1507 'Item can no longer be transferred between libraries.');
1508 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
1509 'We get the same result also if we pass the from-library parameter.');
1512 subtest 'filter_by_for_hold' => sub {
1515 my $biblio = $builder->build_sample_biblio;
1516 is( $biblio->items->filter_by_for_hold->count, 0, 'no item yet' );
1517 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } );
1518 is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' );
1519 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } );
1520 is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' );
1521 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
1522 is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' );
1524 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 0 } );
1525 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 1 } );
1526 is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' );
1528 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 0 } );
1529 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 1 } );
1530 is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' );
1532 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } );
1533 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } );
1534 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0);
1535 is( $biblio->items->filter_by_for_hold->count, 5, '5 items for hold - not damaged if not AllowHoldsOnDamagedItems' );
1536 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1);
1537 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' );
1542 # Reset nb_of_items prior to testing delete
1543 $nb_of_items = Koha::Items->search->count;
1546 $retrieved_item_1->delete;
1547 is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
1549 $schema->storage->txn_rollback;
1551 subtest 'filter_by_visible_in_opac() tests' => sub {
1555 $schema->storage->txn_begin;
1557 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1558 my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
1560 $mocked_category->mock( 'override_hidden_items', sub {
1564 # have a fresh biblio
1565 my $biblio = $builder->build_sample_biblio;
1566 # have two itemtypes
1567 my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
1568 my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
1569 # have 5 items on that biblio
1570 my $item_1 = $builder->build_sample_item(
1572 biblionumber => $biblio->biblionumber,
1574 itype => $itype_1->itemtype,
1579 my $item_2 = $builder->build_sample_item(
1581 biblionumber => $biblio->biblionumber,
1583 itype => $itype_2->itemtype,
1588 my $item_3 = $builder->build_sample_item(
1590 biblionumber => $biblio->biblionumber,
1592 itype => $itype_1->itemtype,
1597 my $item_4 = $builder->build_sample_item(
1599 biblionumber => $biblio->biblionumber,
1601 itype => $itype_2->itemtype,
1606 my $item_5 = $builder->build_sample_item(
1608 biblionumber => $biblio->biblionumber,
1610 itype => $itype_1->itemtype,
1615 my $item_6 = $builder->build_sample_item(
1617 biblionumber => $biblio->biblionumber,
1619 itype => $itype_1->itemtype,
1627 my $mocked_context = Test::MockModule->new('C4::Context');
1628 $mocked_context->mock( 'yaml_preference', sub {
1632 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
1633 is( $biblio->items->filter_by_visible_in_opac->count,
1634 6, 'No rules passed, hidelostitems unset' );
1636 is( $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1637 6, 'No rules passed, hidelostitems unset, patron exception changes nothing' );
1639 $rules = { copynumber => [ 2 ] };
1641 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
1643 $biblio->items->filter_by_visible_in_opac->count,
1645 'No rules passed, hidelostitems set'
1649 $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1651 'No rules passed, hidelostitems set, patron exception changes nothing'
1654 $rules = { withdrawn => [ 1, 2 ], copynumber => [ 2 ] };
1656 $biblio->items->filter_by_visible_in_opac->count,
1658 'Rules on withdrawn, hidelostitems set'
1662 $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1664 'hidelostitems set, rules on withdrawn but patron override passed'
1667 $rules = { itype => [ $itype_1->itemtype ], copynumber => [ 2 ] };
1669 $biblio->items->filter_by_visible_in_opac->count,
1671 'Rules on itype, hidelostitems set'
1674 $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ], copynumber => [ 2 ] };
1676 $biblio->items->filter_by_visible_in_opac->count,
1678 'Rules on itype and withdrawn, hidelostitems set'
1681 $biblio->items->filter_by_visible_in_opac
1683 $item_4->itemnumber,
1684 'The right item is returned'
1687 $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ], copynumber => [ 2 ] };
1689 $biblio->items->filter_by_visible_in_opac->count,
1691 'Rules on itype and withdrawn, hidelostitems set'
1694 $biblio->items->filter_by_visible_in_opac
1696 $item_5->itemnumber,
1697 'The right item is returned'
1700 # Make sure the warning on the about page will work
1701 $rules = { itemlost => ['AB'] };
1702 my $c = Koha::Items->filter_by_visible_in_opac->count;
1703 my @warnings = C4::Context->dbh->selectrow_array('SHOW WARNINGS');
1704 is( $warnings[2], q{Truncated incorrect DOUBLE value: 'AB'});
1706 $schema->storage->txn_rollback;
1709 subtest 'filter_out_lost() tests' => sub {
1713 $schema->storage->txn_begin;
1715 # have a fresh biblio
1716 my $biblio = $builder->build_sample_biblio;
1717 # have 3 items on that biblio
1718 my $item_1 = $builder->build_sample_item(
1720 biblionumber => $biblio->biblionumber,
1724 my $item_2 = $builder->build_sample_item(
1726 biblionumber => $biblio->biblionumber,
1730 my $item_3 = $builder->build_sample_item(
1732 biblionumber => $biblio->biblionumber,
1737 is( $biblio->items->filter_out_lost->next->itemnumber, $item_2->itemnumber, 'Right item returned' );
1738 is( $biblio->items->filter_out_lost->count, 1, 'Only one item is not lost' );
1740 $schema->storage->txn_rollback;
1743 subtest 'move_to_biblio() tests' => sub {
1747 $schema->storage->txn_begin;
1749 my $biblio1 = $builder->build_sample_biblio;
1750 my $biblio2 = $builder->build_sample_biblio;
1751 my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1752 my $item2 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1754 $biblio1->items->move_to_biblio($biblio2);
1756 $item1->discard_changes;
1757 $item2->discard_changes;
1759 is($item1->biblionumber, $biblio2->biblionumber, "Item 1 moved");
1760 is($item2->biblionumber, $biblio2->biblionumber, "Item 2 moved");
1762 $schema->storage->txn_rollback;