Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Copyright 2020 Aleisha Amohia <aleisha@catalyst.net.nz> |
6 |
# |
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. |
11 |
# |
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. |
16 |
# |
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>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use CGI qw ( -utf8 ); |
22 |
use DateTime; |
23 |
use C4::Auth; |
24 |
use C4::Output; |
25 |
use C4::Context; |
26 |
use C4::Letters; |
27 |
use C4::Stats; |
28 |
use Koha::Items; |
29 |
use Koha::Recall; |
30 |
use Koha::Recalls; |
31 |
use Koha::DateUtils; |
32 |
use Koha::Patrons; |
33 |
|
34 |
my $query = new CGI; |
35 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
36 |
{ |
37 |
template_name => "opac-recall.tt", |
38 |
query => $query, |
39 |
type => "opac", |
40 |
authnotrequired => 0, |
41 |
} |
42 |
); |
43 |
|
44 |
my $op = $query->param('op') || ''; |
45 |
my $biblionumber = $query->param('biblionumber'); |
46 |
|
47 |
unless ( C4::Context->preference('UseRecalls') ) { |
48 |
print $query->redirect("/cgi-bin/koha/opac-recall.pl?biblionumber=$biblionumber&nosyspref=1"); |
49 |
exit; |
50 |
} |
51 |
|
52 |
my $biblio = Koha::Biblios->find($biblionumber); |
53 |
my $borrower = Koha::Patrons->find($borrowernumber); |
54 |
my $error; |
55 |
|
56 |
unless ( $biblio->can_be_recalled({ patron => $borrower }) ) { $error = 'unavailable'; } |
57 |
|
58 |
my $items = Koha::Items->search({ biblionumber => $biblionumber }); |
59 |
|
60 |
# check if already recalled |
61 |
my $recalled = Koha::Recalls->search({ biblionumber => $biblionumber, borrowernumber => $borrowernumber, old => undef })->count; |
62 |
if ( defined $recalled ) { |
63 |
my $recalls_per_record = Koha::CirculationRules->get_effective_rule({ categorycode => $borrower->categorycode, branchcode => undef, itemtype => undef, rule_name => 'recalls_per_record' }); |
64 |
|
65 |
if ( defined $recalls_per_record and $recalled >= $recalls_per_record->{rule_value} ){ |
66 |
$error = 'duplicate'; |
67 |
} |
68 |
} |
69 |
|
70 |
# submitting recall request |
71 |
if ($op eq 'request'){ |
72 |
|
73 |
if (!defined $borrowernumber){ |
74 |
# can't place recall if not logged in |
75 |
$error = 'notloggedin'; |
76 |
print $query->redirect("/cgi-bin/koha/opac-detail.pl?biblionumber=$biblionumber"); |
77 |
|
78 |
} elsif ( defined $error and $error eq 'unavailable' ){ |
79 |
# no items available for recall |
80 |
print $query->redirect("/cgi-bin/koha/opac-recall.pl?biblionumber=$biblionumber&error=unavailable"); |
81 |
|
82 |
} elsif ( !defined $error ){ |
83 |
# can recall |
84 |
|
85 |
# check if bib-level or item-level recall |
86 |
my $level = $query->param('type'); |
87 |
my $itemnumber; |
88 |
if ( $level eq 'itemlevel' ) { $itemnumber = $query->param('itemnumber'); } |
89 |
|
90 |
my $expirationdate = scalar $query->param('expirationdate'); |
91 |
if ( $expirationdate ){ |
92 |
my $now = dt_from_string; |
93 |
$expirationdate = dt_from_string($expirationdate)->set({ hour => $now->hour, minute => $now->minute, second => $now->second }); |
94 |
} |
95 |
|
96 |
my $pickup = $query->param('pickup') || $borrower->branchcode; |
97 |
|
98 |
# attempt recall |
99 |
my $recall_request; |
100 |
if ( $item->can_be_recalled({ patron => $borrower }) ) { |
101 |
$recall_request = Koha::Recall->new({ |
102 |
borrowernumber => $borrowernumber, |
103 |
recalldate => dt_from_string(), |
104 |
biblionumber => $biblio->biblionumber, |
105 |
branchcode => $pickup, |
106 |
status => 'R', |
107 |
itemnumber => defined $itemnumber ? $itemnumber : undef, |
108 |
itemtype => $item->itype, |
109 |
expirationdate => $expirationdate, |
110 |
item_level_recall => defined $itemnumber ? 1 : 0 |
111 |
})->store; |
112 |
} |
113 |
|
114 |
if (defined $recall_request->recall_id){ # successful recall |
115 |
my $recall = Koha::Recalls->find($recall_request->recall_id); |
116 |
|
117 |
# set up vars |
118 |
my $timestamp = dt_from_string( $recall->timestamp ); |
119 |
my $due_date; |
120 |
my $checkout_borrower; |
121 |
|
122 |
# get checkout and adjust due date |
123 |
my $checkout = $recall->checkout; |
124 |
my $recall_due_date_interval = Koha::CirculationRules->get_effective_rule({ |
125 |
categorycode => $checkout->patron->categorycode, |
126 |
itemtype => $checkout->item->effective_itemtype, |
127 |
branchcode => C4::Context->userenv->{'branch'}, |
128 |
rule_name => 'recall_due_date_interval', |
129 |
}); |
130 |
unless ( defined $recall_due_date_interval ) { $recall_due_date_interval->{rule_value} = 5 }; |
131 |
$due_date = $timestamp->add( days => $recall_due_date_interval->{rule_value} ); |
132 |
$checkout->update({ date_due => $due_date }); |
133 |
unless ( $recall->item_level_recall ) { $itemnumber = $checkout->itemnumber; } |
134 |
|
135 |
# send notice to user with recalled item checked out |
136 |
my $letter = C4::Letters::GetPreparedLetter ( |
137 |
module => 'circulation', |
138 |
letter_code => 'RETURN_RECALLED_ITEM', |
139 |
branchcode => $recall->branchcode, |
140 |
tables => { |
141 |
biblio => $biblio->biblionumber, |
142 |
borrowers => $checkout->borrowernumber, |
143 |
items => $itemnumber, |
144 |
issues => $itemnumber, |
145 |
}, |
146 |
); |
147 |
|
148 |
C4::Message->enqueue($letter, $checkout->patron->unblessed, 'email'); |
149 |
|
150 |
my $item = Koha::Items->find($itemnumber); |
151 |
# add to statistics table |
152 |
UpdateStats({ |
153 |
branch => C4::Context->userenv->{'branch'}, |
154 |
type => 'recall', |
155 |
itemnumber => $itemnumber, |
156 |
borrowernumber => $borrowernumber, |
157 |
itemtype => $item->effective_itemtype, |
158 |
ccode => $item->ccode, |
159 |
}); |
160 |
|
161 |
# add action log |
162 |
C4::Log::logaction( 'RECALLS', 'CREATE', $recall->recall_id, "Recall requested by borrower #" . $recall->borrowernumber, 'OPAC') if ( C4::Context->preference('RecallsLog') ); |
163 |
$template->param( |
164 |
success => 1, |
165 |
due_interval => $recall_due_date_interval->{rule_value}, |
166 |
due_date => $checkout->date_due, |
167 |
); |
168 |
} else { |
169 |
$error = 'failed'; |
170 |
} |
171 |
} |
172 |
} elsif ($op eq 'cancel'){ |
173 |
my $recall_id = $query->param('recall_id'); |
174 |
Koha::Recalls->find($recall_id)->set_cancelled; |
175 |
print $query->redirect('/cgi-bin/koha/opac-user.pl'); |
176 |
} |
177 |
|
178 |
my $branches = Koha::Libraries->search(); |
179 |
my $single_branch_mode = $branches->count == 1; |
180 |
|
181 |
$template->param( |
182 |
biblio => $biblio, |
183 |
error => $error, |
184 |
items => $items, |
185 |
single_branch_mode => $single_branch_mode, |
186 |
branches => $branches, |
187 |
); |
188 |
|
189 |
output_with_http_headers $query, $cookie, $template->output, 'html'; |
190 |
|