From c76701be978cfccd4c61751711cd8544ee5dc296 Mon Sep 17 00:00:00 2001 From: Jacob O'Mara Date: Wed, 21 Jan 2026 15:36:17 +0000 Subject: [PATCH] Bug 39658: Add link_to_patron and linked_account_links methods - link_to_patron($other_patron): Links patrons, handles group logic - linked_account_links(): Returns all links in patron's group Signed-off-by: Trevor Diamond --- Koha/Patron.pm | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index cbfd466c6e3..d95b3628200 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -46,6 +46,7 @@ use Koha::Encryption; use Koha::Exceptions; use Koha::Exceptions::Password; use Koha::Exceptions::HoldGroup; +use Koha::Exceptions::PatronAccountLink; use Koha::Holds; use Koha::HoldGroups; use Koha::ILL::Requests; @@ -704,6 +705,77 @@ sub linked_accounts_debt { return $total; } +=head3 linked_account_links + + my $links = $patron->linked_account_links; + +Returns all AccountLinks in this patron's link group as a Koha::Patron::AccountLinks +resultset, or undef if patron is not linked. + +=cut + +sub linked_account_links { + my ($self) = @_; + + my $link = $self->account_link; + return unless $link; + + return Koha::Patron::AccountLinks->search( { link_group_id => $link->link_group_id } ); +} + +=head3 link_to_patron + + my $link = $patron->link_to_patron( $other_patron ); + +Links this patron to another patron's account. Handles group creation/joining. +Returns the patron's AccountLink object. + +Throws: + Koha::Exceptions::PatronAccountLink::AlreadyLinked + Koha::Exceptions::PatronAccountLink::DifferentGroups + +=cut + +sub link_to_patron { + my ( $self, $other_patron ) = @_; + + my $own_link = $self->account_link; + my $other_link = $other_patron->account_link; + + # Both already linked to same group + if ( $own_link && $other_link && $own_link->link_group_id == $other_link->link_group_id ) { + Koha::Exceptions::PatronAccountLink::AlreadyLinked->throw(); + } + + # Both belong to different groups + if ( $own_link && $other_link ) { + Koha::Exceptions::PatronAccountLink::DifferentGroups->throw(); + } + + # Determine group ID + my $link_group_id; + if ($own_link) { + $link_group_id = $own_link->link_group_id; + } elsif ($other_link) { + $link_group_id = $other_link->link_group_id; + } else { + $link_group_id = Koha::Patron::AccountLinks->get_next_group_id(); + } + + # Create missing links + unless ($own_link) { + Koha::Patron::AccountLink->new( { link_group_id => $link_group_id, borrowernumber => $self->borrowernumber } ) + ->store; + } + + unless ($other_link) { + Koha::Patron::AccountLink->new( + { link_group_id => $link_group_id, borrowernumber => $other_patron->borrowernumber } )->store; + } + + return $self->account_link; +} + =head3 housebound_profile Returns the HouseboundProfile associated with this patron. -- 2.39.5