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

(-)a/C4/Circulation.pm (-9 / +14 lines)
Lines 2818-2824 sub GetUpcomingDueIssues { Link Here
2818
2818
2819
=head2 CanBookBeRenewed
2819
=head2 CanBookBeRenewed
2820
2820
2821
  ($ok,$error) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
2821
  ($ok,$error,$info) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
2822
2822
2823
Find out whether a borrowed item may be renewed.
2823
Find out whether a borrowed item may be renewed.
2824
2824
Lines 2836-2842 that are automatically renewed. Link Here
2836
C<$CanBookBeRenewed> returns a true value if the item may be renewed. The
2836
C<$CanBookBeRenewed> returns a true value if the item may be renewed. The
2837
item must currently be on loan to the specified borrower; renewals
2837
item must currently be on loan to the specified borrower; renewals
2838
must be allowed for the item's type; and the borrower must not have
2838
must be allowed for the item's type; and the borrower must not have
2839
already renewed the loan. $error will contain the reason the renewal can not proceed
2839
already renewed the loan.
2840
    $error will contain the reason the renewal can not proceed
2841
    $info will contain the soonest renewal date if the error is 'too soon'
2840
2842
2841
=cut
2843
=cut
2842
2844
Lines 2844-2849 sub CanBookBeRenewed { Link Here
2844
    my ( $borrowernumber, $itemnumber, $override_limit, $cron ) = @_;
2846
    my ( $borrowernumber, $itemnumber, $override_limit, $cron ) = @_;
2845
2847
2846
    my $auto_renew = "no";
2848
    my $auto_renew = "no";
2849
    my $soonest;
2847
2850
2848
    my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
2851
    my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
2849
    my $issue = $item->checkout or return ( 0, 'no_checkout' );
2852
    my $issue = $item->checkout or return ( 0, 'no_checkout' );
Lines 2888-2900 sub CanBookBeRenewed { Link Here
2888
            return ( 0, 'overdue');
2891
            return ( 0, 'overdue');
2889
        }
2892
        }
2890
2893
2891
        $auto_renew = _CanBookBeAutoRenewed({
2894
        ( $auto_renew, $soonest ) = _CanBookBeAutoRenewed({
2892
            patron     => $patron,
2895
            patron     => $patron,
2893
            item       => $item,
2896
            item       => $item,
2894
            branchcode => $branchcode,
2897
            branchcode => $branchcode,
2895
            issue      => $issue
2898
            issue      => $issue
2896
        });
2899
        });
2897
        return ( 0, $auto_renew  ) if $auto_renew =~ 'auto_too_soon' && $cron;
2900
        return ( 0, $auto_renew, $soonest ) if $auto_renew =~ 'auto_too_soon' && $cron;
2898
        # cron wants 'too_soon' over 'on_reserve' for performance and to avoid
2901
        # cron wants 'too_soon' over 'on_reserve' for performance and to avoid
2899
        # extra notices being sent. Cron also implies no override
2902
        # extra notices being sent. Cron also implies no override
2900
        return ( 0, $auto_renew  ) if $auto_renew =~ 'auto_account_expired';
2903
        return ( 0, $auto_renew  ) if $auto_renew =~ 'auto_account_expired';
Lines 2997-3005 sub CanBookBeRenewed { Link Here
2997
    }
3000
    }
2998
3001
2999
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
3002
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
3000
    return ( 0, $auto_renew  ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok";
3003
    return ( 0, $auto_renew, $soonest ) if $auto_renew =~ 'too_soon';#$auto_renew ne "no" && $auto_renew ne "ok";
3001
    if ( GetSoonestRenewDate($borrowernumber, $itemnumber) > dt_from_string() ){
3004
    $soonest = GetSoonestRenewDate($borrowernumber, $itemnumber);
3002
        return (0, "too_soon") unless $override_limit;
3005
    if ( $soonest > dt_from_string() ){
3006
        return (0, "too_soon", $soonest ) unless $override_limit;
3003
    }
3007
    }
3004
3008
3005
    return ( 0, "auto_renew" ) if $auto_renew eq "ok" && !$override_limit; # 0 if auto-renewal should not succeed
3009
    return ( 0, "auto_renew" ) if $auto_renew eq "ok" && !$override_limit; # 0 if auto-renewal should not succeed
Lines 4457-4465 sub _CanBookBeAutoRenewed { Link Here
4457
        }
4461
        }
