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

(-)a/Koha/Patron.pm (-1 / +100 lines)
Lines 2879-2884 sub consent { Link Here
2879
        : Koha::Patron::Consent->new( { borrowernumber => $self->borrowernumber, type => $type } );
2879
        : Koha::Patron::Consent->new( { borrowernumber => $self->borrowernumber, type => $type } );
2880
}
2880
}
2881
2881
2882
2883
=head3 can_borrow
2884
2885
my $patron_borrowing_status = $patron->can_borrow( { patron => $patron } );
2886
2887
This method determines whether a borrower is able to borrow based on various parameters.
2888
- Debarrments
2889
- Expiry
2890
- Charges
2891
2892
If any blockers are found, these are returned in a hash
2893
2894
=cut
2895
2896
sub can_borrow {
2897
    my ( $self, $args ) = @_;
2898
2899
    my $patron = $args->{patron};
2900
    my $status = { can_borrow => 1 };
2901
2902
    $status->{debarred}   = 1 if $patron->debarred;
2903
    $status->{expired}    = 1 if $patron->is_expired;
2904
    $status->{can_borrow} = 0 if $status->{debarred} || $status->{expired};
2905
2906
    # Patron charges
2907
    my $patron_charge_limits = $patron->is_patron_inside_charge_limits( { patron => $patron } );
2908
    %$status = ( %$status, %$patron_charge_limits );
2909
    $status->{can_borrow} = 0
2910
        if $patron_charge_limits->{noissuescharge}->{overlimit}
2911
        || $patron_charge_limits->{NoIssuesChargeGuarantees}->{overlimit}
2912
        || $patron_charge_limits->{NoIssuesChargeGuarantorsWithGuarantees}->{overlimit};
2913
2914
    return $status;
2915
}
2916
2917
=head3 is_patron_inside_charge_limits
2918
2919
my $patron_charge_limits = $patron->is_patron_inside_charge_limits( { patron => $patron } );
2920
2921
Checks the current account balance for a patron and any guarantors/guarantees and compares it with any charge limits in place
2922
Takes into account patron category level charge limits in the first instance and defaults to global sysprefs if not set
2923
2924
=cut
2925
2926
sub is_patron_inside_charge_limits {
2927
    my ( $self, $args ) = @_;
2928
2929
    my $borrowernumber       = $args->{borrowernumber};
2930
    my $patron               = $args->{patron} || Koha::Patrons->find( { borrowernumber => $borrowernumber } );
2931
    my $patron_category      = $patron->category;
2932
    my $patron_charge_limits = {};
2933
2934
    my $no_issues_charge = $patron_category->noissuescharge || C4::Context->preference('noissuescharge') || 0;
2935
    my $no_issues_charge_guarantees =
2936
        $patron_category->noissueschargeguarantees || C4::Context->preference('NoIssuesChargeGuarantees') || 0;
2937
    my $no_issues_charge_guarantors_with_guarantees =
2938
           $patron_category->noissueschargeguarantorswithguarantees
2939
        || C4::Context->preference('NoIssuesChargeGuarantorsWithGuarantees')
2940
        || 0;
2941
2942
    my $non_issues_charges            = $patron->account->non_issues_charges;
2943
    my $guarantees_non_issues_charges = 0;
2944
    my $guarantors_non_issues_charges = 0;
2945
2946
    # Check the debt of this patrons guarantees
2947
    if ( defined $no_issues_charge_guarantees ) {
2948
        my @guarantees = map { $_->guarantee } $patron->guarantee_relationships->as_list;
2949
        foreach my $g (@guarantees) {
2950
            $guarantees_non_issues_charges += $g->account->non_issues_charges;
2951
        }
2952
    }
2953
2954
    # Check the debt of this patrons guarantors *and* the guarantees of those guarantors
2955
    if ( defined $no_issues_charge_guarantors_with_guarantees ) {
2956
        $guarantors_non_issues_charges = $patron->relationships_debt(
2957
            { include_guarantors => 1, only_this_guarantor => 0, include_this_patron => 1 } );
2958
    }
2959
2960
    # Return hash for each charge limit - limit, charge, overlimit
2961
    $patron_charge_limits->{noissuescharge} =
2962
        { limit => $no_issues_charge, charge => $non_issues_charges, overlimit => 0 };
2963
    $patron_charge_limits->{noissuescharge}->{overlimit} = 1
2964
        if $no_issues_charge && $non_issues_charges > $no_issues_charge;
2965
2966
    $patron_charge_limits->{NoIssuesChargeGuarantees} =
2967
        { limit => $no_issues_charge_guarantees, charge => $guarantees_non_issues_charges, overlimit => 0 };
2968
    $patron_charge_limits->{NoIssuesChargeGuarantees}->{overlimit} = 1
2969
        if $no_issues_charge_guarantees && $guarantees_non_issues_charges > $no_issues_charge_guarantees;
2970
2971
    $patron_charge_limits->{NoIssuesChargeGuarantorsWithGuarantees} = {
2972
        limit     => $no_issues_charge_guarantors_with_guarantees, charge => $guarantors_non_issues_charges,
2973
        overlimit => 0
2974
    };
2975
    $patron_charge_limits->{NoIssuesChargeGuarantorsWithGuarantees}->{overlimit} = 1
2976
        if $no_issues_charge_guarantors_with_guarantees
2977
        && $guarantors_non_issues_charges > $no_issues_charge_guarantors_with_guarantees;
2978
2979
    return $patron_charge_limits;
2980
}
2981
2882
=head2 Internal methods
2982
=head2 Internal methods
2883
2983
2884
=head3 _type
2984
=head3 _type
2885
- 

Return to bug 28924