-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathcheckouts
executable file
·341 lines (291 loc) · 12.5 KB
/
checkouts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/perl
# Copyright 2014 ByWater Solutions
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use CGI;
use JSON qw(to_json);
use C4::Auth qw(check_cookie_auth haspermission);
use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount );
use C4::Overdues qw(GetFine);
use C4::Context;
use Koha::AuthorisedValues;
use Koha::DateUtils qw( dt_from_string output_pref );
use Koha::Items;
use Koha::ItemTypes;
my $input = CGI->new;
my ( $auth_status, $session ) = check_cookie_auth( $input->cookie('CGISESSID') );
if ( $auth_status ne 'ok' ) {
print CGI::header( '-status' => '401' );
exit 0;
}
my $userid = $session->param('id');
unless ( haspermission( $userid, { circulate => 'circulate_remaining_permissions' } )
|| haspermission( $userid, { borrowers => 'edit_borrowers' } ) )
{
exit 0;
}
my @sort_columns = qw/date_due title itype issuedate branchcode itemcallnumber/;
my @borrowernumber = $input->multi_param('borrowernumber');
my $offset = $input->param('iDisplayStart');
my $results_per_page = $input->param('iDisplayLength') || -1;
my $sorting_column = $input->param('iSortCol_0') || q{};
$sorting_column = ( $sorting_column && $sort_columns[$sorting_column] ) ? $sort_columns[$sorting_column] : 'issuedate';
my $sorting_direction = $input->param('sSortDir_0') || q{};
$sorting_direction = $sorting_direction eq 'asc' ? 'asc' : 'desc';
$results_per_page = undef if ( $results_per_page == -1 );
binmode STDOUT, ":encoding(UTF-8)";
print $input->header( -type => 'application/json', -charset => 'UTF-8' );
my @parameters;
my $sql = '
SELECT
issues.issue_id,
issues.issuedate,
issues.date_due,
issues.date_due < now() as date_due_overdue,
date(issues.date_due) = date(now()) as date_due_today,
issues.timestamp,
issues.auto_renew,
issues.onsite_checkout,
biblio.biblionumber,
biblio.title,
biblio.subtitle,
biblio.medium,
biblio.part_number,
biblio.part_name,
biblio.author,
items.itemnumber,
items.barcode,
branches2.branchname AS homebranch,
items.itemnotes,
items.itemnotes_nonpublic,
items.itemcallnumber,
items.copynumber,
items.replacementprice,
issues.branchcode,
branches.branchname,
items.itype,
biblioitems.itemtype,
items.ccode AS collection,
borrowers.borrowernumber,
borrowers.surname,
borrowers.firstname,
borrowers.cardnumber,
items.itemlost,
items.damaged,
items.location,
items.enumchron,
items.materials,
DATEDIFF( issues.issuedate, CURRENT_DATE() ) AS not_issued_today,
return_claims.id AS return_claim_id,
return_claims.notes AS return_claim_notes,
return_claims.created_on AS return_claim_created_on,
return_claims.updated_on AS return_claim_updated_on
FROM issues
LEFT JOIN items USING ( itemnumber )
LEFT JOIN biblio USING ( biblionumber )
LEFT JOIN biblioitems USING ( biblionumber )
LEFT JOIN borrowers USING ( borrowernumber )
LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
LEFT JOIN return_claims USING ( issue_id )
WHERE issues.borrowernumber
';
if ( @borrowernumber == 1 ) {
$sql .= '= ?';
} else {
$sql .= ' IN (' . join( ',', ('?') x @borrowernumber ) . ') ';
}
push( @parameters, @borrowernumber );
$sql .= " ORDER BY $sorting_column $sorting_direction ";
my $dbh = C4::Context->dbh();
my $sth = $dbh->prepare($sql);
$sth->execute(@parameters);
my $item_level_itypes = C4::Context->preference('item-level_itypes');
my $claims_returned_lost_value = C4::Context->preference('ClaimReturnedLostValue');
my $confirm_parts_required = C4::Context->preference("CircConfirmItemParts");
my $itemtypes = { map { $_->{itemtype} => $_->{translated_description} }
@{ Koha::ItemTypes->search_with_localization->unblessed } };
my @checkouts_today;
my @checkouts_previous;
while ( my $c = $sth->fetchrow_hashref() ) {
my ($charge) = GetIssuingCharges( $c->{itemnumber}, $c->{borrowernumber} );
my $fine = GetFine( $c->{itemnumber}, $c->{borrowernumber} );
my $checkout_obj = Koha::Checkouts->find( $c->{issue_id} );
my ( $can_renew, $can_renew_error, $info ) = CanBookBeRenewed( $checkout_obj->patron, $checkout_obj );
my $can_renew_date = $can_renew_error && $can_renew_error eq 'too_soon'
? output_pref(
{
dt => $info->{soonest_renew_date},
as_due_date => 1
}
)
: undef;
my (
$renewals_count,
$renewals_allowed,
$renewals_remaining,
$unseen_count,
$unseen_allowed,
$unseen_remaining
) = GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
my ( $itemtype, $recordtype, $type_for_stat );
$itemtype = $itemtypes->{ $c->{itype} } if $c->{itype};
$recordtype = $itemtypes->{ $c->{itemtype} } if $c->{itemtype};
$type_for_stat = $item_level_itypes ? $itemtype : $recordtype;
my $location;
if ( $c->{location} ) {
my $av = Koha::AuthorisedValues->get_description_by_koha_field(
{ kohafield => 'items.location', authorised_value => $c->{location} } );
$location = $av->{lib} ? $av->{lib} : '';
}
my $collection;
if ( $c->{collection} ) {
my $av = Koha::AuthorisedValues->get_description_by_koha_field(
{ kohafield => 'items.ccode', authorised_value => $c->{collection} } );
$collection = $av->{lib} ? $av->{lib} : '';
}
my $lost;
my $claims_returned;
if ( $c->{itemlost} ) {
my $av = Koha::AuthorisedValues->get_description_by_koha_field(
{ kohafield => 'items.itemlost', authorised_value => $c->{itemlost} } );
$lost = $av->{lib} ? $av->{lib} : '';
$claims_returned = $c->{itemlost} eq $claims_returned_lost_value;
}
my $damaged;
if ( $c->{damaged} ) {
my $av = Koha::AuthorisedValues->get_description_by_koha_field(
{ kohafield => 'items.damaged', authorised_value => $c->{damaged} } );
$damaged = $av->{lib} ? $av->{lib} : '';
}
my $materials;
if ( $c->{materials} && $confirm_parts_required ) {
my $descriptions = Koha::AuthorisedValues->get_description_by_koha_field(
{ frameworkcode => '', kohafield => 'items.materials', authorised_value => $c->{materials} } );
$materials = $descriptions->{lib} // $c->{materials};
}
my @subtitles = split( / \| /, $c->{'subtitle'} // '' );
my $recalled = 0;
if ( C4::Context->preference('UseRecalls') ) {
my $item = Koha::Items->find( $c->{itemnumber} );
my $recall = undef;
$recall = $item->check_recalls if $item->can_be_waiting_recall;
if ( defined $recall ) {
if ( $recall->item_level ) {
if ( $recall->item_id == $c->{itemnumber} ) {
# item-level recall on this item
$recalled = 1;
} else {
$recalled = 0;
}
} else {
# biblio-level recall, but don't want to mark recalled if the recall has been allocated a different item
if ( !$recall->waiting ) {
$recalled = 1;
}
}
}
}
my $checkout = {
DT_RowId => $c->{itemnumber} . '-' . $c->{borrowernumber},
title => $c->{title},
subtitle => \@subtitles,
medium => $c->{medium} // '',
part_number => $c->{part_number} // '',
part_name => $c->{part_name} // '',
author => $c->{author},
barcode => $c->{barcode},
type_for_stat => $type_for_stat || q{},
itemtype_description => $itemtype || q{},
recordtype_description => $recordtype || q{},
collection => $collection,
location => $location,
homebranch => $c->{homebranch},
itemnotes => $c->{itemnotes},
itemnotes_nonpublic => $c->{itemnotes_nonpublic},
branchcode => $c->{branchcode},
branchname => $c->{branchname},
itemcallnumber => $c->{itemcallnumber} || q{},
copynumber => $c->{copynumber} || q{},
charge => $charge,
fine => $fine,
price => $c->{replacementprice} || q{},
can_renew => $can_renew,
can_renew_error => $can_renew_error,
can_renew_date => $can_renew_date,
itemnumber => $c->{itemnumber},
borrowernumber => $c->{borrowernumber},
biblionumber => $c->{biblionumber},
issuedate => $c->{issuedate},
date_due => $c->{date_due},
date_due_overdue => $c->{date_due_overdue} ? JSON::true : JSON::false,
date_due_today => $c->{date_due_today} ? JSON::true : JSON::false,
timestamp => $c->{timestamp},
auto_renew => $c->{auto_renew},
onsite_checkout => $c->{onsite_checkout},
enumchron => $c->{enumchron},
renewals_count => $renewals_count,
renewals_allowed => $renewals_allowed || 0,
renewals_remaining => $renewals_remaining,
unseen_count => $unseen_count,
unseen_allowed => $unseen_allowed,
unseen_remaining => $unseen_remaining,
return_claim_id => $c->{return_claim_id},
return_claim_notes => $c->{return_claim_notes},
return_claim_created_on => $c->{return_claim_created_on},
return_claim_updated_on => $c->{return_claim_updated_on},
return_claim_created_on_formatted => $c->{return_claim_created_on}
? output_pref( { dt => dt_from_string( $c->{return_claim_created_on} ) } )
: undef,
return_claim_updated_on_formatted => $c->{return_claim_updated_on}
? output_pref( { dt => dt_from_string( $c->{return_claim_updated_on} ) } )
: undef,
lost => $lost,
claims_returned => $claims_returned,
damaged => $damaged,
materials => $materials,
borrower => {
surname => $c->{surname},
firstname => $c->{firstname},
cardnumber => $c->{cardnumber},
},
issued_today => !$c->{not_issued_today},
recalled => $recalled,
};
if ( $c->{not_issued_today} ) {
push( @checkouts_previous, $checkout );
} else {
push( @checkouts_today, $checkout );
}
}
@checkouts_today = sort { $a->{timestamp} cmp $b->{timestamp} } @checkouts_today; # latest to earliest
@checkouts_today = reverse(@checkouts_today)
if ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
@checkouts_previous =
sort { $a->{date_due} cmp $b->{date_due} || $a->{timestamp} cmp $b->{timestamp} }
@checkouts_previous; # latest to earliest
@checkouts_previous = reverse(@checkouts_previous)
if ( C4::Context->preference('previousIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
my @checkouts = ( @checkouts_today, @checkouts_previous );
my $i = 1;
map { $_->{sort_order} = $i++ } @checkouts;
my $data;
$data->{'iTotalRecords'} = scalar @checkouts;
$data->{'iTotalDisplayRecords'} = scalar @checkouts;
$data->{'sEcho'} = $input->param('sEcho') || undef;
$data->{'aaData'} = \@checkouts;
print to_json($data);