Bug 11703 [QA Followup]
[koha.git] / svc / checkouts.pl
1 #!/usr/bin/perl
2
3 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
4
5 # Copyright 2014 ByWater Solutions
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use strict;
23 use warnings;
24
25 use CGI;
26 use JSON qw(to_json);
27
28 use C4::Auth qw(check_cookie_auth);
29 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
30 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount);
31 use C4::Context;
32
33 use Koha::DateUtils;
34
35 my $input = new CGI;
36
37 my ( $auth_status, $sessionID ) =
38   check_cookie_auth( $input->cookie('CGISESSID'),
39     { circulate => 'circulate_remaining_permissions' } );
40
41 if ( $auth_status ne "ok" ) {
42     exit 0;
43 }
44
45 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
46
47 my @borrowernumber   = $input->param('borrowernumber');
48 my $offset           = $input->param('iDisplayStart');
49 my $results_per_page = $input->param('iDisplayLength') || -1;
50 my $sorting_column   = $sort_columns[ $input->param('iSortCol_0') ]
51   || 'issuedate';
52 my $sorting_direction = $input->param('sSortDir_0') eq 'asc' ? 'asc' : 'desc';
53
54 $results_per_page = undef if ( $results_per_page == -1 );
55
56 binmode STDOUT, ":encoding(UTF-8)";
57 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
58
59 my @parameters;
60 my $sql = '
61     SELECT
62         issuedate,
63         date_due,
64
65         biblionumber,
66         biblio.title,
67         author,
68
69         itemnumber,
70         barcode,
71         itemnotes,
72         itemcallnumber,
73         replacementprice,
74
75         issues.branchcode,
76         branchname,
77
78         itype,
79         itemtype,
80
81         borrowernumber,
82         surname,
83         firstname,
84         cardnumber,
85
86         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
87     FROM issues
88         LEFT JOIN items USING ( itemnumber )
89         LEFT JOIN biblio USING ( biblionumber )
90         LEFT JOIN biblioitems USING ( biblionumber )
91         LEFT JOIN borrowers USING ( borrowernumber )
92         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
93     WHERE borrowernumber
94 ';
95
96 if ( @borrowernumber == 1 ) {
97     $sql .= '= ?';
98 }
99 else {
100     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
101 }
102 push( @parameters, @borrowernumber );
103
104 $sql .= " ORDER BY $sorting_column $sorting_direction ";
105
106 my $dbh = C4::Context->dbh();
107 my $sth = $dbh->prepare($sql);
108 $sth->execute(@parameters);
109
110 my $item_level_itypes = C4::Context->preference('item-level_itypes');
111
112 my @checkouts_today;
113 my @checkouts_previous;
114 while ( my $c = $sth->fetchrow_hashref() ) {
115     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
116
117     my ( $can_renew, $can_renew_error ) =
118       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
119
120     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
121       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
122
123     my $checkout = {
124         DT_RowId   => $c->{itemnumber} . '-' . $c->{borrowernumber},
125         title      => $c->{title},
126         author     => $c->{author},
127         barcode    => $c->{barcode},
128         itemtype   => $item_level_itypes ? $c->{itype} : $c->{itemtype},
129         itemnotes  => $c->{itemnotes},
130         branchcode => $c->{branchcode},
131         branchname => $c->{branchname},
132         itemcallnumber => $c->{itemcallnumber}   || q{},
133         charge         => $charge,
134         price          => $c->{replacementprice} || q{},
135         can_renew      => $can_renew,
136         can_renew_error     => $can_renew_error,
137         itemnumber          => $c->{itemnumber},
138         borrowernumber      => $c->{borrowernumber},
139         biblionumber        => $c->{biblionumber},
140         issuedate           => $c->{issuedate},
141         date_due            => $c->{date_due},
142         renewals_count      => $renewals_count,
143         renewals_allowed    => $renewals_allowed,
144         renewals_remaining  => $renewals_remaining,
145         issuedate_formatted => output_pref(
146             {
147                 dt          => dt_from_string( $c->{issuedate} ),
148                 as_due_date => 1
149             }
150         ),
151         date_due_formatted => output_pref(
152             {
153                 dt          => dt_from_string( $c->{date_due} ),
154                 as_due_date => 1
155             }
156         ),
157         subtitle => GetRecordValue(
158             'subtitle',
159             GetMarcBiblio( $c->{biblionumber} ),
160             GetFrameworkCode( $c->{biblionumber} )
161         ),
162         borrower => {
163             surname    => $c->{surname},
164             firstname  => $c->{firstname},
165             cardnumber => $c->{cardnumber},
166         },
167         issued_today => !$c->{not_issued_today},
168     };
169
170     if ( $c->{not_issued_today} ) {
171         push( @checkouts_previous, $checkout );
172     }
173     else {
174         push( @checkouts_today, $checkout );
175     }
176 }
177
178 @checkouts_today = reverse(@checkouts_today)
179   if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
180 @checkouts_previous = reverse(@checkouts_previous)
181   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
182
183 my @checkouts = ( @checkouts_today, @checkouts_previous );
184
185 my $data;
186 $data->{'iTotalRecords'}        = scalar @checkouts;
187 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
188 $data->{'sEcho'}                = $input->param('sEcho') || undef;
189 $data->{'aaData'}               = \@checkouts;
190
191 print to_json($data);