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 806-811 sub fill { Link Here
806
    return $self;
806
    return $self;
807
}
807
}
808
808
809
=head3 sub change_type
810
811
    $hold->change_type                # to record level
812
    $hold->change_type( $itemnumber ) # to item level
813
814
Changes hold type between record and item level holds, only if record has
815
exactly one hold for a patron. This is because Koha expects all holds for
816
a patron on a record to be alike.
817
818
=cut
819
820
sub change_type {
821
    my ( $self, $itemnumber ) = @_;
822
823
    my $record_holds_per_patron = Koha::Holds->search( {
824
        borrowernumber => $self->borrowernumber,
825
        biblionumber => $self->biblionumber,
826
    } );
827
828
    if ( $itemnumber && $self->itemnumber ) {
829
        $self->itemnumber( $itemnumber )->store;
830
        return $self;
831
    }
832
833
    if ( $record_holds_per_patron->count == 1 ) {
834
        $self->set({ itemnumber => $itemnumber ? $itemnumber : undef })->store;
835
    } else {
836
        Koha::Exceptions::Hold::CannotChangeHoldType->throw();
837
    }
838
839
    return $self;
840
}
841
809
=head3 store
842
=head3 store
810
843
811
Override base store method to set default
844
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 => 10;
22
use Test::More tests => 11;
23
23
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 911-913 subtest 'can_update_pickup_location_opac() tests' => sub { Link Here
911
911
912
    $schema->storage->txn_rollback;
912
    $schema->storage->txn_rollback;
913
};
913
};
914
- 
914
915
subtest 'change_type tests' => sub {
916
917
    plan tests => 9;
918
919
    $schema->storage->txn_begin;
920
921
    my $item = $builder->build_object( { class => 'Koha::Items', } );
922
    my $hold = $builder->build_object( {
923
        class => 'Koha::Holds',
924
        value => {
925
            itemnumber => undef,
926
        }
927
    } );
928
929
    my $hold2 = $builder->build_object( {
930
        class => 'Koha::Holds',
931
        value => {
932
            borrowernumber => $hold->borrowernumber,
933
        }
934
    } );
935
936
    ok( $hold->change_type );
937
938
    $hold->discard_changes;
939
940
    is( $hold->itemnumber, undef, 'record hold to record hold, no changes');
941
942
    ok( $hold->change_type( $item->itemnumber ) );
943
944
    $hold->discard_changes;
945
946
    is( $hold->itemnumber, $item->itemnumber, 'record hold to item hold');
947
948
    ok( $hold->change_type( $item->itemnumber ) );
949
950
    $hold->discard_changes;
951
952
    is( $hold->itemnumber, $item->itemnumber, 'item hold to item hold, no changes');
953
954
    ok( $hold->change_type );
955
956
    $hold->discard_changes;
957
958
    is( $hold->itemnumber, undef, 'item hold to record hold');
959
960
    my $hold3 = $builder->build_object( {
961
        class => 'Koha::Holds',
962
        value => {
963
            biblionumber => $hold->biblionumber,
964
            borrowernumber => $hold->borrowernumber,
965
        }
966
    } );
967
968
    throws_ok { $hold->change_type }
969
        'Koha::Exceptions::Hold::CannotChangeHoldType',
970
          'Exception thrown because more than one hold per record';
971
972
    $schema->storage->txn_rollback;
973
};

Return to bug 31692