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 1722-1727 sub add_to_bundle { Link Here
1722
    try {
1722
    try {
1723
        $schema->txn_do(
1723
        $schema->txn_do(
1724
            sub {
1724
            sub {
1725
1726
                Koha::Exceptions::Item::Bundle::BundleIsCheckedOut->throw if $self->checkout;
1727
1725
                my $checkout = $bundle_item->checkout;
1728
                my $checkout = $bundle_item->checkout;
1726
                if ($checkout) {
1729
                if ($checkout) {
1727
                    unless ($options->{force_checkin}) {
1730
                    unless ($options->{force_checkin}) {
Lines 1807-1812 Remove this item from any bundle it may have been attached to. Link Here
1807
sub remove_from_bundle {
1810
sub remove_from_bundle {
1808
    my ($self) = @_;
1811
    my ($self) = @_;
1809
1812
1813
    my $bundle_host = $self->bundle_host;
1814
1815
    return 0 unless $bundle_host; # Should not we raise an exception here?
1816
1817
    Koha::Exceptions::Item::Bundle::BundleIsCheckedOut->throw if $bundle_host->checkout;
1818
1810
    my $bundle_item_rs = $self->_result->item_bundles_item;
1819
    my $bundle_item_rs = $self->_result->item_bundles_item;
1811
    if ( $bundle_item_rs ) {
1820
    if ( $bundle_item_rs ) {
1812
        $bundle_item_rs->delete;
1821
        $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