e8c5d1fcdcd3f35b6db46bf8ab9cac3a879392fa
[srvgit] / 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
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 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 );
30
31 BEGIN {
32     require Exporter;
33     @ISA = qw(Exporter);
34
35     push @EXPORT, qw(
36         &GetTotalIssuesTodayByBorrower
37         &GetTotalIssuesReturnedTodayByBorrower
38         &GetPrecedentStateByBorrower
39     );
40 }
41
42 =head2 get_fields
43   Get fields form syspref 'StatisticsFields'
44   Returns list of valid fields, defaults to 'location|itype|ccode'
45   if syspref is empty or does not contain valid fields
46
47 =cut
48
49 sub get_fields {
50
51     my $syspref = C4::Context->preference('StatisticsFields');
52     my $ret;
53
54     if ( $syspref ) {
55         my @ret;
56         my @spfields = split ('\|', $syspref);
57         my $schema       = Koha::Database->new()->schema();
58         my @columns = $schema->source('Item')->columns;
59
60         foreach my $fn ( @spfields ) {
61             push ( @ret, $fn ) if ( grep { $_ eq $fn } @columns );
62         }
63         $ret = join( '|', @ret);
64     }
65     return $ret || 'location|itype|ccode';
66 }
67
68 =head2 construct_query
69   Build a sql query from a subquery
70   Adds statistics fields to the select and the group by clause
71
72 =cut
73
74 sub construct_query {
75     my $count    = shift;
76     my $subquery = shift;
77     my $fields = get_fields();
78     my @select_fields = split '\|', $fields;
79     my $query = "SELECT COUNT(*) as count_$count,";
80     $query .= join ',', @select_fields;
81
82     $query .= " " . $subquery;
83
84     $fields =~ s/\|/,/g;
85     $query .= " GROUP BY $fields;";
86
87     return $query;
88
89 }
90
91 =head2 GetTotalIssuesTodayByBorrower
92   Return total issues for a borrower at this current day
93
94 =cut
95
96 sub GetTotalIssuesTodayByBorrower {
97     my ($borrowernumber) = @_;
98     my $dbh   = C4::Context->dbh;
99
100     my $query = construct_query "total_issues_today",
101         "FROM (
102             SELECT it.* FROM issues i, items it WHERE i.itemnumber = it.itemnumber AND i.borrowernumber = ? AND DATE(i.issuedate) = CAST(now() AS date)
103             UNION
104             SELECT it.* FROM old_issues oi, items it WHERE oi.itemnumber = it.itemnumber AND oi.borrowernumber = ? AND DATE(oi.issuedate) = CAST(now() AS date)
105         ) tmp";     # alias is required by MySQL
106
107     my $sth = $dbh->prepare($query);
108     $sth->execute($borrowernumber, $borrowernumber);
109     return $sth->fetchall_arrayref( {} );
110 }
111
112 =head2 GetTotalIssuesReturnedTodayByBorrower
113   Return total issues returned by a borrower at this current day
114
115 =cut
116
117 sub GetTotalIssuesReturnedTodayByBorrower {
118     my ($borrowernumber) = @_;
119     my $dbh   = C4::Context->dbh;
120
121     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) ";
122
123     my $sth = $dbh->prepare($query);
124     $sth->execute($borrowernumber);
125     return $sth->fetchall_arrayref( {} );
126 }
127
128 =head2 GetPrecedentStateByBorrower
129   Return the precedent state (before today) for a borrower of his checkins and checkouts
130
131 =cut
132
133 sub GetPrecedentStateByBorrower {
134     my ($borrowernumber) = @_;
135     my $dbh   = C4::Context->dbh;
136
137     my $query = construct_query "precedent_state",
138         "FROM (
139             SELECT it.* FROM issues i, items it WHERE i.borrowernumber = ? AND i.itemnumber = it.itemnumber AND DATE(i.issuedate) < CAST(now() AS date)
140             UNION
141             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)
142         ) tmp";     # alias is required by MySQL
143
144     my $sth = $dbh->prepare($query);
145     $sth->execute($borrowernumber, $borrowernumber);
146     return $sth->fetchall_arrayref( {});
147 }
148
149 1;