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,
88 items.replacementprice,
96 items.ccode AS collection,
98 borrowers.borrowernumber,
101 borrowers.cardnumber,
108 DATEDIFF( issues.issuedate, CURRENT_DATE() ) AS not_issued_today,
110 return_claims.id AS return_claim_id,
111 return_claims.notes AS return_claim_notes,
112 return_claims.created_on AS return_claim_created_on,
113 return_claims.updated_on AS return_claim_updated_on
116 LEFT JOIN items USING ( itemnumber )
117 LEFT JOIN biblio USING ( biblionumber )
118 LEFT JOIN biblioitems USING ( biblionumber )
119 LEFT JOIN borrowers USING ( borrowernumber )
120 LEFT JOIN branches ON ( issues.branchcode = branches.branchcode )
121 LEFT JOIN branches branches2 ON ( items.homebranch = branches2.branchcode )
122 LEFT JOIN return_claims USING ( issue_id )
123 WHERE issues.borrowernumber
126 if ( @borrowernumber == 1 ) {
130 $sql .= ' IN (' . join( ',', ('?') x
@borrowernumber ) . ') ';
132 push( @parameters, @borrowernumber );
134 $sql .= " ORDER BY $sorting_column $sorting_direction ";
136 my $dbh = C4
::Context
->dbh();
137 my $sth = $dbh->prepare($sql);
138 $sth->execute(@parameters);
140 my $item_level_itypes = C4
::Context
->preference('item-level_itypes');
141 my $claims_returned_lost_value = C4
::Context
->preference('ClaimReturnedLostValue');
144 my @checkouts_previous;
145 while ( my $c = $sth->fetchrow_hashref() ) {
146 my ($charge) = GetIssuingCharges
( $c->{itemnumber
}, $c->{borrowernumber
} );
147 my $fine = GetFine
( $c->{itemnumber
}, $c->{borrowernumber
} );
149 my ( $can_renew, $can_renew_error ) =
150 CanBookBeRenewed
( $c->{borrowernumber
}, $c->{itemnumber
} );
152 $can_renew_error && $can_renew_error eq 'too_soon'
155 dt
=> GetSoonestRenewDate
( $c->{borrowernumber
}, $c->{itemnumber
} ),
161 my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
162 GetRenewCount
( $c->{borrowernumber
}, $c->{itemnumber
} );
164 my $type_for_stat = Koha
::ItemTypes
->find( $item_level_itypes ?
$c->{itype
} : $c->{itemtype
} );
165 my $itemtype = Koha
::ItemTypes
->find( $c->{itype
} );
166 my $recordtype = Koha
::ItemTypes
->find( $c->{itemtype
} );
168 if ( $c->{location
} ) {
169 my $av = Koha
::AuthorisedValues
->search({ category
=> 'LOC', authorised_value
=> $c->{location
} });
170 $location = $av->count ?
$av->next->lib : '';
173 if ( $c->{collection
} ) {
174 my $av = Koha
::AuthorisedValues
->search({ category
=> 'CCODE', authorised_value
=> $c->{collection
} });
175 $collection = $av->count ?
$av->next->lib : '';
179 if ( $c->{itemlost
} ) {
180 my $av = Koha
::AuthorisedValues
->search({ category
=> 'LOST', authorised_value
=> $c->{itemlost
} });
181 $lost = $av->count ?
$av->next->lib : '';
182 $claims_returned = $c->{itemlost
} eq $claims_returned_lost_value;
185 if ( $c->{damaged
} ) {
186 my $av = Koha
::AuthorisedValues
->search({ category
=> 'DAMAGED', authorised_value
=> $c->{damaged
} });
187 $damaged = $av->count ?
$av->next->lib : '';
189 my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
191 DT_RowId
=> $c->{itemnumber
} . '-' . $c->{borrowernumber
},
192 title
=> $c->{title
},
193 subtitle
=> \
@subtitles,
194 medium
=> $c->{medium
} // '',
195 part_number
=> $c->{part_number
} // '',
196 part_name
=> $c->{part_name
} // '',
197 author
=> $c->{author
},
198 barcode
=> $c->{barcode
},
199 type_for_stat
=> $type_for_stat ?
$type_for_stat->translated_description : q{},
200 itemtype_description
=> $itemtype ?
$itemtype->translated_description : q{},
201 recordtype_description
=> $recordtype ?
$recordtype->translated_description : q{},
202 collection
=> $collection,
203 location
=> $location,
204 homebranch
=> $c->{homebranch
},
205 itemnotes
=> $c->{itemnotes
},
206 itemnotes_nonpublic
=> $c->{itemnotes_nonpublic
},
207 branchcode
=> $c->{branchcode
},
208 branchname
=> $c->{branchname
},
209 itemcallnumber
=> $c->{itemcallnumber
} || q{},
212 price
=> $c->{replacementprice
} || q{},
213 can_renew
=> $can_renew,
214 can_renew_error
=> $can_renew_error,
215 can_renew_date
=> $can_renew_date,
216 itemnumber
=> $c->{itemnumber
},
217 borrowernumber
=> $c->{borrowernumber
},
218 biblionumber
=> $c->{biblionumber
},
219 issuedate
=> $c->{issuedate
},
220 date_due
=> $c->{date_due
},
221 date_due_overdue
=> $c->{date_due_overdue
} ? JSON
::true
: JSON
::false
,
222 timestamp
=> $c->{timestamp
},
223 onsite_checkout
=> $c->{onsite_checkout
},
224 enumchron
=> $c->{enumchron
},
225 renewals_count
=> $renewals_count,
226 renewals_allowed
=> $renewals_allowed,
227 renewals_remaining
=> $renewals_remaining,
229 return_claim_id
=> $c->{return_claim_id
},
230 return_claim_notes
=> $c->{return_claim_notes
},
231 return_claim_created_on
=> $c->{return_claim_created_on
},
232 return_claim_updated_on
=> $c->{return_claim_updated_on
},
233 return_claim_created_on_formatted
=> $c->{return_claim_created_on
} ? output_pref
({ dt
=> dt_from_string
( $c->{return_claim_created_on
} ) }) : undef,
234 return_claim_updated_on_formatted
=> $c->{return_claim_updated_on
} ? output_pref
({ dt
=> dt_from_string
( $c->{return_claim_updated_on
} ) }) : undef,
236 issuedate_formatted
=> output_pref
(
238 dt
=> dt_from_string
( $c->{issuedate
} ),
242 date_due_formatted
=> output_pref
(
244 dt
=> dt_from_string
( $c->{date_due
} ),
249 claims_returned
=> $claims_returned,
252 surname
=> $c->{surname
},
253 firstname
=> $c->{firstname
},
254 cardnumber
=> $c->{cardnumber
},
256 issued_today
=> !$c->{not_issued_today
},
259 if ( $c->{not_issued_today
} ) {
260 push( @checkouts_previous, $checkout );
263 push( @checkouts_today, $checkout );
268 @checkouts_today = sort { $a->{timestamp
} cmp $b->{timestamp
} } @checkouts_today; # latest to earliest
269 @checkouts_today = reverse(@checkouts_today)
270 if ( C4
::Context
->preference('todaysIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
272 @checkouts_previous =
273 sort { $a->{date_due
} cmp $b->{date_due
} || $a->{timestamp
} cmp $b->{timestamp
} }
274 @checkouts_previous; # latest to earliest
275 @checkouts_previous = reverse(@checkouts_previous)
276 if ( C4
::Context
->preference('previousIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
278 my @checkouts = ( @checkouts_today, @checkouts_previous );
281 map { $_->{sort_order
} = $i++ } @checkouts;
285 $data->{'iTotalRecords'} = scalar @checkouts;
286 $data->{'iTotalDisplayRecords'} = scalar @checkouts;
287 $data->{'sEcho'} = $input->param('sEcho') || undef;
288 $data->{'aaData'} = \
@checkouts;
290 print to_json
($data);