Bug 22521: DBRev 18.12.00.055
[koha.git] / Koha / List / Patron.pm
1 package Koha::List::Patron;
2
3 # Copyright 2013 ByWater Solutions
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 Koha::List::Patron - Management of lists of patrons
23
24 =head1 FUNCTIONS
25
26 =cut
27
28 use Modern::Perl;
29
30 use Carp;
31
32 use Koha::Database;
33
34 use base 'Exporter';
35 our @EXPORT = (
36     qw(
37       GetPatronLists
38
39       DelPatronList
40       AddPatronList
41       ModPatronList
42
43       AddPatronsToList
44       DelPatronsFromList
45       )
46 );
47
48 =head2 GetPatronLists
49
50     my @lists = GetPatronLists( $params );
51
52     Returns an array of lists created by the the given user
53     or the logged in user if none is passed in.
54 =cut
55
56 sub GetPatronLists {
57     my ($params) = @_;
58
59     $params->{owner} ||= C4::Context->userenv->{'number'};
60
61     unless ( $params->{owner} ) {
62         carp("No owner passed in or defined!");
63         return;
64     }
65
66     delete $params->{owner} if C4::Context->IsSuperLibrarian();
67
68     if ( my $owner = $params->{owner} ) {
69         delete $params->{owner};
70         $params->{'-or'} = [
71             owner => $owner,
72             shared => 1,
73         ];
74     }
75
76     my $schema = Koha::Database->new()->schema();
77
78     my @patron_lists = $schema->resultset('PatronList')->search($params);
79
80     return wantarray() ? @patron_lists : \@patron_lists;
81 }
82
83 =head2 DelPatronList
84
85     DelPatronList( { patron_list_id => $list_id [, owner => $owner ] } );
86
87 =cut
88
89 sub DelPatronList {
90     my ($params) = @_;
91
92     $params->{owner} ||= C4::Context->userenv->{'number'};
93
94     unless ( $params->{patron_list_id} ) {
95         croak("No patron list id passed in!");
96     }
97     unless ( $params->{owner} ) {
98         carp("No owner passed in or defined!");
99         return;
100     }
101
102     delete( $params->{owner} ) if ( C4::Context->IsSuperLibrarian() );
103
104     return Koha::Database->new()->schema()->resultset('PatronList')
105       ->search($params)->single()->delete();
106 }
107
108 =head2 AddPatronList
109
110     AddPatronList( { name => $name [, owner => $owner ] } );
111
112 =cut
113
114 sub AddPatronList {
115     my ($params) = @_;
116
117     $params->{owner} ||= C4::Context->userenv->{'number'};
118
119     unless ( $params->{owner} ) {
120         carp("No owner passed in or defined!");
121         return;
122     }
123
124     unless ( $params->{name} ) {
125         carp("No list name passed in!");
126         return;
127     }
128
129     return Koha::Database->new()->schema()->resultset('PatronList')
130       ->create($params);
131 }
132
133 =head2 ModPatronList
134
135     ModPatronList( { patron_list_id => $id, name => $name [, owner => $owner ] } );
136
137 =cut
138
139 sub ModPatronList {
140     my ($params) = @_;
141
142     unless ( $params->{patron_list_id} ) {
143         carp("No patron list id passed in!");
144         return;
145     }
146
147     my ($list) = GetPatronLists(
148         {
149             patron_list_id => $params->{patron_list_id},
150             owner          => $params->{owner}
151         }
152     );
153
154     return $list->update($params);
155 }
156
157 =head2 AddPatronsToList
158
159     AddPatronsToList({ list => $list, cardnumbers => \@cardnumbers });
160
161 =cut
162
163 sub AddPatronsToList {
164     my ($params) = @_;
165
166     my $list            = $params->{list};
167     my $cardnumbers     = $params->{'cardnumbers'};
168     my $borrowernumbers = $params->{'borrowernumbers'};
169
170     return unless ( $list && ( $cardnumbers || $borrowernumbers ) );
171
172     my @borrowernumbers;
173
174     if ($cardnumbers) {
175         @borrowernumbers =
176           Koha::Database->new()->schema()->resultset('Borrower')->search(
177             { cardnumber => { 'IN' => $cardnumbers } },
178             { columns    => [qw/ borrowernumber /] }
179           )->get_column('borrowernumber')->all();
180     }
181     else {
182         @borrowernumbers = @$borrowernumbers;
183     }
184
185     my $patron_list_id = $list->patron_list_id();
186
187     my $plp_rs = Koha::Database->new()->schema()->resultset('PatronListPatron');
188
189     my @results;
190     foreach my $borrowernumber (@borrowernumbers) {
191         my $result = $plp_rs->update_or_create(
192             {
193                 patron_list_id => $patron_list_id,
194                 borrowernumber => $borrowernumber
195             }
196         );
197         push( @results, $result );
198     }
199
200     return wantarray() ? @results : \@results;
201 }
202
203 =head2 DelPatronsFromList
204
205     DelPatronsFromList({ list => $list, patron_list_patrons => \@patron_list_patron_ids });
206
207 =cut
208
209 sub DelPatronsFromList {
210     my ($params) = @_;
211
212     my $list                = $params->{list};
213     my $patron_list_patrons = $params->{patron_list_patrons};
214
215     return unless ( $list && $patron_list_patrons );
216
217     return Koha::Database->new()->schema()->resultset('PatronListPatron')
218       ->search( { patron_list_patron_id => { 'IN' => $patron_list_patrons } } )
219       ->delete();
220 }
221
222 =head1 AUTHOR
223
224 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
225
226 =cut
227
228 1;
229
230 __END__