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( dt_from_string($item->datelastseen)->ymd, $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, $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, $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 my $processing_return = Koha::Account::Lines->search(
460 itemnumber => $item->itemnumber,
461 credit_type_code => 'PROCESSING_FOUND'
465 ok( $processing_return, 'An account line of type PROCESSING_FOUND is added' );
466 is( $processing_return->amount + 0,
468 'The account line of type PROCESSING_FOUND has an amount of -20' );
470 $lost_fee_line->discard_changes;
471 is( $lost_fee_line->amountoutstanding + 0,
472 0, 'Lost fee has no outstanding amount' );
473 is( $lost_fee_line->debit_type_code,
474 'LOST', 'Lost fee now still has account type of LOST' );
475 is( $lost_fee_line->status, 'FOUND',
476 "Lost fee now has account status of FOUND" );
478 is( $patron->account->balance, -119,
479 'The patron balance is -119, a credit that equals the lost fee payment and the processing fee'
483 subtest 'Test without payment or write off' => sub {
487 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
489 my $item = $builder->build_sample_item(
491 biblionumber => $biblio->biblionumber,
492 library => $library->branchcode,
493 replacementprice => 23.00,
494 replacementprice => $replacement_amount,
495 itype => $item_type->itemtype
500 C4::Circulation::AddIssue( $patron, $item->barcode );
502 # Simulate item marked as lost
503 $item->itemlost(3)->store;
504 C4::Circulation::LostItem( $item->itemnumber, 1 );
506 my $processing_fee_lines = Koha::Account::Lines->search(
508 borrowernumber => $patron->id,
509 itemnumber => $item->itemnumber,
510 debit_type_code => 'PROCESSING'
513 is( $processing_fee_lines->count,
514 1, 'Only one processing fee produced' );
515 my $processing_fee_line = $processing_fee_lines->next;
516 is( $processing_fee_line->amount + 0,
518 'The right PROCESSING amount is generated' );
519 is( $processing_fee_line->amountoutstanding + 0,
521 'The right PROCESSING amountoutstanding is generated' );
523 my $lost_fee_lines = Koha::Account::Lines->search(
525 borrowernumber => $patron->id,
526 itemnumber => $item->itemnumber,
527 debit_type_code => 'LOST'
530 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
531 my $lost_fee_line = $lost_fee_lines->next;
532 is( $lost_fee_line->amount + 0,
533 $replacement_amount, 'The right LOST amount is generated' );
534 is( $lost_fee_line->amountoutstanding + 0,
536 'The right LOST amountountstanding is generated' );
538 # Set processingreturn_policy to '0' so processing fee is retained
539 # these tests are just for lostreturn
540 my $processingreturn_rule = $builder->build(
542 source => 'CirculationRule',
545 categorycode => undef,
547 rule_name => 'processingreturn',
553 # Simulate item marked as found
554 $item->itemlost(0)->store;
555 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
557 my $credit_return = Koha::Account::Lines->search(
559 itemnumber => $item->itemnumber,
560 credit_type_code => 'LOST_FOUND'
565 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
566 is( $credit_return->amount + 0,
568 'The account line of type LOST_FOUND has an amount of -99' );
570 $credit_return->amountoutstanding + 0,
572 'The account line of type LOST_FOUND has an amountoutstanding of 0'
575 $lost_fee_line->discard_changes;
576 is( $lost_fee_line->amountoutstanding + 0,
577 0, 'Lost fee has no outstanding amount' );
578 is( $lost_fee_line->debit_type_code,
579 'LOST', 'Lost fee now still has account type of LOST' );
580 is( $lost_fee_line->status, 'FOUND',
581 "Lost fee now has account status of FOUND" );
583 is( $patron->account->balance,
584 20, 'The patron balance is 20, still owes the processing fee' );
588 'Test with partial payment and write off, and remaining debt' =>
593 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 0 );
595 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
596 my $item = $builder->build_sample_item(
598 biblionumber => $biblio->biblionumber,
599 library => $library->branchcode,
600 replacementprice => $replacement_amount,
601 itype => $item_type->itemtype
606 C4::Circulation::AddIssue( $patron, $item->barcode );
608 # Simulate item marked as lost
609 $item->itemlost(1)->store;
610 C4::Circulation::LostItem( $item->itemnumber, 1 );
612 my $processing_fee_lines = Koha::Account::Lines->search(
614 borrowernumber => $patron->id,
615 itemnumber => $item->itemnumber,
616 debit_type_code => 'PROCESSING'
619 is( $processing_fee_lines->count,
620 1, 'Only one processing fee produced' );
621 my $processing_fee_line = $processing_fee_lines->next;
622 is( $processing_fee_line->amount + 0,
624 'The right PROCESSING amount is generated' );
625 is( $processing_fee_line->amountoutstanding + 0,
627 'The right PROCESSING amountoutstanding is generated' );
629 my $lost_fee_lines = Koha::Account::Lines->search(
631 borrowernumber => $patron->id,
632 itemnumber => $item->itemnumber,
633 debit_type_code => 'LOST'
636 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
637 my $lost_fee_line = $lost_fee_lines->next;
638 is( $lost_fee_line->amount + 0,
639 $replacement_amount, 'The right LOST amount is generated' );
640 is( $lost_fee_line->amountoutstanding + 0,
642 'The right LOST amountountstanding is generated' );
644 my $account = $patron->account;
647 $processfee_amount + $replacement_amount,
648 'Balance is PROCESSING + LOST'
651 # Partially pay fee (99 - 27 = 72)
652 my $payment_amount = 24;
653 my $payment = $account->add_credit(
655 amount => $payment_amount,
661 $payment->apply( { debits => [$lost_fee_line] } );
663 # Partially write off fee (72 - 20 = 52)
664 my $write_off_amount = 20;
665 my $write_off = $account->add_credit(
667 amount => $write_off_amount,
672 $write_off->apply( { debits => [$lost_fee_line] } );
675 my $payment_amount_2 = 3;
676 my $payment_2 = $account->add_credit(
678 amount => $payment_amount_2,
685 { debits => [$lost_fee_line] } );
687 # Partially write off fee (52 - 5 = 47)
688 my $write_off_amount_2 = 5;
689 my $write_off_2 = $account->add_credit(
691 amount => $write_off_amount_2,
698 { debits => [$lost_fee_line] } );
703 $replacement_amount -
708 'Balance is PROCESSING + LOST - PAYMENT 1 - WRITEOFF - PAYMENT 2 - WRITEOFF 2'
711 # VOID payment_2 and writeoff_2
712 $payment_2->void({ interface => 'test' });
713 $write_off_2->void({ interface => 'test' });
718 $replacement_amount -
721 'Balance is PROCESSING + LOST - PAYMENT 1 - WRITEOFF (PAYMENT 2 and WRITEOFF 2 VOIDED)'
724 # Store the amountoutstanding value
725 $lost_fee_line->discard_changes;
726 my $outstanding = $lost_fee_line->amountoutstanding;
729 $replacement_amount - $payment_amount - $write_off_amount,
730 "Lost Fee Outstanding is LOST - PAYMENT 1 - WRITEOFF"
733 # Simulate item marked as found
734 $item->itemlost(0)->store;
735 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
737 my $credit_return = Koha::Account::Lines->search(
739 itemnumber => $item->itemnumber,
740 credit_type_code => 'LOST_FOUND'
745 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
749 $processfee_amount - $payment_amount,
750 'Balance is PROCESSING - PAYMENT (LOST_FOUND)'
753 $lost_fee_line->discard_changes;
754 is( $lost_fee_line->amountoutstanding + 0,
755 0, 'Lost fee has no outstanding amount' );
756 is( $lost_fee_line->debit_type_code,
757 'LOST', 'Lost fee now still has account type of LOST' );
758 is( $lost_fee_line->status, 'FOUND',
759 "Lost fee now has account status of FOUND" );
762 $credit_return->amount + 0,
763 ( $payment_amount + $outstanding ) * -1,
764 'The account line of type LOST_FOUND has an amount equal to the payment 1 + outstanding'
767 $credit_return->amountoutstanding + 0,
768 $payment_amount * -1,
769 'The account line of type LOST_FOUND has an amountoutstanding equal to the payment'
774 $processfee_amount - $payment_amount,
775 'The patron balance is the difference between the PROCESSING and the credit'
779 subtest 'Partial payment, existing debits and AccountAutoReconcile' =>
784 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
785 my $barcode = 'KD123456793';
786 my $replacement_amount = 100;
787 my $processfee_amount = 20;
789 my $item_type = $builder->build_object(
791 class => 'Koha::ItemTypes',
795 defaultreplacecost => undef,
797 rentalcharge_daily => 0,
801 my $item = Koha::Item->new(
803 biblionumber => $biblio->biblionumber,
804 homebranch => $library->branchcode,
805 holdingbranch => $library->branchcode,
807 replacementprice => $replacement_amount,
808 itype => $item_type->itemtype
813 C4::Circulation::AddIssue( $patron, $barcode );
815 # Simulate item marked as lost
816 $item->itemlost(1)->store;
817 C4::Circulation::LostItem( $item->itemnumber, 1 );
819 my $lost_fee_lines = Koha::Account::Lines->search(
821 borrowernumber => $patron->id,
822 itemnumber => $item->itemnumber,
823 debit_type_code => 'LOST'
826 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
827 my $lost_fee_line = $lost_fee_lines->next;
828 is( $lost_fee_line->amount + 0,
829 $replacement_amount, 'The right LOST amount is generated' );
830 is( $lost_fee_line->amountoutstanding + 0,
832 'The right LOST amountountstanding is generated' );
834 my $account = $patron->account;
835 is( $account->balance, $replacement_amount, 'Balance is L' );
838 my $payment_amount = 27;
839 my $payment = $account->add_credit(
841 amount => $payment_amount,
846 $payment->apply( { debits => [$lost_fee_line] } );
850 $replacement_amount - $payment_amount,
854 my $manual_debit_amount = 80;
857 amount => $manual_debit_amount,
865 $manual_debit_amount + $replacement_amount - $payment_amount,
866 'Manual debit applied'
869 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 1 );
871 # Simulate item marked as found
872 $item->itemlost(0)->store;
873 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
875 my $credit_return = Koha::Account::Lines->search(
877 itemnumber => $item->itemnumber,
878 credit_type_code => 'LOST_FOUND'
883 ok( $credit_return, 'An account line of type LOST_FOUND is added' );
887 $manual_debit_amount - $payment_amount,
888 'Balance is PROCESSING - payment (LOST_FOUND)'
891 my $manual_debit = Koha::Account::Lines->search(
893 borrowernumber => $patron->id,
894 debit_type_code => 'OVERDUE',
895 status => 'UNRETURNED'
899 $manual_debit->amountoutstanding + 0,
900 $manual_debit_amount - $payment_amount,
901 'reconcile_balance was called'
905 subtest 'Patron deleted' => sub {
908 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
909 my $barcode = 'KD123456794';
910 my $replacement_amount = 100;
911 my $processfee_amount = 20;
913 my $item_type = $builder->build_object(
915 class => 'Koha::ItemTypes',
919 defaultreplacecost => undef,
921 rentalcharge_daily => 0,
925 my $item = Koha::Item->new(
927 biblionumber => $biblio->biblionumber,
928 homebranch => $library->branchcode,
929 holdingbranch => $library->branchcode,
931 replacementprice => $replacement_amount,
932 itype => $item_type->itemtype
937 C4::Circulation::AddIssue( $patron, $barcode );
939 # Simulate item marked as lost
940 $item->itemlost(1)->store;
941 C4::Circulation::LostItem( $item->itemnumber, 1 );
946 # Simulate item marked as found
947 $item->itemlost(0)->store;
948 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 0, 'No refund triggered' );
952 subtest 'restore fine | no overdue' => sub {
957 $builder->build_object( { class => "Koha::Patrons" } );
958 t::lib::Mocks::mock_userenv(
959 { patron => $manager, branchcode => $manager->branchcode } );
961 # Set lostreturn_policy to 'restore' for tests
962 my $specific_rule_restore = $builder->build(
964 source => 'CirculationRule',
966 branchcode => $manager->branchcode,
967 categorycode => undef,
969 rule_name => 'lostreturn',
970 rule_value => 'restore'
975 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
977 my $item = $builder->build_sample_item(
979 biblionumber => $biblio->biblionumber,
980 library => $library->branchcode,
981 replacementprice => $replacement_amount,
982 itype => $item_type->itemtype
987 C4::Circulation::AddIssue( $patron, $item->barcode );
989 # Simulate item marked as lost
990 $item->itemlost(1)->store;
991 C4::Circulation::LostItem( $item->itemnumber, 1 );
993 my $processing_fee_lines = Koha::Account::Lines->search(
995 borrowernumber => $patron->id,
996 itemnumber => $item->itemnumber,
997 debit_type_code => 'PROCESSING'
1000 is( $processing_fee_lines->count,
1001 1, 'Only one processing fee produced' );
1002 my $processing_fee_line = $processing_fee_lines->next;
1003 is( $processing_fee_line->amount + 0,
1005 'The right PROCESSING amount is generated' );
1006 is( $processing_fee_line->amountoutstanding + 0,
1008 'The right PROCESSING amountoutstanding is generated' );
1010 my $lost_fee_lines = Koha::Account::Lines->search(
1012 borrowernumber => $patron->id,
1013 itemnumber => $item->itemnumber,
1014 debit_type_code => 'LOST'
1017 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1018 my $lost_fee_line = $lost_fee_lines->next;
1019 is( $lost_fee_line->amount + 0,
1020 $replacement_amount, 'The right LOST amount is generated' );
1021 is( $lost_fee_line->amountoutstanding + 0,
1022 $replacement_amount,
1023 'The right LOST amountountstanding is generated' );
1025 my $account = $patron->account;
1026 my $debts = $account->outstanding_debits;
1029 my $credit = $account->add_credit(
1031 amount => $account->balance,
1033 interface => 'test',
1036 $credit->apply( { debits => [ $debts->as_list ] } );
1038 # Simulate item marked as found
1039 $item->itemlost(0)->store;
1040 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1041 is( scalar ( grep { $_->message eq 'lost_restored' } @{$item->object_messages} ), 0, 'Restore not triggered when there is no overdue fine found' );
1044 subtest 'restore fine | unforgiven overdue' => sub {
1048 # Set lostreturn_policy to 'restore' for tests
1050 $builder->build_object( { class => "Koha::Patrons" } );
1051 t::lib::Mocks::mock_userenv(
1052 { patron => $manager, branchcode => $manager->branchcode } );
1053 my $specific_rule_restore = $builder->build(
1055 source => 'CirculationRule',
1057 branchcode => $manager->branchcode,
1058 categorycode => undef,
1060 rule_name => 'lostreturn',
1061 rule_value => 'restore'
1066 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1068 my $item = $builder->build_sample_item(
1070 biblionumber => $biblio->biblionumber,
1071 library => $library->branchcode,
1072 replacementprice => $replacement_amount,
1073 itype => $item_type->itemtype
1078 C4::Circulation::AddIssue( $patron, $item->barcode );
1080 # Simulate item marked as lost
1081 $item->itemlost(1)->store;
1082 C4::Circulation::LostItem( $item->itemnumber, 1 );
1084 my $processing_fee_lines = Koha::Account::Lines->search(
1086 borrowernumber => $patron->id,
1087 itemnumber => $item->itemnumber,
1088 debit_type_code => 'PROCESSING'
1091 is( $processing_fee_lines->count,
1092 1, 'Only one processing fee produced' );
1093 my $processing_fee_line = $processing_fee_lines->next;
1094 is( $processing_fee_line->amount + 0,
1096 'The right PROCESSING amount is generated' );
1097 is( $processing_fee_line->amountoutstanding + 0,
1099 'The right PROCESSING amountoutstanding is generated' );
1101 my $lost_fee_lines = Koha::Account::Lines->search(
1103 borrowernumber => $patron->id,
1104 itemnumber => $item->itemnumber,
1105 debit_type_code => 'LOST'
1108 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1109 my $lost_fee_line = $lost_fee_lines->next;
1110 is( $lost_fee_line->amount + 0,
1111 $replacement_amount, 'The right LOST amount is generated' );
1112 is( $lost_fee_line->amountoutstanding + 0,
1113 $replacement_amount,
1114 'The right LOST amountountstanding is generated' );
1116 my $account = $patron->account;
1117 my $debts = $account->outstanding_debits;
1120 my $credit = $account->add_credit(
1122 amount => $account->balance,
1124 interface => 'test',
1127 $credit->apply( { debits => [ $debts->as_list ] } );
1130 my $overdue = $account->add_debit(
1133 user_id => $manager->borrowernumber,
1134 library_id => $library->branchcode,
1135 interface => 'test',
1136 item_id => $item->itemnumber,
1140 $overdue->status('LOST')->store();
1141 $overdue->discard_changes;
1142 is( $overdue->status, 'LOST',
1143 'Overdue status set to LOST' );
1145 # Simulate item marked as found
1146 $item->itemlost(0)->store;
1147 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1148 is( scalar ( grep { $_->message eq 'lost_restored' } @{$item->object_messages} ), 0, 'Restore not triggered when overdue was not forgiven' );
1149 $overdue->discard_changes;
1150 is( $overdue->status, 'FOUND',
1151 'Overdue status updated to FOUND' );
1154 subtest 'restore fine | forgiven overdue' => sub {
1158 # Set lostreturn_policy to 'restore' for tests
1160 $builder->build_object( { class => "Koha::Patrons" } );
1161 t::lib::Mocks::mock_userenv(
1162 { patron => $manager, branchcode => $manager->branchcode } );
1163 my $specific_rule_restore = $builder->build(
1165 source => 'CirculationRule',
1167 branchcode => $manager->branchcode,
1168 categorycode => undef,
1170 rule_name => 'lostreturn',
1171 rule_value => 'restore'
1176 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1178 my $item = $builder->build_sample_item(
1180 biblionumber => $biblio->biblionumber,
1181 library => $library->branchcode,
1182 replacementprice => $replacement_amount,
1183 itype => $item_type->itemtype
1188 C4::Circulation::AddIssue( $patron, $item->barcode );
1190 # Simulate item marked as lost
1191 $item->itemlost(1)->store;
1192 C4::Circulation::LostItem( $item->itemnumber, 1 );
1194 my $processing_fee_lines = Koha::Account::Lines->search(
1196 borrowernumber => $patron->id,
1197 itemnumber => $item->itemnumber,
1198 debit_type_code => 'PROCESSING'
1201 is( $processing_fee_lines->count,
1202 1, 'Only one processing fee produced' );
1203 my $processing_fee_line = $processing_fee_lines->next;
1204 is( $processing_fee_line->amount + 0,
1206 'The right PROCESSING amount is generated' );
1207 is( $processing_fee_line->amountoutstanding + 0,
1209 'The right PROCESSING amountoutstanding is generated' );
1211 my $lost_fee_lines = Koha::Account::Lines->search(
1213 borrowernumber => $patron->id,
1214 itemnumber => $item->itemnumber,
1215 debit_type_code => 'LOST'
1218 is( $lost_fee_lines->count, 1, 'Only one lost item fee produced' );
1219 my $lost_fee_line = $lost_fee_lines->next;
1220 is( $lost_fee_line->amount + 0,
1221 $replacement_amount, 'The right LOST amount is generated' );
1222 is( $lost_fee_line->amountoutstanding + 0,
1223 $replacement_amount,
1224 'The right LOST amountountstanding is generated' );
1226 my $account = $patron->account;
1227 my $debts = $account->outstanding_debits;
1230 my $credit = $account->add_credit(
1232 amount => $account->balance,
1234 interface => 'test',
1237 $credit->apply( { debits => [ $debts->as_list ] } );
1240 my $overdue = $account->add_debit(
1243 user_id => $manager->borrowernumber,
1244 library_id => $library->branchcode,
1245 interface => 'test',
1246 item_id => $item->itemnumber,
1250 $overdue->status('LOST')->store();
1251 is( $overdue->status, 'LOST',
1252 'Overdue status set to LOST' );
1254 t::lib::Mocks::mock_preference( 'AccountAutoReconcile', 0 );
1257 $credit = $account->add_credit(
1260 user_id => $manager->borrowernumber,
1261 library_id => $library->branchcode,
1262 interface => 'test',
1264 item_id => $item->itemnumber
1267 $credit->apply( { debits => [$overdue] } );
1269 # Simulate item marked as found
1270 $item->itemlost(0)->store;
1271 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1272 is( scalar ( grep { $_->message eq 'lost_restored' } @{$item->object_messages} ), 1, 'Restore triggered when overdue was forgiven' );
1273 $overdue->discard_changes;
1274 is( $overdue->status, 'FOUND', 'Overdue status updated to FOUND' );
1275 is( $overdue->amountoutstanding, $overdue->amount, 'Overdue outstanding has been restored' );
1276 $credit->discard_changes;
1277 is( $credit->status, 'VOID', 'Overdue Forgival has been marked as VOID');
1280 subtest 'Continue when userenv is not set' => sub {
1283 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
1284 my $barcode = 'KD123456795';
1285 my $replacement_amount = 100;
1286 my $processfee_amount = 20;
1288 my $item_type = $builder->build_object(
1290 class => 'Koha::ItemTypes',
1292 notforloan => undef,
1294 defaultreplacecost => undef,
1296 rentalcharge_daily => 0,
1300 my $item = $builder->build_sample_item(
1302 biblionumber => $biblio->biblionumber,
1303 homebranch => $library->branchcode,
1304 holdingbranch => $library->branchcode,
1305 barcode => $barcode,
1306 replacementprice => $replacement_amount,
1307 itype => $item_type->itemtype
1312 C4::Circulation::AddIssue( $patron, $barcode );
1314 # Simulate item marked as lost
1315 $item->itemlost(1)->store;
1316 C4::Circulation::LostItem( $item->itemnumber, 1 );
1319 C4::Context->_new_userenv(undef);
1321 # Simluate item marked as found
1322 $item->itemlost(0)->store;
1323 is( scalar ( grep { $_->message eq 'lost_refunded' } @{$item->object_messages} ), 1, 'Refund triggered' );
1328 subtest 'log_action' => sub {
1330 t::lib::Mocks::mock_preference( 'CataloguingLog', 1 );
1332 my $item = Koha::Item->new(
1334 homebranch => $library->{branchcode},
1335 holdingbranch => $library->{branchcode},
1336 biblionumber => $biblio->biblionumber,
1337 location => 'my_loc',
1341 Koha::ActionLogs->search(
1343 module => 'CATALOGUING',
1345 object => $item->itemnumber,
1350 "Item creation logged"
1353 $item->location('another_loc')->store;
1355 Koha::ActionLogs->search(
1357 module => 'CATALOGUING',
1359 object => $item->itemnumber
1363 "Item modification logged"
1368 subtest 'get_transfer' => sub {
1371 my $transfer = $new_item_1->get_transfer();
1372 is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
1374 my $library_to = $builder->build( { source => 'Branch' } );
1376 my $transfer_1 = $builder->build_object(
1378 class => 'Koha::Item::Transfers',
1380 itemnumber => $new_item_1->itemnumber,
1381 frombranch => $new_item_1->holdingbranch,
1382 tobranch => $library_to->{branchcode},
1385 datearrived => undef,
1386 datecancelled => undef,
1387 daterequested => \'NOW()'
1392 $transfer = $new_item_1->get_transfer();
1393 is( ref($transfer), 'Koha::Item::Transfer', 'Koha::Item->get_transfer should return a Koha::Item::Transfer object' );
1395 my $transfer_2 = $builder->build_object(
1397 class => 'Koha::Item::Transfers',
1399 itemnumber => $new_item_1->itemnumber,
1400 frombranch => $new_item_1->holdingbranch,
1401 tobranch => $library_to->{branchcode},
1404 datearrived => undef,
1405 datecancelled => undef,
1406 daterequested => \'NOW()'
1411 $transfer = $new_item_1->get_transfer();
1412 is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the oldest transfer request');
1414 $transfer_2->datesent(\'NOW()')->store;
1415 $transfer = $new_item_1->get_transfer();
1416 is( $transfer->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfer returns the in_transit transfer');
1418 my $transfer_3 = $builder->build_object(
1420 class => 'Koha::Item::Transfers',
1422 itemnumber => $new_item_1->itemnumber,
1423 frombranch => $new_item_1->holdingbranch,
1424 tobranch => $library_to->{branchcode},
1427 datearrived => undef,
1428 datecancelled => undef,
1429 daterequested => \'NOW()'
1434 $transfer_2->datearrived(\'NOW()')->store;
1435 $transfer = $new_item_1->get_transfer();
1436 is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the next queued transfer');
1437 is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' );
1439 $transfer_1->datecancelled(\'NOW()')->store;
1440 $transfer = $new_item_1->get_transfer();
1441 is( $transfer->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfer ignores cancelled transfers');
1444 subtest 'holds' => sub {
1447 my $biblio = $builder->build_sample_biblio();
1448 my $item = $builder->build_sample_item({
1449 biblionumber => $biblio->biblionumber,
1451 is($item->holds->count, 0, "Nothing returned if no holds");
1452 my $hold1 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'T' }});
1453 my $hold2 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
1454 my $hold3 = $builder->build({ source => 'Reserve', value => { itemnumber=>$item->itemnumber, found => 'W' }});
1456 is($item->holds()->count,3,"Three holds found");
1457 is($item->holds({found => 'W'})->count,2,"Two waiting holds found");
1458 is_deeply($item->holds({found => 'T'})->next->unblessed,$hold1,"Found transit holds matches the hold");
1459 is($item->holds({found => undef})->count, 0,"Nothing returned if no matching holds");
1462 subtest 'biblio' => sub {
1465 my $biblio = $retrieved_item_1->biblio;
1466 is( ref( $biblio ), 'Koha::Biblio', 'Koha::Item->biblio should return a Koha::Biblio' );
1467 is( $biblio->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblio should return the correct biblio' );
1470 subtest 'biblioitem' => sub {
1473 my $biblioitem = $retrieved_item_1->biblioitem;
1474 is( ref( $biblioitem ), 'Koha::Biblioitem', 'Koha::Item->biblioitem should return a Koha::Biblioitem' );
1475 is( $biblioitem->biblionumber, $retrieved_item_1->biblionumber, 'Koha::Item->biblioitem should return the correct biblioitem' );
1479 t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
1480 subtest 'checkout' => sub {
1482 my $item = Koha::Items->find( $new_item_1->itemnumber );
1484 my $checkout = $item->checkout;
1485 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no current checkout on this item' );
1488 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1489 C4::Circulation::AddIssue( $patron, $item->barcode );
1490 $checkout = $retrieved_item_1->checkout;
1491 is( ref( $checkout ), 'Koha::Checkout', 'Koha::Item->checkout should return a Koha::Checkout' );
1492 is( $checkout->itemnumber, $item->itemnumber, 'Koha::Item->checkout should return the correct checkout' );
1493 is( $checkout->borrowernumber, $patron->borrowernumber, 'Koha::Item->checkout should return the correct checkout' );
1496 C4::Circulation::AddReturn( $item->barcode );
1498 # There is no more checkout on this item, making sure it will not return old checkouts
1499 $checkout = $item->checkout;
1500 is( $checkout, undef, 'Koha::Item->checkout should return undef if there is no *current* checkout on this item' );
1503 subtest 'can_be_transferred' => sub {
1506 t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
1507 t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
1509 my $biblio = $builder->build_sample_biblio();
1510 my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
1511 my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
1512 my $item = $builder->build_sample_item({
1513 biblionumber => $biblio->biblionumber,
1514 homebranch => $library1->branchcode,
1515 holdingbranch => $library1->branchcode,
1518 is(Koha::Item::Transfer::Limits->search({
1519 fromBranch => $library1->branchcode,
1520 toBranch => $library2->branchcode,
1521 })->count, 0, 'There are no transfer limits between libraries.');
1522 ok($item->can_be_transferred({ to => $library2 }),
1523 'Item can be transferred between libraries.');
1525 my $limit = Koha::Item::Transfer::Limit->new({
1526 fromBranch => $library1->branchcode,
1527 toBranch => $library2->branchcode,
1528 itemtype => $item->effective_itemtype,
1530 is(Koha::Item::Transfer::Limits->search({
1531 fromBranch => $library1->branchcode,
1532 toBranch => $library2->branchcode,
1533 })->count, 1, 'Given we have added a transfer limit,');
1534 is($item->can_be_transferred({ to => $library2 }), 0,
1535 'Item can no longer be transferred between libraries.');
1536 is($item->can_be_transferred({ to => $library2, from => $library1 }), 0,
1537 'We get the same result also if we pass the from-library parameter.');
1540 # Reset nb_of_items prior to testing delete
1541 $nb_of_items = Koha::Items->search->count;
1544 $retrieved_item_1->delete;
1545 is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' );
1547 $schema->storage->txn_rollback;
1549 subtest 'filter_by_visible_in_opac() tests' => sub {
1553 $schema->storage->txn_begin;
1555 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
1556 my $mocked_category = Test::MockModule->new('Koha::Patron::Category');
1558 $mocked_category->mock( 'override_hidden_items', sub {
1562 # have a fresh biblio
1563 my $biblio = $builder->build_sample_biblio;
1564 # have two itemtypes
1565 my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' });
1566 my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' });
1567 # have 5 items on that biblio
1568 my $item_1 = $builder->build_sample_item(
1570 biblionumber => $biblio->biblionumber,
1572 itype => $itype_1->itemtype,
1577 my $item_2 = $builder->build_sample_item(
1579 biblionumber => $biblio->biblionumber,
1581 itype => $itype_2->itemtype,
1586 my $item_3 = $builder->build_sample_item(
1588 biblionumber => $biblio->biblionumber,
1590 itype => $itype_1->itemtype,
1595 my $item_4 = $builder->build_sample_item(
1597 biblionumber => $biblio->biblionumber,
1599 itype => $itype_2->itemtype,
1604 my $item_5 = $builder->build_sample_item(
1606 biblionumber => $biblio->biblionumber,
1608 itype => $itype_1->itemtype,
1613 my $item_6 = $builder->build_sample_item(
1615 biblionumber => $biblio->biblionumber,
1617 itype => $itype_1->itemtype,
1625 my $mocked_context = Test::MockModule->new('C4::Context');
1626 $mocked_context->mock( 'yaml_preference', sub {
1630 t::lib::Mocks::mock_preference( 'hidelostitems', 0 );
1631 is( $biblio->items->filter_by_visible_in_opac->count,
1632 6, 'No rules passed, hidelostitems unset' );
1634 is( $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1635 6, 'No rules passed, hidelostitems unset, patron exception changes nothing' );
1637 $rules = { copynumber => [ 2 ] };
1639 t::lib::Mocks::mock_preference( 'hidelostitems', 1 );
1641 $biblio->items->filter_by_visible_in_opac->count,
1643 'No rules passed, hidelostitems set'
1647 $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1649 'No rules passed, hidelostitems set, patron exception changes nothing'
1652 $rules = { biblionumber => [ $biblio->biblionumber ] };
1654 $biblio->items->filter_by_visible_in_opac->count,
1656 'Biblionumber rule successfully hides all items'
1659 my $biblio2 = $builder->build_sample_biblio;
1660 $rules = { biblionumber => [ $biblio2->biblionumber ] };
1661 my $prefetched = $biblio->items->search({},{ prefetch => ['branchtransfers','reserves'] })->filter_by_visible_in_opac;
1662 ok( $prefetched->next, "Can retrieve object when prefetching and hiding on a duplicated column");
1664 $rules = { withdrawn => [ 1, 2 ], copynumber => [ 2 ] };
1666 $biblio->items->filter_by_visible_in_opac->count,
1668 'Rules on withdrawn, hidelostitems set'
1672 $biblio->items->filter_by_visible_in_opac({ patron => $patron })->count,
1674 'hidelostitems set, rules on withdrawn but patron override passed'
1677 $rules = { itype => [ $itype_1->itemtype ], copynumber => [ 2 ] };
1679 $biblio->items->filter_by_visible_in_opac->count,
1681 'Rules on itype, hidelostitems set'
1684 $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ], copynumber => [ 2 ] };
1686 $biblio->items->filter_by_visible_in_opac->count,
1688 'Rules on itype and withdrawn, hidelostitems set'
1691 $biblio->items->filter_by_visible_in_opac
1693 $item_4->itemnumber,
1694 'The right item is returned'
1697 $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ], copynumber => [ 2 ] };
1699 $biblio->items->filter_by_visible_in_opac->count,
1701 'Rules on itype and withdrawn, hidelostitems set'
1704 $biblio->items->filter_by_visible_in_opac
1706 $item_5->itemnumber,
1707 'The right item is returned'
1710 # Make sure the warning on the about page will work
1711 $rules = { itemlost => ['AB'] };
1712 my $c = Koha::Items->filter_by_visible_in_opac->count;
1713 my @warnings = C4::Context->dbh->selectrow_array('SHOW WARNINGS');
1714 like( $warnings[2], qr/Truncated incorrect (DOUBLE|DECIMAL) value: 'AB'/);
1716 $schema->storage->txn_rollback;
1719 subtest 'filter_out_lost() tests' => sub {
1723 $schema->storage->txn_begin;
1725 # have a fresh biblio
1726 my $biblio = $builder->build_sample_biblio;
1727 # have 3 items on that biblio
1728 my $item_1 = $builder->build_sample_item(
1730 biblionumber => $biblio->biblionumber,
1734 my $item_2 = $builder->build_sample_item(
1736 biblionumber => $biblio->biblionumber,
1740 my $item_3 = $builder->build_sample_item(
1742 biblionumber => $biblio->biblionumber,
1747 is( $biblio->items->filter_out_lost->next->itemnumber, $item_2->itemnumber, 'Right item returned' );
1748 is( $biblio->items->filter_out_lost->count, 1, 'Only one item is not lost' );
1750 $schema->storage->txn_rollback;
1753 subtest 'move_to_biblio() tests' => sub {
1757 $schema->storage->txn_begin;
1759 my $biblio1 = $builder->build_sample_biblio;
1760 my $biblio2 = $builder->build_sample_biblio;
1761 my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1762 my $item2 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
1764 $biblio1->items->move_to_biblio($biblio2);
1766 $item1->discard_changes;
1767 $item2->discard_changes;
1769 is($item1->biblionumber, $biblio2->biblionumber, "Item 1 moved");
1770 is($item2->biblionumber, $biblio2->biblionumber, "Item 2 moved");
1772 $schema->storage->txn_rollback;
1776 subtest 'search_ordered' => sub {
1780 $schema->storage->txn_begin;
1782 my $library_a = $builder->build_object(
1783 { class => 'Koha::Libraries', value => { branchname => 'TEST_A' } } );
1784 my $library_z = $builder->build_object(
1785 { class => 'Koha::Libraries', value => { branchname => 'TEST_Z' } } );
1786 my $biblio = $builder->build_sample_biblio( { serial => 0 } );
1787 my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1788 my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1789 my $item3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1793 # order_by homebranch.branchname
1794 $item1->discard_changes->update( { homebranch => $library_z->branchcode } );
1795 $item2->discard_changes->update( { homebranch => $library_a->branchcode } );
1796 $item3->discard_changes->update( { homebranch => $library_z->branchcode } );
1797 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1798 [ $item2->itemnumber, $item1->itemnumber, $item3->itemnumber ],
1799 "not a serial - order by homebranch" );
1801 # order_by me.enumchron
1802 $biblio->items->update( { homebranch => $library_a->branchcode } );
1803 $item1->discard_changes->update( { enumchron => 'cc' } );
1804 $item2->discard_changes->update( { enumchron => 'bb' } );
1805 $item3->discard_changes->update( { enumchron => 'aa' } );
1806 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1807 [ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ],
1808 "not a serial - order by enumchron" );
1810 # order_by LPAD( me.copynumber, 8, '0' )
1811 $biblio->items->update( { enumchron => undef } );
1812 $item1->discard_changes->update( { copynumber => '12345678' } );
1813 $item2->discard_changes->update( { copynumber => '34567890' } );
1814 $item3->discard_changes->update( { copynumber => '23456789' } );
1815 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1816 [ $item1->itemnumber, $item3->itemnumber, $item2->itemnumber ],
1817 "not a serial - order by LPAD( me.copynumber, 8, '0' )" );
1819 # order_by -desc => 'me.dateaccessioned'
1820 $biblio->items->update( { copynumber => undef } );
1821 $item1->discard_changes->update( { dateaccessioned => '2022-08-19' } );
1822 $item2->discard_changes->update( { dateaccessioned => '2022-07-19' } );
1823 $item3->discard_changes->update( { dateaccessioned => '2022-09-19' } );
1824 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1825 [ $item3->itemnumber, $item1->itemnumber, $item2->itemnumber ],
1826 "not a serial - order by date accessioned desc" );
1831 my $sub_freq = $builder->build( { source => 'SubscriptionFrequency' } );
1833 $builder->build( { source => 'SubscriptionNumberpattern' } );
1834 my $subscription = $builder->build_object(
1836 class => 'Koha::Subscriptions',
1838 biblionumber => $biblio->biblionumber,
1839 periodicity => $sub_freq->{id},
1840 numberpattern => $sub_np->{id},
1841 published_on_template => "[% publisheddatetext %] [% biblionumber %]",
1845 $builder->build_object(
1847 class => 'Koha::Subscription::Histories',
1849 subscriptionid => $subscription->subscriptionid,
1850 biblionumber => $biblio->biblionumber
1855 $biblio->update( { serial => 1 } );
1857 C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1858 $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1859 "notes", "routingnotes" );
1860 C4::Serials::AddItem2Serial( $serialid1, $item1->itemnumber );
1862 C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1863 $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1864 "notes", "routingnotes" );
1865 C4::Serials::AddItem2Serial( $serialid2, $item2->itemnumber );
1867 C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1868 $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1869 "notes", "routingnotes" );
1870 C4::Serials::AddItem2Serial( $serialid3, $item3->itemnumber );
1871 my $serial1 = Koha::Serials->find($serialid1);
1872 my $serial2 = Koha::Serials->find($serialid2);
1873 my $serial3 = Koha::Serials->find($serialid3);
1875 # order_by serial.publisheddate
1876 $serial1->discard_changes->update( { publisheddate => '2022-09-19' } );
1877 $serial2->discard_changes->update( { publisheddate => '2022-07-19' } );
1878 $serial3->discard_changes->update( { publisheddate => '2022-08-19' } );
1880 [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1881 [ $item2->itemnumber, $item3->itemnumber, $item1->itemnumber ],
1882 "serial - order by publisheddate"
1885 # order_by me.enumchron
1886 $serial1->discard_changes->update({ publisheddate => '2022-08-19' });
1887 $serial2->discard_changes->update({ publisheddate => '2022-08-19' });
1888 $serial3->discard_changes->update({ publisheddate => '2022-08-19' });
1889 $item1->discard_changes->update( { enumchron => 'cc' } );
1890 $item2->discard_changes->update( { enumchron => 'bb' } );
1891 $item3->discard_changes->update( { enumchron => 'aa' } );
1892 is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1893 [ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ],
1894 "serial - order by enumchron" );
1896 is( $serial1->publisheddatetext, "publisheddatetext " . $biblio->biblionumber, "Column publisheddatetext rendered correctly from template for serial1" );
1897 is( $serial2->publisheddatetext, "publisheddatetext " . $biblio->biblionumber, "Column publisheddatetext rendered correctly from template for serial2" );
1898 is( $serial3->publisheddatetext, "publisheddatetext " . $biblio->biblionumber, "Column publisheddatetext rendered correctly from template for serial3" );
1902 $schema->storage->txn_rollback;
1906 subtest 'filter_by_for_hold' => sub {
1910 $schema->storage->txn_begin;
1912 my $biblio = $builder->build_sample_biblio;
1913 my $library = $builder->build_object({ class => 'Koha::Libraries' });
1915 t::lib::Mocks::mock_preference('IndependentBranches', 0); # more robust tests
1917 is( $biblio->items->filter_by_for_hold->count, 0, 'no item yet' );
1918 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } );
1919 is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' );
1920 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } );
1921 is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' );
1922 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
1923 is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' );
1925 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 0, library => $library->id } );
1926 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 1, library => $library->id } );
1927 is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' );
1929 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 0, library => $library->id } );
1930 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 1, library => $library->id } );
1931 is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' );
1933 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } );
1934 $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } );
1935 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0);
1936 is( $biblio->items->filter_by_for_hold->count, 5, '5 items for hold - not damaged if not AllowHoldsOnDamagedItems' );
1937 t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1);
1938 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' );
1940 my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
1941 my $not_holdable_itemtype = $itemtype->itemtype;
1942 $builder->build_sample_item(
1944 biblionumber => $biblio->biblionumber,
1945 itype => $not_holdable_itemtype,
1948 Koha::CirculationRules->set_rule(
1950 branchcode => undef,
1951 itemtype => $not_holdable_itemtype,
1952 rule_name => 'holdallowed',
1953 rule_value => 'not_allowed',
1956 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - holdallowed=not_allowed' );
1958 # Remove rule, test notforloan on itemtype
1959 Koha::CirculationRules->set_rule(
1961 branchcode => undef,
1962 itemtype => $not_holdable_itemtype,
1963 rule_name => 'holdallowed',
1964 rule_value => undef,
1967 is( $biblio->items->filter_by_for_hold->count, 7, '7 items for hold - rule deleted' );
1968 $itemtype->notforloan(1)->store;
1969 is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - notforloan' );
1972 my $mock_context = Test::MockModule->new('C4::Context');
1973 $mock_context->mock( 'only_my_library', 1 );
1974 $mock_context->mock( 'mybranch', $library->id );
1975 is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold, filtered by IndependentBranches' );
1978 t::lib::Mocks::mock_preference('item-level_itypes', 0);
1979 $biblio->biblioitem->itemtype($not_holdable_itemtype)->store;
1980 is( $biblio->items->filter_by_for_hold->count, 0, '0 item-level_itypes=0' );
1982 t::lib::Mocks::mock_preference('item-level_itypes', 1);
1984 $schema->storage->txn_rollback;