View | Details | Raw Unified | Return to bug 39658
Collapse All | Expand All

(-)a/Koha/Patron/AccountLink.pm (+135 lines)
Line 0 Link Here
1
package Koha::Patron::AccountLink;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Database;
21
use Koha::Patron::AccountLinks;
22
use Koha::Patrons;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::Patron::AccountLink - Represents a link between patron accounts
29
30
=head1 DESCRIPTION
31
32
Patrons in Koha may have multiple accounts at different libraries within a
33
consortium. This class models those links and provides access to linked accounts.
34
35
=head1 API
36
37
=head2 Class methods
38
39
=head3 patron
40
41
Returns the Koha::Patron object for this link
42
43
=cut
44
45
sub patron {
46
    my ($self) = @_;
47
    return Koha::Patrons->find( $self->borrowernumber );
48
}
49
50
=head3 linked_patrons
51
52
Returns Koha::Patrons of all OTHER patrons in this link group (excluding self)
53
54
=cut
55
56
sub linked_patrons {
57
    my ($self) = @_;
58
59
    my @borrowernumbers = Koha::Patron::AccountLinks->search(
60
        {
61
            link_group_id  => $self->link_group_id,
62
            borrowernumber => { '!=' => $self->borrowernumber }
63
        }
64
    )->get_column('borrowernumber');
65
66
    return Koha::Patrons->search( { borrowernumber => \@borrowernumbers } );
67
}
68
69
=head3 all_linked_borrowernumbers
70
71
Returns arrayref of all borrowernumbers in link group (including self)
72
73
=cut
74
75
sub all_linked_borrowernumbers {
76
    my ($self) = @_;
77
78
    my @borrowernumbers =
79
        Koha::Patron::AccountLinks->search( { link_group_id => $self->link_group_id } )->get_column('borrowernumber');
80
81
    return \@borrowernumbers;
82
}
83
84
=head3 delete
85
86
Override delete to clean up orphaned links.
87
88
When a patron leaves a linked group, if only one patron remains,
89
that patron's link is also deleted (they would be an orphan).
90
91
=cut
92
93
sub delete {
94
    my ($self) = @_;
95
96
    my $link_group_id = $self->link_group_id;
97
98
    # Delete this link
99
    $self->SUPER::delete;
100
101
    # Check for orphans in the group
102
    my @remaining = Koha::Patron::AccountLinks->search( { link_group_id => $link_group_id } )->as_list;
103
104
    # If only one patron remains, delete their orphan link too
105
    if ( @remaining == 1 ) {
106
        $remaining[0]->SUPER::delete;
107
    }
108
109
    return $self;
110
}
111
112
=head3 to_api_mapping
113
114
=cut
115
116
sub to_api_mapping {
117
    return {
118
        id             => 'account_link_id',
119
        link_group_id  => 'link_group_id',
120
        borrowernumber => 'patron_id',
121
        created_on     => 'created_on',
122
    };
123
}
124
125
=head2 Internal methods
126
127
=head3 _type
128
129
=cut
130
131
sub _type {
132
    return 'PatronAccountLink';
133
}
134
135
1;
(-)a/Koha/Patron/AccountLinks.pm (-1 / +60 lines)
Line 0 Link Here
0
- 
1
package Koha::Patron::AccountLinks;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Patron::AccountLink;
21
22
use base qw(Koha::Objects);
23
24
=head1 NAME
25
26
Koha::Patron::AccountLinks - Koha Patron Account Link Object set class
27
28
=head1 API
29
30
=head2 Class Methods
31
32
=head3 get_next_group_id
33
34
Returns the next available link_group_id for creating new link groups
35
36
=cut
37
38
sub get_next_group_id {
39
    my ($class) = @_;
40
    my $max = $class->_resultset->get_column('link_group_id')->max || 0;
41
    return $max + 1;
42
}
43
44
=head3 _type
45
46
=cut
47
48
sub _type {
49
    return 'PatronAccountLink';
50
}
51
52
=head3 object_class
53
54
=cut
55
56
sub object_class {
57
    return 'Koha::Patron::AccountLink';
58
}
59
60
1;

Return to bug 39658