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

(-)a/C4/Circulation.pm (-10 / +15 lines)
Lines 2723-2729 sub GetUpcomingDueIssues { Link Here
2723
2723
2724
=head2 CanBookBeRenewed
2724
=head2 CanBookBeRenewed
2725
2725
2726
  ($ok,$error) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
2726
  ($ok,$error,$info) = &CanBookBeRenewed($borrowernumber, $itemnumber[, $override_limit]);
2727
2727
2728
Find out whether a borrowed item may be renewed.
2728
Find out whether a borrowed item may be renewed.
2729
2729
Lines 2741-2747 that are automatically renewed. Link Here
2741
C<$CanBookBeRenewed> returns a true value if the item may be renewed. The
2741
C<$CanBookBeRenewed> returns a true value if the item may be renewed. The
2742
item must currently be on loan to the specified borrower; renewals
2742
item must currently be on loan to the specified borrower; renewals
2743
must be allowed for the item's type; and the borrower must not have
2743
must be allowed for the item's type; and the borrower must not have
2744
already renewed the loan. $error will contain the reason the renewal can not proceed
2744
already renewed the loan.
2745
    $error will contain the reason the renewal can not proceed
2746
    $info will contain the soonest renewal date if the error is 'too soon'
2745
2747
2746
=cut
2748
=cut
2747
2749
Lines 2749-2754 sub CanBookBeRenewed { Link Here
2749
    my ( $borrowernumber, $itemnumber, $override_limit, $cron ) = @_;
2751
    my ( $borrowernumber, $itemnumber, $override_limit, $cron ) = @_;
2750
2752
2751
    my $auto_renew = "no";
2753
    my $auto_renew = "no";
2754
    my $soonest;
2752
2755
2753
    my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
2756
    my $item      = Koha::Items->find($itemnumber)      or return ( 0, 'no_item' );
2754
    my $issue = $item->checkout or return ( 0, 'no_checkout' );
2757
    my $issue = $item->checkout or return ( 0, 'no_checkout' );
Lines 2793-2805 sub CanBookBeRenewed { Link Here
2793
            return ( 0, 'overdue');
2796
            return ( 0, 'overdue');
2794
        }
2797
        }
2795
2798
2796
        $auto_renew = _CanBookBeAutoRenewed({
2799
        ( $auto_renew, $soonest ) = _CanBookBeAutoRenewed({
2797
            patron     => $patron,
2800
            patron     => $patron,
2798
            item       => $item,
2801
            item       => $item,
2799
            branchcode => $branchcode,
2802
            branchcode => $branchcode,
2800
            issue      => $issue
2803
            issue      => $issue
2801
        });
2804
        });
2802
        return ( 0, $auto_renew  ) if $auto_renew =~ 'auto_too_soon' && $cron;
2805
        return ( 0, $auto_renew, $soonest ) if $auto_renew =~ 'auto_too_soon' && $cron;
2803
        # cron wants 'too_soon' over 'on_reserve' for performance and to avoid
2806
        # cron wants 'too_soon' over 'on_reserve' for performance and to avoid
2804
        # extra notices being sent. Cron also implies no override
2807
        # extra notices being sent. Cron also implies no override
2805
        return ( 0, $auto_renew  ) if $auto_renew =~ 'auto_account_expired';
2808
        return ( 0, $auto_renew  ) if $auto_renew =~ 'auto_account_expired';
Lines 2867-2875 sub CanBookBeRenewed { Link Here
2867
    }
2870
    }
2868
2871
2869
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
2872
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
2870
    return ( 0, $auto_renew  ) 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";
2871
    if ( GetSoonestRenewDate($borrowernumber, $itemnumber) > dt_from_string() ){
2874
    $soonest = GetSoonestRenewDate($borrowernumber, $itemnumber);
2872
        return (0, "too_soon") unless $override_limit;
2875
    if ( $soonest > dt_from_string() ){
2876
        return (0, "too_soon", $soonest ) unless $override_limit;
2873
    }
2877
    }
2874
2878
2875
    return ( 0, "auto_renew" ) if $auto_renew eq "ok" && !$override_limit; # 0 if auto-renewal should not succeed
2879
    return ( 0, "auto_renew" ) if $auto_renew eq "ok" && !$override_limit; # 0 if auto-renewal should not succeed
Lines 4329-4336 sub _CanBookBeAutoRenewed { Link Here
4329
4333
4330
    if ( defined $issuing_rule->{norenewalbefore}
4334
    if ( defined $issuing_rule->{norenewalbefore}
4331
        and $issuing_rule->{norenewalbefore} ne "" ) {
4335
        and $issuing_rule->{norenewalbefore} ne "" ) {
4332
        if ( GetSoonestRenewDate($patron->id, $item->id) > dt_from_string()) {
4336
        my $soonest = GetSoonestRenewDate($patron->id, $item->id);
4333
            return "auto_too_soon";
4337
        if ( $soonest > dt_from_string()) {
4338
            return ( "auto_too_soon", $soonest );
4334
        } else {
4339
        } else {
4335
            return "ok";
4340
            return "ok";
4336
        }
4341
        }
Lines 4342-4348 sub _CanBookBeAutoRenewed { Link Here
4342
    if ( $now >= dt_from_string( $issue->date_due, 'sql' ) ){
4347
    if ( $now >= dt_from_string( $issue->date_due, 'sql' ) ){
4343
        return "ok";
4348
        return "ok";
4344
    } else {
4349
    } else {
4345
        return "auto_too_soon";
4350
        return ( "auto_too_soon", $issue->date_due );
4346
    }
4351
    }
4347
}
4352
}
4348
4353
(-)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 => 93;
421
    plan tests => 100;
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