Bug 8194 - Layout problem on subscription add when showing manual history
[koha.git] / C4 / Members / Statistics.pm
1 package C4::Members::Statistics;
2
3 # Copyright 2012 BibLibre
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::Members::Statistics - Get statistics for patron checkouts
22
23 =cut
24
25 use Modern::Perl;
26
27 use C4::Context;
28
29 our ( @ISA, @EXPORT, @EXPORT_OK, $debug );
30
31 BEGIN {
32     $debug = $ENV{DEBUG} || 0;
33     require Exporter;
34     @ISA = qw(Exporter);
35
36     push @EXPORT, qw(
37         &GetTotalIssuesTodayByBorrower
38         &GetTotalIssuesReturnedTodayByBorrower
39         &GetPrecedentStateByBorrower
40     );
41 }
42
43 =head2 construct_query
44   Build a sql query from a subquery
45   Adds statistics fields to the select and the group by clause
46 =cut
47 sub construct_query {
48     my $count    = shift;
49     my $subquery = shift;
50     my $fields = C4::Context->preference('StatisticsFields') || 'location|itype|ccode';
51     my @select_fields = split '\|', $fields;
52     my $query = "SELECT COUNT(*) as count_$count";
53     $query .= ", " . C4::Context->dbh->quote( $_ ) for @select_fields;
54
55     $query .= " " . $subquery;
56
57     $fields =~ s/\|/,/g;
58     $query .= " GROUP BY $fields;";
59
60     return $query;
61
62 }
63
64 =head2 GetTotalIssuesTodayByBorrower
65   Return total issues for a borrower at this current day
66 =cut
67 sub GetTotalIssuesTodayByBorrower {
68     my ($borrowernumber) = @_;
69     my $dbh   = C4::Context->dbh;
70
71     my $query = construct_query "total_issues_today",
72         "FROM (
73             SELECT it.* FROM issues i, items it WHERE i.itemnumber = it.itemnumber AND i.borrowernumber = ? AND DATE(i.issuedate) = CAST(now() AS date)
74             UNION
75             SELECT it.* FROM old_issues oi, items it WHERE oi.itemnumber = it.itemnumber AND oi.borrowernumber = ? AND DATE(oi.issuedate) = CAST(now() AS date)
76         ) tmp";     # alias is required by MySQL
77
78     my $sth = $dbh->prepare($query);
79     $sth->execute($borrowernumber, $borrowernumber);
80     return $sth->fetchall_arrayref( {} );
81 }
82
83 =head2 GetTotalIssuesReturnedTodayByBorrower
84   Return total issues returned by a borrower at this current day
85 =cut
86 sub GetTotalIssuesReturnedTodayByBorrower {
87     my ($borrowernumber) = @_;
88     my $dbh   = C4::Context->dbh;
89
90     my $query = construct_query "total_issues_returned_today", "FROM old_issues i, items it WHERE i.itemnumber = it.itemnumber AND i.borrowernumber = ? AND DATE(i.returndate) = CAST(now() AS date) ";
91
92     my $sth = $dbh->prepare($query);
93     $sth->execute($borrowernumber);
94     return $sth->fetchall_arrayref( {} );
95 }
96
97 =head2 GetPrecedentStateByBorrower
98   Return the precedent state (before today) for a borrower of his checkins and checkouts
99 =cut
100 sub GetPrecedentStateByBorrower {
101     my ($borrowernumber) = @_;
102     my $dbh   = C4::Context->dbh;
103
104     my $query = construct_query "precedent_state",
105         "FROM (
106             SELECT it.* FROM issues i, items it WHERE i.borrowernumber = ? AND i.itemnumber = it.itemnumber AND DATE(i.issuedate) < CAST(now() AS date)
107             UNION
108             SELECT it.* FROM old_issues oi, items it WHERE oi.borrowernumber = ? AND oi.itemnumber = it.itemnumber AND DATE(oi.issuedate) < CAST(now() AS date) AND DATE(oi.returndate) = CAST(now() AS date)
109         ) tmp";     # alias is required by MySQL
110
111     my $sth = $dbh->prepare($query);
112     $sth->execute($borrowernumber, $borrowernumber);
113     return $sth->fetchall_arrayref( {});
114 }
115
116 1;