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