Bug 26327: Add ->library method to Koha::*Checkout
[koha.git] / Koha / Checkout.pm
blob8fd7b6ad037be4e9a7376c75152c3f6e399f1d9d
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>.
21 use Modern::Perl;
23 use Carp;
24 use DateTime;
25 use Try::Tiny;
27 use Koha::Checkouts::ReturnClaims;
28 use Koha::Database;
29 use Koha::DateUtils;
30 use Koha::Items;
31 use Koha::Libraries;
33 use base qw(Koha::Object);
35 =head1 NAME
37 Koha::Checkout - Koha Checkout object class
39 =head1 API
41 =head2 Class methods
43 =cut
45 =head3 is_overdue
47 my $is_overdue = $checkout->is_overdue( [ $reference_dt ] );
49 Return 1 if the checkout is overdue.
51 A reference date can be passed, in this case it will be used, otherwise today
52 will be the reference date.
54 =cut
56 sub is_overdue {
57 my ( $self, $dt ) = @_;
58 $dt ||= dt_from_string();
60 my $is_overdue =
61 DateTime->compare( dt_from_string( $self->date_due, 'sql' ), $dt ) == -1
62 ? 1
63 : 0;
64 return $is_overdue;
67 =head3 item
69 my $item = $checkout->item;
71 Return the checked out item
73 =cut
75 sub item {
76 my ( $self ) = @_;
77 my $item_rs = $self->_result->item;
78 return Koha::Item->_new_from_dbic( $item_rs );
81 =head3 library
83 my $library = $checkout->library;
85 Return the library in which the transaction took place
87 =cut
89 sub library {
90 my ( $self ) = @_;
91 my $library_rs = $self->_result->library;
92 return Koha::Library->_new_from_dbic( $library_rs );
95 =head3 patron
97 my $patron = $checkout->patron
99 Return the patron for who the checkout has been done
101 =cut
103 sub patron {
104 my ( $self ) = @_;
105 my $patron_rs = $self->_result->borrower;
106 return Koha::Patron->_new_from_dbic( $patron_rs );
109 =head3 to_api_mapping
111 This method returns the mapping for representing a Koha::Checkout object
112 on the API.
114 =cut
116 sub to_api_mapping {
117 return {
118 issue_id => 'checkout_id',
119 borrowernumber => 'patron_id',
120 itemnumber => 'item_id',
121 date_due => 'due_date',
122 branchcode => 'library_id',
123 returndate => 'checkin_date',
124 lastreneweddate => 'last_renewed_date',
125 issuedate => 'checkout_date',
126 notedate => 'note_date',
130 =head3 claim_returned
132 my $return_claim = $checkout->claim_returned();
134 =cut
136 sub claim_returned {
137 my ( $self, $params ) = @_;
139 my $charge_lost_fee = $params->{charge_lost_fee};
141 try {
142 $self->_result->result_source->schema->txn_do(
143 sub {
144 my $claim = Koha::Checkouts::ReturnClaim->new(
146 issue_id => $self->id,
147 itemnumber => $self->itemnumber,
148 borrowernumber => $self->borrowernumber,
149 notes => $params->{notes},
150 created_by => $params->{created_by},
151 created_on => dt_from_string,
153 )->store();
155 my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue');
156 $self->item->itemlost($ClaimReturnedLostValue)->store;
158 my $ClaimReturnedChargeFee = C4::Context->preference('ClaimReturnedChargeFee');
159 $charge_lost_fee =
160 $ClaimReturnedChargeFee eq 'charge' ? 1
161 : $ClaimReturnedChargeFee eq 'no_charge' ? 0
162 : $charge_lost_fee; # $ClaimReturnedChargeFee eq 'ask'
163 C4::Circulation::LostItem( $self->itemnumber, 'claim_returned' ) if $charge_lost_fee;
165 return $claim;
169 catch {
170 if ( $_->isa('Koha::Exceptions::Exception') ) {
171 $_->rethrow();
173 else {
175 Koha::Exceptions::Exception->throw( "Unhandled exception" );
180 =head2 Internal methods
182 =head3 _type
184 =cut
186 sub _type {
187 return 'Issue';
190 =head1 AUTHOR
192 Kyle M Hall <kyle@bywatersolutions.com>
194 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
196 =cut