Bug 32166: Use import record id for retrieving correct inputs
[koha-ffzg.git] / acqui / spent.pl
index 8909eb0..b8269d0 100755 (executable)
@@ -30,14 +30,15 @@ this script is designed to show the spent amount in budgets
 =cut
 
 use C4::Context;
-use C4::Auth;
-use C4::Output;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
 use Modern::Perl;
 use CGI qw ( -utf8 );
+use C4::Acquisition qw( get_rounded_price );
 use Koha::Acquisition::Invoice::Adjustments;
 
 my $dbh      = C4::Context->dbh;
-my $input    = new CGI;
+my $input    = CGI->new;
 my $bookfund = $input->param('fund');
 my $fund_code = $input->param('fund_code');
 
@@ -46,9 +47,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         template_name   => "acqui/spent.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { acquisition => '*' },
-        debug           => 1,
     }
 );
 
@@ -56,14 +55,14 @@ my $query = <<EOQ;
 SELECT
     aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
     quantity-quantityreceived AS tleft,
-    ecost, budgetdate, entrydate,
+    budgetdate, entrydate,
     aqbasket.booksellerid,
-    itype,
+    GROUP_CONCAT(DISTINCT itype SEPARATOR '|') as itypes,
     title,
     aqorders.invoiceid,
     aqinvoices.invoicenumber,
     quantityreceived,
-    unitprice,
+    unitprice_tax_included,
     datereceived,
     aqbooksellers.name as vendorname
 FROM (aqorders, aqbasket)
@@ -80,10 +79,20 @@ LEFT JOIN aqbooksellers ON
 WHERE
     aqorders.basketno=aqbasket.basketno AND
     budget_id=? AND
-    (datecancellationprinted IS NULL OR
-        datecancellationprinted='0000-00-00') AND
+    datecancellationprinted IS NULL AND
     datereceived IS NOT NULL
-    GROUP BY aqorders.ordernumber
+    GROUP BY aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
+             tleft,
+             budgetdate, entrydate,
+             aqbasket.booksellerid,
+             title,
+             aqorders.invoiceid,
+             aqinvoices.invoicenumber,
+             quantityreceived,
+             unitprice_tax_included,
+             datereceived,
+             aqbooksellers.name
+
 EOQ
 my $sth = $dbh->prepare($query);
 $sth->execute($bookfund);
@@ -94,10 +103,11 @@ my $subtotal = 0;
 my @spent;
 while ( my $data = $sth->fetchrow_hashref ) {
     my $recv = $data->{'quantityreceived'};
+    $data->{'itemtypes'} = [split('\|', $data->{itypes})];
     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;
     }
@@ -106,7 +116,7 @@ while ( my $data = $sth->fetchrow_hashref ) {
 
 my $total = $subtotal;
 $query = qq{
-    SELECT invoicenumber, shipmentcost
+    SELECT invoiceid, invoicenumber, shipmentcost
     FROM aqinvoices
     WHERE shipmentcost_budgetid = ?
 };
@@ -116,13 +126,14 @@ my @shipmentcosts;
 while (my $data = $sth->fetchrow_hashref) {
     push @shipmentcosts, {
         shipmentcost => sprintf("%.2f", $data->{shipmentcost}),
+        invoiceid => $data->{invoiceid},
         invoicenumber => $data->{invoicenumber}
     };
     $total += $data->{shipmentcost};
 }
 $sth->finish;
 
-my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $bookfund, closedate => { '!=' => undef } }, { join => 'invoiceid' } );
+my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $bookfund, closedate => { '!=' => undef } }, { prefetch => 'invoiceid' },  );
 while ( my $adj = $adjustments->next ){
     $total += $adj->adjustment;
 }