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