Bug 6934: Fix code in CashRegisterStats (dataTables pagination, more accurate descrip...
[koha.git] / reports / stats.print.pl
1 #!/usr/bin/perl
2
3 use strict;
4 #use warnings; FIXME - Bug 2505
5 use CGI qw ( -utf8 );
6 use C4::Output;
7
8 use C4::Auth;
9 use C4::Context;
10 use Date::Manip;
11 use C4::Stats;
12 use Text::CSV_XS;
13 &Date_Init("DateFormat=non-US"); # set non-USA date, eg:19/08/2005
14
15 my $csv = Text::CSV_XS->new(
16     {
17         'quote_char'  => '"',
18         'escape_char' => '"',
19         'sep_char'    => ',',
20         'binary'      => 1
21     }
22 );
23
24 my $input=new CGI;
25 my $time=$input->param('time');
26 my $time2=$input->param('time2');
27
28 my @loop1;
29 my @loop2;
30 my $date;
31 my $date2;
32 if ($time eq 'yesterday'){
33         $date=ParseDate('yesterday');
34         $date2=ParseDate('today');
35 }
36 if ($time eq 'today'){
37         $date=ParseDate('today');
38         $date2=ParseDate('tomorrow');
39 }
40 if ($time eq 'daybefore'){
41         $date=ParseDate('2 days ago');
42         $date2=ParseDate('yesterday');
43 }
44 if ($time eq 'month') {
45         $date = ParseDate('1 month ago');
46         $date2 = ParseDate('today');
47
48 }
49 if ($time=~ /\//){
50         $date=ParseDate($time);
51         $date2=ParseDateDelta('+ 1 day');
52         $date2=DateCalc($date,$date2);
53 }
54
55 if ($time eq ''){
56         $date=ParseDate('today');
57         $date2=ParseDate('tomorrow');
58 }
59
60 if ($time2 ne ''){
61             $date=ParseDate($time);
62             $date2=ParseDate($time2);
63 }
64
65 my $date=UnixDate($date,'%Y-%m-%d');
66 my $date2=UnixDate($date2,'%Y-%m-%d');
67
68 #warn "MASON: DATE: $date, $date2";
69
70 #get a list of every payment
71 my @payments=TotalPaid($date,$date2);
72
73 my $count=@payments;
74 # print "MASON: number of payments=$count\n";
75
76 my $i=0;
77 my $totalcharges=0;
78 my $totalcredits=0;
79 my $totalpaid=0;
80 my $totalwritten=0;
81
82 # lets get a a list of all individual item charges paid for by that payment
83 while ($i<$count ){
84
85        my $count;
86        my @charges;
87
88        if ($payments[$i]{'type'} ne 'writeoff'){         # lets ignore writeoff payments!.
89            @charges=getcharges($payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'}, $payments[$i]{'proccode'});
90            $totalcharges++;
91            $count=@charges;
92
93            # getting each of the charges and putting them into a array to be printed out
94            #this loops per charge per person
95            for (my $i2=0;$i2<$count;$i2++){
96
97                my $hour=substr($payments[$i]{'timestamp'},8,2);
98                my $min=substr($payments[$i]{'timestamp'},10,2);
99                my $sec=substr($payments[$i]{'timestamp'},12,2);
100                my $time="$hour:$min:$sec";
101                my $time2="$payments[$i]{'date'}";
102 #               my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
103                my $branch=$payments[$i]{'branch'};
104
105                my @rows1 = ($branch,          # lets build up a row
106                             $payments[$i]->{'datetime'},
107                             $payments[$i]->{'surname'},
108                             $payments[$i]->{'firstname'},
109                             $charges[$i2]->{'description'},
110                             $charges[$i2]->{'accounttype'},
111    # rounding amounts to 2dp and adding dollar sign to make excel read it as currency format
112                             "\$".sprintf("%.2f", $charges[$i2]->{'amount'}), 
113                             $payments[$i]->{'type'},
114                             "\$".$payments[$i]->{'value'});
115
116                push (@loop1, \@rows1);
117                $totalpaid = $totalpaid + $payments[$i]->{'value'};
118            }
119        } else {
120          ++$totalwritten;
121        }
122
123        $i++; #increment the while loop
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 = ($credits[$i]->{'branchcode'},
135                     $credits[$i]->{'date'},
136                     $credits[$i]->{'surname'},
137                     $credits[$i]->{'firstname'},
138                     $credits[$i]->{'description'},
139                     $credits[$i]->{'accounttype'},
140                     "\$".$credits[$i]->{'amount'});
141
142        push (@loop2, \@rows2);
143        $totalcredits = $totalcredits + $credits[$i]->{'amount'};
144        $i++;
145 }
146
147 #takes off first char minus sign "-100.00"
148 $totalcredits = substr($totalcredits, 1);
149
150 print $input->header(
151     -type       => 'application/vnd.ms-excel',
152     -attachment => "stats.csv",
153 );
154 print "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
155
156
157 for my $row ( @loop1 ) {
158
159     $csv->combine(@$row);
160     my $string = $csv->string;
161     print $string, "\n";
162 }
163
164 print ",,,,,,,\n";
165
166 for my $row ( @loop2 ) {
167
168     $csv->combine(@$row);
169     my $string = $csv->string;
170     print $string, "\n";
171 }
172
173 print ",,,,,,,\n";
174 print ",,,,,,,\n";
175 print ",,Total Amount Paid, $totalpaid\n";
176 print ",,Total Number Written, $totalwritten\n";
177 print ",,Total Amount Credits, $totalcredits\n";
178