Bug 15581: Display the latest auto renew date possible when renewing manually

If an issue marked as auto_renew is renewed manually, we want to display
the latest auto renew date possible.

Test plan:
1/ Define circ rules as in the previous patch.
2/ Check a item out, mark it as an auto renewal
3/ Back date the issuedate and make sure it will be too late to renew it
4/ Use the Circulation > renew page (circ/renew.pl) to manually renew
this issue.
You should get a warning "You barcode has been scheduled for automatic renewal
and cannot be renewed anymore since DATE."
If the pref AllowRenewalLimitOverride is set, you will be allowed to
renew it anyway.

Sponsored-by: University of the Arts London
Signed-off-by: Jonathan Field <jonathan.field@ptfs-europe.com>

Signed-off-by: Katrin Fischer  <katrin.fischer@bsz-bw.de>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
This commit is contained in:
Jonathan Druart 2016-01-14 14:38:23 +00:00 committed by Kyle M Hall
parent 53dfa99727
commit 15b1f6c7fe
4 changed files with 93 additions and 2 deletions

View file

@ -85,6 +85,7 @@ BEGIN {
&AddRenewal
&GetRenewCount
&GetSoonestRenewDate
&GetLatestAutoRenewDate
&GetItemIssue
&GetItemIssues
&GetIssuingCharges
@ -3141,6 +3142,55 @@ sub GetSoonestRenewDate {
return $now;
}
=head2 GetLatestAutoRenewDate
$NoAutoRenewalAfterThisDate = &GetLatestAutoRenewDate($borrowernumber, $itemnumber);
Find out the latest possible auto renew date of a borrowed item.
C<$borrowernumber> is the borrower number of the patron who currently
has the item on loan.
C<$itemnumber> is the number of the item to renew.
C<$GetLatestAutoRenewDate> returns the DateTime of the latest possible
auto renew date, based on the value "No auto renewal after" of the applicable
issuing rule.
Returns undef if there is no date specify in the circ rules or if the patron, loan,
or item cannot be found.
=cut
sub GetLatestAutoRenewDate {
my ( $borrowernumber, $itemnumber ) = @_;
my $dbh = C4::Context->dbh;
my $item = GetItem($itemnumber) or return;
my $itemissue = GetItemIssue($itemnumber) or return;
$borrowernumber ||= $itemissue->{borrowernumber};
my $borrower = C4::Members::GetMember( borrowernumber => $borrowernumber )
or return;
my $branchcode = _GetCircControlBranch( $item, $borrower );
my $issuingrule =
GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode );
my $now = dt_from_string;
return if not $issuingrule->{no_auto_renewal_after}
or $issuingrule->{no_auto_renewal_after} eq '';
my $maximum_renewal_date = dt_from_string($itemissue->{issuedate});
$maximum_renewal_date->add(
$issuingrule->{lengthunit} => $issuingrule->{no_auto_renewal_after}
);
return $maximum_renewal_date;
}
=head2 GetIssuingCharges
($charge, $item_type) = &GetIssuingCharges($itemnumber, $borrowernumber);

View file

@ -47,7 +47,7 @@ my $override_holds = $cgi->param('override_holds');
my ( $item, $issue, $borrower );
my $error = q{};
my $soonest_renew_date;
my ( $soonest_renew_date, $latest_auto_renew_date );
if ($barcode) {
$item = $schema->resultset("Item")->single( { barcode => $barcode } );
@ -82,6 +82,12 @@ if ($barcode) {
$item->itemnumber(),
);
}
if ( $error && ( $error eq 'auto_too_late' ) ) {
$latest_auto_renew_date = C4::Circulation::GetLatestAutoRenewDate(
$borrower->borrowernumber(),
$item->itemnumber(),
);
}
if ($can_renew) {
my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
my $date_due = AddRenewal( undef, $item->itemnumber(), $branchcode );
@ -106,6 +112,7 @@ if ($barcode) {
borrower => $borrower,
error => $error,
soonestrenewdate => $soonest_renew_date,
latestautorenewdate => $latest_auto_renew_date,
);
}

View file

@ -79,7 +79,7 @@
[% ELSIF error == "auto_too_late" %]
<p>[% item.biblio.title %] [% item.biblioitem.subtitle %] ( [% item.barcode %] ) has been scheduled for automatic renewal and cannot be renewed since [% latestrenewdate | $KohaDates %]. </p>
<p>[% item.biblio.title %] [% item.biblioitem.subtitle %] ( [% item.barcode %] ) has been scheduled for automatic renewal and cannot be renewed anymore since [% latestautorenewdate | $KohaDates %]. </p>
[% IF Koha.Preference('AllowRenewalLimitOverride') %]
<form method="post" action="/cgi-bin/koha/circ/renew.pl">

View file

@ -593,6 +593,40 @@ C4::Context->dbh->do("DELETE FROM accountlines");
is( $error, 'auto_renew', 'Cannot renew, renew is automatic' );
};
subtest "GetLatestAutoRenewDate" => sub {
plan tests => 3;
my $item_to_auto_renew = $builder->build(
{ source => 'Item',
value => {
biblionumber => $biblionumber,
homebranch => $branch,
holdingbranch => $branch,
}
}
);
my $ten_days_before = dt_from_string->add( days => -10 );
my $ten_days_ahead = dt_from_string->add( days => 10 );
AddIssue( $renewing_borrower, $item_to_auto_renew->{barcode}, $ten_days_ahead, undef, $ten_days_before, undef, { auto_renew => 1 } );
$dbh->do('UPDATE issuingrules SET norenewalbefore = 7, no_auto_renewal_after = ""');
my $latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date, undef, 'GetLatestAutoRenewDate should return undef if no_auto_renewal_after is not defined' );
my $five_days_before = dt_from_string->add( days => -5 );
$dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 5');
$latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date->truncate( to => 'minute' ),
$five_days_before->truncate( to => 'minute' ),
'GetLatestAutoRenewDate should return -5 days if no_auto_renewal_after = 5 and date_due is 10 days before'
);
my $five_days_ahead = dt_from_string->add( days => 5 );
$dbh->do('UPDATE issuingrules SET norenewalbefore = 10, no_auto_renewal_after = 15');
$latest_auto_renew_date = GetLatestAutoRenewDate( $renewing_borrowernumber, $item_to_auto_renew->{itemnumber} );
is( $latest_auto_renew_date->truncate( to => 'minute' ),
$five_days_ahead->truncate( to => 'minute' ),
'GetLatestAutoRenewDate should return +5 days if no_auto_renewal_after = 15 and date_due is 10 days before'
);
};
# Too many renewals
# set policy to forbid renewals