bugfix: made yuipath available to login page template
[koha_fer] / C4 / Acquisition.pm
index bef5234..4853da2 100644 (file)
@@ -17,7 +17,6 @@ package C4::Acquisition;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id$
 
 use strict;
 require Exporter;
@@ -30,7 +29,7 @@ use Time::localtime;
 use vars qw($VERSION @ISA @EXPORT);
 
 # set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
+$VERSION = 3.00;
 
 # used in receiveorder subroutine
 # to provide library specific handling
@@ -60,12 +59,11 @@ orders, basket and parcels.
   &GetBasket &NewBasket &CloseBasket
   &GetPendingOrders &GetOrder &GetOrders
   &GetOrderNumber &GetLateOrders &NewOrder &DelOrder
-  &SearchOrder &GetHistory
+  &SearchOrder &GetHistory &GetRecentAcqui
   &ModOrder &ModReceiveOrder &ModOrderBiblioNumber
-  &GetParcels &GetParcel 
+  &GetParcels &GetParcel
 );
 
-
 =head2 FUNCTIONS ABOUT BASKETS
 
 =over 2
@@ -96,15 +94,16 @@ sub GetBasket {
     my $dbh        = C4::Context->dbh;
     my $query = "
         SELECT  aqbasket.*,
-                borrowers.firstname+' '+borrowers.surname AS authorisedbyname,
-                borrowers.branchcode AS branch
+                concat( b.firstname,' ',b.surname) AS authorisedbyname,
+                b.branchcode AS branch
         FROM    aqbasket
-        LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
+        LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
         WHERE basketno=?
     ";
     my $sth=$dbh->prepare($query);
     $sth->execute($basketno);
-    return ( $sth->fetchrow_hashref );
+    my $basket = $sth->fetchrow_hashref;
+       return ( $basket );
 }
 
 #------------------------------------------------------------#
