Bug 27945: Add limit article request feature
[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::Patrons;
29 use Koha::ArticleRequests;
30 use Try::Tiny;
31
32 my $cgi = CGI->new;
33
34 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
35     {
36         template_name   => "circ/request-article.tt",
37         query           => $cgi,
38         type            => "intranet",
39         flagsrequired   => { circulate => 'circulate_remaining_permissions' },
40     }
41 );
42
43 my $action            = $cgi->param('action') || q{};
44 my $biblionumber      = $cgi->param('biblionumber');
45 my $patron_cardnumber = $cgi->param('patron_cardnumber');
46 my $patron_id         = $cgi->param('patron_id');
47
48 my $biblio = Koha::Biblios->find($biblionumber);
49 output_and_exit( $cgi, $cookie, $template, 'unknown_biblio')
50     unless $biblio;
51
52 my $patron =
53     $patron_id         ? Koha::Patrons->find($patron_id)
54   : $patron_cardnumber ? Koha::Patrons->find( { cardnumber => $patron_cardnumber } )
55   : undef;
56
57 if ( $action eq 'create' ) {
58     my $borrowernumber = $cgi->param('borrowernumber');
59     my $branchcode     = $cgi->param('branchcode');
60
61     my $itemnumber   = $cgi->param('itemnumber')   || undef;
62     my $title        = $cgi->param('title')        || undef;
63     my $author       = $cgi->param('author')       || undef;
64     my $volume       = $cgi->param('volume')       || undef;
65     my $issue        = $cgi->param('issue')        || undef;
66     my $date         = $cgi->param('date')         || undef;
67     my $pages        = $cgi->param('pages')        || undef;
68     my $chapters     = $cgi->param('chapters')     || undef;
69     my $patron_notes = $cgi->param('patron_notes') || undef;
70     my $format       = $cgi->param('format')       || undef;
71
72     try {
73         my $ar = Koha::ArticleRequest->new(
74             {
75                 borrowernumber => $borrowernumber,
76                 biblionumber   => $biblionumber,
77                 branchcode     => $branchcode,
78                 itemnumber     => $itemnumber,
79                 title          => $title,
80                 author         => $author,
81                 volume         => $volume,
82                 issue          => $issue,
83                 date           => $date,
84                 pages          => $pages,
85                 chapters       => $chapters,
86                 patron_notes   => $patron_notes,
87                 format         => $format,
88             }
89         )->store();
90     } catch {
91         $template->param(
92             error_message => $_->{message}
93         );
94     };
95
96 }
97
98 if ( !$patron && $patron_cardnumber ) {
99     my $results = C4::Utils::DataTables::Members::search(
100         {
101             searchmember => $patron_cardnumber,
102             dt_params    => { iDisplayLength => -1 },
103         }
104     );
105
106     my $patrons = $results->{patrons};
107
108     if ( scalar @$patrons == 1 ) {
109         $patron = Koha::Patrons->find( $patrons->[0]->{borrowernumber} );
110     }
111     elsif (@$patrons) {
112         $template->param( patrons => $patrons );
113     }
114     else {
115         $template->param( no_patrons_found => $patron_cardnumber );
116     }
117 }
118
119 if( $patron && !$patron->can_request_article) {
120     $patron = undef;
121     $template->param(
122         error_message => 'Patron cannot request more articles for today'
123     );
124 }
125
126 $template->param(
127     biblio => $biblio,
128     patron => $patron,
129     subscriptionsnumber => CountSubscriptionFromBiblionumber($biblionumber),
130     C4::Search::enabled_staff_search_views,
131 );
132
133 output_html_with_http_headers $cgi, $cookie, $template->output;