Bug 16960 - Patron::Modifications should be fixed
[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
29 use Koha::Patron::Modification;
30
31 use base qw(Koha::Objects);
32
33 =head2 pending_count
34
35 $count = Koha::Patron::Modifications->pending_count();
36
37 Returns the number of pending modifications for existing patrons.
38
39 =cut
40
41 sub pending_count {
42     my ( $self, $branchcode ) = @_;
43
44     my $dbh   = C4::Context->dbh;
45     my $query = "
46         SELECT COUNT(*) AS count
47         FROM borrower_modifications, borrowers
48         WHERE borrower_modifications.borrowernumber > 0
49         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
50     ";
51
52     my @params;
53     if ($branchcode) {
54         $query .= " AND borrowers.branchcode = ? ";
55         push( @params, $branchcode );
56     }
57
58     my $sth = $dbh->prepare($query);
59     $sth->execute(@params);
60     my $result = $sth->fetchrow_hashref();
61
62     return $result->{count};
63 }
64
65 =head2 pending
66
67 $arrayref = Koha::Patron::Modifications->pending();
68
69 Returns an arrayref of hashrefs for all pending modifications for existing patrons.
70
71 =cut
72
73 sub pending {
74     my ( $self, $branchcode ) = @_;
75
76     my $dbh   = C4::Context->dbh;
77     my $query = "
78         SELECT borrower_modifications.*
79         FROM borrower_modifications, borrowers
80         WHERE borrower_modifications.borrowernumber > 0
81         AND borrower_modifications.borrowernumber = borrowers.borrowernumber
82     ";
83
84     my @params;
85     if ($branchcode) {
86         $query .= " AND borrowers.branchcode = ? ";
87         push( @params, $branchcode );
88     }
89     $query .= " ORDER BY borrowers.surname, borrowers.firstname";
90     my $sth = $dbh->prepare($query);
91     $sth->execute(@params);
92
93     my @m;
94     while ( my $row = $sth->fetchrow_hashref() ) {
95         foreach my $key ( keys %$row ) {
96             delete $row->{$key} unless defined $row->{$key};
97         }
98
99         push( @m, $row );
100     }
101
102     return \@m;
103 }
104
105 sub _type {
106     return 'BorrowerModification';
107 }
108
109 =head3 object_class
110
111 =cut
112
113 sub object_class {
114     return 'Koha::Patron::Modification';
115 }
116
117 1;