Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2014 BibLibre |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use CGI; |
22 |
|
23 |
use C4::Auth; |
24 |
use C4::Context; |
25 |
use C4::Output; |
26 |
|
27 |
use C4::Branch; |
28 |
use C4::Budgets; |
29 |
use C4::Category; |
30 |
use C4::Dates qw/format_date format_date_in_iso/; |
31 |
use C4::ItemType; |
32 |
use C4::Koha; |
33 |
use C4::Reports; |
34 |
|
35 |
use Koha::I18N; |
36 |
|
37 |
my $cgi = new CGI; |
38 |
my $template_name = 'reports/suggestions_stats.tt'; |
39 |
my $line = $cgi->param("line"); |
40 |
my $column = $cgi->param("column"); |
41 |
|
42 |
if ($line and $column) { |
43 |
$template_name = 'reports/suggestions_stats_results.tt'; |
44 |
} |
45 |
|
46 |
my ( $template, $borrowernumber, $cookie ) = get_template_and_user( |
47 |
{ |
48 |
template_name => $template_name, |
49 |
query => $cgi, |
50 |
type => 'intranet', |
51 |
authnotrequired => 0, |
52 |
flagsrequired => { reports => '*' }, |
53 |
} |
54 |
); |
55 |
|
56 |
if ($line and $column) { |
57 |
my ($filters, $groupby) = ({}, {}); |
58 |
foreach my $param ($cgi->param()) { |
59 |
my $value = $cgi->param($param); |
60 |
if ( (defined $value) && ($value ne '') ) { |
61 |
if ($param =~ /^filter_(.*)/) { |
62 |
$filters->{$1} = $cgi->param($param); |
63 |
} elsif ($param =~ /^groupby_(.*)/) { |
64 |
$groupby->{$1} = $cgi->param($param); |
65 |
} |
66 |
} |
67 |
} |
68 |
|
69 |
my $result = calculate($line, $column, $groupby, $filters); |
70 |
|
71 |
$template->param( |
72 |
line => $line, |
73 |
column => $column, |
74 |
filters => $filters, |
75 |
result => $result, |
76 |
); |
77 |
} else { |
78 |
my $budget_hierarchy = C4::Budgets::GetBudgetHierarchy; |
79 |
|
80 |
# Remove all children from the first level of this array. |
81 |
@$budget_hierarchy = grep { |
82 |
not defined $_->{budget_parent_id} |
83 |
} @$budget_hierarchy; |
84 |
|
85 |
$template->param( budget_hierarchy => $budget_hierarchy); |
86 |
} |
87 |
|
88 |
my $display_values = {}; |
89 |
|
90 |
$display_values->{status} = { |
91 |
ACCEPTED => gettext('Accepted'), |
92 |
ASKED => gettext('Pending'), |
93 |
AVAILABLE => gettext('Available'), |
94 |
CHECKED => gettext('Checked'), |
95 |
ORDERED => gettext('Ordered'), |
96 |
REJECTED => gettext('Rejected'), |
97 |
}; |
98 |
|
99 |
my $budgets = C4::Budgets::GetBudgets; |
100 |
foreach my $budget (@$budgets) { |
101 |
my $budget_id = $budget->{budget_id}; |
102 |
$display_values->{budgetid}->{$budget_id} = $budget->{budget_name}; |
103 |
} |
104 |
|
105 |
my @categories = C4::Category->all; |
106 |
foreach my $category (@categories) { |
107 |
my $code = $category->categorycode; |
108 |
$display_values->{categorycode}->{$code} = $category->description; |
109 |
} |
110 |
|
111 |
my $bsort1 = C4::Koha::GetAuthorisedValues('Bsort1'); |
112 |
foreach my $av (@$bsort1) { |
113 |
my $value = $av->{authorised_value}; |
114 |
$display_values->{sort1}->{$value} = $av->{lib}; |
115 |
} |
116 |
|
117 |
my $bsort2 = C4::Koha::GetAuthorisedValues('Bsort2'); |
118 |
foreach my $av (@$bsort2) { |
119 |
my $value = $av->{authorised_value}; |
120 |
$display_values->{sort2}->{$value} = $av->{lib}; |
121 |
} |
122 |
|
123 |
my $suggest = C4::Koha::GetAuthorisedValues('SUGGEST'); |
124 |
foreach my $av (@$suggest) { |
125 |
my $value = $av->{authorised_value}; |
126 |
$display_values->{reason}->{$value} = $av->{lib}; |
127 |
} |
128 |
|
129 |
my $branches = C4::Branch::GetBranchesLoop('', 0); |
130 |
foreach my $branch (@$branches) { |
131 |
my $branchcode = $branch->{branchcode}; |
132 |
$display_values->{branchcode}->{$branchcode} = $branch->{branchname}; |
133 |
} |
134 |
|
135 |
my @itemtypes = C4::ItemType->all; |
136 |
foreach my $itemtype (@itemtypes) { |
137 |
my $itype = $itemtype->itemtype; |
138 |
$display_values->{itemtype}->{$itype} = $itemtype->description; |
139 |
} |
140 |
|
141 |
$template->param( |
142 |
display_values => $display_values, |
143 |
); |
144 |
|
145 |
output_html_with_http_headers $cgi, $cookie, $template->output; |
146 |
|
147 |
sub get_select_expr { |
148 |
my ($field, $groupby) = @_; |
149 |
|
150 |
my $select_expr = $field; |
151 |
if ($groupby->{$field}) { |
152 |
if ($groupby->{$field} eq 'year') { |
153 |
$select_expr = "DATE_FORMAT($field, '%Y')"; |
154 |
} elsif ($groupby->{$field} eq 'month') { |
155 |
$select_expr = "DATE_FORMAT($field, '%Y-%m')"; |
156 |
} elsif ($groupby->{$field} eq 'week') { |
157 |
$select_expr = "DATE_FORMAT($field, '%x-%v')"; |
158 |
} elsif ($groupby->{$field} eq 'dow') { |
159 |
$select_expr = "DATE_FORMAT($field, '%w - %W')"; |
160 |
} |
161 |
} |
162 |
|
163 |
return $select_expr; |
164 |
} |
165 |
|
166 |
sub calculate { |
167 |
my ($line, $column, $groupby, $filters) = @_; |
168 |
|
169 |
my @valid_fields = (qw( |
170 |
suggesteddate rejecteddate ordereddate publicationyear status budgetid |
171 |
categorycode sort1 sort2 branchcode reason itemtype |
172 |
)); |
173 |
return unless (grep /^$line$/, @valid_fields); |
174 |
return unless (grep /^$column$/, @valid_fields); |
175 |
|
176 |
my $dbh = C4::Context->dbh; |
177 |
|
178 |
# preparing calculation |
179 |
my $linefield = get_select_expr($line, $groupby); |
180 |
my $colfield = get_select_expr($column, $groupby); |
181 |
my $query = qq| |
182 |
SELECT $linefield AS linefield, $colfield AS columnfield, COUNT(*) |
183 |
FROM suggestions |
184 |
|; |
185 |
|
186 |
if (grep /^(categorycode|sort1|sort2)$/, ($line, $column, keys %$filters)) { |
187 |
$query .= ' JOIN borrowers ON (suggestions.suggestedby = borrowers.borrowernumber) '; |
188 |
} |
189 |
|
190 |
my @conditions; |
191 |
my @bind_values; |
192 |
|
193 |
foreach my $filter (qw(suggesteddate rejecteddate ordereddate)) { |
194 |
if ($filters->{"${filter}_from"}) { |
195 |
push @conditions, "$filter >= ?"; |
196 |
push @bind_values, format_date_in_iso($filters->{"${filter}_from"}); |
197 |
} |
198 |
if ($filters->{"${filter}_to"}) { |
199 |
push @conditions, "$filter <= ?"; |
200 |
push @bind_values, format_date_in_iso($filters->{"${filter}_to"}); |
201 |
} |
202 |
} |
203 |
|
204 |
foreach my $filter (qw(status categorycode sort1 sort2 branchcode reason itemtype)) { |
205 |
if ($filters->{$filter}) { |
206 |
push @conditions, "$filter = ?"; |
207 |
push @bind_values, $filters->{$filter}; |
208 |
} |
209 |
} |
210 |
|
211 |
if ($filters->{publicationyear_from}) { |
212 |
push @conditions, 'publicationyear >= ?'; |
213 |
push @bind_values, $filters->{publicationyear_from}; |
214 |
} |
215 |
if ($filters->{publicationyear_to}) { |
216 |
push @conditions, 'publicationyear <= ?'; |
217 |
push @bind_values, $filters->{publicationyear_to}; |
218 |
} |
219 |
|
220 |
if ($filters->{budgetid}) { |
221 |
my @descendants; |
222 |
if ($filters->{budgetid_children}) { |
223 |
@descendants = C4::Budgets::GetBudgetDescendantsIds( |
224 |
$filters->{budgetid}); |
225 |
} |
226 |
my $cond = 'budgetid IN (' |
227 |
. join (',', ('?') x (@descendants + 1)) |
228 |
. ')'; |
229 |
push @conditions, $cond; |
230 |
push @bind_values, $filters->{budgetid}, @descendants; |
231 |
} |
232 |
|
233 |
if (@conditions) { |
234 |
$query .= ' WHERE ' . join(' AND ', @conditions); |
235 |
} |
236 |
|
237 |
$query .= " GROUP BY linefield, columnfield ORDER BY linefield, columnfield"; |
238 |
my $sth = $dbh->prepare($query); |
239 |
$sth->execute(@bind_values); |
240 |
|
241 |
my $table; |
242 |
while ( my ( $row, $col, $value ) = $sth->fetchrow ) { |
243 |
$col = "NULL" unless defined $col; |
244 |
$row = "NULL" unless defined $row; |
245 |
|
246 |
if (not defined($table->{$row}->{$col})) { |
247 |
$table->{$row}->{$col} = 0; |
248 |
} |
249 |
$table->{$row}->{$col} += $value; |
250 |
} |
251 |
|
252 |
my (@rows, @cols); |
253 |
foreach my $row (keys %$table) { |
254 |
foreach my $col (keys %{ $table->{$row} }) { |
255 |
push @cols, $col unless grep /^$col$/, @cols; |
256 |
} |
257 |
push @rows, $row unless grep /^$row$/, @rows; |
258 |
} |
259 |
@rows = sort @rows; |
260 |
@cols = sort @cols; |
261 |
|
262 |
my $result = { |
263 |
table => $table, |
264 |
rows => \@rows, |
265 |
cols => \@cols, |
266 |
}; |
267 |
return $result; |
268 |
} |