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

(-)a/Koha/Biblio.pm (-47 lines)
Lines 274-326 sub check_booking { Link Here
274
    return ( ( $total_bookable - $booked_count ) > 0 ) ? 1 : 0;
274
    return ( ( $total_bookable - $booked_count ) > 0 ) ? 1 : 0;
275
}
275
}
276
276
277
=head3 assign_item_for_booking
278
279
=cut
280
281
sub assign_item_for_booking {
282
    my ( $self, $params ) = @_;
283
284
    my $start_date = dt_from_string( $params->{start_date} );
285
    my $end_date   = dt_from_string( $params->{end_date} );
286
287
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
288
289
    my $existing_bookings = $self->bookings(
290
        [
291
            start_date => {
292
                '-between' => [
293
                    $dtf->format_datetime($start_date),
294
                    $dtf->format_datetime($end_date)
295
                ]
296
            },
297
            end_date => {
298
                '-between' => [
299
                    $dtf->format_datetime($start_date),
300
                    $dtf->format_datetime($end_date)
301
                ]
302
            },
303
            {
304
                start_date => { '<' => $dtf->format_datetime($start_date) },
305
                end_date   => { '>' => $dtf->format_datetime($end_date) }
306
            }
307
        ]
308
    );
309
310
    my $checkouts = $self->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } );
311
312
    my $bookable_items = $self->bookable_items->search(
313
        {
314
            itemnumber => [
315
                '-and' => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query },
316
                { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query }
317
            ]
318
        },
319
        { rows => 1 }
320
    );
321
    return $bookable_items->single->itemnumber;
322
}
323
324
=head3 can_be_transferred
277
=head3 can_be_transferred
325
278
326
$biblio->can_be_transferred({ to => $to_library, from => $from_library })
279
$biblio->can_be_transferred({ to => $to_library, from => $from_library })
(-)a/Koha/Booking.pm (-9 / +57 lines)
Lines 128-141 sub store { Link Here
128
128
129
            # Assign item at booking time
129
            # Assign item at booking time
130
            if ( !$self->item_id ) {
130
            if ( !$self->item_id ) {
131
                $self->item_id(
131
                $self->_assign_item_for_booking;
132
                    $self->biblio->assign_item_for_booking(
133
                        {
134
                            start_date => $self->start_date,
135
                            end_date   => $self->end_date
136
                        }
137
                    )
138
                );
139
            }
132
            }
140
133
141
            $self = $self->SUPER::store;
134
            $self = $self->SUPER::store;
Lines 145-150 sub store { Link Here
145
    return $self;
138
    return $self;
146
}
139
}
147
140
141
=head3 _assign_item_for_booking
142
143
  $self->_assign_item_for_booking;
144
145
Used internally in Koha::Booking->store to ensure we have an item assigned for the booking.
146
147
=cut
148
149
sub _assign_item_for_booking {
150
    my ($self) = @_;
151
152
    my $biblio = $self->biblio;
153
154
    my $start_date = dt_from_string( $self->start_date );
155
    my $end_date   = dt_from_string( $self->end_date );
156
157
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
158
159
    my $existing_bookings = $biblio->bookings(
160
        [
161
            start_date => {
162
                '-between' => [
163
                    $dtf->format_datetime($start_date),
164
                    $dtf->format_datetime($end_date)
165
                ]
166
            },
167
            end_date => {
168
                '-between' => [
169
                    $dtf->format_datetime($start_date),
170
                    $dtf->format_datetime($end_date)
171
                ]
172
            },
173
            {
174
                start_date => { '<' => $dtf->format_datetime($start_date) },
175
                end_date   => { '>' => $dtf->format_datetime($end_date) }
176
            }
177
        ]
178
    );
179
180
    my $checkouts =
181
        $biblio->current_checkouts->search( { date_due => { '>=' => $dtf->format_datetime($start_date) } } );
182
183
    my $bookable_items = $biblio->bookable_items->search(
184
        {
185
            itemnumber => [
186
                '-and' => { '-not_in' => $existing_bookings->_resultset->get_column('item_id')->as_query },
187
                { '-not_in' => $checkouts->_resultset->get_column('itemnumber')->as_query }
188
            ]
189
        },
190
        { rows => 1 }
191
    );
192
193
    my $itemnumber = $bookable_items->single->itemnumber;
194
    return $self->item_id($itemnumber);
195
}
196
148
=head3 get_items_that_can_fill
197
=head3 get_items_that_can_fill
149
198
150
    my $items = $bookings->get_items_that_can_fill();
199
    my $items = $bookings->get_items_that_can_fill();
151
- 

Return to bug 35248