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

(-)a/C4/Circulation.pm (-15 / +36 lines)
Lines 69-74 use Koha::Library::Hours; Link Here
69
use Koha::Patron::Quotas;
69
use Koha::Patron::Quotas;
70
use Koha::Patron::Quota;
70
use Koha::Patron::Quota;
71
use Koha::Patron::Quota::Usage;
71
use Koha::Patron::Quota::Usage;
72
use Koha::Patron::Quota::Usages;
72
73
73
use Carp qw( carp );
74
use Carp qw( carp );
74
use List::MoreUtils qw( any );
75
use List::MoreUtils qw( any );
Lines 1377-1387 sub CanBookBeIssued { Link Here
1377
1378
1378
    # CHECK FOR QUOTAS
1379
    # CHECK FOR QUOTAS
1379
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1380
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
1380
        unless ( $quota->has_available_quota ) {
1381
        if (ref($quota) eq 'ARRAY') {
1382
            # Multiple available quotas found - need confirmation from user
1383
            $needsconfirmation{QUOTA_SELECT} = $quota;
1384
        }
1385
        elsif (!$quota->has_available_quota) {
1381
            if ( C4::Context->preference("AllowQuotaOverride") ) {
1386
            if ( C4::Context->preference("AllowQuotaOverride") ) {
1382
                $needsconfirmation{QUOTA_EXCEEDED} = {
1387
                $needsconfirmation{QUOTA_EXCEEDED} = {
1383
                    available => $quota->available_quota,
1388
                    available => $quota->available_quota,
1384
                    total => $quota->allocation,
1389
                    total => $quota->allocation, 
1385
                    used => $quota->used,
1390
                    used => $quota->used,
1386
                };
1391
                };
1387
            }
1392
            }
Lines 1628-1633 sub AddIssue { Link Here
1628
    my $recall_id              = $params && $params->{recall_id};
1633
    my $recall_id              = $params && $params->{recall_id};
1629
    my $confirmations          = $params->{confirmations};
1634
    my $confirmations          = $params->{confirmations};
1630
    my $forced                 = $params->{forced};
1635
    my $forced                 = $params->{forced};
1636
    my $selected_quota_id      = $params && $params->{selected_quota_id};
1631
    my $dbh                    = C4::Context->dbh;
1637
    my $dbh                    = C4::Context->dbh;
1632
    my $barcodecheck           = CheckValidBarcode($barcode);
1638
    my $barcodecheck           = CheckValidBarcode($barcode);
1633
1639
Lines 2013-2021 sub AddIssue { Link Here
2013
                if C4::Context->preference('RealTimeHoldsQueue');
2019
                if C4::Context->preference('RealTimeHoldsQueue');
2014
2020
2015
            # Check quotas and record usage if needed
2021
            # Check quotas and record usage if needed
2016
            if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
2022
            my $quota;
2017
                # Update patron's used quota value
2023
            if ($selected_quota_id) {
2018
                $quota->add_usage({
2024
                $quota = Koha::Patron::Quotas->find($selected_quota_id);
2025
            } else {
2026
                $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber); 
2027
            }
2028
2029
            if ($quota) {
2030
              # Update patron's used quota value
2031
              $quota->add_usage({
2019
                    patron_id => $patron->patron_id, 
2032
                    patron_id => $patron->patron_id, 
2020
                    issue_id => $issue->issue_id,
2033
                    issue_id => $issue->issue_id,
2021
                });
2034
                });
Lines 3374-3383 sub CanBookBeRenewed { Link Here
3374
        }
3387
        }
3375
    }
3388
    }
3376
3389
3377
    # CHECK FOR QUOTAS  
3390
    # # CHECK FOR QUOTAS  
3378
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
3391
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3379
        unless ( $quota->has_available_quota ) {
3392
        
3380
            return ( 0, "QUOTA_EXCEEDED" );
3393
        # Get current active quota for the patron
3394
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3395
            
3396
            if (!$active_quota->has_available_quota) {
3397
                return (0, "QUOTA_EXCEEDED");
3398
            }
3381
        }
3399
        }
3382
    }
3400
    }
