View | Details | Raw Unified | Return to bug 26515
Collapse All | Expand All

(-)a/Koha/Acquisition/Order.pm (-38 / +56 lines)
Lines 124-172 sub cancel { Link Here
124
    my $delete_biblio = $params->{delete_biblio};
124
    my $delete_biblio = $params->{delete_biblio};
125
    my $reason        = $params->{reason};
125
    my $reason        = $params->{reason};
126
126
127
    try {
127
    # Delete the related items
128
        # Delete the related items
128
    my $items = $self->items;
129
        $self->items->safe_delete;
129
    while ( my $item = $items->next ) {
130
130
        my $safe_to_delete = $item->safe_to_delete;
131
        my $biblio = $self->biblio;
131
        if ( $safe_to_delete eq '1' ) {
132
        if ( $biblio and $delete_biblio ) {
132
            $item->safe_delete;
133
133
        }
134
            if (
134
        else {
135
                $biblio->active_orders->search(
135
            $self->add_message(
136
                    { ordernumber => { '!=' => $self->ordernumber } }
136
                {
137
                )->count == 0
137
                    message => 'error_delitem',
138
                and $biblio->subscriptions->count == 0
138
                    payload => { item => $item, reason => $safe_to_delete }
139
                and $biblio->items->count == 0
140
              )
141
            {
142
143
                my $error = DelBiblio( $biblio->id );
144
                $self->add_message( { message => 'error_delbiblio'} )
145
                  if $error;
146
            }
147
            else {
148
                if ( $biblio->active_orders->search(
149
                    { ordernumber => { '!=' => $self->ordernumber } }
150
                )->count > 0 ) {
151
                    $self->add_message( { message => 'error_delbiblio_active_orders' } );
152
                }
153
                elsif ( $biblio->subscriptions->count > 0 ) {
154
                    $self->add_message( { message => 'error_delbiblio_subscriptions' } );
155
                }
156
                else { # $biblio->items->count > 0
157
                    $self->add_message( { message => 'error_delbiblio_items' } );
158
                }
139
                }
159
            }
140
            );
160
        }
141
        }
161
    }
142
    }
162
    catch {
143
163
        if ( ref($_) eq 'Koha::Exceptions::Object::CannotBeDeleted' ) {
144
    my $biblio = $self->biblio;
164
            my $object = $_->object;
145
    if ( $biblio and $delete_biblio ) {
165
            if ( ref($object) eq 'Koha::Item' ) {
146
166
                $self->add_message({ message => 'error_delitem' });
147
        if (
148
            $biblio->active_orders->search(
149
                { ordernumber => { '!=' => $self->ordernumber } }
150
            )->count == 0
151
            and $biblio->subscriptions->count == 0
152
            and $biblio->items->count == 0
153
            )
154
        {
155
156
            my $error = DelBiblio( $biblio->id );
157
            $self->add_message(
158
                {
159
                    message => 'error_delbiblio',
160
                    payload => { biblio => $biblio, reason => $error }
161
                }
162
            ) if $error;
163
        }
164
        else {
165
166
            my $message;
167
168
            if ( $biblio->active_orders->search(
169
                { ordernumber => { '!=' => $self->ordernumber } }
170
            )->count > 0 ) {
171
                $message = 'error_delbiblio_active_orders';
172
            }
173
            elsif ( $biblio->subscriptions->count > 0 ) {
174
                $message = 'error_delbiblio_subscriptions';
167
            }
175
            }
176
            else { # $biblio->items->count > 0
177
                $message = 'error_delbiblio_items';
178
            }
179
180
            $self->add_message(
181
                {
182
                    message => $message,
183
                    payload => { biblio => $biblio }
184
                }
185
            );
168
        }
186
        }
169
    };
187
    }
170
188
171
    # Update order status
189
    # Update order status
172
    $self->set(
190
    $self->set(
(-)a/t/db_dependent/Koha/Acquisition/Order.t (-10 / +69 lines)
Lines 592-598 subtest 'filter_by_current & filter_by_cancelled' => sub { Link Here
592
592
593
subtest 'cancel() tests' => sub {
593
subtest 'cancel() tests' => sub {
594
594
595
    plan tests => 38;
595
    plan tests => 52;
596
596
597
    $schema->storage->txn_begin;
597
    $schema->storage->txn_begin;
598
598
Lines 608-620 subtest 'cancel() tests' => sub { Link Here
608
    # => message about not being able to delete
608
    # => message about not being able to delete
609
609
610
    my $item      = $builder->build_sample_item;
610
    my $item      = $builder->build_sample_item;
611
    my $biblio_id = $item->biblio->id;
611
    my $biblio_id = $item->biblionumber;
612
    my $order     = $builder->build_object(
612
    my $order     = $builder->build_object(
613
        {
613
        {
614
            class => 'Koha::Acquisition::Orders',
614
            class => 'Koha::Acquisition::Orders',
615
            value => {
615
            value => {
616
                orderstatus             => 'new',
616
                orderstatus             => 'new',
617
                biblionumber            => $item->biblio->id,
617
                biblionumber            => $item->biblionumber,
618
                datecancellationprinted => undef,
618
                datecancellationprinted => undef,
619
                cancellationreason      => undef,
619
                cancellationreason      => undef,
620
            }
620
            }
Lines 625-631 subtest 'cancel() tests' => sub { Link Here
625
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
625
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
626
    t::lib::Mocks::mock_userenv({ patron => $patron });
626
    t::lib::Mocks::mock_userenv({ patron => $patron });
627
627
628
    # Add a checkout so cancelling fails because od 'book_on_loan'
628
    # Add a checkout so deleting the item fails because od 'book_on_loan'
629
    C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
629
    C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
630
630
631
    my $result = $order->cancel({ reason => $reason });
631
    my $result = $order->cancel({ reason => $reason });
Lines 672-678 subtest 'cancel() tests' => sub { Link Here
672
    # => biblio remains untouched
672
    # => biblio remains untouched
673
673
674
    my $item_1 = $builder->build_sample_item;
674
    my $item_1 = $builder->build_sample_item;
675
    $biblio_id = $item_1->biblio->id;
675
    $biblio_id = $item_1->biblionumber;
676
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id });
676
    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id });
677
    $order     = $builder->build_object(
677
    $order     = $builder->build_object(
678
        {
678
        {
Lines 708-714 subtest 'cancel() tests' => sub { Link Here
708
    # => biblio delete error notified
708
    # => biblio delete error notified
709
709
710
    $item      = $builder->build_sample_item;
710
    $item      = $builder->build_sample_item;
711
    $biblio_id = $item->biblio->id;
711
    $biblio_id = $item->biblionumber;
712
    $order     = $builder->build_object(
712
    $order     = $builder->build_object(
713
        {
713
        {
714
            class => 'Koha::Acquisition::Orders',
714
            class => 'Koha::Acquisition::Orders',
Lines 755-761 subtest 'cancel() tests' => sub { Link Here
755
    # => biblio delete error notified
755
    # => biblio delete error notified
756
756
757
    $item      = $builder->build_sample_item;
757
    $item      = $builder->build_sample_item;
758
    $biblio_id = $item->biblio->id;
758
    $biblio_id = $item->biblionumber;
759
    $order     = $builder->build_object(
759
    $order     = $builder->build_object(
760
        {
760
        {
761
            class => 'Koha::Acquisition::Orders',
761
            class => 'Koha::Acquisition::Orders',
Lines 798-810 subtest 'cancel() tests' => sub { Link Here
798
    # => biblio in order is removed
798
    # => biblio in order is removed
799
799
800
    $item      = $builder->build_sample_item;
800
    $item      = $builder->build_sample_item;
801
    $biblio_id = $item->biblio->id;
801
    $biblio_id = $item->biblionumber;
802
    $order     = $builder->build_object(
802
    $order     = $builder->build_object(
803
        {
803
        {
804
            class => 'Koha::Acquisition::Orders',
804
            class => 'Koha::Acquisition::Orders',
805
            value => {
805
            value => {
806
                orderstatus             => 'new',
806
                orderstatus             => 'new',
807
                biblionumber            => $item->biblio->id,
807
                biblionumber            => $item->biblionumber,
808
                datecancellationprinted => undef,
808
                datecancellationprinted => undef,
809
                cancellationreason      => undef,
809
                cancellationreason      => undef,
810
            }
810
            }
Lines 823-827 subtest 'cancel() tests' => sub { Link Here
823
    @messages = @{ $order->messages };
823
    @messages = @{ $order->messages };
824
    is( scalar @messages, 0, 'No errors' );
824
    is( scalar @messages, 0, 'No errors' );
825
825
826
    # Scenario:
827
    # * order with two items attached
828
    # * one of the items is on loan
829
    # => order is cancelled
830
    # => item on loan is kept
831
    # => the other item is removed
832
    # => biblio remains untouched
833
    # => biblio delete error notified
834
    # => item delete error notified
835
836
    $item_1    = $builder->build_sample_item;
837
    $item_2    = $builder->build_sample_item({ biblionumber => $item_1->biblionumber });
838
    my $item_3 = $builder->build_sample_item({ biblionumber => $item_1->biblionumber });
839
    $biblio_id = $item_1->biblionumber;
840
    $order     = $builder->build_object(
841
        {
842
            class => 'Koha::Acquisition::Orders',
843
            value => {
844
                orderstatus             => 'new',
845
                biblionumber            => $biblio_id,
846
                datecancellationprinted => undef,
847
                cancellationreason      => undef,
848
            }
849
        }
850
    );
851
    $order->add_item( $item_1->id );
852
    $order->add_item( $item_2->id );
853
    $order->add_item( $item_3->id );
854
855
    # Add a checkout so deleting the item fails because od 'book_on_loan'
856
    C4::Circulation::AddIssue( $patron->unblessed, $item_2->barcode );
857
    C4::Reserves::AddReserve(
858
        {
859
            branchcode     => $item_3->holdingbranch,
860
            borrowernumber => $patron->borrowernumber,
861
            biblionumber   => $biblio_id,
862
            itemnumber     => $item_3->id,
863
            found          => 'W',
864
        }
865
    );
866
867
    $order->cancel({ reason => $reason, delete_biblio => 1 })
868
          ->discard_changes;
869
870
    is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' );
871
    isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' );
872
    is( $order->cancellationreason, $reason, 'cancellationreason is undef' );
873
    is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' );
874
    is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The on loan item is still present' );
875
    is( ref(Koha::Biblios->find($biblio_id)), 'Koha::Biblio', 'The biblio is still present' );
876
    @messages = @{ $order->messages };
877
    is( $messages[0]->message, 'error_delitem', 'Cannot delete on loan item' );
878
    is( $messages[0]->payload->{item}->id, $item_2->id, 'Cannot delete on loan item' );
879
    is( $messages[0]->payload->{reason}, 'book_on_loan', 'Item on loan notified' );
880
    is( $messages[1]->message, 'error_delitem', 'Cannot delete reserved and found item' );
881
    is( $messages[1]->payload->{item}->id, $item_3->id, 'Cannot delete reserved and found item' );
882
    is( $messages[1]->payload->{reason}, 'book_reserved', 'Item reserved notified' );
883
    is( $messages[2]->message, 'error_delbiblio_items', 'Cannot delete on loan item' );
884
    is( $messages[2]->payload->{biblio}->id, $biblio_id, 'The right biblio is attached' );
885
826
    $schema->storage->txn_rollback;
886
    $schema->storage->txn_rollback;
827
};
887
};
828
- 

Return to bug 26515