Bug 15758: Koha::Libraries - Remove GetBranches
[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 - Managment 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     my $schema = Koha::Database->new()->schema();
69
70     my @patron_lists = $schema->resultset('PatronList')->search($params);
71
72     return wantarray() ? @patron_lists : \@patron_lists;
73 }
74
75 =head2 DelPatronList
76
77     DelPatronList( { patron_list_id => $list_id [, owner => $owner ] } );
78
79 =cut
80
81 sub DelPatronList {
82     my ($params) = @_;
83
84     $params->{owner} ||= C4::Context->userenv->{'number'};
85
86     unless ( $params->{patron_list_id} ) {
87         croak("No patron list id passed in!");
88     }
89     unless ( $params->{owner} ) {
90         carp("No owner passed in or defined!");
91         return;
92     }
93
94     delete( $params->{owner} ) if ( C4::Context->IsSuperLibrarian() );
95
96     return Koha::Database->new()->schema()->resultset('PatronList')
97       ->search($params)->single()->delete();
98 }
99
100 =head2 AddPatronList
101
102     AddPatronList( { name => $name [, owner => $owner ] } );
103
104 =cut
105
106 sub AddPatronList {
107     my ($params) = @_;
108
109     $params->{owner} ||= C4::Context->userenv->{'number'};
110
111     unless ( $params->{owner} ) {
112         carp("No owner passed in or defined!");
113         return;
114     }
115
116     unless ( $params->{name} ) {
117         carp("No list name passed in!");
118         return;
119     }
120
121     return Koha::Database->new()->schema()->resultset('PatronList')
122       ->create($params);
123 }
124
125 =head2 ModPatronList
126
127     ModPatronList( { patron_list_id => $id, name => $name [, owner => $owner ] } );
128
129 =cut
130
131 sub ModPatronList {
132     my ($params) = @_;
133
134     unless ( $params->{patron_list_id} ) {
135         carp("No patron list id passed in!");
136         return;
137     }
138
139     my ($list) = GetPatronLists(
140         {
141             patron_list_id => $params->{patron_list_id},
142             owner          => $params->{owner}
143         }
144     );
145
146     return $list->update($params);
147 }
148
149 =head2 AddPatronsToList
150
151     AddPatronsToList({ list => $list, cardnumbers => \@cardnumbers });
152
153 =cut
154
155 sub AddPatronsToList {
156     my ($params) = @_;
157
158     my $list            = $params->{list};
159     my $cardnumbers     = $params->{'cardnumbers'};
160     my $borrowernumbers = $params->{'borrowernumbers'};
161
162     return unless ( $list && ( $cardnumbers || $borrowernumbers ) );
163
164     my @borrowernumbers;
165
166     if ($cardnumbers) {
167         @borrowernumbers =
168           Koha::Database->new()->schema()->resultset('Borrower')->search(
169             { cardnumber => { 'IN' => $cardnumbers } },
170             { columns    => [qw/ borrowernumber /] }
171           )->get_column('borrowernumber')->all();
172     }
173     else {
174         @borrowernumbers = @$borrowernumbers;
175     }
176
177     my $patron_list_id = $list->patron_list_id();
178
179     my $plp_rs = Koha::Database->new()->schema()->resultset('PatronListPatron');
180
181     my @results;
182     foreach my $borrowernumber (@borrowernumbers) {
183         my $result = $plp_rs->update_or_create(
184             {
185                 patron_list_id => $patron_list_id,
186                 borrowernumber => $borrowernumber
187             }
188         );
189         push( @results, $result );
190     }
191
192     return wantarray() ? @results : \@results;
193 }
194
195 =head2 DelPatronsFromList
196
197     DelPatronsFromList({ list => $list, patron_list_patrons => \@patron_list_patron_ids });
198
199 =cut
200
201 sub DelPatronsFromList {
202     my ($params) = @_;
203
204     my $list                = $params->{list};
205     my $patron_list_patrons = $params->{patron_list_patrons};
206
207     return unless ( $list && $patron_list_patrons );
208
209     return Koha::Database->new()->schema()->resultset('PatronListPatron')
210       ->search( { patron_list_patron_id => { 'IN' => $patron_list_patrons } } )
211       ->delete();
212 }
213
214 =head1 AUTHOR
215
216 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
217
218 =cut
219
220 1;
221
222 __END__