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

(-)a/Koha/Exceptions/Hold.pm (+4 lines)
Lines 23-28 use Exception::Class ( Link Here
23
    'Koha::Exceptions::Hold' => {
23
    'Koha::Exceptions::Hold' => {
24
        isa => 'Koha::Exception',
24
        isa => 'Koha::Exception',
25
    },
25
    },
26
    'Koha::Exceptions::Hold::CannotChangeHoldType' => {
27
        isa         => 'Koha::Exceptions::Hold',
28
        description => "Record cannot have both record and item level holds at the same time",
29
    },
26
    'Koha::Exceptions::Hold::CannotSuspendFound' => {
30
    'Koha::Exceptions::Hold::CannotSuspendFound' => {
27
        isa         => 'Koha::Exceptions::Hold',
31
        isa         => 'Koha::Exceptions::Hold',
28
        description => "Found holds cannot be suspended",
32
        description => "Found holds cannot be suspended",
(-)a/Koha/Hold.pm (+33 lines)
Lines 844-849 sub fill { Link Here
844
    return $self;
844
    return $self;
845
}
845
}
846
846
847
=head3 sub change_type
848
849
    $hold->change_type                # to record level
850
    $hold->change_type( $itemnumber ) # to item level
851
852
Changes hold type between record and item level holds, only if record has
853
exactly one hold for a patron. This is because Koha expects all holds for
854
a patron on a record to be alike.
855
856
=cut
857
858
sub change_type {
859
    my ( $self, $itemnumber ) = @_;
860
861
    my $record_holds_per_patron = Koha::Holds->search( {
862
        borrowernumber => $self->borrowernumber,
863
        biblionumber => $self->biblionumber,
864
    } );
865
866
    if ( $itemnumber && $self->itemnumber ) {
867
        $self->itemnumber( $itemnumber )->store;
868
        return $self;
869
    }
870
871
    if ( $record_holds_per_patron->count == 1 ) {
872
        $self->set({ itemnumber => $itemnumber ? $itemnumber : undef })->store;
873
    } else {
874
        Koha::Exceptions::Hold::CannotChangeHoldType->throw();
875
    }
876
877
    return $self;
878
}
879
847
=head3 store
880
=head3 store
848
881
849
Override base store method to set default
882
Override base store method to set default
(-)a/t/db_dependent/Koha/Hold.t (-2 / +60 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 12;
23
23
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 953-957 subtest 'Koha::Hold::item_group tests' => sub { Link Here
953
953
954
    is( $hold->item_group->id, $item_group->id, "Got correct item group" );
954
    is( $hold->item_group->id, $item_group->id, "Got correct item group" );
955
955
956
};
957
958
subtest 'change_type tests' => sub {
959
960
    plan tests => 9;
961
962
    $schema->storage->txn_begin;
963
964
    my $item = $builder->build_object( { class => 'Koha::Items', } );
965
    my $hold = $builder->build_object( {
966
        class => 'Koha::Holds',
967
        value => {
968
            itemnumber => undef,
969
        }
970
    } );
971
972
    my $hold2 = $builder->build_object( {
973
        class => 'Koha::Holds',
974
        value => {
975
            borrowernumber => $hold->borrowernumber,
976
        }
977
    } );
978
979
    ok( $hold->change_type );
980
981
    $hold->discard_changes;
982
983
    is( $hold->itemnumber, undef, 'record hold to record hold, no changes');
984
985
    ok( $hold->change_type( $item->itemnumber ) );
986
987
    $hold->discard_changes;
988
989
    is( $hold->itemnumber, $item->itemnumber, 'record hold to item hold');
990
991
    ok( $hold->change_type( $item->itemnumber ) );
992
993
    $hold->discard_changes;
994
995
    is( $hold->itemnumber, $item->itemnumber, 'item hold to item hold, no changes');
996
997
    ok( $hold->change_type );
998
999
    $hold->discard_changes;
1000
1001
    is( $hold->itemnumber, undef, 'item hold to record hold');
1002
1003
    my $hold3 = $builder->build_object( {
1004
        class => 'Koha::Holds',
1005
        value => {
1006
            biblionumber => $hold->biblionumber,
1007
            borrowernumber => $hold->borrowernumber,
1008
        }
1009
    } );
1010
1011
    throws_ok { $hold->change_type }
1012
        'Koha::Exceptions::Hold::CannotChangeHoldType',
1013
          'Exception thrown because more than one hold per record';
1014
956
    $schema->storage->txn_rollback;
1015
    $schema->storage->txn_rollback;
957
};
1016
};
958
- 

Return to bug 31692