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

(-)a/C4/Circulation.pm (-14 / +35 lines)
Lines 68-73 use Koha::Library::Hours; Link Here
68
use Koha::Patron::Quotas;
68
use Koha::Patron::Quotas;
69
use Koha::Patron::Quota;
69
use Koha::Patron::Quota;
70
use Koha::Patron::Quota::Usage;
70
use Koha::Patron::Quota::Usage;
71
use Koha::Patron::Quota::Usages;
71
72
72
use Carp qw( carp );
73
use Carp qw( carp );
73
use List::MoreUtils qw( any );
74
use List::MoreUtils qw( any );
Lines 1371-1377 sub CanBookBeIssued { Link Here
1371
1372
1372
    # CHECK FOR QUOTAS
1373
    # CHECK FOR QUOTAS
1373
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1374
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1374
        unless ( $quota->has_available_quota ) {
1375
        if (ref($quota) eq 'ARRAY') {
1376
            # Multiple available quotas found - need confirmation from user
1377
            $needsconfirmation{QUOTA_SELECT} = $quota;
1378
        }
1379
        elsif (!$quota->has_available_quota) {
1375
            if ( C4::Context->preference("AllowQuotaOverride") ) {
1380
            if ( C4::Context->preference("AllowQuotaOverride") ) {
1376
                $needsconfirmation{QUOTA_EXCEEDED} = {
1381
                $needsconfirmation{QUOTA_EXCEEDED} = {
1377
                    available => $quota->available_quota,
1382
                    available => $quota->available_quota,
Lines 1620-1625 sub AddIssue { Link Here
1620
    my $auto_renew             = $params && $params->{auto_renew};
1625
    my $auto_renew             = $params && $params->{auto_renew};
1621
    my $cancel_recall          = $params && $params->{cancel_recall};
1626
    my $cancel_recall          = $params && $params->{cancel_recall};
1622
    my $recall_id              = $params && $params->{recall_id};
1627
    my $recall_id              = $params && $params->{recall_id};
1628
    my $selected_quota_id      = $params && $params->{selected_quota_id};
1623
    my $dbh                    = C4::Context->dbh;
1629
    my $dbh                    = C4::Context->dbh;
1624
    my $barcodecheck           = CheckValidBarcode($barcode);
1630
    my $barcodecheck           = CheckValidBarcode($barcode);
1625
1631
Lines 1979-1987 sub AddIssue { Link Here
1979
                if C4::Context->preference('RealTimeHoldsQueue');
1985
                if C4::Context->preference('RealTimeHoldsQueue');
1980
1986
1981
            # Check quotas and record usage if needed
1987
            # Check quotas and record usage if needed
1982
            if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1988
            my $quota;
1983
                # Update patron's used quota value
1989
            if ($selected_quota_id) {
1984
                $quota->add_usage({
1990
                $quota = Koha::Patron::Quotas->find($selected_quota_id);
1991
            } else {
1992
                $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber);
1993
            }
1994
1995
            if ($quota) {
1996
              # Update patron's used quota value
1997
              $quota->add_usage({
1985
                    patron_id => $patron->patron_id,
1998
                    patron_id => $patron->patron_id,
1986
                    issue_id => $issue->issue_id,
1999
                    issue_id => $issue->issue_id,
1987
                });
2000
                });
Lines 3337-3346 sub CanBookBeRenewed { Link Here
3337
        }
3350
        }
3338
    }
3351
    }
3339
3352
3340
    # CHECK FOR QUOTAS
3353
    # # CHECK FOR QUOTAS
3341
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
3354
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3342
        unless ( $quota->has_available_quota ) {
3355
3343
            return ( 0, "QUOTA_EXCEEDED" );
3356
        # Get current active quota for the patron
3357
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3358
3359
            if (!$active_quota->has_available_quota) {
3360
                return (0, "QUOTA_EXCEEDED");
3361
            }
3344
        }
3362
        }
3345
    }
3363
    }
3346
3364
Lines 3457-3468 sub AddRenewal { Link Here
3457
    my $circ_library = Koha::Libraries->find( _GetCircControlBranch( $item_object, $patron ) );
3475
    my $circ_library = Koha::Libraries->find( _GetCircControlBranch( $item_object, $patron ) );
3458
3476
3459
    # Check quotas and record usage if needed
3477
    # Check quotas and record usage if needed
3460
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
3478
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3461
        # Update patron's used quota value
3479
        # Get current active quota for the patron
3462
        $quota->add_usage({
3480
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3463
            patron_id => $patron->patron_id,
3481
            # Update patron's used quota value
3464
            issue_id => $issue->issue_id,
3482
            $active_quota->add_usage({
3465
        });
3483
                patron_id => $patron->patron_id,
3484
                issue_id  => $issue->issue_id,
3485
            });
3486
        }
3466
    }
3487
    }
3467
3488
3468
    my $schema = Koha::Database->schema;
3489
    my $schema = Koha::Database->schema;
(-)a/Koha/Patron/Quotas.pm (-23 / +55 lines)
Lines 1-9 Link Here
1
package Koha::Patron::Quotas;
1
package Koha::Patron::Quotas;
2
2
3
use base qw(Koha::Objects);
3
use Modern::Perl;
4
use Modern::Perl;
4
use Koha::Patron::Quota;
5
use Koha::Patron::Quota;
5
use Koha::Patrons;
6
use Koha::Patrons;
6
use base qw(Koha::Objects);
7
7
8
=head1 NAME
8
=head1 NAME
9
9
Lines 25-71 Searches for any applicable quota for the given patron. Returns: Link Here
25
=cut
25
=cut
26
26
27
sub get_patron_quota {
27
sub get_patron_quota {
28
    my ($self, $patron_id) = @_;
28
    my ( $self, $patron_id ) = @_;
29
29
30
    my $patron_quota = undef;
30
    my @available_quotas;
31
    my $first_found_quota = undef;
31
    my $first_found_quota = undef;
32
32
33
    # First check all guarantor quotas if enabled
33
    # First check patron's own quota
34
    if (C4::Context->preference('UseGuarantorQuota')) {
34
    my $patron_quota = $self->search(
35
        my $patron = Koha::Patrons->find($patron_id);
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);
36
        my @guarantors = $patron->guarantor_relationships->guarantors->as_list;
50
        my @guarantors = $patron->guarantor_relationships->guarantors->as_list;
37
51
38
        foreach my $guarantor (@guarantors) {
52
        foreach my $guarantor (@guarantors) {
39
            my $guarantor_quota = $self->search(
53
            my $guarantor_quota = $self->search(
40
                {
54
                {
41
                    patron_id => $guarantor->borrowernumber,
55
                    patron_id  => $guarantor->borrowernumber,
42
                    start_date => { '<=' => \'CURRENT_DATE' },
56
                    start_date => { '<=' => \'CURRENT_DATE' },
43
                    end_date   => { '>=' => \'CURRENT_DATE' }
57
                    end_date   => { '>=' => \'CURRENT_DATE' }
44
                }
58
                }
45
            )->single;
59
            )->single;
46
60
47
            if ($guarantor_quota) {
61
            if ($guarantor_quota) {
48
                # Return immediately if guarantor quota has availability
62
49
                if ($guarantor_quota->has_available_quota) {
50
                    return $guarantor_quota;
51
                }
52
                # Store first found quota in case we need it later
63
                # Store first found quota in case we need it later
53
                $first_found_quota ||= $guarantor_quota;
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
                }
54
            }
70
            }
55
        }
71
        }
56
    }
72
    }
57
73
58
    # Check patron's own quota if no available guarantor quota was found
74
    # Return single quota if only one available, array of quotas if multiple available,
59
    $patron_quota = $self->search(
75
    # first found quota if none available
60
        {
76
    return $available_quotas[0] if @available_quotas == 1;
61
            patron_id => $patron_id,
77
    return \@available_quotas   if @available_quotas > 1;
62
            start_date => { '<=' => \'CURRENT_DATE' },
78
    return $first_found_quota;
63
            end_date   => { '>=' => \'CURRENT_DATE' }
64
        }
65
    )->single;
66
67
    # Return patron quota if exists, otherwise first found guarantor quota
68
    return $patron_quota || $first_found_quota;
69
}
79
}
70
80
71
=head3 create_quota
81
=head3 create_quota
Lines 107-112 sub get_active_quotas { Link Here
107
    );
