Bug 17600: Standardize our EXPORT_OK
[srvgit] / opac / sci / sci-main.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21
22 use C4::Auth qw( get_template_and_user );
23 use C4::Circulation qw( AddReturn );
24 use C4::Output qw( output_html_with_http_headers );
25 use Koha::Items;
26
27 use List::MoreUtils qw( uniq );
28 use Try::Tiny qw( catch try );
29
30 my $cgi = CGI->new;
31
32 # 404 if feature is disabled
33 unless ( C4::Context->preference('SelfCheckInModule') ) {
34     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
35     exit;
36 }
37
38 # Let Auth know this is a SCI context
39 $cgi->param( -name => 'sci_user_login', -values => [1] );
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "sci/sci-main.tt",
44         flagsrequired   => { self_check => 'self_checkin_module' },
45         query           => $cgi,
46         type            => "opac"
47     }
48 );
49
50 my $op = $cgi->param('op') // '';
51
52 if ( $op eq 'check_in' ) {
53     ## Get the barcodes, perform some basic validation
54     # Remove empty ones
55     my @barcodes = grep { $_ ne '' } $cgi->multi_param('barcode');
56
57     # Remove duplicates
58     @barcodes = uniq @barcodes;
59
60     # Read the library we are logged in from userenv
61     my $library = C4::Context->userenv->{'branch'};
62     my @success;
63     my @errors;
64
65     # Return items
66     foreach my $barcode (@barcodes) {
67         try {
68             my ( $success, $messages, $checkout, $patron );
69             my $item = Koha::Items->find( { barcode => $barcode } );
70             my $human_required = 0;
71             if (   C4::Context->preference("CircConfirmItemParts")
72                 && defined($item)
73                 && $item->materials )
74             {
75                 $human_required                   = 1;
76                 $success                          = 0;
77                 $messages->{additional_materials} = 1;
78             }
79
80             ( $success, $messages, $checkout, $patron ) =
81               AddReturn( $barcode, $library ) unless $human_required;
82             if ($success) {
83                 push @success,
84                   {
85                     barcode  => $barcode,
86                     messages => $messages,
87                     checkout => $checkout,
88                     patron   => $patron
89                   };
90             }
91             else {
92                 push @errors,
93                   {
94                     barcode  => $barcode,
95                     messages => $messages,
96                     checkout => $checkout,
97                     patron   => $patron
98                   };
99             }
100         }
101         catch {
102             push @errors, { barcode => $barcode, messages => 'unknown_error' };
103         };
104     }
105     $template->param( success => \@success, errors => \@errors, checkins => 1 );
106 }
107
108 # Make sure timeout has a reasonable value
109 my $timeout = C4::Context->preference('SelfCheckInTimeout') || 120;
110 $template->param( refresh_timeout => $timeout );
111
112 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };