Bug 12729 - Overdue items won't show as overdue in red in circulation
[koha.git] / svc / checkouts
blobf56bdffb83634b7aabcf59a309a9d45c0728c5e0
1 #!/usr/bin/perl
3 # Copyright 2014 ByWater Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 use warnings;
23 use CGI;
24 use JSON qw(to_json);
26 use C4::Auth qw(check_cookie_auth);
27 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
28 use C4::Circulation
29 qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
30 use C4::Context;
32 use Koha::DateUtils;
34 my $input = new CGI;
36 my ( $auth_status, $sessionID ) =
37 check_cookie_auth( $input->cookie('CGISESSID'),
38 { circulate => 'circulate_remaining_permissions' } );
40 if ( $auth_status ne "ok" ) {
41 exit 0;
44 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
46 my @borrowernumber = $input->param('borrowernumber');
47 my $offset = $input->param('iDisplayStart');
48 my $results_per_page = $input->param('iDisplayLength') || -1;
49 my $sorting_column = $sort_columns[ $input->param('iSortCol_0') ]
50 || 'issuedate';
51 my $sorting_direction = $input->param('sSortDir_0') eq 'asc' ? 'asc' : 'desc';
53 $results_per_page = undef if ( $results_per_page == -1 );
55 binmode STDOUT, ":encoding(UTF-8)";
56 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
58 my @parameters;
59 my $sql = '
60 SELECT
61 issuedate,
62 date_due,
63 date_due < now() as date_due_overdue,
65 biblionumber,
66 biblio.title,
67 author,
69 itemnumber,
70 barcode,
71 itemnotes,
72 itemcallnumber,
73 replacementprice,
75 issues.branchcode,
76 branchname,
78 itype,
79 itemtype,
81 borrowernumber,
82 surname,
83 firstname,
84 cardnumber,
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
96 if ( @borrowernumber == 1 ) {
97 $sql .= '= ?';
99 else {
100 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
102 push( @parameters, @borrowernumber );
104 $sql .= " ORDER BY $sorting_column $sorting_direction ";
106 my $dbh = C4::Context->dbh();
107 my $sth = $dbh->prepare($sql);
108 $sth->execute(@parameters);
110 my $item_level_itypes = C4::Context->preference('item-level_itypes');
112 my @checkouts_today;
113 my @checkouts_previous;
114 while ( my $c = $sth->fetchrow_hashref() ) {
115 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
117 my ( $can_renew, $can_renew_error ) =
118 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
119 my $can_renew_date =
120 $can_renew_error eq 'too_soon'
121 ? output_pref(
123 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
124 as_due_date => 1
127 : undef;
129 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
130 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
132 my $checkout = {
133 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
134 title => $c->{title},
135 author => $c->{author},
136 barcode => $c->{barcode},
137 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
138 itemnotes => $c->{itemnotes},
139 branchcode => $c->{branchcode},
140 branchname => $c->{branchname},
141 itemcallnumber => $c->{itemcallnumber} || q{},
142 charge => $charge,
143 price => $c->{replacementprice} || q{},
144 can_renew => $can_renew,
145 can_renew_error => $can_renew_error,
146 can_renew_date => $can_renew_date,
147 itemnumber => $c->{itemnumber},
148 borrowernumber => $c->{borrowernumber},
149 biblionumber => $c->{biblionumber},
150 issuedate => $c->{issuedate},
151 date_due => $c->{date_due},
152 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
153 renewals_count => $renewals_count,
154 renewals_allowed => $renewals_allowed,
155 renewals_remaining => $renewals_remaining,
156 issuedate_formatted => output_pref(
158 dt => dt_from_string( $c->{issuedate} ),
159 as_due_date => 1
162 date_due_formatted => output_pref(
164 dt => dt_from_string( $c->{date_due} ),
165 as_due_date => 1
168 subtitle => GetRecordValue(
169 'subtitle',
170 GetMarcBiblio( $c->{biblionumber} ),
171 GetFrameworkCode( $c->{biblionumber} )
173 borrower => {
174 surname => $c->{surname},
175 firstname => $c->{firstname},
176 cardnumber => $c->{cardnumber},
178 issued_today => !$c->{not_issued_today},
181 if ( $c->{not_issued_today} ) {
182 push( @checkouts_previous, $checkout );
184 else {
185 push( @checkouts_today, $checkout );
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' );
194 my @checkouts = ( @checkouts_today, @checkouts_previous );
196 my $data;
197 $data->{'iTotalRecords'} = scalar @checkouts;
198 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
199 $data->{'sEcho'} = $input->param('sEcho') || undef;
200 $data->{'aaData'} = \@checkouts;
202 print to_json($data);