Bug 19080: Handle non-existing patrons gratefully
[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 => 1 },
44         debug           => 1,
45     }
46 );
47
48 my $borrowernumber = $input->param('borrowernumber');
49
50 # Set informations for the patron
51 my $patron = Koha::Patrons->find( $borrowernumber );
52 unless ( $patron ) {
53     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
54     exit;
55 }
56
57 my $category = $patron->category;
58 my $borrower= $patron->unblessed;
59 $borrower->{description} = $category->description;
60 $borrower->{category_type} = $category->category_type;
61
62 foreach my $key ( keys %$borrower ) {
63     $template->param( $key => $borrower->{$key} );
64 }
65 $template->param(
66     categoryname    => $borrower->{'description'},
67 );
68 # Construct column names
69 my $fields = C4::Members::Statistics::get_fields();
70 our @statistic_column_names = split '\|', $fields;
71 our @value_column_names = ( 'count_precedent_state', 'count_total_issues_today', 'count_total_issues_returned_today' );
72 our @column_names = ( @statistic_column_names, @value_column_names );
73
74 # Get statistics
75 my $precedent_state = GetPrecedentStateByBorrower( $borrowernumber );
76 my $total_issues_today = GetTotalIssuesTodayByBorrower( $borrowernumber );
77 my $total_issues_returned_today = GetTotalIssuesReturnedTodayByBorrower( $borrowernumber );
78 my $r = merge (
79     @$precedent_state, @$total_issues_today, @$total_issues_returned_today
80 );
81
82 add_actual_state( $r );
83 my ( $total, $datas ) = build_array( $r );
84
85 # Gettings sums
86 my $count_total_precedent_state = $total->{count_precedent_state} || 0;
87 my $count_total_issues = $total->{count_total_issues_today} || 0;
88 my $count_total_issues_returned = $total->{count_total_issues_returned_today} || 0;
89 my $count_total_actual_state = ($count_total_precedent_state - $count_total_issues_returned + $count_total_issues);
90
91 if (C4::Context->preference('ExtendedPatronAttributes')) {
92     my $attributes = GetBorrowerAttributes($borrowernumber);
93     $template->param(
94         ExtendedPatronAttributes => 1,
95         extendedattributes => $attributes
96     );
97 }
98
99 $template->param( picture => 1 ) if $patron->image;
100
101 $template->param(%$borrower);
102
103 $template->param( adultborrower => 1 ) if ( $borrower->{category_type} eq 'A' || $borrower->{category_type} eq 'I' );
104
105 $template->param(
106     statisticsview     => 1,
107     datas              => $datas,
108     column_names       => \@statistic_column_names,
109     count_total_issues => $count_total_issues,
110     count_total_issues_returned => $count_total_issues_returned,
111     count_total_precedent_state => $count_total_precedent_state,
112     count_total_actual_state => $count_total_actual_state,
113     RoutingSerials => C4::Context->preference('RoutingSerials'),
114 );
115
116 output_html_with_http_headers $input, $cookie, $template->output;
117
118
119 =head1 FUNCTIONS
120
121 =head2 add_actual_state
122
123   Add a 'count_actual_state' key in all hashes
124   count_actual_state = count_precedent_state - count_total_issues_returned_today + count_total_issues_today
125
126 =cut
127
128 sub add_actual_state {
129     my ( $array ) = @_;
130     for my $hash ( @$array ) {
131         $hash->{count_actual_state} = ( $hash->{count_precedent_state} // 0 ) - ( $hash->{count_total_issues_returned_today} // 0 ) + ( $hash->{count_total_issues_today} // 0 );
132     }
133 }
134
135 =head2 build_array
136
137   Build a new array containing values of hashes.
138   It used by template whitch display silly values.
139   ex:
140     $array = [
141       {
142         'count_total_issues_returned_today' => 1,
143         'ccode' => 'ccode',
144         'count_actual_state' => 1,
145         'count_precedent_state' => 1,
146         'homebranch' => 'homebranch',
147         'count_total_issues_today' => 1,
148         'itype' => 'itype'
149       }
150     ];
151   and returns:
152     [
153       [
154         'homebranch',
155         'itype',
156         'ccode',
157         1,
158         1,
159         1,
160         1
161       ]
162     ];
163
164 =cut
165
166 sub build_array {
167     my ( $array ) = @_;
168     my ( @r, $total );
169     for my $hash ( @$array) {
170         my @line;
171         for my $cn ( ( @column_names, 'count_actual_state') ) {
172             if ( grep /$cn/, ( @value_column_names, 'count_actual_state') ) {
173                 $hash->{$cn} //= 0;
174                 if ( exists $total->{$cn} ) {
175                     $total->{$cn} += $hash->{$cn} if $hash->{$cn};
176                 } else {
177                     $total->{$cn} = $hash->{$cn};
178                 }
179             }
180             push @line, $hash->{$cn};
181         }
182         push @r, \@line;
183     }
184     return ( $total, \@r );
185 }
186
187 =head2 merge
188
189   Merge hashes with the same statistic column names into one
190   param: array, a arrayref of arrayrefs
191   ex:
192   @array = (
193      {
194        'ccode' => 'ccode',
195        'count_precedent_state' => '1',
196        'homebranch' => 'homebranch',
197        'itype' => 'itype'
198      },
199      {
200        'count_total_issues_returned_today' => '1',
201        'ccode' => 'ccode',
202        'homebranch' => 'homebranch',
203        'itype' => 'itype'
204      }
205    );
206    and returns:
207    [
208      {
209        'count_total_issues_returned_today' => '1',
210        'ccode' => 'ccode',
211        'count_precedent_state' => '1',
212        'homebranch' => 'homebranch',
213        'itype' => 'itype'
214      }
215    ];
216
217 =cut
218
219 sub merge {
220     my @array = @_;
221     my @r;
222     for my $h ( @array ) {
223         my $exists = 0;
224         for my $ch ( @r ) {
225             $exists = 1;
226             for my $cn ( @statistic_column_names ) {
227                 if ( $ch->{$cn} and not $ch->{$cn} eq $h->{$cn} ) {
228                     $exists = 0;
229                     last;
230                 }
231             }
232             if ($exists){
233                 for my $cn ( @value_column_names ) {
234                     next if not exists $h->{$cn};
235                     $ch->{$cn} = $h->{$cn} ? $h->{$cn} : 0;
236                 }
237                 last;
238             }
239         }
240
241         if ( not $exists ) {push @r, $h;}
242     }
243     return \@r;
244 }