@@ -, +, @@ when renewing manually --- C4/Circulation.pm | 50 ++++++++++++++++++++ circ/renew.pl | 9 +++- .../intranet-tmpl/prog/en/modules/circ/renew.tt | 2 +- t/db_dependent/Circulation.t | 36 +++++++++++++- 4 files changed, 94 insertions(+), 3 deletions(-) --- a/C4/Circulation.pm +++ a/C4/Circulation.pm @@ -86,6 +86,7 @@ BEGIN { &AddRenewal &GetRenewCount &GetSoonestRenewDate + &GetLatestAutoRenewDate &GetItemIssue &GetItemIssues &GetIssuingCharges @@ -3171,6 +3172,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); --- a/circ/renew.pl +++ a/circ/renew.pl @@ -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, ); } --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt @@ -79,7 +79,7 @@ [% ELSIF error == "auto_too_late" %] -

[% item.biblio.title %] [% item.biblioitem.subtitle %] ( [% item.barcode %] ) has been scheduled for automatic renewal and cannot be renewed since [% latestrenewdate | $KohaDates %].

+

[% item.biblio.title %] [% item.biblioitem.subtitle %] ( [% item.barcode %] ) has been scheduled for automatic renewal and cannot be renewed anymore since [% latestautorenewdate | $KohaDates %].

[% IF Koha.Preference('AllowRenewalLimitOverride') %]
--- a/t/db_dependent/Circulation.t +++ a/t/db_dependent/Circulation.t @@ -30,7 +30,7 @@ use Koha::Database; use t::lib::TestBuilder; -use Test::More tests => 83; +use Test::More tests => 84; BEGIN { use_ok('C4::Circulation'); @@ -573,6 +573,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 --