Bug 14585: Fixing up online help on main page
[koha.git] / C4 / Members / Statistics.pm
blob2dffd302429c670a4b19d06979aa058db3eede30
1 package C4::Members::Statistics;
3 # Copyright 2012 BibLibre
4 # This file is part of Koha.
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.
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.
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>.
19 =head1 NAME
21 C4::Members::Statistics - Get statistics for patron checkouts
23 =cut
25 use Modern::Perl;
27 use C4::Context;
29 our ( @ISA, @EXPORT, @EXPORT_OK, $debug );
31 BEGIN {
32 $debug = $ENV{DEBUG} || 0;
33 require Exporter;
34 @ISA = qw(Exporter);
36 push @EXPORT, qw(
37 &GetTotalIssuesTodayByBorrower
38 &GetTotalIssuesReturnedTodayByBorrower
39 &GetPrecedentStateByBorrower
44 our $fields = get_fields();
46 sub get_fields {
47 my $r = C4::Context->preference('StatisticsFields') || 'location|itype|ccode';
48 unless ( $r =~ m/^(\w|\d|\||-)+$/) {
49 warn "Members/Statistics : Bad value for syspref StatisticsFields" if $debug;
50 $r = 'location|itype|ccode';
52 return $r;
55 =head2 construct_query
56 Build a sql query from a subquery
57 Adds statistics fields to the select and the group by clause
58 =cut
59 sub construct_query {
60 my $count = shift;
61 my $subquery = shift;
62 my @select_fields = split '\|', $fields;
63 my $query = "SELECT COUNT(*) as count_$count,";
64 $query .= join ',', @select_fields;
66 $query .= " " . $subquery;
68 $fields =~ s/\|/,/g;
69 $query .= " GROUP BY $fields;";
71 return $query;
75 =head2 GetTotalIssuesTodayByBorrower
76 Return total issues for a borrower at this current day
77 =cut
78 sub GetTotalIssuesTodayByBorrower {
79 my ($borrowernumber) = @_;
80 my $dbh = C4::Context->dbh;
82 my $query = construct_query "total_issues_today",
83 "FROM (
84 SELECT it.* FROM issues i, items it WHERE i.itemnumber = it.itemnumber AND i.borrowernumber = ? AND DATE(i.issuedate) = CAST(now() AS date)
85 UNION
86 SELECT it.* FROM old_issues oi, items it WHERE oi.itemnumber = it.itemnumber AND oi.borrowernumber = ? AND DATE(oi.issuedate) = CAST(now() AS date)
87 ) tmp"; # alias is required by MySQL
89 my $sth = $dbh->prepare($query);
90 $sth->execute($borrowernumber, $borrowernumber);
91 return $sth->fetchall_arrayref( {} );
94 =head2 GetTotalIssuesReturnedTodayByBorrower
95 Return total issues returned by a borrower at this current day
96 =cut
97 sub GetTotalIssuesReturnedTodayByBorrower {
98 my ($borrowernumber) = @_;
99 my $dbh = C4::Context->dbh;
101 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) ";
103 my $sth = $dbh->prepare($query);
104 $sth->execute($borrowernumber);
105 return $sth->fetchall_arrayref( {} );
108 =head2 GetPrecedentStateByBorrower
109 Return the precedent state (before today) for a borrower of his checkins and checkouts
110 =cut
111 sub GetPrecedentStateByBorrower {
112 my ($borrowernumber) = @_;
113 my $dbh = C4::Context->dbh;
115 my $query = construct_query "precedent_state",
116 "FROM (
117 SELECT it.* FROM issues i, items it WHERE i.borrowernumber = ? AND i.itemnumber = it.itemnumber AND DATE(i.issuedate) < CAST(now() AS date)
118 UNION
119 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)
120 ) tmp"; # alias is required by MySQL
122 my $sth = $dbh->prepare($query);
123 $sth->execute($borrowernumber, $borrowernumber);
124 return $sth->fetchall_arrayref( {});