Lines 1-17
Link Here
|
|
|
1 |
package Koha::Patron::Quotas; |
2 |
|
3 |
use Modern::Perl; |
4 |
use Koha::Patron::Quota; |
5 |
use Koha::Patrons; |
6 |
use base qw(Koha::Objects); |
7 |
|
8 |
=head1 NAME |
9 |
|
10 |
Koha::Patron::Quotas - Koha Patron Quota Object set class |
11 |
|
12 |
=head1 API |
13 |
|
14 |
=head2 Class Methods |
15 |
|
1 |
=head3 get_patron_quota |
16 |
=head3 get_patron_quota |
2 |
|
17 |
|
3 |
Returns the active quota for a given patron |
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 |
4 |
|
24 |
|
5 |
=cut |
25 |
=cut |
6 |
|
26 |
|
7 |
sub get_patron_quota { |
27 |
sub get_patron_quota { |
|
|
28 |
my ($self, $patron_id) = @_; |
29 |
|
30 |
my $patron_quota = undef; |
31 |
my $first_found_quota = undef; |
32 |
|
33 |
# First check all guarantor quotas if enabled |
34 |
if (C4::Context->preference('UseGuarantorQuota')) { |
35 |
my $patron = Koha::Patrons->find($patron_id); |
36 |
my @guarantors = $patron->guarantor_relationships->guarantors->as_list; |
37 |
|
38 |
foreach my $guarantor (@guarantors) { |
39 |
my $guarantor_quota = $self->search( |
40 |
{ |
41 |
patron_id => $guarantor->borrowernumber, |
42 |
start_date => { '<=' => \'CURRENT_DATE' }, |
43 |
end_date => { '>=' => \'CURRENT_DATE' } |
44 |
} |
45 |
)->single; |
46 |
|
47 |
if ($guarantor_quota) { |
48 |
# Return immediately if guarantor quota has availability |
49 |
if ($guarantor_quota->has_available_quota) { |
50 |
return $guarantor_quota; |
51 |
} |
52 |
# Store first found quota in case we need it later |
53 |
$first_found_quota ||= $guarantor_quota; |
54 |
} |
55 |
} |
56 |
} |
57 |
|
58 |
# Check patron's own quota if no available guarantor quota was found |
59 |
$patron_quota = $self->search( |
60 |
{ |
61 |
patron_id => $patron_id, |
62 |
start_date => { '<=' => \'CURRENT_DATE' }, |
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 |
} |
70 |
|
71 |
=head3 create_quota |
72 |
|
73 |
Creates a new quota record for a patron |
74 |
|
75 |
=cut |
76 |
|
77 |
sub create_quota { |
78 |
my ( $self, $params ) = @_; |
79 |
|
80 |
return Koha::Patron::Quota->new($params)->store; |
81 |
} |
82 |
|
83 |
=head3 search_by_patron |
84 |
|
85 |
Finds all quotas for a patron |
86 |
|
87 |
=cut |
88 |
|
89 |
sub search_by_patron { |
8 |
my ( $self, $patron_id ) = @_; |
90 |
my ( $self, $patron_id ) = @_; |
|
|
91 |
return $self->search( { patron_id => $patron_id } ); |
92 |
} |
9 |
|
93 |
|
|
|
94 |
=head3 get_active_quotas |
95 |
|
96 |
Returns multiple active quota records |
97 |
|
98 |
=cut |
99 |
|
100 |
sub get_active_quotas { |
101 |
my ($self) = @_; |
10 |
return $self->search( |
102 |
return $self->search( |
11 |
{ |
103 |
{ |
12 |
patron_id => $patron_id, |
|
|
13 |
start_date => { '<=' => \'CURRENT_DATE' }, |
104 |
start_date => { '<=' => \'CURRENT_DATE' }, |
14 |
end_date => { '>=' => \'CURRENT_DATE' }, |
105 |
end_date => { '>=' => \'CURRENT_DATE' } |
15 |
} |
106 |
} |
16 |
)->single; |
107 |
); |
|
|
108 |
} |
109 |
|
110 |
=head2 Internal methods |
111 |
|
112 |
=head3 _type |
113 |
|
114 |
=cut |
115 |
|
116 |
sub _type { |
117 |
return 'PatronQuota'; |
118 |
} |
119 |
|
120 |
=head3 object_class |
121 |
|
122 |
Returns the package name for patron quota objects |
123 |
|
124 |
=cut |
125 |
|
126 |
sub object_class { |
127 |
return 'Koha::Patron::Quota'; |
17 |
} |
128 |
} |
18 |
- |
129 |
|
|
|
130 |
1; |