Bug 25898: Prohibit indirect object notation
[koha.git] / members / statistics.pl
blob93367b3ed79be32883b2cd463ac34214694bf3e9
1 #!/usr/bin/perl
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 members/statistics.pl
21 Generate statistic issues for a member
23 =cut
25 use Modern::Perl;
27 use CGI qw ( -utf8 );
28 use C4::Auth;
29 use C4::Context;
30 use C4::Members;
31 use C4::Members::Statistics;
32 use C4::Output;
33 use Koha::Patrons;
34 use Koha::Patron::Categories;
36 my $input = CGI->new;
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 { template_name => "members/statistics.tt",
40 query => $input,
41 type => "intranet",
42 flagsrequired => { borrowers => 'edit_borrowers' },
43 debug => 1,
47 my $borrowernumber = $input->param('borrowernumber');
49 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
50 my $patron = Koha::Patrons->find( $borrowernumber );
51 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
53 my $category = $patron->category;
55 # Construct column names
56 my $fields = C4::Members::Statistics::get_fields();
57 our @statistic_column_names = split '\|', $fields;
58 our @value_column_names = ( 'count_precedent_state', 'count_total_issues_today', 'count_total_issues_returned_today' );
59 our @column_names = ( @statistic_column_names, @value_column_names );
61 # Get statistics
62 my $precedent_state = GetPrecedentStateByBorrower( $borrowernumber );
63 my $total_issues_today = GetTotalIssuesTodayByBorrower( $borrowernumber );
64 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower( $borrowernumber );
65 my $r = merge (
66 @$precedent_state, @$total_issues_today, @$total_issues_returned_today
69 add_actual_state( $r );
70 my ( $total, $datas ) = build_array( $r );
72 # Gettings sums
73 my $count_total_precedent_state = $total->{count_precedent_state} || 0;
74 my $count_total_issues = $total->{count_total_issues_today} || 0;
75 my $count_total_issues_returned = $total->{count_total_issues_returned_today} || 0;
76 my $count_total_actual_state = ($count_total_precedent_state - $count_total_issues_returned + $count_total_issues);
78 $template->param(
79 patron => $patron,
80 statisticsview => 1,
81 datas => $datas,
82 column_names => \@statistic_column_names,
83 count_total_issues => $count_total_issues,
84 count_total_issues_returned => $count_total_issues_returned,
85 count_total_precedent_state => $count_total_precedent_state,
86 count_total_actual_state => $count_total_actual_state,
89 output_html_with_http_headers $input, $cookie, $template->output;
92 =head1 FUNCTIONS
94 =head2 add_actual_state
96 Add a 'count_actual_state' key in all hashes
97 count_actual_state = count_precedent_state - count_total_issues_returned_today + count_total_issues_today
99 =cut
101 sub add_actual_state {
102 my ( $array ) = @_;
103 for my $hash ( @$array ) {
104 $hash->{count_actual_state} = ( $hash->{count_precedent_state} // 0 ) - ( $hash->{count_total_issues_returned_today} // 0 ) + ( $hash->{count_total_issues_today} // 0 );
108 =head2 build_array
110 Build a new array containing values of hashes.
111 It used by template whitch display silly values.
113 $array = [
115 'count_total_issues_returned_today' => 1,
116 'ccode' => 'ccode',
117 'count_actual_state' => 1,
118 'count_precedent_state' => 1,
119 'homebranch' => 'homebranch',
120 'count_total_issues_today' => 1,
121 'itype' => 'itype'
124 and returns:
127 'homebranch',
128 'itype',
129 'ccode',
137 =cut
139 sub build_array {
140 my ( $array ) = @_;
141 my ( @r, $total );
142 for my $hash ( @$array) {
143 my @line;
144 for my $cn ( ( @column_names, 'count_actual_state') ) {
145 if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
146 $hash->{$cn} //= 0;
147 if ( exists $total->{$cn} ) {
148 $total->{$cn} += $hash->{$cn} if $hash->{$cn};
149 } else {
150 $total->{$cn} = $hash->{$cn};
153 push @line, $hash->{$cn};
155 push @r, \@line;
157 return ( $total, \@r );
160 =head2 merge
162 Merge hashes with the same statistic column names into one
163 param: array, a arrayref of arrayrefs
165 @array = (
167 'ccode' => 'ccode',
168 'count_precedent_state' => '1',
169 'homebranch' => 'homebranch',
170 'itype' => 'itype'
173 'count_total_issues_returned_today' => '1',
174 'ccode' => 'ccode',
175 'homebranch' => 'homebranch',
176 'itype' => 'itype'
179 and returns:
182 'count_total_issues_returned_today' => '1',
183 'ccode' => 'ccode',
184 'count_precedent_state' => '1',
185 'homebranch' => 'homebranch',
186 'itype' => 'itype'
190 =cut
192 sub merge {
193 my @array = @_;
194 my @r;
195 for my $h ( @array ) {
196 my $exists = 0;
197 for my $ch ( @r ) {
198 $exists = 1;
199 for my $cn ( @statistic_column_names ) {
200 if ( ( not defined $ch->{$cn} && defined $h->{$cn} )
201 || ( defined $ch->{$cn} && not defined $h->{$cn} )
202 || ( $ch->{$cn} ne $h->{$cn} ) )
204 $exists = 0;
205 last;
208 if ($exists){
209 for my $cn ( @value_column_names ) {
210 next if not exists $h->{$cn};
211 $ch->{$cn} = $h->{$cn} ? $h->{$cn} : 0;
213 last;
217 if ( not $exists ) {push @r, $h;}
219 return \@r;