for error managing in .htaccess
[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 eq 'month') {
63         $date = ParseDate('1 month ago');
64         $date2 = ParseDate('today');
65         warn "d : $date // d2 : $date2";
66 }
67 if ($time=~ /\//){
68         $date=ParseDate($time);
69         $date2=ParseDateDelta('+ 1 day');
70         $date2=DateCalc($date,$date2);
71 }
72 $date=UnixDate($date,'%Y-%m-%d');
73 $date2=UnixDate($date2,'%Y-%m-%d');
74         warn "d : $date // d2 : $date2";
75 my @payments=TotalPaid($date,$date2);
76 my $count=@payments;
77 my $total=0;
78 my $oldtime;
79 my $totalw=0;
80 my @loop;
81 my %row;
82 my $i=0;
83 while ($i<$count){
84         warn " pay : ".$payments[$i]{'timestamp'};
85         my $time=$payments[$i]{'datetime'};
86         my $payments=$payments[$i]{'value'};
87         my $charge=0;
88         my @temp=split(/ /,$payments[$i]{'datetime'});
89         my $date=$temp[0];
90         my @charges=getcharges($payments[$i]{'borrowernumber'},$payments[$i]{'timestamp'});
91         my $count=@charges;
92         my $temptotalf=0;
93         my $temptotalr=0;
94         my $temptotalres=0;
95         my $temptotalren=0;
96         my $temptotalw=0;
97         for (my $i2=0;$i2<$count;$i2++){
98                 $charge+=$charges[$i2]->{'amount'};
99                 %row = ( name   => $charges[$i2]->{'description'},
100                                         type   => $charges[$i2]->{'accounttype'},
101                                         time   => $charges[$i2]->{'timestamp'},
102                                         amount => $charges[$i2]->{'amount'},
103                                         branch => $charges[$i2]->{'amountoutstanding'} );
104                 push(@loop, \%row);
105                 if ($payments[$i]{'accountytpe'} ne 'W'){
106                         if ($charges[$i2]->{'accounttype'} eq 'Rent'){
107                                 $temptotalr+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
108                         }
109                         if ($charges[$i2]->{'accounttype'} eq 'F' || $charges[$i2]->{'accounttype'} eq 'FU' || $charges[$i2]->{'accounttype'} eq 'FN' ){
110                                 $temptotalf+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
111                         }
112                         if ($charges[$i2]->{'accounttype'} eq 'Res'){
113                                 $temptotalres+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
114                         }
115                         if ($charges[$i2]->{'accounttype'} eq 'R'){
116                         $temptotalren+=$charges[$i2]->{'amount'}-$charges[$i2]->{'amountoutstanding'};
117                         }
118                 }
119         }
120         my $hour=substr($payments[$i]{'timestamp'},8,2);
121         my  $min=substr($payments[$i]{'timestamp'},10,2);
122         my $sec=substr($payments[$i]{'timestamp'},12,2);
123         my $time="$hour:$min:$sec";
124         my $time2="$payments[$i]{'date'}";
125         my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
126         my $bornum=$payments[$i]{'borrowernumber'};
127         my $oldtime=$payments[$i]{'timestamp'};
128         my $oldtype=$payments[$i]{'accounttype'};
129         while ($bornum eq $payments[$i]{'borrowernumber'} && $oldtype == $payments[$i]{'accounttype'}  && $oldtime eq $payments[$i]{'timestamp'}){
130                 my $hour=substr($payments[$i]{'timestamp'},8,2);
131                 my  $min=substr($payments[$i]{'timestamp'},10,2);
132                 my $sec=substr($payments[$i]{'timestamp'},12,2);
133                 my $time="$hour:$min:$sec";
134                 my $time2="$payments[$i]{'date'}";
135                 my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
136                 if ($payments[$i]{'accounttype'} eq 'W'){
137                         $totalw+=$payments[$i]{'amount'};
138                 } else {
139                         $payments[$i]{'amount'}=$payments[$i]{'amount'}*-1;
140                         $total+=$payments[$i]{'amount'};
141                 }
142
143                 %row = ( name   => "<b>".$payments[$i]{'firstname'}.$payments[$i]{'surname'} . "</b>",
144                                         type   => $payments[$i]{'accounttype'}, time   => $payments[$i]{'date'},
145                                         amount => $payments[$i]{'amount'}, branch => $branch );
146                 push(@loop, \%row);
147                 $oldtype=$payments[$i]{'accounttype'};
148                 $oldtime=$payments[$i]{'timestamp'};
149                 $bornum=$payments[$i]{'borrowernumber'};
150                 $i++;
151         }
152 }
153
154 $template->param( loop1   => \@loop,
155                   totalw => $totalw,
156                   total  => $total );
157
158 output_html_with_http_headers $input, $cookie, $template->output;
159
160