Templated stats.pl. Did not template the fox/shannon/etc. stuff because I figured...
[koha.git] / stats.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #written 14/1/2000
6 #script to display reports
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 use CGI;
28 use C4::Output;
29 use HTML::Template;
30 use C4::Auth;
31 use C4::Interface::CGI::Output;
32 use C4::Context;
33 use Date::Manip;
34 use C4::Stats;
35
36 my $input=new CGI;
37 my $time=$input->param('time');
38
39 my ($template, $loggedinuser, $cookie)
40     = get_template_and_user({template_name => "stats.tmpl",
41                              query => $input,
42                              type => "intranet",
43                              authnotrequired => 0,
44                              flagsrequired => {borrowers => 1},
45                              debug => 1,
46                              });
47
48 my $date;
49 my $date2;
50 if ($time eq 'yesterday'){
51   $date=ParseDate('yesterday');
52   $date2=ParseDate('today');
53 }
54 if ($time eq 'today'){
55   $date=ParseDate('today');
56   $date2=ParseDate('tomorrow');
57 }
58 if ($time eq 'daybefore'){
59   $date=ParseDate('2 days ago');
60   $date2=ParseDate('yesterday');
61 }
62 if ($time=~ /\//){
63   $date=ParseDate($time);
64   $date2=ParseDateDelta('+ 1 day');
65   $date2=DateCalc($date,$date2);
66 }
67 $date=UnixDate($date,'%Y-%m-%d');
68 $date2=UnixDate($date2,'%Y-%m-%d');
69 my @payments=TotalPaid($date);
70 my $count=@payments;
71 my $total=0;
72 my %levin;
73 my %foxton;
74 my %shannon;
75 my $oldtime;
76 my $totalw=0;
77 my @loop;
78 my %row;
79 my $i=0;
80 while ($i<$count){
81   my $time=$payments[$i]{'datetime'};
82   my $payments=$payments[$i]{'value'};
83   my $charge=0;
84   my @temp=split(/ /,$payments[$i]{'datetime'});
85   my $date=$temp[0];
86   my @charges=getcharges($payments[$i]{'borrowernumber'},$payments[$i]{'timestamp'});
87   my $count=@charges;
88   my $temptotalf=0;
89   my $temptotalr=0;
90   my $temptotalres=0;
91   my $temptotalren=0;
92   my $temptotalw=0;
93   for (my $i2=0;$i2<$count;$i2++){
94      $charge+=$charges[$i2]->{'amount'};
95      %row = ( name   => $charges[$i2]->{'description'},
96               type   => $charges[$i2]->{'accounttype'},
97               time   => $charges[$i2]->{'timestamp'},
98               amount => $charges[$i2]->{'amount'},
99               branch => $charges[$i2]->{'amountoutstanding'} );
100       push(@loop, \%row);
101       if ($payments[$i]{'accountytpe'} ne 'W'){
102         if ($charges[$i2]->{'accounttype'} eq 'Rent'){
103           $temptotalr+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
104         }
105         if ($charges[$i2]->{'accounttype'} eq 'F' || $charges[$i2]->{'accounttype'} eq 'FU' || $charges[$i2]->{'accounttype'} eq 'FN' ){
106           $temptotalf+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
107         }
108         if ($charges[$i2]->{'accounttype'} eq 'Res'){
109           $temptotalres+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
110         }
111         if ($charges[$i2]->{'accounttype'} eq 'R'){
112          $temptotalren+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
113         }
114      }
115   }
116
117   my $hour=substr($payments[$i]{'timestamp'},8,2);
118   my  $min=substr($payments[$i]{'timestamp'},10,2);
119   my $sec=substr($payments[$i]{'timestamp'},12,2);
120   my $time="$hour:$min:$sec";
121   my $time2="$payments[$i]{'date'}";
122   my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
123   if ($branch eq 'C'){
124     $levin{'totalf'}+=$temptotalf;
125     $levin{'totalres'}+=$temptotalres;
126     $levin{'totalren'}+=$temptotalren;
127     $levin{'totalr'}+=$temptotalr;
128   } elsif ($branch eq 'F'){
129     $foxton{'totalf'}+=$temptotalf;
130     $foxton{'totalres'}+=$temptotalres;
131     $foxton{'totalren'}+=$temptotalren;
132     $foxton{'totalr'}+=$temptotalr;
133   } elsif ($branch eq 'S'){
134     $shannon{'totalf'}+=$temptotalf;
135     $shannon{'totalres'}+=$temptotalres;
136     $shannon{'totalren'}+=$temptotalren;
137     $shannon{'totalr'}+=$temptotalr;
138   }
139   my $bornum=$payments[$i]{'borrowernumber'};
140   my $oldtime=$payments[$i]{'timestamp'};
141   my $oldtype=$payments[$i]{'accounttype'};
142   while ($bornum eq $payments[$i]{'borrowernumber'} && $oldtype == $payments[$i]{'accounttype'}  && $oldtime eq $payments[$i]{'timestamp'}){
143      my $hour=substr($payments[$i]{'timestamp'},8,2);
144      my  $min=substr($payments[$i]{'timestamp'},10,2);
145      my $sec=substr($payments[$i]{'timestamp'},12,2);
146      my $time="$hour:$min:$sec";
147          my $time2="$payments[$i]{'date'}";
148      my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
149
150     if ($payments[$i]{'accounttype'} eq 'W'){
151       $totalw+=$payments[$i]{'amount'};
152     } else {
153       $payments[$i]{'amount'}=$payments[$i]{'amount'}*-1;
154       $total+=$payments[$i]{'amount'};
155       if ($branch eq 'C'){
156         $levin{'total'}+=$payments[$i]{'amount'};
157       }
158       if ($branch eq 'F'){
159         $foxton{'total'}+=$payments[$i]{'amount'};
160       }
161       if ($branch eq 'S'){
162         $shannon{'total'}+=$payments[$i]{'amount'};
163       }
164
165     }
166
167    %row = ( name   => $payments[$i]{'firstname'} . " <b>" . $payments[$i]{'surname'} . "</b>",
168             type   => $payments[$i]{'accounttype'},
169             time   => $payments[$i]{'date'},
170             amount => $payments[$i]{'amount'},
171             branch => $branch );
172     
173     push(@loop, \%row);
174              
175     $oldtype=$payments[$i]{'accounttype'};
176     $oldtime=$payments[$i]{'timestamp'};
177     $bornum=$payments[$i]{'borrowernumber'};
178     $i++;
179
180   }
181 }
182
183 $template->param( loop   => \@loop,
184                   totalw => $totalw,
185                   total  => $total );
186
187 output_html_with_http_headers $input, $cookie, $template->output;
188
189