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

(-)a/Koha/Item.pm (-4 / +11 lines)
Lines 528-534 sub bookings { Link Here
528
528
529
=head3 find_booking
529
=head3 find_booking
530
530
531
Find the first booking that would conflict with the passed checkout dates
531
  my $booking = $item->find_booking( { checkout_date => $now, due_date => $future_date } );
532
533
Find the first booking that would conflict with the passed checkout dates for this item.
534
535
FIXME: This can be simplified, it was originally intended to iterate all biblio level bookings
536
to catch cases where this item may be the last available to satisfy a biblio level only booking.
537
However, we dropped the biblio level functionality prior to push as bugs were found in it's
538
implimentation.
532
539
533
=cut
540
=cut
534
541
Lines 542-548 sub find_booking { Link Here
542
    my $dtf      = Koha::Database->new->schema->storage->datetime_parser;
549
    my $dtf      = Koha::Database->new->schema->storage->datetime_parser;
543
    my $bookings = $biblio->bookings(
550
    my $bookings = $biblio->bookings(
544
        [
551
        [
545
            # Checkout starts during booked period
552
            # Proposed checkout starts during booked period
546
            start_date => {
553
            start_date => {
547
                '-between' => [
554
                '-between' => [
548
                    $dtf->format_datetime($checkout_date),
555
                    $dtf->format_datetime($checkout_date),
Lines 550-556 sub find_booking { Link Here
550
                ]
557
                ]
551
            },
558
            },
552
559
553
            # Checkout is due during booked period
560
            # Proposed checkout is due during booked period
554
            end_date => {
561
            end_date => {
555
                '-between' => [
562
                '-between' => [
556
                    $dtf->format_datetime($checkout_date),
563
                    $dtf->format_datetime($checkout_date),
Lines 558-564 sub find_booking { Link Here
558
                ]
565
                ]
559
            },
566
            },
560
567
561
            # Checkout contains booked period
568
            # Proposed checkout would contain the booked period
562
            {
569
            {
563
                start_date => { '<' => $dtf->format_datetime($checkout_date) },
570
                start_date => { '<' => $dtf->format_datetime($checkout_date) },
564
                end_date   => { '>' => $dtf->format_datetime($due_date) }
571
                end_date   => { '>' => $dtf->format_datetime($due_date) }
(-)a/t/db_dependent/Koha/Item.t (-2 / +112 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
use utf8;
21
use utf8;
22
22
23
use Test::More tests => 32;
23
use Test::More tests => 33;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
26
26
Lines 2408-2410 subtest 'bookings' => sub { Link Here
2408
2408
2409
    $schema->storage->txn_rollback;
2409
    $schema->storage->txn_rollback;
2410
};
2410
};
2411
- 
2411
2412
subtest 'find_booking' => sub {
2413
    plan tests => 6;
2414
2415
    $schema->storage->txn_begin;
2416
2417
    my $biblio        = $builder->build_sample_biblio();
2418
    my $item          = $builder->build_sample_item( { biblionumber => $biblio->biblionumber, bookable => 1 } );
2419
    my $found_booking = $item->find_booking(
2420
        {
2421
            checkout_date => dt_from_string(),
2422
            due_date      => dt_from_string()->add( days => 7 ),
2423
        }
2424
    );
2425
2426
    is(
2427
        $found_booking, undef,
2428
        "Nothing returned from Koha::Item->find_booking if there are no bookings"
2429
    );
2430
2431
    my $start_1 = dt_from_string()->subtract( days => 7 );
2432
    my $end_1   = dt_from_string()->subtract( days => 1 );
2433
    my $start_2 = dt_from_string();
2434
    my $end_2   = dt_from_string()->add( days => 7 );
2435
    my $start_3 = dt_from_string()->add( days => 8 );
2436
    my $end_3   = dt_from_string()->add( days => 16 );
2437
2438
    # Past booking
2439
    my $booking1 = $builder->build_object(
2440
        {
2441
            class => 'Koha::Bookings',
2442
            value => {
2443
                biblio_id  => $biblio->biblionumber,
2444
                item_id    => $item->itemnumber,
2445
                start_date => $start_1,
2446
                end_date   => $end_1
2447
            }
2448
        }
2449
    );
2450
2451
    $found_booking = $item->find_booking(
2452
        {
2453
            checkout_date => dt_from_string(),
2454
            due_date      => dt_from_string()->add( days => 7 ),
2455
        }
2456
    );
2457
2458
    is(
2459
        $found_booking,
2460
        undef,
2461
        "Koha::Item->find_booking returns undefined if the passed dates do not conflict with any item bookings"
2462
    );
2463
2464
    # Current booking
2465
    my $booking2 = $builder->build_object(
2466
        {
2467
            class => 'Koha::Bookings',
2468
            value => {
2469
                biblio_id  => $biblio->biblionumber,
2470
                item_id    => $item->itemnumber,
2471
                start_date => $start_2,
2472
                end_date   => $end_2
2473
            }
2474
        }
2475
    );
2476
2477
    $found_booking = $item->find_booking(
2478
        {
2479
            checkout_date => dt_from_string(),
2480
            due_date      => dt_from_string()->add( days => 7 ),
2481
        }
2482
    );
2483
    is(
2484
        ref($found_booking),
2485
        'Koha::Booking',
2486
        "Koha::Item->find_booking returns a Koha::Booking if one exists that would clash with the passed dates"
2487
    );
2488
    is( $found_booking->booking_id, $booking2->booking_id, "Koha::Item->find_booking returns the current booking" );
2489
2490
    # Future booking
2491
    my $booking3 = $builder->build_object(
2492
        {
2493
            class => 'Koha::Bookings',
2494
            value => {
2495
                biblio_id  => $biblio->biblionumber,
2496
                item_id    => $item->itemnumber,
2497
                start_date => $start_3,
2498
                end_date   => $end_3
2499
            }
2500
        }
2501
    );
2502
2503
    $found_booking = $item->find_booking(
2504
        {
2505
            checkout_date => dt_from_string(),
2506
            due_date      => dt_from_string()->add( days => 7 ),
2507
        }
2508
    );
2509
2510
    is(
2511
        ref($found_booking),
2512
        'Koha::Booking',
2513
        "Koha::Item->find_booking returns a Koha::Booking if one exists that would clash with the passed dates"
2514
    );
2515
    is(
2516
        $found_booking->booking_id, $booking2->booking_id,
2517
        "Koha::Item->find_booking returns the current booking not a future one"
2518
    );
2519
2520
    $schema->storage->txn_rollback;
2521
};

Return to bug 35248