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 => 16;
28 use C4::Circulation qw( AddIssue LostItem AddReturn );
30 use C4::Serials qw( NewIssue AddItem2Serial );
32 use Koha::Item::Transfer::Limits;
35 use Koha::DateUtils qw( dt_from_string );
37 use t::lib::TestBuilder;
41 my $schema = Koha::Database->new->schema;
42 $schema->storage->txn_begin;
44 my $dbh = C4::Context->dbh;
46 my $builder = t::lib::TestBuilder->new;
47 my $library = $builder->build( { source => 'Branch' } );
48 my $nb_of_items = Koha::Items->search->count;
49 my $biblio = $builder->build_sample_biblio();
50 my $new_item_1 = $builder->build_sample_item({
51 biblionumber => $biblio->biblionumber,
52 homebranch => $library->{branchcode},
53 holdingbranch => $library->{branchcode},
55 my $new_item_2 = $builder->build_sample_item({
56 biblionumber => $biblio->biblionumber,
57 homebranch => $library->{branchcode},
58 holdingbranch => $library->{branchcode},
62 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
64 like( $new_item_1->itemnumber, qr|^\d+$|, 'Adding a new item should have set the itemnumber' );
65 is( Koha::Items->search->count, $nb_of_items + 2, 'The 2 items should have been added' );
67 my $retrieved_item_1 = Koha::Items->find( $new_item_1->itemnumber );
68 is( $retrieved_item_1->barcode, $new_item_1->barcode, 'Find a item by id should return the correct item' );
70 subtest 'store' => sub {
73 my $biblio = $builder->build_sample_biblio;
74 my $today = dt_from_string->set( hour => 0, minute => 0, second => 0 );
75 my $item = Koha::Item->new(
77 homebranch => $library->{branchcode},
78 holdingbranch => $library->{branchcode},
79 biblionumber => $biblio->biblionumber,
82 )->store->get_from_storage;
84 is( t::lib::Dates::compare( $item->replacementpricedate, $today ),
85 0, 'replacementpricedate must have been set to today if not given' );
86 is( t::lib::Dates::compare( $item->datelastseen, $today ),
87 0, 'datelastseen must have been set to today if not given' );
90 $biblio->biblioitem->itemtype,
91 'items.itype must have been set to biblioitem.itemtype is not given'
95 subtest 'permanent_location' => sub {
98 subtest 'location passed to ->store' => sub {
101 my $location = 'my_loc';
103 homebranch => $library->{branchcode},
104 holdingbranch => $library->{branchcode},
105 biblionumber => $biblio->biblionumber,
106 location => $location,
110 # NewItemsDefaultLocation not set
111 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', '' );
113 # Not passing permanent_location on creating the item
114 my $item = Koha::Item->new($attributes)->store->get_from_storage;
115 is( $item->location, $location,
116 'location must have been set to location if given' );
117 is( $item->permanent_location, $item->location,
118 'permanent_location must have been set to location if not given' );
121 # Passing permanent_location on creating the item
122 $item = Koha::Item->new(
123 { %$attributes, permanent_location => 'perm_loc' } )
124 ->store->get_from_storage;
125 is( $item->permanent_location, 'perm_loc',
126 'permanent_location must have been kept if given' );
131 # NewItemsDefaultLocation set
132 my $default_location = 'default_location';
133 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', $default_location );
135 # Not passing permanent_location on creating the item
136 my $item = Koha::Item->new($attributes)->store->get_from_storage;
137 is( $item->location, $location,
138 'location must have been kept if given' );
139 is( $item->permanent_location, $location,
140 'permanent_location must have been set to the location given' );
143 # Passing permanent_location on creating the item
144 $item = Koha::Item->new(
145 { %$attributes, permanent_location => 'perm_loc' } )
146 ->store->get_from_storage;
147 is( $item->location, $location,
148 'location must have been kept if given' );
149 is( $item->permanent_location, 'perm_loc',
150 'permanent_location must have been kept if given' );
155 subtest 'location NOT passed to ->store' => sub {
159 homebranch => $library->{branchcode},
160 holdingbranch => $library->{branchcode},
161 biblionumber => $biblio->biblionumber,
165 # NewItemsDefaultLocation not set
166 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', '' );
168 # Not passing permanent_location on creating the item
169 my $item = Koha::Item->new($attributes)->store->get_from_storage;
170 is( $item->location, undef,
171 'location not passed and no default, it is undef' );
172 is( $item->permanent_location, $item->location,
173 'permanent_location must have been set to location if not given' );
176 # Passing permanent_location on creating the item
177 $item = Koha::Item->new(
178 { %$attributes, permanent_location => 'perm_loc' } )
179 ->store->get_from_storage;
180 is( $item->permanent_location, 'perm_loc',
181 'permanent_location must have been kept if given' );
186 # NewItemsDefaultLocation set
187 my $default_location = 'default_location';
188 t::lib::Mocks::mock_preference( 'NewItemsDefaultLocation', $default_location );
190 # Not passing permanent_location on creating the item
191 my $item = Koha::Item->new($attributes)->store->get_from_storage;
192 is( $item->location, $default_location,
193 'location must have been set to default location if not given' );
194 is( $item->permanent_location, $default_location,
195 'permanent_location must have been set to the default location as well' );
198 # Passing permanent_location on creating the item
199 $item = Koha::Item->new(
200 { %$attributes, permanent_location => 'perm_loc' } )
201 ->store->get_from_storage;
202 is( $item->location, $default_location,
203 'location must have been set to default location if not given' );
204 is( $item->permanent_location, 'perm_loc',
205 'permanent_location must have been kept if given' );
212 subtest '*_on updates' => sub {
215 # Once the '_on' value is set (triggered by the related field turning from false to true)
216 # it should not be re-set for any changes outside of the related field being 'unset'.
218 my @fields = qw( itemlost withdrawn damaged );
219 my $today = dt_from_string();
220 my $yesterday = $today->clone()->subtract( days => 1 );
222 for my $field ( @fields ) {
223 my $item = $builder->build_sample_item(
226 itemlost_on => undef,
228 withdrawn_on => undef,
233 my $field_on = $field . '_on';
235 # Set field for the first time
236 Time::Fake->offset( $yesterday->epoch );
237 $item->$field(1)->store;
238 $item->get_from_storage;
239 is( t::lib::Dates::compare( $item->$field_on, $yesterday ),
240 0, $field_on . " was set upon first truthy setting" );
242 # Update the field to a new 'true' value
243 Time::Fake->offset( $today->epoch );
244 $item->$field(2)->store;
245 $item->get_from_storage;
246 is( t::lib::Dates::compare( $item->$field_on, $yesterday ),
247 0, $field_on . " was not updated upon second truthy setting" );
249 # Update the field to a new 'false' value
250 $item->$field(0)->store;
251 $item->get_from_storage;
252 is($item->$field_on, undef, $field_on . " was unset upon untruthy setting");
258 subtest '_lost_found_trigger' => sub {
261 t::lib::Mocks::mock_preference( 'WhenLostChargeReplacementFee', 1 );
262 t::lib::Mocks::mock_preference( 'WhenLostForgiveFine', 0 );
264 my $processfee_amount = 20;
265 my $replacement_amount = 99.00;
266 my $item_type = $builder->build_object(
268 class => 'Koha::ItemTypes',
272 defaultreplacecost => undef,
273 processfee => $processfee_amount,
274 rentalcharge_daily => 0,
278 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
280 $biblio = $builder->build_sample_biblio( { author => 'Hall, Daria' } );
282 subtest 'Full write-off tests' => sub {
286 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
288 $builder->build_object( { class => "Koha::Patrons" } );
289 t::lib::Mocks::mock_userenv(
290 { patron => $manager, branchcode => $manager->branchcode } );
292 my $item = $builder->build_sample_item(
294 biblionumber => $biblio->biblionumber,
295 library => $library->branchcode,
296 replacementprice => $replacement_amount,
297 itype => $item_type->itemtype,
301 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
303 # Simulate item marked as lost
304 $item->itemlost(3)->store;
305 C4::Circulation::LostItem( $item->itemnumber, 1 );
307 my $processing_fee_lines = Koha::Account::Lines->search(
309 borrowernumber => $patron->id,
310 itemnumber => $item->itemnumber,
311 debit_type_code => 'PROCESSING'
314 is( $processing_fee_lines->count,
315 1, 'Only one processing fee produced' );
316 my $processing_fee_line = $processing_fee_lines->next;
317 is( $processing_fee_line->amount + 0,
319 'The right PROCESSING amount is generated' );
320 is( $processing_fee_line->amountoutstanding + 0,
322 'The right PROCESSING amountoutstanding is generated' );
324 my $lost_fee_lines = Koha::Account::Lines->search(
326 borrowernumber => $patron->id,
327 itemnumber => $item->itemnumber,
328 debit_type_code => 'LOST'
331 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
332 my $lost_fee_line = $lost_fee_lines->next;
333 is( $lost_fee_line->amount + 0,
334 $replacement_amount, 'The right LOST amount is generated' );
335 is( $lost_fee_line->amountoutstanding + 0,
337 'The right LOST amountoutstanding is generated' );
338 is( $lost_fee_line->status, undef, 'The LOST status was not set' );
340 my $account = $patron->account;
341 my $debts = $account->outstanding_debits;
344 my $credit = $account->add_credit(
346 amount => $account->balance,
351 $credit->apply( { debits => [ $debts->as_list ] } );
353 # Simulate item marked as found
354 $item->itemlost(0)->store;
355 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 0, 'No LOST_FOUND account line added' );
357 $lost_fee_line->discard_changes; # reload from DB
358 is( $lost_fee_line->amountoutstanding + 0,
359 0, 'Lost fee has no outstanding amount' );
360 is( $lost_fee_line->debit_type_code,
361 'LOST', 'Lost fee now still has account type of LOST' );
362 is( $lost_fee_line->status, 'FOUND',
363 "Lost fee now has account status of FOUND - No Refund" );
365 is( $patron->account->balance,
366 -0, 'The patron balance is 0, everything was written off' );
369 subtest 'Full payment tests' => sub {
373 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
375 my $item = $builder->build_sample_item(
377 biblionumber => $biblio->biblionumber,
378 library => $library->branchcode,
379 replacementprice => $replacement_amount,
380 itype => $item_type->itemtype
385 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
387 # Simulate item marked as lost
388 $item->itemlost(1)->store;
389 C4::Circulation::LostItem( $item->itemnumber, 1 );
391 my $processing_fee_lines = Koha::Account::Lines->search(
393 borrowernumber => $patron->id,
394 itemnumber => $item->itemnumber,
395 debit_type_code => 'PROCESSING'
398 is( $processing_fee_lines->count,
399 1, 'Only one processing fee produced' );
400 my $processing_fee_line = $processing_fee_lines->next;
401 is( $processing_fee_line->amount + 0,
403 'The right PROCESSING amount is generated' );
404 is( $processing_fee_line->amountoutstanding + 0,
406 'The right PROCESSING amountoutstanding is generated' );
408 my $lost_fee_lines = Koha::Account::Lines->search(
410 borrowernumber => $patron->id,
411 itemnumber => $item->itemnumber,
412 debit_type_code => 'LOST'
415 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
416 my $lost_fee_line = $lost_fee_lines->next;
417 is( $lost_fee_line->amount + 0,
418 $replacement_amount, 'The right LOST amount is generated' );
419 is( $lost_fee_line->amountoutstanding + 0,
421 'The right LOST amountountstanding is generated' );
423 my $account = $patron->account;
424 my $debts = $account->outstanding_debits;
427 my $credit = $account->add_credit(
429 amount => $account->balance,
434 $credit->apply( { debits => [ $debts->as_list ] } );
436 # Simulate item marked as found
437 $item->itemlost(0)->store;
438 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
440 my $credit_return = Koha::Account::Lines->search(
442 itemnumber => $item->itemnumber,
443 credit_type_code => 'LOST_FOUND'
448 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
449 is( $credit_return->amount + 0,
451 'The account line of type LOST_FOUND has an amount of -99' );
453 $credit_return->amountoutstanding + 0,
455 'The account line of type LOST_FOUND has an amountoutstanding of -99'
458 $lost_fee_line->discard_changes;
459 is( $lost_fee_line->amountoutstanding + 0,
460 0, 'Lost fee has no outstanding amount' );
461 is( $lost_fee_line->debit_type_code,
462 'LOST', 'Lost fee now still has account type of LOST' );
463 is( $lost_fee_line->status, 'FOUND',
464 "Lost fee now has account status of FOUND" );
466 is( $patron->account->balance, -99,
467 'The patron balance is -99, a credit that equals the lost fee payment'
471 subtest 'Test without payment or write off' => sub {
475 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
477 my $item = $builder->build_sample_item(
479 biblionumber => $biblio->biblionumber,
480 library => $library->branchcode,
481 replacementprice => 23.00,
482 replacementprice => $replacement_amount,
483 itype => $item_type->itemtype
488 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
490 # Simulate item marked as lost
491 $item->itemlost(3)->store;
492 C4::Circulation::LostItem( $item->itemnumber, 1 );
494 my $processing_fee_lines = Koha::Account::Lines->search(
496 borrowernumber => $patron->id,
497 itemnumber => $item->itemnumber,
498 debit_type_code => 'PROCESSING'
501 is( $processing_fee_lines->count,
502 1, 'Only one processing fee produced' );
503 my $processing_fee_line = $processing_fee_lines->next;
504 is( $processing_fee_line->amount + 0,
506 'The right PROCESSING amount is generated' );
507 is( $processing_fee_line->amountoutstanding + 0,
509 'The right PROCESSING amountoutstanding is generated' );
511 my $lost_fee_lines = Koha::Account::Lines->search(
513 borrowernumber => $patron->id,
514 itemnumber => $item->itemnumber,
515 debit_type_code => 'LOST'
518 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
519 my $lost_fee_line = $lost_fee_lines->next;
520 is( $lost_fee_line->amount + 0,
521 $replacement_amount, 'The right LOST amount is generated' );
522 is( $lost_fee_line->amountoutstanding + 0,
524 'The right LOST amountountstanding is generated' );
526 # Simulate item marked as found
527 $item->itemlost(0)->store;
528 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
530 my $credit_return = Koha::Account::Lines->search(
532 itemnumber => $item->itemnumber,
533 credit_type_code => 'LOST_FOUND'
538 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
539 is( $credit_return->amount + 0,
541 'The account line of type LOST_FOUND has an amount of -99' );
543 $credit_return->amountoutstanding + 0,
545 'The account line of type LOST_FOUND has an amountoutstanding of 0'
548 $lost_fee_line->discard_changes;
549 is( $lost_fee_line->amountoutstanding + 0,
550 0, 'Lost fee has no outstanding amount' );
551 is( $lost_fee_line->debit_type_code,
552 'LOST', 'Lost fee now still has account type of LOST' );
553 is( $lost_fee_line->status, 'FOUND',
554 "Lost fee now has account status of FOUND" );
556 is( $patron->account->balance,
557 20, 'The patron balance is 20, still owes the processing fee' );
561 'Test with partial payment and write off, and remaining debt' =>
566 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 0 );
568 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
569 my $item = $builder->build_sample_item(
571 biblionumber => $biblio->biblionumber,
572 library => $library->branchcode,
573 replacementprice => $replacement_amount,
574 itype => $item_type->itemtype
579 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
581 # Simulate item marked as lost
582 $item->itemlost(1)->store;
583 C4::Circulation::LostItem( $item->itemnumber, 1 );
585 my $processing_fee_lines = Koha::Account::Lines->search(
587 borrowernumber => $patron->id,
588 itemnumber => $item->itemnumber,
589 debit_type_code => 'PROCESSING'
592 is( $processing_fee_lines->count,
593 1, 'Only one processing fee produced' );
594 my $processing_fee_line = $processing_fee_lines->next;
595 is( $processing_fee_line->amount + 0,
597 'The right PROCESSING amount is generated' );
598 is( $processing_fee_line->amountoutstanding + 0,
600 'The right PROCESSING amountoutstanding is generated' );
602 my $lost_fee_lines = Koha::Account::Lines->search(
604 borrowernumber => $patron->id,
605 itemnumber => $item->itemnumber,
606 debit_type_code => 'LOST'
609 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
610 my $lost_fee_line = $lost_fee_lines->next;
611 is( $lost_fee_line->amount + 0,
612 $replacement_amount, 'The right LOST amount is generated' );
613 is( $lost_fee_line->amountoutstanding + 0,
615 'The right LOST amountountstanding is generated' );
617 my $account = $patron->account;
620 $processfee_amount + $replacement_amount,
621 'Balance is PROCESSING + LOST'
624 # Partially pay fee (99 - 27 = 72)
625 my $payment_amount = 24;
626 my $payment = $account->add_credit(
628 amount => $payment_amount,
634 $payment->apply( { debits => [$lost_fee_line] } );
636 # Partially write off fee (72 - 20 = 52)
637 my $write_off_amount = 20;
638 my $write_off = $account->add_credit(
640 amount => $write_off_amount,
645 $write_off->apply( { debits => [$lost_fee_line] } );
648 my $payment_amount_2 = 3;
649 my $payment_2 = $account->add_credit(
651 amount => $payment_amount_2,
658 { debits => [$lost_fee_line] } );
660 # Partially write off fee (52 - 5 = 47)
661 my $write_off_amount_2 = 5;
662 my $write_off_2 = $account->add_credit(
664 amount => $write_off_amount_2,
671 { debits => [$lost_fee_line] } );
676 $replacement_amount -
681 'Balance is PROCESSING + LOST - PAYMENT 1 - WRITEOFF - PAYMENT 2 - WRITEOFF 2'
684 # VOID payment_2 and writeoff_2
685 $payment_2->void({ interface => 'test' });
686 $write_off_2->void({ interface => 'test' });
691 $replacement_amount -
694 'Balance is PROCESSING + LOST - PAYMENT 1 - WRITEOFF (PAYMENT 2 and WRITEOFF 2 VOIDED)'
697 # Store the amountoutstanding value
698 $lost_fee_line->discard_changes;
699 my $outstanding = $lost_fee_line->amountoutstanding;
702 $replacement_amount - $payment_amount - $write_off_amount,
703 "Lost Fee Outstanding is LOST - PAYMENT 1 - WRITEOFF"
706 # Simulate item marked as found
707 $item->itemlost(0)->store;
708 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
710 my $credit_return = Koha::Account::Lines->search(
712 itemnumber => $item->itemnumber,
713 credit_type_code => 'LOST_FOUND'
718 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
722 $processfee_amount - $payment_amount,
723 'Balance is PROCESSING - PAYMENT (LOST_FOUND)'
726 $lost_fee_line->discard_changes;
727 is( $lost_fee_line->amountoutstanding + 0,
728 0, 'Lost fee has no outstanding amount' );
729 is( $lost_fee_line->debit_type_code,
730 'LOST', 'Lost fee now still has account type of LOST' );
731 is( $lost_fee_line->status, 'FOUND',
732 "Lost fee now has account status of FOUND" );
735 $credit_return->amount + 0,
736 ( $payment_amount + $outstanding ) * -1,
737 'The account line of type LOST_FOUND has an amount equal to the payment 1 + outstanding'
740 $credit_return->amountoutstanding + 0,
741 $payment_amount * -1,
742 'The account line of type LOST_FOUND has an amountoutstanding equal to the payment'
747 $processfee_amount - $payment_amount,
748 'The patron balance is the difference between the PROCESSING and the credit'
752 subtest 'Partial payment, existing debits and AccountAutoReconcile' =>
757 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
758 my $barcode = 'KD123456793';
759 my $replacement_amount = 100;
760 my $processfee_amount = 20;
762 my $item_type = $builder->build_object(
764 class => 'Koha::ItemTypes',
768 defaultreplacecost => undef,
770 rentalcharge_daily => 0,
774 my $item = Koha::Item->new(
776 biblionumber => $biblio->biblionumber,
777 homebranch => $library->branchcode,
778 holdingbranch => $library->branchcode,
780 replacementprice => $replacement_amount,
781 itype => $item_type->itemtype
786 C4::Circulation::AddIssue( $patron->unblessed, $barcode );
788 # Simulate item marked as lost
789 $item->itemlost(1)->store;
790 C4::Circulation::LostItem( $item->itemnumber, 1 );
792 my $lost_fee_lines = Koha::Account::Lines->search(
794 borrowernumber => $patron->id,
795 itemnumber => $item->itemnumber,
796 debit_type_code => 'LOST'
799 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
800 my $lost_fee_line = $lost_fee_lines->next;
801 is( $lost_fee_line->amount + 0,
802 $replacement_amount, 'The right LOST amount is generated' );
803 is( $lost_fee_line->amountoutstanding + 0,
805 'The right LOST amountountstanding is generated' );
807 my $account = $patron->account;
808 is( $account->balance, $replacement_amount, 'Balance is L' );
811 my $payment_amount = 27;
812 my $payment = $account->add_credit(
814 amount => $payment_amount,
819 $payment->apply( { debits => [$lost_fee_line] } );
823 $replacement_amount - $payment_amount,
827 my $manual_debit_amount = 80;
830 amount => $manual_debit_amount,
838 $manual_debit_amount + $replacement_amount - $payment_amount,
839 'Manual debit applied'
842 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
844 # Simulate item marked as found
845 $item->itemlost(0)->store;
846 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
848 my $credit_return = Koha::Account::Lines->search(
850 itemnumber => $item->itemnumber,
851 credit_type_code => 'LOST_FOUND'
856 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
860 $manual_debit_amount - $payment_amount,
861 'Balance is PROCESSING - payment (LOST_FOUND)'
864 my $manual_debit = Koha::Account::Lines->search(
866 borrowernumber => $patron->id,
867 debit_type_code => 'OVERDUE',
868 status => 'UNRETURNED'
872 $manual_debit->amountoutstanding + 0,
873 $manual_debit_amount - $payment_amount,
874 'reconcile_balance was called'
878 subtest 'Patron deleted' => sub {
881 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
882 my $barcode = 'KD123456794';
883 my $replacement_amount = 100;
884 my $processfee_amount = 20;
886 my $item_type = $builder->build_object(
888 class => 'Koha::ItemTypes',
892 defaultreplacecost => undef,
894 rentalcharge_daily => 0,
898 my $item = Koha::Item->new(
900 biblionumber => $biblio->biblionumber,
901 homebranch => $library->branchcode,
902 holdingbranch => $library->branchcode,
904 replacementprice => $replacement_amount,
905 itype => $item_type->itemtype
910 C4::Circulation::AddIssue( $patron->unblessed, $barcode );
912 # Simulate item marked as lost
913 $item->itemlost(1)->store;
914 C4::Circulation::LostItem( $item->itemnumber, 1 );
919 # Simulate item marked as found
920 $item->itemlost(0)->store;
921 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 0, 'No refund triggered' );
925 subtest 'restore fine | no overdue' => sub {
930 $builder->build_object( { class => "Koha::Patrons" } );
931 t::lib::Mocks::mock_userenv(
932 { patron => $manager, branchcode => $manager->branchcode } );
934 # Set lostreturn_policy to 'restore' for tests
935 my $specific_rule_restore = $builder->build(
937 source => 'CirculationRule',
939 branchcode => $manager->branchcode,
940 categorycode => undef,
942 rule_name => 'lostreturn',
943 rule_value => 'restore'
948 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
950 my $item = $builder->build_sample_item(
952 biblionumber => $biblio->biblionumber,
953 library => $library->branchcode,
954 replacementprice => $replacement_amount,
955 itype => $item_type->itemtype
960 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
962 # Simulate item marked as lost
963 $item->itemlost(1)->store;
964 C4::Circulation::LostItem( $item->itemnumber, 1 );
966 my $processing_fee_lines = Koha::Account::Lines->search(
968 borrowernumber => $patron->id,
969 itemnumber => $item->itemnumber,
970 debit_type_code => 'PROCESSING'
973 is( $processing_fee_lines->count,
974 1, 'Only one processing fee produced' );
975 my $processing_fee_line = $processing_fee_lines->next;
976 is( $processing_fee_line->amount + 0,
978 'The right PROCESSING amount is generated' );
979 is( $processing_fee_line->amountoutstanding + 0,
981 'The right PROCESSING amountoutstanding is generated' );
983 my $lost_fee_lines = Koha::Account::Lines->search(
985 borrowernumber => $patron->id,
986 itemnumber => $item->itemnumber,
987 debit_type_code => 'LOST'
990 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
991 my $lost_fee_line = $lost_fee_lines->next;
992 is( $lost_fee_line->amount + 0,
993 $replacement_amount, 'The right LOST amount is generated' );
994 is( $lost_fee_line->amountoutstanding + 0,
996 'The right LOST amountountstanding is generated' );
998 my $account = $patron->account;
999 my $debts = $account->outstanding_debits;
1002 my $credit = $account->add_credit(
1004 amount => $account->balance,
1006 interface => 'test',
1009 $credit->apply( { debits => [ $debts->as_list ] } );
1011 # Simulate item marked as found
1012 $item->itemlost(0)->store;
1013 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1014 is( scalar ( grep { $_->message eq 'lost_restored' } @{$item->object_messages} ), 0, 'Restore not triggered when there is no overdue fine found' );
1017 subtest 'restore fine | unforgiven overdue' => sub {
1021 # Set lostreturn_policy to 'restore' for tests
1023 $builder->build_object( { class => "Koha::Patrons" } );
1024 t::lib::Mocks::mock_userenv(
1025 { patron => $manager, branchcode => $manager->branchcode } );
1026 my $specific_rule_restore = $builder->build(
1028 source => 'CirculationRule',
1030 branchcode => $manager->branchcode,
1031 categorycode => undef,
1033 rule_name => 'lostreturn',
1034 rule_value => 'restore'
1039 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1041 my $item = $builder->build_sample_item(
1043 biblionumber => $biblio->biblionumber,
1044 library => $library->branchcode,
1045 replacementprice => $replacement_amount,
1046 itype => $item_type->itemtype
1051 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
1053 # Simulate item marked as lost
1054 $item->itemlost(1)->store;
1055 C4::Circulation::LostItem( $item->itemnumber, 1 );
1057 my $processing_fee_lines = Koha::Account::Lines->search(
1059 borrowernumber => $patron->id,
1060 itemnumber => $item->itemnumber,
1061 debit_type_code => 'PROCESSING'
1064 is( $processing_fee_lines->count,
1065 1, 'Only one processing fee produced' );
1066 my $processing_fee_line = $processing_fee_lines->next;
1067 is( $processing_fee_line->amount + 0,
1069 'The right PROCESSING amount is generated' );
1070 is( $processing_fee_line->amountoutstanding + 0,
1072 'The right PROCESSING amountoutstanding is generated' );
1074 my $lost_fee_lines = Koha::Account::Lines->search(
1076 borrowernumber => $patron->id,
1077 itemnumber => $item->itemnumber,
1078 debit_type_code => 'LOST'
1081 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1082 my $lost_fee_line = $lost_fee_lines->next;
1083 is( $lost_fee_line->amount + 0,
1084 $replacement_amount, 'The right LOST amount is generated' );
1085 is( $lost_fee_line->amountoutstanding + 0,
1086 $replacement_amount,
1087 'The right LOST amountountstanding is generated' );
1089 my $account = $patron->account;
1090 my $debts = $account->outstanding_debits;
1093 my $credit = $account->add_credit(
1095 amount => $account->balance,
1097 interface => 'test',
1100 $credit->apply( { debits => [ $debts->as_list ] } );
1103 my $overdue = $account->add_debit(
1106 user_id => $manager->borrowernumber,
1107 library_id => $library->branchcode,
1108 interface => 'test',
1109 item_id => $item->itemnumber,
1113 $overdue->status('LOST')->store();
1114 $overdue->discard_changes;
1115 is( $overdue->status, 'LOST',
1116 'Overdue status set to LOST' );
1118 # Simulate item marked as found
1119 $item->itemlost(0)->store;
1120 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1121 is( scalar ( grep { $_->message eq 'lost_restored' } @{$item->object_messages} ), 0, 'Restore not triggered when overdue was not forgiven' );
1122 $overdue->discard_changes;
1123 is( $overdue->status, 'FOUND',
1124 'Overdue status updated to FOUND' );
1127 subtest 'restore fine | forgiven overdue' => sub {
1131 # Set lostreturn_policy to 'restore' for tests
1133 $builder->build_object( { class => "Koha::Patrons" } );
1134 t::lib::Mocks::mock_userenv(
1135 { patron => $manager, branchcode => $manager->branchcode } );
1136 my $specific_rule_restore = $builder->build(
1138 source => 'CirculationRule',
1140 branchcode => $manager->branchcode,
1141 categorycode => undef,
1143 rule_name => 'lostreturn',
1144 rule_value => 'restore'
1149 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1151 my $item = $builder->build_sample_item(
1153 biblionumber => $biblio->biblionumber,
1154 library => $library->branchcode,
1155 replacementprice => $replacement_amount,
1156 itype => $item_type->itemtype
1161 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
1163 # Simulate item marked as lost
1164 $item->itemlost(1)->store;
1165 C4::Circulation::LostItem( $item->itemnumber, 1 );
1167 my $processing_fee_lines = Koha::Account::Lines->search(
1169 borrowernumber => $patron->id,
1170 itemnumber => $item->itemnumber,
1171 debit_type_code => 'PROCESSING'
1174 is( $processing_fee_lines->count,
1175 1, 'Only one processing fee produced' );
1176 my $processing_fee_line = $processing_fee_lines->next;
1177 is( $processing_fee_line->amount + 0,
1179 'The right PROCESSING amount is generated' );
1180 is( $processing_fee_line->amountoutstanding + 0,
1182 'The right PROCESSING amountoutstanding is generated' );
1184 my $lost_fee_lines = Koha::Account::Lines->search(
1186 borrowernumber => $patron->id,
1187 itemnumber => $item->itemnumber,
1188 debit_type_code => 'LOST'
1191 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1192 my $lost_fee_line = $lost_fee_lines->next;
1193 is( $lost_fee_line->amount + 0,
1194 $replacement_amount, 'The right LOST amount is generated' );
1195 is( $lost_fee_line->amountoutstanding + 0,
1196 $replacement_amount,
1197 'The right LOST amountountstanding is generated' );
1199 my $account = $patron->account;
1200 my $debts = $account->outstanding_debits;
1203 my $credit = $account->add_credit(
1205 amount => $account->balance,
1207 interface => 'test',
1210 $credit->apply( { debits => [ $debts->as_list ] } );
1213 my $overdue = $account->add_debit(
1216 user_id => $manager->borrowernumber,
1217 library_id => $library->branchcode,
1218 interface => 'test',
1219 item_id => $item->itemnumber,
1223 $overdue->status('LOST')->store();
1224 is( $overdue->status, 'LOST',
1225 'Overdue status set to LOST' );
1227 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 0 );
1230 $credit = $account->add_credit(
1233 user_id => $manager->borrowernumber,
1234 library_id => $library->branchcode,
1235 interface => 'test',
1237 item_id => $item->itemnumber
1240 $credit->apply( { debits => [$overdue] } );
1242 # Simulate item marked as found
1243 $item->itemlost(0)->store;
1244 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1245 is( scalar ( grep { $_->message eq 'lost_restored' } @{$item->object_messages} ), 1, 'Restore triggered when overdue was forgiven' );
1246 $overdue->discard_changes;
1247 is( $overdue->status, 'FOUND', 'Overdue status updated to FOUND' );
1248 is( $overdue->amountoutstanding, $overdue->amount, 'Overdue outstanding has been restored' );
1249 $credit->discard_changes;
1250 is( $credit->status, 'VOID', 'Overdue Forgival has been marked as VOID');
1253 subtest 'Continue when userenv is not set' => sub {
1256 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1257 my $barcode = 'KD123456795';
1258 my $replacement_amount = 100;
1259 my $processfee_amount = 20;
1261 my $item_type = $builder->build_object(
1263 class => 'Koha::ItemTypes',
1265 notforloan => undef,
1267 defaultreplacecost => undef,
1269 rentalcharge_daily => 0,
1273 my $item = $builder->build_sample_item(
1275 biblionumber => $biblio->biblionumber,
1276 homebranch => $library->branchcode,
1277 holdingbranch => $library->branchcode,
1278 barcode => $barcode,
1279 replacementprice => $replacement_amount,
1280 itype => $item_type->itemtype
1285 C4::Circulation::AddIssue( $patron->unblessed, $barcode );
1287 # Simulate item marked as lost
1288 $item->itemlost(1)->store;
1289 C4::Circulation::LostItem( $item->itemnumber, 1 );
1292 C4::Context->_new_userenv(undef);
1294 # Simluate item marked as found
1295 $item->itemlost(0)->store;
1296 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1301 subtest 'log_action' => sub {
1303 t::lib::Mocks::mock_preference( 'CataloguingLog', 1 );
1305 my $item = Koha::Item->new(
1307 homebranch => $library->{branchcode},
1308 holdingbranch => $library->{branchcode},
1309 biblionumber => $biblio->biblionumber,
1310 location => 'my_loc',
1314 Koha::ActionLogs->search(
1316 module => 'CATALOGUING',
1318 object => $item->itemnumber,
1323 "Item creation logged"
1326 $item->location('another_loc')->store;
1328 Koha::ActionLogs->search(
1330 module => 'CATALOGUING',
1332 object => $item->itemnumber
1336 "Item modification logged"
1341 subtest 'get_transfer' => sub {
1344 my $transfer = $new_item_1->get_transfer();
1345 is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
1347 my $library_to = $builder->build( { source => 'Branch' } );
1349 my $transfer_1 = $builder->build_object(
1351 class => 'Koha::Item::Transfers',
1353 itemnumber => $new_item_1->itemnumber,
1354 frombranch => $new_item_1->holdingbranch,
1355 tobranch => $library_to->{branchcode},
1358 datearrived => undef,
1359 datecancelled => undef,
1360 daterequested => \'NOW()'
1365 $transfer = $new_item_1->get_transfer();
1366 is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' );
1368 my $transfer_2 = $builder->build_object(
1370 class => 'Koha::Item::Transfers',
1372 itemnumber => $new_item_1->itemnumber,
1373 frombranch => $new_item_1->holdingbranch,
1374 tobranch => $library_to->{branchcode},
1377 datearrived => undef,
1378 datecancelled => undef,
1379 daterequested => \'NOW()'
1384 $transfer = $new_item_1->get_transfer();
1385 is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the oldest transfer request');
1387 $transfer_2->datesent(\'NOW()')->store;
1388 $transfer = $new_item_1->get_transfer();
1389 is( $transfer->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfer returns the in_transit transfer');
1391 my $transfer_3 = $builder->build_object(
1393 class => 'Koha::Item::Transfers',
1395 itemnumber => $new_item_1->itemnumber,
1396 frombranch => $new_item_1->holdingbranch,
1397 tobranch => $library_to->{branchcode},
1400 datearrived => undef,
1401 datecancelled => undef,
1402 daterequested => \'NOW()'
1407 $transfer_2->datearrived(\'NOW()')->store;
1408 $transfer = $new_item_1->get_transfer();
1409 is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the next queued transfer');
1410 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' );
1412 $transfer_1->datecancelled(\'NOW()')->store;
1413 $transfer = $new_item_1->get_transfer();
1414 is( $transfer->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfer ignores cancelled transfers');
1417 subtest 'holds' => sub {
1420 my $biblio = $builder->build_sample_biblio();
1421 my $item = $builder->build_sample_item({
1422 biblionumber => $biblio->biblionumber,
1424 is($item->holds->count, 0, "Nothing returned if no holds");
1425 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
1426 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
1427 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
1429 is($item->holds()->count,3,"Three holds found");
1430 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
1431 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
1432 is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
1435 subtest 'biblio' => sub {
1438 my $biblio = $retrieved_item_1->biblio;
1439 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
1440 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
1443 subtest 'biblioitem' => sub {
1446 my $biblioitem = $retrieved_item_1->biblioitem;
1447 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
1448 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
1452 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
1453 subtest 'checkout' => sub {
1455 my $item = Koha::Items->find( $new_item_1->itemnumber );
1457 my $checkout = $item->checkout;
1458 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
1461 my $patron = $builder->build({ source => 'Borrower' });
1462 C4::Circulation::AddIssue( $patron, $item->barcode );
1463 $checkout = $retrieved_item_1->checkout;
1464 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
1465 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
1466 is( $checkout->borrowernumber, $patron->{borrowernumber}, 'Koha::Item->checkout should return the correct checkout' );
1469 C4::Circulation::AddReturn( $item->barcode );
1471 # There is no more checkout on this item, making sure it will not return old checkouts
1472 $checkout = $item->checkout;
1473 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
1476 subtest 'can_be_transferred' => sub {
1479 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
1480 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
1482 my $biblio = $builder->build_sample_biblio();
1483 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1484 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1485 my $item = $builder->build_sample_item({
1486 biblionumber => $biblio->biblionumber,
1487 homebranch => $library1->branchcode,
1488 holdingbranch => $library1->branchcode,
1491 is(Koha::Item::Transfer::Limits->search({
1492 fromBranch => $library1->branchcode,
1493 toBranch => $library2->branchcode,
1494 })->count, 0, 'There are no transfer limits between libraries.');
1495 ok($item->can_be_transferred({ to => $library2 }),
1496 'Item can be transferred between libraries.');
1498 my $limit = Koha::Item::Transfer::Limit->new({
1499 fromBranch => $library1->branchcode,
1500 toBranch => $library2->branchcode,
1501 itemtype => $item->effective_itemtype,
1503 is(Koha::Item::Transfer::Limits->search({
1504 fromBranch => $library1->branchcode,
1505 toBranch => $library2->branchcode,
1506 })->count, 1, 'Given we have added a transfer limit,');
1507 is($item->can_be_transferred({ to => $library2 }), 0,
1508 'Item can no longer be transferred between libraries.');
1509 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
1510 'We get the same result also if we pass the from-library parameter.');
1513 subtest 'filter_by_for_hold' => sub {
1516 my $biblio = $builder->build_sample_biblio;
1517 is( $biblio->items->filter_by_for_hold->count, 0, 'no item yet' );
1518 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } );
1519 is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' );
1520 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } );
1521 is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' );
1522 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
1523 is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' );
1525 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 0 } );
1526 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 1 } );
1527 is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' );
1529 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 0 } );
1530 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 1 } );
1531 is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' );
1533 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } );
1534 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } );
1535 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0);
1536 is( $biblio->items->filter_by_for_hold->count, 5, '5 items for hold - not damaged if not AllowHoldsOnDamagedItems' );
1537 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1);
1538 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' );
1540 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1541 my $not_holdable_itemtype = $itemtype->itemtype;
1542 $builder->build_sample_item(
1544 biblionumber => $biblio->biblionumber,
1545 itype => $not_holdable_itemtype,
1548 Koha::CirculationRules->set_rule(
1550 branchcode => undef,
1551 itemtype => $not_holdable_itemtype,
1552 rule_name => 'holdallowed',
1553 rule_value => 'not_allowed',
1556 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - holdallowed=not_allowed' );
1558 # Remove rule, test notforloan on itemtype
1559 Koha::CirculationRules->set_rule(
1561 branchcode => undef,
1562 itemtype => $not_holdable_itemtype,
1563 rule_name => 'holdallowed',
1564 rule_value => undef,
1567 is( $biblio->items->filter_by_for_hold->count, 7, '7 items for hold - rule deleted' );
1568 $itemtype->notforloan(1)->store;
1569 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - notforloan' );
1571 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1572 $biblio->biblioitem->itemtype($not_holdable_itemtype)->store;
1573 is( $biblio->items->filter_by_for_hold->count, 0, '0 item-level_itypes=0' );
1575 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1580 # Reset nb_of_items prior to testing delete
1581 $nb_of_items = Koha::Items->search->count;
1584 $retrieved_item_1->delete;
1585 is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
1587 $schema->storage->txn_rollback;
1589 subtest 'filter_by_visible_in_opac() tests' => sub {
1593 $schema->storage->txn_begin;
1595 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1596 my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
1598 $mocked_category->mock( 'override_hidden_items', sub {
1602 # have a fresh biblio
1603 my $biblio = $builder->build_sample_biblio;
1604 # have two itemtypes
1605 my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
1606 my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
1607 # have 5 items on that biblio
1608 my $item_1 = $builder->build_sample_item(
1610 biblionumber => $biblio->biblionumber,
1612 itype => $itype_1->itemtype,
1617 my $item_2 = $builder->build_sample_item(
1619 biblionumber => $biblio->biblionumber,
1621 itype => $itype_2->itemtype,
1626 my $item_3 = $builder->build_sample_item(
1628 biblionumber => $biblio->biblionumber,
1630 itype => $itype_1->itemtype,
1635 my $item_4 = $builder->build_sample_item(
1637 biblionumber => $biblio->biblionumber,
1639 itype => $itype_2->itemtype,
1644 my $item_5 = $builder->build_sample_item(
1646 biblionumber => $biblio->biblionumber,
1648 itype => $itype_1->itemtype,
1653 my $item_6 = $builder->build_sample_item(
1655 biblionumber => $biblio->biblionumber,
1657 itype => $itype_1->itemtype,
1665 my $mocked_context = Test::MockModule->new('C4::Context');
1666 $mocked_context->mock( 'yaml_preference', sub {
1670 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
1671 is( $biblio->items->filter_by_visible_in_opac->count,
1672 6, 'No rules passed, hidelostitems unset' );
1674 is( $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1675 6, 'No rules passed, hidelostitems unset, patron exception changes nothing' );
1677 $rules = { copynumber => [ 2 ] };
1679 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
1681 $biblio->items->filter_by_visible_in_opac->count,
1683 'No rules passed, hidelostitems set'
1687 $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1689 'No rules passed, hidelostitems set, patron exception changes nothing'
1692 $rules = { biblionumber => [ $biblio->biblionumber ] };
1694 $biblio->items->filter_by_visible_in_opac->count,
1696 'Biblionumber rule successfully hides all items'
1699 my $biblio2 = $builder->build_sample_biblio;
1700 $rules = { biblionumber => [ $biblio2->biblionumber ] };
1701 my $prefetched = $biblio->items->search({},{ prefetch => ['branchtransfers','reserves'] })->filter_by_visible_in_opac;
1702 ok( $prefetched->next, "Can retrieve object when prefetching and hiding on a duplicated column");
1704 $rules = { withdrawn => [ 1, 2 ], copynumber => [ 2 ] };
1706 $biblio->items->filter_by_visible_in_opac->count,
1708 'Rules on withdrawn, hidelostitems set'
1712 $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1714 'hidelostitems set, rules on withdrawn but patron override passed'
1717 $rules = { itype => [ $itype_1->itemtype ], copynumber => [ 2 ] };
1719 $biblio->items->filter_by_visible_in_opac->count,
1721 'Rules on itype, hidelostitems set'
1724 $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ], copynumber => [ 2 ] };
1726 $biblio->items->filter_by_visible_in_opac->count,
1728 'Rules on itype and withdrawn, hidelostitems set'
1731 $biblio->items->filter_by_visible_in_opac
1733 $item_4->itemnumber,
1734 'The right item is returned'
1737 $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ], copynumber => [ 2 ] };
1739 $biblio->items->filter_by_visible_in_opac->count,
1741 'Rules on itype and withdrawn, hidelostitems set'
1744 $biblio->items->filter_by_visible_in_opac
1746 $item_5->itemnumber,
1747 'The right item is returned'
1750 # Make sure the warning on the about page will work
1751 $rules = { itemlost => ['AB'] };
1752 my $c = Koha::Items->filter_by_visible_in_opac->count;
1753 my @warnings = C4::Context->dbh->selectrow_array('SHOW WARNINGS');
1754 is( $warnings[2], q{Truncated incorrect DOUBLE value: 'AB'});
1756 $schema->storage->txn_rollback;
1759 subtest 'filter_out_lost() tests' => sub {
1763 $schema->storage->txn_begin;
1765 # have a fresh biblio
1766 my $biblio = $builder->build_sample_biblio;
1767 # have 3 items on that biblio
1768 my $item_1 = $builder->build_sample_item(
1770 biblionumber => $biblio->biblionumber,
1774 my $item_2 = $builder->build_sample_item(
1776 biblionumber => $biblio->biblionumber,
1780 my $item_3 = $builder->build_sample_item(
1782 biblionumber => $biblio->biblionumber,
1787 is( $biblio->items->filter_out_lost->next->itemnumber, $item_2->itemnumber, 'Right item returned' );
1788 is( $biblio->items->filter_out_lost->count, 1, 'Only one item is not lost' );
1790 $schema->storage->txn_rollback;
1793 subtest 'move_to_biblio() tests' => sub {
1797 $schema->storage->txn_begin;
1799 my $biblio1 = $builder->build_sample_biblio;
1800 my $biblio2 = $builder->build_sample_biblio;
1801 my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1802 my $item2 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1804 $biblio1->items->move_to_biblio($biblio2);
1806 $item1->discard_changes;
1807 $item2->discard_changes;
1809 is($item1->biblionumber, $biblio2->biblionumber, "Item 1 moved");
1810 is($item2->biblionumber, $biblio2->biblionumber, "Item 2 moved");
1812 $schema->storage->txn_rollback;
1816 subtest 'search_ordered' => sub {
1820 $schema->storage->txn_begin;
1822 my $library_a = $builder->build_object(
1823 { class => 'Koha::Libraries', value => { branchname => 'TEST_A' } } );
1824 my $library_z = $builder->build_object(
1825 { class => 'Koha::Libraries', value => { branchname => 'TEST_Z' } } );
1826 my $biblio = $builder->build_sample_biblio( { serial => 0 } );
1827 my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1828 my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1829 my $item3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1833 # order_by homebranch.branchname
1834 $item1->discard_changes->update( { homebranch => $library_z->branchcode } );
1835 $item2->discard_changes->update( { homebranch => $library_a->branchcode } );
1836 $item3->discard_changes->update( { homebranch => $library_z->branchcode } );
1837 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1838 [ $item2->itemnumber, $item1->itemnumber, $item3->itemnumber ],
1839 "not a serial - order by homebranch" );
1841 # order_by me.enumchron
1842 $biblio->items->update( { homebranch => $library_a->branchcode } );
1843 $item1->discard_changes->update( { enumchron => 'cc' } );
1844 $item2->discard_changes->update( { enumchron => 'bb' } );
1845 $item3->discard_changes->update( { enumchron => 'aa' } );
1846 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1847 [ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ],
1848 "not a serial - order by enumchron" );
1850 # order_by LPAD( me.copynumber, 8, '0' )
1851 $biblio->items->update( { enumchron => undef } );
1852 $item1->discard_changes->update( { copynumber => '12345678' } );
1853 $item2->discard_changes->update( { copynumber => '34567890' } );
1854 $item3->discard_changes->update( { copynumber => '23456789' } );
1855 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1856 [ $item1->itemnumber, $item3->itemnumber, $item2->itemnumber ],
1857 "not a serial - order by LPAD( me.copynumber, 8, '0' )" );
1859 # order_by -desc => 'me.dateaccessioned'
1860 $biblio->items->update( { copynumber => undef } );
1861 $item1->discard_changes->update( { dateaccessioned => '2022-08-19' } );
1862 $item2->discard_changes->update( { dateaccessioned => '2022-07-19' } );
1863 $item3->discard_changes->update( { dateaccessioned => '2022-09-19' } );
1864 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1865 [ $item3->itemnumber, $item1->itemnumber, $item2->itemnumber ],
1866 "not a serial - order by date accessioned desc" );
1871 my $sub_freq = $builder->build( { source => 'SubscriptionFrequency' } );
1873 $builder->build( { source => 'SubscriptionNumberpattern' } );
1874 my $subscription = $builder->build_object(
1876 class => 'Koha::Subscriptions',
1878 biblionumber => $biblio->biblionumber,
1879 periodicity => $sub_freq->{id},
1880 numberpattern => $sub_np->{id}
1884 $builder->build_object(
1886 class => 'Koha::Subscription::Histories',
1888 subscriptionid => $subscription->subscriptionid,
1889 biblionumber => $biblio->biblionumber
1894 $biblio->update( { serial => 1 } );
1896 C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1897 $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1898 "notes", "routingnotes" );
1899 C4::Serials::AddItem2Serial( $serialid1, $item1->itemnumber );
1901 C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1902 $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1903 "notes", "routingnotes" );
1904 C4::Serials::AddItem2Serial( $serialid2, $item2->itemnumber );
1906 C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1907 $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1908 "notes", "routingnotes" );
1909 C4::Serials::AddItem2Serial( $serialid3, $item3->itemnumber );
1910 my $serial1 = Koha::Serials->find($serialid1);
1911 my $serial2 = Koha::Serials->find($serialid2);
1912 my $serial3 = Koha::Serials->find($serialid3);
1914 # order_by serial.publisheddate
1915 $serial1->discard_changes->update( { publisheddate => '2022-09-19' } );
1916 $serial2->discard_changes->update( { publisheddate => '2022-07-19' } );
1917 $serial3->discard_changes->update( { publisheddate => '2022-08-19' } );
1919 [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1920 [ $item2->itemnumber, $item3->itemnumber, $item1->itemnumber ],
1921 "serial - order by publisheddate"
1924 # order_by me.enumchron
1925 $serial1->discard_changes->update({ publisheddate => '2022-08-19' });
1926 $serial2->discard_changes->update({ publisheddate => '2022-08-19' });
1927 $serial3->discard_changes->update({ publisheddate => '2022-08-19' });
1928 $item1->discard_changes->update( { enumchron => 'cc' } );
1929 $item2->discard_changes->update( { enumchron => 'bb' } );
1930 $item3->discard_changes->update( { enumchron => 'aa' } );
1931 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1932 [ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ],
1933 "serial - order by enumchron" );
1937 $schema->storage->txn_rollback;