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

(-)a/C4/Circulation.pm (-31 / +36 lines)
Lines 1377-1401 sub CanBookBeIssued { Link Here
1377
    }
1377
    }
1378
1378
1379
    # CHECK FOR QUOTAS
1379
    # CHECK FOR QUOTAS
1380
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1380
    if ( my $quotas = $patron->all_quotas->filter_by_active ) {
1381
        if (ref($quota) eq 'ARRAY') {
1381
        if ( $quotas->count > 1 ) {
1382
1382
            # Multiple available quotas found - need confirmation from user
1383
            # Multiple available quotas found - need confirmation from user
1383
            $needsconfirmation{QUOTA_SELECT} = $quota;
1384
            $needsconfirmation{QUOTA_SELECT} = $quotas;
1384
        }
1385
        } elsif ( $quotas->count == 1 ) {
1385
        elsif (!$quota->has_available_quota) {
1386
            my $quota = $quotas->next;
1386
            if ( C4::Context->preference("AllowQuotaOverride") ) {
1387
            if ( !$quota->has_available_quota ) {
1387
                $needsconfirmation{QUOTA_EXCEEDED} = {
1388
                if ( C4::Context->preference("AllowQuotaOverride") ) {
1388
                    available => $quota->available_quota,
1389
                    $needsconfirmation{QUOTA_EXCEEDED} = {
1389
                    total => $quota->allocation, 
1390
                        available => $quota->available_quota,
1390
                    used => $quota->used,
1391
                        total     => $quota->allocation,
1391
                };
1392
                        used      => $quota->used,
1392
            }
1393
                    };
1393
            else {
1394
                } else {
1394
                $issuingimpossible{QUOTA_EXCEEDED} = {
1395
                    $issuingimpossible{QUOTA_EXCEEDED} = {
1395
                    available => $quota->available_quota,
1396
                        available => $quota->available_quota,
1396
                    total => $quota->allocation, 
1397
                        total     => $quota->allocation,
1397
                    used => $quota->used,
1398
                        used      => $quota->used,
1398
                };
1399
                    };
1400
                }
1399
            }
1401
            }
1400
        }
1402
        }
1401
    }
1403
    }
Lines 2023-2029 sub AddIssue { Link Here
2023
            if ($selected_quota_id) {
2025
            if ($selected_quota_id) {
2024
                $quota = Koha::Patron::Quotas->find($selected_quota_id);
2026
                $quota = Koha::Patron::Quotas->find($selected_quota_id);
2025
            } else {
2027
            } else {
2026
                $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber); 
2028
                $quota = $patron->all_quotas->filter_by_active->single;
2027
            }
2029
            }
2028
2030
2029
            if ($quota) {
2031
            if ($quota) {
Lines 3388-3401 sub CanBookBeRenewed { Link Here
3388
    }
3390
    }
3389
3391
3390
    # # CHECK FOR QUOTAS  
3392
    # # CHECK FOR QUOTAS  
3391
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3393
    if ( my $quota_usage = Koha::Patron::Quota::Usages->find( { issue_id => $issue->issue_id } ) ) {
3392
        
3394
3393
        # Get current active quota for the patron
3395
        # Get current actives quota for the patron
3394
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3396
        if ( my $active_quotas = $patron->all_quotas->filter_by_active ) {
3395
            
3397
            my $all_exceeded = 1;
3396
            if (!$active_quota->has_available_quota) {
3398
            while ( my $active_quota = $active_quotas->next ) {
3397
                return (0, "QUOTA_EXCEEDED");
3399
                if ( $active_quota->has_available_quota ) {
3400
                    $all_exceeded = 0;
3401
                }
3398
            }
3402
            }
3403
            return ( 0, "QUOTA_EXCEEDED" ) if $all_exceeded;
3399
        }
3404
        }
3400
    }
3405
    }
3401
3406
Lines 3516-3527 sub AddRenewal { Link Here
3516
    # Check quotas and record usage if needed
3521
    # Check quotas and record usage if needed
3517
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3522
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3518
        # Get current active quota for the patron
3523
        # Get current active quota for the patron
3519
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3524
        if (my $active_quota = Koha::Patron::Quotas->find($quota_usage->patron_quota_id)) {
3520
            # Update patron's used quota value
3525
            # Update patron's used quota value
3521
            $active_quota->add_usage({
3526
                $active_quota->add_usage({
3522
                patron_id => $patron->patron_id,
3527
                    patron_id => $patron->borrowernumber,
3523
                issue_id  => $issue->issue_id,
3528
                    issue_id  => $issue->issue_id,
3524
            });
3529
                });
3525
        }
3530
        }
