From 218763181701745af88258bd734ba0bf42bedf43 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Thu, 30 Jan 2025 18:23:09 +0000 Subject: [PATCH] Bug 38924: Add Usage relations to Quota objects --- Koha/Patron/Quota.pm | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/Koha/Patron/Quota.pm b/Koha/Patron/Quota.pm index 1f1da940425..8a49060ccf3 100644 --- a/Koha/Patron/Quota.pm +++ b/Koha/Patron/Quota.pm @@ -1,9 +1,10 @@ package Koha::Patron::Quota; +use base qw(Koha::Object); use Modern::Perl; use Koha::DateUtils qw( dt_from_string ); +use Koha::Patron::Quotas; use Koha::Exceptions::Quota; -use base qw(Koha::Object); =head1 NAME @@ -114,8 +115,8 @@ Returns the Koha::Patron associated with this quota =cut sub patron { - my ( $self ) = @_; - return Koha::Patron->_new_from_dbic($self->_result->patron); + my ($self) = @_; + return Koha::Patron->_new_from_dbic( $self->_result->patron ); } =head3 is_active @@ -143,6 +144,42 @@ sub is_active { return ( $start <= $today && $end >= $today ); } +=head3 usages + +Returns all usage records for this quota + +=cut + +sub usages { + my ($self) = @_; + my $usages_rs = $self->_result->patron_quota_usages; + return Koha::Patron::Quota::Usages->_new_from_dbic($usages_rs); +} + +=head3 add_usage + +Creates a new usage record for this quota with the specified parameters. +Returns the new Koha::Patron::Quota::Usage object. + +=cut + +sub add_usage { + my ( $self, $params ) = @_; + + # Set defaults + $params->{patron_id} = $self->patron_id; + $params->{patron_quota_id} = $self->id; + $params->{issue_id} = undef unless exists $params->{issue_id}; + + # Create usage record + my $usage = Koha::Patron::Quota::Usage->new($params)->store; + + # Update quota used amount + $self->add_to_quota( $params->{amount} ); + + return $usage; +} + =head2 Internal methods =head3 _type -- 2.39.5