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 / +61 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 955-957 subtest 'Koha::Hold::item_group tests' => sub { Link Here
955
955
956
    $schema->storage->txn_rollback;
956
    $schema->storage->txn_rollback;
957
};
957
};
958
- 
958
959
subtest 'change_type tests' => sub {
960
961
    plan tests => 9;
962
963
    $schema->storage->txn_begin;
964
965
    my $item = $builder->build_object( { class => 'Koha::Items', } );
966
    my $hold = $builder->build_object( {
967
        class => 'Koha::Holds',
968
        value => {
969
            itemnumber => undef,
970
        }
971
    } );
972
973
    my $hold2 = $builder->build_object( {
974
        class => 'Koha::Holds',
975
        value => {
976
            borrowernumber => $hold->borrowernumber,
977
        }
978
    } );
979
980
    ok( $hold->change_type );
981
982
    $hold->discard_changes;
983
984
    is( $hold->itemnumber, undef, 'record hold to record hold, no changes');
985
986
    ok( $hold->change_type( $item->itemnumber ) );
987
988
    $hold->discard_changes;
989
990
    is( $hold->itemnumber, $item->itemnumber, 'record hold to item hold');
991
992
    ok( $hold->change_type( $item->itemnumber ) );
993
994
    $hold->discard_changes;
995
996
    is( $hold->itemnumber, $item->itemnumber, 'item hold to item hold, no changes');
997
998
    ok( $hold->change_type );
999
1000
    $hold->discard_changes;
1001
1002
    is( $hold->itemnumber, undef, 'item hold to record hold');
1003
1004
    my $hold3 = $builder->build_object( {
1005
        class => 'Koha::Holds',
1006
        value => {
1007
            biblionumber => $hold->biblionumber,
1008
            borrowernumber => $hold->borrowernumber,
1009
        }
1010
    } );
1011
1012
    throws_ok { $hold->change_type }
1013
        'Koha::Exceptions::Hold::CannotChangeHoldType',
1014
          'Exception thrown because more than one hold per record';
1015
1016
    $schema->storage->txn_rollback;
1017
};

Return to bug 31692