Bug 12623: SQLHelper replacement - Borrower::Modifications
[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
30 sub new {
31     my ( $class, %args ) = @_;
32
33     return bless( \%args, $class );
34 }
35
36 =head2 AddModifications
37
38 Koha::Borrower::Modifications->AddModifications( $data );
39
40 Adds or updates modifications for a borrower.
41
42 Requires either the key borrowernumber, or verification_token
43 to be part of the passed in hash.
44
45 =cut
46
47 sub AddModifications {
48     my ( $self, $data ) = @_;
49
50     delete $data->{borrowernumber};
51     if( $self->{borrowernumber} ) {
52         return if( not keys %$data );
53         $data->{borrowernumber} = $self->{borrowernumber};
54     }
55     elsif( $self->{verification_token} ) {
56         $data->{verification_token} = $self->{verification_token};
57     }
58     else {
59         return;
60     }
61
62     my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
63     return $rs->update_or_create($data);
64 }
65
66 =head2 Verify
67
68 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
69
70 Returns true if the passed in token is valid.
71
72 =cut
73
74 sub Verify {
75     my ( $self, $verification_token ) = @_;
76
77     $verification_token =
78       ($verification_token)
79       ? $verification_token
80       : $self->{'verification_token'};
81
82     my $dbh   = C4::Context->dbh;
83     my $query = "
84         SELECT COUNT(*) AS count
85         FROM borrower_modifications
86         WHERE verification_token = ?
87     ";
88     my $sth = $dbh->prepare($query);
89     $sth->execute($verification_token);
90     my $result = $sth->fetchrow_hashref();
91
92     return $result->{'count'};
93 }
94
95 =head2 GetPendingModificationsCount
96
97 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
98
99 Returns the number of pending modifications for existing borrowers.
100
101 =cut
102
103 sub GetPendingModificationsCount {
104     my ( $self, $branchcode ) = @_;
105
106     my $dbh   = C4::Context->dbh;
107     my $query = "
108         SELECT COUNT(*) AS count
109         FROM borrower_modifications, borrowers
110         WHERE borrower_modifications.borrowernumber > 0
111         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
112     ";
113
114     my @params;
115     if ($branchcode) {
116         $query .= " AND borrowers.branchcode = ? ";
117         push( @params, $branchcode );
118     }
119
120     my $sth = $dbh->prepare($query);
121     $sth->execute(@params);
122     my $result = $sth->fetchrow_hashref();
123
124     return $result->{'count'};
125 }
126
127 =head2 GetPendingModifications
128
129 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
130
131 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
132
133 =cut
134
135 sub GetPendingModifications {
136     my ( $self, $branchcode ) = @_;
137
138     my $dbh   = C4::Context->dbh;
139     my $query = "
140         SELECT borrower_modifications.*
141         FROM borrower_modifications, borrowers
142         WHERE borrower_modifications.borrowernumber > 0
143         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
144     ";
145
146     my @params;
147     if ($branchcode) {
148         $query .= " AND borrowers.branchcode = ? ";
149         push( @params, $branchcode );
150     }
151     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
152     my $sth = $dbh->prepare($query);
153     $sth->execute(@params);
154
155     my @m;
156     while ( my $row = $sth->fetchrow_hashref() ) {
157         foreach my $key ( keys %$row ) {
158             delete $row->{$key} unless defined $row->{$key};
159         }
160
161         push( @m, $row );
162     }
163
164     return \@m;
165 }
166
167 =head2 ApproveModifications
168
169 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
170
171 Commits the pending modifications to the borrower record and removes
172 them from the modifications table.
173
174 =cut
175
176 sub ApproveModifications {
177     my ( $self, $borrowernumber ) = @_;
178
179     $borrowernumber =
180       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
181
182     return unless $borrowernumber;
183
184     my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
185     delete $data->{timestamp};
186     delete $data->{verification_token};
187
188     my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
189         borrowernumber => $data->{borrowernumber},
190     });
191     if( $rs->update($data) ) {
192         $self->DelModifications( { borrowernumber => $borrowernumber } );
193     }
194 }
195
196 =head2 DenyModifications
197
198 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
199
200 Removes the modifications from the table for the given borrower,
201 without commiting the changes to the borrower record.
202
203 =cut
204
205 sub DenyModifications {
206     my ( $self, $borrowernumber ) = @_;
207
208     $borrowernumber =
209       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
210
211     return unless $borrowernumber;
212
213     return $self->DelModifications( { borrowernumber => $borrowernumber } );
214 }
215
216 =head2 DelModifications
217
218 Koha::Borrower::Modifications->DelModifications({
219   [ borrowernumber => $borrowernumber ],
220   [ verification_token => $verification_token ]
221 });
222
223 Deletes the modifications for the given borrowernumber or verification token.
224
225 =cut
226
227 sub DelModifications {
228     my ( $self, $params ) = @_;
229
230     my ( $field, $value );
231
232     if ( $params->{'borrowernumber'} ) {
233         $field = 'borrowernumber';
234         $value = $params->{'borrowernumber'};
235     }
236     elsif ( $params->{'verification_token'} ) {
237         $field = 'verification_token';
238         $value = $params->{'verification_token'};
239     }
240
241     return unless $value;
242
243     my $dbh = C4::Context->dbh;
244
245     $field = $dbh->quote_identifier($field);
246
247     my $query = "
248         DELETE
249         FROM borrower_modifications
250         WHERE $field = ?
251     ";
252
253     my $sth = $dbh->prepare($query);
254     return $sth->execute($value);
255 }
256
257 =head2 GetModifications
258
259 $hashref = Koha::Borrower::Modifications->GetModifications({
260   [ borrowernumber => $borrowernumber ],
261   [ verification_token => $verification_token ]
262 });
263
264 Gets the modifications for the given borrowernumber or verification token.
265
266 =cut
267
268 sub GetModifications {
269     my ( $self, $params ) = @_;
270
271     my ( $field, $value );
272
273     if ( defined( $params->{'borrowernumber'} ) ) {
274         $field = 'borrowernumber';
275         $value = $params->{'borrowernumber'};
276     }
277     elsif ( defined( $params->{'verification_token'} ) ) {
278         $field = 'verification_token';
279         $value = $params->{'verification_token'};
280     }
281
282     return unless $value;
283
284     my $dbh = C4::Context->dbh;
285
286     $field = $dbh->quote_identifier($field);
287
288     my $query = "
289         SELECT *
290         FROM borrower_modifications
291         WHERE $field = ?
292     ";
293
294     my $sth = $dbh->prepare($query);
295     $sth->execute($value);
296     my $data = $sth->fetchrow_hashref();
297
298     foreach my $key ( keys %$data ) {
299         delete $data->{$key} unless ( defined( $data->{$key} ) );
300     }
301
302     return $data;
303 }
304
305 1;