Bug 17600: Standardize our EXPORT_OK
[srvgit] / acqui / ordered.pl
index e393490..84b4955 100755 (executable)
@@ -30,12 +30,13 @@ this script is to show orders ordered but not yet received
 use C4::Context;
 use Modern::Perl;
 use CGI qw ( -utf8 );
-use C4::Auth;
-use C4::Output;
+use C4::Auth qw( get_template_and_user );
+use C4::Output qw( output_html_with_http_headers );
 use Koha::Acquisition::Invoice::Adjustments;
+use C4::Acquisition qw( get_rounded_price );
 
 my $dbh     = C4::Context->dbh;
-my $input   = new CGI;
+my $input   = CGI->new;
 my $fund_id = $input->param('fund');
 my $fund_code = $input->param('fund_code');
 
@@ -44,9 +45,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
         template_name   => "acqui/ordered.tt",
         query           => $input,
         type            => "intranet",
-        authnotrequired => 0,
         flagsrequired   => { acquisition => '*' },
-        debug           => 1,
     }
 );
 
@@ -54,24 +53,31 @@ my $query = <<EOQ;
 SELECT
     aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
     quantity-quantityreceived AS tleft,
-    ecost, budgetdate, entrydate,
+    ecost_tax_included, budgetdate, entrydate,
     aqbasket.booksellerid,
-    itype,
+    aqbooksellers.name as vendorname,
+    GROUP_CONCAT(DISTINCT itype SEPARATOR '|') AS itypes,
     title
-FROM (aqorders, aqbasket)
+FROM aqorders
+JOIN aqbasket USING (basketno)
 LEFT JOIN biblio ON
     biblio.biblionumber=aqorders.biblionumber
 LEFT JOIN aqorders_items ON
     aqorders.ordernumber=aqorders_items.ordernumber
 LEFT JOIN items ON
     items.itemnumber=aqorders_items.itemnumber
+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
+    datecancellationprinted IS NULL AND
     (quantity > quantityreceived OR quantityreceived IS NULL)
-    GROUP BY aqorders.ordernumber
+    GROUP BY aqorders.biblionumber, aqorders.basketno, aqorders.ordernumber,
+             tleft,
+             ecost_tax_included, budgetdate, entrydate,
+             aqbasket.booksellerid,
+             aqbooksellers.name,
+             title
 EOQ
 
 my $sth = $dbh->prepare($query);
@@ -84,12 +90,13 @@ my @ordered;
 
 my $total = 0;
 while ( my $data = $sth->fetchrow_hashref ) {
+    $data->{'itemtypes'} = [split('\|', $data->{itypes})];
     my $left = $data->{'tleft'};
     if ( !$left || $left eq '' ) {
         $left = $data->{'quantity'};
     }
     if ( $left && $left > 0 ) {
-        my $subtotal = $left * $data->{'ecost'};
+        my $subtotal = $left * get_rounded_price( $data->{'ecost_tax_included'} );
         $data->{subtotal} = sprintf( "%.2f", $subtotal );
         $data->{'left'} = $left;
         push @ordered, $data;
@@ -97,7 +104,7 @@ while ( my $data = $sth->fetchrow_hashref ) {
     }
 }
 
-my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $fund_id, closedate => undef, encumber_open => 1 }, { join => 'invoiceid' } );
+my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $fund_id, closedate => undef, encumber_open => 1 }, { prefetch => 'invoiceid' } );
 while ( my $adj = $adjustments->next ){
     $total += $adj->adjustment;
 }