3 # Copyright 2017 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 => 12;
25 use t::lib::TestBuilder;
28 use C4::Circulation qw( AddIssue AddReturn );
32 use Koha::DateUtils qw(dt_from_string);
35 my $schema = Koha::Database->schema;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'basket() tests' => sub {
42 $schema->storage->txn_begin;
44 my $basket = $builder->build_object(
46 class => 'Koha::Acquisition::Baskets'
49 my $order = $builder->build_object(
51 class => 'Koha::Acquisition::Orders',
52 value => { basketno => $basket->basketno }
56 my $retrieved_basket = $order->basket;
57 is( ref($retrieved_basket), 'Koha::Acquisition::Basket',
58 'Type is correct for ->basket' );
59 is_deeply( $retrieved_basket->unblessed,
60 $basket->unblessed, "Correct basket found and updated" );
62 $schema->storage->txn_rollback;
65 subtest 'biblio() tests' => sub {
69 $schema->storage->txn_begin;
71 my $order = $builder->build_object(
73 class => 'Koha::Acquisition::Orders',
74 value => { biblionumber => undef }
78 is( $order->biblio, undef, 'If no linked biblio, undef is returned' );
80 # Add and link a biblio to the order
81 my $biblio = $builder->build_sample_biblio();
82 $order->set({ biblionumber => $biblio->biblionumber })->store->discard_changes;
84 my $THE_biblio = $order->biblio;
85 is( ref($THE_biblio), 'Koha::Biblio', 'Returns a Koha::Biblio object' );
86 is( $THE_biblio->biblionumber, $biblio->biblionumber, 'It is not cheating about the object' );
88 $order->biblio->delete;
89 $order = Koha::Acquisition::Orders->find($order->ordernumber);
90 ok( $order, 'The order is not deleted if the biblio is deleted' );
91 is( $order->biblio, undef, 'order.biblio is correctly set to NULL when the biblio is deleted' );
93 $schema->storage->txn_rollback;
96 subtest 'store' => sub {
99 $schema->storage->txn_begin;
100 my $o = $builder->build_object(
102 class => 'Koha::Acquisition::Orders'
106 subtest 'entrydate' => sub {
111 t::lib::Mocks::mock_preference( 'TimeFormat', '12hr' );
112 $order = Koha::Acquisition::Order->new(
114 basketno => $o->basketno,
115 biblionumber => $o->biblionumber,
116 budget_id => $o->budget_id,
120 $order->discard_changes;
121 like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
123 t::lib::Mocks::mock_preference( 'TimeFormat', '24hr' );
124 $order = Koha::Acquisition::Order->new(
126 basketno => $o->basketno,
127 biblionumber => $o->biblionumber,
128 budget_id => $o->budget_id,
132 $order->discard_changes;
133 like( $order->entrydate, qr|^\d{4}-\d{2}-\d{2}$| );
135 $schema->storage->txn_rollback;
138 subtest 'fund' => sub {
141 $schema->storage->txn_begin;
142 my $o = $builder->build_object(
144 class => 'Koha::Acquisition::Orders',
148 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
149 is( ref( $order->fund ),
150 'Koha::Acquisition::Fund',
151 '->fund should return a Koha::Acquisition::Fund object' );
152 $schema->storage->txn_rollback;
155 subtest 'invoice' => sub {
158 $schema->storage->txn_begin;
159 my $o = $builder->build_object(
161 class => 'Koha::Acquisition::Orders',
162 value => { cancellationreason => 'XXXXXXXX', invoiceid => undef }, # not received yet
166 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
167 is( $order->invoice, undef,
168 '->invoice should return undef if no invoice defined yet');
170 my $invoice = $builder->build_object(
172 class => 'Koha::Acquisition::Invoices',
176 $o->invoiceid( $invoice->invoiceid )->store;
177 $order = Koha::Acquisition::Orders->find( $o->ordernumber );
178 is( ref( $order->invoice ), 'Koha::Acquisition::Invoice',
179 '->invoice should return a Koha::Acquisition::Invoice object if an invoice is defined');
181 $schema->storage->txn_rollback;
184 subtest 'subscription' => sub {
187 $schema->storage->txn_begin;
188 my $o = $builder->build_object(
190 class => 'Koha::Acquisition::Orders',
191 value => { subscriptionid => undef }, # not linked to a subscription
195 my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
196 is( $order->subscription, undef,
197 '->subscription should return undef if not created from a subscription');
199 $o = $builder->build_object(
201 class => 'Koha::Acquisition::Orders',
202 # Will be linked to a subscription by TestBuilder
206 $order = Koha::Acquisition::Orders->find( $o->ordernumber );
207 is( ref( $order->subscription ), 'Koha::Subscription',
208 '->subscription should return a Koha::Subscription object if created from a subscription');
210 $schema->storage->txn_rollback;
213 subtest 'duplicate_to | add_item' => sub {
216 $schema->storage->txn_begin;
218 my $item = $builder->build_sample_item;
219 my $order_no_sub = $builder->build_object(
221 class => 'Koha::Acquisition::Orders',
224 biblionumber => $item->biblionumber,
225 subscriptionid => undef, # not linked to a subscription
229 $order_no_sub->basket->create_items(undef)->store; # use syspref
230 $order_no_sub->add_item( $item->itemnumber );
232 $item = $builder->build_sample_item;
233 my $order_from_sub = $builder->build_object(
235 class => 'Koha::Acquisition::Orders',
238 biblionumber => $item->biblionumber,
239 # Will be linked to a subscription by TestBuilder
243 $order_from_sub->basket->create_items(undef)->store; # use syspref
244 $order_from_sub->add_item( $item->itemnumber );
246 my $basket_to = $builder->build_object(
247 { class => 'Koha::Acquisition::Baskets' });
249 subtest 'Create item on receiving' => sub {
252 t::lib::Mocks::mock_preference('AcqCreateItem', 'receiving');
254 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
255 is( $duplicated_order->items->count, 0,
256 'Items should not be copied if the original order did not create items on ordering'
259 $duplicated_order = $order_from_sub->duplicate_to($basket_to);
260 is( $duplicated_order->items->count, 0,
261 'Items should not be copied if the original order is created from a subscription'
265 subtest 'Create item on ordering' => sub {
268 t::lib::Mocks::mock_preference('AcqCreateItem', 'ordering');
270 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
271 is( $duplicated_order->items->count, 1,
272 'Items should be copied if items are created on ordering'
275 $duplicated_order = $order_from_sub->duplicate_to($basket_to);
276 is( $duplicated_order->items->count, 0,
277 'Items should never be copied if the original order is created from a subscription'
281 subtest 'Regression tests' => sub {
284 my $duplicated_order = $order_no_sub->duplicate_to($basket_to);
285 is($duplicated_order->invoiceid, undef, "invoiceid should be set to null for a new duplicated order");
288 $schema->storage->txn_rollback;
291 subtest 'current_item_level_holds() tests' => sub {
295 $schema->storage->txn_begin;
297 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
298 my $biblio = $builder->build_sample_biblio();
299 my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
300 my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
301 my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
303 C4::Reserves::AddReserve(
305 branchcode => $patron->branchcode,
306 borrowernumber => $patron->borrowernumber,
307 biblionumber => $biblio->biblionumber,
308 reservation_date => dt_from_string->add( days => -2 ),
309 itemnumber => $item_1->itemnumber,
312 C4::Reserves::AddReserve(
314 branchcode => $patron->branchcode,
315 borrowernumber => $patron->borrowernumber,
316 biblionumber => $biblio->biblionumber,
317 reservation_date => dt_from_string->add( days => -2 ),
318 itemnumber => $item_2->itemnumber,
321 # Add a hold in the future
322 C4::Reserves::AddReserve(
324 branchcode => $patron->branchcode,
325 borrowernumber => $patron->borrowernumber,
326 biblionumber => $biblio->biblionumber,
327 reservation_date => dt_from_string->add( days => 2 ),
328 itemnumber => $item_3->itemnumber,
332 # Add an order with no biblionumber
333 my $order = $builder->build_object(
335 class => 'Koha::Acquisition::Orders',
337 biblionumber => undef
342 my $holds = $order->current_item_level_holds;
344 is( ref($holds), 'Koha::Holds', 'Koha::Holds iterator returned if no linked biblio' );
345 is( $holds->count, 0, 'Count is 0 if no linked biblio' );
347 $order->set({ biblionumber => $biblio->biblionumber })->store->discard_changes;
349 $holds = $order->current_item_level_holds;
351 is( ref($holds), 'Koha::Holds', 'Koha::Holds iterator returned if no linked items' );
352 is( $holds->count, 0, 'Count is 0 if no linked items' );
354 $order->add_item( $item_2->itemnumber );
355 $order->add_item( $item_3->itemnumber );
357 $holds = $order->current_item_level_holds;
358 is( $holds->count, 1, 'Only current (not future) holds are returned');
360 $schema->storage->txn_rollback;
363 subtest 'claim*' => sub {
366 $schema->storage->txn_begin;
367 my $order = $builder->build_object(
369 class => 'Koha::Acquisition::Orders',
373 my $now = dt_from_string;
374 is( $order->claims->count, 0, 'No claim yet, ->claims should return an empty set');
375 is( $order->claims_count, 0, 'No claim yet, ->claims_count should return 0');
376 is( $order->claimed_date, undef, 'No claim yet, ->claimed_date should return undef');
378 my $claim_1 = $order->claim;
379 my $claim_2 = $order->claim;
381 $claim_1->claimed_on($now->clone->subtract(days => 1))->store;
382 $claim_2->claimed_on($now)->store;
384 is( $order->claims->count, 2, '->claims should return the correct number of claims');
385 is( $order->claims_count, 2, '->claims_count should return the correct number of claims');
386 is( dt_from_string($order->claimed_date), $now, '->claimed_date should return the date of the last claim');
388 $schema->storage->txn_rollback;
391 subtest 'filter_by_late' => sub {
394 $schema->storage->txn_begin;
395 my $now = dt_from_string;
396 my $bookseller = $builder->build_object(
398 class => 'Koha::Acquisition::Booksellers',
399 value => { deliverytime => 2 }
402 my $basket_1 = $builder->build_object(
404 class => 'Koha::Acquisition::Baskets',
406 booksellerid => $bookseller->id,
411 my $order_1 = $builder->build_object(
413 class => 'Koha::Acquisition::Orders',
415 basketno => $basket_1->basketno,
416 datereceived => undef,
417 datecancellationprinted => undef,
418 estimated_delivery_date => undef,
422 my $basket_2 = $builder->build_object( # expected tomorrow
424 class => 'Koha::Acquisition::Baskets',
426 booksellerid => $bookseller->id,
427 closedate => $now->clone->subtract( days => 1 ),
431 my $order_2 = $builder->build_object(
433 class => 'Koha::Acquisition::Orders',
435 basketno => $basket_2->basketno,
436 datereceived => undef,
437 datecancellationprinted => undef,
438 estimated_delivery_date => undef,
442 my $basket_3 = $builder->build_object( # expected yesterday (1 day)
444 class => 'Koha::Acquisition::Baskets',
446 booksellerid => $bookseller->id,
447 closedate => $now->clone->subtract( days => 3 ),
451 my $order_3 = $builder->build_object(
453 class => 'Koha::Acquisition::Orders',
455 basketno => $basket_3->basketno,
456 datereceived => undef,
457 datecancellationprinted => undef,
458 estimated_delivery_date => undef,
462 my $basket_4 = $builder->build_object( # expected 3 days ago
464 class => 'Koha::Acquisition::Baskets',
466 booksellerid => $bookseller->id,
467 closedate => $now->clone->subtract( days => 5 ),
471 my $order_4 = $builder->build_object(
473 class => 'Koha::Acquisition::Orders',
475 basketno => $basket_4->basketno,
476 datereceived => undef,
477 datecancellationprinted => undef,
478 estimated_delivery_date => undef,
483 my $orders = Koha::Acquisition::Orders->search(
487 $order_1->ordernumber, $order_2->ordernumber,
488 $order_3->ordernumber, $order_4->ordernumber,
494 my $late_orders = $orders->filter_by_lates;
495 is( $late_orders->count, 3 );
497 $late_orders = $orders->filter_by_lates( { delay => 0 } );
498 is( $late_orders->count, 3 );
500 $late_orders = $orders->filter_by_lates( { delay => 1 } );
501 is( $late_orders->count, 3 );
503 $late_orders = $orders->filter_by_lates( { delay => 3 } );
504 is( $late_orders->count, 2 );
506 $late_orders = $orders->filter_by_lates( { delay => 4 } );
507 is( $late_orders->count, 1 );
509 $late_orders = $orders->filter_by_lates( { delay => 5 } );
510 is( $late_orders->count, 1 );
512 $late_orders = $orders->filter_by_lates( { delay => 6 } );
513 is( $late_orders->count, 0 );
515 $late_orders = $orders->filter_by_lates(
516 { estimated_from => $now->clone->subtract( days => 6 ) } );
517 is( $late_orders->count, 2 );
518 is( $late_orders->next->ordernumber, $order_3->ordernumber );
520 $late_orders = $orders->filter_by_lates(
521 { estimated_from => $now->clone->subtract( days => 5 ) } );
522 is( $late_orders->count, 2 );
523 is( $late_orders->next->ordernumber, $order_3->ordernumber );
525 $late_orders = $orders->filter_by_lates(
526 { estimated_from => $now->clone->subtract( days => 4 ) } );
527 is( $late_orders->count, 2 );
528 is( $late_orders->next->ordernumber, $order_3->ordernumber );
530 $late_orders = $orders->filter_by_lates(
531 { estimated_from => $now->clone->subtract( days => 3 ) } );
532 is( $late_orders->count, 2 );
534 $late_orders = $orders->filter_by_lates(
535 { estimated_from => $now->clone->subtract( days => 1 ) } );
536 is( $late_orders->count, 1 );
538 $late_orders = $orders->filter_by_lates(
540 estimated_from => $now->clone->subtract( days => 4 ),
541 estimated_to => $now->clone->subtract( days => 3 )
544 is( $late_orders->count, 1 );
546 my $basket_5 = $builder->build_object( # closed today
548 class => 'Koha::Acquisition::Baskets',
550 booksellerid => $bookseller->id,
555 my $order_5 = $builder->build_object(
557 class => 'Koha::Acquisition::Orders',
559 basketno => $basket_4->basketno,
560 datereceived => undef,
561 datecancellationprinted => undef,
562 estimated_delivery_date => $now->clone->subtract( days => 2 ),
566 $late_orders = $orders->filter_by_lates(
568 estimated_from => $now->clone->subtract( days => 3 ),
569 estimated_to => $now->clone->subtract( days => 2 )
572 is( $late_orders->count, 1 );
574 $schema->storage->txn_rollback;
577 subtest 'filter_by_current & filter_by_cancelled' => sub {
580 $schema->storage->txn_begin;
581 my $now = dt_from_string;
582 my $order_1 = $builder->build_object(
584 class => 'Koha::Acquisition::Orders',
586 datecancellationprinted => undef,
590 my $order_2 = $builder->build_object(
592 class => 'Koha::Acquisition::Orders',
594 datecancellationprinted => undef,
598 my $order_3 = $builder->build_object(
600 class => 'Koha::Acquisition::Orders',
602 datecancellationprinted => dt_from_string,
607 my $orders = Koha::Acquisition::Orders->search(
611 $order_1->ordernumber, $order_2->ordernumber,
612 $order_3->ordernumber,
618 is( $orders->filter_by_current->count, 2);
619 is( $orders->filter_by_cancelled->count, 1);
622 $schema->storage->txn_rollback;
625 subtest 'cancel() tests' => sub {
629 $schema->storage->txn_begin;
631 my $reason = 'Some reason';
634 # * order with one item attached
635 # * the item is on loan
636 # * delete_biblio is passed
637 # => order is not cancelled
638 # => item in order is not removed
639 # => biblio in order is not removed
640 # => message about not being able to delete
642 my $item = $builder->build_sample_item;
643 my $biblio_id = $item->biblionumber;
644 my $order = $builder->build_object(
646 class => 'Koha::Acquisition::Orders',
648 orderstatus => 'new',
649 biblionumber => $item->biblionumber,
650 datecancellationprinted => undef,
651 cancellationreason => undef,
655 $order->add_item( $item->id );
657 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
658 t::lib::Mocks::mock_userenv({ patron => $patron });
660 # Add a checkout so deleting the item fails because od 'book_on_loan'
661 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
663 my $result = $order->cancel({ reason => $reason });
664 # refresh the order object
665 $order->discard_changes;
667 is( $result, $order, 'self is returned' );
668 is( $order->orderstatus, 'cancelled', 'Order is not marked as cancelled' );
669 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is not undef' );
670 is( $order->cancellationreason, $reason, 'cancellationreason is set' );
671 is( ref(Koha::Items->find($item->id)), 'Koha::Item', 'The item is present' );
672 is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is present' );
673 my @messages = @{ $order->object_messages };
674 is( $messages[0]->message, 'error_delitem', 'An error message is attached to the order' );
677 # * order with one item attached
678 # * the item is no longer on loan
679 # * delete_biblio not passed
680 # => order is cancelled
681 # => item in order is removed
682 # => biblio remains untouched
684 C4::Circulation::AddReturn( $item->barcode );
686 $order = Koha::Acquisition::Orders->find($order->ordernumber);
687 $order->cancel({ reason => $reason })
690 is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
691 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
692 is( $order->cancellationreason, $reason, 'cancellationreason is undef' );
693 is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
694 is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is present' );
695 @messages = @{ $order->object_messages };
696 is( scalar @messages, 0, 'No messages' );
699 # * order with one item attached
700 # * biblio has another item
701 # => order is cancelled
702 # => item in order is removed
703 # => the extra item remains untouched
704 # => biblio remains untouched
706 my $item_1 = $builder->build_sample_item;
707 $biblio_id = $item_1->biblionumber;
708 my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id });
709 $order = $builder->build_object(
711 class => 'Koha::Acquisition::Orders',
713 orderstatus => 'new',
714 biblionumber => $biblio_id,
715 datecancellationprinted => undef,
716 cancellationreason => undef,
720 $order->add_item( $item_1->id );
722 $order->cancel({ reason => $reason, delete_biblio => 1 })
725 is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
726 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
727 is( $order->cancellationreason, $reason, 'cancellationreason is undef' );
728 is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' );
729 is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The item is still present' );
730 is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
731 @messages = @{ $order->object_messages };
732 is( $messages[0]->message, 'error_delbiblio_items', 'Cannot delete biblio and it gets notified' );
735 # * order with one item attached
736 # * there's another order pointing to the biblio
737 # => order is cancelled
738 # => item in order is removed
739 # => biblio remains untouched
740 # => biblio delete error notified
742 $item = $builder->build_sample_item;
743 $biblio_id = $item->biblionumber;
744 $order = $builder->build_object(
746 class => 'Koha::Acquisition::Orders',
748 orderstatus => 'new',
749 biblionumber => $biblio_id,
750 datecancellationprinted => undef,
751 cancellationreason => undef,
755 $order->add_item( $item->id );
758 $builder->build_object(
760 class => 'Koha::Acquisition::Orders',
762 orderstatus => 'new',
763 biblionumber => $biblio_id,
764 datecancellationprinted => undef,
765 cancellationreason => undef,
770 $order->cancel({ reason => $reason, delete_biblio => 1 })
773 is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
774 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
775 is( $order->cancellationreason, $reason, 'cancellationreason is undef' );
776 is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
777 is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
778 @messages = @{ $order->object_messages };
779 is( $messages[0]->message, 'error_delbiblio_active_orders', 'Cannot delete biblio and it gets notified' );
782 # * order with one item attached
783 # * there's a subscription on the biblio
784 # => order is cancelled
785 # => item in order is removed
786 # => biblio remains untouched
787 # => biblio delete error notified
789 $item = $builder->build_sample_item;
790 $biblio_id = $item->biblionumber;
791 $order = $builder->build_object(
793 class => 'Koha::Acquisition::Orders',
795 orderstatus => 'new',
796 biblionumber => $biblio_id,
797 datecancellationprinted => undef,
798 cancellationreason => undef,
802 $order->add_item( $item->id );
805 $builder->build_object(
807 class => 'Koha::Subscriptions',
809 biblionumber => $biblio_id,
814 $order->cancel({ reason => $reason, delete_biblio => 1 })
817 is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
818 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
819 is( $order->cancellationreason, $reason, 'cancellationreason is undef' );
820 is( Koha::Items->find($item->id), undef, 'The item is no longer present' );
821 is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
822 @messages = @{ $order->object_messages };
823 is( $messages[0]->message, 'error_delbiblio_subscriptions', 'Cannot delete biblio and it gets notified' );
826 # * order with one item attached
827 # * delete_biblio is passed
828 # => order is cancelled
829 # => item in order is removed
830 # => biblio in order is removed
832 $item = $builder->build_sample_item;
833 $biblio_id = $item->biblionumber;
834 $order = $builder->build_object(
836 class => 'Koha::Acquisition::Orders',
838 orderstatus => 'new',
839 biblionumber => $item->biblionumber,
840 datecancellationprinted => undef,
841 cancellationreason => undef,
845 $order->add_item( $item->id );
847 $order->cancel({ reason => $reason, delete_biblio => 1 })
850 is( $order->orderstatus, 'cancelled', 'Order is not marked as cancelled' );
851 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is not undef' );
852 is( $order->cancellationreason, $reason, 'cancellationreason is set' );
853 is( Koha::Items->find($item->id), undef, 'The item is not present' );
854 is( Koha::Biblios->find($biblio_id), undef, 'The biblio is not present' );
855 @messages = @{ $order->object_messages };
856 is( scalar @messages, 0, 'No errors' );
859 # * order with two items attached
860 # * one of the items is on loan
861 # => order is cancelled
862 # => item on loan is kept
863 # => the other item is removed
864 # => biblio remains untouched
865 # => biblio delete error notified
866 # => item delete error notified
868 $item_1 = $builder->build_sample_item;
869 $item_2 = $builder->build_sample_item({ biblionumber => $item_1->biblionumber });
870 my $item_3 = $builder->build_sample_item({ biblionumber => $item_1->biblionumber });
871 $biblio_id = $item_1->biblionumber;
872 $order = $builder->build_object(
874 class => 'Koha::Acquisition::Orders',
876 orderstatus => 'new',
877 biblionumber => $biblio_id,
878 datecancellationprinted => undef,
879 cancellationreason => undef,
883 $order->add_item( $item_1->id );
884 $order->add_item( $item_2->id );
885 $order->add_item( $item_3->id );
887 # Add a checkout so deleting the item fails because od 'book_on_loan'
888 C4::Circulation::AddIssue( $patron->unblessed, $item_2->barcode );
889 C4::Reserves::AddReserve(
891 branchcode => $item_3->holdingbranch,
892 borrowernumber => $patron->borrowernumber,
893 biblionumber => $biblio_id,
894 itemnumber => $item_3->id,
899 $order->cancel({ reason => $reason, delete_biblio => 1 })
902 is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
903 isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
904 is( $order->cancellationreason, $reason, 'cancellationreason is undef' );
905 is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' );
906 is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The on loan item is still present' );
907 is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
908 @messages = @{ $order->object_messages };
909 is( $messages[0]->message, 'error_delitem', 'Cannot delete on loan item' );
910 is( $messages[0]->payload->{item}->id, $item_2->id, 'Cannot delete on loan item' );
911 is( $messages[0]->payload->{reason}, 'book_on_loan', 'Item on loan notified' );
912 is( $messages[1]->message, 'error_delitem', 'Cannot delete reserved and found item' );
913 is( $messages[1]->payload->{item}->id, $item_3->id, 'Cannot delete reserved and found item' );
914 is( $messages[1]->payload->{reason}, 'book_reserved', 'Item reserved notified' );
915 is( $messages[2]->message, 'error_delbiblio_items', 'Cannot delete on loan item' );
916 is( $messages[2]->payload->{biblio}->id, $biblio_id, 'The right biblio is attached' );
918 $schema->storage->txn_rollback;