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

(-)a/C4/Circulation.pm (-31 / +36 lines)
Lines 1371-1395 sub CanBookBeIssued { Link Here
1371
    }
1371
    }
1372
1372
1373
    # CHECK FOR QUOTAS
1373
    # CHECK FOR QUOTAS
1374
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1374
    if ( my $quotas = $patron->all_quotas->filter_by_active ) {
1375
        if (ref($quota) eq 'ARRAY') {
1375
        if ( $quotas->count > 1 ) {
1376
1376
            # Multiple available quotas found - need confirmation from user
1377
            # Multiple available quotas found - need confirmation from user
1377
            $needsconfirmation{QUOTA_SELECT} = $quota;
1378
            $needsconfirmation{QUOTA_SELECT} = $quotas;
1378
        }
1379
        } elsif ( $quotas->count == 1 ) {
1379
        elsif (!$quota->has_available_quota) {
1380
            my $quota = $quotas->next;
1380
            if ( C4::Context->preference("AllowQuotaOverride") ) {
1381
            if ( !$quota->has_available_quota ) {
1381
                $needsconfirmation{QUOTA_EXCEEDED} = {
1382
                if ( C4::Context->preference("AllowQuotaOverride") ) {
1382
                    available => $quota->available_quota,
1383
                    $needsconfirmation{QUOTA_EXCEEDED} = {
1383
                    total => $quota->allocation,
1384
                        available => $quota->available_quota,
1384
                    used => $quota->used,
1385
                        total     => $quota->allocation,
1385
                };
1386
                        used      => $quota->used,
1386
            }
1387
                    };
1387
            else {
1388
                } else {
1388
                $issuingimpossible{QUOTA_EXCEEDED} = {
1389
                    $issuingimpossible{QUOTA_EXCEEDED} = {
1389
                    available => $quota->available_quota,
1390
                        available => $quota->available_quota,
1390
                    total => $quota->allocation,
1391
                        total     => $quota->allocation,
1391
                    used => $quota->used,
1392
                        used      => $quota->used,
1392
                };
1393
                    };
1394
                }
1393
            }
1395
            }
1394
        }
1396
        }
1395
    }
1397
    }
Lines 1989-1995 sub AddIssue { Link Here
1989
            if ($selected_quota_id) {
1991
            if ($selected_quota_id) {
1990
                $quota = Koha::Patron::Quotas->find($selected_quota_id);
1992
                $quota = Koha::Patron::Quotas->find($selected_quota_id);
1991
            } else {
1993
            } else {
1992
                $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber);
1994
                $quota = $patron->all_quotas->filter_by_active->single;
1993
            }
1995
            }
1994
1996
1995
            if ($quota) {
1997
            if ($quota) {
Lines 3351-3364 sub CanBookBeRenewed { Link Here
3351
    }
3353
    }
3352
3354
3353
    # # CHECK FOR QUOTAS
3355
    # # CHECK FOR QUOTAS
3354
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3356
    if ( my $quota_usage = Koha::Patron::Quota::Usages->find( { issue_id => $issue->issue_id } ) ) {
3355
3357
3356
        # Get current active quota for the patron
3358
        # Get current actives quota for the patron
3357
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3359
        if ( my $active_quotas = $patron->all_quotas->filter_by_active ) {
3358
3360
            my $all_exceeded = 1;
3359
            if (!$active_quota->has_available_quota) {
3361
            while ( my $active_quota = $active_quotas->next ) {
3360
                return (0, "QUOTA_EXCEEDED");
3362
                if ( $active_quota->has_available_quota ) {
3363
                    $all_exceeded = 0;
3364
                }
3361
            }
3365
            }
3366
            return ( 0, "QUOTA_EXCEEDED" ) if $all_exceeded;
3362
        }
3367
        }
3363
    }
3368
    }
3364
3369
Lines 3477-3488 sub AddRenewal { Link Here
3477
    # Check quotas and record usage if needed
3482
    # Check quotas and record usage if needed
3478
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3483
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3479
        # Get current active quota for the patron
3484
        # Get current active quota for the patron
3480
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3485
        if (my $active_quota = Koha::Patron::Quotas->find($quota_usage->patron_quota_id)) {
3481
            # Update patron's used quota value
3486
            # Update patron's used quota value
3482
            $active_quota->add_usage({
3487
                $active_quota->add_usage({
3483
                patron_id => $patron->patron_id,
3488
                    patron_id => $patron->borrowernumber,
3484
                issue_id  => $issue->issue_id,
3489
                    issue_id  => $issue->issue_id,
3485
            });
3490
                });
3486
        }
3491
        }
3487
    }
3492
    }
3488
3493
(-)a/Koha/Patron.pm (+63 lines)
Lines 614-619 sub is_guarantor { Link Here
614
    return $self->guarantee_relationships()->count();
614
    return $self->guarantee_relationships()->count();
615
}
615
}
616
616
617
=head3 quotas
618
619
Returns any quotas this patron may have
620
621
=cut
622
623
sub quotas {
624
    my ($self) = @_;
625
    my $quotas_rs = $self->_result->patron_quotas;
626
    return Koha::Patron::Quotas->_new_from_dbic($quotas_rs);
627
}
628
629
=head3 guarantors_quotas
630
631
Returns any guarantor quotas this patron may use
632
633
=cut
634
635
sub guarantors_quotas {
636
    my ($self) = @_;
637
    my $guarantors_query =
638
        $self->_result->borrower_relationships_guarantors->search( {}, { columns => ['guarantor_id'] } )->as_query;
639
    return Koha::Patron::Quotas->search(
640
        {
641
            patron_id => { '-in' => $guarantors_query },
642
        }
643
    );
644
}
645
646
=head3 all_quotas
647
648
Returns a Koha::Patron::Quotas resultset containing:
649
- Directly related quotas (first)
650
- Guarantor quotas (second, if applicable)
651
652
=cut
653
654
sub all_quotas {
655
    my ($self) = @_;
656
657
    # Step 1: Build a subquery to fetch the patron's own ID + guarantor IDs from borrower_relationships
658
    my $guarantors_query =
659
        $self->_result->borrower_relationships_guarantees->search( {}, { columns => ['guarantor_id'] } )->as_query;
660
661
    # Step 2: Fetch quotas where borrowernumber is either:
662
    # - The patron's own borrowernumber
663
    # - One of the guarantor_id values found in the subquery
664
    return Koha::Patron::Quotas->search(
665
        {
666
            '-or' => [
667
                { patron_id => $self->id },                         # The patron's own quotas
668
                { patron_id => { '-in' => $guarantors_query } },    # Guarantor quotas if they exist
669
            ],
670
        },
671
        {
672
            order_by => \[
673
                "CASE WHEN patron_id = ? THEN 0 ELSE 1 END, patron_id",
674
                $self->id
675
            ],
676
        }
677
    );
678
}
679
617
=head3 relationships_debt
680
=head3 relationships_debt
618
681
619
Returns the amount owed by the patron's guarantors *and* the other guarantees of those guarantors
682
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 320-325 if ($patron) { Link Here
320
    {
320
    {
321
        $template->param( limited_category => 1 );
321
        $template->param( limited_category => 1 );
322
    }
322
    }
323
323
}
324
}
324
325
325
#
326
#
326
- 

Return to bug 38924