HLT mod for till reconciliation, opens to csv'ed excel.
[koha.git] / stats.print.pl
1 #!/usr/bin/perl\r
2 \r
3 #things to do\r
4 \r
5 # First sort by branch\r
6 #Then sort by surname\r
7 \r
8 #_Branch_:  Could we have Levin displaying as L, please, not C__\r
9 \r
10 #_Totals_ :\r
11 #*Total Paid *\r
12 #*Total written off*\r
13 #*Total credits (which will include manual credits and credits for lost books returned*\r
14 \r
15 #use strict;\r
16 use CGI;\r
17 use C4::Output;\r
18 use HTML::Template;\r
19 use C4::Auth;\r
20 use C4::Interface::CGI::Output;\r
21 use C4::Context;\r
22 use Date::Manip;\r
23 use C4::Stats;\r
24 use Data::Dumper;\r
25 \r
26 use Text::CSV_XS;\r
27 \r
28 my $csv = Text::CSV_XS->new(\r
29     {\r
30         'quote_char'   => '"',\r
31         'escape_char'  => '"',\r
32         'sep_char'     => ',',\r
33         'binary'       => 1,\r
34         'always_quote' => 1,\r
35     }\r
36 );\r
37 \r
38 my $input=new CGI;\r
39 \r
40 #my $time=$input->param('time');\r
41 #my $time="month";\r
42 #my $time="today";\r
43 \r
44 my $date;\r
45 my $date2;\r
46 if ($time eq 'yesterday'){\r
47         $date=ParseDate('yesterday');\r
48         $date2=ParseDate('today');\r
49 }\r
50 if ($time eq 'today'){\r
51         $date=ParseDate('today');\r
52         $date2=ParseDate('tomorrow');\r
53 }\r
54 if ($time eq 'daybefore'){\r
55         $date=ParseDate('2 days ago');\r
56         $date2=ParseDate('yesterday');\r
57 }\r
58 if ($time eq 'month') {\r
59         $date = ParseDate('1 month ago');\r
60         $date2 = ParseDate('today');\r
61 \r
62 }\r
63 if ($time=~ /\//){\r
64         $date=ParseDate($time);\r
65         $date2=ParseDateDelta('+ 1 day');\r
66         $date2=DateCalc($date,$date2);\r
67 }\r
68 \r
69 #my $date=UnixDate($date,'%Y-%m-%d');\r
70 #my $date2=UnixDate($date2,'%Y-%m-%d');\r
71 \r
72 my $date="2005-08-19";\r
73 my $date2="2005-08-20";\r
74 \r
75 #my $date="2005-01-05";\r
76 #my $date2="2005-01-06";\r
77 \r
78 #get a list of every payment\r
79 my @payments=TotalPaid($date,$date2);\r
80 \r
81 my $count=@payments;\r
82 # print "MASON: number of payments=$count\n";\r
83 \r
84 my $i=0;\r
85 my $totalcharges=0;\r
86 my $totalcredits=0;\r
87 my $totalpaid=0;\r
88 my $totalwritten=0;\r
89 \r
90 # lets get a a list of all individual item charges paid for by that payment\r
91 while ($i<$count ){\r
92 \r
93        my $count;\r
94        my @charges;\r
95 \r
96        if ($payments[$i]{'type'} ne 'writeoff'){         # lets ignore writeoff payments!.\r
97            @charges=getcharges($payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'}, $payments[$i]{'proccode'});\r
98            $totalcharges++;\r
99            $count=@charges;\r
100            # getting each of the charges and putting them into a array to be printed out\r
101            #this loops per charge per person\r
102            for (my $i2=0;$i2<$count;$i2++){\r
103                my $hour=substr($payments[$i]{'timestamp'},8,2);\r
104                my $min=substr($payments[$i]{'timestamp'},10,2);\r
105                my $sec=substr($payments[$i]{'timestamp'},12,2);\r
106                my $time="$hour:$min:$sec";\r
107                my $time2="$payments[$i]{'date'}";\r
108                my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});\r
109                my @rows1 = ($branch,          # lets build up a row\r
110                             $payments[$i]->{'datetime'},\r
111                             $payments[$i]->{'surname'},\r
112                             $payments[$i]->{'firstname'},\r
113                             $charges[$i2]->{'description'},\r
114                             $charges[$i2]->{'accounttype'},\r
115                             sprintf("%.2f", $charges[$i2]->{'amount'}), # rounding amounts to 2dp\r
116                             $payments[$i]->{'type'},\r
117                             $payments[$i]->{'value'});\r
118                push (@loop1, \@rows1);\r
119            }\r
120        } else {\r
121          ++$totalwritten;\r
122        }\r
123        $i++; #increment the while loop\r
124        $totalpaid = $totalpaid + $payments[$i]->{'value'};\r
125 }\r
126 \r
127 \r
128 \r
129 #get credits and append to the bottom of payments\r
130 my @credits=getcredits($date,$date2);\r
131 \r
132 #print Dumper(@credits);\r
133 \r
134 my $count=@credits;\r
135 my $i=0;\r
136 \r
137 while ($i<$count ){\r
138 \r
139        my @rows2 = ($credits[$i]->{'branchcode'},\r
140                     $credits[$i]->{'date'},\r
141                     $credits[$i]->{'surname'},\r
142                     $credits[$i]->{'firstname'},\r
143                     $credits[$i]->{'description'},\r
144                     $credits[$i]->{'accounttype'},\r
145                     $credits[$i]->{'amount'});\r
146 \r
147        push (@loop2, \@rows2);\r
148        $i++; #increment the while loop\r
149        $totalcredits = $totalcredits + $credits[$i]->{'amount'};\r
150        ;\r
151 \r
152 }\r
153 #takes off first char minus sign "-100.00"\r
154 \r
155 \r
156 $totalcredits = substr($totalcredits, 1);\r
157 \r
158 \r
159 print $input->header(\r
160     -type       => 'application/vnd.ms-excel',\r
161     -attachment => "moo.csv",\r
162 );\r
163 print "Branch, Datetime, Surame, Firstnames, Description, Type, Invoice amount, Payment type, Payment Amount\n";\r
164 \r
165 \r
166 for my $row ( @loop1 ) {\r
167 \r
168     $csv->combine(@$row);\r
169     my $string = $csv->string;\r
170     print $string, "\n";\r
171 }\r
172 \r
173 print ",,,,,,,\n";\r
174 \r
175 for my $row ( @loop2 ) {\r
176 \r
177     $csv->combine(@$row);\r
178     my $string = $csv->string;\r
179     print $string, "\n";\r
180 }\r
181 \r
182 print ",,,,,,,\n";\r
183 print ",,,,,,,\n";\r
184 print ",,Total Amount Paid, $totalpaid\n";\r
185 print ",,Total Number Written, $totalwritten\n";\r
186 print ",,Total Amount Credits, $totalcredits\n";\r
187 \r