ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / acqui / spent.pl
index 988b8c2..80d9361 100755 (executable)
@@ -34,6 +34,7 @@ use C4::Auth;
 use C4::Output;
 use Modern::Perl;
 use CGI qw ( -utf8 );
+use C4::Acquisition;
 use Koha::Acquisition::Invoice::Adjustments;
 
 my $dbh      = C4::Context->dbh;
@@ -56,15 +57,16 @@ my $query = <<EOQ;
 SELECT
     aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
     quantity-quantityreceived AS tleft,
-    ecost, budgetdate, entrydate,
+    budgetdate, entrydate,
     aqbasket.booksellerid,
     itype,
     title,
     aqorders.invoiceid,
     aqinvoices.invoicenumber,
     quantityreceived,
-    unitprice,
-    datereceived
+    unitprice_tax_included,
+    datereceived,
+    aqbooksellers.name as vendorname
 FROM (aqorders, aqbasket)
 LEFT JOIN biblio ON
     biblio.biblionumber=aqorders.biblionumber
@@ -74,13 +76,27 @@ LEFT JOIN items ON
     aqorders_items.itemnumber = items.itemnumber
 LEFT JOIN aqinvoices ON
     aqorders.invoiceid = aqinvoices.invoiceid
+LEFT JOIN aqbooksellers ON
+    aqbasket.booksellerid = aqbooksellers.id
 WHERE
     aqorders.basketno=aqbasket.basketno AND
     budget_id=? AND
     (datecancellationprinted IS NULL OR
         datecancellationprinted='0000-00-00') AND
     datereceived IS NOT NULL
-    GROUP BY aqorders.ordernumber
+    GROUP BY aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
+             tleft,
+             ecost, budgetdate, entrydate,
+             aqbasket.booksellerid,
+             itype,
+             title,
+             aqorders.invoiceid,
+             aqinvoices.invoicenumber,
+             quantityreceived,
+             unitprice,
+             datereceived,
+             aqbooksellers.name
+
 EOQ
 my $sth = $dbh->prepare($query);
 $sth->execute($bookfund);
@@ -92,9 +108,9 @@ my @spent;
 while ( my $data = $sth->fetchrow_hashref ) {
     my $recv = $data->{'quantityreceived'};
     if ( $recv > 0 ) {
-        my $rowtotal = $recv * $data->{'unitprice'};
+        my $rowtotal = $recv * get_rounded_price($data->{'unitprice_tax_included'});
         $data->{'rowtotal'}  = sprintf( "%.2f", $rowtotal );
-        $data->{'unitprice'} = sprintf( "%.2f", $data->{'unitprice'} );
+        $data->{'unitprice_tax_included'} = sprintf( "%.2f", $data->{'unitprice_tax_included'} );
         $subtotal += $rowtotal;
         push @spent, $data;
     }