4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Dates qw/format_date format_date_in_iso/;
27 use Date::Calc qw/Today Add_Delta_YM/;
32 $debug = $ENV{DEBUG} || 0;
36 my $order = $input->param('order');
37 my $startdate=$input->param('from');
38 my $enddate=$input->param('to');
39 my $max_bill=$input->param('ratio');
41 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
43 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 template_name => "circ/billing.tmpl",
49 flagsrequired => { circulate => 1 },
67 my ( $year, $month, $day ) = Today();
68 my $todaysdate = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
69 # Find yesterday for the default shelf pull start and end dates
70 # A default of the prior years's holds is a reasonable way to pull holds
71 my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
73 # Predefine the start and end dates if they are not already defined
74 $startdate =~ s/^\s+//;
75 $startdate =~ s/\s+$//;
78 # Check if null, should string match, if so set start and end date to yesterday
79 if (!defined($startdate) or $startdate eq "") {
80 $startdate = format_date($datelastyear);
82 if (!defined($enddate) or $enddate eq "") {
83 $enddate = format_date($todaysdate);
85 if (!defined($max_bill) or $max_bill eq "") {
86 $max_bill = C4::Context->preference('noissuescharge');
92 my $dbh = C4::Context->dbh;
93 my ($sqlorderby, $sqldatewhere, $presqldatewhere) = ("","","");
94 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
95 # the dates below is to check for compliance of the current date range
96 #$sqldatewhere .= " AND date >= " . $dbh->quote(format_date_in_iso($startdate)) if ($startdate) ;
97 $sqldatewhere .= " AND date <= " . $dbh->quote(format_date_in_iso($enddate)) if ($enddate) ;
98 # the date below is to check for compliance of all fees prior
99 $presqldatewhere .= " AND date < " . $dbh->quote(format_date_in_iso($startdate)) if ($startdate) ;
101 if ($order eq "patron") {
102 $sqlorderby = " order by surname, firstname ";
103 } elsif ($order eq "fee") {
104 $sqlorderby = " order by l_amountoutstanding DESC ";
105 } elsif ($order eq "desc") {
106 $sqlorderby = " order by l_description ";
107 } elsif ($order eq "type") {
108 $sqlorderby = " order by l_accounttype ";
109 } elsif ($order eq "date") {
110 $sqlorderby = " order by l_date DESC ";
111 } elsif ($order eq "total") {
112 $sqlorderby = " order by sum_amount DESC ";
114 $sqlorderby = " order by surname, firstname ";
118 GROUP_CONCAT(accountlines.accounttype ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_accounttype,
119 GROUP_CONCAT(description ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_description,
120 GROUP_CONCAT(round(amountoutstanding,2) ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_amountoutstanding,
121 GROUP_CONCAT(accountlines.date ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_date,
122 GROUP_CONCAT(accountlines.itemnumber ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_itemnumber,
124 max(accountlines.date) as maxdate,
125 round(sum(amountoutstanding),2) as sum_amount,
126 borrowers.borrowernumber as borrowernumber,
127 borrowers.surname as surname,
128 borrowers.firstname as firstname,
129 borrowers.email as email,
130 borrowers.phone as phone,
131 accountlines.itemnumber,
133 accountlines.date as accountdate
135 borrowers, accountlines
137 accountlines.borrowernumber = borrowers.borrowernumber
138 AND accountlines.amountoutstanding <> 0
139 AND accountlines.borrowernumber
140 IN (SELECT borrowernumber FROM accountlines
141 where borrowernumber >= 0
143 GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill )
144 AND accountlines.borrowernumber
145 NOT IN (SELECT borrowernumber FROM accountlines
146 where borrowernumber >= 0
148 GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill )
152 if (C4::Context->preference('IndependantBranches')){
153 $strsth .= " AND borrowers.branchcode=? ";
155 $strsth .= " GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill " . $sqlorderby;
156 my $sth = $dbh->prepare($strsth);
158 if (C4::Context->preference('IndependantBranches')){
159 $sth->execute(C4::Context->userenv->{'branch'});
167 while ( my $data = $sth->fetchrow_hashref ) {
172 l_accountype => $data->{l_accounttype},
173 l_description => $data->{l_description},
174 l_amountoutstanding => $data->{l_amountoutstanding},
175 l_date => $data->{l_date},
176 l_itemnumber => $data->{l_itemnumber},
177 l_accounttype => $data->{l_accounttype},
178 l_title => $data->{l_title},
180 maxdate => $data->{maxdate},
181 sum_amount => $data->{sum_amount},
182 borrowernumber => $data->{borrowernumber},
183 surname => $data->{surname},
184 firstname => $data->{firstname},
185 phone => $data->{phone},
186 email => $data->{email},
187 patronname => $data->{surname} . ", " . $data->{firstname} ,
188 description => $data->{description},
189 amountoutstanding => $data->{amountoutstanding},
190 accountdata => $data->{accountdata}
199 todaysdate => format_date($todaysdate),
203 billingloop => \@billingdata,
204 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
205 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
208 output_html_with_http_headers $input, $cookie, $template->output;