Bug 12927: Problems with item information tab on acq order from staged page
[koha.git] / Koha / Borrower / Modifications.pm
blobd01565518c713e7385d2cacdf4719418e16a852a
1 package Koha::Borrower::Modifications;
3 # Copyright 2012 ByWater Solutions
4 # This file is part of Koha.
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.
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.
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.
19 =head1 NAME
21 C4::Borrowers::Modifications
23 =cut
25 use Modern::Perl;
27 use C4::Context;
28 use C4::Debug;
30 sub new {
31 my ( $class, %args ) = @_;
33 return bless( \%args, $class );
36 =head2 AddModifications
38 Koha::Borrower::Modifications->AddModifications( $data );
40 Adds or updates modifications for a borrower.
42 Requires either the key borrowernumber, or verification_token
43 to be part of the passed in hash.
45 =cut
47 sub AddModifications {
48 my ( $self, $data ) = @_;
50 delete $data->{borrowernumber};
51 if( $self->{borrowernumber} ) {
52 return if( not keys %$data );
53 $data->{borrowernumber} = $self->{borrowernumber};
54 $data->{verification_token} = '';
56 elsif( $self->{verification_token} ) {
57 $data->{verification_token} = $self->{verification_token};
58 $data->{borrowernumber} = 0;
60 else {
61 return;
64 my $rs = Koha::Database->new()->schema->resultset('BorrowerModification');
65 return $rs->update_or_create($data, { key => 'primary' } );
68 =head2 Verify
70 $verified = Koha::Borrower::Modifications->Verify( $verification_token );
72 Returns true if the passed in token is valid.
74 =cut
76 sub Verify {
77 my ( $self, $verification_token ) = @_;
79 $verification_token =
80 ($verification_token)
81 ? $verification_token
82 : $self->{'verification_token'};
84 my $dbh = C4::Context->dbh;
85 my $query = "
86 SELECT COUNT(*) AS count
87 FROM borrower_modifications
88 WHERE verification_token = ?
90 my $sth = $dbh->prepare($query);
91 $sth->execute($verification_token);
92 my $result = $sth->fetchrow_hashref();
94 return $result->{'count'};
97 =head2 GetPendingModificationsCount
99 $count = Koha::Borrower::Modifications->GetPendingModificationsCount();
101 Returns the number of pending modifications for existing borrowers.
103 =cut
105 sub GetPendingModificationsCount {
106 my ( $self, $branchcode ) = @_;
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
116 my @params;
117 if ($branchcode) {
118 $query .= " AND borrowers.branchcode = ? ";
119 push( @params, $branchcode );
122 my $sth = $dbh->prepare($query);
123 $sth->execute(@params);
124 my $result = $sth->fetchrow_hashref();
126 return $result->{'count'};
129 =head2 GetPendingModifications
131 $arrayref = Koha::Borrower::Modifications->GetPendingModifications();
133 Returns an arrayref of hashrefs for all pending modifications for existing borrowers.
135 =cut
137 sub GetPendingModifications {
138 my ( $self, $branchcode ) = @_;
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
148 my @params;
149 if ($branchcode) {
150 $query .= " AND borrowers.branchcode = ? ";
151 push( @params, $branchcode );
153 $query .= " ORDER BY borrowers.surname, borrowers.firstname";
154 my $sth = $dbh->prepare($query);
155 $sth->execute(@params);
157 my @m;
158 while ( my $row = $sth->fetchrow_hashref() ) {
159 foreach my $key ( keys %$row ) {
160 delete $row->{$key} unless defined $row->{$key};
163 push( @m, $row );
166 return \@m;
169 =head2 ApproveModifications
171 Koha::Borrower::Modifications->ApproveModifications( $borrowernumber );
173 Commits the pending modifications to the borrower record and removes
174 them from the modifications table.
176 =cut
178 sub ApproveModifications {
179 my ( $self, $borrowernumber ) = @_;
181 $borrowernumber =
182 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
184 return unless $borrowernumber;
186 my $data = $self->GetModifications( { borrowernumber => $borrowernumber } );
187 delete $data->{timestamp};
188 delete $data->{verification_token};
190 my $rs = Koha::Database->new()->schema->resultset('Borrower')->search({
191 borrowernumber => $data->{borrowernumber},
193 if( $rs->update($data) ) {
194 $self->DelModifications( { borrowernumber => $borrowernumber } );
198 =head2 DenyModifications
200 Koha::Borrower::Modifications->DenyModifications( $borrowernumber );
202 Removes the modifications from the table for the given borrower,
203 without commiting the changes to the borrower record.
205 =cut
207 sub DenyModifications {
208 my ( $self, $borrowernumber ) = @_;
210 $borrowernumber =
211 ($borrowernumber) ? $borrowernumber : $self->{'borrowernumber'};
213 return unless $borrowernumber;
215 return $self->DelModifications( { borrowernumber => $borrowernumber } );
218 =head2 DelModifications
220 Koha::Borrower::Modifications->DelModifications({
221 [ borrowernumber => $borrowernumber ],
222 [ verification_token => $verification_token ]
225 Deletes the modifications for the given borrowernumber or verification token.
227 =cut
229 sub DelModifications {
230 my ( $self, $params ) = @_;
232 my ( $field, $value );
234 if ( $params->{'borrowernumber'} ) {
235 $field = 'borrowernumber';
236 $value = $params->{'borrowernumber'};
238 elsif ( $params->{'verification_token'} ) {
239 $field = 'verification_token';
240 $value = $params->{'verification_token'};
243 return unless $value;
245 my $dbh = C4::Context->dbh;
247 $field = $dbh->quote_identifier($field);
249 my $query = "
250 DELETE
251 FROM borrower_modifications
252 WHERE $field = ?
255 my $sth = $dbh->prepare($query);
256 return $sth->execute($value);
259 =head2 GetModifications
261 $hashref = Koha::Borrower::Modifications->GetModifications({
262 [ borrowernumber => $borrowernumber ],
263 [ verification_token => $verification_token ]
266 Gets the modifications for the given borrowernumber or verification token.
268 =cut
270 sub GetModifications {
271 my ( $self, $params ) = @_;
273 my ( $field, $value );
275 if ( defined( $params->{'borrowernumber'} ) ) {
276 $field = 'borrowernumber';
277 $value = $params->{'borrowernumber'};
279 elsif ( defined( $params->{'verification_token'} ) ) {
280 $field = 'verification_token';
281 $value = $params->{'verification_token'};
284 return unless $value;
286 my $dbh = C4::Context->dbh;
288 $field = $dbh->quote_identifier($field);
290 my $query = "
291 SELECT *
292 FROM borrower_modifications
293 WHERE $field = ?
296 my $sth = $dbh->prepare($query);
297 $sth->execute($value);
298 my $data = $sth->fetchrow_hashref();
300 foreach my $key ( keys %$data ) {
301 delete $data->{$key} unless ( defined( $data->{$key} ) );
304 return $data;