From 8f5af31df3bf185a408f5fc219d66e16f9e1bcf7 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 --- 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(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index b30f358..80939a7 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -85,6 +85,7 @@ BEGIN { &AddRenewal &GetRenewCount &GetSoonestRenewDate + &GetLatestAutoRenewDate &GetItemIssue &GetItemIssues &GetIssuingCharges @@ -3097,6 +3098,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 a87ba76..2b4114e 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 f0479ea..e741209 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 058675a..3a6cfdf 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -29,7 +29,7 @@ use Koha::Database; use t::lib::TestBuilder; -use Test::More tests => 83; +use Test::More tests => 84; BEGIN { use_ok('C4::Circulation'); @@ -555,6 +555,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.1.0