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

(-)a/C4/Circulation.pm (-107 / +107 lines)
Lines 2701-2706 sub CanBookBeRenewed { Link Here
2701
2701
2702
    my $patron = $issue->patron or return;
2702
    my $patron = $issue->patron or return;
2703
2703
2704
    # override_limit will override anything else except on_reserve
2705
    unless ( $override_limit ){
2706
        my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed );
2707
        my $issuing_rule = Koha::CirculationRules->get_effective_rules(
2708
            {
2709
                categorycode => $patron->categorycode,
2710
                itemtype     => $item->effective_itemtype,
2711
                branchcode   => $branchcode,
2712
                rules => [
2713
                    'renewalsallowed',
2714
                    'no_auto_renewal_after',
2715
                    'no_auto_renewal_after_hard_limit',
2716
                    'lengthunit',
2717
                    'norenewalbefore',
2718
                ]
2719
            }
2720
        );
2721
2722
        return ( 0, "too_many" )
2723
          if not $issuing_rule->{renewalsallowed} or $issuing_rule->{renewalsallowed} <= $issue->renewals;
2724
2725
        my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
2726
        my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
2727
        $patron         = Koha::Patrons->find($borrowernumber); # FIXME Is this really useful?
2728
        my $restricted  = $patron->is_debarred;
2729
        my $hasoverdues = $patron->has_overdues;
2730
2731
        if ( $restricted and $restrictionblockrenewing ) {
2732
            return ( 0, 'restriction');
2733
        } elsif ( ($hasoverdues and $overduesblockrenewing eq 'block') || ($issue->is_overdue and $overduesblockrenewing eq 'blockitem') ) {
2734
            return ( 0, 'overdue');
2735
        }
2736
2737
        if ( $issue->auto_renew ) {
2738
2739
            if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) {
2740
                return ( 0, 'auto_account_expired' );
2741
            }
2742
2743
            if ( defined $issuing_rule->{no_auto_renewal_after}
2744
                    and $issuing_rule->{no_auto_renewal_after} ne "" ) {
2745
                # Get issue_date and add no_auto_renewal_after
2746
                # If this is greater than today, it's too late for renewal.
2747
                my $maximum_renewal_date = dt_from_string($issue->issuedate, 'sql');
2748
                $maximum_renewal_date->add(
2749
                    $issuing_rule->{lengthunit} => $issuing_rule->{no_auto_renewal_after}
2750
                );
2751
                my $now = dt_from_string;
2752
                if ( $now >= $maximum_renewal_date ) {
2753
                    return ( 0, "auto_too_late" );
2754
                }
2755
            }
2756
            if ( defined $issuing_rule->{no_auto_renewal_after_hard_limit}
2757
                          and $issuing_rule->{no_auto_renewal_after_hard_limit} ne "" ) {
2758
                # If no_auto_renewal_after_hard_limit is >= today, it's also too late for renewal
2759
                if ( dt_from_string >= dt_from_string( $issuing_rule->{no_auto_renewal_after_hard_limit} ) ) {
2760
                    return ( 0, "auto_too_late" );
2761
                }
2762
            }
2763
2764
            if ( C4::Context->preference('OPACFineNoRenewalsBlockAutoRenew') ) {
2765
                my $fine_no_renewals = C4::Context->preference("OPACFineNoRenewals");
2766
                my $amountoutstanding =
2767
                  C4::Context->preference("OPACFineNoRenewalsIncludeCredit")
2768
                  ? $patron->account->balance
2769
                  : $patron->account->outstanding_debits->total_outstanding;
2770
                if ( $amountoutstanding and $amountoutstanding > $fine_no_renewals ) {
2771
                    return ( 0, "auto_too_much_oweing" );
2772
                }
2773
            }
2774
        }
2775
2776
        if ( defined $issuing_rule->{norenewalbefore}
2777
            and $issuing_rule->{norenewalbefore} ne "" )
2778
        {
2779
2780
            # Calculate soonest renewal by subtracting 'No renewal before' from due date
2781
            my $soonestrenewal = dt_from_string( $issue->date_due, 'sql' )->subtract(
2782
                $issuing_rule->{lengthunit} => $issuing_rule->{norenewalbefore} );
2783
2784
            # Depending on syspref reset the exact time, only check the date
2785
            if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date'
2786
                and $issuing_rule->{lengthunit} eq 'days' )
2787
            {
2788
                $soonestrenewal->truncate( to => 'day' );
2789
            }
2790
2791
            if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) )
2792
            {
2793
                return ( 0, "auto_too_soon" ) if $issue->auto_renew;
2794
                return ( 0, "too_soon" );
2795
            }
2796
            elsif ( $issue->auto_renew ) {
2797
                return ( 0, "auto_renew" );
2798
            }
2799
        }
2800
2801
        # Fallback for automatic renewals:
2802
        # If norenewalbefore is undef, don't renew before due date.
2803
        if ( $issue->auto_renew ) {
2804
            my $now = dt_from_string;
2805
            return ( 0, "auto_renew" )
2806
              if $now >= dt_from_string( $issue->date_due, 'sql' );
2807
            return ( 0, "auto_too_soon" );
2808
        }
2809
    }
2810
2704
    my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber);
2811
    my ( $resfound, $resrec, undef ) = C4::Reserves::CheckReserves($itemnumber);
2705
2812
2706
    # This item can fill one or more unfilled reserve, can those unfilled reserves
2813
    # This item can fill one or more unfilled reserve, can those unfilled reserves
Lines 2768-2879 sub CanBookBeRenewed { Link Here
2768
    }
