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