4458
    }
4462
    }
4459
4463
4460
    if ( GetSoonestRenewDate( $patron->id, $item->id ) > dt_from_string() )
4464
    my $soonest = GetSoonestRenewDate($patron->id, $item->id);
4465
    if ( $soonest > dt_from_string() )
4461
    {
4466
    {
4462
        return "auto_too_soon";
4467
        return ( "auto_too_soon", $soonest );
4463
    }
4468
    }
4464
4469
4465
    return "ok";
4470
    return "ok";
(-)a/t/db_dependent/Circulation.t (-9 / +16 lines)
Lines 418-424 subtest "GetIssuingCharges tests" => sub { Link Here
418
418
419
my ( $reused_itemnumber_1, $reused_itemnumber_2 );
419
my ( $reused_itemnumber_1, $reused_itemnumber_2 );
420
subtest "CanBookBeRenewed tests" => sub {
420
subtest "CanBookBeRenewed tests" => sub {
421
    plan tests => 97;
421
    plan tests => 104;
422
422
423
    C4::Context->set_preference('ItemsDeniedRenewal','');
423
    C4::Context->set_preference('ItemsDeniedRenewal','');
424
    # Generate test biblio
424
    # Generate test biblio
Lines 794-804 subtest "CanBookBeRenewed tests" => sub { Link Here
794
    );
794
    );
795
795
796
    $issue = AddIssue( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
796
    $issue = AddIssue( $renewing_borrower, $item_4->barcode, undef, undef, undef, undef, { auto_renew => 1 } );
797
    ( $renewokay, $error ) =
797
    my $info;
798
    ( $renewokay, $error, $info ) =
798
      CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
799
      CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
799
    is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
800
    is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
800
    is( $error, 'auto_too_soon',
801
    is( $error, 'auto_too_soon',
801
        'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
802
        'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = undef (returned code is auto_too_soon)' );
803
    is( $info, $issue->date_due, "Due date is returned as earliest renewal date when error is 'auto_too_soon'" );
802
    AddReserve(
804
    AddReserve(
803
        {
805
        {
804
            branchcode       => $branch,
806
            branchcode       => $branch,
Lines 817-825 subtest "CanBookBeRenewed tests" => sub { Link Here
817
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
819
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
818
    is( $renewokay, 0, 'Still should not be able to renew' );
820
    is( $renewokay, 0, 'Still should not be able to renew' );
819
    is( $error, 'on_reserve', 'returned code is on_reserve, reserve checked when not checking for cron' );
821
    is( $error, 'on_reserve', 'returned code is on_reserve, reserve checked when not checking for cron' );
820
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber, undef, 1 );
822
    ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber, undef, 1 );
821
    is( $renewokay, 0, 'Still should not be able to renew' );
823
    is( $renewokay, 0, 'Still should not be able to renew' );
822
    is( $error, 'auto_too_soon', 'returned code is auto_too_soon, reserve not checked when checking for cron' );
824
    is( $error, 'auto_too_soon', 'returned code is auto_too_soon, reserve not checked when checking for cron' );
825
    is( $info, $issue->date_due, "Due date is returned as earliest renewal date when error is 'auto_too_soon'" );
823
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber, 1 );
826
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber, 1 );
824
    is( $renewokay, 0, 'Still should not be able to renew' );
827
    is( $renewokay, 0, 'Still should not be able to renew' );
825
    is( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
828
    is( $error, 'on_reserve', 'returned code is on_reserve, auto_too_soon limit is overridden' );
Lines 852-890 subtest "CanBookBeRenewed tests" => sub { Link Here
852
        }
855
        }
853
    );
856
    );
854
857
855
    ( $renewokay, $error ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
858
    ( $renewokay, $error, $info ) = CanBookBeRenewed($renewing_borrowernumber, $item_1->itemnumber);
856
    is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
859
    is( $renewokay, 0, 'Bug 7413: Cannot renew, renewal is premature');
857
    is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
860
    is( $error, 'too_soon', 'Bug 7413: Cannot renew, renewal is premature (returned code is too_soon)');
861
    is( $info, dt_from_string($issue->date_due)->subtract( days => 7 ), "Soonest renew date returned when error is 'too_soon'");
858
862
859
    # Bug 14101
863
    # Bug 14101
860
    # Test premature automatic renewal
864
    # Test premature automatic renewal
861
    ( $renewokay, $error ) =
865
    ( $renewokay, $error, $info ) =
862
      CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
866
      CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
863
    is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
867
    is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
864
    is( $error, 'auto_too_soon',
868
    is( $error, 'auto_too_soon',
865
        'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
869
        'Bug 14101: Cannot renew, renewal is automatic and premature (returned code is auto_too_soon)'
866
    );
870
    );
871
    is( $info, dt_from_string($issue->date_due)->subtract( days => 7 ), "Soonest renew date returned when error is 'auto_too_soon'");
867
872
868
    $renewing_borrower_obj->autorenew_checkouts(0)->store;
873
    $renewing_borrower_obj->autorenew_checkouts(0)->store;
869
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
874
    ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
870
    is( $renewokay, 0, 'No renewal before is 7, patron opted out of auto_renewal still cannot renew early' );
875
    is( $renewokay, 0, 'No renewal before is 7, patron opted out of auto_renewal still cannot renew early' );
871
    is( $error, 'too_soon', 'Error is too_soon, no auto' );
876
    is( $error, 'too_soon', 'Error is too_soon, no auto' );
877
    is( $info, dt_from_string($issue->date_due)->subtract( days => 7 ), "Soonest renew date returned when error is 'too_soon'");
872
    $renewing_borrower_obj->autorenew_checkouts(1)->store;
878
    $renewing_borrower_obj->autorenew_checkouts(1)->store;
873
879
874
    # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
880
    # Change policy so that loans can only be renewed exactly on due date (0 days prior to due date)
875
    # and test automatic renewal again
881
    # and test automatic renewal again
876
    $dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'norenewalbefore'});
882
    $dbh->do(q{UPDATE circulation_rules SET rule_value = '0' WHERE rule_name = 'norenewalbefore'});
877
    ( $renewokay, $error ) =
883
    ( $renewokay, $error, $info ) =
878
      CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
884
      CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
879
    is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
885
    is( $renewokay, 0, 'Bug 14101: Cannot renew, renewal is automatic and premature' );
880
    is( $error, 'auto_too_soon',
886
    is( $error, 'auto_too_soon',
881
        'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
887
        'Bug 14101: Cannot renew, renewal is automatic and premature, "No renewal before" = 0 (returned code is auto_too_soon)'
882
    );
888
    );
889
    is( $info, dt_from_string($issue->date_due), "Soonest renew date returned when error is 'auto_too_soon'");
883
890
884
    $renewing_borrower_obj->autorenew_checkouts(0)->store;
891
    $renewing_borrower_obj->autorenew_checkouts(0)->store;
885
    ( $renewokay, $error ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
892
    ( $renewokay, $error, $info ) = CanBookBeRenewed( $renewing_borrowernumber, $item_4->itemnumber );
886
    is( $renewokay, 0, 'No renewal before is 0, patron opted out of auto_renewal still cannot renew early' );
893
    is( $renewokay, 0, 'No renewal before is 0, patron opted out of auto_renewal still cannot renew early' );
887
    is( $error, 'too_soon', 'Error is too_soon, no auto' );
894
    is( $error, 'too_soon', 'Error is too_soon, no auto' );
895
    is( $info, dt_from_string($issue->date_due), "Soonest renew date returned when error is 'auto_too_soon'");
888
    $renewing_borrower_obj->autorenew_checkouts(1)->store;
896
    $renewing_borrower_obj->autorenew_checkouts(1)->store;
889
897
890
    # Change policy so that loans can be renewed 99 days prior to the due date
898
    # Change policy so that loans can be renewed 99 days prior to the due date
891
- 

Return to bug 30167