Bug 26352: Switch from using call() to call_recursive()
[koha.git] / circ / request-article.pl
1 #!/usr/bin/perl
2
3 # Copyright 2015 ByWater Solutions
4 #
5 # This file is part of Koha.
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
22 use C4::Output qw( output_and_exit output_html_with_http_headers );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Utils::DataTables::Members;
25 use C4::Search qw( enabled_staff_search_views );
26 use C4::Serials qw( CountSubscriptionFromBiblionumber );
27 use Koha::Biblios;
28 use Koha::Logger;
29 use Koha::Patrons;
30 use Koha::ArticleRequests;
31
32 use Scalar::Util qw( blessed );
33 use Try::Tiny;
34
35 my $cgi = CGI->new;
36
37 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
38     {
39         template_name   => "circ/request-article.tt",
40         query           => $cgi,
41         type            => "intranet",
42         flagsrequired   => { circulate => 'circulate_remaining_permissions' },
43     }
44 );
45
46 my $action            = $cgi->param('action') || q{};
47 my $biblionumber      = $cgi->param('biblionumber');
48 my $patron_cardnumber = $cgi->param('patron_cardnumber');
49 my $patron_id         = $cgi->param('patron_id');
50
51 my $biblio = Koha::Biblios->find($biblionumber);
52 output_and_exit( $cgi, $cookie, $template, 'unknown_biblio')
53     unless $biblio;
54
55 my $patron =
56     $patron_id         ? Koha::Patrons->find($patron_id)
57   : $patron_cardnumber ? Koha::Patrons->find( { cardnumber => $patron_cardnumber } )
58   : undef;
59
60 if ( $action eq 'create' ) {
61     my $borrowernumber = $cgi->param('borrowernumber');
62     my $branchcode     = $cgi->param('branchcode');
63
64     my $itemnumber   = $cgi->param('itemnumber')   || undef;
65     my $title        = $cgi->param('title')        || undef;
66     my $author       = $cgi->param('author')       || undef;
67     my $volume       = $cgi->param('volume')       || undef;
68     my $issue        = $cgi->param('issue')        || undef;
69     my $date         = $cgi->param('date')         || undef;
70     my $pages        = $cgi->param('pages')        || undef;
71     my $chapters     = $cgi->param('chapters')     || undef;
72     my $patron_notes = $cgi->param('patron_notes') || undef;
73     my $format       = $cgi->param('format')       || undef;
74
75     try {
76         my $ar = Koha::ArticleRequest->new(
77             {
78                 borrowernumber => $borrowernumber,
79                 biblionumber   => $biblionumber,
80                 branchcode     => $branchcode,
81                 itemnumber     => $itemnumber,
82                 title          => $title,
83                 author         => $author,
84                 volume         => $volume,
85                 issue          => $issue,
86                 date           => $date,
87                 pages          => $pages,
88                 chapters       => $chapters,
89                 patron_notes   => $patron_notes,
90                 format         => $format,
91             }
92         )->request;
93     } catch {
94         if ( blessed $_ and $_->isa('Koha::Exceptions::ArticleRequest::LimitReached') ) {
95             $template->param(
96                 error_message => 'article_request_limit_reached'
97             );
98         }
99         else {
100             Koha::Logger->get->debug("Unhandled exception when placing an article request ($_)");
101             $template->param(
102                 error_message => 'article_request_unhandled_exception'
103             );
104         }
105     };
106
107 }
108
109 if ( !$patron && $patron_cardnumber ) {
110     my $results = C4::Utils::DataTables::Members::search(
111         {
112             searchmember => $patron_cardnumber,
113             dt_params    => { iDisplayLength => -1 },
114         }
115     );
116
117     my $patrons = $results->{patrons};
118
119     if ( scalar @$patrons == 1 ) {
120         $patron = Koha::Patrons->find( $patrons->[0]->{borrowernumber} );
121     }
122     elsif (@$patrons) {
123         $template->param( patrons => $patrons );
124     }
125     else {
126         $template->param( no_patrons_found => $patron_cardnumber );
127     }
128 }
129
130 if( $patron && !$patron->can_request_article) {
131     $patron = undef;
132     $template->param(
133         error_message => 'article_request_limit_reached'
134     );
135 }
136
137 $template->param(
138     biblio => $biblio,
139     patron => $patron,
140     subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber),
141     C4::Search::enabled_staff_search_views,
142 );
143
144 output_html_with_http_headers $cgi, $cookie, $template->output;