Bug 15028: Make t/db_dependent/Letters.t set the numbering pattern it needs
[koha.git] / svc / checkouts
blobcc703aeaeb4a4d60ab35730b3499c9d2ca5d9759
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::Overdues qw(GetFine);
31 use C4::Context;
33 use Koha::DateUtils;
35 my $input = new CGI;
37 my ( $auth_status, $sessionID ) =
38 check_cookie_auth( $input->cookie('CGISESSID'),
39 { circulate => 'circulate_remaining_permissions' } );
41 if ( $auth_status ne "ok" ) {
42 exit 0;
45 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
47 my @borrowernumber = $input->param('borrowernumber');
48 my $offset = $input->param('iDisplayStart');
49 my $results_per_page = $input->param('iDisplayLength') || -1;
51 my $sorting_column = $input->param('iSortCol_0') || q{};
52 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
54 my $sorting_direction = $input->param('sSortDir_0') || q{};
55 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
57 $results_per_page = undef if ( $results_per_page == -1 );
59 binmode STDOUT, ":encoding(UTF-8)";
60 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
62 my @parameters;
63 my $sql = '
64 SELECT
65 issuedate,
66 date_due,
67 date_due < now() as date_due_overdue,
68 issues.timestamp,
70 onsite_checkout,
72 biblionumber,
73 biblio.title,
74 author,
76 itemnumber,
77 barcode,
78 itemnotes,
79 itemcallnumber,
80 replacementprice,
82 issues.branchcode,
83 branchname,
85 items.itype,
86 itemtype_item.description AS itype_description,
87 biblioitems.itemtype,
88 itemtype_bib.description AS itemtype_description,
90 borrowernumber,
91 surname,
92 firstname,
93 cardnumber,
95 itemlost,
96 damaged,
97 location,
99 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
100 FROM issues
101 LEFT JOIN items USING ( itemnumber )
102 LEFT JOIN biblio USING ( biblionumber )
103 LEFT JOIN biblioitems USING ( biblionumber )
104 LEFT JOIN borrowers USING ( borrowernumber )
105 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
106 LEFT JOIN itemtypes itemtype_bib ON ( biblioitems.itemtype = itemtype_bib.itemtype )
107 LEFT JOIN itemtypes itemtype_item ON ( items.itype = itemtype_item.itemtype )
108 WHERE borrowernumber
111 if ( @borrowernumber == 1 ) {
112 $sql .= '= ?';
114 else {
115 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
117 push( @parameters, @borrowernumber );
119 $sql .= " ORDER BY $sorting_column $sorting_direction ";
121 my $dbh = C4::Context->dbh();
122 my $sth = $dbh->prepare($sql);
123 $sth->execute(@parameters);
125 my $item_level_itypes = C4::Context->preference('item-level_itypes');
127 my @checkouts_today;
128 my @checkouts_previous;
129 while ( my $c = $sth->fetchrow_hashref() ) {
130 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
131 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
133 my ( $can_renew, $can_renew_error ) =
134 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
135 my $can_renew_date =
136 $can_renew_error && $can_renew_error eq 'too_soon'
137 ? output_pref(
139 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
140 as_due_date => 1
143 : undef;
145 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
146 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
148 my $checkout = {
149 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
150 title => $c->{title},
151 author => $c->{author},
152 barcode => $c->{barcode},
153 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
154 itemtype_description => $item_level_itypes ? $c->{itype_description} : $c->{itemtype_description},
155 location => $c->{location} ? GetAuthorisedValueByCode( 'LOC', $c->{location} ) : q{},
156 itemnotes => $c->{itemnotes},
157 branchcode => $c->{branchcode},
158 branchname => $c->{branchname},
159 itemcallnumber => $c->{itemcallnumber} || q{},
160 charge => $charge,
161 fine => $fine,
162 price => $c->{replacementprice} || q{},
163 can_renew => $can_renew,
164 can_renew_error => $can_renew_error,
165 can_renew_date => $can_renew_date,
166 itemnumber => $c->{itemnumber},
167 borrowernumber => $c->{borrowernumber},
168 biblionumber => $c->{biblionumber},
169 issuedate => $c->{issuedate},
170 date_due => $c->{date_due},
171 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
172 timestamp => $c->{timestamp},
173 onsite_checkout => $c->{onsite_checkout},
174 renewals_count => $renewals_count,
175 renewals_allowed => $renewals_allowed,
176 renewals_remaining => $renewals_remaining,
177 issuedate_formatted => output_pref(
179 dt => dt_from_string( $c->{issuedate} ),
180 as_due_date => 1
183 date_due_formatted => output_pref(
185 dt => dt_from_string( $c->{date_due} ),
186 as_due_date => 1
189 subtitle => GetRecordValue(
190 'subtitle',
191 GetMarcBiblio( $c->{biblionumber} ),
192 GetFrameworkCode( $c->{biblionumber} )
194 lost => $c->{itemlost} ? GetAuthorisedValueByCode( 'LOST', $c->{itemlost} ) : undef,
195 damaged => $c->{damaged} ? GetAuthorisedValueByCode( 'DAMAGED', $c->{damaged} ) : undef,
196 borrower => {
197 surname => $c->{surname},
198 firstname => $c->{firstname},
199 cardnumber => $c->{cardnumber},
201 issued_today => !$c->{not_issued_today},
204 if ( $c->{not_issued_today} ) {
205 push( @checkouts_previous, $checkout );
207 else {
208 push( @checkouts_today, $checkout );
213 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
214 @checkouts_today = reverse(@checkouts_today)
215 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
217 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
218 @checkouts_previous = reverse(@checkouts_previous)
219 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
221 my @checkouts = ( @checkouts_today, @checkouts_previous );
223 my $i = 1;
224 map { $_->{sort_order} = $i++ } @checkouts;
227 my $data;
228 $data->{'iTotalRecords'} = scalar @checkouts;
229 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
230 $data->{'sEcho'} = $input->param('sEcho') || undef;
231 $data->{'aaData'} = \@checkouts;
233 print to_json($data);