From 357be6749f082363248515e896281553e9c14154 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 15 Sep 2025 13:26:46 +0100 Subject: [PATCH] Bug 40808: Add schema relationships for polymorphic account line linking Adds the necessary DBIx::Class relationships to support polymorphic linking between account lines and various entities: 1. Koha::Schema::Result::Reserve - Adds accountline_links relationship to enable holds to find their linked account lines 2. Koha::Schema::Result::AccountlineLink - Enhanced polymorphic methods for accessing linked objects and validation These relationships enable proper traversal from holds to account lines and support the polymorphic linking system. --- Koha/Schema/Result/AccountlineLink.pm | 50 +++++++++++++-------------- Koha/Schema/Result/Reserve.pm | 13 +++++++ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/Koha/Schema/Result/AccountlineLink.pm b/Koha/Schema/Result/AccountlineLink.pm index bc18dbca6f8..2d57e8fa4d6 100644 --- a/Koha/Schema/Result/AccountlineLink.pm +++ b/Koha/Schema/Result/AccountlineLink.pm @@ -43,7 +43,7 @@ foreign key to accountlines.accountlines_id is_nullable: 0 size: 32 -Type of linked entity: hold, old_hold, checkout, old_checkout, article_request, etc. +Type of linked entity: hold, checkout, article_request, etc. =head2 linked_id @@ -143,9 +143,22 @@ This is a polymorphic relationship. sub linked_object { my ($self) = @_; + # Special handling for holds - they can be in reserves OR old_reserves + if ($self->link_type eq 'hold') { + # Try current holds first + my $hold = $self->result_source->schema->resultset('Reserve')->find({ + reserve_id => $self->linked_id + }); + return $hold if $hold; + + # If not found in current holds, try old holds + return $self->result_source->schema->resultset('OldReserve')->find({ + reserve_id => $self->linked_id + }); + } + + # Handle other link types normally my $type_map = { - 'hold' => ['Reserve', 'reserve_id'], - 'old_hold' => ['OldReserve', 'reserve_id'], 'checkout' => ['Issue', 'issue_id'], 'old_checkout' => ['OldIssue', 'issue_id'], 'article_request' => ['ArticleRequest', 'id'], @@ -159,38 +172,25 @@ sub linked_object { }); } -=head2 linked_object_class +=head2 koha_objects_class -Returns the Koha::Object class name for the linked object +Missing POD for koha_objects_class. =cut -sub linked_object_class { - my ($self) = @_; - - my $class_map = { - 'hold' => 'Koha::Hold', - 'old_hold' => 'Koha::Old::Hold', - 'checkout' => 'Koha::Checkout', - 'old_checkout' => 'Koha::Old::Checkout', - 'article_request' => 'Koha::ArticleRequest', - }; - - return $class_map->{$self->link_type}; +sub koha_objects_class { + 'Koha::Account::Links'; } -=head2 validate_link_integrity +=head2 koha_object_class -Validates that the linked entity actually exists +Missing POD for koha_object_class. =cut -sub validate_link_integrity { - my ($self) = @_; - - my $linked_object = $self->linked_object; - return defined($linked_object) ? 1 : 0; +sub koha_object_class { + 'Koha::Account::Link'; } # You can replace this text with custom code or comments, and it will be preserved on regeneration -1; \ No newline at end of file +1; diff --git a/Koha/Schema/Result/Reserve.pm b/Koha/Schema/Result/Reserve.pm index 3e65b00f67c..ac23c2cb20e 100644 --- a/Koha/Schema/Result/Reserve.pm +++ b/Koha/Schema/Result/Reserve.pm @@ -588,4 +588,17 @@ __PACKAGE__->belongs_to( { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, ); +__PACKAGE__->has_many( + "accountline_links", + "Koha::Schema::Result::AccountlineLink", + sub { + my ($args) = @_; + return { + "$args->{foreign_alias}.link_type" => 'hold', + "$args->{foreign_alias}.linked_id" => { '=' => { -ident => "$args->{self_alias}.reserve_id" } }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + 1; -- 2.51.0