3 # This file is part of Koha.
5 # Copyright 2020 Aleisha Amohia <aleisha@catalyst.net.nz>
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>.
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
27 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
29 template_name => "opac-recall.tt",
35 my $op = $query->param('op') || '';
36 my $biblionumber = $query->param('biblionumber');
37 my $biblio = Koha::Biblios->find( $biblionumber );
39 if ( C4::Context->preference('UseRecalls') ) {
41 my $patron = Koha::Patrons->find( $borrowernumber );
44 unless ( $biblio->can_be_recalled({ patron => $patron }) ) { $error = 'unavailable'; }
46 my $items = Koha::Items->search({ biblionumber => $biblionumber })->as_list;
48 # check if already recalled
49 my $recalled = $biblio->recalls->filter_by_current->search({ patron_id => $borrowernumber })->count;
50 if ( defined $recalled and $recalled > 0 ) {
51 my $recalls_per_record = Koha::CirculationRules->get_effective_rule({
52 categorycode => $patron->categorycode,
55 rule_name => 'recalls_per_record'
57 if ( defined $recalls_per_record and $recalls_per_record->rule_value and $recalled >= $recalls_per_record->rule_value ){
62 # submitting recall request
63 if ($op eq 'cud-request'){
65 if ( defined $error and $error eq 'unavailable' ){
66 # no items available for recall
67 print $query->redirect("/cgi-bin/koha/opac-recall.pl?biblionumber=$biblionumber&error=unavailable");
69 } elsif ( !defined $error ){
72 my $level = $query->param('type');
73 my $pickuploc = $query->param('pickup');
74 my $expdate = $query->param('expirationdate');
75 my $itemnumber = $query->param('itemnumber');
77 my ( $recall, $due_interval, $due_date );
78 if ( $level eq 'itemlevel' and defined $itemnumber ) {
79 my $item = Koha::Items->find( $itemnumber );
80 if ( $item->can_be_recalled({ patron => $patron }) ) {
81 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
84 branchcode => $pickuploc,
86 expirationdate => $expdate,
93 if ( $biblio->can_be_recalled({ patron => $patron }) ) {
94 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
97 branchcode => $pickuploc,
98 expirationdate => $expdate,
105 if ( defined $recall ) {
108 due_interval => $due_interval,
109 due_date => $due_date,
115 } elsif ($op eq 'cud-cancel'){
116 my $recall_id = $query->param('recall_id');
117 Koha::Recalls->find( $recall_id )->set_cancelled;
118 print $query->redirect('/cgi-bin/koha/opac-user.pl');
121 my $branches = Koha::Libraries->search();
122 my $single_branch_mode = $branches->count == 1;
128 single_branch_mode => $single_branch_mode,
129 branches => $branches,
133 # UseRecalls disabled
140 $template->param( recallsview => 1 );
142 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };