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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 issues.date_due < now() as date_due_overdue,
72 issues.onsite_checkout,
84 branches2.branchname AS homebranch,
86 items.itemnotes_nonpublic,
89 items.replacementprice,
97 items.ccode AS collection,
99 borrowers.borrowernumber,
102 borrowers.cardnumber,
110 DATEDIFF( issues.issuedate, CURRENT_DATE() ) AS not_issued_today,
112 return_claims.id AS return_claim_id,
113 return_claims.notes AS return_claim_notes,
114 return_claims.created_on AS return_claim_created_on,
115 return_claims.updated_on AS return_claim_updated_on
118 LEFT JOIN items USING ( itemnumber )
119 LEFT JOIN biblio USING ( biblionumber )
120 LEFT JOIN biblioitems USING ( biblionumber )
121 LEFT JOIN borrowers USING ( borrowernumber )
122 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
123 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
124 LEFT JOIN return_claims USING ( issue_id )
125 WHERE issues.borrowernumber
128 if ( @borrowernumber == 1 ) {
132 $sql .= ' IN (' . join( ',', ('?') x
@borrowernumber ) . ') ';
134 push( @parameters, @borrowernumber );
136 $sql .= " ORDER BY $sorting_column $sorting_direction ";
138 my $dbh = C4
::Context
->dbh();
139 my $sth = $dbh->prepare($sql);
140 $sth->execute(@parameters);
142 my $item_level_itypes = C4
::Context
->preference('item-level_itypes');
143 my $claims_returned_lost_value = C4
::Context
->preference('ClaimReturnedLostValue');
144 my $confirm_parts_required = C4
::Context
->preference("CircConfirmItemParts");
146 my $itemtypes = { map { $_->{itemtype
} => $_->{translated_description
} } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
149 my @checkouts_previous;
150 while ( my $c = $sth->fetchrow_hashref() ) {
151 my ($charge) = GetIssuingCharges
( $c->{itemnumber
}, $c->{borrowernumber
} );
152 my $fine = GetFine
( $c->{itemnumber
}, $c->{borrowernumber
} );
154 my ( $can_renew, $can_renew_error ) =
155 CanBookBeRenewed
( $c->{borrowernumber
}, $c->{itemnumber
} );
157 $can_renew_error && $can_renew_error eq 'too_soon'
160 dt
=> GetSoonestRenewDate
( $c->{borrowernumber
}, $c->{itemnumber
} ),
166 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
167 GetRenewCount
( $c->{borrowernumber
}, $c->{itemnumber
} );
169 my ( $itemtype, $recordtype, $type_for_stat );
170 $itemtype = $itemtypes->{ $c->{itype
} } if $c->{itype
};
171 $recordtype = $itemtypes->{ $c->{itemtype
} } if $c->{itemtype
};
172 $type_for_stat = $item_level_itypes ?
$itemtype : $recordtype;
175 if ( $c->{location
} ) {
176 my $av = Koha
::AuthorisedValues
->get_description_by_koha_field(
177 { kohafield
=> 'items.location', authorised_value
=> $c->{location
} } );
178 $location = $av->{lib
} ?
$av->{lib
} : '';
181 if ( $c->{collection
} ) {
182 my $av = Koha
::AuthorisedValues
->get_description_by_koha_field(
183 { kohafield
=> 'items.ccode', authorised_value
=> $c->{collection
} } );
184 $collection = $av->{lib
} ?
$av->{lib
} : '';
188 if ( $c->{itemlost
} ) {
189 my $av = Koha
::AuthorisedValues
->get_description_by_koha_field(
190 { kohafield
=> 'items.itemlost', authorised_value
=> $c->{itemlost
} } );
191 $lost = $av->{lib
} ?
$av->{lib
} : '';
192 $claims_returned = $c->{itemlost
} eq $claims_returned_lost_value;
195 if ( $c->{damaged
} ) {
196 my $av = Koha
::AuthorisedValues
->get_description_by_koha_field(
197 { kohafield
=> 'items.damaged', authorised_value
=> $c->{damaged
} } );
198 $damaged = $av->{lib
} ?
$av->{lib
} : '';
201 if ( $c->{materials
} && $confirm_parts_required ) {
202 my $descriptions = Koha
::AuthorisedValues
->get_description_by_koha_field({frameworkcode
=> '', kohafield
=>'items.materials', authorised_value
=> $c->{materials
} });
203 $materials = $descriptions->{lib
} // $c->{materials
};
205 my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
207 DT_RowId
=> $c->{itemnumber
} . '-' . $c->{borrowernumber
},
208 title
=> $c->{title
},
209 subtitle
=> \
@subtitles,
210 medium
=> $c->{medium
} // '',
211 part_number
=> $c->{part_number
} // '',
212 part_name
=> $c->{part_name
} // '',
213 author
=> $c->{author
},
214 barcode
=> $c->{barcode
},
215 type_for_stat
=> $type_for_stat || q{},
216 itemtype_description
=> $itemtype || q{},
217 recordtype_description
=> $recordtype || q{},
218 collection
=> $collection,
219 location
=> $location,
220 homebranch
=> $c->{homebranch
},
221 itemnotes
=> $c->{itemnotes
},
222 itemnotes_nonpublic
=> $c->{itemnotes_nonpublic
},
223 branchcode
=> $c->{branchcode
},
224 branchname
=> $c->{branchname
},
225 itemcallnumber
=> $c->{itemcallnumber
} || q{},
226 copynumber
=> $c->{copynumber
} || q{},
229 price
=> $c->{replacementprice
} || q{},
230 can_renew
=> $can_renew,
231 can_renew_error
=> $can_renew_error,
232 can_renew_date
=> $can_renew_date,
233 itemnumber
=> $c->{itemnumber
},
234 borrowernumber
=> $c->{borrowernumber
},
235 biblionumber
=> $c->{biblionumber
},
236 issuedate
=> $c->{issuedate
},
237 date_due
=> $c->{date_due
},
238 date_due_overdue
=> $c->{date_due_overdue
} ? JSON
::true
: JSON
::false
,
239 timestamp
=> $c->{timestamp
},
240 onsite_checkout
=> $c->{onsite_checkout
},
241 enumchron
=> $c->{enumchron
},
242 renewals_count
=> $renewals_count,
243 renewals_allowed
=> $renewals_allowed,
244 renewals_remaining
=> $renewals_remaining,
246 return_claim_id
=> $c->{return_claim_id
},
247 return_claim_notes
=> $c->{return_claim_notes
},
248 return_claim_created_on
=> $c->{return_claim_created_on
},
249 return_claim_updated_on
=> $c->{return_claim_updated_on
},
250 return_claim_created_on_formatted
=> $c->{return_claim_created_on
} ? output_pref
({ dt
=> dt_from_string
( $c->{return_claim_created_on
} ) }) : undef,
251 return_claim_updated_on_formatted
=> $c->{return_claim_updated_on
} ? output_pref
({ dt
=> dt_from_string
( $c->{return_claim_updated_on
} ) }) : undef,
253 issuedate_formatted
=> output_pref
(
255 dt
=> dt_from_string
( $c->{issuedate
} ),
259 date_due_formatted
=> output_pref
(
261 dt
=> dt_from_string
( $c->{date_due
} ),
266 claims_returned
=> $claims_returned,
268 materials
=> $materials,
270 surname
=> $c->{surname
},
271 firstname
=> $c->{firstname
},
272 cardnumber
=> $c->{cardnumber
},
274 issued_today
=> !$c->{not_issued_today
},
277 if ( $c->{not_issued_today
} ) {
278 push( @checkouts_previous, $checkout );
281 push( @checkouts_today, $checkout );
286 @checkouts_today = sort { $a->{timestamp
} cmp $b->{timestamp
} } @checkouts_today; # latest to earliest
287 @checkouts_today = reverse(@checkouts_today)
288 if ( C4
::Context
->preference('todaysIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
290 @checkouts_previous =
291 sort { $a->{date_due
} cmp $b->{date_due
} || $a->{timestamp
} cmp $b->{timestamp
} }
292 @checkouts_previous; # latest to earliest
293 @checkouts_previous = reverse(@checkouts_previous)
294 if ( C4
::Context
->preference('previousIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
296 my @checkouts = ( @checkouts_today, @checkouts_previous );
299 map { $_->{sort_order
} = $i++ } @checkouts;
303 $data->{'iTotalRecords'} = scalar @checkouts;
304 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
305 $data->{'sEcho'} = $input->param('sEcho') || undef;
306 $data->{'aaData'} = \
@checkouts;
308 print to_json
($data);