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