117
    );
108
}
118
}
109
119
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
110
=head2 Internal methods
142
=head2 Internal methods
111
143
112
=head3 _type
144
=head3 _type
Lines 119-125 sub _type { Link Here
119
151
120
=head3 object_class
152
=head3 object_class
121
153
122
Returns the package name for patron quota objects
154
Returns the package name for koha patron quota objects
123
155
124
=cut
156
=cut
125
157
(-)a/circ/circulation.pl (-1 / +2 lines)
Lines 186-191 if ( $restoreduedatespec && $restoreduedatespec eq "highholds_empty" ) { Link Here
186
my $issueconfirmed = $query->param('issueconfirmed');
186
my $issueconfirmed = $query->param('issueconfirmed');
187
my $cancelreserve  = $query->param('cancelreserve');
187
my $cancelreserve  = $query->param('cancelreserve');
188
my $cancel_recall  = $query->param('cancel_recall');
188
my $cancel_recall  = $query->param('cancel_recall');
189
my $selected_quota_id = $query->param('selected_quota_id');
189
my $recall_id      = $query->param('recall_id');
190
my $recall_id      = $query->param('recall_id');
190
my $debt_confirmed = $query->param('debt_confirmed') || 0;     # Don't show the debt error dialog twice
191
my $debt_confirmed = $query->param('debt_confirmed') || 0;     # Don't show the debt error dialog twice
191
my $charges        = $query->param('charges')        || q{};
192
my $charges        = $query->param('charges')        || q{};
Lines 547-553 if ( @$barcodes && $op eq 'cud-checkout' ) { Link Here
547
                    {
548
                    {
548
                        onsite_checkout        => $onsite_checkout,        auto_renew => $session->param('auto_renew'),
549
                        onsite_checkout        => $onsite_checkout,        auto_renew => $session->param('auto_renew'),
549
                        switch_onsite_checkout => $switch_onsite_checkout, cancel_recall => $cancel_recall,
550
                        switch_onsite_checkout => $switch_onsite_checkout, cancel_recall => $cancel_recall,
550
                        recall_id              => $recall_id,
551
                        recall_id              => $recall_id,              selected_quota_id => $selected_quota_id,
551
                    }
552
                    }
552
                );
553
                );
553
                $template_params->{issue} = $issue;
554
                $template_params->{issue} = $issue;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt (-3 / +21 lines)
Lines 158-164 Link Here
158
158
159
                [%IF ( QUOTA_EXCEEDED ) %]
159
                [%IF ( QUOTA_EXCEEDED ) %]
160
                    <li class="needsconfirm age_restriction">
160
                    <li class="needsconfirm age_restriction">
161
                        Quota Exceeded [% QUOTA_EXCEEDED | html %].
161
                        Quota Exceeded. Available: [% QUOTA_EXCEEDED.available | html %] / [% QUOTA_EXCEEDED.total | html %].
162
                        [% IF CAN_user_circulate_force_checkout %]
162
                        [% IF CAN_user_circulate_force_checkout %]
163
                            Check out anyway?
163
                            Check out anyway?
164
                        [% END %]
164
                        [% END %]
Lines 402-407 Link Here
402
                            [% END %]
402
                            [% END %]
403
                        [% END %]
403
                        [% END %]
404
404
405
                        [% IF ( QUOTA_SELECT ) %]
406
                            <p>
407
                                <label for="selected_quota_id">Select quota to use:</label>
408
                                <select name="selected_quota_id" id="selected_quota_id" required>
409
                                    <option value="">Select a quota</option>
410
                                    [% FOREACH quota IN QUOTA_SELECT %]
411
                                        <option value="[% quota.id | html %]">
412
                                            [% IF quota.patron_id != patron.borrowernumber %]
413
                                                Guarantor ([% INCLUDE 'patron-title.inc' patron = quota.patron %]) -
414
                                            [% ELSE %]
415
                                                This patron ([% INCLUDE 'patron-title.inc' patron = quota.patron %]) -
416
                                            [% END %]
417
                                            Available: [% quota.available_quota | html %] / [% quota.allocation | html %]
418
                                        </option>
419
                                    [% END %]
420
                                </select>
421
                            </p>
422
                        [% END %]
423
405
                        <input type="hidden" name="barcode" value="[% barcode | html %]" />
424
                        <input type="hidden" name="barcode" value="[% barcode | html %]" />
406
                        <input type="hidden" name="borrowernumber" value="[% patron.borrowernumber | html %]" />
425
                        <input type="hidden" name="borrowernumber" value="[% patron.borrowernumber | html %]" />
407
                        <input type="hidden" name="issueconfirmed" value="1" />
426
                        <input type="hidden" name="issueconfirmed" value="1" />
Lines 663-669 Link Here
663
                [% END %]
682
                [% END %]
664
683
665
                [%IF ( QUOTA_EXCEEDED ) %]
684
                [%IF ( QUOTA_EXCEEDED ) %]
666
                    <li>Quota Exceeded [% QUOTA_EXCEEDED | html %].</li>
685
                    <li>Quota Exceeded. Available: [% QUOTA_EXCEEDED.available | html %] / [% QUOTA_EXCEEDED.total | html %].</li>
667
                [% END %]
686
                [% END %]
668
687
669
                [% IF ( EXPIRED ) %]
688
                [% IF ( EXPIRED ) %]
670
- 

Return to bug 38924