|
Line 0
Link Here
|
|
|
1 |
package Koha::Patron::Quota; |
| 2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use Koha::DateUtils qw( dt_from_string ); |
| 5 |
use Koha::Exceptions::Quota; |
| 6 |
use base qw(Koha::Object); |
| 7 |
|
| 8 |
=head1 NAME |
| 9 |
|
| 10 |
Koha::Patron::Quota - Koha Patron::Quota Object class |
| 11 |
|
| 12 |
=head1 API |
| 13 |
|
| 14 |
=head2 Class Methods |
| 15 |
|
| 16 |
=head3 store |
| 17 |
|
| 18 |
Overloaded store method to prevent creation of overlapping quota periods for a user |
| 19 |
|
| 20 |
=cut |
| 21 |
|
| 22 |
sub store { |
| 23 |
my ($self) = @_; |
| 24 |
|
| 25 |
# Parse dates into DateTime objects first |
| 26 |
my $start_dt = dt_from_string( $self->start_date ); |
| 27 |
my $end_dt = dt_from_string( $self->end_date ); |
| 28 |
|
| 29 |
# Throw exception for quota period clash |
| 30 |
my $dtf = Koha::Database->new->schema->storage->datetime_parser; |
| 31 |
my $existing_quota = Koha::Patron::Quotas->search( |
| 32 |
{ |
| 33 |
'-and' => [ |
| 34 |
{ |
| 35 |
'-or' => [ |
| 36 |
start_date => { |
| 37 |
'-between' => [ |
| 38 |
$dtf->format_datetime($start_dt), |
| 39 |
$dtf->format_datetime($end_dt) |
| 40 |
] |
| 41 |
}, |
| 42 |
end_date => { |
| 43 |
'-between' => [ |
| 44 |
$dtf->format_datetime($start_dt), |
| 45 |
$dtf->format_datetime($end_dt) |
| 46 |
] |
| 47 |
}, |
| 48 |
{ |
| 49 |
start_date => { '<' => $dtf->format_datetime($start_dt) }, |
| 50 |
end_date => { '>' => $dtf->format_datetime($end_dt) } |
| 51 |
} |
| 52 |
] |
| 53 |
}, |
| 54 |
{ |
| 55 |
patron_id => $self->patron_id, |
| 56 |
( |
| 57 |
$self->in_storage |
| 58 |
? ( id => { '!=' => $self->id } ) |
| 59 |
: () |
| 60 |
), |
| 61 |
} |
| 62 |
] |
| 63 |
} |
| 64 |
); |
| 65 |
Koha::Exceptions::Quota::Clash->throw() |
| 66 |
if $existing_quota->count; |
| 67 |
|
| 68 |
return $self->SUPER::store(); |
| 69 |
} |
| 70 |
|
| 71 |
=head3 add_to_quota |
| 72 |
|
| 73 |
Adds specified amount to the quota_used value |
| 74 |
|
| 75 |
=cut |
| 76 |
|
| 77 |
sub add_to_quota { |
| 78 |
my ( $self, $amount ) = @_; |
| 79 |
|
| 80 |
$amount //= 1; |
| 81 |
my $new_used = $self->used + $amount; |
| 82 |
$self->used($new_used); |
| 83 |
return $self->store; |
| 84 |
} |
| 85 |
|
| 86 |
=head3 has_available_quota |
| 87 |
|
| 88 |
Returns boolean indicating if there is quota available |
| 89 |
|
| 90 |
=cut |
| 91 |
|
| 92 |
sub has_available_quota { |
| 93 |
my ($self) = @_; |
| 94 |
return $self->available_quota >= 1; |
| 95 |
} |
| 96 |
|
| 97 |
=head3 available_quota |
| 98 |
|
| 99 |
Returns the amount still available in the quota |
| 100 |
|
| 101 |
=cut |
| 102 |
|
| 103 |
sub available_quota { |
| 104 |
my ($self) = @_; |
| 105 |
return $self->allocation - $self->used; |
| 106 |
} |
| 107 |
|
| 108 |
=head2 Instance Methods |
| 109 |
|
| 110 |
=head3 patron |
| 111 |
|
| 112 |
Returns the Koha::Patron associated with this quota |
| 113 |
|
| 114 |
=cut |
| 115 |
|
| 116 |
sub patron { |
| 117 |
my ( $self ) = @_; |
| 118 |
return Koha::Patron->_new_from_dbic($self->_result->patron); |
| 119 |
} |
| 120 |
|
| 121 |
=head3 is_active |
| 122 |
|
| 123 |
Returns boolean indicating if the quota period includes today's date |
| 124 |
|
| 125 |
=cut |
| 126 |
|
| 127 |
sub is_active { |
| 128 |
my ($self) = @_; |
| 129 |
my $today = dt_from_string; |
| 130 |
|
| 131 |
my $start = DateTime->new( |
| 132 |
year => substr( $self->start_date, 0, 4 ), |
| 133 |
month => substr( $self->start_date, 5, 2 ), |
| 134 |
day => substr( $self->start_date, 8, 2 ) |
| 135 |
); |
| 136 |
|
| 137 |
my $end = DateTime->new( |
| 138 |
year => substr( $self->end_date, 0, 4 ), |
| 139 |
month => substr( $self->end_date, 5, 2 ), |
| 140 |
day => substr( $self->end_date, 8, 2 ) |
| 141 |
); |
| 142 |
|
| 143 |
return ( $start <= $today && $end >= $today ); |
| 144 |
} |
| 145 |
|
| 146 |
=head2 Internal methods |
| 147 |
|
| 148 |
=head3 _type |
| 149 |
|
| 150 |
=cut |
| 151 |
|
| 152 |
sub _type { |
| 153 |
return 'PatronQuota'; |
| 154 |
} |
| 155 |
|
| 156 |
1; |