Bug 11703 [QA Followup] - Restore showing earliest renewal date for 'too early' renewals
[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
31   qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
32 use C4::Context;
33
34 use Koha::DateUtils;
35
36 my $input = new CGI;
37
38 my ( $auth_status, $sessionID ) =
39   check_cookie_auth( $input->cookie('CGISESSID'),
40     { circulate => 'circulate_remaining_permissions' } );
41
42 if ( $auth_status ne "ok" ) {
43     exit 0;
44 }
45
46 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
47
48 my @borrowernumber   = $input->param('borrowernumber');
49 my $offset           = $input->param('iDisplayStart');
50 my $results_per_page = $input->param('iDisplayLength') || -1;
51 my $sorting_column   = $sort_columns[ $input->param('iSortCol_0') ]
52   || 'issuedate';
53 my $sorting_direction = $input->param('sSortDir_0') eq 'asc' ? 'asc' : 'desc';
54
55 $results_per_page = undef if ( $results_per_page == -1 );
56
57 binmode STDOUT, ":encoding(UTF-8)";
58 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
59
60 my @parameters;
61 my $sql = '
62     SELECT
63         issuedate,
64         date_due,
65
66         biblionumber,
67         biblio.title,
68         author,
69
70         itemnumber,
71         barcode,
72         itemnotes,
73         itemcallnumber,
74         replacementprice,
75
76         issues.branchcode,
77         branchname,
78
79         itype,
80         itemtype,
81
82         borrowernumber,
83         surname,
84         firstname,
85         cardnumber,
86
87         DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
88     FROM issues
89         LEFT JOIN items USING ( itemnumber )
90         LEFT JOIN biblio USING ( biblionumber )
91         LEFT JOIN biblioitems USING ( biblionumber )
92         LEFT JOIN borrowers USING ( borrowernumber )
93         LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
94     WHERE borrowernumber
95 ';
96
97 if ( @borrowernumber == 1 ) {
98     $sql .= '= ?';
99 }
100 else {
101     $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
102 }
103 push( @parameters, @borrowernumber );
104
105 $sql .= " ORDER BY $sorting_column $sorting_direction ";
106
107 my $dbh = C4::Context->dbh();
108 my $sth = $dbh->prepare($sql);
109 $sth->execute(@parameters);
110
111 my $item_level_itypes = C4::Context->preference('item-level_itypes');
112
113 my @checkouts_today;
114 my @checkouts_previous;
115 while ( my $c = $sth->fetchrow_hashref() ) {
116     my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
117
118     my ( $can_renew, $can_renew_error ) =
119       CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
120     my $can_renew_date =
121       $can_renew_error eq 'too_soon'
122       ? output_pref(
123         {
124             dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
125             as_due_date => 1
126         }
127       )
128       : undef;
129
130     my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
131       GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
132
133     my $checkout = {
134         DT_RowId   => $c->{itemnumber} . '-' . $c->{borrowernumber},
135         title      => $c->{title},
136         author     => $c->{author},
137         barcode    => $c->{barcode},
138         itemtype   => $item_level_itypes ? $c->{itype} : $c->{itemtype},
139         itemnotes  => $c->{itemnotes},
140         branchcode => $c->{branchcode},
141         branchname => $c->{branchname},
142         itemcallnumber => $c->{itemcallnumber}   || q{},
143         charge         => $charge,
144         price          => $c->{replacementprice} || q{},
145         can_renew      => $can_renew,
146         can_renew_error     => $can_renew_error,
147         can_renew_date      => $can_renew_date,
148         itemnumber          => $c->{itemnumber},
149         borrowernumber      => $c->{borrowernumber},
150         biblionumber        => $c->{biblionumber},
151         issuedate           => $c->{issuedate},
152         date_due            => $c->{date_due},
153         renewals_count      => $renewals_count,
154         renewals_allowed    => $renewals_allowed,
155         renewals_remaining  => $renewals_remaining,
156         issuedate_formatted => output_pref(
157             {
158                 dt          => dt_from_string( $c->{issuedate} ),
159                 as_due_date => 1
160             }
161         ),
162         date_due_formatted => output_pref(
163             {
164                 dt          => dt_from_string( $c->{date_due} ),
165                 as_due_date => 1
166             }
167         ),
168         subtitle => GetRecordValue(
169             'subtitle',
170             GetMarcBiblio( $c->{biblionumber} ),
171             GetFrameworkCode( $c->{biblionumber} )
172         ),
173         borrower => {
174             surname    => $c->{surname},
175             firstname  => $c->{firstname},
176             cardnumber => $c->{cardnumber},
177         },
178         issued_today => !$c->{not_issued_today},
179     };
180
181     if ( $c->{not_issued_today} ) {
182         push( @checkouts_previous, $checkout );
183     }
184     else {
185         push( @checkouts_today, $checkout );
186     }
187 }
188
189 @checkouts_today = reverse(@checkouts_today)
190   if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
191 @checkouts_previous = reverse(@checkouts_previous)
192   if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
193
194 my @checkouts = ( @checkouts_today, @checkouts_previous );
195
196 my $data;
197 $data->{'iTotalRecords'}        = scalar @checkouts;
198 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
199 $data->{'sEcho'}                = $input->param('sEcho') || undef;
200 $data->{'aaData'}               = \@checkouts;
201
202 print to_json($data);