Bug 20661: Shortcut circ scripts if a blocking error appeared
[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;
23 use C4::Auth;
24 use C4::Utils::DataTables::Members;
25 use Koha::Biblios;
26 use Koha::Patrons;
27 use Koha::ArticleRequests;
28
29 my $cgi = new CGI;
30
31 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
32     {
33         template_name   => "circ/request-article.tt",
34         query           => $cgi,
35         type            => "intranet",
36         authnotrequired => 0,
37         flagsrequired   => { circulate => 'circulate_remaining_permissions' },
38     }
39 );
40
41 my $action            = $cgi->param('action') || q{};
42 my $biblionumber      = $cgi->param('biblionumber');
43 my $patron_cardnumber = $cgi->param('patron_cardnumber');
44 my $patron_id         = $cgi->param('patron_id');
45
46 my $biblio = Koha::Biblios->find($biblionumber);
47 output_and_exit( $cgi, $cookie, $template, 'unknown_biblio')
48     unless $biblio;
49
50 my $patron =
51     $patron_id         ? Koha::Patrons->find($patron_id)
52   : $patron_cardnumber ? Koha::Patrons->find( { cardnumber => $patron_cardnumber } )
53   : undef;
54
55 if ( $action eq 'create' ) {
56     my $borrowernumber = $cgi->param('borrowernumber');
57     my $branchcode     = $cgi->param('branchcode');
58
59     my $itemnumber   = $cgi->param('itemnumber')   || undef;
60     my $title        = $cgi->param('title')        || undef;
61     my $author       = $cgi->param('author')       || undef;
62     my $volume       = $cgi->param('volume')       || undef;
63     my $issue        = $cgi->param('issue')        || undef;
64     my $date         = $cgi->param('date')         || undef;
65     my $pages        = $cgi->param('pages')        || undef;
66     my $chapters     = $cgi->param('chapters')     || undef;
67     my $patron_notes = $cgi->param('patron_notes') || undef;
68
69     my $ar = Koha::ArticleRequest->new(
70         {
71             borrowernumber => $borrowernumber,
72             biblionumber   => $biblionumber,
73             branchcode     => $branchcode,
74             itemnumber     => $itemnumber,
75             title          => $title,
76             author         => $author,
77             volume         => $volume,
78             issue          => $issue,
79             date           => $date,
80             pages          => $pages,
81             chapters       => $chapters,
82             patron_notes   => $patron_notes,
83         }
84     )->store();
85
86 }
87
88 if ( !$patron && $patron_cardnumber ) {
89     my $results = C4::Utils::DataTables::Members::search(
90         {
91             searchmember => $patron_cardnumber,
92             dt_params    => { iDisplayLength => -1 },
93         }
94     );
95
96     my $patrons = $results->{patrons};
97
98     if ( scalar @$patrons == 1 ) {
99         $patron = Koha::Patrons->find( $patrons->[0]->{borrowernumber} );
100     }
101     elsif (@$patrons) {
102         $template->param( patrons => $patrons );
103     }
104     else {
105         $template->param( no_patrons_found => $patron_cardnumber );
106     }
107 }
108
109 $template->param(
110     biblio => $biblio,
111     patron => $patron,
112 );
113
114 output_html_with_http_headers $cgi, $cookie, $template->output;