3 # Copyright 2020 Aleisha Amohia <aleisha@catalyst.net.nz>
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use Koha::DateUtils qw( dt_from_string );
27 use C4::Stats qw( UpdateStats );
29 use base qw(Koha::Objects);
33 Koha::Recalls - Koha Recalls Object set class
39 =head3 filter_by_current
41 my $current_recalls = $recalls->filter_by_current;
43 Returns a new resultset, filtering out finished recalls.
47 sub filter_by_current {
62 =head3 filter_by_finished
64 my $finished_recalls = $recalls->filter_by_finished;
66 Returns a new resultset, filtering out current recalls.
70 sub filter_by_finished {
86 my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
87 patron => $patron_object,
88 biblio => $biblio_object,
89 branchcode => $branchcode,
91 expirationdate => $expirationdate,
95 Add a new requested recall. We assume at this point that a recall is allowed to be placed on this item or biblio. We are past the checks and are now doing the recall.
96 Interface param is either OPAC or INTRANET
97 Send a RETURN_RECALLED_ITEM notice.
98 Add statistics and logs.
99 #FIXME: Add recallnotes and priority when staff-side recalls is added
104 my ( $self, $params ) = @_;
106 my $patron = $params->{patron};
107 my $biblio = $params->{biblio};
108 return if ( !defined($patron) or !defined($biblio) );
109 my $branchcode = $params->{branchcode};
110 $branchcode ||= $patron->branchcode;
111 my $item = $params->{item};
112 my $itemnumber = $item ? $item->itemnumber : undef;
113 my $expirationdate = $params->{expirationdate};
114 my $interface = $params->{interface};
116 if ( $expirationdate ){
117 my $now = dt_from_string;
118 $expirationdate = dt_from_string($expirationdate)->set({ hour => $now->hour, minute => $now->minute, second => $now->second });
121 my $recall_request = Koha::Recall->new({
122 patron_id => $patron->borrowernumber,
123 created_date => dt_from_string(),
124 biblio_id => $biblio->biblionumber,
125 pickup_library_id => $branchcode,
126 status => 'requested',
127 item_id => defined $itemnumber ? $itemnumber : undef,
128 expiration_date => $expirationdate,
129 item_level => defined $itemnumber ? 1 : 0,
132 if (defined $recall_request->id){ # successful recall
133 my $recall = Koha::Recalls->find( $recall_request->id );
135 # get checkout and adjust due date based on circulation rules
136 my $checkout = $recall->checkout;
137 my $recall_due_date_interval = Koha::CirculationRules->get_effective_rule({
138 categorycode => $checkout->patron->categorycode,
139 itemtype => $checkout->item->effective_itemtype,
140 branchcode => $branchcode,
141 rule_name => 'recall_due_date_interval',
143 my $due_interval = defined $recall_due_date_interval ? $recall_due_date_interval->rule_value : 5;
144 my $timestamp = dt_from_string( $recall->timestamp );
145 my $due_date = $timestamp->add( days => $due_interval );
146 $checkout->update({ date_due => $due_date });
148 # get itemnumber of most relevant checkout if a biblio-level recall
149 unless ( $recall->item_level ) { $itemnumber = $checkout->itemnumber; }
151 # send notice to user with recalled item checked out
152 my $letter = C4::Letters::GetPreparedLetter (
153 module => 'circulation',
154 letter_code => 'RETURN_RECALLED_ITEM',
155 branchcode => $recall->pickup_library_id,
157 biblio => $biblio->biblionumber,
158 borrowers => $checkout->borrowernumber,
159 items => $itemnumber,
160 issues => $itemnumber,
164 C4::Message->enqueue( $letter, $checkout->patron, 'email' );
166 $item = Koha::Items->find( $itemnumber );
167 # add to statistics table
168 C4::Stats::UpdateStats(
170 branch => C4::Context->userenv->{'branch'},
172 itemnumber => $itemnumber,
173 borrowernumber => $recall->patron_id,
174 itemtype => $item->effective_itemtype,
175 ccode => $item->ccode,
176 categorycode => $checkout->patron->categorycode
181 'after_recall_action',
184 payload => { recall => $recall->get_from_storage }, # FIXME Bug 32107
189 C4::Log::logaction( 'RECALLS', 'CREATE', $recall->id, "Recall requested by borrower #" . $recall->patron_id, $interface ) if ( C4::Context->preference('RecallsLog') );
191 return ( $recall, $due_interval, $due_date );
194 # unable to add recall
200 my $message = Koha::Recalls->move_recall({
201 recall_id = $recall_id,
203 item => $item_object,
204 borrowernumber => $borrowernumber,
207 A patron is attempting to check out an item that has been recalled by another patron.
208 If the recall is requested/overdue, they have the option of cancelling the recall.
209 If the recall is waiting, they also have the option of reverting the waiting status.
211 We can also fulfill the recall here if the recall is placed by this borrower.
213 recall_id = ID of the recall to perform the action on
214 action = either cancel or revert
215 item = item object that the patron is attempting to check out
216 borrowernumber = borrowernumber of the patron that is attemptig to check out
221 my ( $self, $params ) = @_;
223 my $recall_id = $params->{recall_id};
224 my $action = $params->{action};
225 return 'no recall_id provided' if ( !defined $recall_id );
226 my $item = $params->{item};
227 my $borrowernumber = $params->{borrowernumber};
229 my $message = 'no action provided';
231 if ( $action and $action eq 'cancel' ) {
232 my $recall = Koha::Recalls->find( $recall_id );
233 $recall->set_cancelled;
234 $message = 'cancelled';
235 } elsif ( $action and $action eq 'revert' ) {
236 my $recall = Koha::Recalls->find( $recall_id );
237 $recall->revert_waiting;
238 $message = 'reverted';
241 if ( $message eq 'no action provided' and $item and $item->biblionumber and $borrowernumber ) {
242 # move_recall was not called to revert or cancel, but was called to fulfill
243 my $recall = Koha::Recalls->search(
245 patron_id => $borrowernumber,
246 biblio_id => $item->biblionumber,
247 item_id => [ $item->itemnumber, undef ],
250 { order_by => { -asc => 'created_date' } }
253 $recall->set_fulfilled;
254 $message = 'fulfilled';
261 =head2 Internal methods
276 return 'Koha::Recall';