test suite: way to run single test file
[koha.git] / circ / billing.pl
index f73962d..b369f22 100755 (executable)
@@ -24,12 +24,8 @@ use C4::Output;
 use CGI;
 use C4::Auth;
 use C4::Dates qw/format_date format_date_in_iso/;
-
-use vars qw($debug);
-
-BEGIN {
-    $debug = $ENV{DEBUG} || 0;
-}
+use C4::Debug;
+use Date::Calc qw/Today Add_Delta_YM/;
 
 my $input = new CGI;
 my $order = $input->param('order');
@@ -63,20 +59,11 @@ my $biblionumber;
 my $title;
 my $author;
 
-my @datearr    = localtime( time() );
-my 
-$todaysdate =
-    ( 1900 + $datearr[5] ) . '-'
-  . sprintf( "%0.2d", ( $datearr[4] + 1 ) ) . '-'
-  . sprintf( "%0.2d", $datearr[3] );
-
+my ( $year, $month, $day ) = Today();
+my $todaysdate   = sprintf("%-04.4d-%-02.2d-%02.2d", $year, $month, $day);
 # Find yesterday for the default shelf pull start and end dates
-#    A defualt of the prior years's holds is a reasonable way to pull holds 
-my @datearr_yesterday    = localtime( time() - 86400*365 );
-my $yesterdaysdate =
-    ( 1900 + $datearr_yesterday[5] ) . '-'
-  . sprintf( "%0.2d", ( $datearr_yesterday[4] + 1 ) ) . '-'
-  . sprintf( "%0.2d", $datearr_yesterday[3] );
+#    A default of the prior years's holds is a reasonable way to pull holds 
+my $datelastyear = sprintf("%-04.4d-%-02.2d-%02.2d", Add_Delta_YM($year, $month, $day, -1, 0));
 
 #              Predefine the start and end dates if they are not already defined
 $startdate =~ s/^\s+//;
