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/;
31 $debug = $ENV{DEBUG} || 0;
35 my $order = $input->param('order');
36 my $startdate=$input->param('from');
37 my $enddate=$input->param('to');
38 my $max_bill=$input->param('ratio');
40 my $theme = $input->param('theme'); # only used if allowthemeoverride is set
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
44 template_name => "circ/billing.tmpl",
48 flagsrequired => { circulate => 1 },
66 my @datearr = localtime( time() );
69 ( 1900 + $datearr[5] ) . '-'
70 . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
71 . sprintf( "%0.2d", $datearr[3] );
73 # Find yesterday for the default shelf pull start and end dates
74 # A defualt of the prior years's holds is a reasonable way to pull holds
75 my @datearr_yesterday = localtime( time() - 86400*365 );
77 ( 1900 + $datearr_yesterday[5] ) . '-'
78 . sprintf( "%0.2d", ( $datearr_yesterday[4] + 1 ) ) . '-'
79 . sprintf( "%0.2d", $datearr_yesterday[3] );
81 # Predefine the start and end dates if they are not already defined
82 $startdate =~ s/^\s+//;
83 $startdate =~ s/\s+$//;
86 # Check if null, should string match, if so set start and end date to yesterday
87 if (!defined($startdate) or $startdate eq "") {
88 $startdate = format_date($yesterdaysdate);
90 if (!defined($enddate) or $enddate eq "") {
91 $enddate = format_date($todaysdate);
93 if (!defined($max_bill) or $max_bill eq "") {
94 $max_bill = C4::Context->preference('noissuescharge');
100 my $dbh = C4::Context->dbh;
101 my ($sqlorderby, $sqldatewhere, $presqldatewhere) = ("","","");
102 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
103 # the dates below is to check for compliance of the current date range
104 #$sqldatewhere .= " AND date >= " . $dbh->quote(format_date_in_iso($startdate)) if ($startdate) ;
105 $sqldatewhere .= " AND date <= " . $dbh->quote(format_date_in_iso($enddate)) if ($enddate) ;
106 # the date below is to check for compliance of all fees prior
107 $presqldatewhere .= " AND date < " . $dbh->quote(format_date_in_iso($startdate)) if ($startdate) ;
109 if ($order eq "patron") {
110 $sqlorderby = " order by surname, firstname ";
111 } elsif ($order eq "fee") {
112 $sqlorderby = " order by l_amountoutstanding DESC ";
113 } elsif ($order eq "desc") {
114 $sqlorderby = " order by l_description ";
115 } elsif ($order eq "type") {
116 $sqlorderby = " order by l_accounttype ";
117 } elsif ($order eq "date") {
118 $sqlorderby = " order by l_date DESC ";
119 } elsif ($order eq "total") {
120 $sqlorderby = " order by sum_amount DESC ";
122 $sqlorderby = " order by surname, firstname ";
126 GROUP_CONCAT(accountlines.accounttype ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_accounttype,
127 GROUP_CONCAT(description ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_description,
128 GROUP_CONCAT(round(amountoutstanding,2) ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_amountoutstanding,
129 GROUP_CONCAT(accountlines.date ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_date,
130 GROUP_CONCAT(accountlines.itemnumber ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_itemnumber,
132 max(accountlines.date) as maxdate,
133 round(sum(amountoutstanding),2) as sum_amount,
134 borrowers.borrowernumber as borrowernumber,
135 borrowers.surname as surname,
136 borrowers.firstname as firstname,
137 borrowers.email as email,
138 borrowers.phone as phone,
139 accountlines.itemnumber,
141 accountlines.date as accountdate
143 borrowers, accountlines
145 accountlines.borrowernumber = borrowers.borrowernumber
146 AND accountlines.amountoutstanding <> 0
147 AND accountlines.borrowernumber
148 IN (SELECT borrowernumber FROM accountlines
149 where borrowernumber >= 0
151 GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill )
152 AND accountlines.borrowernumber
153 NOT IN (SELECT borrowernumber FROM accountlines
154 where borrowernumber >= 0
156 GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill )
160 if (C4::Context->preference('IndependantBranches')){
161 $strsth .= " AND borrowers.branchcode=? ";
163 $strsth .= " GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill " . $sqlorderby;
164 my $sth = $dbh->prepare($strsth);
166 if (C4::Context->preference('IndependantBranches')){
167 $sth->execute(C4::Context->userenv->{'branch'});
175 while ( my $data = $sth->fetchrow_hashref ) {
180 l_accountype => $data->{l_accounttype},
181 l_description => $data->{l_description},
182 l_amountoutstanding => $data->{l_amountoutstanding},
183 l_date => $data->{l_date},
184 l_itemnumber => $data->{l_itemnumber},
185 l_accounttype => $data->{l_accounttype},
186 l_title => $data->{l_title},
188 maxdate => $data->{maxdate},
189 sum_amount => $data->{sum_amount},
190 borrowernumber => $data->{borrowernumber},
191 surname => $data->{surname},
192 firstname => $data->{firstname},
193 phone => $data->{phone},
194 email => $data->{email},
195 patronname => $data->{surname} . ", " . $data->{firstname} ,
196 description => $data->{description},
197 amountoutstanding => $data->{amountoutstanding},
198 accountdata => $data->{accountdata}
207 todaysdate => format_date($todaysdate),
211 reserveloop => \@reservedata,
212 "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
213 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
216 output_html_with_http_headers $input, $cookie, $template->output;