Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / members / statistics.pl
1 #!/usr/bin/perl
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 members/statistics.pl
20
21   Generate statistic issues for a member
22
23 =cut
24
25 use Modern::Perl;
26
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;
35
36 my $input = CGI->new;
37
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     }
44 );
45
46 my $borrowernumber = $input->param('borrowernumber');
47
48 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
49 my $patron         = Koha::Patrons->find( $borrowernumber );
50 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
51
52 my $category = $patron->category;
53
54 # Construct column names
55 my $fields = C4::Members::Statistics::get_fields();
56 our @statistic_column_names = split '\|', $fields;
57 our @value_column_names = ( 'count_precedent_state', 'count_total_issues_today', 'count_total_issues_returned_today' );
58 our @column_names = ( @statistic_column_names, @value_column_names );
59
60 # Get statistics
61 my $precedent_state = GetPrecedentStateByBorrower( $borrowernumber );
62 my $total_issues_today = GetTotalIssuesTodayByBorrower( $borrowernumber );
63 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower( $borrowernumber );
64 my $r = merge (
65     @$precedent_state, @$total_issues_today, @$total_issues_returned_today
66 );
67
68 add_actual_state( $r );
69 my ( $total, $datas ) = build_array( $r );
70
71 # Gettings sums
72 my $count_total_precedent_state = $total->{count_precedent_state} || 0;
73 my $count_total_issues = $total->{count_total_issues_today} || 0;
74 my $count_total_issues_returned = $total->{count_total_issues_returned_today} || 0;
75 my $count_total_actual_state = ($count_total_precedent_state - $count_total_issues_returned + $count_total_issues);
76
77 $template->param(
78     patron             => $patron,
79     statisticsview     => 1,
80     datas              => $datas,
81     column_names       => \@statistic_column_names,
82     count_total_issues => $count_total_issues,
83     count_total_issues_returned => $count_total_issues_returned,
84     count_total_precedent_state => $count_total_precedent_state,
85     count_total_actual_state => $count_total_actual_state,
86 );
87
88 output_html_with_http_headers $input, $cookie, $template->output;
89
90
91 =head1 FUNCTIONS
92
93 =head2 add_actual_state
94
95   Add a 'count_actual_state' key in all hashes
96   count_actual_state = count_precedent_state - count_total_issues_returned_today + count_total_issues_today
97
98 =cut
99
100 sub add_actual_state {
101     my ( $array ) = @_;
102     for my $hash ( @$array ) {
103         $hash->{count_actual_state} = ( $hash->{count_precedent_state} // 0 ) - ( $hash->{count_total_issues_returned_today} // 0 ) + ( $hash->{count_total_issues_today} // 0 );
104     }
105 }
106
107 =head2 build_array
108
109   Build a new array containing values of hashes.
110   It used by template whitch display silly values.
111   ex:
112     $array = [
113       {
114         'count_total_issues_returned_today' => 1,
115         'ccode' => 'ccode',
116         'count_actual_state' => 1,
117         'count_precedent_state' => 1,
118         'homebranch' => 'homebranch',
119         'count_total_issues_today' => 1,
120         'itype' => 'itype'
121       }
122     ];
123   and returns:
124     [
125       [
126         'homebranch',
127         'itype',
128         'ccode',
129         1,
130         1,
131         1,
132         1
133       ]
134     ];
135
136 =cut
137
138 sub build_array {
139     my ( $array ) = @_;
140     my ( @r, $total );
141     for my $hash ( @$array) {
142         my @line;
143         for my $cn ( ( @column_names, 'count_actual_state') ) {
144             if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
145                 $hash->{$cn} //= 0;
146                 if ( exists $total->{$cn} ) {
147                     $total->{$cn} += $hash->{$cn} if $hash->{$cn};
148                 } else {
149                     $total->{$cn} = $hash->{$cn};
150                 }
151             }
152             push @line, $hash->{$cn};
153         }
154         push @r, \@line;
155     }
156     return ( $total, \@r );
157 }
158
159 =head2 merge
160
161   Merge hashes with the same statistic column names into one
162   param: array, a arrayref of arrayrefs
163   ex:
164   @array = (
165      {
166        'ccode' => 'ccode',
167        'count_precedent_state' => '1',
168        'homebranch' => 'homebranch',
169        'itype' => 'itype'
170      },
171      {
172        'count_total_issues_returned_today' => '1',
173        'ccode' => 'ccode',
174        'homebranch' => 'homebranch',
175        'itype' => 'itype'
176      }
177    );
178    and returns:
179    [
180      {
181        'count_total_issues_returned_today' => '1',
182        'ccode' => 'ccode',
183        'count_precedent_state' => '1',
184        'homebranch' => 'homebranch',
185        'itype' => 'itype'
186      }
187    ];
188
189 =cut
190
191 sub merge {
192     my @array = @_;
193     my @r;
194     for my $h ( @array ) {
195         my $exists = 0;
196         for my $ch ( @r ) {
197             $exists = 1;
198             for my $cn ( @statistic_column_names ) {
199                 if (   ( not defined $ch->{$cn} && defined $h->{$cn} )
200                     || ( defined $ch->{$cn} && not defined $h->{$cn} )
201                     || ( $ch->{$cn} ne $h->{$cn} ) )
202                 {
203                     $exists = 0;
204                     last;
205                 }
206             }
207             if ($exists){
208                 for my $cn ( @value_column_names ) {
209                     next if not exists $h->{$cn};
210                     $ch->{$cn} = $h->{$cn} ? $h->{$cn} : 0;
211                 }
212                 last;
213             }
214         }
215
216         if ( not $exists ) {push @r, $h;}
217     }
218     return \@r;
219 }