Bug 14697: DBRev 19.06.00.047
[koha.git] / Koha / Checkout.pm
1 package Koha::Checkout;
2
3 # Copyright ByWater Solutions 2015
4 # Copyright 2016 Koha Development Team
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use Modern::Perl;
22
23 use Carp;
24 use DateTime;
25 use Try::Tiny;
26
27 use Koha::Checkouts::ReturnClaims;
28 use Koha::Database;
29 use Koha::DateUtils;
30 use Koha::Items;
31
32 use base qw(Koha::Object);
33
34 =head1 NAME
35
36 Koha::Checkout - Koha Checkout object class
37
38 =head1 API
39
40 =head2 Class methods
41
42 =cut
43
44 =head3 is_overdue
45
46 my  $is_overdue = $checkout->is_overdue( [ $reference_dt ] );
47
48 Return 1 if the checkout is overdue.
49
50 A reference date can be passed, in this case it will be used, otherwise today
51 will be the reference date.
52
53 =cut
54
55 sub is_overdue {
56     my ( $self, $dt ) = @_;
57     $dt ||= DateTime->now( time_zone => C4::Context->tz );
58     my $is_overdue =
59       DateTime->compare( dt_from_string( $self->date_due, 'sql' ), $dt ) == -1
60       ? 1
61       : 0;
62     return $is_overdue;
63 }
64
65 =head3 item
66
67 my $item = $checkout->item;
68
69 Return the checked out item
70
71 =cut
72
73 sub item {
74     my ( $self ) = @_;
75     my $item_rs = $self->_result->item;
76     return Koha::Item->_new_from_dbic( $item_rs );
77 }
78
79 =head3 patron
80
81 my $patron = $checkout->patron
82
83 Return the patron for who the checkout has been done
84
85 =cut
86
87 sub patron {
88     my ( $self ) = @_;
89     my $patron_rs = $self->_result->borrower;
90     return Koha::Patron->_new_from_dbic( $patron_rs );
91 }
92
93 =head3 to_api_mapping
94
95 This method returns the mapping for representing a Koha::Checkout object
96 on the API.
97
98 =cut
99
100 sub to_api_mapping {
101     return {
102         issue_id        => 'checkout_id',
103         borrowernumber  => 'patron_id',
104         itemnumber      => 'item_id',
105         date_due        => 'due_date',
106         branchcode      => 'library_id',
107         returndate      => 'checkin_date',
108         lastreneweddate => 'last_renewed_date',
109         issuedate       => 'checkout_date',
110         notedate        => 'note_date',
111     };
112 }
113
114 =head3 claim_returned
115
116 my $return_claim = $checkout->claim_returned();
117
118 =cut
119
120 sub claim_returned {
121     my ( $self, $params ) = @_;
122
123     my $charge_lost_fee = $params->{charge_lost_fee};
124
125     try {
126         $self->_result->result_source->schema->txn_do(
127             sub {
128                 my $claim = Koha::Checkouts::ReturnClaim->new(
129                     {
130                         issue_id       => $self->id,
131                         itemnumber     => $self->itemnumber,
132                         borrowernumber => $self->borrowernumber,
133                         notes          => $params->{notes},
134                         created_by     => $params->{created_by},
135                         created_on     => dt_from_string,
136                     }
137                 )->store();
138
139                 my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue');
140                 C4::Items::ModItem( { itemlost => $ClaimReturnedLostValue }, undef, $self->itemnumber );
141
142                 my $ClaimReturnedChargeFee = C4::Context->preference('ClaimReturnedChargeFee');
143                 $charge_lost_fee =
144                     $ClaimReturnedChargeFee eq 'charge'    ? 1
145                 : $ClaimReturnedChargeFee eq 'no_charge' ? 0
146                 :   $charge_lost_fee;    # $ClaimReturnedChargeFee eq 'ask'
147                 C4::Circulation::LostItem( $self->itemnumber, 'claim_returned' ) if $charge_lost_fee;
148
149                 return $claim;
150             }
151         );
152     }
153     catch {
154         if ( $_->isa('Koha::Exceptions::Exception') ) {
155             $_->rethrow();
156         }
157         else {
158             # ?
159             Koha::Exceptions::Exception->throw( "Unhandled exception" );
160         }
161     };
162 }
163
164 =head2 Internal methods
165
166 =head3 _type
167
168 =cut
169
170 sub _type {
171     return 'Issue';
172 }
173
174 =head1 AUTHOR
175
176 Kyle M Hall <kyle@bywatersolutions.com>
177
178 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
179
180 =cut
181
182 1;