View | Details | Raw Unified | Return to bug 30168
Collapse All | Expand All

(-)a/C4/Circulation.pm (-19 / +10 lines)
Lines 2871-2877 sub CanBookBeRenewed { Link Here
2871
2871
2872
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
2872
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
2873
    return ( 0, $auto_renew, $soonest ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok";
2873
    return ( 0, $auto_renew, $soonest ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok";
2874
    $soonest = GetSoonestRenewDate($borrowernumber, $itemnumber);
2874
    $soonest = GetSoonestRenewDate($issue);
2875
    if ( $soonest > dt_from_string() ){
2875
    if ( $soonest > dt_from_string() ){
2876
        return (0, "too_soon", $soonest ) unless $override_limit;
2876
        return (0, "too_soon", $soonest ) unless $override_limit;
2877
    }
2877
    }
Lines 3151-3164 sub GetRenewCount { Link Here
3151
3151
3152
=head2 GetSoonestRenewDate
3152
=head2 GetSoonestRenewDate
3153
3153
3154
  $NoRenewalBeforeThisDate = &GetSoonestRenewDate($borrowernumber, $itemnumber);
3154
  $NoRenewalBeforeThisDate = &GetSoonestRenewDate($issue);
3155
3155
3156
Find out the soonest possible renew date of a borrowed item.
3156
Find out the soonest possible renew date of a borrowed item.
3157
3157
3158
C<$borrowernumber> is the borrower number of the patron who currently
3158
C<$checkout> is the checkout object to renew.
3159
has the item on loan.
3160
3161
C<$itemnumber> is the number of the item to renew.
3162
3159
3163
C<$GetSoonestRenewDate> returns the DateTime of the soonest possible
3160
C<$GetSoonestRenewDate> returns the DateTime of the soonest possible
3164
renew date, based on the value "No renewal before" of the applicable
3161
renew date, based on the value "No renewal before" of the applicable
Lines 3169-3184 cannot be found. Link Here
3169
=cut
3166
=cut
3170
3167
3171
sub GetSoonestRenewDate {
3168
sub GetSoonestRenewDate {
3172
    my ( $borrowernumber, $itemnumber ) = @_;
3169
    my ( $checkout ) = @_;
3173
3170
3174
    my $dbh = C4::Context->dbh;
3171
    my $item   = $checkout->item or return;
3175
3172
    my $patron = $checkout->patron or return;
3176
    my $item      = Koha::Items->find($itemnumber)      or return;
3177
    my $itemissue = $item->checkout or return;
3178
3179
    $borrowernumber ||= $itemissue->borrowernumber;
3180
    my $patron = Koha::Patrons->find( $borrowernumber )
3181
      or return;
3182
3173
3183
    my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed );
3174
    my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed );
3184
    my $issuing_rule = Koha::CirculationRules->get_effective_rules(
3175
    my $issuing_rule = Koha::CirculationRules->get_effective_rules(
Lines 3198-3204 sub GetSoonestRenewDate { Link Here
3198
        and $issuing_rule->{norenewalbefore} ne "" )
3189
        and $issuing_rule->{norenewalbefore} ne "" )
3199
    {
3190
    {
3200
        my $soonestrenewal =
3191
        my $soonestrenewal =
3201
          dt_from_string( $itemissue->date_due )->subtract(
3192
          dt_from_string( $checkout->date_due )->subtract(
3202
            $issuing_rule->{lengthunit} => $issuing_rule->{norenewalbefore} );
3193
            $issuing_rule->{lengthunit} => $issuing_rule->{norenewalbefore} );
3203
3194
3204
        if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date'
3195
        if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date'
Lines 3207-3215 sub GetSoonestRenewDate { Link Here
3207
            $soonestrenewal->truncate( to => 'day' );
3198
            $soonestrenewal->truncate( to => 'day' );
3208
        }
3199
        }
3209
        return $soonestrenewal if $now < $soonestrenewal;
3200
        return $soonestrenewal if $now < $soonestrenewal;
3210
    } elsif ( $itemissue->auto_renew && $patron->autorenew_checkouts ) {
3201
    } elsif ( $checkout->auto_renew && $patron->autorenew_checkouts ) {
3211
        # Checkouts with auto-renewing fall back to due date
3202
        # Checkouts with auto-renewing fall back to due date
3212
        my $soonestrenewal = dt_from_string( $itemissue->date_due );
3203
        my $soonestrenewal = dt_from_string( $checkout->date_due );
3213
        if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date'
3204
        if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date'
3214
            and $issuing_rule->{lengthunit} eq 'days' )
3205
            and $issuing_rule->{lengthunit} eq 'days' )
3215
        {
3206
        {
Lines 4333-4339 sub _CanBookBeAutoRenewed { Link Here
4333
4324
4334
    if ( defined $issuing_rule->{norenewalbefore}
4325
    if ( defined $issuing_rule->{norenewalbefore}
4335
        and $issuing_rule->{norenewalbefore} ne "" ) {
4326
        and $issuing_rule->{norenewalbefore} ne "" ) {
4336
        my $soonest = GetSoonestRenewDate($patron->id, $item->id);
4327
        my $soonest = GetSoonestRenewDate($issue);
4337
        if ( $soonest > dt_from_string()) {
4328
        if ( $soonest > dt_from_string()) {
4338
            return ( "auto_too_soon", $soonest );
4329
            return ( "auto_too_soon", $soonest );
4339
        } else {
4330
        } else {
(-)a/t/db_dependent/Circulation.t (-6 / +5 lines)
Lines 4984-4990 subtest "GetSoonestRenewDate tests" => sub { Link Here
4984
    # Test 'exact time' setting for syspref NoRenewalBeforePrecision
4984
    # Test 'exact time' setting for syspref NoRenewalBeforePrecision
4985
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
4985
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact_time' );
4986
    is(
4986
    is(
4987
        GetSoonestRenewDate( $patron->id, $item->itemnumber ),
4987
        GetSoonestRenewDate( $issue ),
4988
        $datedue->clone->add( days => -7 ),
4988
        $datedue->clone->add( days => -7 ),
4989
        'Bug 14395: Renewals permitted 7 days before due date, as expected'
4989
        'Bug 14395: Renewals permitted 7 days before due date, as expected'
4990
    );
4990
    );
Lines 4993-4999 subtest "GetSoonestRenewDate tests" => sub { Link Here
4993
    # Test 'date' setting for syspref NoRenewalBeforePrecision
4993
    # Test 'date' setting for syspref NoRenewalBeforePrecision
4994
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
4994
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
4995
    is(
4995
    is(
4996
        GetSoonestRenewDate( $patron->id, $item->itemnumber ),
4996
        GetSoonestRenewDate( $issue ),
4997
        $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
4997
        $datedue->clone->add( days => -7 )->truncate( to => 'day' ),
4998
        'Bug 14395: Renewals permitted 7 days before due date, as expected'
4998
        'Bug 14395: Renewals permitted 7 days before due date, as expected'
4999
    );
4999
    );
Lines 5010-5016 subtest "GetSoonestRenewDate tests" => sub { Link Here
5010
    );
5010
    );
5011
5011
5012
    is(
5012
    is(
5013
        GetSoonestRenewDate( $patron->id, $item->itemnumber ),
5013
        GetSoonestRenewDate( $issue ),
5014
        dt_from_string,
5014
        dt_from_string,
5015
        'Checkouts without auto-renewal can be renewed immediately if no norenewalbefore'
5015
        'Checkouts without auto-renewal can be renewed immediately if no norenewalbefore'
5016
    );
5016
    );
Lines 5018-5030 subtest "GetSoonestRenewDate tests" => sub { Link Here
5018
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
5018
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'date' );
5019
    $issue->auto_renew(1)->store;
5019
    $issue->auto_renew(1)->store;
5020
    is(
5020
    is(
5021
        GetSoonestRenewDate( $patron->id, $item->itemnumber ),
5021
        GetSoonestRenewDate( $issue ),
5022
        $datedue->clone->truncate( to => 'day' ),
5022
        $datedue->clone->truncate( to => 'day' ),
5023
        'Checkouts with auto-renewal can be renewed earliest on due date if no renewalbefore'
5023
        'Checkouts with auto-renewal can be renewed earliest on due date if no renewalbefore'
5024
    );
5024
    );
5025
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact' );
5025
    t::lib::Mocks::mock_preference( 'NoRenewalBeforePrecision', 'exact' );
5026
    is(
5026
    is(
5027
        GetSoonestRenewDate( $patron->id, $item->itemnumber ),
5027
        GetSoonestRenewDate( $issue ),
5028
        $datedue,
5028
        $datedue,
5029
        'Checkouts with auto-renewal can be renewed earliest on due date if no renewalbefore'
5029
        'Checkouts with auto-renewal can be renewed earliest on due date if no renewalbefore'
5030
    );
5030
    );
5031
- 

Return to bug 30168