Koha/svc/checkouts
Fridolin Somers 6b2d109315
Bug 23427: Fix previous checkouts sort order
In patron circulation or details page, previous checkouts are displayed sorted by due date.
Actual sort order is not honoring system preference "previousIssuesDefaultSortOrder". Todays's checkouts is correct thanks to Bug 13908.

Patch adds comments that refer to the text of system preferences "previousIssuesDefaultSortOrder" and "TodayIssuesDefaultSortOrder" :
  latest to earliest = asc
  earliest to latest = desc

Test plan :
1) Create for a patron two old issues with a few days between them
2) Set preference previousIssuesDefaultSortOrder = "latest to earliest"
3) Go to patron circulation page and check sort order is OK
4) Go to patron details page and check sort order is OK
5) Set preference previousIssuesDefaultSortOrder = "earliest to latest"
6) Go to patron circulation page and check sort order is OK
7) Go to patron details page and check sort order is OK

Signed-off-by: Michal Denar <black23@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
2019-11-07 13:23:10 +00:00

288 lines
10 KiB
Perl
Executable file

#!/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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use CGI;
use JSON qw(to_json);
use C4::Auth qw(check_cookie_auth haspermission get_session);
use C4::Circulation qw(GetIssuingCharges CanBookBeRenewed GetRenewCount GetSoonestRenewDate);
use C4::Overdues qw(GetFine);
use C4::Context;
use Koha::AuthorisedValues;
use Koha::DateUtils;
use Koha::ItemTypes;
my $input = new CGI;
my ( $auth_status, $sessionID ) =
check_cookie_auth( $input->cookie('CGISESSID'));
my $session = get_session($sessionID);
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 => 'text/plain', -charset => 'UTF-8' );
my @parameters;
my $sql = '
SELECT
issues.issuedate,
issues.date_due,
issues.date_due < now() as date_due_overdue,
issues.timestamp,
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.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,
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 @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 ( $can_renew, $can_renew_error ) =
CanBookBeRenewed( $c->{borrowernumber}, $c->{itemnumber} );
my $can_renew_date =
$can_renew_error && $can_renew_error eq 'too_soon'
? output_pref(
{
dt => GetSoonestRenewDate( $c->{borrowernumber}, $c->{itemnumber} ),
as_due_date => 1
}
)
: undef;
my ( $renewals_count, $renewals_allowed, $renewals_remaining ) =
GetRenewCount( $c->{borrowernumber}, $c->{itemnumber} );
my $type_for_stat = Koha::ItemTypes->find( $item_level_itypes ? $c->{itype} : $c->{itemtype} );
my $itemtype = Koha::ItemTypes->find( $c->{itype} );
my $recordtype = Koha::ItemTypes->find( $c->{itemtype} );
my $location;
if ( $c->{location} ) {
my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $c->{location} });
$location = $av->count ? $av->next->lib : '';
}
my $collection;
if ( $c->{collection} ) {
my $av = Koha::AuthorisedValues->search({ category => 'CCODE', authorised_value => $c->{collection} });
$collection = $av->count ? $av->next->lib : '';
}
my $lost;
my $claims_returned;
if ( $c->{itemlost} ) {
my $av = Koha::AuthorisedValues->search({ category => 'LOST', authorised_value => $c->{itemlost} });
$lost = $av->count ? $av->next->lib : '';
$claims_returned = $c->{itemlost} eq $claims_returned_lost_value;
}
my $damaged;
if ( $c->{damaged} ) {
my $av = Koha::AuthorisedValues->search({ category => 'DAMAGED', authorised_value => $c->{damaged} });
$damaged = $av->count ? $av->next->lib : '';
}
my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
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 ? $type_for_stat->translated_description : q{},
itemtype_description => $itemtype ? $itemtype->translated_description : q{},
recordtype_description => $recordtype ? $recordtype->translated_description : 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{},
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,
timestamp => $c->{timestamp},
onsite_checkout => $c->{onsite_checkout},
enumchron => $c->{enumchron},
renewals_count => $renewals_count,
renewals_allowed => $renewals_allowed,
renewals_remaining => $renewals_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,
issuedate_formatted => output_pref(
{
dt => dt_from_string( $c->{issuedate} ),
as_due_date => 1
}
),
date_due_formatted => output_pref(
{
dt => dt_from_string( $c->{date_due} ),
as_due_date => 1
}
),
lost => $lost,
claims_returned => $claims_returned,
damaged => $damaged,
borrower => {
surname => $c->{surname},
firstname => $c->{firstname},
cardnumber => $c->{cardnumber},
},
issued_today => !$c->{not_issued_today},
};
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)
unless ( C4::Context->preference('todaysIssuesDefaultSortOrder') eq 'desc' ); # earliest to latest
@checkouts_previous = sort { $a->{date_due} cmp $b->{date_due} } @checkouts_previous; # latest to earliest
@checkouts_previous = reverse(@checkouts_previous)
unless ( 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);