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>.
26 use C4::Stats qw( UpdateStats );
28 use base qw(Koha::Objects);
32 Koha::Recalls - Koha Recalls Object set class
36 =head2 Internal methods
42 my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
43 patron => $patron_object,
44 biblio => $biblio_object,
45 branchcode => $branchcode,
47 expirationdate => $expirationdate,
51 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.
52 Interface param is either OPAC or INTRANET
53 Send a RETURN_RECALLED_ITEM notice.
54 Add statistics and logs.
55 #FIXME: Add recallnotes and priority when staff-side recalls is added
60 my ( $self, $params ) = @_;
62 my $patron = $params->{patron};
63 my $biblio = $params->{biblio};
64 return if ( !defined($patron) or !defined($biblio) );
65 my $branchcode = $params->{branchcode};
66 $branchcode ||= $patron->branchcode;
67 my $item = $params->{item};
68 my $itemnumber = $item ? $item->itemnumber : undef;
69 my $expirationdate = $params->{expirationdate};
70 my $interface = $params->{interface};
72 if ( $expirationdate ){
73 my $now = dt_from_string;
74 $expirationdate = dt_from_string($expirationdate)->set({ hour => $now->hour, minute => $now->minute, second => $now->second });
77 my $recall_request = Koha::Recall->new({
78 borrowernumber => $patron->borrowernumber,
79 recalldate => dt_from_string(),
80 biblionumber => $biblio->biblionumber,
81 branchcode => $branchcode,
83 itemnumber => defined $itemnumber ? $itemnumber : undef,
84 expirationdate => $expirationdate,
85 item_level_recall => defined $itemnumber ? 1 : 0,
88 if (defined $recall_request->recall_id){ # successful recall
89 my $recall = Koha::Recalls->find( $recall_request->recall_id );
91 # get checkout and adjust due date based on circulation rules
92 my $checkout = $recall->checkout;
93 my $recall_due_date_interval = Koha::CirculationRules->get_effective_rule({
94 categorycode => $checkout->patron->categorycode,
95 itemtype => $checkout->item->effective_itemtype,
96 branchcode => $branchcode,
97 rule_name => 'recall_due_date_interval',
99 my $due_interval = defined $recall_due_date_interval ? $recall_due_date_interval->rule_value : 5;
100 my $timestamp = dt_from_string( $recall->timestamp );
101 my $due_date = $timestamp->add( days => $due_interval );
102 $checkout->update({ date_due => $due_date });
104 # get itemnumber of most relevant checkout if a biblio-level recall
105 unless ( $recall->item_level_recall ) { $itemnumber = $checkout->itemnumber; }
107 # send notice to user with recalled item checked out
108 my $letter = C4::Letters::GetPreparedLetter (
109 module => 'circulation',
110 letter_code => 'RETURN_RECALLED_ITEM',
111 branchcode => $recall->branchcode,
113 biblio => $biblio->biblionumber,
114 borrowers => $checkout->borrowernumber,
115 items => $itemnumber,
116 issues => $itemnumber,
120 C4::Message->enqueue( $letter, $checkout->patron->unblessed, 'email' );
122 $item = Koha::Items->find( $itemnumber );
123 # add to statistics table
125 branch => C4::Context->userenv->{'branch'},
127 itemnumber => $itemnumber,
128 borrowernumber => $recall->borrowernumber,
129 itemtype => $item->effective_itemtype,
130 ccode => $item->ccode,
134 C4::Log::logaction( 'RECALLS', 'CREATE', $recall->recall_id, "Recall requested by borrower #" . $recall->borrowernumber, $interface ) if ( C4::Context->preference('RecallsLog') );
136 return ( $recall, $due_interval, $due_date );
139 # unable to add recall
145 my $message = Koha::Recalls->move_recall({
146 recall_id = $recall_id,
148 item => $item_object,
149 borrowernumber => $borrowernumber,
152 A patron is attempting to check out an item that has been recalled by another patron. If the recall is requested/overdue, they have the option of cancelling the recall. If the recall is waiting, they also have the option of reverting the waiting status.
154 We can also fulfill the recall here if the recall is placed by this borrower.
156 recall_id = ID of the recall to perform the action on
157 action = either cancel or revert
158 item = item object that the patron is attempting to check out
159 borrowernumber = borrowernumber of the patron that is attemptig to check out
164 my ( $self, $params ) = @_;
166 my $recall_id = $params->{recall_id};
167 my $action = $params->{action};
168 return 'no recall_id provided' if ( !defined $recall_id );
169 my $item = $params->{item};
170 my $borrowernumber = $params->{borrowernumber};
172 my $message = 'no action provided';
174 if ( $action and $action eq 'cancel' ) {
175 my $recall = Koha::Recalls->find( $recall_id );
176 $recall->set_cancelled;
177 $message = 'cancelled';
178 } elsif ( $action and $action eq 'revert' ) {
179 my $recall = Koha::Recalls->find( $recall_id );
180 $recall->revert_waiting;
181 $message = 'reverted';
184 if ( $message eq 'no action provided' and $item and $item->biblionumber and $borrowernumber ) {
185 # move_recall was not called to revert or cancel, but was called to fulfill
186 my $recall = Koha::Recalls->find({ borrowernumber => $borrowernumber, biblionumber => $item->biblionumber, itemnumber => [ $item->itemnumber, undef ], old => undef });
188 $recall->set_finished;
189 $message = 'fulfilled';
209 return 'Koha::Recall';