3383
3401
Lines 3496-3507 sub AddRenewal { Link Here
3496
    my $circ_library = Koha::Libraries->find( _GetCircControlBranch( $item_object, $patron ) );
3514
    my $circ_library = Koha::Libraries->find( _GetCircControlBranch( $item_object, $patron ) );
3497
3515
3498
    # Check quotas and record usage if needed
3516
    # Check quotas and record usage if needed
3499
    if ( my $quota = Koha::Patron::Quotas->get_patron_quota($patron->borrowernumber) ) {
3517
    if (my $quota_usage = Koha::Patron::Quota::Usages->find({ issue_id => $issue->issue_id })) {
3500
        # Update patron's used quota value
3518
        # Get current active quota for the patron
3501
        $quota->add_usage({
3519
        if (my $active_quota = Koha::Patron::Quotas->get_active_quota($quota_usage->patron_id)) {
3502
            patron_id => $patron->patron_id, 
3520
            # Update patron's used quota value
3503
            issue_id => $issue->issue_id,
3521
            $active_quota->add_usage({
3504
        });
3522
                patron_id => $patron->patron_id,
3523
                issue_id  => $issue->issue_id,
3524
            });
3525
        }
3505
    }
3526
    }
3506
3527
3507
    my $schema = Koha::Database->schema;
3528
    my $schema = Koha::Database->schema;
(-)a/Koha/Patron/Quotas.pm (-24 / +56 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 / +3 lines)
Lines 187-192 if ( $restoreduedatespec && $restoreduedatespec eq "highholds_empty" ) { Link Here
187
my $issueconfirmed = $query->param('issueconfirmed');
187
my $issueconfirmed = $query->param('issueconfirmed');
188
my $cancelreserve  = $query->param('cancelreserve');
188
my $cancelreserve  = $query->param('cancelreserve');
189
my $cancel_recall  = $query->param('cancel_recall');
189
my $cancel_recall  = $query->param('cancel_recall');
190
my $selected_quota_id = $query->param('selected_quota_id');
190
my $recall_id      = $query->param('recall_id');
191
my $recall_id      = $query->param('recall_id');
191
my $debt_confirmed = $query->param('debt_confirmed') || 0;     # Don't show the debt error dialog twice
192
my $debt_confirmed = $query->param('debt_confirmed') || 0;     # Don't show the debt error dialog twice
192
my $charges        = $query->param('charges')        || q{};
193
my $charges        = $query->param('charges')        || q{};
Lines 559-565 if ( @$barcodes && $op eq 'cud-checkout' ) { Link Here
559
                        cancel_recall          => $cancel_recall,
560
                        cancel_recall          => $cancel_recall,
560
                        recall_id              => $recall_id,
561
                        recall_id              => $recall_id,
561
                        confirmations          => [ grep { /^[A-Z_]+$/ } keys %{$needsconfirmation} ],
562
                        confirmations          => [ grep { /^[A-Z_]+$/ } keys %{$needsconfirmation} ],
562
                        forced                 => [ keys %{$issuingimpossible} ]
563
                        forced                 => [ keys %{$issuingimpossible} ],
564
                        selected_quota_id      => $selected_quota_id
563
                    }
565
                    }
564
                );
566
                );
565
                $template_params->{issue} = $issue;
567
                $template_params->{issue} = $issue;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt (-3 / +21 lines)
Lines 157-163 Link Here
157
157
158
                [%IF ( QUOTA_EXCEEDED ) %]
158
                [%IF ( QUOTA_EXCEEDED ) %]
159
                    <li class="needsconfirm age_restriction">
159
                    <li class="needsconfirm age_restriction">
160
                        Quota Exceeded [% QUOTA_EXCEEDED | html %].
160
                        Quota Exceeded. Available: [% QUOTA_EXCEEDED.available | html %] / [% QUOTA_EXCEEDED.total | html %].
161
                        [% IF CAN_user_circulate_force_checkout %]
161
                        [% IF CAN_user_circulate_force_checkout %]
162
                            Check out anyway?
162
                            Check out anyway?
163
                        [% END %]
163
                        [% END %]
Lines 401-406 Link Here
401
                            [% END %]
401
                            [% END %]
402
                        [% END %]
402
                        [% END %]
403
403
404
                        [% IF ( QUOTA_SELECT ) %]
405
                            <p>
406
                                <label for="selected_quota_id">Select quota to use:</label>
407
                                <select name="selected_quota_id" id="selected_quota_id" required>
408
                                    <option value="">Select a quota</option>
409
                                    [% FOREACH quota IN QUOTA_SELECT %]
410
                                        <option value="[% quota.id | html %]">
411
                                            [% IF quota.patron_id != patron.borrowernumber %]
412
                                                Guarantor ([% INCLUDE 'patron-title.inc' patron = quota.patron %]) -
413
                                            [% ELSE %]
414
                                                This patron ([% INCLUDE 'patron-title.inc' patron = quota.patron %]) -
415
                                            [% END %]
416
                                            Available: [% quota.available_quota | html %] / [% quota.allocation | html %]
417
                                        </option>
418
                                    [% END %]
419
                                </select>
420
                            </p>
421
                        [% END %]
422
404
                        <input type="hidden" name="barcode" value="[% barcode | html %]" />
423
                        <input type="hidden" name="barcode" value="[% barcode | html %]" />
405
                        <input type="hidden" name="borrowernumber" value="[% patron.borrowernumber | html %]" />
424
                        <input type="hidden" name="borrowernumber" value="[% patron.borrowernumber | html %]" />
406
                        <input type="hidden" name="issueconfirmed" value="1" />
425
                        <input type="hidden" name="issueconfirmed" value="1" />
Lines 662-668 Link Here
662
                [% END %]
681
                [% END %]
663
682
664
                [%IF ( QUOTA_EXCEEDED ) %]
683
                [%IF ( QUOTA_EXCEEDED ) %]
665
                    <li>Quota Exceeded [% QUOTA_EXCEEDED | html %].</li>
684
                    <li>Quota Exceeded. Available: [% QUOTA_EXCEEDED.available | html %] / [% QUOTA_EXCEEDED.total | html %].</li>
666
                [% END %]
685
                [% END %]
667
686
668
                [% IF ( EXPIRED ) %]
687
                [% IF ( EXPIRED ) %]
669
- 

Return to bug 38924