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
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.
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);
30 use Koha::AuthorisedValues;
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' })) {
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' );
69 date_due < now() as date_due_overdue,
84 branches2.branchname AS homebranch,
96 items.ccode AS collection,
108 DATEDIFF( issuedate, CURRENT_DATE() ) AS not_issued_today
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 )
119 if ( @borrowernumber == 1 ) {
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');
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} );
144 $can_renew_error && $can_renew_error eq 'too_soon'
147 dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
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} );
158 if ( $c->{location} ) {
159 my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
160 $location = $av->count ? $av->next->lib : '';
163 if ( $c->{collection} ) {
164 my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $c->{collection} });
165 $collection = $av->count ? $av->next->lib : '';
168 if ( $c->{itemlost} ) {
169 my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
170 $lost = $av->count ? $av->next->lib : '';
173 if ( $c->{damaged} ) {
174 my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
175 $damaged = $av->count ? $av->next->lib : '';
178 DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
179 title => $c->{title},
180 subtitle => C4::Biblio::SplitKohaField($c->{'subtitle'}),
181 medium => $c->{medium} // '',
182 part_number => $c->{part_number} // '',
183 part_name => $c->{part_name} // '',
184 author => $c->{author},
185 barcode => $c->{barcode},
186 itemtype => $item_level_itypes ? $c->{itype} : $c->{itemtype},
187 itemtype_description => $itemtype ? $itemtype->translated_description : q{},
188 collection => $collection,
189 location => $location,
190 homebranch => $c->{homebranch},
191 itemnotes => $c->{itemnotes},
192 itemnotes_nonpublic => $c->{itemnotes_nonpublic},
193 branchcode => $c->{branchcode},
194 branchname => $c->{branchname},
195 itemcallnumber => $c->{itemcallnumber} || q{},
198 price => $c->{replacementprice} || q{},
199 can_renew => $can_renew,
200 can_renew_error => $can_renew_error,
201 can_renew_date => $can_renew_date,
202 itemnumber => $c->{itemnumber},
203 borrowernumber => $c->{borrowernumber},
204 biblionumber => $c->{biblionumber},
205 issuedate => $c->{issuedate},
206 date_due => $c->{date_due},
207 date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
208 timestamp => $c->{timestamp},
209 onsite_checkout => $c->{onsite_checkout},
210 enumchron => $c->{enumchron},
211 renewals_count => $renewals_count,
212 renewals_allowed => $renewals_allowed,
213 renewals_remaining => $renewals_remaining,
214 issuedate_formatted => output_pref(
216 dt => dt_from_string( $c->{issuedate} ),
220 date_due_formatted => output_pref(
222 dt => dt_from_string( $c->{date_due} ),
229 surname => $c->{surname},
230 firstname => $c->{firstname},
231 cardnumber => $c->{cardnumber},
233 issued_today => !$c->{not_issued_today},
236 if ( $c->{not_issued_today} ) {
237 push( @checkouts_previous, $checkout );
240 push( @checkouts_today, $checkout );
245 @checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today;
246 @checkouts_today = reverse(@checkouts_today)
247 unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' );
249 @checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous;
250 @checkouts_previous = reverse(@checkouts_previous)
251 if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' );
253 my @checkouts = ( @checkouts_today, @checkouts_previous );
256 map { $_->{sort_order} = $i++ } @checkouts;
260 $data->{'iTotalRecords'} = scalar @checkouts;
261 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
262 $data->{'sEcho'} = $input->param('sEcho') || undef;
263 $data->{'aaData'} = \@checkouts;
265 print to_json($data);