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

(-)a/Koha/Exceptions/Item/Bundle.pm (+4 lines)
Lines 27-32 use Exception::Class ( Link Here
27
        isa         => 'Koha::Exceptions::Item::Bundle',
27
        isa         => 'Koha::Exceptions::Item::Bundle',
28
        description => "A bundle cannot be added to a bundle",
28
        description => "A bundle cannot be added to a bundle",
29
    },
29
    },
30
    'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut' => {
31
        isa         => 'Koha::Exceptions::Item::Bundle',
32
        description => 'Someone tried to add an item to a checked out bundle',
33
    },
30
    'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut' => {
34
    'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut' => {
31
        isa         => 'Koha::Exceptions::Item::Bundle',
35
        isa         => 'Koha::Exceptions::Item::Bundle',
32
        description => 'Someone tried to add a checked out item to a bundle',
36
        description => 'Someone tried to add a checked out item to a bundle',
(-)a/Koha/Item.pm (+9 lines)
Lines 1700-1705 sub add_to_bundle { Link Here
1700
    try {
1700
    try {
1701
        $schema->txn_do(
1701
        $schema->txn_do(
1702
            sub {
1702
            sub {
1703
1704
                Koha::Exceptions::Item::Bundle::BundleIsCheckedOut->throw if $self->checkout;
1705
1703
                my $checkout = $bundle_item->checkout;
1706
                my $checkout = $bundle_item->checkout;
1704
                if ($checkout) {
1707
                if ($checkout) {
1705
                    unless ($options->{force_checkin}) {
1708
                    unless ($options->{force_checkin}) {
Lines 1785-1790 Remove this item from any bundle it may have been attached to. Link Here
1785
sub remove_from_bundle {
1788
sub remove_from_bundle {
1786
    my ($self) = @_;
1789
    my ($self) = @_;
1787
1790
1791
    my $bundle_host = $self->bundle_host;
1792
1793
    return 0 unless $bundle_host; # Should not we raise an exception here?
1794
1795
    Koha::Exceptions::Item::Bundle::BundleIsCheckedOut->throw if $bundle_host->checkout;
1796
1788
    my $bundle_item_rs = $self->_result->item_bundles_item;
1797
    my $bundle_item_rs = $self->_result->item_bundles_item;
1789
    if ( $bundle_item_rs ) {
1798
    if ( $bundle_item_rs ) {
1790
        $bundle_item_rs->delete;
1799
        $bundle_item_rs->delete;
(-)a/Koha/REST/V1/Items.pm (-5 / +29 lines)
Lines 327-332 sub add_to_bundle { Link Here
327
                }
327
                }
328
            );
328
            );
329
        }
329
        }
330
        elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut' )
331
        {
332
            return $c->render(
333
                status  => 409,
334
                openapi => {
335
                    error      => 'Bundle is checked out',
336
                    error_code => 'bundle_checked_out'
337
                }
338
            );
339
        }
330
        elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut' )
340
        elsif ( ref($_) eq 'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut' )
331
        {
341
        {
332
            return $c->render(
342
            return $c->render(
Lines 400-410 sub remove_from_bundle { Link Here
400
        );
410
        );
401
    }
411
    }
402
412
403
    $bundle_item->remove_from_bundle;
413
    return try {
404
    return $c->render(
414
        $bundle_item->remove_from_bundle;
405
        status  => 204,
415
        return $c->render(
406
        openapi => q{}
416
            status  => 204,
407
    );
417
            openapi => q{}
418
        );
419
    } catch {
420
        if ( ref($_) eq 'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut' ) {
421
            return $c->render(
422
                status  => 409,
423
                openapi => {
424
                    error      => 'Bundle is checked out',
425
                    error_code => 'bundle_checked_out'
426
                }
427
            );
428
        } else {
429
            $c->unhandled_exception($_);
430
        }
431
    };
408
}
432
}
409
433
410
1;
434
1;
(-)a/t/db_dependent/Koha/Item.t (-7 / +21 lines)
Lines 214-220 subtest 'bundle_host tests' => sub { Link Here
214
};
214
};
215
215
216
subtest 'add_to_bundle tests' => sub {
216
subtest 'add_to_bundle tests' => sub {
217
    plan tests => 11;
217
    plan tests => 12;
218
218
219
    $schema->storage->txn_begin;
219
    $schema->storage->txn_begin;
220
220
Lines 263-268 subtest 'add_to_bundle tests' => sub { Link Here
263
    'Koha::Exceptions::Item::Bundle::IsBundle',
263
    'Koha::Exceptions::Item::Bundle::IsBundle',
264
      'Exception thrown if you try to add a bundle host to a bundle item';
264
      'Exception thrown if you try to add a bundle host to a bundle item';
265
265
266
    C4::Circulation::AddIssue( $patron->unblessed, $host_item->barcode );
267
    throws_ok { $host_item->add_to_bundle($bundle_item2) }
268
    'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut',
269
      'Exception thrown if you try to add an item to a checked out bundle';
270
    C4::Circulation::AddReturn( $host_item->barcode, $host_item->homebranch );
271
    $host_item->discard_changes;
272
266
    C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
273
    C4::Circulation::AddIssue( $patron->unblessed, $bundle_item2->barcode );
267
    throws_ok { $host_item->add_to_bundle($bundle_item2) }
274
    throws_ok { $host_item->add_to_bundle($bundle_item2) }
268
    'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
275
    'Koha::Exceptions::Item::Bundle::ItemIsCheckedOut',
Lines 286-304 subtest 'add_to_bundle tests' => sub { Link Here
286
};
293
};
287
294
288
subtest 'remove_from_bundle tests' => sub {
295
subtest 'remove_from_bundle tests' => sub {
289
    plan tests => 3;
296
    plan tests => 4;
290
297
291
    $schema->storage->txn_begin;
298
    $schema->storage->txn_begin;
292
299
293
    my $host_item = $builder->build_sample_item();
300
    my $host_item = $builder->build_sample_item();
294
    my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
301
    my $bundle_item1 = $builder->build_sample_item({ notforloan => 1 });
295
    $schema->resultset('ItemBundle')
302
    $host_item->add_to_bundle($bundle_item1);
296
      ->create(
303
297
        { host => $host_item->itemnumber, item => $bundle_item1->itemnumber } );
304
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
305
    t::lib::Mocks::mock_userenv( { branchcode => $patron->branchcode } );
306
307
    C4::Circulation::AddIssue( $patron->unblessed, $host_item->barcode );
308
    throws_ok { $bundle_item1->remove_from_bundle }
309
    'Koha::Exceptions::Item::Bundle::BundleIsCheckedOut',
310
      'Exception thrown if you try to add an item to a checked out bundle';
311
    my ( $doreturn, $messages, $issue ) = C4::Circulation::AddReturn( $host_item->barcode, $host_item->homebranch );
312
    $bundle_item1->discard_changes;
298
313
299
    is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
314
    is($bundle_item1->remove_from_bundle(), 1, 'remove_from_bundle returns 1 when item is removed from a bundle');
300
    is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
315
    is($bundle_item1->notforloan, 0, 'remove_from_bundle resets notforloan to 0');
301
    $bundle_item1 = $bundle_item1->get_from_storage;
316
    $bundle_item1->discard_changes;
302
    is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
317
    is($bundle_item1->remove_from_bundle(), 0, 'remove_from_bundle returns 0 when item is not in a bundle');
303
318
304
    $schema->storage->txn_rollback;
319
    $schema->storage->txn_rollback;
305
- 

Return to bug 33817