1 package Koha::Checkout;
3 # Copyright ByWater Solutions 2015
4 # Copyright 2016 Koha Development Team
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Try::Tiny qw( catch try );
26 use C4::Circulation qw( LostItem MarkIssueReturned );
27 use Koha::Checkouts::Renewals;
28 use Koha::Checkouts::ReturnClaims;
30 use Koha::DateUtils qw( dt_from_string );
34 use base qw(Koha::Object);
38 Koha::Checkout - Koha Checkout object class
48 my $is_overdue = $checkout->is_overdue( [ $reference_dt ] );
50 Return 1 if the checkout is overdue.
52 A reference date can be passed, in this case it will be used, otherwise today
53 will be the reference date.
58 my ( $self, $dt ) = @_;
59 $dt ||= dt_from_string();
62 DateTime->compare( dt_from_string( $self->date_due, 'sql' ), $dt ) == -1
70 my $item = $checkout->item;
72 Return the checked out item
78 my $item_rs = $self->_result->item;
79 return Koha::Item->_new_from_dbic( $item_rs );
84 my $account_lines = $checkout->account_lines;
86 Return the checked out account_lines
92 my $account_lines_rs = $self->_result->account_lines;
93 return Koha::Account::Lines->_new_from_dbic( $account_lines_rs );
98 my $library = $checkout->library;
100 Return the library in which the transaction took place
106 my $library_rs = $self->_result->library;
107 return Koha::Library->_new_from_dbic( $library_rs );
112 my $patron = $checkout->patron
114 Return the patron for who the checkout has been done
120 my $patron_rs = $self->_result->patron;
121 return Koha::Patron->_new_from_dbic( $patron_rs );
126 my $issuer = $checkout->issuer
128 Return the patron by whom the checkout was done
134 my $issuer_rs = $self->_result->issuer;
135 return unless $issuer_rs;
136 return Koha::Patron->_new_from_dbic( $issuer_rs );
141 my $renewals = $checkout->renewals;
143 Return a Koha::Checkouts::Renewals set attached to this checkout
149 my $renewals_rs = $self->_result->renewals;
150 return unless $renewals_rs;
151 return Koha::Checkouts::Renewals->_new_from_dbic( $renewals_rs );
154 =head3 to_api_mapping
156 This method returns the mapping for representing a Koha::Checkout object
163 issue_id => 'checkout_id',
164 borrowernumber => 'patron_id',
165 itemnumber => 'item_id',
166 date_due => 'due_date',
167 branchcode => 'library_id',
168 returndate => 'checkin_date',
169 lastreneweddate => 'last_renewed_date',
170 issuedate => 'checkout_date',
171 notedate => 'note_date',
172 noteseen => 'note_seen',
176 =head3 claim_returned
178 my $return_claim = $checkout->claim_returned();
180 This method sets the checkout as claimed return. It will:
182 1. Add a new row to the `return_claims` table
183 2. Set the item as lost using the 'ClaimReturnedLostValue'
184 3. Charge a fee depending on the value of ClaimReturnedChargeFee
185 3a. If set to charge, then accruing overdues will be halted
186 3b. If set to charge, then any existing transfers will be cancelled
187 and the holding branch will be set back to 'frombranch'.
188 4. The issue will be marked as returned as per the 'MarkLostItemsAsReturned' preference
193 my ( $self, $params ) = @_;
195 my $charge_lost_fee = $params->{charge_lost_fee};
198 $self->_result->result_source->schema->txn_do(
200 my $claim = Koha::Checkouts::ReturnClaim->new(
202 issue_id => $self->id,
203 itemnumber => $self->itemnumber,
204 borrowernumber => $self->borrowernumber,
205 notes => $params->{notes},
206 created_by => $params->{created_by},
207 created_on => dt_from_string,
211 my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue');
212 $self->item->itemlost($ClaimReturnedLostValue)->store;
214 my $ClaimReturnedChargeFee = C4::Context->preference('ClaimReturnedChargeFee');
216 $ClaimReturnedChargeFee eq 'charge' ? 1
217 : $ClaimReturnedChargeFee eq 'no_charge' ? 0
218 : $charge_lost_fee; # $ClaimReturnedChargeFee eq 'ask'
220 if ( $charge_lost_fee ) {
221 C4::Circulation::LostItem( $self->itemnumber, 'claim_returned' );
223 elsif ( C4::Context->preference( 'MarkLostItemsAsReturned' ) =~ m/claim_returned/ ) {
224 C4::Circulation::MarkIssueReturned( $self->borrowernumber, $self->itemnumber, undef, $self->patron->privacy );
232 if ( $_->isa('Koha::Exception') ) {
237 Koha::Exception->throw( "Unhandled exception" );
242 =head2 Internal methods
254 Kyle M Hall <kyle@bywatersolutions.com>
256 Jonathan Druart <jonathan.druart@bugs.koha-community.org>