Bug 18403: Add sub output_and_exit_if_error - unknown_patron & cannot_see_patron_infos
[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 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.
10 #
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.
14 #
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.
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;
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;
35
36 my $input = new CGI;
37
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,
45     }
46 );
47
48 my $borrowernumber = $input->param('borrowernumber');
49
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 } );
53
54 my $category = $patron->category;
55 my $borrower= $patron->unblessed;
56 $borrower->{description} = $category->description;
57 $borrower->{category_type} = $category->category_type;
58
59 $template->param(
60     categoryname    => $borrower->{'description'},
61 );
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 );
67
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
74 );
75
76 add_actual_state( $r );
77 my ( $total, $datas ) = build_array( $r );
78
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);
84
85 if (C4::Context->preference('ExtendedPatronAttributes')) {
86     my $attributes = GetBorrowerAttributes($borrowernumber);
87     $template->param(
88         ExtendedPatronAttributes => 1,
89         extendedattributes => $attributes
90     );
91 }
92
93 $template->param( picture => 1 ) if $patron->image;
94
95 $template->param(%$borrower);
96
97 $template->param( adultborrower => 1 ) if ( $borrower->{category_type} eq 'A' || $borrower->{category_type} eq 'I' );
98
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,
107 );
108
109 output_html_with_http_headers $input, $cookie, $template->output;
110
111
112 =head1 FUNCTIONS
113
114 =head2 add_actual_state
115
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
118
119 =cut
120
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 );
125     }
126 }
127
128 =head2 build_array
129
130   Build a new array containing values of hashes.
131   It used by template whitch display silly values.
132   ex:
133     $array = [
134       {
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'
142       }
143     ];
144   and returns:
145     [
146       [
147         'homebranch',
148         'itype',
149         'ccode',
150         1,
151         1,
152         1,
153         1
154       ]
155     ];
156
157 =cut
158
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};
171                 }
172             }
173             push @line, $hash->{$cn};
174         }
175         push @r, \@line;
176     }
177     return ( $total, \@r );
178 }
179
180 =head2 merge
181
182   Merge hashes with the same statistic column names into one
183   param: array, a arrayref of arrayrefs
184   ex:
185   @array = (
186      {
187        'ccode' => 'ccode',
188        'count_precedent_state' => '1',
189        'homebranch' => 'homebranch',
190        'itype' => 'itype'
191      },
192      {
193        'count_total_issues_returned_today' => '1',
194        'ccode' => 'ccode',
195        'homebranch' => 'homebranch',
196        'itype' => 'itype'
197      }
198    );
199    and returns:
200    [
201      {
202        'count_total_issues_returned_today' => '1',
203        'ccode' => 'ccode',
204        'count_precedent_state' => '1',
205        'homebranch' => 'homebranch',
206        'itype' => 'itype'
207      }
208    ];
209
210 =cut
211
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;
223                 }
224             }
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;
229                 }
230                 last;
231             }
232         }
233
234         if ( not $exists ) {push @r, $h;}
235     }
236     return \@r;
237 }