From 15b1f6c7fe472ce4b1c0f73e2f0dbb0fe05a15ad Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 14 Jan 2016 14:38:23 +0000 Subject: [PATCH] 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 Signed-off-by: Katrin Fischer Signed-off-by: Kyle M Hall --- C4/Circulation.pm | 50 +++++++++++++++++++ circ/renew.pl | 9 +++- .../prog/en/modules/circ/renew.tt | 2 +- t/db_dependent/Circulation.t | 34 +++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 46ff960fec..782ca7b913 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -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); diff --git a/circ/renew.pl b/circ/renew.pl index a87ba76740..2b4114e2c9 100755 --- a/circ/renew.pl +++ b/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, ); } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt index edbaaf1360..acac0791e7 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/renew.tt +++ b/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') %]
diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 443cf3c4e4..723c27b1fa 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -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 -- 2.39.5