Bug 24840: Replace DateTime->now with dt_from_string
[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
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.
12 #
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.
17 #
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>.
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 ||= dt_from_string();
58
59     my $is_overdue =
60       DateTime->compare( dt_from_string( $self->date_due, 'sql' ), $dt ) == -1
61       ? 1
62       : 0;
63     return $is_overdue;
64 }
65
66 =head3 item
67
68 my $item = $checkout->item;
69
70 Return the checked out item
71
72 =cut
73
74 sub item {
75     my ( $self ) = @_;
76     my $item_rs = $self->_result->item;
77     return Koha::Item->_new_from_dbic( $item_rs );
78 }
79
80 =head3 patron
81
82 my $patron = $checkout->patron
83
84 Return the patron for who the checkout has been done
85
86 =cut
87
88 sub patron {
89     my ( $self ) = @_;
90     my $patron_rs = $self->_result->borrower;
91     return Koha::Patron->_new_from_dbic( $patron_rs );
92 }
93
94 =head3 to_api_mapping
95
96 This method returns the mapping for representing a Koha::Checkout object
97 on the API.
98
99 =cut
100
101 sub to_api_mapping {
102     return {
103         issue_id        => 'checkout_id',
104         borrowernumber  => 'patron_id',
105         itemnumber      => 'item_id',
106         date_due        => 'due_date',
107         branchcode      => 'library_id',
108         returndate      => 'checkin_date',
109         lastreneweddate => 'last_renewed_date',
110         issuedate       => 'checkout_date',
111         notedate        => 'note_date',
112     };
113 }
114
115 =head3 claim_returned
116
117 my $return_claim = $checkout->claim_returned();
118
119 =cut
120
121 sub claim_returned {
122     my ( $self, $params ) = @_;
123
124     my $charge_lost_fee = $params->{charge_lost_fee};
125
126     try {
127         $self->_result->result_source->schema->txn_do(
128             sub {
129                 my $claim = Koha::Checkouts::ReturnClaim->new(
130                     {
131                         issue_id       => $self->id,
132                         itemnumber     => $self->itemnumber,
133                         borrowernumber => $self->borrowernumber,
134                         notes          => $params->{notes},
135                         created_by     => $params->{created_by},
136                         created_on     => dt_from_string,
137                     }
138                 )->store();
139
140                 my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue');
141                 $self->item->itemlost($ClaimReturnedLostValue)->store;
142
143                 my $ClaimReturnedChargeFee = C4::Context->preference('ClaimReturnedChargeFee');
144                 $charge_lost_fee =
145                     $ClaimReturnedChargeFee eq 'charge'    ? 1
146                 : $ClaimReturnedChargeFee eq 'no_charge' ? 0
147                 :   $charge_lost_fee;    # $ClaimReturnedChargeFee eq 'ask'
148                 C4::Circulation::LostItem( $self->itemnumber, 'claim_returned' ) if $charge_lost_fee;
149
150                 return $claim;
151             }
152         );
153     }
154     catch {
155         if ( $_->isa('Koha::Exceptions::Exception') ) {
156             $_->rethrow();
157         }
158         else {
159             # ?
160             Koha::Exceptions::Exception->throw( "Unhandled exception" );
161         }
162     };
163 }
164
165 =head2 Internal methods
166
167 =head3 _type
168
169 =cut
170
171 sub _type {
172     return 'Issue';
173 }
174
175 =head1 AUTHOR
176
177 Kyle M Hall <kyle@bywatersolutions.com>
178
179 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
180
181 =cut
182
183 1;