Bug 29931: [21.05.x] Check cookie status before continuing
[koha.git] / svc / return_claims
1 #!/usr/bin/perl
2
3 # Copyright 2019 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 CGI;
23 use JSON qw(to_json);
24
25 use C4::Auth qw(check_cookie_auth haspermission get_session);
26 use C4::Context;
27
28 use Koha::AuthorisedValues;
29 use Koha::DateUtils;
30 use Koha::Patrons;
31
32 my $input = CGI->new;
33
34 my ( $auth_status, $sessionID ) = check_cookie_auth( $input->cookie('CGISESSID') );
35 if( $auth_status ne 'ok' ) {
36     print CGI::header( '-status' => '401' );
37     exit 0;
38 }
39
40 my $session = get_session($sessionID);
41 my $userid  = $session->param('id');
42
43 unless (
44     haspermission(
45         $userid, { circulate => 'circulate_remaining_permissions' }
46     )
47     || haspermission( $userid, { borrowers => 'edit_borrowers' } )
48   )
49 {
50     exit 0;
51 }
52
53 my @sort_columns = qw/title notes created_on updated_on/;
54
55 my $borrowernumber   = $input->param('borrowernumber');
56 my $offset           = $input->param('iDisplayStart');
57 my $results_per_page = $input->param('iDisplayLength') || -1;
58
59 my $sorting_column = $input->param('iSortCol_0') || q{};
60 $sorting_column =
61   ( $sorting_column && $sort_columns[$sorting_column] )
62   ? $sort_columns[$sorting_column]
63   : 'created_on';
64
65 my $sorting_direction = $input->param('sSortDir_0') || q{};
66 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
67
68 $results_per_page = undef if ( $results_per_page == -1 );
69
70 binmode STDOUT, ":encoding(UTF-8)";
71 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
72
73 my $sql = qq{
74     SELECT
75         return_claims.*,
76
77         biblio.biblionumber,
78         biblio.title,
79         biblio.subtitle,
80         biblio.author,
81
82         items.enumchron,
83         items.barcode,
84         items.itemlost
85     FROM return_claims
86         LEFT JOIN items USING ( itemnumber )
87         LEFT JOIN biblio USING ( biblionumber )
88         LEFT JOIN biblioitems USING ( biblionumber )
89     WHERE return_claims.borrowernumber = ?
90     ORDER BY $sorting_column $sorting_direction
91 };
92
93 my $dbh = C4::Context->dbh();
94 my $sth = $dbh->prepare($sql);
95 $sth->execute($borrowernumber);
96
97 my $resolved = 0;
98 my $unresolved = 0;
99 my @return_claims;
100 while ( my $claim = $sth->fetchrow_hashref() ) {
101     $claim->{created_on_formatted}  = output_pref( { dt => dt_from_string( $claim->{created_on} ) } ) if $claim->{created_on};
102     $claim->{updated_on_formatted}  = output_pref( { dt => dt_from_string( $claim->{updated_on} ) } ) if $claim->{updated_on};
103     $claim->{resolved_on_formatted} = output_pref( { dt => dt_from_string( $claim->{resolved_on} ) } ) if $claim->{resolved_on};
104
105     my $patron = $claim->{resolved_by} ? Koha::Patrons->find( $claim->{resolved_by} ) : undef;
106     $claim->{resolved_by_data} = $patron->unblessed if $patron;
107
108     my $resolution = $claim->{resolution}
109       ? Koha::AuthorisedValues->find(
110         {
111             category         => 'RETURN_CLAIM_RESOLUTION',
112             authorised_value => $claim->{resolution},
113         }
114       )
115       : undef;
116     $claim->{resolution_data} = $resolution->unblessed if $resolution;
117
118     $claim->{resolved_on} ? $resolved++ : $unresolved++;
119
120     push( @return_claims, $claim );
121 }
122
123 my $data = {
124     iTotalRecords        => scalar @return_claims,
125     iTotalDisplayRecords => scalar @return_claims,
126     sEcho                => $input->param('sEcho') || undef,
127     aaData               => \@return_claims,
128     resolved             => $resolved,
129     unresolved           => $unresolved
130 };
131
132 print to_json($data);