Bug 23517: Add a spec for PUT /holds/{hold_id}/priority
[koha.git] / svc / checkouts
blob892eab84fe91e12883c1824c98026174f3d42c4c
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 Modern::Perl;
22 use CGI;
23 use JSON qw(to_json);
25 use C4::Auth qw(check_cookie_auth haspermission get_session);
26 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
27 use C4::Overdues qw(GetFine);
28 use C4::Context;
30 use Koha::AuthorisedValues;
31 use Koha::DateUtils;
32 use Koha::ItemTypes;
34 my $input = new CGI;
36 my ( $auth_status, $sessionID ) =
37 check_cookie_auth( $input->cookie('CGISESSID'));
39 my $session = get_session($sessionID);
40 my $userid = $session->param('id');
42 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
43 || haspermission($userid, { borrowers => 'edit_borrowers' })) {
44 exit 0;
47 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
49 my @borrowernumber = $input->multi_param('borrowernumber');
50 my $offset = $input->param('iDisplayStart');
51 my $results_per_page = $input->param('iDisplayLength') || -1;
53 my $sorting_column = $input->param('iSortCol_0') || q{};
54 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
56 my $sorting_direction = $input->param('sSortDir_0') || q{};
57 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
59 $results_per_page = undef if ( $results_per_page == -1 );
61 binmode STDOUT, ":encoding(UTF-8)";
62 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
64 my @parameters;
65 my $sql = '
66 SELECT
67 issuedate,
68 date_due,
69 date_due < now() as date_due_overdue,
70 issues.timestamp,
72 onsite_checkout,
74 biblionumber,
75 biblio.title,
76 biblio.subtitle,
77 biblio.medium,
78 biblio.part_number,
79 biblio.part_name,
80 author,
82 itemnumber,
83 barcode,
84 branches2.branchname AS homebranch,
85 itemnotes,
86 itemnotes_nonpublic,
87 itemcallnumber,
88 replacementprice,
90 issues.branchcode,
91 branches.branchname,
93 items.itype,
94 biblioitems.itemtype,
96 items.ccode AS collection,
98 borrowernumber,
99 surname,
100 firstname,
101 cardnumber,
103 itemlost,
104 damaged,
105 location,
106 items.enumchron,
108 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
109 FROM issues
110 LEFT JOIN items USING ( itemnumber )
111 LEFT JOIN biblio USING ( biblionumber )
112 LEFT JOIN biblioitems USING ( biblionumber )
113 LEFT JOIN borrowers USING ( borrowernumber )
114 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
115 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
116 WHERE borrowernumber
119 if ( @borrowernumber == 1 ) {
120 $sql .= '= ?';
122 else {
123 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
125 push( @parameters, @borrowernumber );
127 $sql .= " ORDER BY $sorting_column $sorting_direction ";
129 my $dbh = C4::Context->dbh();
130 my $sth = $dbh->prepare($sql);
131 $sth->execute(@parameters);
133 my $item_level_itypes = C4::Context->preference('item-level_itypes');
135 my @checkouts_today;
136 my @checkouts_previous;
137 while ( my $c = $sth->fetchrow_hashref() ) {
138 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
139 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
141 my ( $can_renew, $can_renew_error ) =
142 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
143 my $can_renew_date =
144 $can_renew_error && $can_renew_error eq 'too_soon'
145 ? output_pref(
147 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
148 as_due_date => 1
151 : undef;
153 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
154 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
156 my $itemtype = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
157 my $location;
158 if ( $c->{location} ) {
159 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
160 $location = $av->count ? $av->next->lib : '';
162 my $collection;
163 if ( $c->{collection} ) {
164 my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $c->{collection} });
165 $collection = $av->count ? $av->next->lib : '';
167 my $lost;
168 if ( $c->{itemlost} ) {
169 my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
170 $lost = $av->count ? $av->next->lib : '';
172 my $damaged;
173 if ( $c->{damaged} ) {
174 my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
175 $damaged = $av->count ? $av->next->lib : '';
177 my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
178 my $checkout = {
179 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
180 title => $c->{title},
181 subtitle => \@subtitles,
182 medium => $c->{medium} // '',
183 part_number => $c->{part_number} // '',
184 part_name => $c->{part_name} // '',
185 author => $c->{author},
186 barcode => $c->{barcode},
187 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
188 itemtype_description => $itemtype ? $itemtype->translated_description : q{},
189 collection => $collection,
190 location => $location,
191 homebranch => $c->{homebranch},
192 itemnotes => $c->{itemnotes},
193 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
194 branchcode => $c->{branchcode},
195 branchname => $c->{branchname},
196 itemcallnumber => $c->{itemcallnumber} || q{},
197 charge => $charge,
198 fine => $fine,
199 price => $c->{replacementprice} || q{},
200 can_renew => $can_renew,
201 can_renew_error => $can_renew_error,
202 can_renew_date => $can_renew_date,
203 itemnumber => $c->{itemnumber},
204 borrowernumber => $c->{borrowernumber},
205 biblionumber => $c->{biblionumber},
206 issuedate => $c->{issuedate},
207 date_due => $c->{date_due},
208 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
209 timestamp => $c->{timestamp},
210 onsite_checkout => $c->{onsite_checkout},
211 enumchron => $c->{enumchron},
212 renewals_count => $renewals_count,
213 renewals_allowed => $renewals_allowed,
214 renewals_remaining => $renewals_remaining,
215 issuedate_formatted => output_pref(
217 dt => dt_from_string( $c->{issuedate} ),
218 as_due_date => 1
221 date_due_formatted => output_pref(
223 dt => dt_from_string( $c->{date_due} ),
224 as_due_date => 1
227 lost => $lost,
228 damaged => $damaged,
229 borrower => {
230 surname => $c->{surname},
231 firstname => $c->{firstname},
232 cardnumber => $c->{cardnumber},
234 issued_today => !$c->{not_issued_today},
237 if ( $c->{not_issued_today} ) {
238 push( @checkouts_previous, $checkout );
240 else {
241 push( @checkouts_today, $checkout );
246 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
247 @checkouts_today = reverse(@checkouts_today)
248 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
250 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
251 @checkouts_previous = reverse(@checkouts_previous)
252 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
254 my @checkouts = ( @checkouts_today, @checkouts_previous );
256 my $i = 1;
257 map { $_->{sort_order} = $i++ } @checkouts;
260 my $data;
261 $data->{'iTotalRecords'} = scalar @checkouts;
262 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
263 $data->{'sEcho'} = $input->param('sEcho') || undef;
264 $data->{'aaData'} = \@checkouts;
266 print to_json($data);