3526
    }
3531
    }
3527
3532
(-)a/Koha/Patron.pm (+63 lines)
Lines 616-621 sub is_guarantor { Link Here
616
    return $self->guarantee_relationships()->count();
616
    return $self->guarantee_relationships()->count();
617
}
617
}
618
618
619
=head3 quotas
620
621
Returns any quotas this patron may have
622
623
=cut
624
625
sub quotas {
626
    my ($self) = @_;
627
    my $quotas_rs = $self->_result->patron_quotas;
628
    return Koha::Patron::Quotas->_new_from_dbic($quotas_rs);
629
}
630
631
=head3 guarantors_quotas
632
633
Returns any guarantor quotas this patron may use
634
635
=cut
636
637
sub guarantors_quotas {
638
    my ($self) = @_;
639
    my $guarantors_query =
640
        $self->_result->borrower_relationships_guarantors->search( {}, { columns => ['guarantor_id'] } )->as_query;
641
    return Koha::Patron::Quotas->search(
642
        {
643
            patron_id => { '-in' => $guarantors_query },
644
        }
645
    );
646
}
647
648
=head3 all_quotas
649
650
Returns a Koha::Patron::Quotas resultset containing:
651
- Directly related quotas (first)
652
- Guarantor quotas (second, if applicable)
653
654
=cut
655
656
sub all_quotas {
657
    my ($self) = @_;
658
659
    # Step 1: Build a subquery to fetch the patron's own ID + guarantor IDs from borrower_relationships
660
    my $guarantors_query =
661
        $self->_result->borrower_relationships_guarantees->search( {}, { columns => ['guarantor_id'] } )->as_query;
662
663
    # Step 2: Fetch quotas where borrowernumber is either:
664
    # - The patron's own borrowernumber
665
    # - One of the guarantor_id values found in the subquery
666
    return Koha::Patron::Quotas->search(
667
        {
668
            '-or' => [
669
                { patron_id => $self->id },                         # The patron's own quotas
670
                { patron_id => { '-in' => $guarantors_query } },    # Guarantor quotas if they exist
671
            ],
672
        },
673
        {
674
            order_by => \[
675
                "CASE WHEN patron_id = ? THEN 0 ELSE 1 END, patron_id",
676
                $self->id
677
            ],
678
        }
679
    );
680
}
681
619
=head3 relationships_debt
682
=head3 relationships_debt
620
683
621
Returns the amount owed by the patron's guarantors *and* the other guarantees of those guarantors
684
Returns the amount owed by the patron's guarantors *and* the other guarantees of those guarantors
(-)a/Koha/Patron/Quotas.pm (-101 / +7 lines)
Lines 13-83 Koha::Patron::Quotas - Koha Patron Quota Object set class Link Here
13
13
14
=head2 Class Methods
14
=head2 Class Methods
15
15
16
=head3 get_patron_quota
17
18
    my $quota = Koha::Patron::Quotas->get_patron_quota($patron_id);
