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

(-)a/C4/Circulation.pm (-36 / +44 lines)
Lines 1768-1814 sub AddIssue { Link Here
1768
            # and SwitchOnSiteCheckouts is enabled this converts it to a regular checkout
1768
            # and SwitchOnSiteCheckouts is enabled this converts it to a regular checkout
1769
            $issue = Koha::Checkouts->find( { itemnumber => $item_object->itemnumber } );
1769
            $issue = Koha::Checkouts->find( { itemnumber => $item_object->itemnumber } );
1770
1770
1771
            # If this checkout relates to a booking, handle booking status appropriately
1771
            my $schema = Koha::Database->schema;
1772
            if ( my $booking = $item_object->find_booking( { checkout_date => $issuedate, due_date => $datedue } ) ) {
1772
            $schema->txn_do(
1773
                if ( $booking->patron_id == $patron->borrowernumber ) {
1773
                sub {
1774
                    # If this checkout relates to a booking, handle booking status appropriately
1775
                    if ( my $booking =
1776
                        $item_object->find_booking( { checkout_date => $issuedate, due_date => $datedue } ) )
1777
                    {
1778
                        if ( $booking->patron_id == $patron->borrowernumber ) {
1774
1779
1775
                    # Patron's own booking - mark as completed
1780
                            # Patron's own booking - mark as completed and link checkout to booking
1776
                    $booking->status('completed')->store;
1781
                            $booking->status('completed')->store;
1777
                } else {
1782
                            $issue_attributes->{'booking_id'} = $booking->booking_id;
1783
                        } else {
1778
1784
1779
                    # Another patron's booking - only cancel if checkout period overlaps with actual booking period
1785
                            # Another patron's booking - only cancel if checkout period overlaps with actual booking period
1780
                    my $booking_start = dt_from_string( $booking->start_date );
1786
                            my $booking_start = dt_from_string( $booking->start_date );
1781
                    my $booking_end   = dt_from_string( $booking->end_date );
1787
                            my $booking_end   = dt_from_string( $booking->end_date );
1782
1788
1783
                    # Check if checkout period overlaps with actual booking period (not just lead/trail)
1789
                            # Check if checkout period overlaps with actual booking period (not just lead/trail)
1784
                    if (   ( $issuedate >= $booking_start && $issuedate <= $booking_end )
1790
                            if (   ( $issuedate >= $booking_start && $issuedate <= $booking_end )
1785
                        || ( $datedue >= $booking_start   && $datedue <= $booking_end )
1791
                                || ( $datedue >= $booking_start   && $datedue <= $booking_end )
1786
                        || ( $issuedate <= $booking_start && $datedue >= $booking_end ) )
1792
                                || ( $issuedate <= $booking_start && $datedue >= $booking_end ) )
1787
                    {
1793
                            {
1788
                        # Checkout overlaps with actual booking period - cancel the booking
1794
                                # Checkout overlaps with actual booking period - cancel the booking
1789
                        $booking->status('cancelled')->store;
1795
                                $booking->status('cancelled')->store;
1796
                            }
1797
1798
                            # If only in lead/trail period, do nothing - librarian has made informed decision
1799
                        }
1790
                    }
1800
                    }
1791
1801
1792
                    # If only in lead/trail period, do nothing - librarian has made informed decision
1802
                    if ($issue) {
1793
                }
1803
                        $issue->set($issue_attributes)->store;
1794
            }
1804
                    } else {
1805
                        $issue = Koha::Checkout->new(
1806
                            {
1807
                                itemnumber => $item_object->itemnumber,
1808
                                %$issue_attributes,
1809
                            }
1810
                        )->store;
1795
1811
1796
            if ($issue) {
1812
                        # Ensure item is no longer listed in the holds queue
1797
                $issue->set($issue_attributes)->store;
1813
                        $dbh->do(
1798
            } else {
1814
                            q{DELETE tmp_holdsqueue, hold_fill_targets FROM tmp_holdsqueue LEFT JOIN hold_fill_targets USING ( itemnumber ) WHERE itemnumber = ?},
1799
                $issue = Koha::Checkout->new(
1815
                            undef, $item_object->id
1800
                    {
1816
                        );
1801
                        itemnumber => $item_object->itemnumber,
1802
                        %$issue_attributes,
1803
                    }
1817
                    }
1804
                )->store;
1818
                }
1805
1819
            );
1806
                # Ensure item is no longer listed in the holds queue
1807
                $dbh->do(
1808
                    q{DELETE tmp_holdsqueue, hold_fill_targets FROM tmp_holdsqueue LEFT JOIN hold_fill_targets USING ( itemnumber ) WHERE itemnumber = ?},
1809
                    undef, $item_object->id
1810
                );
1811
            }
1812
            $issue->discard_changes;
1820
            $issue->discard_changes;
1813
1821
1814
            #Update borrowers.lastseen
1822
            #Update borrowers.lastseen
Lines 3475-3487 sub AddRenewal { Link Here
3475
            # of how many times it has been renewed.
3483
            # of how many times it has been renewed.
3476
            my $renews = ( $issue->renewals_count || 0 ) + 1;
3484
            my $renews = ( $issue->renewals_count || 0 ) + 1;
3477
            my $sth    = $dbh->prepare(
3485
            my $sth    = $dbh->prepare(
3478
                "UPDATE issues SET date_due = ?, renewals_count = ?, unseen_renewals = ?, lastreneweddate = ? WHERE issue_id = ?"
3486
                "UPDATE issues SET date_due = ?, renewals_count = ?, unseen_renewals = ?, lastreneweddate = ?, booking_id = ? WHERE issue_id = ?"
3479
            );
3487
            );
3480
3488
3481
            eval {
3489
            eval {
3482
                $sth->execute(
3490
                $sth->execute(
3483
                    $datedue->strftime('%Y-%m-%d %H:%M'), $renews, $unseen_renewals, $lastreneweddate,
3491
                    $datedue->strftime('%Y-%m-%d %H:%M'), $renews, $unseen_renewals, $lastreneweddate,
3484
                    $issue->issue_id
3492
                    $issue->booking_id, $issue->issue_id
3485
                );
3493
                );
3486
            };
3494
            };
3487
            if ( $sth->err ) {
3495
            if ( $sth->err ) {
(-)a/Koha/Booking.pm (+28 lines)
Lines 92-97 sub item { Link Here
92
    return Koha::Item->_new_from_dbic($item_rs);
92
    return Koha::Item->_new_from_dbic($item_rs);
93
}
93
}
94
94
95
=head3 checkout
96
97
Returns the related Koha::Checkout object for this booking, if any
98
99
=cut
100
101
sub checkout {
102
    my ($self) = @_;
103
104
    my $checkout_rs = $self->_result->issues->first;
105
    return unless $checkout_rs;
106
    return Koha::Checkout->_new_from_dbic($checkout_rs);
107
}
108
109
=head3 old_checkout
110
111
Returns the related Koha::Old::Checkout object for this booking, if any
112
113
=cut
114
115
sub old_checkout {
116
    my ($self) = @_;
117
118
    my $old_checkout_rs = $self->_result->old_issues->first;
119
    return unless $old_checkout_rs;
120
    return Koha::Old::Checkout->_new_from_dbic($old_checkout_rs);
121
}
122
95
=head3 store
123
=head3 store
96
124
97
Booking specific store method to catch booking clashes and ensure we have an item assigned
125
Booking specific store method to catch booking clashes and ensure we have an item assigned
(-)a/Koha/Checkout.pm (+17 lines)
Lines 24-29 use DateTime; Link Here
24
use Try::Tiny qw( catch try );
24
use Try::Tiny qw( catch try );
25
25
26
use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem MarkIssueReturned );
26
use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem MarkIssueReturned );
27
use Koha::Booking;
27
use Koha::Checkouts::Renewals;
28
use Koha::Checkouts::Renewals;
28
use Koha::Checkouts::ReturnClaims;
29
use Koha::Checkouts::ReturnClaims;
29
use Koha::Database;
30
use Koha::Database;
Lines 165-170 sub renewals { Link Here
165
    return Koha::Checkouts::Renewals->_new_from_dbic($renewals_rs);
166
    return Koha::Checkouts::Renewals->_new_from_dbic($renewals_rs);
166
}
167
}
167
168
169
=head3 booking
170
171
my $booking = $checkout->booking;
172
173
Return the linked booking
174
175
=cut
176
177
sub booking {
178
    my ($self) = @_;
179
    my $booking_rs = $self->_result->booking;
180
    return unless $booking_rs;
181
    return Koha::Booking->_new_from_dbic($booking_rs);
182
}
183
168
=head3 attempt_auto_renew
184
=head3 attempt_auto_renew
169
185
170
  my ($success, $error, $updated) = $checkout->auto_renew({ confirm => 1 });
