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 => 9;
22
use Test::More tests => 10;
23
23
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 863-865 subtest 'cancellation_requestable_from_opac() tests' => sub { Link Here
863
863
864
    $schema->storage->txn_rollback;
864
    $schema->storage->txn_rollback;
865
};
865
};
866
- 
866
867
subtest 'change_type tests' => sub {
868
869
    plan tests => 9;
870
871
    $schema->storage->txn_begin;
872
873
    my $item = $builder->build_object( { class => 'Koha::Items', } );
874
    my $hold = $builder->build_object( { 
875
        class => 'Koha::Holds', 
876
        value => {
877
            itemnumber => undef,
878
        }
879
    } );
880
881
    my $hold2 = $builder->build_object( { 
882
        class => 'Koha::Holds', 
883
        value => {
884
            borrowernumber => $hold->borrowernumber,
885
        }
886
    } );
887
888
    ok( $hold->change_type );
889
890
    $hold->discard_changes;
891
892
    is( $hold->itemnumber, undef, 'record hold to record hold, no changes');
893
894
    ok( $hold->change_type( $item->itemnumber ) );
895
896
    $hold->discard_changes;
897
898
    is( $hold->itemnumber, $item->itemnumber, 'record hold to item hold');
899
900
    ok( $hold->change_type( $item->itemnumber ) );
901
902
    $hold->discard_changes;
903
904
    is( $hold->itemnumber, $item->itemnumber, 'item hold to item hold, no changes');
905
906
    ok( $hold->change_type );
907
908
    $hold->discard_changes;
909
910
    is( $hold->itemnumber, undef, 'item hold to record hold');
911
912
    my $hold3 = $builder->build_object( { 
913
        class => 'Koha::Holds', 
914
        value => {
915
            biblionumber => $hold->biblionumber,
916
            borrowernumber => $hold->borrowernumber,
917
        }
918
    } );
919
920
    throws_ok { $hold->change_type }
921
        'Koha::Exceptions::Hold::CannotChangeHoldType',
922
          'Exception thrown because more than one hold per record';
923
924
    $schema->storage->txn_rollback;
925
};

Return to bug 31692