@@ -181,13 +180,15 @@ sub CloseBasket {
 
 =over 4
 
-$orders = &GetPendingOrders($booksellerid);
+$orders = &GetPendingOrders($booksellerid, $grouped);
 
 Finds pending orders from the bookseller with the given ID. Ignores
 completed and cancelled orders.
 
 C<$orders> is a reference-to-array; each element is a
 reference-to-hash with the following fields:
+C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
+in a single result line 
 
 =over 2
 
@@ -209,19 +210,21 @@ Results are ordered from most to least recent.
 =cut
 
 sub GetPendingOrders {
-    my $supplierid = @_;
+    my ($supplierid,$grouped) = @_;
     my $dbh = C4::Context->dbh;
     my $strsth = "
-        SELECT    count(*),authorisedby,creationdate,aqbasket.basketno,
-                  closedate,surname,firstname,aqorders.title 
+        SELECT    ".($grouped?"count(*),":"")."aqbasket.basketno,
+                    surname,firstname,aqorders.*,
+                    aqbasket.closedate, aqbasket.creationdate
         FROM      aqorders
         LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
         WHERE booksellerid=?
-        AND (quantity > quantityreceived OR quantityreceived is NULL)
-        AND datecancellationprinted IS NULL
-        AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
+            AND (quantity > quantityreceived OR quantityreceived is NULL)
+            AND datecancellationprinted IS NULL
+            AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
     ";
+    ## FIXME  Why 180 days ???
     if ( C4::Context->preference("IndependantBranches") ) {
         my $userenv = C4::Context->userenv;
         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
@@ -231,15 +234,14 @@ sub GetPendingOrders {
               . "' or borrowers.branchcode ='')";
         }
     }
-    $strsth .= " group by basketno order by aqbasket.basketno";
+    $strsth .= " group by aqbasket.basketno" if $grouped;
+    $strsth .= " order by aqbasket.basketno";
+
     my $sth = $dbh->prepare($strsth);
     $sth->execute($supplierid);
-    my @results = ();
-    while ( my $data = $sth->fetchrow_hashref ) {
-        push( @results, $data );
-    }
+    my $results = $sth->fetchall_arrayref({});
     $sth->finish;
-    return \@results;
+    return $results;
 }
 
 #------------------------------------------------------------#
@@ -266,26 +268,27 @@ biblio, and biblioitems tables in the Koha database.
 sub GetOrders {
     my ( $basketno, $orderby ) = @_;
     my $dbh   = C4::Context->dbh;
-    my $query ="
-        SELECT  aqorderbreakdown.*,
-                biblio.*,
+    my $query  ="
+         SELECT  aqorderbreakdown.*,
+                biblio.*,biblioitems.*,
                 aqorders.*,
+                aqbookfund.bookfundname,
                 biblio.title
-        FROM    aqorders,biblio
-        LEFT JOIN aqorderbreakdown ON
-                    aqorders.ordernumber=aqorderbreakdown.ordernumber
+        FROM    aqorders
+            LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
+            LEFT JOIN biblio           ON biblio.biblionumber=aqorders.biblionumber
+            LEFT JOIN biblioitems      ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
+            LEFT JOIN aqbookfund       ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
         WHERE   basketno=?
-            AND biblio.biblionumber=aqorders.biblionumber
             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
     ";
 
-    $orderby = "biblio.title" unless $orderby;
+    $orderby = "biblioitems.publishercode" unless $orderby;
     $query .= " ORDER BY $orderby";
     my $sth = $dbh->prepare($query);
     $sth->execute($basketno);
     my @results;
 
-    #  print $query;
     while ( my $data = $sth->fetchrow_hashref ) {
         push @results, $data;
     }
@@ -301,7 +304,7 @@ sub GetOrders {
 
 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
 
-Looks up the ordernumber with the given biblionumber 
+Looks up the ordernumber with the given biblionumber and biblioitemnumber.
 
 Returns the number of this order.
 
@@ -311,16 +314,16 @@ Returns the number of this order.
 
 =cut
 sub GetOrderNumber {
-    my ( $biblionumber ) = @_;
+    my ( $biblionumber,$biblioitemnumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $query = "
         SELECT ordernumber
         FROM   aqorders
         WHERE  biblionumber=?
-       
+        AND    biblioitemnumber=?
     ";
     my $sth = $dbh->prepare($query);
-    $sth->execute( $biblionumber );
+    $sth->execute( $biblionumber, $biblioitemnumber );
 
     return $sth->fetchrow;
 }
@@ -336,7 +339,7 @@ $order = &GetOrder($ordernumber);
 Looks up an order by order number.
 
 Returns a reference-to-hash describing the order. The keys of
-C<$order> are fields from the biblio, , aqorders, and
+C<$order> are fields from the biblio, biblioitems, aqorders, and
 aqorderbreakdown tables of the Koha database.
 
 =back
@@ -348,11 +351,12 @@ sub GetOrder {
     my $dbh      = C4::Context->dbh;
     my $query = "
         SELECT *
-        FROM   biblio,aqorders
+        FROM   aqorders
         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
+        LEFT JOIN biblio on           biblio.biblionumber=aqorders.biblionumber
+        LEFT JOIN biblioitems on       biblioitems.biblionumber=aqorders.biblionumber
         WHERE aqorders.ordernumber=?
-        AND   biblio.biblionumber=aqorders.biblionumber
-       
+
     ";
     my $sth= $dbh->prepare($query);
     $sth->execute($ordnum);
@@ -392,9 +396,9 @@ C<$subscription> may be either "yes", or anything else for "no".
 
 sub NewOrder {
    my (
-        $basketno,  $biblionumber,       $title,        $quantity,
+        $basketno,  $bibnum,       $title,        $quantity,
         $listprice, $booksellerid, $authorisedby, $notes,
-        $bookfund,    $rrp,          $ecost,
+        $bookfund,  $bibitemnum,   $rrp,          $ecost,
         $gst,       $budget,       $cost,         $sub,
         $invoice,   $sort1,        $sort2
       )
@@ -434,21 +438,21 @@ sub NewOrder {
     my $query = "
         INSERT INTO aqorders
            ( biblionumber,title,basketno,quantity,listprice,notes,
-      rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now() )
+           biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now() )
     ";
     my $sth = $dbh->prepare($query);
 
     $sth->execute(
-        $biblionumber, $title,      $basketno, $quantity, $listprice,
-        $notes,  $rrp,      $ecost,    $gst,
+        $bibnum, $title,      $basketno, $quantity, $listprice,
+        $notes,  $bibitemnum, $rrp,      $ecost,    $gst,
         $cost,   $sub,        $sort1,    $sort2
     );
     $sth->finish;
 
     #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
     my $ordnum = $dbh->{'mysql_insertid'};
-    my $query = "
+    $query = "
         INSERT INTO aqorderbreakdown (ordernumber,bookfundid)
         VALUES (?,?)
     ";
@@ -483,9 +487,9 @@ table are also updated to the new book fund ID.
 
 sub ModOrder {
     my (
-        $title,      $ordnum,   $quantity, $listprice, $biblionumber,
+        $title,      $ordnum,   $quantity, $listprice, $bibnum,
         $basketno,   $supplier, $who,      $notes,     $bookfund,
-        $rrp,      $ecost,    $gst,       $budget,
+        $bibitemnum, $rrp,      $ecost,    $gst,       $budget,
         $cost,       $invoice,  $sort1,    $sort2
       )
       = @_;
@@ -502,10 +506,10 @@ sub ModOrder {
     $sth->execute(
         $title, $quantity, $listprice, $basketno, $rrp,
         $ecost, $cost,     $invoice,   $notes,    $sort1,
-        $sort2, $ordnum,   $biblionumber
+        $sort2, $ordnum,   $bibnum
     );
     $sth->finish;
-    my $query = "
+    $query = "
         UPDATE aqorderbreakdown
         SET    bookfundid=?
         WHERE  ordernumber=?
@@ -540,6 +544,17 @@ Updates the order with order number C<$ordernum> and biblionumber C<$biblionumbe
 
 =cut
 
+sub ModOrderBiblioNumber {
+    my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
+    my $dbh = C4::Context->dbh;
+    my $query = "
+      UPDATE aqorders
+      SET    biblioitemnumber = ?
+      WHERE  ordernumber = ?
+      AND biblionumber =  ?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
+}
 
 #------------------------------------------------------------#
 
@@ -568,13 +583,16 @@ Also updates the book fund ID in the aqorderbreakdown table.
 sub ModReceiveOrder {
     my (
         $biblionumber,    $ordnum,  $quantrec, $user, $cost,
-        $invoiceno, $freight, $rrp,      $bookfund
+        $invoiceno, $freight, $rrp, $bookfund, $daterecieved
       )
       = @_;
     my $dbh = C4::Context->dbh;
+#     warn "DATE BEFORE : $daterecieved";
+    $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
+#     warn "DATE REC : $daterecieved";
     my $query = "
         UPDATE aqorders
-        SET    quantityreceived=?,datereceived=now(),booksellerinvoicenumber=?,
+        SET    quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
                unitprice=?,freight=?,rrp=?
         WHERE biblionumber=? AND ordernumber=?
     ";
@@ -583,8 +601,8 @@ sub ModReceiveOrder {
     if ($suggestionid) {
         ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
     }
-    $sth->execute( $quantrec, $invoiceno, $cost, $freight, $rrp, $biblionumber,
-        $ordnum );
+    $sth->execute( $quantrec,$daterecieved, $invoiceno, $cost, $freight, $rrp, $biblionumber,
+        $ordnum);
     $sth->finish;
 
     # Allows libraries to change their bookfund during receiving orders
@@ -599,6 +617,7 @@ sub ModReceiveOrder {
         $sth->execute( $bookfund, $ordnum );
         $sth->finish;
     }
+    return $daterecieved;
 }
 
 #------------------------------------------------------------#
@@ -638,26 +657,25 @@ C<@results> is an array of references-to-hash with the following keys:
 =cut
 
 sub SearchOrder {
-### Requires fixing for KOHA 3 API for performance. Currently just fiixed so it works
-## Very CPU expensive searches seems to be repeated!! 
-## This search can be directed to ZEBRA for title,isbn etc. ordernumber ,booksellerid to acquiorders
-    my ( $search, $id, $biblio, $catview ) = @_;
+    my ( $search, $id, $biblionumber, $catview ) = @_;
     my $dbh = C4::Context->dbh;
     my @data = split( ' ', $search );
     my @searchterms;
     if ($id) {
         @searchterms = ($id);
     }
-    map { push( @searchterms, "$_%", "% $_%" ) } @data;
-    push( @searchterms, $search, $search, $biblio );
+    map { push( @searchterms, "$_%", "%$_%" ) } @data;
+    push( @searchterms, $search, $search, $biblionumber );
     my $query;
-    if ($id) {
+  ### FIXME  THIS CAN raise a problem if more THAN ONE biblioitem is linked to one biblio  
+    if ($id) {  
         $query =
-          "SELECT *,biblio.title FROM aqorders,biblio,aqbasket
-            WHERE biblio.biblionumber=aqorders.biblionumber AND
-            aqorders.basketno = aqbasket.basketno
-            AND aqbasket.booksellerid = ?
-
+          "SELECT *,biblio.title 
+           FROM aqorders 
+           LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber 
+           LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber 
+           LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
+            WHERE aqbasket.booksellerid = ?
             AND ((datecancellationprinted is NULL)
             OR (datecancellationprinted = '0000-00-00'))
             AND (("
@@ -665,17 +683,17 @@ sub SearchOrder {
             join( " AND ",
                 map { "(biblio.title like ? or biblio.title like ?)" } @data )
           )
-          . ") OR biblio.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
+          . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
 
     }
     else {
         $query =
           " SELECT *,biblio.title
-            FROM   aqorders,biblio,aqbasket
-            WHERE  aqorders.biblionumber = biblio.biblionumber
-            AND    aqorders.basketno = aqbasket.basketno
-         
-            AND    ((datecancellationprinted is NULL)
+            FROM   aqorders
+            LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
+            LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
+            LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber      
+            WHERE  ((datecancellationprinted is NULL)
             OR     (datecancellationprinted = '0000-00-00'))
             AND    (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
             AND (("
@@ -683,15 +701,19 @@ sub SearchOrder {
             join( " AND ",
                 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
           )
-          . ") or biblio.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
+          . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
     }
     $query .= " GROUP BY aqorders.ordernumber";
+    ### $query
     my $sth = $dbh->prepare($query);
     $sth->execute(@searchterms);
     my @results = ();
-
-
-
+    my $query2 = "
+        SELECT *
+        FROM   biblio
+        WHERE  biblionumber=?
+    ";
+    my $sth2 = $dbh->prepare($query2);
     my $query3 = "
         SELECT *
         FROM   aqorderbreakdown
@@ -700,11 +722,8 @@ sub SearchOrder {
     my $sth3 = $dbh->prepare($query3);
 
     while ( my $data = $sth->fetchrow_hashref ) {
-## Retrieving a whole marc record just to extract seriestitle is very poor performance
-## Rewrite these searches
-my $record=MARCgetbiblio($dbh,$data->{'biblionumber'});
-my $data2=MARCmarc2koha($dbh,$record,"biblios");
-       
+        $sth2->execute( $data->{'biblionumber'} );
+        my $data2 = $sth2->fetchrow_hashref;
         $data->{'author'}      = $data2->{'author'};
         $data->{'seriestitle'} = $data2->{'seriestitle'};
         $sth3->execute( $data->{'ordernumber'} );
@@ -713,8 +732,9 @@ my $data2=MARCmarc2koha($dbh,$record,"biblios");
         $data->{'bookfundid'} = $data3->{'bookfundid'};
         push( @results, $data );
     }
+    ### @results
     $sth->finish;
-
+    $sth2->finish;
     $sth3->finish;
     return @results;
 }
@@ -736,7 +756,7 @@ cancelled.
 =cut
 
 sub DelOrder {
-    my ( $biblionumber, $ordnum ) = @_;
+    my ( $bibnum, $ordnum ) = @_;
     my $dbh = C4::Context->dbh;
     my $query = "
         UPDATE aqorders
@@ -744,15 +764,13 @@ sub DelOrder {
         WHERE  biblionumber=? AND ordernumber=?
     ";
     my $sth = $dbh->prepare($query);
-    $sth->execute( $biblionumber, $ordnum );
+    $sth->execute( $bibnum, $ordnum );
     $sth->finish;
 }
 
 
 =back
 
-=back
-
 =head2 FUNCTIONS ABOUT PARCELS
 
 =over 2
@@ -771,7 +789,7 @@ Looks up all of the received items from the supplier with the given
 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
 
 C<@results> is an array of references-to-hash. The keys of each element are fields from
-the aqorders, biblio tables of the Koha database.
+the aqorders, biblio, and biblioitems tables of the Koha database.
 
 C<@results> is sorted alphabetically by book title.
 
@@ -780,7 +798,6 @@ C<@results> is sorted alphabetically by book title.
 =cut
 
 sub GetParcel {
-
     #gets all orders from a certain supplier, orders them alphabetically
     my ( $supplierid, $code, $datereceived ) = @_;
     my $dbh     = C4::Context->dbh;
@@ -802,10 +819,11 @@ sub GetParcel {
                 aqorders.listprice,
                 aqorders.rrp,
                 aqorders.ecost
-        FROM aqorders,aqbasket
+        FROM aqorders 
+        LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
-        WHERE aqbasket.basketno=aqorders.basketno
-            AND aqbasket.booksellerid=?
+        WHERE 
+            aqbasket.booksellerid=?
             AND aqorders.booksellerinvoicenumber LIKE  \"$code\"
             AND aqorders.datereceived= \'$datereceived\'";
 
@@ -813,19 +831,20 @@ sub GetParcel {
         my $userenv = C4::Context->userenv;
         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
             $strsth .=
-                " and (borrowers.branchcode = '"
+                " AND (borrowers.branchcode = '"
               . $userenv->{branch}
-              . "' or borrowers.branchcode ='')";
+              . "' OR borrowers.branchcode ='')";
         }
     }
-    $strsth .= " order by aqbasket.basketno";
+    $strsth .= " ORDER BY aqbasket.basketno";
     ### parcelinformation : $strsth
+    warn "STH : $strsth";
     my $sth = $dbh->prepare($strsth);
     $sth->execute($supplierid);
     while ( my $data = $sth->fetchrow_hashref ) {
         push( @results, $data );
     }
-    ### countparcelbiblio: $count
+    ### countparcelbiblio: scalar(@results)
     $sth->finish;
 
     return @results;
@@ -878,9 +897,8 @@ sub GetParcels {
                 count(DISTINCT biblionumber) AS biblio,
                 sum(quantity) AS itemsexpected,
                 sum(quantityreceived) AS itemsreceived
-        FROM   aqorders, aqbasket
-        WHERE  aqbasket.basketno = aqorders.basketno
-             AND aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
+        FROM   aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
+        WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
     ";
 
     $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" " if ($code);
@@ -891,17 +909,13 @@ sub GetParcels {
 
     $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
     $strsth .= "order by $order " if ($order);
+### $strsth
     my $sth = $dbh->prepare($strsth);
 
     $sth->execute;
-    my @results;
-
-    while ( my $data2 = $sth->fetchrow_hashref ) {
-        push @results, $data2;
-    }
-
+    my $results = $sth->fetchall_arrayref({});
     $sth->finish;
-    return @results;
+    return @$results;
 }
 
 #------------------------------------------------------------#
@@ -922,7 +936,6 @@ the table of supplier with late issues. This table is full of hashref.
 =cut
 
 sub GetLateOrders {
-## requirse fixing for KOHA 3 API. Currently does not return publisher
     my $delay      = shift;
     my $supplierid = shift;
     my $branch     = shift;
@@ -936,7 +949,7 @@ sub GetLateOrders {
     #    warn " $dbdriver";
     if ( $dbdriver eq "mysql" ) {
         $strsth = "
-            SELECT aqbasket.basketno,
+            SELECT aqbasket.basketno,aqorders.ordernumber,
                 DATE(aqbasket.closedate) AS orderdate,
                 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
                 aqorders.rrp AS unitpricesupplier,
@@ -947,11 +960,12 @@ sub GetLateOrders {
                 aqbooksellers.name AS supplier,
                 aqorders.title,
                 biblio.author,
-               
+                biblioitems.publishercode AS publisher,
+                biblioitems.publicationyear,
                 DATEDIFF(CURDATE( ),closedate) AS latesince
-            FROM  ((
+            FROM  (((
                 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
-            
+            LEFT JOIN biblioitems ON  biblioitems.biblionumber=biblio.biblionumber)
             LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
             LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
             (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
@@ -988,11 +1002,12 @@ sub GetLateOrders {
                     aqbooksellers.name AS supplier,
                     biblio.title,
                     biblio.author,
-                   
+                    biblioitems.publishercode AS publisher,
+                    biblioitems.publicationyear,
                     (CURDATE -  closedate) AS latesince
-                    FROM(( 
+                    FROM(( (
                         (aqorders LEFT JOIN biblio on biblio.biblionumber = aqorders.biblionumber)
-                       
+                        LEFT JOIN biblioitems on  biblioitems.biblionumber=biblio.biblionumber)
                         LEFT JOIN aqorderbreakdown on aqorders.ordernumber = aqorderbreakdown.ordernumber)
                         LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
                         (aqbasket LEFT JOIN borrowers on aqbasket.authorisedby = borrowers.borrowernumber) LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
@@ -1055,20 +1070,19 @@ sub GetHistory {
                 aqorders.quantity,
                 aqorders.quantityreceived,
                 aqorders.ecost,
-                aqorders.ordernumber
-            FROM aqorders,aqbasket,aqbooksellers,biblio";
-
-        $query .= ",borrowers "
-          if ( C4::Context->preference("IndependantBranches") );
-
-        $query .="
-            WHERE aqorders.basketno=aqbasket.basketno
-            AND   aqbasket.booksellerid=aqbooksellers.id
-            AND   biblio.biblionumber=aqorders.biblionumber ";
-
-        $query .= " AND aqbasket.authorisedby=borrowers.borrowernumber"
+                aqorders.ordernumber,
+                aqorders.booksellerinvoicenumber as invoicenumber,
+                aqbooksellers.id as id,
+                aqorders.biblionumber
+            FROM aqorders 
+            LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno 
+            LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
+            LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
+
+        $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
           if ( C4::Context->preference("IndependantBranches") );
 
+        $query .= " WHERE 1 ";
         $query .= " AND biblio.title LIKE " . $dbh->quote( "%" . $title . "%" )
           if $title;
 
@@ -1083,6 +1097,7 @@ sub GetHistory {
 
         $query .= " AND creationdate<" . $dbh->quote($to_placed_on)
           if $to_placed_on;
+        $query .= " AND (datecancellationprinted is NULL or datecancellationprinted='0000-00-00')";
 
         if ( C4::Context->preference("IndependantBranches") ) {
             my $userenv = C4::Context->userenv;
@@ -1111,6 +1126,32 @@ sub GetHistory {
     return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
 }
 
+=head2 GetRecentAcqui
+
+   $results = GetRecentAcqui($days);
+
+   C<$results> is a ref to a table which containts hashref
+
+=cut
+
+sub GetRecentAcqui {
+    my $limit  = shift;
+    my $dbh    = C4::Context->dbh;
+    my $query = "
+        SELECT *
+        FROM   biblio
+        ORDER BY timestamp DESC
+        LIMIT  0,".$limit;
+
+    my $sth = $dbh->prepare($query);
+    $sth->execute;
+    my @results;
+    while(my $data = $sth->fetchrow_hashref){
+        push @results,$data;
+    }
+    return \@results;
+}
+
 END { }    # module clean-up code here (global destructor)
 
 1;