New XML API
[koha.git] / reports / stats.screen.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5 use C4::Output;
6 use HTML::Template;
7 use C4::Auth;
8 use C4::Interface::CGI::Output;
9 use C4::Context;
10 use Date::Manip;
11 use C4::Date;
12 use C4::Stats;
13 &Date_Init("DateFormat=non-US"); # set non-USA date, eg:19/08/2005
14
15 my $input=new CGI;
16 my $time=$input->param('time');
17 my $date=$input->param('from');
18 my $date2=$input->param('to');
19 my ($template, $loggedinuser, $cookie)
20     = get_template_and_user({template_name => "reports/stats.screen.tmpl",
21                              query => $input,
22                              type => "intranet",
23                              authnotrequired => 0,
24                              flagsrequired => {borrowers => 1},
25                              debug => 1,
26                              });
27
28
29 #get a list of every payment
30 my @payments=TotalPaid($date,$date2);
31
32 my $count=@payments;
33 # warn "number of payments=$count\n";
34
35 my $i=0;
36 my $totalcharges=0;
37 my $totalcredits=0;
38 my $totalpaid=0;
39 my $totalwritten=0;
40 my $totalwrittenamount=0;
41 my $totalinvoicesamount=0;
42 my $totalinvoices=0;
43 my @loop1;
44 my @loop2;
45 my @loop3;
46
47 # lets get a a list of all individual item charges paid for by that payment
48 while ($i<$count ){
49
50        my $count;
51        my @charges;
52
53        if ($payments[$i]->{'accounttype'} ne 'W'){         # lets ignore writeoff payments!.
54            @charges=getcharges($payments[$i]{'borrowernumber'}, $payments[$i]{'offset'}, $payments[$i]{'accountno'});
55            $totalcharges++;
56            $count=@charges;
57
58            # getting each of the charges and putting them into a array to be printed out
59            #this loops per charge per person
60            for (my $i2=0;$i2<$count;$i2++){
61               
62                my $time2="$payments[$i]{'date'}";
63 #               my $branch=Getpaidbranch($time2,$payments[$i]{'borrowernumber'});
64
65                # lets build up a row
66                my %rows1 = ( datetime => $payments[$i]->{'timestamp'},
67                             surname => $payments[$i]->{'surname'},
68                             firstname => $payments[$i]->{'firstname'},
69                             description => $payments[$i]->{'description'},
70                             accounttype => $charges[$i2]->{'accounttype'},
71                             amount => sprintf("%.2f",  $charges[$i2]->{'amount'}), # rounding amounts to 2dp
72                             type => $payments[$i]->{'accounttype'},
73                             value => sprintf("%.2f", $payments[$i]->{'amount'}*(-1))); # rounding amounts to 2dp
74
75                push (@loop1, \%rows1);
76   $totalpaid = sprintf("%.2f",$totalpaid + $payments[$i]->{'amount'}*(-1));
77            }
78        } else {
79 $totalwrittenamount= sprintf("%.2f",$totalwrittenamount + $payments[$i]->{'amount'}*(-1));
80          ++$totalwritten;
81        }
82       
83      
84  $i++; #increment the while loop
85 }
86
87 #get credits and append to the bottom of payments
88 my @credits=getcredits($date,$date2);
89
90 my $count=@credits;
91 my $i=0;
92
93 while ($i<$count ){
94
95        my %rows2 = (creditdate          => format_date($credits[$i]->{'date'}),
96                     creditsurname       => $credits[$i]->{'surname'},
97                     creditfirstname     => $credits[$i]->{'firstname'},
98                     creditdescription   => $credits[$i]->{'description'},
99                     creditaccounttype   => $credits[$i]->{'accounttype'},
100                     creditamount        => sprintf("%.2f",$credits[$i]->{'amount'}*(-1)));
101
102        push (@loop2, \%rows2);
103     
104        $totalcredits =sprintf("%.2f", $totalcredits + $credits[$i]->{'amount'});
105          $i++; #increment the while loop
106
107 }
108
109
110 #takes off first char minus sign "-100.00"
111 $totalcredits = substr($totalcredits, 1);
112
113 my @invoices=getinvoices($date,$date2);
114 my $count=@invoices;
115 my $i=0;
116
117 while ($i<$count ){
118
119        my %rows3 = (invoicesdate          => format_date($invoices[$i]->{'date'}),
120                    invoicessurname       => $invoices[$i]->{'surname'},
121                    invoicesfirstname     => $invoices[$i]->{'firstname'},
122                     invoicesdescription   => $invoices[$i]->{'description'},
123                     invoicesaccounttype   => $invoices[$i]->{'accounttype'},
124                     invoicesamount        => sprintf("%.2f",$invoices[$i]->{'amount'}),
125         invoicesamountremaining=>sprintf("%.2f",$invoices[$i]->{'amountoutstanding'}));
126        push (@loop3, \%rows3);
127          $totalinvoicesamount =sprintf("%.2f", $totalinvoicesamount + $invoices[$i]->{'amountoutstanding'});
128        $totalinvoices =sprintf("%.2f", $totalinvoices + $invoices[$i]->{'amount'});
129          $i++; #increment the while loop
130
131 }
132 $template->param( loop1               => \@loop1,
133                   loop2               => \@loop2,
134                  loop3               => \@loop3,
135                   totalpaid           => $totalpaid,
136                   totalcredits        => $totalcredits,
137                 totalcreditsamount        => sprintf("%.2f",$totalcredits-$totalwrittenamount),
138         totalwrittenamount        => $totalwrittenamount,
139                   totalwritten        => $totalwritten ,
140         totalinvoices=>$totalinvoices, totalinvoicesamount=>$totalinvoicesamount        );
141
142 output_html_with_http_headers $input, $cookie, $template->output;