6af0179fe502fe06aa44da36e7cca3b0f9cb2402
[koha-ffzg.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 C4::Circulation qw(MarkIssueReturned);
28 use Koha::Checkouts::ReturnClaims;
29 use Koha::Database;
30 use Koha::DateUtils;
31 use Koha::Items;
32 use Koha::Libraries;
33
34 use base qw(Koha::Object);
35
36 =head1 NAME
37
38 Koha::Checkout - Koha Checkout object class
39
40 =head1 API
41
42 =head2 Class methods
43
44 =cut
45
46 =head3 is_overdue
47
48 my  $is_overdue = $checkout->is_overdue( [ $reference_dt ] );
49
50 Return 1 if the checkout is overdue.
51
52 A reference date can be passed, in this case it will be used, otherwise today
53 will be the reference date.
54
55 =cut
56
57 sub is_overdue {
58     my ( $self, $dt ) = @_;
59     $dt ||= dt_from_string();
60
61     my $is_overdue =
62       DateTime->compare( dt_from_string( $self->date_due, 'sql' ), $dt ) == -1
63       ? 1
64       : 0;
65     return $is_overdue;
66 }
67
68 =head3 item
69
70 my $item = $checkout->item;
71
72 Return the checked out item
73
74 =cut
75
76 sub item {
77     my ( $self ) = @_;
78     my $item_rs = $self->_result->item;
79     return Koha::Item->_new_from_dbic( $item_rs );
80 }
81
82 =head3 library
83
84 my $library = $checkout->library;
85
86 Return the library in which the transaction took place
87
88 =cut
89
90 sub library {
91     my ( $self ) = @_;
92     my $library_rs = $self->_result->library;
93     return Koha::Library->_new_from_dbic( $library_rs );
94 }
95
96 =head3 patron
97
98 my $patron = $checkout->patron
99
100 Return the patron for who the checkout has been done
101
102 =cut
103
104 sub patron {
105     my ( $self ) = @_;
106     my $patron_rs = $self->_result->borrower;
107     return Koha::Patron->_new_from_dbic( $patron_rs );
108 }
109
110 =head3 issuer
111
112 my $issuer = $checkout->issuer
113
114 Return the patron by whom the checkout was done
115
116 =cut
117
118 sub issuer {
119     my ( $self ) = @_;
120     my $issuer_rs = $self->_result->issuer;
121     return unless $issuer_rs;
122     return Koha::Patron->_new_from_dbic( $issuer_rs );
123 }
124
125 =head3 to_api_mapping
126
127 This method returns the mapping for representing a Koha::Checkout object
128 on the API.
129
130 =cut
131
132 sub to_api_mapping {
133     return {
134         issue_id        => 'checkout_id',
135         borrowernumber  => 'patron_id',
136         itemnumber      => 'item_id',
137         date_due        => 'due_date',
138         branchcode      => 'library_id',
139         returndate      => 'checkin_date',
140         lastreneweddate => 'last_renewed_date',
141         issuedate       => 'checkout_date',
142         notedate        => 'note_date',
143     };
144 }
145
146 =head3 claim_returned
147
148 my $return_claim = $checkout->claim_returned();
149
150 =cut
151
152 sub claim_returned {
153     my ( $self, $params ) = @_;
154
155     my $charge_lost_fee = $params->{charge_lost_fee};
156
157     try {
158         $self->_result->result_source->schema->txn_do(
159             sub {
160                 my $claim = Koha::Checkouts::ReturnClaim->new(
161                     {
162                         issue_id       => $self->id,
163                         itemnumber     => $self->itemnumber,
164                         borrowernumber => $self->borrowernumber,
165                         notes          => $params->{notes},
166                         created_by     => $params->{created_by},
167                         created_on     => dt_from_string,
168                     }
169                 )->store();
170
171                 my $ClaimReturnedLostValue = C4::Context->preference('ClaimReturnedLostValue');
172                 $self->item->itemlost($ClaimReturnedLostValue)->store;
173
174                 my $ClaimReturnedChargeFee = C4::Context->preference('ClaimReturnedChargeFee');
175                 $charge_lost_fee =
176                     $ClaimReturnedChargeFee eq 'charge'    ? 1
177                 : $ClaimReturnedChargeFee eq 'no_charge' ? 0
178                 :   $charge_lost_fee;    # $ClaimReturnedChargeFee eq 'ask'
179
180                 if ( $charge_lost_fee ) {
181                     C4::Circulation::LostItem( $self->itemnumber, 'claim_returned' );
182                 }
183                 elsif ( C4::Context->preference( 'MarkLostItemsAsReturned' ) =~ m/claim_returned/ ) {
184                     C4::Circulation::MarkIssueReturned( $self->borrowernumber, $self->itemnumber, undef, $self->patron->privacy );
185                 }
186
187                 return $claim;
188             }
189         );
190     }
191     catch {
192         if ( $_->isa('Koha::Exceptions::Exception') ) {
193             $_->rethrow();
194         }
195         else {
196             # ?
197             Koha::Exceptions::Exception->throw( "Unhandled exception" );
198         }
199     };
200 }
201
202 =head2 Internal methods
203
204 =head3 _type
205
206 =cut
207
208 sub _type {
209     return 'Issue';
210 }
211
212 =head1 AUTHOR
213
214 Kyle M Hall <kyle@bywatersolutions.com>
215
216 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
217
218 =cut
219
220 1;