(bug #3348) Budget and funds broken
[koha.git] / C4 / Bookfund.pm
index ada1e49..5933858 100644 (file)
@@ -222,17 +222,18 @@ sub GetBookFundBreakdown {
 
     # do a query for spent totals.
     my $query = "
-        Select distinct quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived
-    as qrev,subscription,title,itype as itemtype,aqorders.biblionumber,aqorders.booksellerinvoicenumber,
+        Select quantity,datereceived,freight,unitprice,listprice,ecost,quantityreceived
+    as qrev,subscription,title,itemtype,aqorders.biblionumber,aqorders.booksellerinvoicenumber,
     quantity-quantityreceived as tleft,
     aqorders.ordernumber
-    as ordnum,entrydate,budgetdate,aqbasket.booksellerid,aqbasket.basketno
-    from aqorders
-    inner join aqorderbreakdown on aqorderbreakdown.ordernumber = aqorders.ordernumber
-    inner join aqbasket on aqbasket.basketno = aqorders.basketno
-    left join items on  items.biblionumber=aqorders.biblionumber
-    where bookfundid=? 
-   and (datereceived >= ? and datereceived < ?)
+    as ordnum,entrydate,budgetdate,booksellerid,aqbasket.basketno
+    from aqorderbreakdown,aqbasket,aqorders
+    left join biblioitems on  biblioitems.biblioitemnumber=aqorders.biblioitemnumber 
+    where bookfundid=? and
+    aqorders.ordernumber=aqorderbreakdown.ordernumber and
+    aqorders.basketno=aqbasket.basketno
+   and (
+       (datereceived >= ? and datereceived < ?))
     and (datecancellationprinted is NULL or
           datecancellationprinted='0000-00-00')
     and (closedate >= ? and closedate < ?)
@@ -243,11 +244,9 @@ sub GetBookFundBreakdown {
     my ($spent) = 0;
     while ( my $data = $sth->fetchrow_hashref ) {
         if($data->{datereceived}){
-            if ( $data->{'subscription'} == 1 ) {
-                $spent += $data->{'quantity'} * $data->{'unitprice'};
-            }
-            else {
-                $spent += ( $data->{'unitprice'} ) * ($data->{'qrev'}?$data->{'qrev'}:0);
+            my $recv = $data->{'qrev'};
+            if ( $recv > 0 ) {
+                $spent += $recv * $data->{'unitprice'};
             }
 
         }