HLT mod for till reconciliation.
[koha.git] / stats.screen.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 \r
41 my $input=new CGI;\r
42 my $time=$input->param('time');\r
43 \r
44 my ($template, $loggedinuser, $cookie)\r
45     = get_template_and_user({template_name => "stats.screen.tmpl",\r
46                              query => $input,\r
47                              type => "intranet",\r
48                              authnotrequired => 1,\r
49                              flagsrequired => {borrowers => 1},\r
50                              debug => 1,\r
51                              });\r
52 \r
53 \r
54 \r
55 #my $time=$input->param('time');\r
56 #my $time="month";\r
57 #my $time="today";\r
58 \r
59 my $date;\r
60 my $date2;\r
61 if ($time eq 'yesterday'){\r
62         $date=ParseDate('yesterday');\r
63         $date2=ParseDate('today');\r
64 }\r
65 if ($time eq 'today'){\r
66         $date=ParseDate('today');\r
67         $date2=ParseDate('tomorrow');\r
68 }\r
69 if ($time eq 'daybefore'){\r
70         $date=ParseDate('2 days ago');\r
71         $date2=ParseDate('yesterday');\r
72 }\r
73 if ($time eq 'month') {\r
74         $date = ParseDate('1 month ago');\r
75         $date2 = ParseDate('today');\r
76 \r
77 }\r
78 if ($time=~ /\//){\r
79         $date=ParseDate($time);\r
80         $date2=ParseDateDelta('+ 1 day');\r
81         $date2=DateCalc($date,$date2);\r
82 }\r
83 \r
84 #my $date=UnixDate($date,'%Y-%m-%d');\r
85 #my $date2=UnixDate($date2,'%Y-%m-%d');\r
86 \r
87 my $date="2005-08-19";\r
88 my $date2="2005-08-20";\r
89 \r
90 #my $date="2005-01-05";\r
91 #my $date2="2005-01-06";\r
92 \r
93 #get a list of every payment\r
94 my @payments=TotalPaid($date,$date2);\r
95 \r
96 my $count=@payments;\r
97 # print "MASON: number of payments=$count\n";\r
98 \r
99 my $i=0;\r
100 my $totalcharges=0;\r
101 my $totalcredits=0;\r
102 my $totalpaid=0;\r
103 my $totalwritten=0;\r
104 \r
105 # lets get a a list of all individual item charges paid for by that payment\r
106 while ($i<$count ){\r
107 \r
108        my $count;\r
109        my @charges;\r
110 \r
111        if ($payments[$i]{'type'} ne 'writeoff'){         # lets ignore writeoff payments!.\r
112            @charges=getcharges($payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'}, $payments[$i]{'proccode'});\r
113            $totalcharges++;\r
114            $count=@charges;\r
115            # getting each of the charges and putting them into a array to be printed out\r
116            #this loops per charge per person\r
117            for (my $i2=0;$i2<$count;$i2++){\r
118                my $hour=substr($payments[$i]{'timestamp'},8,2);\r
119                my $min=substr($payments[$i]{'timestamp'},10,2);\r
120                my $sec=substr($payments[$i]{'timestamp'},12,2);\r
121                my $time="$hour:$min:$sec";\r
122                my $time2="$payments[$i]{'date'}";\r
123                my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});\r
124 \r
125 \r
126                my $fullname = join ' ', $payments[$i]->{'firstname'}, $payments[$i]->{'surname'};\r
127 \r
128                # lets build up a row\r
129                my %rows1 = (branch => $branch,\r
130                             datetime => $payments[$i]->{'datetime'},\r
131                             surname => $payments[$i]->{'surname'},\r
132                             firstname => $payments[$i]->{'firstname'},\r
133                             description => $charges[$i2]->{'description'},\r
134                             accounttype => $charges[$i2]->{'accounttype'},\r
135                             amount => sprintf("%.2f", $charges[$i2]->{'amount'}), # rounding amounts to 2dp\r
136                             type => $payments[$i]->{'type'},\r
137                             value => sprintf("%.2f", $charges[$i2]->{'type'})); # rounding amounts to 2dp\r
138 \r
139                push (@loop1, \%rows1);\r
140            }\r
141        } else {\r
142          ++$totalwritten;\r
143        }\r
144        $i++; #increment the while loop\r
145        $totalpaid = $totalpaid + $payments[$i]->{'value'};\r
146 }\r
147 \r
148 \r
149 \r
150 #get credits and append to the bottom of payments\r
151 my @credits=getcredits($date,$date2);\r
152 \r
153 #print Dumper(@credits);\r
154 \r
155 my $count=@credits;\r
156 my $i=0;\r
157 \r
158 while ($i<$count ){\r
159 \r
160        my %rows2 = (creditbranch        => $credits[$i]->{'branchcode'},\r
161                     creditdate          => $credits[$i]->{'date'},\r
162                     creditsurname       => $credits[$i]->{'surname'},\r
163                     creditfirstname     => $credits[$i]->{'firstname'},\r
164                     creditdescription   => $credits[$i]->{'description'},\r
165                     creditaccounttype   => $credits[$i]->{'accounttype'},\r
166                     creditamount        => $credits[$i]->{'amount'});\r
167 \r
168        push (@loop2, \%rows2);\r
169        $i++; #increment the while loop\r
170        $totalcredits = $totalcredits + $credits[$i]->{'amount'};\r
171        ;\r
172 \r
173 }\r
174 #takes off first char minus sign "-100.00"\r
175 \r
176 \r
177 $totalcredits = substr($totalcredits, 1);\r
178 \r
179 $template->param( loop1               => \@loop1,\r
180                   loop2               => \@loop2,\r
181                   totalpaid           => $totalpaid,\r
182                   totalcredits        => $totalcredits,\r
183                   totalwritten        => $totalwritten );\r
184 \r
185 output_html_with_http_headers $input, $cookie, $template->output;\r
186 \r
187 \r