19
20
Searches for any applicable quota for the given patron. Returns:
21
- First available quota found for patron or their guarantor (if UseGuarantorQuota enabled)
22
- If no available quota found, returns the first found quota
23
- Returns undef if no quota found
24
25
=cut
26
27
sub get_patron_quota {
28
    my ( $self, $patron_id ) = @_;
29
30
    my @available_quotas;
31
    my $first_found_quota = undef;
32
33
    # First check patron's own quota
34
    my $patron_quota = $self->search(
35
        {
36
            patron_id  => $patron_id,
37
            start_date => { '<=' => \'CURRENT_DATE' },
38
            end_date   => { '>=' => \'CURRENT_DATE' }
39
        }
40
    )->single;
41
42
    if ( $patron_quota && $patron_quota->has_available_quota ) {
43
        push @available_quotas, $patron_quota;
44
    }
45
    $first_found_quota ||= $patron_quota if $patron_quota;
46
47
    # Then check all guarantor quotas if enabled
48
    if ( C4::Context->preference('UseGuarantorQuota') ) {
49
        my $patron     = Koha::Patrons->find($patron_id);
50
        my @guarantors = $patron->guarantor_relationships->guarantors->as_list;
51
52
        foreach my $guarantor (@guarantors) {
53
            my $guarantor_quota = $self->search(
54
                {
55
                    patron_id  => $guarantor->borrowernumber,
56
                    start_date => { '<=' => \'CURRENT_DATE' },
57
                    end_date   => { '>=' => \'CURRENT_DATE' }
58
                }
59
            )->single;
60
            
61
            if ($guarantor_quota) {
62
63
                # Store first found quota in case we need it later
64
                $first_found_quota ||= $guarantor_quota;
65
66
                # Collect available guarantor quotas
67
                if ( $guarantor_quota->has_available_quota ) {
68
                    push @available_quotas, $guarantor_quota;
69
                }
70
            }
71
        }
72
    }
73
74
    # Return single quota if only one available, array of quotas if multiple available,
75
    # first found quota if none available
76
    return $available_quotas[0] if @available_quotas == 1;
77
    return \@available_quotas   if @available_quotas > 1;
78
    return $first_found_quota;
79
}
80
81
=head3 create_quota
16
=head3 create_quota
82
17
83
Creates a new quota record for a patron
18
Creates a new quota record for a patron
Lines 90-144 sub create_quota { Link Here
90
    return Koha::Patron::Quota->new($params)->store;
25
    return Koha::Patron::Quota->new($params)->store;
91
}
26
}
92
27
93
=head3 search_by_patron
28
=head3 filter_by_active
94
29
95
Finds all quotas for a patron
30
    $quotas->filter_by_active;
96
31
97
=cut
32
Returns the currently active quotas for the resultset.
98
33
99
sub search_by_patron {
34
Active means start_date <= NOW() and end_date >= NOW().
100
    my ( $self, $patron_id ) = @_;
101
    return $self->search( { patron_id => $patron_id } );
102
}
103
104
=head3 get_active_quotas
105
106
Returns multiple active quota records
107
35
108
=cut
36
=cut
109
37
110
sub get_active_quotas {
38
sub filter_by_active {
111
    my ($self) = @_;
39
    my ($self) = @_;
112
    return $self->search(
40
    return $self->search(
113
        {
41
        {
114
            start_date => { '<=' => \'CURRENT_DATE' },
42
            start_date => { '<=' => \'NOW()' },
115
            end_date   => { '>=' => \'CURRENT_DATE' }
43
            end_date   => { '>=' => \'NOW()' },
116
        }
44
        }
117
    );
45
    );
118
}
46
}
119
47
120
=head2 get_active_quota
121
122
  my $active_quota = Koha::Patron::Quotas->get_active_quota($patron_id);
123
124
Returns the currently active quota for a patron if one exists.
125
Active means start_date <= NOW() and end_date >= NOW().
126
Returns undef if no active quota found.
127
128
=cut
129
130
sub get_active_quota {
131
    my ( $self, $patron_id ) = @_;
132
133
    return Koha::Patron::Quotas->search(
134
        {
135
            patron_id  => $patron_id,
136
            start_date => { '<=' => \'CURRENT_DATE' },
137
            end_date   => { '>=' => \'CURRENT_DATE' }
138
        }
139
    )->single;
140
}
141
142
=head2 Internal methods
48
=head2 Internal methods
143
49
144
=head3 _type
50
=head3 _type
(-)a/Koha/REST/V1/Patrons/Quotas.pm (-1 / +1 lines)
Lines 34-40 sub list { Link Here
34
        }
34
        }
35
35
36
        if ($only_active) {
36
        if ($only_active) {
37
            $quotas_set = $quotas_set->get_active_quotas;
37
            $quotas_set = $quotas_set->filter_by_active;
38
        }
38
        }
39
39
40
        return $c->render(
40
        return $c->render(
(-)a/circ/circulation.pl (-1 / +1 lines)
Lines 321-326 if ($patron) { Link Here
321
    {
321
    {
322
        $template->param( limited_category => 1 );
322
        $template->param( limited_category => 1 );
323
    }
323
    }
324
324
}
325
}
325
326
326
#
327
#
327
- 

Return to bug 38924