fix for bug 2488: OPACItemsResultsDisplay/singlebranchmode
[koha.git] / circ / stats.pl
blob6d8a8ead5e4a0ff0ac7a4ad988dfecb6ec403b26
1 #!/usr/bin/perl
4 #written 14/1/2000
5 #script to display reports
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA 02111-1307 USA
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Output;
28 use C4::Auth;
29 use Date::Manip;
30 use C4::Stats;
32 use vars qw($debug);
34 BEGIN {
35 $debug = $ENV{DEBUG} || 0;
38 my $input = new CGI;
39 my $time = $input->param('time');
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 template_name => "circ/stats.tmpl",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { reports => 1 },
48 debug => 1,
52 my $date;
53 my $date2;
54 if ( $time eq '' ) {
55 $template->param(notime => '1');
56 output_html_with_http_headers $input, $cookie, $template->output;
57 exit;
59 if ( $time eq 'yesterday' ) {
60 $date = ParseDate('yesterday');
61 $date2 = ParseDate('today');
63 if ( $time eq 'today' ) {
64 $date = ParseDate('today');
65 $date2 = ParseDate('tomorrow');
67 if ( $time eq 'daybefore' ) {
68 $date = ParseDate('2 days ago');
69 $date2 = ParseDate('yesterday');
71 if ( $time eq 'month' ) {
72 $date = ParseDate('1 month ago');
73 $date2 = ParseDate('today');
74 $debug and warn "d : $date // d2 : $date2";
76 if ( $time =~ /\// ) {
77 $date = ParseDate($time);
78 $date2 = ParseDateDelta('+ 1 day');
79 $date2 = DateCalc( $date, $date2 );
81 $date = UnixDate( $date, '%Y-%m-%d' );
82 $date2 = UnixDate( $date2, '%Y-%m-%d' );
83 $debug and warn "d : $date // d2 : $date2";
84 my @payments = TotalPaid( $date, $date2 );
85 my $count = @payments;
86 my $total = 0;
87 my $totalw = 0;
88 my $oldtime;
89 my @loop;
90 my %row;
91 my $i = 0;
93 while ( $i < $count ) {
94 $debug and warn " pay : " . $payments[$i]{'timestamp'};
95 my $time = $payments[$i]{'datetime'};
96 my $payments = $payments[$i]{'value'};
97 my $charge = 0;
98 my @temp = split( / /, $payments[$i]{'datetime'} );
99 my $date = $temp[0];
100 my @charges =
101 getcharges( $payments[$i]{'borrowernumber'}, $payments[$i]{'timestamp'} );
102 my $count = @charges;
103 my $temptotalf = 0;
104 my $temptotalr = 0;
105 my $temptotalres = 0;
106 my $temptotalren = 0;
107 my $temptotalw = 0;
109 for ( my $i2 = 0 ; $i2 < $count ; $i2++ ) {
110 $charge += $charges[$i2]->{'amount'};
111 %row = (
112 name => $charges[$i2]->{'description'},
113 type => $charges[$i2]->{'accounttype'},
114 time => $charges[$i2]->{'timestamp'},
115 amount => $charges[$i2]->{'amount'},
116 branch => $charges[$i2]->{'amountoutstanding'}
118 push( @loop, \%row );
119 if ( $payments[$i]{'accountytpe'} ne 'W' ) {
120 if ( $charges[$i2]->{'accounttype'} eq 'Rent' ) {
121 $temptotalr +=
122 $charges[$i2]->{'amount'} -
123 $charges[$i2]->{'amountoutstanding'};
125 if ( $charges[$i2]->{'accounttype'} eq 'F'
126 || $charges[$i2]->{'accounttype'} eq 'FU'
127 || $charges[$i2]->{'accounttype'} eq 'FN' )
129 $temptotalf +=
130 $charges[$i2]->{'amount'} -
131 $charges[$i2]->{'amountoutstanding'};
133 if ( $charges[$i2]->{'accounttype'} eq 'Res' ) {
134 $temptotalres +=
135 $charges[$i2]->{'amount'} -
136 $charges[$i2]->{'amountoutstanding'};
138 if ( $charges[$i2]->{'accounttype'} eq 'R' ) {
139 $temptotalren +=
140 $charges[$i2]->{'amount'} -
141 $charges[$i2]->{'amountoutstanding'};
145 my $time2 = $payments[$i]{'date'};
146 my $branch = Getpaidbranch( $time2, $payments[$i]{'borrowernumber'} );
147 my $borrowernumber = $payments[$i]{'borrowernumber'};
148 my $oldtime = $payments[$i]{'timestamp'};
149 my $oldtype = $payments[$i]{'accounttype'};
151 while ($borrowernumber eq $payments[$i]{'borrowernumber'}
152 && $oldtype == $payments[$i]{'accounttype'}
153 && $oldtime eq $payments[$i]{'timestamp'} )
155 my $xtime2 = $payments[$i]{'date'};
156 my $branch = Getpaidbranch( $xtime2, $payments[$i]{'borrowernumber'} );
157 if ( $payments[$i]{'accounttype'} eq 'W' ) {
158 $totalw += $payments[$i]{'amount'};
160 else {
161 $payments[$i]{'amount'} = $payments[$i]{'amount'} * -1;
162 $total += $payments[$i]{'amount'};
165 %row = (
166 name => "<b>"
167 . $payments[$i]{'firstname'}
168 . $payments[$i]{'surname'} . "</b>",
169 type => $payments[$i]{'accounttype'},
170 time => $payments[$i]{'date'},
171 amount => $payments[$i]{'amount'},
172 branch => $branch
174 push( @loop, \%row );
175 $oldtype = $payments[$i]{'accounttype'};
176 $oldtime = $payments[$i]{'timestamp'};
177 $borrowernumber = $payments[$i]{'borrowernumber'};
178 $i++;
182 $template->param(
183 loop1 => \@loop,
184 totalw => $totalw,
185 total => $total
188 output_html_with_http_headers $input, $cookie, $template->output;