Bug 14570: Make it possible to add multiple guarantors to a record
[koha.git] / Koha / Patron.pm
index d13d27d..ab49821 100644 (file)
@@ -27,8 +27,10 @@ use Text::Unaccent qw( unac_string );
 
 use C4::Context;
 use C4::Log;
+use Koha::Account;
 use Koha::AuthUtils;
 use Koha::Checkouts;
+use Koha::Club::Enrollments;
 use Koha::Database;
 use Koha::DateUtils;
 use Koha::Exceptions::Password;
@@ -39,12 +41,11 @@ use Koha::Patron::Categories;
 use Koha::Patron::HouseboundProfile;
 use Koha::Patron::HouseboundRole;
 use Koha::Patron::Images;
+use Koha::Patron::Relationships;
 use Koha::Patrons;
-use Koha::Virtualshelves;
-use Koha::Club::Enrollments;
-use Koha::Account;
 use Koha::Subscription::Routinglists;
 use Koha::Token;
+use Koha::Virtualshelves;
 
 use base qw(Koha::Object);
 
@@ -372,41 +373,67 @@ sub category {
     return Koha::Patron::Category->_new_from_dbic( $self->_result->categorycode );
 }
 
-=head3 guarantor
-
-Returns a Koha::Patron object for this patron's guarantor
+=head3 image
 
 =cut
 
-sub guarantor {
-    my ( $self ) = @_;
-
-    return unless $self->guarantorid();
-
-    return Koha::Patrons->find( $self->guarantorid() );
-}
-
 sub image {
     my ( $self ) = @_;
 
     return scalar Koha::Patron::Images->find( $self->borrowernumber );
 }
 
+=head3 library
+
+Returns a Koha::Library object representing the patron's home library.
+
+=cut
+
 sub library {
     my ( $self ) = @_;
     return Koha::Library->_new_from_dbic($self->_result->branchcode);
 }
 
-=head3 guarantees
+=head3 guarantor_relationships
+
+Returns Koha::Patron::Relationships object for this patron's guarantors
 
-Returns the guarantees (list of Koha::Patron) of this patron
+Returns the set of relationships for the patrons that are guarantors for this patron.
+
+This is returned instead of a Koha::Patron object because the guarantor
+may not exist as a patron in Koha. If this is true, the guarantors name
+exists in the Koha::Patron::Relationship object and will have no guarantor_id.
 
 =cut
 
-sub guarantees {
-    my ( $self ) = @_;
+sub guarantor_relationships {
+    my ($self) = @_;
 
-    return Koha::Patrons->search( { guarantorid => $self->borrowernumber }, { order_by => { -asc => ['surname','firstname'] } } );
+    return Koha::Patron::Relationships->search( { guarantee_id => $self->id } );
+}
+
+=head3 guarantee_relationships
+
+Returns Koha::Patron::Relationships object for this patron's guarantors
+
+Returns the set of relationships for the patrons that are guarantees for this patron.
+
+The method returns Koha::Patron::Relationship objects for the sake
+of consistency with the guantors method.
+A guarantee by definition must exist as a patron in Koha.
+
+=cut
+
+sub guarantee_relationships {
+    my ($self) = @_;
+
+    return Koha::Patron::Relationships->search(
+        { guarantor_id => $self->id },
+        {
+            prefetch => 'guarantee',
+            order_by => { -asc => [ 'guarantee.surname', 'guarantee.firstname' ] },
+        }
+    );
 }
 
 =head3 housebound_profile
@@ -444,23 +471,22 @@ Returns the siblings of this patron.
 =cut
 
 sub siblings {
-    my ( $self ) = @_;
+    my ($self) = @_;
 
-    my $guarantor = $self->guarantor;
+    my @guarantors = $self->guarantor_relationships()->guarantors();
 
-    return unless $guarantor;
+    return unless @guarantors;
 
-    return Koha::Patrons->search(
-        {
-            guarantorid => {
-                '!=' => undef,
-                '=' => $guarantor->id,
-            },
-            borrowernumber => {
-                '!=' => $self->borrowernumber,
-            }
-        }
-    );
+    my @siblings =
+      map { $_->guarantee_relationships()->guarantees() } @guarantors;
+
+    return unless @siblings;
+
+    my %seen;
+    @siblings =
+      grep { !$seen{ $_->id }++ && ( $_->id != $self->id ) } @siblings;
+
+    return wantarray ? @siblings : Koha::Patrons->search( { borrowernumber => { -in => [ map { $_->id } @siblings ] } } );
 }
 
 =head3 merge_with
@@ -1419,6 +1445,34 @@ sub _anonymize_column {
     $self->$col($val);
 }
 
+=head3 add_guarantor
+
+    my @relationships = $patron->add_guarantor(
+        {
+            borrowernumber => $borrowernumber,
+            relationships  => $relationship,
+        }
+    );
+
+    Adds a new guarantor to a patron.
+
+=cut
+
+sub add_guarantor {
+    my ( $self, $params ) = @_;
+
+    my $guarantor_id = $params->{guarantor_id};
+    my $relationship = $params->{relationship};
+
+    return Koha::Patron::Relationship->new(
+        {
+            guarantee_id => $self->id,
+            guarantor_id => $guarantor_id,
+            relationship => $relationship
+        }
+    )->store();
+}
+
 =head2 Internal methods
 
 =head3 _type