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
31 use C4::Members::Statistics;
34 use Koha::Patron::Categories;
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 { template_name => "members/statistics.tt",
42 flagsrequired => { borrowers => 'edit_borrowers' },
46 my $borrowernumber = $input->param('borrowernumber');
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 } );
52 my $category = $patron->category;
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 );
61 my $precedent_state = GetPrecedentStateByBorrower( $borrowernumber );
62 my $total_issues_today = GetTotalIssuesTodayByBorrower( $borrowernumber );
63 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower( $borrowernumber );
65 @$precedent_state, @$total_issues_today, @$total_issues_returned_today
68 add_actual_state( $r );
69 my ( $total, $datas ) = build_array( $r );
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);
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,
88 output_html_with_http_headers $input, $cookie, $template->output;
93 =head2 add_actual_state
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
100 sub add_actual_state {
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 );
109 Build a new array containing values of hashes.
110 It used by template whitch display silly values.
114 'count_total_issues_returned_today' => 1,
116 'count_actual_state' => 1,
117 'count_precedent_state' => 1,
118 'homebranch' => 'homebranch',
119 'count_total_issues_today' => 1,
141 for my $hash ( @$array) {
143 for my $cn ( ( @column_names, 'count_actual_state') ) {
144 if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
146 if ( exists $total->{$cn} ) {
147 $total->{$cn} += $hash->{$cn} if $hash->{$cn};
149 $total->{$cn} = $hash->{$cn};
152 push @line, $hash->{$cn};
156 return ( $total, \@r );
161 Merge hashes with the same statistic column names into one
162 param: array, a arrayref of arrayrefs
167 'count_precedent_state' => '1',
168 'homebranch' => 'homebranch',
172 'count_total_issues_returned_today' => '1',
174 'homebranch' => 'homebranch',
181 'count_total_issues_returned_today' => '1',
183 'count_precedent_state' => '1',
184 'homebranch' => 'homebranch',
194 for my $h ( @array ) {
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} ) )
208 for my $cn ( @value_column_names ) {
209 next if not exists $h->{$cn};
210 $ch->{$cn} = $h->{$cn} ? $h->{$cn} : 0;
216 if ( not $exists ) {push @r, $h;}