Bug 15548: Move new patron related code to Patron*
[koha.git] / Koha / Patron / Modifications.pm
1 package Koha::Patron::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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 =head1 NAME
20
21 Koha::Patron::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::Patron::Modifications->AddModifications( $data );
39
40 Adds or updates modifications for a patron
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         $data->{verification_token} = '';
55     }
56     elsif( $self->{verification_token} ) {
57         $data->{verification_token} = $self->{verification_token};
58         $data->{borrowernumber} = 0;
59     }
60     else {
61         return;
62     }
63
64     my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
65     return $rs->update_or_create($data, { key => 'primary' } );
66 }
67
68 =head2 Verify
69
70 $verified = Koha::Patron::Modifications->Verify( $verification_token );
71
72 Returns true if the passed in token is valid.
73
74 =cut
75
76 sub Verify {
77     my ( $self, $verification_token ) = @_;
78
79     $verification_token =
80       ($verification_token)
81       ? $verification_token
82       : $self->{'verification_token'};
83
84     my $dbh   = C4::Context->dbh;
85     my $query = "
86         SELECT COUNT(*) AS count
87         FROM borrower_modifications
88         WHERE verification_token = ?
89     ";
90     my $sth = $dbh->prepare($query);
91     $sth->execute($verification_token);
92     my $result = $sth->fetchrow_hashref();
93
94     return $result->{'count'};
95 }
96
97 =head2 GetPendingModificationsCount
98
99 $count = Koha::Patron::Modifications->GetPendingModificationsCount();
100
101 Returns the number of pending modifications for existing patron.
102
103 =cut
104
105 sub GetPendingModificationsCount {
106     my ( $self, $branchcode ) = @_;
107
108     my $dbh   = C4::Context->dbh;
109     my $query = "
110         SELECT COUNT(*) AS count
111         FROM borrower_modifications, borrowers
112         WHERE borrower_modifications.borrowernumber > 0
113         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
114     ";
115
116     my @params;
117     if ($branchcode) {
118         $query .= " AND borrowers.branchcode = ? ";
119         push( @params, $branchcode );
120     }
121
122     my $sth = $dbh->prepare($query);
123     $sth->execute(@params);
124     my $result = $sth->fetchrow_hashref();
125
126     return $result->{'count'};
127 }
128
129 =head2 GetPendingModifications
130
131 $arrayref = Koha::Patron::Modifications->GetPendingModifications();
132
133 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
134
135 =cut
136
137 sub GetPendingModifications {
138     my ( $self, $branchcode ) = @_;
139
140     my $dbh   = C4::Context->dbh;
141     my $query = "
142         SELECT borrower_modifications.*
143         FROM borrower_modifications, borrowers
144         WHERE borrower_modifications.borrowernumber > 0
145         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
146     ";
147
148     my @params;
149     if ($branchcode) {
150         $query .= " AND borrowers.branchcode = ? ";
151         push( @params, $branchcode );
152     }
153     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
154     my $sth = $dbh->prepare($query);
155     $sth->execute(@params);
156
157     my @m;
158     while ( my $row = $sth->fetchrow_hashref() ) {
159         foreach my $key ( keys %$row ) {
160             delete $row->{$key} unless defined $row->{$key};
161         }
162
163         push( @m, $row );
164     }
165
166     return \@m;
167 }
168
169 =head2 ApproveModifications
170
171 Koha::Patron::Modifications->ApproveModifications( $borrowernumber );
172
173 Commits the pending modifications to the borrower record and removes
174 them from the modifications table.
175
176 =cut
177
178 sub ApproveModifications {
179     my ( $self, $borrowernumber ) = @_;
180
181     $borrowernumber =
182       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
183
184     return unless $borrowernumber;
185
186     my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
187     delete $data->{timestamp};
188     delete $data->{verification_token};
189
190     my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
191         borrowernumber => $data->{borrowernumber},
192     });
193     if( $rs->update($data) ) {
194         $self->DelModifications( { borrowernumber => $borrowernumber } );
195     }
196 }
197
198 =head2 DenyModifications
199
200 Koha::Patron::Modifications->DenyModifications( $borrowernumber );
201
202 Removes the modifications from the table for the given patron,
203 without committing the changes to the patron record.
204
205 =cut
206
207 sub DenyModifications {
208     my ( $self, $borrowernumber ) = @_;
209
210     $borrowernumber =
211       ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
212
213     return unless $borrowernumber;
214
215     return $self->DelModifications( { borrowernumber => $borrowernumber } );
216 }
217
218 =head2 DelModifications
219
220 Koha::Patron::Modifications->DelModifications({
221   [ borrowernumber => $borrowernumber ],
222   [ verification_token => $verification_token ]
223 });
224
225 Deletes the modifications for the given borrowernumber or verification token.
226
227 =cut
228
229 sub DelModifications {
230     my ( $self, $params ) = @_;
231
232     my ( $field, $value );
233
234     if ( $params->{'borrowernumber'} ) {
235         $field = 'borrowernumber';
236         $value = $params->{'borrowernumber'};
237     }
238     elsif ( $params->{'verification_token'} ) {
239         $field = 'verification_token';
240         $value = $params->{'verification_token'};
241     }
242
243     return unless $value;
244
245     my $dbh = C4::Context->dbh;
246
247     $field = $dbh->quote_identifier($field);
248
249     my $query = "
250         DELETE
251         FROM borrower_modifications
252         WHERE $field = ?
253     ";
254
255     my $sth = $dbh->prepare($query);
256     return $sth->execute($value);
257 }
258
259 =head2 GetModifications
260
261 $hashref = Koha::Patron::Modifications->GetModifications({
262   [ borrowernumber => $borrowernumber ],
263   [ verification_token => $verification_token ]
264 });
265
266 Gets the modifications for the given borrowernumber or verification token.
267
268 =cut
269
270 sub GetModifications {
271     my ( $self, $params ) = @_;
272
273     my ( $field, $value );
274
275     if ( defined( $params->{'borrowernumber'} ) ) {
276         $field = 'borrowernumber';
277         $value = $params->{'borrowernumber'};
278     }
279     elsif ( defined( $params->{'verification_token'} ) ) {
280         $field = 'verification_token';
281         $value = $params->{'verification_token'};
282     }
283
284     return unless $value;
285
286     my $dbh = C4::Context->dbh;
287
288     $field = $dbh->quote_identifier($field);
289
290     my $query = "
291         SELECT *
292         FROM borrower_modifications
293         WHERE $field = ?
294     ";
295
296     my $sth = $dbh->prepare($query);
297     $sth->execute($value);
298     my $data = $sth->fetchrow_hashref();
299
300     foreach my $key ( keys %$data ) {
301         delete $data->{$key} unless ( defined( $data->{$key} ) );
302     }
303
304     return $data;
305 }
306
307 1;