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

(-)a/Koha/Checkout.pm (+26 lines)
Lines 29-34 use Koha::Database; Link Here
29
use Koha::DateUtils;
29
use Koha::DateUtils;
30
use Koha::Items;
30
use Koha::Items;
31
31
32
use Koha::Exceptions::Object;
33
32
use base qw(Koha::Object);
34
use base qw(Koha::Object);
33
35
34
=head1 NAME
36
=head1 NAME
Lines 90-95 sub patron { Link Here
90
    return Koha::Patron->_new_from_dbic( $patron_rs );
92
    return Koha::Patron->_new_from_dbic( $patron_rs );
91
}
93
}
92
94
95
=head3 store
96
97
my $checkout = $checkout->store
98
99
Overrides the default store subroutine.
100
101
=cut
102
103
sub store {
104
    my ( $self ) = @_;
105
106
    my $checkout_type = $self->checkout_type;
107
    if ( $checkout_type ) {
108
        unless ( Koha::Checkouts::is_valid_checkout_type( $checkout_type ) ) {
109
            Koha::Exceptions::Object::FKConstraint->throw(
110
                broken_fk => 'checkout_type',
111
                value     => $self->checkout_type,
112
            );
113
        }
114
    }
115
116
    return $self->SUPER::store();
117
}
118
93
=head3 to_api_mapping
119
=head3 to_api_mapping
94
120
95
This method returns the mapping for representing a Koha::Checkout object
121
This method returns the mapping for representing a Koha::Checkout object
(-)a/Koha/Checkouts.pm (+22 lines)
Lines 22-27 use Modern::Perl; Link Here
22
use Carp;
22
use Carp;
23
23
24
use C4::Context;
24
use C4::Context;
25
use Koha::AuthorisedValues;
25
use Koha::Checkout;
26
use Koha::Checkout;
26
use Koha::Database;
27
use Koha::Database;
27
28
Lines 54-59 sub calculate_dropbox_date { Link Here
54
    return $dropbox_date;
55
    return $dropbox_date;
55
}
56
}
56
57
58
=head3 is_valid_checkout_type
59
60
my $valid_checkout_type = Koha::Checkouts::is_valid_checkout_type( $type );
61
62
Checks authorised values for valid checkout types.
63
64
=cut
65
66
sub is_valid_checkout_type {
67
    my ( $type ) = @_;
68
69
    return 1 if not defined $type; # undefined refers to normal checkout
70
71
    my $av = Koha::AuthorisedValues->search({
72
        category         => 'CHECKOUT_TYPE',
73
        authorised_value => $type,
74
    });
75
76
    return $av->count ? 1 : 0;
77
}
78
57
=head3 type
79
=head3 type
58
80
59
=cut
81
=cut
(-)a/t/db_dependent/Koha/Checkouts.t (-2 / +53 lines)
Lines 19-27 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 7;
22
use Test::More tests => 8;
23
use Test::Exception;
23
24
24
use C4::Circulation;
25
use C4::Circulation;
26
use Koha::AuthorisedValues;
27
use Koha::AuthorisedValueCategories;
25
use Koha::Checkouts;
28
use Koha::Checkouts;
26
use Koha::Database;
29
use Koha::Database;
27
use Koha::DateUtils qw( dt_from_string );
30
use Koha::DateUtils qw( dt_from_string );
Lines 122-127 subtest 'patron' => sub { Link Here
122
    );
125
    );
123
};
126
};
124
127
128
subtest 'store' => sub {
129
    plan tests => 1;
130
131
    subtest 'validate checkout_type' => sub {
132
        plan tests => 3;
133
134
        # This should exist on clean installations, but just make sure...
135
        unless ( Koha::AuthorisedValueCategories->search( {
136
            category_name => 'CHECKOUT_TYPE'
137
        } )->count ) {
138
            $builder->build_object( {
139
                class=> 'Koha::AuthorisedValueCategories',
140
                value => { category_name => 'CHECKOUT_TYPE' }
141
            } );
142
        }
143
144
        my $av = $builder->build_object( {
145
            class=> 'Koha::AuthorisedValues',
146
            value => { category => 'CHECKOUT_TYPE' }
147
        } );
148
149
        my $patron = $builder->build_object( {
150
            class=> 'Koha::Patrons',
151
            value => { branchcode => $library->{branchcode} }
152
        } );
153
        my $item   = $builder->build_object( { class=> 'Koha::Items' } );
154
        my $checkout = Koha::Checkout->new(
155
            {   borrowernumber => $patron->borrowernumber,
156
                itemnumber     => $item->itemnumber,
157
                branchcode     => $library->{branchcode},
158
            }
159
        )->store;
160
161
        ok( $checkout->issue_id > 0,
162
            'store() was successful with undef checkout_type' );
163
164
        $checkout->set({ checkout_type => $av->authorised_value })->store;
165
        is( $checkout->checkout_type, $av->authorised_value,
166
            'store() was successful with a valid checkout_type' );
167
168
        throws_ok {
169
            $checkout->set({ checkout_type => 'NON_EXISTENT' })->store
170
        } 'Koha::Exceptions::Object::FKConstraint',
171
          'store() throws an exception with an invalid checkout_type';
172
173
        $checkout->delete;
174
    };
175
};
176
125
$retrieved_checkout_1->delete;
177
$retrieved_checkout_1->delete;
126
is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' );
178
is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' );
127
179
128
- 

Return to bug 25037