@@ -85,7 +72,7 @@ $enddate =~ s/^\s+//;
 $enddate =~ s/\s+$//;
 #              Check if null, should string match, if so set start and end date to yesterday
 if (!defined($startdate) or $startdate eq "") {
-       $startdate = format_date($yesterdaysdate);
+       $startdate = format_date($datelastyear);
 }
 if (!defined($enddate) or $enddate eq "") {
        $enddate = format_date($todaysdate);
@@ -100,34 +87,42 @@ if (!defined($max_bill) or $max_bill eq "") {
 my $dbh    = C4::Context->dbh;
 my ($sqlorderby, $sqldatewhere, $presqldatewhere) = ("","","");
 $debug and warn format_date_in_iso($startdate) . "\n" . format_date_in_iso($enddate);
+my @query_params = ();
 # the dates below is to check for compliance of the current date range
-#$sqldatewhere .= " AND date >= " . $dbh->quote(format_date_in_iso($startdate))  if ($startdate) ;
-$sqldatewhere .= " AND date <= " . $dbh->quote(format_date_in_iso($enddate))  if ($enddate) ;
+if ($enddate) {
+    $sqldatewhere .= " AND date <= ?";
+    push @query_params, format_date_in_iso($enddate);
+}
+push @query_params, $max_bill;
 # the date below is to check for compliance of all fees prior
-$presqldatewhere .= " AND date < " . $dbh->quote(format_date_in_iso($startdate))  if ($startdate) ;
+if ($startdate) {
+    $presqldatewhere .= " AND date < ?";
+    push @query_params, format_date_in_iso($startdate);
+}
+push @query_params, $max_bill;
 
 if ($order eq "patron") {
-       $sqlorderby = " order by surname, firstname ";
+       $sqlorderby = " ORDER BY surname, firstname ";
 } elsif ($order eq "fee") {
-    $sqlorderby = " order by l_amountoutstanding DESC ";
+    $sqlorderby = " ORDER BY l_amountoutstanding DESC ";
 } elsif ($order eq "desc") {
-    $sqlorderby = " order by l_description ";
+    $sqlorderby = " ORDER BY l_description ";
 } elsif ($order eq "type") {
-    $sqlorderby = " order by l_accounttype ";
+    $sqlorderby = " ORDER BY l_accounttype ";
 } elsif ($order eq "date") {
-    $sqlorderby = " order by l_date DESC ";
+    $sqlorderby = " ORDER BY l_date DESC ";
 } elsif ($order eq "total") {
-    $sqlorderby = " order by sum_amount DESC ";
+    $sqlorderby = " ORDER BY sum_amount DESC ";
 } else {
-       $sqlorderby = " order by surname, firstname ";
+       $sqlorderby = " ORDER BY surname, firstname ";
 }
 my $strsth =
        "SELECT 
-               GROUP_CONCAT(accountlines.accounttype ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_accounttype,
-               GROUP_CONCAT(description ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_description,
-               GROUP_CONCAT(round(amountoutstanding,2) ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_amountoutstanding, 
-               GROUP_CONCAT(accountlines.date ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_date, 
-               GROUP_CONCAT(accountlines.itemnumber ORDER BY accountlines.date DESC SEPARATOR '<br>') as l_itemnumber, 
+               GROUP_CONCAT(accountlines.accounttype ORDER BY accountlines.date DESC SEPARATOR '<br/>') as l_accounttype,
+               GROUP_CONCAT(description ORDER BY accountlines.date DESC SEPARATOR '<br/>') as l_description,
+               GROUP_CONCAT(round(amountoutstanding,2) ORDER BY accountlines.date DESC SEPARATOR '<br/>') as l_amountoutstanding, 
+               GROUP_CONCAT(accountlines.date ORDER BY accountlines.date DESC SEPARATOR '<br/>') as l_date, 
+               GROUP_CONCAT(accountlines.itemnumber ORDER BY accountlines.date DESC SEPARATOR '<br/>') as l_itemnumber, 
                count(*) as cnt, 
                max(accountlines.date) as maxdate,
                round(sum(amountoutstanding),2) as sum_amount, 
@@ -148,34 +143,32 @@ my $strsth =
                        IN (SELECT borrowernumber FROM accountlines 
                                where borrowernumber >= 0
                                $sqldatewhere 
-                               GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill ) 
+                               GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= ? ) 
                AND accountlines.borrowernumber 
                        NOT IN (SELECT borrowernumber FROM accountlines 
                                where borrowernumber >= 0
                                $presqldatewhere 
-                               GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill ) 
+                               GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= ? ) 
 ";
 
 
 if (C4::Context->preference('IndependantBranches')){
        $strsth .= " AND borrowers.branchcode=? ";
+    push @query_params, C4::Context->userenv->{'branch'};
 }
-$strsth .= " GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= $max_bill " . $sqlorderby;
+$strsth .= " GROUP BY accountlines.borrowernumber HAVING sum(amountoutstanding) >= ? " . $sqlorderby;
+push @query_params, $max_bill;
+
 my $sth = $dbh->prepare($strsth);
+$sth->execute(@query_params);
 
-if (C4::Context->preference('IndependantBranches')){
-       $sth->execute(C4::Context->userenv->{'branch'});
-}
-else {
-       $sth->execute();
-}      
-my @reservedata;
+my @billingdata;
 my $previous;
 my $this;
 while ( my $data = $sth->fetchrow_hashref ) {   
     my @itemlist;
     push(
-        @reservedata,
+        @billingdata,
         {
                                l_accountype                    =>              $data->{l_accounttype},
                                l_description                   =>              $data->{l_description},
@@ -208,7 +201,7 @@ $template->param(
     from            => $startdate,
     to              => $enddate,
     ratio           => $max_bill,
-    reserveloop     => \@reservedata,
+    billingloop     => \@billingdata,
     "BiblioDefaultView".C4::Context->preference("BiblioDefaultView") => 1,
     DHTMLcalendar_dateformat =>  C4::Dates->DHTMLcalendar(),
 );