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