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