2875
    }
2769
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
2876
    return ( 0, "on_reserve" ) if $resfound;    # '' when no hold was found
2770
2877
2771
    return ( 1, undef ) if $override_limit;
2772
2773
    my $branchcode = _GetCircControlBranch( $item->unblessed, $patron->unblessed );
2774
    my $issuing_rule = Koha::CirculationRules->get_effective_rules(
2775
        {
2776
            categorycode => $patron->categorycode,
2777
            itemtype     => $item->effective_itemtype,
2778
            branchcode   => $branchcode,
2779
            rules => [
2780
                'renewalsallowed',
2781
                'no_auto_renewal_after',
2782
                'no_auto_renewal_after_hard_limit',
2783
                'lengthunit',
2784
                'norenewalbefore',
2785
            ]
2786
        }
2787
    );
2788
2789
    return ( 0, "too_many" )
2790
      if not $issuing_rule->{renewalsallowed} or $issuing_rule->{renewalsallowed} <= $issue->renewals;
2791
2792
    my $overduesblockrenewing = C4::Context->preference('OverduesBlockRenewing');
2793
    my $restrictionblockrenewing = C4::Context->preference('RestrictionBlockRenewing');
2794
    $patron         = Koha::Patrons->find($borrowernumber); # FIXME Is this really useful?
2795
    my $restricted  = $patron->is_debarred;
2796
    my $hasoverdues = $patron->has_overdues;
2797
2798
    if ( $restricted and $restrictionblockrenewing ) {
2799
        return ( 0, 'restriction');
2800
    } elsif ( ($hasoverdues and $overduesblockrenewing eq 'block') || ($issue->is_overdue and $overduesblockrenewing eq 'blockitem') ) {
2801
        return ( 0, 'overdue');
2802
    }
2803
2804
    if ( $issue->auto_renew ) {
2805
2806
        if ( $patron->category->effective_BlockExpiredPatronOpacActions and $patron->is_expired ) {
2807
            return ( 0, 'auto_account_expired' );
2808
        }
2809
2810
        if ( defined $issuing_rule->{no_auto_renewal_after}
2811
                and $issuing_rule->{no_auto_renewal_after} ne "" ) {
2812
            # Get issue_date and add no_auto_renewal_after
2813
            # If this is greater than today, it's too late for renewal.
2814
            my $maximum_renewal_date = dt_from_string($issue->issuedate, 'sql');
2815
            $maximum_renewal_date->add(
2816
                $issuing_rule->{lengthunit} => $issuing_rule->{no_auto_renewal_after}
2817
            );
2818
            my $now = dt_from_string;
2819
            if ( $now >= $maximum_renewal_date ) {
2820
                return ( 0, "auto_too_late" );
2821
            }
2822
        }
2823
        if ( defined $issuing_rule->{no_auto_renewal_after_hard_limit}
2824
                      and $issuing_rule->{no_auto_renewal_after_hard_limit} ne "" ) {
2825
            # If no_auto_renewal_after_hard_limit is >= today, it's also too late for renewal
2826
            if ( dt_from_string >= dt_from_string( $issuing_rule->{no_auto_renewal_after_hard_limit} ) ) {
2827
                return ( 0, "auto_too_late" );
2828
            }
2829
        }
2830
2831
        if ( C4::Context->preference('OPACFineNoRenewalsBlockAutoRenew') ) {
2832
            my $fine_no_renewals = C4::Context->preference("OPACFineNoRenewals");
2833
            my $amountoutstanding =
2834
              C4::Context->preference("OPACFineNoRenewalsIncludeCredit")
2835
              ? $patron->account->balance
2836
              : $patron->account->outstanding_debits->total_outstanding;
2837
            if ( $amountoutstanding and $amountoutstanding > $fine_no_renewals ) {
2838
                return ( 0, "auto_too_much_oweing" );
2839
            }
2840
        }
2841
    }
2842
2843
    if ( defined $issuing_rule->{norenewalbefore}
2844
        and $issuing_rule->{norenewalbefore} ne "" )
2845
    {
2846
2847
        # Calculate soonest renewal by subtracting 'No renewal before' from due date
2848
        my $soonestrenewal = dt_from_string( $issue->date_due, 'sql' )->subtract(
2849
            $issuing_rule->{lengthunit} => $issuing_rule->{norenewalbefore} );
2850
2851
        # Depending on syspref reset the exact time, only check the date
2852
        if ( C4::Context->preference('NoRenewalBeforePrecision') eq 'date'
2853
            and $issuing_rule->{lengthunit} eq 'days' )
2854
        {
2855
            $soonestrenewal->truncate( to => 'day' );
2856
        }
2857
2858
        if ( $soonestrenewal > DateTime->now( time_zone => C4::Context->tz() ) )
2859
        {
2860
            return ( 0, "auto_too_soon" ) if $issue->auto_renew;
2861
            return ( 0, "too_soon" );
2862
        }
2863
        elsif ( $issue->auto_renew ) {
2864
            return ( 0, "auto_renew" );
2865
        }
2866
    }
2867
2868
    # Fallback for automatic renewals:
2869
    # If norenewalbefore is undef, don't renew before due date.
2870
    if ( $issue->auto_renew ) {
2871
        my $now = dt_from_string;
2872
        return ( 0, "auto_renew" )
2873
          if $now >= dt_from_string( $issue->date_due, 'sql' );
2874
        return ( 0, "auto_too_soon" );
2875
    }
2876
2877
    return ( 1, undef );
2878
    return ( 1, undef );
2878
}
2879
}
2879
2880
2880
- 

Return to bug 19014