Bug 13436: DBRev 3.19.00.001
[koha.git] / svc / checkouts
blob51fd861cb90996e80e7b9661ff7e9586fc8d92b9
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 qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
29 use C4::Koha qw(GetAuthorisedValueByCode);
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;
50 my $sorting_column = $input->param('iSortCol_0') || q{};
51 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
53 my $sorting_direction = $input->param('sSortDir_0') || q{};
54 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
56 $results_per_page = undef if ( $results_per_page == -1 );
58 binmode STDOUT, ":encoding(UTF-8)";
59 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
61 my @parameters;
62 my $sql = '
63 SELECT
64 issuedate,
65 date_due,
66 date_due < now() as date_due_overdue,
67 issues.timestamp,
69 onsite_checkout,
71 biblionumber,
72 biblio.title,
73 author,
75 itemnumber,
76 barcode,
77 itemnotes,
78 itemcallnumber,
79 replacementprice,
81 issues.branchcode,
82 branchname,
84 items.itype,
85 itemtype_item.description AS itype_description,
86 biblioitems.itemtype,
87 itemtype_bib.description AS itemtype_description,
89 borrowernumber,
90 surname,
91 firstname,
92 cardnumber,
94 itemlost,
95 damaged,
97 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
98 FROM issues
99 LEFT JOIN items USING ( itemnumber )
100 LEFT JOIN biblio USING ( biblionumber )
101 LEFT JOIN biblioitems USING ( biblionumber )
102 LEFT JOIN borrowers USING ( borrowernumber )
103 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
104 LEFT JOIN itemtypes itemtype_bib ON ( biblioitems.itemtype = itemtype_bib.itemtype )
105 LEFT JOIN itemtypes itemtype_item ON ( items.itype = itemtype_item.itemtype )
106 WHERE borrowernumber
109 if ( @borrowernumber == 1 ) {
110 $sql .= '= ?';
112 else {
113 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
115 push( @parameters, @borrowernumber );
117 $sql .= " ORDER BY $sorting_column $sorting_direction ";
119 my $dbh = C4::Context->dbh();
120 my $sth = $dbh->prepare($sql);
121 $sth->execute(@parameters);
123 my $item_level_itypes = C4::Context->preference('item-level_itypes');
125 my @checkouts_today;
126 my @checkouts_previous;
127 while ( my $c = $sth->fetchrow_hashref() ) {
128 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
130 my ( $can_renew, $can_renew_error ) =
131 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
132 my $can_renew_date =
133 $can_renew_error && $can_renew_error eq 'too_soon'
134 ? output_pref(
136 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
137 as_due_date => 1
140 : undef;
142 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
143 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
145 my $checkout = {
146 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
147 title => $c->{title},
148 author => $c->{author},
149 barcode => $c->{barcode},
150 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
151 itemtype_description => $item_level_itypes ? $c->{itype_description} : $c->{itemtype_description},
152 itemnotes => $c->{itemnotes},
153 branchcode => $c->{branchcode},
154 branchname => $c->{branchname},
155 itemcallnumber => $c->{itemcallnumber} || q{},
156 charge => $charge,
157 price => $c->{replacementprice} || q{},
158 can_renew => $can_renew,
159 can_renew_error => $can_renew_error,
160 can_renew_date => $can_renew_date,
161 itemnumber => $c->{itemnumber},
162 borrowernumber => $c->{borrowernumber},
163 biblionumber => $c->{biblionumber},
164 issuedate => $c->{issuedate},
165 date_due => $c->{date_due},
166 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
167 timestamp => $c->{timestamp},
168 onsite_checkout => $c->{onsite_checkout},
169 renewals_count => $renewals_count,
170 renewals_allowed => $renewals_allowed,
171 renewals_remaining => $renewals_remaining,
172 issuedate_formatted => output_pref(
174 dt => dt_from_string( $c->{issuedate} ),
175 as_due_date => 1
178 date_due_formatted => output_pref(
180 dt => dt_from_string( $c->{date_due} ),
181 as_due_date => 1
184 subtitle => GetRecordValue(
185 'subtitle',
186 GetMarcBiblio( $c->{biblionumber} ),
187 GetFrameworkCode( $c->{biblionumber} )
189 lost => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST', $c->{itemlost} ) : undef,
190 damaged => $c->{damaged} ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} ) : undef,
191 borrower => {
192 surname => $c->{surname},
193 firstname => $c->{firstname},
194 cardnumber => $c->{cardnumber},
196 issued_today => !$c->{not_issued_today},
199 if ( $c->{not_issued_today} ) {
200 push( @checkouts_previous, $checkout );
202 else {
203 push( @checkouts_today, $checkout );
207 @checkouts_today = reverse(@checkouts_today)
208 if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
209 @checkouts_previous = reverse(@checkouts_previous)
210 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
212 my @checkouts = ( @checkouts_today, @checkouts_previous );
214 my $data;
215 $data->{'iTotalRecords'} = scalar @checkouts;
216 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
217 $data->{'sEcho'} = $input->param('sEcho') || undef;
218 $data->{'aaData'} = \@checkouts;
220 print to_json($data);