Bug 18089 - Unit test
[koha.git] / Koha / List / Patron.pm
blob1baa3b0cd1b6a8475135090ffb715c3ab63d424d
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 - Managment 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 my $schema = Koha::Database->new()->schema();
70 my @patron_lists = $schema->resultset('PatronList')->search($params);
72 return wantarray() ? @patron_lists : \@patron_lists;
75 =head2 DelPatronList
77 DelPatronList( { patron_list_id => $list_id [, owner => $owner ] } );
79 =cut
81 sub DelPatronList {
82 my ($params) = @_;
84 $params->{owner} ||= C4::Context->userenv->{'number'};
86 unless ( $params->{patron_list_id} ) {
87 croak("No patron list id passed in!");
89 unless ( $params->{owner} ) {
90 carp("No owner passed in or defined!");
91 return;
94 delete( $params->{owner} ) if ( C4::Context->IsSuperLibrarian() );
96 return Koha::Database->new()->schema()->resultset('PatronList')
97 ->search($params)->single()->delete();
100 =head2 AddPatronList
102 AddPatronList( { name => $name [, owner => $owner ] } );
104 =cut
106 sub AddPatronList {
107 my ($params) = @_;
109 $params->{owner} ||= C4::Context->userenv->{'number'};
111 unless ( $params->{owner} ) {
112 carp("No owner passed in or defined!");
113 return;
116 unless ( $params->{name} ) {
117 carp("No list name passed in!");
118 return;
121 return Koha::Database->new()->schema()->resultset('PatronList')
122 ->create($params);
125 =head2 ModPatronList
127 ModPatronList( { patron_list_id => $id, name => $name [, owner => $owner ] } );
129 =cut
131 sub ModPatronList {
132 my ($params) = @_;
134 unless ( $params->{patron_list_id} ) {
135 carp("No patron list id passed in!");
136 return;
139 my ($list) = GetPatronLists(
141 patron_list_id => $params->{patron_list_id},
142 owner => $params->{owner}
146 return $list->update($params);
149 =head2 AddPatronsToList
151 AddPatronsToList({ list => $list, cardnumbers => \@cardnumbers });
153 =cut
155 sub AddPatronsToList {
156 my ($params) = @_;
158 my $list = $params->{list};
159 my $cardnumbers = $params->{'cardnumbers'};
160 my $borrowernumbers = $params->{'borrowernumbers'};
162 return unless ( $list && ( $cardnumbers || $borrowernumbers ) );
164 my @borrowernumbers;
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();
173 else {
174 @borrowernumbers = @$borrowernumbers;
177 my $patron_list_id = $list->patron_list_id();
179 my $plp_rs = Koha::Database->new()->schema()->resultset('PatronListPatron');
181 my @results;
182 foreach my $borrowernumber (@borrowernumbers) {
183 my $result = $plp_rs->update_or_create(
185 patron_list_id => $patron_list_id,
186 borrowernumber => $borrowernumber
189 push( @results, $result );
192 return wantarray() ? @results : \@results;
195 =head2 DelPatronsFromList
197 DelPatronsFromList({ list => $list, patron_list_patrons => \@patron_list_patron_ids });
199 =cut
201 sub DelPatronsFromList {
202 my ($params) = @_;
204 my $list = $params->{list};
205 my $patron_list_patrons = $params->{patron_list_patrons};
207 return unless ( $list && $patron_list_patrons );
209 return Koha::Database->new()->schema()->resultset('PatronListPatron')
210 ->search( { patron_list_patron_id => { 'IN' => $patron_list_patrons } } )
211 ->delete();
214 =head1 AUTHOR
216 Kyle M Hall, E<lt>kyle@bywatersolutions.comE<gt>
218 =cut
222 __END__