From e36005c7ecca7f5c548cb767206421a72cc521b9 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Thu, 30 Jan 2025 16:16:49 +0000 Subject: [PATCH] Bug 38924: Add Koha::Patron::Quota::Usage(s) classes Add ::Usage(s) classes for working with quota allocation usage. We overload store to ensure data consistency for the checkout_id field which is linked to either the issue or old_issue table appropriatly. We include a checkout relationship that handles the lack of foreign key due to issues/old_issues. We also add the Usage relations to Quota objects --- Koha/Patron/Quota.pm | 43 +++++++++++++++++++-- Koha/Patron/Quota/Usage.pm | 75 +++++++++++++++++++++++++++++++++++++ Koha/Patron/Quota/Usages.pm | 35 +++++++++++++++++ 3 files changed, 150 insertions(+), 3 deletions(-) create mode 100644 Koha/Patron/Quota/Usage.pm create mode 100644 Koha/Patron/Quota/Usages.pm 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 diff --git a/Koha/Patron/Quota/Usage.pm b/Koha/Patron/Quota/Usage.pm new file mode 100644 index 00000000000..7d3696fc7f7 --- /dev/null +++ b/Koha/Patron/Quota/Usage.pm @@ -0,0 +1,75 @@ +package Koha::Patron::Quota::Usage; + +use base qw(Koha::Object); +use Modern::Perl; +use Koha::Patron::Quota; +use Koha::Patron::Quota::Usages; + +=head1 NAME + +Koha::Patron::Quota::Usage - Koha Patron Quota Usage Object class + +=head1 API + +=head2 Class methods + +=cut + +=head3 patron + +Returns the patron this quota usage belongs to + +=cut + +sub patron { + my ($self) = @_; + my $rs = $self->_result->patron; + return Koha::Patron->_new_from_dbic($rs); +} + +=head3 quota + +Returns the quota this usage belongs to + +=cut + +sub quota { + my ($self) = @_; + my $rs = $self->_result->patron_quota; + return Koha::Patron::Quota->_new_from_dbic($rs); +} + +=head3 store + +Overloaded I method to set implement issue_id foreign key in code + +=cut + +sub store { + my ($self) = @_; + + # Check that we have a valid issue_id + unless ( !$self->issue_id + || Koha::Checkouts->find( $self->issue_id ) + || Koha::Old::Checkouts->find( $self->issue_id ) ) + { + Koha::Exceptions::Object::FKConstraint->throw( + error => 'Broken FK Contraint', + broken_fk => 'issue_id' + ); + } + + return $self->SUPER::store(); +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'PatronQuotaUsage'; +} + +1; diff --git a/Koha/Patron/Quota/Usages.pm b/Koha/Patron/Quota/Usages.pm new file mode 100644 index 00000000000..4fd82e26c67 --- /dev/null +++ b/Koha/Patron/Quota/Usages.pm @@ -0,0 +1,35 @@ +package Koha::Patron::Quota::Usages; + +use base qw(Koha::Objects); +use Modern::Perl; +use Koha::Patron::Quota::Usage; + +=head1 NAME + +Koha::Patron::Quota::Usages - Koha Patron Quota Usages Object set class + +=head1 API + +=head2 Class methods + +=cut + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'PatronQuotaUsage'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::Patron::Quota::Usage'; +} + +1; -- 2.48.1