Bug 3813: SIP2, Bad Patron Information Response to Message 64
[koha.git] / reports / stats.print.pl
blob1d89520fc07fa59b9ce84abf097c1c9bfc9c71b2
1 #!/usr/bin/perl
3 use strict;
4 use CGI;
5 use C4::Output;
7 use C4::Auth;
8 use C4::Context;
9 use Date::Manip;
10 use C4::Stats;
11 use Text::CSV_XS;
12 &Date_Init("DateFormat=non-US"); # set non-USA date, eg:19/08/2005
14 my $csv = Text::CSV_XS->new(
16 'quote_char' => '"',
17 'escape_char' => '"',
18 'sep_char' => ',',
19 'binary' => 1
23 my $input=new CGI;
24 my $time=$input->param('time');
25 my $time2=$input->param('time2');
27 my @loop1;
28 my @loop2;
29 my $date;
30 my $date2;
31 if ($time eq 'yesterday'){
32 $date=ParseDate('yesterday');
33 $date2=ParseDate('today');
35 if ($time eq 'today'){
36 $date=ParseDate('today');
37 $date2=ParseDate('tomorrow');
39 if ($time eq 'daybefore'){
40 $date=ParseDate('2 days ago');
41 $date2=ParseDate('yesterday');
43 if ($time eq 'month') {
44 $date = ParseDate('1 month ago');
45 $date2 = ParseDate('today');
48 if ($time=~ /\//){
49 $date=ParseDate($time);
50 $date2=ParseDateDelta('+ 1 day');
51 $date2=DateCalc($date,$date2);
54 if ($time eq ''){
55 $date=ParseDate('today');
56 $date2=ParseDate('tomorrow');
59 if ($time2 ne ''){
60 $date=ParseDate($time);
61 $date2=ParseDate($time2);
64 my $date=UnixDate($date,'%Y-%m-%d');
65 my $date2=UnixDate($date2,'%Y-%m-%d');
67 #warn "MASON: DATE: $date, $date2";
69 #get a list of every payment
70 my @payments=TotalPaid($date,$date2);
72 my $count=@payments;
73 # print "MASON: number of payments=$count\n";
75 my $i=0;
76 my $totalcharges=0;
77 my $totalcredits=0;
78 my $totalpaid=0;
79 my $totalwritten=0;
81 # lets get a a list of all individual item charges paid for by that payment
82 while ($i<$count ){
84 my $count;
85 my @charges;
87 if ($payments[$i]{'type'} ne 'writeoff'){ # lets ignore writeoff payments!.
88 @charges=getcharges($payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'}, $payments[$i]{'proccode'});
89 $totalcharges++;
90 $count=@charges;
92 # getting each of the charges and putting them into a array to be printed out
93 #this loops per charge per person
94 for (my $i2=0;$i2<$count;$i2++){
96 my $hour=substr($payments[$i]{'timestamp'},8,2);
97 my $min=substr($payments[$i]{'timestamp'},10,2);
98 my $sec=substr($payments[$i]{'timestamp'},12,2);
99 my $time="$hour:$min:$sec";
100 my $time2="$payments[$i]{'date'}";
101 # my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
102 my $branch=$payments[$i]{'branch'};
104 my @rows1 = ($branch, # lets build up a row
105 $payments[$i]->{'datetime'},
106 $payments[$i]->{'surname'},
107 $payments[$i]->{'firstname'},
108 $charges[$i2]->{'description'},
109 $charges[$i2]->{'accounttype'},
110 # rounding amounts to 2dp and adding dollar sign to make excel read it as currency format
111 "\$".sprintf("%.2f", $charges[$i2]->{'amount'}),
112 $payments[$i]->{'type'},
113 "\$".$payments[$i]->{'value'});
115 push (@loop1, \@rows1);
116 $totalpaid = $totalpaid + $payments[$i]->{'value'};
118 } else {
119 ++$totalwritten;
122 $i++; #increment the while loop
125 #get credits and append to the bottom of payments
126 my @credits=getcredits($date,$date2);
128 my $count=@credits;
129 my $i=0;
131 while ($i<$count ){
133 my @rows2 = ($credits[$i]->{'branchcode'},
134 $credits[$i]->{'date'},
135 $credits[$i]->{'surname'},
136 $credits[$i]->{'firstname'},
137 $credits[$i]->{'description'},
138 $credits[$i]->{'accounttype'},
139 "\$".$credits[$i]->{'amount'});
141 push (@loop2, \@rows2);
142 $totalcredits = $totalcredits + $credits[$i]->{'amount'};
143 $i++;
146 #takes off first char minus sign "-100.00"
147 $totalcredits = substr($totalcredits, 1);
149 print $input->header(
150 -type => 'application/vnd.ms-excel',
151 -attachment => "stats.csv",
153 print "Branch, Datetime, Surname, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";
156 for my $row ( @loop1 ) {
158 $csv->combine(@$row);
159 my $string = $csv->string;
160 print $string, "\n";
163 print ",,,,,,,\n";
165 for my $row ( @loop2 ) {
167 $csv->combine(@$row);
168 my $string = $csv->string;
169 print $string, "\n";
172 print ",,,,,,,\n";
173 print ",,,,,,,\n";
174 print ",,Total Amount Paid, $totalpaid\n";
175 print ",,Total Number Written, $totalwritten\n";
176 print ",,Total Amount Credits, $totalcredits\n";