Merge remote-tracking branch 'origin/new/bug_8382'
[koha.git] / reports / stats.screen.pl
1 #!/usr/bin/perl
2
3 # Copyright Katipo Communications 2006
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Stats;
28 use C4::Accounts;
29 use C4::Debug;
30 use Date::Manip;
31
32 my $input = new CGI;
33 my $time  = $input->param('time');
34 my $time2 = $input->param('time2');
35 my $op    = $input->param('submit');
36
37 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
38     {
39         template_name   => "reports/stats_screen.tmpl",
40         query           => $input,
41         type            => "intranet",
42         authnotrequired => 1,
43         flagsrequired   => { reports => '*' },
44         debug           => 1,
45     }
46 );
47
48 ( $time  = "today" )    if !$time;
49 ( $time2 = "tomorrow" ) if !$time2;
50
51 my $date  = ParseDate($time);
52 my $date2 = ParseDate($time2);
53 $date  = UnixDate( $date,  '%Y-%m-%d' );
54 $date2 = UnixDate( $date2, '%Y-%m-%d' );
55 $debug and warn "MASON: TIME: $time, $time2";
56 $debug and warn "MASON: DATE: $date, $date2";
57
58 # get a list of every payment
59 my @payments = TotalPaid( $date, $date2 );
60
61 my $count = @payments;
62
63 $debug and warn "MASON: number of payments=$count\n";
64
65 my $i            = 0;
66 my $totalcharges = 0;
67 my $totalcredits = 0;
68 my $totalpaid    = 0;
69 my $totalwritten = 0;
70 my @loop1;
71 my @loop2;
72
73 # lets get a a list of all individual item charges paid for by that payment
74
75 foreach my $payment (@payments) {
76
77     my @charges;
78     if ( $payment->{'type'} ne 'writeoff' ) {
79
80         @charges = getcharges(
81             $payment->{'borrowernumber'},
82             $payment->{'timestamp'},
83             $payment->{'proccode'}
84         );
85         $totalcharges++;
86         my $count = @charges;
87
88    # getting each of the charges and putting them into a array to be printed out
89    #this loops per charge per person
90         for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
91             my $hour = substr( $payment->{'timestamp'}, 8,  2 );
92             my $min  = substr( $payment->{'timestamp'}, 10, 2 );
93             my $sec  = substr( $payment->{'timestamp'}, 12, 2 );
94             my $time = "$hour:$min:$sec";
95             my $time2 = "$payment->{'date'}";
96
97   #               my $branch=Getpaidbranch($time2,$payment->{'borrowernumber'});
98             my $branch = $payment->{'branch'};
99
100             # lets build up a row
101             my %rows1 = (
102                 branch      => $branch,
103                 datetime    => $payment->{'datetime'},
104                 surname     => $payment->{'surname'},
105                 firstname   => $payment->{'firstname'},
106                 description => $charges[$i2]->{'description'},
107                 accounttype => $charges[$i2]->{'accounttype'},
108                 amount      => sprintf( "%.2f", $charges[$i2]->{'amount'} )
109                 ,    # rounding amounts to 2dp
110                 type  => $payment->{'type'},
111                 value => sprintf( "%.2f", $payment->{'value'} )
112             );       # rounding amounts to 2dp
113
114             push( @loop1, \%rows1 );
115
116         }
117             $totalpaid = $totalpaid + $payment->{'value'};
118                         $debug and warn "totalpaid = $totalpaid";               
119     }
120     else {
121         ++$totalwritten;
122     }
123
124 }
125
126 #get credits and append to the bottom of payments
127 my @credits = getcredits( $date, $date2 );
128
129 my $count = @credits;
130 my $i     = 0;
131
132 while ( $i < $count ) {
133
134     my %rows2 = (
135         creditbranch      => $credits[$i]->{'branchcode'},
136         creditdate        => $credits[$i]->{'date'},
137         creditsurname     => $credits[$i]->{'surname'},
138         creditfirstname   => $credits[$i]->{'firstname'},
139         creditdescription => $credits[$i]->{'description'},
140         creditaccounttype => $credits[$i]->{'accounttype'},
141         creditamount      => sprintf( "%.2f", $credits[$i]->{'amount'} )
142     );
143
144     push( @loop2, \%rows2 );
145     $totalcredits = $totalcredits + $credits[$i]->{'amount'};
146     $i++;    #increment the while loop
147 }
148
149 #takes off first char minus sign "-100.00"
150 $totalcredits = substr( $totalcredits, 1 );
151
152 my $totalrefunds = 0;
153 my @loop3;
154 my @refunds = getrefunds( $date, $date2 );
155 $count = @refunds;
156 $i     = 0;
157
158 while ( $i < $count ) {
159
160     my %rows3 = (
161         refundbranch      => $refunds[$i]->{'branchcode'},
162         refunddate        => $refunds[$i]->{'datetime'},
163         refundsurname     => $refunds[$i]->{'surname'},
164         refundfirstname   => $refunds[$i]->{'firstname'},
165         refunddescription => $refunds[$i]->{'description'},
166         refundaccounttype => $refunds[$i]->{'accounttype'},
167         refundamount      => sprintf( "%.2f", $refunds[$i]->{'amount'} )
168     );
169
170     push( @loop3, \%rows3 );
171     $totalrefunds = $totalrefunds + $refunds[$i]->{'amount'};
172     $i++;    #increment the while loop
173 }
174
175 my $totalcash = $totalpaid - $totalrefunds;
176
177 if ( $op eq 'To Excel' ) {
178
179     my $csv = Text::CSV_XS->new(
180         {
181             'quote_char'  => '"',
182             'escape_char' => '"',
183             'sep_char'    => ',',
184             'binary'      => 1
185         }
186     );
187
188     print $input->header(
189         -type       => 'application/vnd.ms-excel',
190         -attachment => "stats.csv",
191     );
192     print
193 "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
194
195     $DB::single = 1;
196
197     for my $row (@loop1) {
198         my @array = (
199             $row->{'branch'},      $row->{'datetime'},
200             $row->{'surname'},     $row->{'firstname'},
201             $row->{'description'}, $row->{'accounttype'},
202             $row->{'amount'},      $row->{'type'},
203             $row->{'value'}
204         );
205
206         $csv->combine(@array);
207         my $string = $csv->string(@array);
208         print $string, "\n";
209     }
210     print ",,,,,,,\n";
211     print
212 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
213
214     for my $row (@loop2) {
215
216         my @array = (
217             $row->{'creditbranch'},      $row->{'creditdate'},
218             $row->{'creditsurname'},     $row->{'creditfirstname'},
219             $row->{'creditdescription'}, $row->{'creditaccounttype'},
220             $row->{'creditamount'}
221         );
222
223         $csv->combine(@array);
224         my $string = $csv->string(@array);
225         print $string, "\n";
226     }
227     print ",,,,,,,\n";
228     print
229 "Branch, Date/time, Surname, Firstname, Description, Charge Type, Invoice Amount\n";
230
231     for my $row (@loop3) {
232         my @array = (
233             $row->{'refundbranch'},      $row->{'refunddate'},
234             $row->{'refundsurname'},     $row->{'refundfirstname'},
235             $row->{'refunddescription'}, $row->{'refundaccounttype'},
236             $row->{'refundamount'}
237         );
238
239         $csv->combine(@array);
240         my $string = $csv->string(@array);
241         print $string, "\n";
242
243     }
244
245     print ",,,,,,,\n";
246     print ",,,,,,,\n";
247     print ",,Total Amount Paid, $totalpaid\n";
248     print ",,Total Number Written, $totalwritten\n";
249     print ",,Total Amount Credits, $totalcredits\n";
250     print ",,Total Amount Refunds, $totalrefunds\n";
251 }
252 else {
253     $template->param(
254         date         => $time,
255         date2        => $time2,
256         loop1        => \@loop1,
257         loop2        => \@loop2,
258         loop3        => \@loop3,
259         totalpaid    => $totalpaid,
260         totalcredits => $totalcredits,
261         totalwritten => $totalwritten,
262         totalrefund  => $totalrefunds,
263         totalcash    => $totalcash,
264         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
265     );
266     output_html_with_http_headers $input, $cookie, $template->output;
267 }
268