Bug 19532: Recalls on OPAC
[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 = new CGI;
27 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
28     {
29         template_name   => "opac-recall.tt",
30         query           => $query,
31         type            => "opac",
32         authnotrequired => 0,
33     }
34 );
35
36 my $op = $query->param('op') || '';
37 my $biblionumber = $query->param('biblionumber');
38 my $biblio = Koha::Biblios->find( $biblionumber );
39
40 if ( C4::Context->preference('UseRecalls') ) {
41
42     my $patron = Koha::Patrons->find( $borrowernumber );
43     my $error;
44
45     unless ( $biblio->can_be_recalled({ patron => $patron }) ) { $error = 'unavailable'; }
46
47     my $items = Koha::Items->search({ biblionumber => $biblionumber });
48
49     # check if already recalled
50     my $recalled = scalar $biblio->recalls;
51     if ( defined $recalled and $recalled > 0 ) {
52         my $recalls_per_record = Koha::CirculationRules->get_effective_rule({
53             categorycode => $patron->categorycode,
54             branchcode => undef,
55             itemtype => undef,
56             rule_name => 'recalls_per_record'
57         });
58         if ( defined $recalls_per_record and $recalls_per_record->rule_value and $recalled >= $recalls_per_record->rule_value ){
59             $error = 'duplicate';
60         }
61     }
62
63     # submitting recall request
64     if ($op eq 'request'){
65
66         if ( defined $error and $error eq 'unavailable' ){
67             # no items available for recall
68             print $query->redirect("/cgi-bin/koha/opac-recall.pl?biblionumber=$biblionumber&error=unavailable");
69
70         } elsif ( !defined $error ){
71             # can recall
72
73             my $level = $query->param('type');
74             my $pickuploc = $query->param('pickup');
75             my $expdate = $query->param('expirationdate');
76             my $itemnumber = $query->param('itemnumber');
77
78             my ( $recall, $due_interval, $due_date );
79             if ( $level eq 'itemlevel' and defined $itemnumber ) {
80                 my $item = Koha::Items->find( $itemnumber );
81                 if ( $item->can_be_recalled({ patron => $patron }) ) {
82                     ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
83                         patron => $patron,
84                         biblio => $biblio,
85                         branchcode => $pickuploc,
86                         item => $item,
87                         expirationdate => $expdate,
88                         interface => 'OPAC',
89                     });
90                 } else {
91                     $error = 'cannot';
92                 }
93             } else {
94                 if ( $biblio->can_be_recalled({ patron => $patron }) ) {
95                     ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
96                         patron => $patron,
97                         biblio => $biblio,
98                         branchcode => $pickuploc,
99                         expirationdate => $expdate,
100                         interface => 'OPAC',
101                     });
102                 } else {
103                     $error = 'cannot';
104                 }
105             }
106             if ( defined $recall ) {
107                 $template->param(
108                     success => 1,
109                     due_interval => $due_interval,
110                     due_date => $due_date,
111                 );
112             } else {
113                 $error = 'failed';
114             }
115         }
116     } elsif ($op eq 'cancel'){
117         my $recall_id = $query->param('recall_id');
118         Koha::Recalls->find( $recall_id )->set_cancelled;
119         print $query->redirect('/cgi-bin/koha/opac-user.pl');
120     }
121
122     my $branches = Koha::Libraries->search();
123     my $single_branch_mode = $branches->count == 1;
124
125     $template->param(
126         biblio => $biblio,
127         error => $error,
128         items => $items,
129         single_branch_mode => $single_branch_mode,
130         branches => $branches,
131     );
132
133 } else {
134     # UseRecalls disabled
135     $template->param(
136         nosyspref => 1,
137         biblio => $biblio,
138     );
139 }
140
141 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };