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
28 use C4::Auth qw( get_template_and_user );
31 use C4::Members::Statistics qw(
32 GetPrecedentStateByBorrower
33 GetTotalIssuesReturnedTodayByBorrower
34 GetTotalIssuesTodayByBorrower
36 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
38 use Koha::Patron::Categories;
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 { template_name => "members/statistics.tt",
46 flagsrequired => { borrowers => 'edit_borrowers' },
50 my $borrowernumber = $input->param('borrowernumber');
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 } );
56 my $category = $patron->category;
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 );
65 my $precedent_state = GetPrecedentStateByBorrower( $borrowernumber );
66 my $total_issues_today = GetTotalIssuesTodayByBorrower( $borrowernumber );
67 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower( $borrowernumber );
69 @$precedent_state, @$total_issues_today, @$total_issues_returned_today
72 add_actual_state( $r );
73 my ( $total, $datas ) = build_array( $r );
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);
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,
92 output_html_with_http_headers $input, $cookie, $template->output;
97 =head2 add_actual_state
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
104 sub add_actual_state {
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 );
113 Build a new array containing values of hashes.
114 It used by template whitch display silly values.
118 'count_total_issues_returned_today' => 1,
120 'count_actual_state' => 1,
121 'count_precedent_state' => 1,
122 'homebranch' => 'homebranch',
123 'count_total_issues_today' => 1,
145 for my $hash ( @$array) {
147 for my $cn ( ( @column_names, 'count_actual_state') ) {
148 if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
150 if ( exists $total->{$cn} ) {
151 $total->{$cn} += $hash->{$cn} if $hash->{$cn};
153 $total->{$cn} = $hash->{$cn};
156 push @line, $hash->{$cn};
160 return ( $total, \@r );
165 Merge hashes with the same statistic column names into one
166 param: array, a arrayref of arrayrefs
171 'count_precedent_state' => '1',
172 'homebranch' => 'homebranch',
176 'count_total_issues_returned_today' => '1',
178 'homebranch' => 'homebranch',
185 'count_total_issues_returned_today' => '1',
187 'count_precedent_state' => '1',
188 'homebranch' => 'homebranch',
198 for my $h ( @array ) {
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} ) )
212 for my $cn ( @value_column_names ) {
213 next if not exists $h->{$cn};
214 $ch->{$cn} = $h->{$cn} ? $h->{$cn} : 0;
220 if ( not $exists ) {push @r, $h;}