Bug 15644 - City dropdown default selection when modifying a patron matches only...
[koha.git] / svc / checkouts
blob98251b5b07e3631605d13fa13845bda66c0eaafb
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 haspermission get_session);
27 use C4::Biblio qw(GetMarcBiblio GetFrameworkCode GetRecordValue );
28 use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
29 use C4::Overdues qw(GetFine);
30 use C4::Context;
32 use Koha::AuthorisedValues;
33 use Koha::DateUtils;
34 use Koha::ItemTypes;
36 my $input = new CGI;
38 my ( $auth_status, $sessionID ) =
39 check_cookie_auth( $input->cookie('CGISESSID'));
41 my $session = get_session($sessionID);
42 my $userid = $session->param('id');
44 unless (haspermission($userid, { circulate => 'circulate_remaining_permissions' })
45 || haspermission($userid, { borrowers => '*' })) {
46 exit 0;
49 my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
51 my @borrowernumber = $input->multi_param('borrowernumber');
52 my $offset = $input->param('iDisplayStart');
53 my $results_per_page = $input->param('iDisplayLength') || -1;
55 my $sorting_column = $input->param('iSortCol_0') || q{};
56 $sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
58 my $sorting_direction = $input->param('sSortDir_0') || q{};
59 $sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
61 $results_per_page = undef if ( $results_per_page == -1 );
63 binmode STDOUT, ":encoding(UTF-8)";
64 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
66 my @parameters;
67 my $sql = '
68 SELECT
69 issuedate,
70 date_due,
71 date_due < now() as date_due_overdue,
72 issues.timestamp,
74 onsite_checkout,
76 biblionumber,
77 biblio.title,
78 author,
80 itemnumber,
81 barcode,
82 branches2.branchname AS homebranch,
83 itemnotes,
84 itemnotes_nonpublic,
85 itemcallnumber,
86 replacementprice,
88 issues.branchcode,
89 branches.branchname,
91 items.itype,
92 biblioitems.itemtype,
94 borrowernumber,
95 surname,
96 firstname,
97 cardnumber,
99 itemlost,
100 damaged,
101 location,
102 items.enumchron,
104 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
105 FROM issues
106 LEFT JOIN items USING ( itemnumber )
107 LEFT JOIN biblio USING ( biblionumber )
108 LEFT JOIN biblioitems USING ( biblionumber )
109 LEFT JOIN borrowers USING ( borrowernumber )
110 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
111 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
112 WHERE borrowernumber
115 if ( @borrowernumber == 1 ) {
116 $sql .= '= ?';
118 else {
119 $sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
121 push( @parameters, @borrowernumber );
123 $sql .= " ORDER BY $sorting_column $sorting_direction ";
125 my $dbh = C4::Context->dbh();
126 my $sth = $dbh->prepare($sql);
127 $sth->execute(@parameters);
129 my $item_level_itypes = C4::Context->preference('item-level_itypes');
131 my @checkouts_today;
132 my @checkouts_previous;
133 while ( my $c = $sth->fetchrow_hashref() ) {
134 my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
135 my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
137 my ( $can_renew, $can_renew_error ) =
138 CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
139 my $can_renew_date =
140 $can_renew_error && $can_renew_error eq 'too_soon'
141 ? output_pref(
143 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
144 as_due_date => 1
147 : undef;
149 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
150 GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
152 my $itemtype = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
153 my $location;
154 if ( $c->{location} ) {
155 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
156 $location = $av->count ? $av->next->lib : '';
158 my $lost;
159 if ( $c->{itemlost} ) {
160 my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
161 $lost = $av->count ? $av->next->lib : '';
163 my $damaged;
164 if ( $c->{damaged} ) {
165 my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
166 $damaged = $av->count ? $av->next->lib : '';
168 my $checkout = {
169 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
170 title => $c->{title},
171 author => $c->{author},
172 barcode => $c->{barcode},
173 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
174 itemtype_description => $itemtype->translated_description,
175 location => $location,
176 homebranch => $c->{homebranch},
177 itemnotes => $c->{itemnotes},
178 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
179 branchcode => $c->{branchcode},
180 branchname => $c->{branchname},
181 itemcallnumber => $c->{itemcallnumber} || q{},
182 charge => $charge,
183 fine => $fine,
184 price => $c->{replacementprice} || q{},
185 can_renew => $can_renew,
186 can_renew_error => $can_renew_error,
187 can_renew_date => $can_renew_date,
188 itemnumber => $c->{itemnumber},
189 borrowernumber => $c->{borrowernumber},
190 biblionumber => $c->{biblionumber},
191 issuedate => $c->{issuedate},
192 date_due => $c->{date_due},
193 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
194 timestamp => $c->{timestamp},
195 onsite_checkout => $c->{onsite_checkout},
196 enumchron => $c->{enumchron},
197 renewals_count => $renewals_count,
198 renewals_allowed => $renewals_allowed,
199 renewals_remaining => $renewals_remaining,
200 issuedate_formatted => output_pref(
202 dt => dt_from_string( $c->{issuedate} ),
203 as_due_date => 1
206 date_due_formatted => output_pref(
208 dt => dt_from_string( $c->{date_due} ),
209 as_due_date => 1
212 subtitle => GetRecordValue(
213 'subtitle',
214 GetMarcBiblio({ biblionumber => $c->{biblionumber} }),
215 GetFrameworkCode( $c->{biblionumber} ) ),
216 lost => $lost,
217 damaged => $damaged,
218 borrower => {
219 surname => $c->{surname},
220 firstname => $c->{firstname},
221 cardnumber => $c->{cardnumber},
223 issued_today => !$c->{not_issued_today},
226 if ( $c->{not_issued_today} ) {
227 push( @checkouts_previous, $checkout );
229 else {
230 push( @checkouts_today, $checkout );
235 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
236 @checkouts_today = reverse(@checkouts_today)
237 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
239 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
240 @checkouts_previous = reverse(@checkouts_previous)
241 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
243 my @checkouts = ( @checkouts_today, @checkouts_previous );
245 my $i = 1;
246 map { $_->{sort_order} = $i++ } @checkouts;
249 my $data;
250 $data->{'iTotalRecords'} = scalar @checkouts;
251 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
252 $data->{'sEcho'} = $input->param('sEcho') || undef;
253 $data->{'aaData'} = \@checkouts;
255 print to_json($data);