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