Bug 32482: (follow-up) Add markup comments
[koha.git] / opac / opac-recall.pl
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 C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_html_with_http_headers );
24 use C4::Context;
25
26 my $query = CGI->new;
27 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
28     {
29         template_name   => "opac-recall.tt",
30         query           => $query,
31         type            => "opac",
32     }
33 );
34
35 my $op = $query->param('op') || '';
36 my $biblionumber = $query->param('biblionumber');
37 my $biblio = Koha::Biblios->find( $biblionumber );
38
39 if ( C4::Context->preference('UseRecalls') ) {
40
41     my $patron = Koha::Patrons->find( $borrowernumber );
42     my $error;
43
44     unless ( $biblio->can_be_recalled({ patron => $patron }) ) { $error = 'unavailable'; }
45
46     my $items = Koha::Items->search({ biblionumber => $biblionumber })->as_list;
47
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,
53             branchcode => undef,
54             itemtype => undef,
55             rule_name => 'recalls_per_record'
56         });
57         if ( defined $recalls_per_record and $recalls_per_record->rule_value and $recalled >= $recalls_per_record->rule_value ){
58             $error = 'duplicate';
59         }
60     }
61
62     # submitting recall request
63     if ($op eq 'request'){
64
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");
68
69         } elsif ( !defined $error ){
70             # can recall
71
72             my $level = $query->param('type');
73             my $pickuploc = $query->param('pickup');
74             my $expdate = $query->param('expirationdate');
75             my $itemnumber = $query->param('itemnumber');
76
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({
82                         patron => $patron,
83                         biblio => $biblio,
84                         branchcode => $pickuploc,
85                         item => $item,
86                         expirationdate => $expdate,
87                         interface => 'OPAC',
88                     });
89                 } else {
90                     $error = 'cannot';
91                 }
92             } else {
93                 if ( $biblio->can_be_recalled({ patron => $patron }) ) {
94                     ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
95                         patron => $patron,
96                         biblio => $biblio,
97                         branchcode => $pickuploc,
98                         expirationdate => $expdate,
99                         interface => 'OPAC',
100                     });
101                 } else {
102                     $error = 'cannot';
103                 }
104             }
105             if ( defined $recall ) {
106                 $template->param(
107                     success => 1,
108                     due_interval => $due_interval,
109                     due_date => $due_date,
110                 );
111             } else {
112                 $error = 'failed';
113             }
114         }
115     } elsif ($op eq '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');
119     }
120
121     my $branches = Koha::Libraries->search();
122     my $single_branch_mode = $branches->count == 1;
123
124     $template->param(
125         biblio => $biblio,
126         error => $error,
127         items => $items,
128         single_branch_mode => $single_branch_mode,
129         branches => $branches,
130     );
131
132 } else {
133     # UseRecalls disabled
134     $template->param(
135         nosyspref => 1,
136         biblio => $biblio,
137     );
138 }
139
140 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };