Bug 12406: adding the column maxsuspensiondays in the tests
[koha.git] / Koha / Borrower / Modifications.pm
1 package Koha::Borrower::Modifications;
2
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 =head1 NAME
20
21 C4::Borrowers::Modifications
22
23 =cut
24
25 use Modern::Perl;
26
27 use C4::Context;
28 use C4::Debug;
29 use C4::SQLHelper qw(InsertInTable UpdateInTable);
30
31 sub new {
32     my ( $class, %args ) = @_;
33
34     return bless( \%args, $class );
35 }
36
37 =head2 AddModifications
38
39 Koha::Borrower::Modifications->AddModifications( $data );
40
41 Adds or updates modifications for a borrower.
42
43 Requires either the key borrowernumber, or verification_token
44 to be part of the passed in hash.
45
46 =cut
47
48 sub AddModifications {
49     my ( $self, $data ) = @_;
50
51     if ( $self->{'borrowernumber'} ) {
52         delete $data->{'borrowernumber'};
53
54         if ( keys %$data ) {
55             $data->{'borrowernumber'} = $self->{'borrowernumber'};
56             my $dbh = C4::Context->dbh;
57
58             my $query = "
59                 SELECT COUNT(*) AS count
60                 FROM borrower_modifications
61                 WHERE borrowernumber = ?
62             ";
63
64             my $sth = $dbh->prepare($query);
65             $sth->execute( $self->{'borrowernumber'} );
66             my $result = $sth->fetchrow_hashref();
67
68             if ( $result->{'count'} ) {
69                 $data->{'verification_token'} = q{};
70                 return UpdateInTable( "borrower_modifications", $data );
71             }
72             else {
73                 return InsertInTable( "borrower_modifications", $data );
74             }
75         }
76
77     }
78     elsif ( $self->{'verification_token'} ) {
79         delete $data->{'borrowernumber'};
80         $data->{'verification_token'} = $self->{'verification_token'};
81
82         return InsertInTable( "borrower_modifications", $data );
83     }
84     else {
85         return;
86     }
87 }
88
89 =head2 Verify
90
91 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
92
93 Returns true if the passed in token is valid.
94
95 =cut
96
97 sub Verify {
98     my ( $self, $verification_token ) = @_;
99
100     $verification_token =
101       ($verification_token)
102       ? $verification_token
103       : $self->{'verification_token'};
104
105     my $dbh   = C4::Context->dbh;
106     my $query = "
107         SELECT COUNT(*) AS count
108         FROM borrower_modifications
109         WHERE verification_token = ?
110     ";
111     my $sth = $dbh->prepare($query);
112     $sth->execute($verification_token);
113     my $result = $sth->fetchrow_hashref();
114
115     return $result->{'count'};
116 }
117
118 =head2 GetPendingModificationsCount
119
120 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
121
122 Returns the number of pending modifications for existing borrowers.
123 =cut
124
125 sub GetPendingModificationsCount {
126     my ( $self, $branchcode ) = @_;
127
128     my $dbh   = C4::Context->dbh;
129     my $query = "
130         SELECT COUNT(*) AS count
131         FROM borrower_modifications, borrowers
132         WHERE borrower_modifications.borrowernumber > 0
133         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
134     ";
135
136     my @params;
137     if ($branchcode) {
138         $query .= " AND borrowers.branchcode = ? ";
139         push( @params, $branchcode );
140     }
141
142     my $sth = $dbh->prepare($query);
143     $sth->execute(@params);
144     my $result = $sth->fetchrow_hashref();
145
146     return $result->{'count'};
147 }
148
149 =head2 GetPendingModifications
150
151 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
152
153 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
154
155 =cut
156
157 sub GetPendingModifications {
158     my ( $self, $branchcode ) = @_;
159
160     my $dbh   = C4::Context->dbh;
161     my $query = "
162         SELECT borrower_modifications.*
163         FROM borrower_modifications, borrowers
164         WHERE borrower_modifications.borrowernumber > 0
165         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
166     ";
167
168     my @params;
169     if ($branchcode) {
170         $query .= " AND borrowers.branchcode = ? ";
171         push( @params, $branchcode );
172     }
173     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
174     my $sth = $dbh->prepare($query);
175     $sth->execute(@params);
176
177     my @m;
178     while ( my $row = $sth->fetchrow_hashref() ) {
179         foreach my $key ( keys %$row ) {
180             delete $row->{$key} unless defined $row->{$key};
181         }
182
183         push( @m, $row );
184     }
185
186     return \@m;
187 }
188
189 =head2 ApproveModifications
190
191 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
192
193 Commits the pending modifications to the borrower record and removes
194 them from the modifications table.
195
196 =cut
197
198 sub ApproveModifications {
199     my ( $self, $borrowernumber ) = @_;
200
201     $borrowernumber =
202       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
203
204     return unless $borrowernumber;
205
206     my $data = $self->GetModifications({ borrowernumber => $borrowernumber });
207
208     if ( UpdateInTable( "borrowers", $data ) ) {
209         $self->DelModifications({ borrowernumber => $borrowernumber });
210     }
211 }
212
213 =head2 DenyModifications
214
215 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
216
217 Removes the modifications from the table for the given borrower,
218 without commiting the changes to the borrower record.
219
220 =cut
221
222 sub DenyModifications {
223     my ( $self, $borrowernumber ) = @_;
224
225     $borrowernumber =
226       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
227
228     return unless $borrowernumber;
229
230     return $self->DelModifications({ borrowernumber => $borrowernumber });
231 }
232
233 =head2 DelModifications
234
235 Koha::Borrower::Modifications->DelModifications({
236   [ borrowernumber => $borrowernumber ],
237   [ verification_token => $verification_token ]
238 });
239
240 Deletes the modifications for the given borrowernumber or verification token.
241
242 =cut
243
244 sub DelModifications {
245     my ( $self, $params ) = @_;
246
247     my ( $field, $value );
248
249     if ( $params->{'borrowernumber'} ) {
250         $field = 'borrowernumber';
251         $value = $params->{'borrowernumber'};
252     }
253     elsif ( $params->{'verification_token'} ) {
254         $field = 'verification_token';
255         $value = $params->{'verification_token'};
256     }
257
258     return unless $value;
259
260     my $dbh = C4::Context->dbh;
261
262     $field = $dbh->quote_identifier($field);
263
264     my $query = "
265         DELETE
266         FROM borrower_modifications
267         WHERE $field = ?
268     ";
269
270     my $sth = $dbh->prepare($query);
271     return $sth->execute($value);
272 }
273
274 =head2 GetModifications
275
276 $hashref = Koha::Borrower::Modifications->GetModifications({
277   [ borrowernumber => $borrowernumber ],
278   [ verification_token => $verification_token ]
279 });
280
281 Gets the modifications for the given borrowernumber or verification token.
282
283 =cut
284
285 sub GetModifications {
286     my ( $self, $params ) = @_;
287
288     my ( $field, $value );
289
290     if ( defined( $params->{'borrowernumber'} ) ) {
291         $field = 'borrowernumber';
292         $value = $params->{'borrowernumber'};
293     }
294     elsif ( defined( $params->{'verification_token'} ) ) {
295         $field = 'verification_token';
296         $value = $params->{'verification_token'};
297     }
298
299     return unless $value;
300
301     my $dbh = C4::Context->dbh;
302
303     $field = $dbh->quote_identifier($field);
304
305     my $query = "
306         SELECT *
307         FROM borrower_modifications
308         WHERE $field = ?
309     ";
310
311     my $sth = $dbh->prepare($query);
312     $sth->execute($value);
313     my $data = $sth->fetchrow_hashref();
314
315     foreach my $key ( keys %$data ) {
316         delete $data->{$key} unless ( defined( $data->{$key} ) );
317     }
318
319     return $data;
320 }
321
322 1;