Bug 20034: Switch single-column templates to Bootstrap grid: Circulation
[koha.git] / members / statistics.pl
blob616449603ad1a06db548747aae34f44bc4ce44af
1 #!/usr/bin/perl
3 # Copyright 2012 BibLibre
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along with
16 # Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 =head1 members/statistics.pl
21 Generate statistic issues for a member
23 =cut
25 use Modern::Perl;
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::Members::Attributes qw(GetBorrowerAttributes);
33 use C4::Output;
34 use Koha::Patrons;
36 my $input = new CGI;
38 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
39 { template_name => "members/statistics.tt",
40 query => $input,
41 type => "intranet",
42 authnotrequired => 0,
43 flagsrequired => { borrowers => 'edit_borrowers' },
44 debug => 1,
48 my $borrowernumber = $input->param('borrowernumber');
50 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
51 my $patron = Koha::Patrons->find( $borrowernumber );
52 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
54 my $category = $patron->category;
55 my $borrower= $patron->unblessed;
56 $borrower->{description} = $category->description;
57 $borrower->{category_type} = $category->category_type;
59 $template->param(
60 categoryname => $borrower->{'description'},
62 # Construct column names
63 my $fields = C4::Members::Statistics::get_fields();
64 our @statistic_column_names = split '\|', $fields;
65 our @value_column_names = ( 'count_precedent_state', 'count_total_issues_today', 'count_total_issues_returned_today' );
66 our @column_names = ( @statistic_column_names, @value_column_names );
68 # Get statistics
69 my $precedent_state = GetPrecedentStateByBorrower( $borrowernumber );
70 my $total_issues_today = GetTotalIssuesTodayByBorrower( $borrowernumber );
71 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower( $borrowernumber );
72 my $r = merge (
73 @$precedent_state, @$total_issues_today, @$total_issues_returned_today
76 add_actual_state( $r );
77 my ( $total, $datas ) = build_array( $r );
79 # Gettings sums
80 my $count_total_precedent_state = $total->{count_precedent_state} || 0;
81 my $count_total_issues = $total->{count_total_issues_today} || 0;
82 my $count_total_issues_returned = $total->{count_total_issues_returned_today} || 0;
83 my $count_total_actual_state = ($count_total_precedent_state - $count_total_issues_returned + $count_total_issues);
85 if (C4::Context->preference('ExtendedPatronAttributes')) {
86 my $attributes = GetBorrowerAttributes($borrowernumber);
87 $template->param(
88 ExtendedPatronAttributes => 1,
89 extendedattributes => $attributes
93 $template->param( picture => 1 ) if $patron->image;
95 $template->param(%$borrower);
97 $template->param( adultborrower => 1 ) if ( $borrower->{category_type} eq 'A' || $borrower->{category_type} eq 'I' );
99 $template->param(
100 statisticsview => 1,
101 datas => $datas,
102 column_names => \@statistic_column_names,
103 count_total_issues => $count_total_issues,
104 count_total_issues_returned => $count_total_issues_returned,
105 count_total_precedent_state => $count_total_precedent_state,
106 count_total_actual_state => $count_total_actual_state,
109 output_html_with_http_headers $input, $cookie, $template->output;
112 =head1 FUNCTIONS
114 =head2 add_actual_state
116 Add a 'count_actual_state' key in all hashes
117 count_actual_state = count_precedent_state - count_total_issues_returned_today + count_total_issues_today
119 =cut
121 sub add_actual_state {
122 my ( $array ) = @_;
123 for my $hash ( @$array ) {
124 $hash->{count_actual_state} = ( $hash->{count_precedent_state} // 0 ) - ( $hash->{count_total_issues_returned_today} // 0 ) + ( $hash->{count_total_issues_today} // 0 );
128 =head2 build_array
130 Build a new array containing values of hashes.
131 It used by template whitch display silly values.
133 $array = [
135 'count_total_issues_returned_today' => 1,
136 'ccode' => 'ccode',
137 'count_actual_state' => 1,
138 'count_precedent_state' => 1,
139 'homebranch' => 'homebranch',
140 'count_total_issues_today' => 1,
141 'itype' => 'itype'
144 and returns:
147 'homebranch',
148 'itype',
149 'ccode',
157 =cut
159 sub build_array {
160 my ( $array ) = @_;
161 my ( @r, $total );
162 for my $hash ( @$array) {
163 my @line;
164 for my $cn ( ( @column_names, 'count_actual_state') ) {
165 if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
166 $hash->{$cn} //= 0;
167 if ( exists $total->{$cn} ) {
168 $total->{$cn} += $hash->{$cn} if $hash->{$cn};
169 } else {
170 $total->{$cn} = $hash->{$cn};
173 push @line, $hash->{$cn};
175 push @r, \@line;
177 return ( $total, \@r );
180 =head2 merge
182 Merge hashes with the same statistic column names into one
183 param: array, a arrayref of arrayrefs
185 @array = (
187 'ccode' => 'ccode',
188 'count_precedent_state' => '1',
189 'homebranch' => 'homebranch',
190 'itype' => 'itype'
193 'count_total_issues_returned_today' => '1',
194 'ccode' => 'ccode',
195 'homebranch' => 'homebranch',
196 'itype' => 'itype'
199 and returns:
202 'count_total_issues_returned_today' => '1',
203 'ccode' => 'ccode',
204 'count_precedent_state' => '1',
205 'homebranch' => 'homebranch',
206 'itype' => 'itype'
210 =cut
212 sub merge {
213 my @array = @_;
214 my @r;
215 for my $h ( @array ) {
216 my $exists = 0;
217 for my $ch ( @r ) {
218 $exists = 1;
219 for my $cn ( @statistic_column_names ) {
220 if ( $ch->{$cn} and not $ch->{$cn} eq $h->{$cn} ) {
221 $exists = 0;
222 last;
225 if ($exists){
226 for my $cn ( @value_column_names ) {
227 next if not exists $h->{$cn};
228 $ch->{$cn} = $h->{$cn} ? $h->{$cn} : 0;
230 last;
234 if ( not $exists ) {push @r, $h;}
236 return \@r;