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' },
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 );
62 my $precedent_state = GetPrecedentStateByBorrower
( $borrowernumber );
63 my $total_issues_today = GetTotalIssuesTodayByBorrower
( $borrowernumber );
64 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower
( $borrowernumber );
66 @
$precedent_state, @
$total_issues_today, @
$total_issues_returned_today
69 add_actual_state
( $r );
70 my ( $total, $datas ) = build_array
( $r );
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);
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;
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
101 sub add_actual_state
{
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 );
110 Build a new array containing values of hashes.
111 It used by template whitch display silly values.
115 'count_total_issues_returned_today' => 1,
117 'count_actual_state' => 1,
118 'count_precedent_state' => 1,
119 'homebranch' => 'homebranch',
120 'count_total_issues_today' => 1,
142 for my $hash ( @
$array) {
144 for my $cn ( ( @column_names, 'count_actual_state') ) {
145 if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
147 if ( exists $total->{$cn} ) {
148 $total->{$cn} += $hash->{$cn} if $hash->{$cn};
150 $total->{$cn} = $hash->{$cn};
153 push @line, $hash->{$cn};
157 return ( $total, \
@r );
162 Merge hashes with the same statistic column names into one
163 param: array, a arrayref of arrayrefs
168 'count_precedent_state' => '1',
169 'homebranch' => 'homebranch',
173 'count_total_issues_returned_today' => '1',
175 'homebranch' => 'homebranch',
182 'count_total_issues_returned_today' => '1',
184 'count_precedent_state' => '1',
185 'homebranch' => 'homebranch',
195 for my $h ( @array ) {
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} ) )
209 for my $cn ( @value_column_names ) {
210 next if not exists $h->{$cn};
211 $ch->{$cn} = $h->{$cn} ?
$h->{$cn} : 0;
217 if ( not $exists ) {push @r, $h;}