Bug 22922: Allow to modify hold dates on reserve/request.pl
[koha.git] / Koha / List / Patron.pm
blobc6f43848a3136fe7e6556a16460fe08fccfe682a
1 package Koha::List::Patron;
3 # Copyright 2013 ByWater Solutions
5 # This file is part of Koha.
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.
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.
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>.
20 =head1 NAME
22 Koha::List::Patron - Management of lists of patrons
24 =head1 FUNCTIONS
26 =cut
28 use Modern::Perl;
30 use Carp;
32 use Koha::Database;
34 use base 'Exporter';
35 our @EXPORT = (
36 qw(
37 GetPatronLists
39 DelPatronList
40 AddPatronList
41 ModPatronList
43 AddPatronsToList
44 DelPatronsFromList
48 =head2 GetPatronLists
50 my @lists = GetPatronLists( $params );
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
56 sub GetPatronLists {
57 my ($params) = @_;
59 $params->{owner} ||= C4::Context->userenv->{'number'};
61 unless ( $params->{owner} ) {
62 carp("No owner passed in or defined!");
63 return;
66 delete $params->{owner} if C4::Context->IsSuperLibrarian();
68 if ( my $owner = $params->{owner} ) {
69 delete $params->{owner};
70 $params->{'-or'} = [
71 owner => $owner,
72 shared => 1,
76 my $schema = Koha::Database->new()->schema();
78 my @patron_lists = $schema->resultset('PatronList')->search($params);
80 return wantarray() ? @patron_lists : \@patron_lists;
83 =head2 DelPatronList
85 DelPatronList( { patron_list_id => $list_id [, owner => $owner ] } );
87 =cut
89 sub DelPatronList {
90 my ($params) = @_;
92 $params->{owner} ||= C4::Context->userenv->{'number'};
94 unless ( $params->{patron_list_id} ) {
95 croak("No patron list id passed in!");
97 unless ( $params->{owner} ) {
98 carp("No owner passed in or defined!");
99 return;
102 delete( $params->{owner} ) if ( C4::Context->IsSuperLibrarian() );
104 return Koha::Database->new()->schema()->resultset('PatronList')
105 ->search($params)->single()->delete();
108 =head2 AddPatronList
110 AddPatronList( { name => $name [, owner => $owner ] } );
112 =cut
114 sub AddPatronList {
115 my ($params) = @_;
117 $params->{owner} ||= C4::Context->userenv->{'number'};
119 unless ( $params->{owner} ) {
120 carp("No owner passed in or defined!");
121 return;
124 unless ( $params->{name} ) {
125 carp("No list name passed in!");
126 return;
129 return Koha::Database->new()->schema()->resultset('PatronList')
130 ->create($params);
133 =head2 ModPatronList
135 ModPatronList( { patron_list_id => $id, name => $name [, owner => $owner ] } );
137 =cut
139 sub ModPatronList {
140 my ($params) = @_;
142 unless ( $params->{patron_list_id} ) {
143 carp("No patron list id passed in!");
144 return;
147 my ($list) = GetPatronLists(
149 patron_list_id => $params->{patron_list_id},
150 owner => $params->{owner}
154 return $list->update($params);
157 =head2 AddPatronsToList
159 AddPatronsToList({ list => $list, cardnumbers => \@cardnumbers });
161 =cut
163 sub AddPatronsToList {
164 my ($params) = @_;
166 my $list = $params->{list};
167 my $cardnumbers = $params->{'cardnumbers'};
168 my $borrowernumbers = $params->{'borrowernumbers'};
170 return unless ( $list && ( $cardnumbers || $borrowernumbers ) );
172 my @borrowernumbers;
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();
181 else {
182 @borrowernumbers = @$borrowernumbers;
185 my $patron_list_id = $list->patron_list_id();
187 my $plp_rs = Koha::Database->new()->schema()->resultset('PatronListPatron');
189 my @results;
190 foreach my $borrowernumber (@borrowernumbers) {
191 my $result = $plp_rs->update_or_create(
193 patron_list_id => $patron_list_id,
194 borrowernumber => $borrowernumber
197 push( @results, $result );
200 return wantarray() ? @results : \@results;
203 =head2 DelPatronsFromList
205 DelPatronsFromList({ list => $list, patron_list_patrons => \@patron_list_patron_ids });
207 =cut
209 sub DelPatronsFromList {
210 my ($params) = @_;
212 my $list = $params->{list};
213 my $patron_list_patrons = $params->{patron_list_patrons};
215 return unless ( $list && $patron_list_patrons );
217 return Koha::Database->new()->schema()->resultset('PatronListPatron')
218 ->search( { patron_list_patron_id => { 'IN' => $patron_list_patrons } } )
219 ->delete();
222 =head1 AUTHOR
224 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
226 =cut
230 __END__