Merge branch '3.14.x'
[koha.git] / reports / stats.print.pl
blob066aafa4b6a843e227b6cfd7325a3277b5b23840
1 #!/usr/bin/perl
3 use strict;
4 #use warnings; FIXME - Bug 2505
5 use CGI;
6 use C4::Output;
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
15 my $csv = Text::CSV_XS->new(
17 'quote_char' => '"',
18 'escape_char' => '"',
19 'sep_char' => ',',
20 'binary' => 1
24 my $input=new CGI;
25 my $time=$input->param('time');
26 my $time2=$input->param('time2');
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');
36 if ($time eq 'today'){
37 $date=ParseDate('today');
38 $date2=ParseDate('tomorrow');
40 if ($time eq 'daybefore'){
41 $date=ParseDate('2 days ago');
42 $date2=ParseDate('yesterday');
44 if ($time eq 'month') {
45 $date = ParseDate('1 month ago');
46 $date2 = ParseDate('today');
49 if ($time=~ /\//){
50 $date=ParseDate($time);
51 $date2=ParseDateDelta('+ 1 day');
52 $date2=DateCalc($date,$date2);
55 if ($time eq ''){
56 $date=ParseDate('today');
57 $date2=ParseDate('tomorrow');
60 if ($time2 ne ''){
61 $date=ParseDate($time);
62 $date2=ParseDate($time2);
65 my $date=UnixDate($date,'%Y-%m-%d');
66 my $date2=UnixDate($date2,'%Y-%m-%d');
68 #warn "MASON: DATE: $date, $date2";
70 #get a list of every payment
71 my @payments=TotalPaid($date,$date2);
73 my $count=@payments;
74 # print "MASON: number of payments=$count\n";
76 my $i=0;
77 my $totalcharges=0;
78 my $totalcredits=0;
79 my $totalpaid=0;
80 my $totalwritten=0;
82 # lets get a a list of all individual item charges paid for by that payment
83 while ($i<$count ){
85 my $count;
86 my @charges;
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;
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++){
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'};
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'});
116 push (@loop1, \@rows1);
117 $totalpaid = $totalpaid + $payments[$i]->{'value'};
119 } else {
120 ++$totalwritten;
123 $i++; #increment the while loop
126 #get credits and append to the bottom of payments
127 my @credits=getcredits($date,$date2);
129 my $count=@credits;
130 my $i=0;
132 while ($i<$count ){
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'});
142 push (@loop2, \@rows2);
143 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
144 $i++;
147 #takes off first char minus sign "-100.00"
148 $totalcredits = substr($totalcredits, 1);
150 print $input->header(
151 -type => 'application/vnd.ms-excel',
152 -attachment => "stats.csv",
154 print "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
157 for my $row ( @loop1 ) {
159 $csv->combine(@$row);
160 my $string = $csv->string;
161 print $string, "\n";
164 print ",,,,,,,\n";
166 for my $row ( @loop2 ) {
168 $csv->combine(@$row);
169 my $string = $csv->string;
170 print $string, "\n";
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";