186
  my ($success, $error, $updated) = $checkout->auto_renew({ confirm => 1 });
Lines 227-232 sub to_api_mapping { Link Here
227
        issue_id        => 'checkout_id',
243
        issue_id        => 'checkout_id',
228
        borrowernumber  => 'patron_id',
244
        borrowernumber  => 'patron_id',
229
        itemnumber      => 'item_id',
245
        itemnumber      => 'item_id',
246
        booking_id      => 'booking_id',
230
        date_due        => 'due_date',
247
        date_due        => 'due_date',
231
        branchcode      => 'library_id',
248
        branchcode      => 'library_id',
232
        returndate      => 'checkin_date',
249
        returndate      => 'checkin_date',
(-)a/Koha/Old/Checkout.pm (-1 / +17 lines)
Lines 17-22 package Koha::Old::Checkout; Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Booking;
20
use Koha::Database;
21
use Koha::Database;
21
use Koha::Exceptions::SysPref;
22
use Koha::Exceptions::SysPref;
22
use Koha::Libraries;
23
use Koha::Libraries;
Lines 120-125 sub renewals { Link Here
120
    return Koha::Checkouts::Renewals->_new_from_dbic($renewals_rs);
121
    return Koha::Checkouts::Renewals->_new_from_dbic($renewals_rs);
121
}
122
}
122
123
124
=head3 booking
125
126
my $booking = $checkout->booking;
127
128
Return the linked booking
129
130
=cut
131
132
sub booking {
133
    my ($self) = @_;
134
    my $booking_rs = $self->_result->booking;
135
    return unless $booking_rs;
136
    return Koha::Booking->_new_from_dbic($booking_rs);
137
}
138
123
=head3 anonymize
139
=head3 anonymize
124
140
125
    $checkout->anonymize();
141
    $checkout->anonymize();
Lines 155-160 sub to_api_mapping { Link Here
155
        issue_id        => 'checkout_id',
171
        issue_id        => 'checkout_id',
156
        borrowernumber  => 'patron_id',
172
        borrowernumber  => 'patron_id',
157
        itemnumber      => 'item_id',
173
        itemnumber      => 'item_id',
174
        booking_id      => 'booking_id',
158
        date_due        => 'due_date',
175
        date_due        => 'due_date',
159
        branchcode      => 'library_id',
176
        branchcode      => 'library_id',
160
        returndate      => 'checkin_date',
177
        returndate      => 'checkin_date',
161
- 

Return to bug 40665