Bug 35269: Rename `update_item_location` to `location_update_trigger`
[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 qw( carp croak );
31
32 use Koha::Database;
33
34 our (@ISA, @EXPORT_OK);
35 BEGIN {
36     require Exporter;
37     @ISA    = qw(Exporter);
38     @EXPORT_OK = qw(
39       GetPatronLists
40
41       DelPatronList
42       AddPatronList
43       ModPatronList
44
45       AddPatronsToList
46       DelPatronsFromList
47     );
48 }
49
50 =head2 GetPatronLists
51
52     my @lists = GetPatronLists( $params );
53
54     Returns an array of lists created by the the given user
55     or the logged in user if none is passed in.
56 =cut
57
58 sub GetPatronLists {
59     my ($params) = @_;
60
61     $params->{owner} ||= C4::Context->userenv->{'number'};
62
63     unless ( $params->{owner} ) {
64         carp("No owner passed in or defined!");
65         return;
66     }
67
68     delete $params->{owner} if C4::Context->IsSuperLibrarian();
69
70     if ( my $owner = $params->{owner} ) {
71         delete $params->{owner};
72         $params->{'-or'} = [
73             owner => $owner,
74             shared => 1,
75         ];
76     }
77
78     my $schema = Koha::Database->new()->schema();
79
80     my @patron_lists = $schema->resultset('PatronList')->search($params);
81
82     return wantarray() ? @patron_lists : \@patron_lists;
83 }
84
85 =head2 DelPatronList
86
87     DelPatronList( { patron_list_id => $list_id [, owner => $owner ] } );
88
89 =cut
90
91 sub DelPatronList {
92     my ($params) = @_;
93
94     $params->{owner} ||= C4::Context->userenv->{'number'};
95
96     unless ( $params->{patron_list_id} ) {
97         croak("No patron list id passed in!");
98     }
99     unless ( $params->{owner} ) {
100         carp("No owner passed in or defined!");
101         return;
102     }
103
104     delete( $params->{owner} ) if ( C4::Context->IsSuperLibrarian() );
105
106     return Koha::Database->new()->schema()->resultset('PatronList')
107       ->search($params)->single()->delete();
108 }
109
110 =head2 AddPatronList
111
112     AddPatronList( { name => $name [, owner => $owner ] } );
113
114 =cut
115
116 sub AddPatronList {
117     my ($params) = @_;
118
119     $params->{owner} ||= C4::Context->userenv->{'number'};
120
121     unless ( $params->{owner} ) {
122         carp("No owner passed in or defined!");
123         return;
124     }
125
126     unless ( $params->{name} ) {
127         carp("No list name passed in!");
128         return;
129     }
130
131     return Koha::Database->new()->schema()->resultset('PatronList')
132       ->create($params);
133 }
134
135 =head2 ModPatronList
136
137     ModPatronList( { patron_list_id => $id, name => $name [, owner => $owner ] } );
138
139 =cut
140
141 sub ModPatronList {
142     my ($params) = @_;
143
144     unless ( $params->{patron_list_id} ) {
145         carp("No patron list id passed in!");
146         return;
147     }
148
149     my ($list) = GetPatronLists(
150         {
151             patron_list_id => $params->{patron_list_id},
152             owner          => $params->{owner}
153         }
154     );
155
156     return $list->update($params);
157 }
158
159 =head2 AddPatronsToList
160
161     AddPatronsToList({ list => $list, cardnumbers => \@cardnumbers });
162
163 =cut
164
165 sub AddPatronsToList {
166     my ($params) = @_;
167
168     my $list            = $params->{list};
169     my $cardnumbers     = $params->{'cardnumbers'};
170     my $borrowernumbers = $params->{'borrowernumbers'};
171
172     return unless ( $list && ( $cardnumbers || $borrowernumbers ) );
173
174     my @borrowernumbers;
175
176     my %search_param;
177     if ($cardnumbers) {
178         $search_param{cardnumber} = { 'IN' => $cardnumbers };
179     } else {
180         $search_param{borrowernumber} = { 'IN' => $borrowernumbers };
181     }
182
183     @borrowernumbers =
184       Koha::Database->new()->schema()->resultset('Borrower')->search(
185         \%search_param,
186         { columns    => [qw/ borrowernumber /] }
187       )->get_column('borrowernumber')->all();
188
189     my $patron_list_id = $list->patron_list_id();
190
191     my $plp_rs = Koha::Database->new()->schema()->resultset('PatronListPatron');
192
193     my @results;
194     foreach my $borrowernumber (@borrowernumbers) {
195         my $result = $plp_rs->update_or_create(
196             {
197                 patron_list_id => $patron_list_id,
198                 borrowernumber => $borrowernumber
199             }
200         );
201         push( @results, $result );
202     }
203
204     return wantarray() ? @results : \@results;
205 }
206
207 =head2 DelPatronsFromList
208
209     DelPatronsFromList({ list => $list, patron_list_patrons => \@patron_list_patron_ids });
210
211 =cut
212
213 sub DelPatronsFromList {
214     my ($params) = @_;
215
216     my $list                = $params->{list};
217     my $patron_list_patrons = $params->{patron_list_patrons};
218
219     return unless ( $list && $patron_list_patrons );
220
221     return Koha::Database->new()->schema()->resultset('PatronListPatron')
222       ->search( { patron_list_patron_id => { 'IN' => $patron_list_patrons } } )
223       ->delete();
224 }
225
226 =head1 AUTHOR
227
228 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
229
230 =cut
231
232 1;
233
234 __END__