Bug 7700: Cart's more details view shows identity numbers
[koha_gimpoz] / C4 / Acquisition.pm
index e6a7f07..4fd5cc2 100644 (file)
@@ -13,28 +13,106 @@ package C4::Acquisition;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-# $Id$
 
 use strict;
-require Exporter;
+use warnings;
+use Carp;
 use C4::Context;
-use C4::Date;
+use C4::Debug;
+use C4::Dates qw(format_date format_date_in_iso);
 use MARC::Record;
 use C4::Suggestions;
+use C4::Biblio;
+use C4::Debug;
+use C4::SQLHelper qw(InsertInTable);
+
 use Time::localtime;
+use HTML::Entities;
 
 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 ); };
+BEGIN {
+    # set the version for version checking
+    $VERSION = 3.01;
+    require Exporter;
+    @ISA    = qw(Exporter);
+    @EXPORT = qw(
+        &GetBasket &NewBasket &CloseBasket &DelBasket &ModBasket
+       &GetBasketAsCSV
+        &GetBasketsByBookseller &GetBasketsByBasketgroup
+        &GetBasketsInfosByBookseller
+
+        &ModBasketHeader
+
+        &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
+        &GetBasketgroups &ReOpenBasketgroup
+
+        &NewOrder &DelOrder &ModOrder &GetPendingOrders &GetOrder &GetOrders
+        &GetOrderNumber &GetLateOrders &GetOrderFromItemnumber
+        &SearchOrder &GetHistory &GetRecentAcqui
+        &ModReceiveOrder &ModOrderBiblioitemNumber
+        &GetCancelledOrders
+
+        &NewOrderItem &ModOrderItem
+
+        &GetParcels &GetParcel
+        &GetContracts &GetContract
+
+        &GetItemnumbersFromOrder
+
+        &AddClaim
+    );
+}
+
+
+
+
+
+sub GetOrderFromItemnumber {
+    my ($itemnumber) = @_;
+    my $dbh          = C4::Context->dbh;
+    my $query        = qq|
+
+    SELECT  * from aqorders    LEFT JOIN aqorders_items
+    ON (     aqorders.ordernumber = aqorders_items.ordernumber   )
+    WHERE itemnumber = ?  |;
+
+    my $sth = $dbh->prepare($query);
+
+#    $sth->trace(3);
+
+    $sth->execute($itemnumber);
+
+    my $order = $sth->fetchrow_hashref;
+    return ( $order  );
+
+}
+
+# Returns the itemnumber(s) associated with the ordernumber given in parameter
+sub GetItemnumbersFromOrder {
+    my ($ordernumber) = @_;
+    my $dbh          = C4::Context->dbh;
+    my $query        = "SELECT itemnumber FROM aqorders_items WHERE ordernumber=?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($ordernumber);
+    my @tab;
+
+    while (my $order = $sth->fetchrow_hashref) {
+    push @tab, $order->{'itemnumber'};
+    }
+
+    return @tab;
+
+}
+
+
+
+
 
-# used in receiveorder subroutine
-# to provide library specific handling
-my $library_name = C4::Context->preference("LibraryName");
 
 =head1 NAME
 
@@ -51,42 +129,15 @@ orders, basket and parcels.
 
 =head1 FUNCTIONS
 
-=over 2
-
-=cut
-
-@ISA    = qw(Exporter);
-@EXPORT = qw(
-  &GetBasket &NewBasket &CloseBasket
-  &GetPendingOrders &GetOrder &GetOrders
-  &GetOrderNumber &GetLateOrders &NewOrder &DelOrder
-  &SearchOrder &GetHistory
-  &ModOrder &ModReceiveOrder &ModOrderBiblioNumber
-  &GetParcels &GetParcel
-);
-
 =head2 FUNCTIONS ABOUT BASKETS
 
-=over 2
-
-=cut
-
-#------------------------------------------------------------#
-
 =head3 GetBasket
 
-=over 4
-
-$aqbasket = &GetBasket($basketnumber);
+  $aqbasket = &GetBasket($basketnumber);
 
 get all basket informations in aqbasket for a given basket
 
-return :
-informations for a given basket returned as a hashref.
-
-=back
-
-=back
+B<returns:> informations for a given basket returned as a hashref.
 
 =cut
 
@@ -95,35 +146,43 @@ 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 );
 }
 
 #------------------------------------------------------------#
 
 =head3 NewBasket
 
-=over 4
-
-$basket = &NewBasket();
+  $basket = &NewBasket( $booksellerid, $authorizedby, $basketname, 
+      $basketnote, $basketbooksellernote, $basketcontractnumber );
 
 Create a new basket in aqbasket table
 
+=over
+
+=item C<$booksellerid> is a foreign key in the aqbasket table
+
+=item C<$authorizedby> is the username of who created the basket
+
 =back
 
+The other parameters are optional, see ModBasketHeader for more info on them.
+
 =cut
 
 # FIXME : this function seems to be unused.
 
 sub NewBasket {
-    my ( $booksellerid, $authorisedby ) = @_;
+    my ( $booksellerid, $authorisedby, $basketname, $basketnote, $basketbooksellernote, $basketcontractnumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $query = "
         INSERT INTO aqbasket
@@ -131,10 +190,10 @@ sub NewBasket {
         VALUES  (now(),'$booksellerid','$authorisedby')
     ";
     my $sth =
-      $dbh->do($query);
-
+    $dbh->do($query);
 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
     my $basket = $dbh->{'mysql_insertid'};
+    ModBasketHeader($basket, $basketname || '', $basketnote || '', $basketbooksellernote || '', $basketcontractnumber || undef);
     return $basket;
 }
 
@@ -142,14 +201,10 @@ sub NewBasket {
 
 =head3 CloseBasket
 
-=over 4
-
-&CloseBasket($basketno);
+  &CloseBasket($basketno);
 
 close a basket (becomes unmodifiable,except for recieves)
 
-=back
-
 =cut
 
 sub CloseBasket {
@@ -166,29 +221,512 @@ sub CloseBasket {
 
 #------------------------------------------------------------#
 
+=head3 GetBasketAsCSV
+
+  &GetBasketAsCSV($basketno);
+
+Export a basket as CSV
+
+=cut
+
+sub GetBasketAsCSV {
+    my ($basketno) = @_;
+    my $basket = GetBasket($basketno);
+    my @orders = GetOrders($basketno);
+    my $contract = GetContract($basket->{'contractnumber'});
+    my $csv = Text::CSV->new();
+    my $output; 
+
+    # TODO: Translate headers
+    my @headers = qw(contractname ordernumber entrydate isbn author title publishercode collectiontitle notes quantity rrp);
+
+    $csv->combine(@headers);                                                                                                        
+    $output = $csv->string() . "\n";   
+
+    my @rows;
+    foreach my $order (@orders) {
+       my @cols;
+       # newlines are not valid characters for Text::CSV combine()
+        $order->{'notes'} =~ s/[\r\n]+//g;
+       push(@cols,
+               $contract->{'contractname'},
+               $order->{'ordernumber'},
+               $order->{'entrydate'}, 
+               $order->{'isbn'},
+               $order->{'author'},
+               $order->{'title'},
+               $order->{'publishercode'},
+               $order->{'collectiontitle'},
+               $order->{'notes'},
+               $order->{'quantity'},
+               $order->{'rrp'},
+           );
+       push (@rows, \@cols);
+    }
+
+    foreach my $row (@rows) {
+       $csv->combine(@$row);                                                                                                                    
+       $output .= $csv->string() . "\n";    
+
+    }
+                                                                                                                                                      
+    return $output;             
+
+}
+
+
+=head3 CloseBasketgroup
+
+  &CloseBasketgroup($basketgroupno);
+
+close a basketgroup
+
+=cut
+
+sub CloseBasketgroup {
+    my ($basketgroupno) = @_;
+    my $dbh        = C4::Context->dbh;
+    my $sth = $dbh->prepare("
+        UPDATE aqbasketgroups
+        SET    closed=1
+        WHERE  id=?
+    ");
+    $sth->execute($basketgroupno);
+}
+
+#------------------------------------------------------------#
+
+=head3 ReOpenBaskergroup($basketgroupno)
+
+  &ReOpenBaskergroup($basketgroupno);
+
+reopen a basketgroup
+
+=cut
+
+sub ReOpenBasketgroup {
+    my ($basketgroupno) = @_;
+    my $dbh        = C4::Context->dbh;
+    my $sth = $dbh->prepare("
+        UPDATE aqbasketgroups
+        SET    closed=0
+        WHERE  id=?
+    ");
+    $sth->execute($basketgroupno);
+}
+
+#------------------------------------------------------------#
+
+
+=head3 DelBasket
+
+  &DelBasket($basketno);
+
+Deletes the basket that has basketno field $basketno in the aqbasket table.
+
+=over
+
+=item C<$basketno> is the primary key of the basket in the aqbasket table.
+
+=back
+
+=cut
+
+sub DelBasket {
+    my ( $basketno ) = @_;
+    my $query = "DELETE FROM aqbasket WHERE basketno=?";
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($basketno);
+    $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 ModBasket
+
+  &ModBasket($basketinfo);
+
+Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
+
+=over
+
+=item C<$basketno> is the primary key of the basket in the aqbasket table.
+
 =back
 
+=cut
+
+sub ModBasket {
+    my $basketinfo = shift;
+    my $query = "UPDATE aqbasket SET ";
+    my @params;
+    foreach my $key (keys %$basketinfo){
+        if ($key ne 'basketno'){
+            $query .= "$key=?, ";
+            push(@params, $basketinfo->{$key} || undef );
+        }
+    }
+# get rid of the "," at the end of $query
+    if (substr($query, length($query)-2) eq ', '){
+        chop($query);
+        chop($query);
+        $query .= ' ';
+    }
+    $query .= "WHERE basketno=?";
+    push(@params, $basketinfo->{'basketno'});
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@params);
+    $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 ModBasketHeader
+
+  &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber);
+
+Modifies a basket's header.
+
+=over
+
+=item C<$basketno> is the "basketno" field in the "aqbasket" table;
+
+=item C<$basketname> is the "basketname" field in the "aqbasket" table;
+
+=item C<$note> is the "note" field in the "aqbasket" table;
+
+=item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
+
+=item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
+
+=back
+
+=cut
+
+sub ModBasketHeader {
+    my ($basketno, $basketname, $note, $booksellernote, $contractnumber) = @_;
+    my $query = "UPDATE aqbasket SET basketname=?, note=?, booksellernote=? WHERE basketno=?";
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($basketname,$note,$booksellernote,$basketno);
+    if ( $contractnumber ) {
+        my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
+        my $sth2 = $dbh->prepare($query2);
+        $sth2->execute($contractnumber,$basketno);
+        $sth2->finish;
+    }
+    $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 GetBasketsByBookseller
+
+  @results = &GetBasketsByBookseller($booksellerid, $extra);
+
+Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
+
+=over
+
+=item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
+
+=item C<$extra> is the extra sql parameters, can be
+
+ $extra->{groupby}: group baskets by column
+    ex. $extra->{groupby} = aqbasket.basketgroupid
+ $extra->{orderby}: order baskets by column
+ $extra->{limit}: limit number of results (can be helpful for pagination)
+
+=back
+
+=cut
+
+sub GetBasketsByBookseller {
+    my ($booksellerid, $extra) = @_;
+    my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
+    if ($extra){
+        if ($extra->{groupby}) {
+            $query .= " GROUP by $extra->{groupby}";
+        }
+        if ($extra->{orderby}){
+            $query .= " ORDER by $extra->{orderby}";
+        }
+        if ($extra->{limit}){
+            $query .= " LIMIT $extra->{limit}";
+        }
+    }
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($booksellerid);
+    my $results = $sth->fetchall_arrayref({});
+    $sth->finish;
+    return $results
+}
+
+=head3 GetBasketsInfosByBookseller
+
+    my $baskets = GetBasketsInfosByBookseller($supplierid);
+
+Returns in a arrayref of hashref all about booksellers baskets, plus:
+    total_biblios: Number of distinct biblios in basket
+    total_items: Number of items in basket
+    expected_items: Number of non-received items in basket
+
+=cut
+
+sub GetBasketsInfosByBookseller {
+    my ($supplierid) = @_;
+
+    return unless $supplierid;
+
+    my $dbh = C4::Context->dbh;
+    my $query = qq{
+        SELECT aqbasket.*,
+          SUM(aqorders.quantity) AS total_items,
+          COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
+          SUM(IF(aqorders.datereceived IS NULL, aqorders.quantity, 0)) AS expected_items
+        FROM aqbasket
+          LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
+        WHERE booksellerid = ?
+        GROUP BY aqbasket.basketno
+    };
+    my $sth = $dbh->prepare($query);
+    $sth->execute($supplierid);
+    return $sth->fetchall_arrayref({});
+}
+
+
+#------------------------------------------------------------#
+
+=head3 GetBasketsByBasketgroup
+
+  $baskets = &GetBasketsByBasketgroup($basketgroupid);
+
+Returns a reference to all baskets that belong to basketgroup $basketgroupid.
+
+=cut
+
+sub GetBasketsByBasketgroup {
+    my $basketgroupid = shift;
+    my $query = "SELECT * FROM aqbasket
+                LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?";
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($basketgroupid);
+    my $results = $sth->fetchall_arrayref({});
+    $sth->finish;
+    return $results
+}
+
+#------------------------------------------------------------#
+
+=head3 NewBasketgroup
+
+  $basketgroupid = NewBasketgroup(\%hashref);
+
+Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
+
+$hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
+
+$hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
+
+$hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
+
+=cut
+
+sub NewBasketgroup {
+    my $basketgroupinfo = shift;
+    die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
+    my $query = "INSERT INTO aqbasketgroups (";
+    my @params;
+    foreach my $field ('name', 'deliveryplace', 'deliverycomment', 'closed') {
+        if ( $basketgroupinfo->{$field} ) {
+            $query .= "$field, ";
+            push(@params, $basketgroupinfo->{$field});
+        }
+    }
+    $query .= "booksellerid) VALUES (";
+    foreach (@params) {
+        $query .= "?, ";
+    }
+    $query .= "?)";
+    push(@params, $basketgroupinfo->{'booksellerid'});
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@params);
+    my $basketgroupid = $dbh->{'mysql_insertid'};
+    if( $basketgroupinfo->{'basketlist'} ) {
+        foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
+            my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
+            my $sth2 = $dbh->prepare($query2);
+            $sth2->execute($basketgroupid, $basketno);
+        }
+    }
+    return $basketgroupid;
+}
+
+#------------------------------------------------------------#
+
+=head3 ModBasketgroup
+
+  ModBasketgroup(\%hashref);
+
+Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
+
+$hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
+
+$hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
+
+$hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
+
+$hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
+
+=cut
+
+sub ModBasketgroup {
+    my $basketgroupinfo = shift;
+    die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
+    my $dbh = C4::Context->dbh;
+    my $query = "UPDATE aqbasketgroups SET ";
+    my @params;
+    foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
+        if ( defined $basketgroupinfo->{$field} ) {
+            $query .= "$field=?, ";
+            push(@params, $basketgroupinfo->{$field});
+        }
+    }
+    chop($query);
+    chop($query);
+    $query .= " WHERE id=?";
+    push(@params, $basketgroupinfo->{'id'});
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@params);
+
+    $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
+    $sth->execute($basketgroupinfo->{'id'});
+
+    if($basketgroupinfo->{'basketlist'} && @{$basketgroupinfo->{'basketlist'}}){
+        $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
+        foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
+            $sth->execute($basketgroupinfo->{'id'}, $basketno);
+            $sth->finish;
+        }
+    }
+    $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+=head3 DelBasketgroup
+
+  DelBasketgroup($basketgroupid);
+
+Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
+
+=over
+
+=item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
+
+=back
+
+=cut
+
+sub DelBasketgroup {
+    my $basketgroupid = shift;
+    die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
+    my $query = "DELETE FROM aqbasketgroups WHERE id=?";
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($basketgroupid);
+    $sth->finish;
+}
+
+#------------------------------------------------------------#
+
+
 =head2 FUNCTIONS ABOUT ORDERS
 
-=over 2
+=head3 GetBasketgroup
+
+  $basketgroup = &GetBasketgroup($basketgroupid);
+
+Returns a reference to the hash containing all infermation about the basketgroup.
 
 =cut
 
+sub GetBasketgroup {
+    my $basketgroupid = shift;
+    die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
+    my $query = "SELECT * FROM aqbasketgroups WHERE id=?";
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($basketgroupid);
+    my $result = $sth->fetchrow_hashref;
+    $sth->finish;
+    return $result
+}
+
 #------------------------------------------------------------#
 
-=head3 GetPendingOrders
+=head3 GetBasketgroups
 
-=over 4
+  $basketgroups = &GetBasketgroups($booksellerid);
+
+Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
+
+=cut
+
+sub GetBasketgroups {
+    my $booksellerid = shift;
+    die "bookseller id is required to edit a basketgroup" unless $booksellerid;
+    my $query = "SELECT * FROM aqbasketgroups WHERE booksellerid=? ORDER BY `id` DESC";
+    my $dbh = C4::Context->dbh;
+    my $sth = $dbh->prepare($query);
+    $sth->execute($booksellerid);
+    my $results = $sth->fetchall_arrayref({});
+    $sth->finish;
+    return $results
+}
 
-$orders = &GetPendingOrders($booksellerid);
+#------------------------------------------------------------#
+
+=head2 FUNCTIONS ABOUT ORDERS
+
+=cut
+
+#------------------------------------------------------------#
+
+=head3 GetPendingOrders
+
+  $orders = &GetPendingOrders($booksellerid, $grouped, $owner);
 
 Finds pending orders from the bookseller with the given ID. Ignores
 completed and cancelled orders.
 
+C<$booksellerid> contains the bookseller identifier
+C<$grouped> contains 0 or 1. 0 means returns the list, 1 means return the total
+C<$owner> contains 0 or 1. 0 means any owner. 1 means only the list of orders entered by the user itself.
+
 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
+=over
 
 =item C<authorizedby>
 
@@ -196,58 +734,63 @@ reference-to-hash with the following fields:
 
 =item C<basketno>
 
-These give the value of the corresponding field in the aqorders table
-of the Koha database.
-
 =back
 
-=back
+These give the value of the corresponding field in the aqorders table
+of the Koha database.
 
 Results are ordered from most to least recent.
 
 =cut
 
 sub GetPendingOrders {
-    my $supplierid = @_;
+    my ($supplierid,$grouped,$owner,$basketno) = @_;
     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,biblio.*,biblioitems.isbn,
+                    aqbasket.closedate, aqbasket.creationdate, aqbasket.basketname,
+                    aqorders.*
         FROM      aqorders
         LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
+        LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
+        LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
         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";
+    my @query_params = ( $supplierid );
+    my $userenv = C4::Context->userenv;
     if ( C4::Context->preference("IndependantBranches") ) {
-        my $userenv = C4::Context->userenv;
         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
-            $strsth .=
-                " and (borrowers.branchcode = '"
-              . $userenv->{branch}
-              . "' or borrowers.branchcode ='')";
+            $strsth .= " and (borrowers.branchcode = ?
+                        or borrowers.branchcode  = '')";
+            push @query_params, $userenv->{branch};
         }
     }
-    $strsth .= " group by basketno order by aqbasket.basketno";
-    my $sth = $dbh->prepare($strsth);
-    $sth->execute($supplierid);
-    my @results = ();
-    while ( my $data = $sth->fetchrow_hashref ) {
-        push( @results, $data );
+    if ($owner) {
+        $strsth .= " AND aqbasket.authorisedby=? ";
+        push @query_params, $userenv->{'number'};
+    }
+    if ($basketno) {
+        $strsth .= " AND aqbasket.basketno=? ";
+        push @query_params, $basketno;
     }
+    $strsth .= " group by aqbasket.basketno" if $grouped;
+    $strsth .= " order by aqbasket.basketno";
+
+    my $sth = $dbh->prepare($strsth);
+    $sth->execute( @query_params );
+    my $results = $sth->fetchall_arrayref({});
     $sth->finish;
-    return \@results;
+    return $results;
 }
 
 #------------------------------------------------------------#
 
 =head3 GetOrders
 
-=over 4
-
-@orders = &GetOrders($basketnumber, $orderby);
+  @orders = &GetOrders($basketnumber, $orderby);
 
 Looks up the pending (non-cancelled) orders with the given basket
 number. If C<$booksellerID> is non-empty, only orders from that seller
@@ -258,58 +801,51 @@ C<&basket> returns a two-element array. C<@orders> is an array of
 references-to-hash, whose keys are the fields from the aqorders,
 biblio, and biblioitems tables in the Koha database.
 
-=back
-
 =cut
 
 sub GetOrders {
     my ( $basketno, $orderby ) = @_;
     my $dbh   = C4::Context->dbh;
-    my $query ="
-        SELECT  aqorderbreakdown.*,
-                biblio.*,biblioitems.*,
+    my $query  ="
+        SELECT biblio.*,biblioitems.*,
                 aqorders.*,
+                aqbudgets.*,
                 biblio.title
-        FROM    aqorders,biblio,biblioitems
-        LEFT JOIN aqorderbreakdown ON
-                    aqorders.ordernumber=aqorderbreakdown.ordernumber
+        FROM    aqorders
+            LEFT JOIN aqbudgets        ON aqbudgets.budget_id = aqorders.budget_id
+            LEFT JOIN biblio           ON biblio.biblionumber = aqorders.biblionumber
+            LEFT JOIN biblioitems      ON biblioitems.biblionumber =biblio.biblionumber
         WHERE   basketno=?
-            AND biblio.biblionumber=aqorders.biblionumber
-            AND biblioitems.biblioitemnumber=aqorders.biblioitemnumber
             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
     ";
 
-    $orderby = "biblioitems.publishercode" unless $orderby;
+    $orderby = "biblioitems.publishercode,biblio.title" 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;
-    }
+    my $results = $sth->fetchall_arrayref({});
     $sth->finish;
-    return @results;
+    return @$results;
 }
 
 #------------------------------------------------------------#
 
 =head3 GetOrderNumber
 
-=over 4
-
-$ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
+  $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
 
 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
 
 Returns the number of this order.
 
+=over
+
 =item C<$ordernumber> is the order number.
 
 =back
 
 =cut
+
 sub GetOrderNumber {
     my ( $biblionumber,$biblioitemnumber ) = @_;
     my $dbh = C4::Context->dbh;
@@ -329,33 +865,28 @@ sub GetOrderNumber {
 
 =head3 GetOrder
 
-=over 4
-
-$order = &GetOrder($ordernumber);
+  $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, biblioitems, aqorders, and
-aqorderbreakdown tables of the Koha database.
-
-=back
+C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
 
 =cut
 
 sub GetOrder {
-    my ($ordnum) = @_;
+    my ($ordernumber) = @_;
     my $dbh      = C4::Context->dbh;
     my $query = "
-        SELECT *
-        FROM   biblio,biblioitems,aqorders
-        LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
+        SELECT biblioitems.*, biblio.*, aqorders.*
+        FROM   aqorders
+        LEFT JOIN biblio on           biblio.biblionumber=aqorders.biblionumber
+        LEFT JOIN biblioitems on       biblioitems.biblionumber=aqorders.biblionumber
         WHERE aqorders.ordernumber=?
-        AND   biblio.biblionumber=aqorders.biblionumber
-        AND   biblioitems.biblioitemnumber=aqorders.biblioitemnumber
+
     ";
     my $sth= $dbh->prepare($query);
-    $sth->execute($ordnum);
+    $sth->execute($ordernumber);
     my $data = $sth->fetchrow_hashref;
     $sth->finish;
     return $data;
@@ -363,202 +894,231 @@ sub GetOrder {
 
 #------------------------------------------------------------#
 
-=head3 NewOrder
+=head3 NewOrder
+
+  &NewOrder(\%hashref);
+
+Adds a new order to the database. Any argument that isn't described
+below is the new value of the field with the same name in the aqorders
+table of the Koha database.
+
+=over
+
+=item $hashref->{'basketno'} is the basketno foreign key in aqorders, it is mandatory
+
+=item $hashref->{'ordernumber'} is a "minimum order number."
+
+=item $hashref->{'budgetdate'} is effectively ignored.
+If it's undef (anything false) or the string 'now', the current day is used.
+Else, the upcoming July 1st is used.
+
+=item $hashref->{'subscription'} may be either "yes", or anything else for "no".
+
+=item $hashref->{'uncertainprice'} may be 0 for "the price is known" or 1 for "the price is uncertain"
+
+=item defaults entrydate to Now
+
+The following keys are used: "biblionumber", "title", "basketno", "quantity", "notes", "biblioitemnumber", "rrp", "ecost", "gst", "unitprice", "subscription", "sort1", "sort2", "booksellerinvoicenumber", "listprice", "budgetdate", "purchaseordernumber", "branchcode", "booksellerinvoicenumber", "bookfundid".
+
+=back
+
+=cut
+
+sub NewOrder {
+    my $orderinfo = shift;
+#### ------------------------------
+    my $dbh = C4::Context->dbh;
+    my @params;
+
+
+    # if these parameters are missing, we can't continue
+    for my $key (qw/basketno quantity biblionumber budget_id/) {
+        croak "Mandatory parameter $key missing" unless $orderinfo->{$key};
+    }
+
+    if ( defined $orderinfo->{subscription} && $orderinfo->{'subscription'} eq 'yes' ) {
+        $orderinfo->{'subscription'} = 1;
+    } else {
+        $orderinfo->{'subscription'} = 0;
+    }
+    $orderinfo->{'entrydate'} ||= C4::Dates->new()->output("iso");
+    if (!$orderinfo->{quantityreceived}) {
+        $orderinfo->{quantityreceived} = 0;
+    }
+
+    my $ordernumber=InsertInTable("aqorders",$orderinfo);
+    return ( $orderinfo->{'basketno'}, $ordernumber );
+}
+
+
+
+#------------------------------------------------------------#
+
+=head3 NewOrderItem
+
+  &NewOrderItem();
 
-=over 4
+=cut
 
-  &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
-    $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
-    $ecost, $gst, $budget, $unitprice, $subscription,
-    $booksellerinvoicenumber);
+sub NewOrderItem {
+    my ($itemnumber, $ordernumber)  = @_;
+    my $dbh = C4::Context->dbh;
+    my $query = qq|
+            INSERT INTO aqorders_items
+                (itemnumber, ordernumber)
+            VALUES (?,?)    |;
 
-Adds a new order to the database. Any argument that isn't described
-below is the new value of the field with the same name in the aqorders
-table of the Koha database.
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $itemnumber, $ordernumber);
+}
 
-C<$ordnum> is a "minimum order number." After adding the new entry to
-the aqorders table, C<&neworder> finds the first entry in aqorders
-with order number greater than or equal to C<$ordnum>, and adds an
-entry to the aqorderbreakdown table, with the order number just found,
-and the book fund ID of the newly-added order.
+#------------------------------------------------------------#
 
-C<$budget> is effectively ignored.
+=head3 ModOrder
 
-C<$subscription> may be either "yes", or anything else for "no".
+  &ModOrder(\%hashref);
 
-=back
+Modifies an existing order. Updates the order with order number
+$hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All 
+other keys of the hash update the fields with the same name in the aqorders 
+table of the Koha database.
 
 =cut
 
-sub NewOrder {
-   my (
-        $basketno,  $bibnum,       $title,        $quantity,
-        $listprice, $booksellerid, $authorisedby, $notes,
-        $bookfund,  $bibitemnum,   $rrp,          $ecost,
-        $gst,       $budget,       $cost,         $sub,
-        $invoice,   $sort1,        $sort2
-      )
-      = @_;
-
-    my $year  = localtime->year() + 1900;
-    my $month = localtime->mon() + 1;       # months starts at 0, add 1
-
-    if ( !$budget || $budget eq 'now' ) {
-        $budget = "now()";
-    }
+sub ModOrder {
+    my $orderinfo = shift;
 
-    # if month is july or more, budget start is 1 jul, next year.
-    elsif ( $month >= '7' ) {
-        ++$year;                            # add 1 to year , coz its next year
-        $budget = "'$year-07-01'";
-    }
-    else {
+    die "Ordernumber is required"     if $orderinfo->{'ordernumber'} eq  '' ;
+    die "Biblionumber is required"  if  $orderinfo->{'biblionumber'} eq '';
 
-        # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
-        $budget = "'$year-07-01'";
-    }
+    my $dbh = C4::Context->dbh;
+    my @params;
 
-    if ( $sub eq 'yes' ) {
-        $sub = 1;
-    }
-    else {
-        $sub = 0;
-    }
+    # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
+    $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
 
-    # if $basket empty, it's also a new basket, create it
-    unless ($basketno) {
-        $basketno = NewBasket( $booksellerid, $authorisedby );
+#    delete($orderinfo->{'branchcode'});
+    # the hash contains a lot of entries not in aqorders, so get the columns ...
+    my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
+    $sth->execute;
+    my $colnames = $sth->{NAME};
+    my $query = "UPDATE aqorders SET ";
+
+    foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
+        # ... and skip hash entries that are not in the aqorders table
+        # FIXME : probably not the best way to do it (would be better to have a correct hash)
+        next unless grep(/^$orderinfokey$/, @$colnames);
+            $query .= "$orderinfokey=?, ";
+            push(@params, $orderinfo->{$orderinfokey});
     }
 
-    my $dbh = C4::Context->dbh;
-    my $query = "
-        INSERT INTO aqorders
-           ( biblionumber,title,basketno,quantity,listprice,notes,
-           biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now() )
-    ";
-    my $sth = $dbh->prepare($query);
-
-    $sth->execute(
-        $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 = "
-        INSERT INTO aqorderbreakdown (ordernumber,bookfundid)
-        VALUES (?,?)
-    ";
+    $query .= "timestamp=NOW()  WHERE  ordernumber=?";
+#   push(@params, $specorderinfo{'ordernumber'});
+    push(@params, $orderinfo->{'ordernumber'} );
     $sth = $dbh->prepare($query);
-    $sth->execute( $ordnum, $bookfund );
+    $sth->execute(@params);
     $sth->finish;
-    return ( $basketno, $ordnum );
 }
 
 #------------------------------------------------------------#
 
-=head3 ModOrder
+=head3 ModOrderItem
 
-=over 4
+  &ModOrderItem(\%hashref);
 
-&ModOrder($title, $ordernumber, $quantity, $listprice,
-    $biblionumber, $basketno, $supplier, $who, $notes,
-    $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
-    $unitprice, $booksellerinvoicenumber);
+Modifies the itemnumber in the aqorders_items table. The input hash needs three entities:
 
-Modifies an existing order. Updates the order with order number
-C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
-update the fields with the same name in the aqorders table of the Koha
-database.
+=over
 
-Entries with order number C<$ordernumber> in the aqorderbreakdown
-table are also updated to the new book fund ID.
+=item - itemnumber: the old itemnumber
+=item - ordernumber: the order this item is attached to
+=item - newitemnumber: the new itemnumber we want to attach the line to
 
 =back
 
 =cut
 
-sub ModOrder {
-    my (
-        $title,      $ordnum,   $quantity, $listprice, $bibnum,
-        $basketno,   $supplier, $who,      $notes,     $bookfund,
-        $bibitemnum, $rrp,      $ecost,    $gst,       $budget,
-        $cost,       $invoice,  $sort1,    $sort2
-      )
-      = @_;
+sub ModOrderItem {
+    my $orderiteminfo = shift;
+    if (! $orderiteminfo->{'ordernumber'} || ! $orderiteminfo->{'itemnumber'} || ! $orderiteminfo->{'newitemnumber'}){
+        die "Ordernumber, itemnumber and newitemnumber is required";
+    }
+
     my $dbh = C4::Context->dbh;
-    my $query = "
-        UPDATE aqorders
-        SET    title=?,
-               quantity=?,listprice=?,basketno=?,
-               rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
-               notes=?,sort1=?, sort2=?
-        WHERE  ordernumber=? AND biblionumber=?
-    ";
-    my $sth = $dbh->prepare($query);
-    $sth->execute(
-        $title, $quantity, $listprice, $basketno, $rrp,
-        $ecost, $cost,     $invoice,   $notes,    $sort1,
-        $sort2, $ordnum,   $bibnum
-    );
-    $sth->finish;
-    my $query = "
-        UPDATE aqorderbreakdown
-        SET    bookfundid=?
-        WHERE  ordernumber=?
-    ";
-    $sth = $dbh->prepare($query);
 
-    unless ( $sth->execute( $bookfund, $ordnum ) )
-    {    # zero rows affected [Bug 734]
-        my $query ="
-            INSERT INTO aqorderbreakdown
-                     (ordernumber,bookfundid)
-            VALUES   (?,?)
-        ";
-        $sth = $dbh->prepare($query);
-        $sth->execute( $ordnum, $bookfund );
-    }
-    $sth->finish;
+    my $query = "UPDATE aqorders_items set itemnumber=? where itemnumber=? and ordernumber=?";
+    my @params = ($orderiteminfo->{'newitemnumber'}, $orderiteminfo->{'itemnumber'}, $orderiteminfo->{'ordernumber'});
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@params);
+    return 0;
 }
 
 #------------------------------------------------------------#
 
-=head3 ModOrderBiblioNumber
 
-=over 4
+=head3 ModOrderBibliotemNumber
 
-&ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
+  &ModOrderBiblioitemNumber($biblioitemnumber,$ordernumber, $biblionumber);
 
 Modifies the biblioitemnumber for an existing order.
 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
 
-=back
-
 =cut
 
-sub ModOrderBiblioNumber {
-    my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
+#FIXME: is this used at all?
+sub ModOrderBiblioitemNumber {
+    my ($biblioitemnumber,$ordernumber, $biblionumber) = @_;
     my $dbh = C4::Context->dbh;
     my $query = "
-      UPDATE aqorders
-      SET    biblioitemnumber = ?
-      WHERE  ordernumber = ?
-      AND biblionumber =  ?";
+    UPDATE aqorders
+    SET    biblioitemnumber = ?
+    WHERE  ordernumber = ?
+    AND biblionumber =  ?";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $biblioitemnumber, $ordernumber, $biblionumber );
+}
+
+=head3 GetCancelledOrders
+
+  my @orders = GetCancelledOrders($basketno, $orderby);
+
+Returns cancelled orders for a basket
+
+=cut
+
+sub GetCancelledOrders {
+    my ( $basketno, $orderby ) = @_;
+
+    return () unless $basketno;
+
+    my $dbh   = C4::Context->dbh;
+    my $query = "
+        SELECT biblio.*, biblioitems.*, aqorders.*, aqbudgets.*
+        FROM aqorders
+          LEFT JOIN aqbudgets   ON aqbudgets.budget_id = aqorders.budget_id
+          LEFT JOIN biblio      ON biblio.biblionumber = aqorders.biblionumber
+          LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
+        WHERE basketno = ?
+          AND (datecancellationprinted IS NOT NULL
+               AND datecancellationprinted <> '0000-00-00')
+    ";
+
+    $orderby = "aqorders.datecancellationprinted desc, aqorders.timestamp desc"
+        unless $orderby;
+    $query .= " ORDER BY $orderby";
     my $sth = $dbh->prepare($query);
-    $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
+    $sth->execute($basketno);
+    my $results = $sth->fetchall_arrayref( {} );
+
+    return @$results;
 }
 
+
 #------------------------------------------------------------#
 
 =head3 ModReceiveOrder
 
-=over 4
-
-&ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
+  &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
     $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
     $freight, $bookfund, $rrp);
 
@@ -566,52 +1126,71 @@ Updates an order, to reflect the fact that it was received, at least
 in part. All arguments not mentioned below update the fields with the
 same name in the aqorders table of the Koha database.
 
+If a partial order is received, splits the order into two.  The received
+portion must have a booksellerinvoicenumber.
+
 Updates the order with bibilionumber C<$biblionumber> and ordernumber
 C<$ordernumber>.
 
-Also updates the book fund ID in the aqorderbreakdown table.
-
-=back
-
 =cut
 
 
 sub ModReceiveOrder {
     my (
-        $biblio,    $ordnum,  $quantrec, $user, $cost,
-        $invoiceno, $freight, $rrp,      $bookfund
-      )
-      = @_;
+        $biblionumber,    $ordernumber,  $quantrec, $user, $cost,
+        $invoiceno, $freight, $rrp, $budget_id, $datereceived
+    )
+    = @_;
     my $dbh = C4::Context->dbh;
-    my $query = "
-        UPDATE aqorders
-        SET    quantityreceived=?,datereceived=now(),booksellerinvoicenumber=?,
-               unitprice=?,freight=?,rrp=?
-        WHERE biblionumber=? AND ordernumber=?
-    ";
-    my $sth = $dbh->prepare($query);
-    my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblio );
+    $datereceived = C4::Dates->output('iso') unless $datereceived;
+    my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
     if ($suggestionid) {
-        ModStatus( $suggestionid, 'AVAILABLE', '', $biblio );
+        ModSuggestion( {suggestionid=>$suggestionid,
+                        STATUS=>'AVAILABLE',
+                        biblionumber=> $biblionumber}
+                        );
     }
-    $sth->execute( $quantrec, $invoiceno, $cost, $freight, $rrp, $biblio,
-        $ordnum );
-    $sth->finish;
 
-    # Allows libraries to change their bookfund during receiving orders
-    # allows them to adjust budgets
-    if ( C4::Context->preferene("LooseBudgets") ) {
-        my $query = "
-            UPDATE aqorderbreakdown
-            SET    bookfundid=?
-            WHERE  ordernumber=?
-        ";
-        my $sth = $dbh->prepare($query);
-        $sth->execute( $bookfund, $ordnum );
+    my $sth=$dbh->prepare("
+        SELECT * FROM   aqorders
+        WHERE           biblionumber=? AND aqorders.ordernumber=?");
+
+    $sth->execute($biblionumber,$ordernumber);
+    my $order = $sth->fetchrow_hashref();
+    $sth->finish();
+
+    if ( $order->{quantity} > $quantrec ) {
+        $sth=$dbh->prepare("
+            UPDATE aqorders
+            SET quantityreceived=?
+                , datereceived=?
+                , booksellerinvoicenumber=?
+                , unitprice=?
+                , freight=?
+                , rrp=?
+                , quantity=?
+            WHERE biblionumber=? AND ordernumber=?");
+
+        $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordernumber);
+        $sth->finish;
+
+        # create a new order for the remaining items, and set its bookfund.
+        foreach my $orderkey ( "linenumber", "allocation" ) {
+            delete($order->{'$orderkey'});
+        }
+        $order->{'quantity'} -= $quantrec;
+        $order->{'quantityreceived'} = 0;
+        my $newOrder = NewOrder($order);
+} else {
+        $sth=$dbh->prepare("update aqorders
+                            set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
+                                unitprice=?,freight=?,rrp=?
+                            where biblionumber=? and ordernumber=?");
+        $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordernumber);
         $sth->finish;
     }
+    return $datereceived;
 }
-
 #------------------------------------------------------------#
 
 =head3 SearchOrder
@@ -649,102 +1228,57 @@ C<@results> is an array of references-to-hash with the following keys:
 =cut
 
 sub SearchOrder {
-    my ( $search, $id, $biblio, $catview ) = @_;
+#### -------- SearchOrder-------------------------------
+    my ($ordernumber, $search, $supplierid, $basket) = @_;
+
     my $dbh = C4::Context->dbh;
-    my @data = split( ' ', $search );
-    my @searchterms;
-    if ($id) {
-        @searchterms = ($id);
+    my @args = ();
+    my $query =
+            "SELECT *
+            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  (datecancellationprinted is NULL)";
+
+    if($ordernumber){
+        $query .= " AND (aqorders.ordernumber=?)";
+        push @args, $ordernumber;
     }
-    map { push( @searchterms, "$_%", "% $_%" ) } @data;
-    push( @searchterms, $search, $search, $biblio );
-    my $query;
-    if ($id) {
-        $query =
-          "SELECT *,biblio.title FROM aqorders,biblioitems,biblio,aqbasket
-            WHERE aqorders.biblioitemnumber = biblioitems.biblioitemnumber AND
-            aqorders.basketno = aqbasket.basketno
-            AND aqbasket.booksellerid = ?
-            AND biblio.biblionumber=aqorders.biblionumber
-            AND ((datecancellationprinted is NULL)
-            OR (datecancellationprinted = '0000-00-00'))
-            AND (("
-          . (
-            join( " AND ",
-                map { "(biblio.title like ? or biblio.title like ?)" } @data )
-          )
-          . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
-
+    if($search){
+        $query .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
+        push @args, ("%$search%","%$search%","%$search%");
     }
-    else {
-        $query =
-          " SELECT *,biblio.title
-            FROM   aqorders,biblioitems,biblio,aqbasket
-            WHERE  aqorders.biblioitemnumber = biblioitems.biblioitemnumber
-            AND    aqorders.basketno = aqbasket.basketno
-            AND    biblio.biblionumber=aqorders.biblionumber
-            AND    ((datecancellationprinted is NULL)
-            OR     (datecancellationprinted = '0000-00-00'))
-            AND    (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
-            AND (("
-          . (
-            join( " AND ",
-                map { "(biblio.title like ? OR biblio.title like ?)" } @data )
-          )
-          . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
+    if($supplierid){
+        $query .= "AND aqbasket.booksellerid = ?";
+        push @args, $supplierid;
     }
-    $query .= " GROUP BY aqorders.ordernumber";
-    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
-        WHERE  ordernumber=?
-    ";
-    my $sth3 = $dbh->prepare($query3);
-
-    while ( my $data = $sth->fetchrow_hashref ) {
-        $sth2->execute( $data->{'biblionumber'} );
-        my $data2 = $sth2->fetchrow_hashref;
-        $data->{'author'}      = $data2->{'author'};
-        $data->{'seriestitle'} = $data2->{'seriestitle'};
-        $sth3->execute( $data->{'ordernumber'} );
-        my $data3 = $sth3->fetchrow_hashref;
-        $data->{'branchcode'} = $data3->{'branchcode'};
-        $data->{'bookfundid'} = $data3->{'bookfundid'};
-        push( @results, $data );
+    if($basket){
+        $query .= "AND aqorders.basketno = ?";
+        push @args, $basket;
     }
+
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@args);
+    my $results = $sth->fetchall_arrayref({});
     $sth->finish;
-    $sth2->finish;
-    $sth3->finish;
-    return @results;
+    return $results;
 }
 
 #------------------------------------------------------------#
 
 =head3 DelOrder
 
-=over 4
-
-&DelOrder($biblionumber, $ordernumber);
+  &DelOrder($biblionumber, $ordernumber);
 
 Cancel the order with the given order and biblio numbers. It does not
 delete any entries in the aqorders table, it merely marks them as
 cancelled.
 
-=back
-
 =cut
 
 sub DelOrder {
-    my ( $bibnum, $ordnum ) = @_;
+    my ( $bibnum, $ordernumber ) = @_;
     my $dbh = C4::Context->dbh;
     my $query = "
         UPDATE aqorders
@@ -752,28 +1286,24 @@ sub DelOrder {
         WHERE  biblionumber=? AND ordernumber=?
     ";
     my $sth = $dbh->prepare($query);
-    $sth->execute( $bibnum, $ordnum );
+    $sth->execute( $bibnum, $ordernumber );
     $sth->finish;
+    my @itemnumbers = GetItemnumbersFromOrder( $ordernumber );
+    foreach my $itemnumber (@itemnumbers){
+       C4::Items::DelItem( $dbh, $bibnum, $itemnumber );
+    }
+    
 }
 
-
-=back
-
-=back
-
 =head2 FUNCTIONS ABOUT PARCELS
 
-=over 2
-
 =cut
 
 #------------------------------------------------------------#
 
 =head3 GetParcel
 
-=over 4
-
-@results = &GetParcel($booksellerid, $code, $date);
+  @results = &GetParcel($booksellerid, $code, $date);
 
 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.
@@ -783,18 +1313,15 @@ the aqorders, biblio, and biblioitems tables of the Koha database.
 
 C<@results> is sorted alphabetically by book title.
 
-=back
-
 =cut
 
 sub GetParcel {
-
     #gets all orders from a certain supplier, orders them alphabetically
     my ( $supplierid, $code, $datereceived ) = @_;
     my $dbh     = C4::Context->dbh;
     my @results = ();
     $code .= '%'
-      if $code;  # add % if we search on a given code (otherwise, let him empty)
+    if $code;  # add % if we search on a given code (otherwise, let him empty)
     my $strsth ="
         SELECT  authorisedby,
                 creationdate,
@@ -802,38 +1329,40 @@ sub GetParcel {
                 closedate,surname,
                 firstname,
                 aqorders.biblionumber,
-                aqorders.title,
                 aqorders.ordernumber,
                 aqorders.quantity,
                 aqorders.quantityreceived,
                 aqorders.unitprice,
                 aqorders.listprice,
                 aqorders.rrp,
-                aqorders.ecost
-        FROM aqorders,aqbasket
+                aqorders.ecost,
+                biblio.title
+        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=?
-            AND aqorders.booksellerinvoicenumber LIKE  \"$code\"
-            AND aqorders.datereceived= \'$datereceived\'";
+        LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
+        WHERE
+            aqbasket.booksellerid = ?
+            AND aqorders.booksellerinvoicenumber LIKE ?
+            AND aqorders.datereceived = ? ";
 
+    my @query_params = ( $supplierid, $code, $datereceived );
     if ( C4::Context->preference("IndependantBranches") ) {
         my $userenv = C4::Context->userenv;
         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
-            $strsth .=
-                " and (borrowers.branchcode = '"
-              . $userenv->{branch}
-              . "' or borrowers.branchcode ='')";
+            $strsth .= " and (borrowers.branchcode = ?
+                        or borrowers.branchcode  = '')";
+            push @query_params, $userenv->{branch};
         }
     }
-    $strsth .= " order by aqbasket.basketno";
-    ### parcelinformation : $strsth
+    $strsth .= " ORDER BY aqbasket.basketno";
+    # ## parcelinformation : $strsth
     my $sth = $dbh->prepare($strsth);
-    $sth->execute($supplierid);
+    $sth->execute( @query_params );
     while ( my $data = $sth->fetchrow_hashref ) {
         push( @results, $data );
     }
-    ### countparcelbiblio: $count
+    # ## countparcelbiblio: scalar(@results)
     $sth->finish;
 
     return @results;
@@ -843,13 +1372,14 @@ sub GetParcel {
 
 =head3 GetParcels
 
-=over 4
+  $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
 
-$results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
 get a lists of parcels.
 
 * Input arg :
 
+=over
+
 =item $bookseller
 is the bookseller this function has to get parcels.
 
@@ -862,9 +1392,13 @@ is the booksellerinvoicenumber.
 =item $datefrom & $dateto
 to know on what date this function has to filter its search.
 
+=back
+
 * return:
 a pointer on a hash list containing parcel informations as such :
 
+=over
+
 =item Creation date
 
 =item Last operation
@@ -880,53 +1414,59 @@ a pointer on a hash list containing parcel informations as such :
 sub GetParcels {
     my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
     my $dbh    = C4::Context->dbh;
+    my @query_params = ();
     my $strsth ="
         SELECT  aqorders.booksellerinvoicenumber,
-                datereceived,
+                datereceived,purchaseordernumber,
                 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 = ? and datereceived IS NOT NULL
     ";
+    push @query_params, $bookseller;
 
-    $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" " if ($code);
+    if ( defined $code ) {
+        $strsth .= ' and aqorders.booksellerinvoicenumber like ? ';
+        # add a % to the end of the code to allow stemming.
+        push @query_params, "$code%";
+    }
 
-    $strsth .= "and datereceived >=" . $dbh->quote($datefrom) . " " if ($datefrom);
+    if ( defined $datefrom ) {
+        $strsth .= ' and datereceived >= ? ';
+        push @query_params, $datefrom;
+    }
 
-    $strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto);
+    if ( defined $dateto ) {
+        $strsth .=  'and datereceived <= ? ';
+        push @query_params, $dateto;
+    }
 
     $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
-    $strsth .= "order by $order " if ($order);
-    my $sth = $dbh->prepare($strsth);
 
-    $sth->execute;
-    my @results;
+    # can't use a placeholder to place this column name.
+    # but, we could probably be checking to make sure it is a column that will be fetched.
+    $strsth .= "order by $order " if ($order);
 
-    while ( my $data2 = $sth->fetchrow_hashref ) {
-        push @results, $data2;
-    }
+    my $sth = $dbh->prepare($strsth);
 
+    $sth->execute( @query_params );
+    my $results = $sth->fetchall_arrayref({});
     $sth->finish;
-    return @results;
+    return @$results;
 }
 
 #------------------------------------------------------------#
 
 =head3 GetLateOrders
 
-=over 4
-
-@results = &GetLateOrders;
+  @results = &GetLateOrders;
 
 Searches for bookseller with late orders.
 
 return:
 the table of supplier with late issues. This table is full of hashref.
 
-=back
-
 =cut
 
 sub GetLateOrders {
@@ -937,96 +1477,85 @@ sub GetLateOrders {
     my $dbh = C4::Context->dbh;
 
     #BEWARE, order of parenthesis and LEFT JOIN is important for speed
-    my $strsth;
     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
 
-    #    warn " $dbdriver";
-    if ( $dbdriver eq "mysql" ) {
-        $strsth = "
-            SELECT aqbasket.basketno,
-                DATE(aqbasket.closedate) AS orderdate,
-                aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
-                aqorders.rrp AS unitpricesupplier,
-                aqorders.ecost AS unitpricelib,
-                (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
-                aqbookfund.bookfundname AS budget,
-                borrowers.branchcode AS branch,
-                aqbooksellers.name AS supplier,
-                aqorders.title,
-                biblio.author,
-                biblioitems.publishercode AS publisher,
-                biblioitems.publicationyear,
-                DATEDIFF(CURDATE( ),closedate) AS latesince
-            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
-            WHERE aqorders.basketno = aqbasket.basketno
-            AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY))
-            AND ((datereceived = '' OR datereceived is null)
-            OR (aqorders.quantityreceived < aqorders.quantity) )
+    my @query_params = ($delay);       # delay is the first argument regardless
+    my $select = "
+    SELECT aqbasket.basketno,
+        aqorders.ordernumber,
+        DATE(aqbasket.closedate)  AS orderdate,
+        aqorders.rrp              AS unitpricesupplier,
+        aqorders.ecost            AS unitpricelib,
+        aqorders.claims_count     AS claims_count,
+        aqorders.claimed_date     AS claimed_date,
+        aqbudgets.budget_name     AS budget,
+        borrowers.branchcode      AS branch,
+        aqbooksellers.name        AS supplier,
+        aqbooksellers.id          AS supplierid,
+        biblio.author, biblio.title,
+        biblioitems.publishercode AS publisher,
+        biblioitems.publicationyear,
+    ";
+    my $from = "
+    FROM
+        aqorders LEFT JOIN biblio     ON biblio.biblionumber         = aqorders.biblionumber
+        LEFT JOIN biblioitems         ON biblioitems.biblionumber    = biblio.biblionumber
+        LEFT JOIN aqbudgets           ON aqorders.budget_id          = aqbudgets.budget_id,
+        aqbasket LEFT JOIN borrowers  ON aqbasket.authorisedby       = borrowers.borrowernumber
+        LEFT JOIN aqbooksellers       ON aqbasket.booksellerid       = aqbooksellers.id
+        WHERE aqorders.basketno = aqbasket.basketno
+        AND ( datereceived = ''
+            OR datereceived IS NULL
+            OR aqorders.quantityreceived < aqorders.quantity
+        )
+        AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
+    ";
+    my $having = "";
+    if ($dbdriver eq "mysql") {
+        $select .= "
+        aqorders.quantity - IFNULL(aqorders.quantityreceived,0)                 AS quantity,
+        (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
+        DATEDIFF(CURDATE( ),closedate) AS latesince
         ";
-        $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
-        $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'"
-          if ($branch);
-        $strsth .=
-          " AND borrowers.branchcode like \'"
-          . C4::Context->userenv->{branch} . "\'"
-          if ( C4::Context->preference("IndependantBranches")
-            && C4::Context->userenv
-            && C4::Context->userenv->{flags} != 1 );
-        $strsth .=" HAVING quantity<>0
-                    AND unitpricesupplier<>0
-                    AND unitpricelib<>0
-                    ORDER BY latesince,basketno,borrowers.branchcode, supplier
+        $from .= " AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL ? DAY)) ";
+        $having = "
+        HAVING quantity          <> 0
+            AND unitpricesupplier <> 0
+            AND unitpricelib      <> 0
         ";
+    } else {
+        # FIXME: account for IFNULL as above
+        $select .= "
+                aqorders.quantity                AS quantity,
+                aqorders.quantity * aqorders.rrp AS subtotal,
+                (CURDATE - closedate)            AS latesince
+        ";
+        $from .= " AND (closedate <= (CURDATE -(INTERVAL ? DAY)) ";
     }
-    else {
-        $strsth = "
-            SELECT aqbasket.basketno,
-                   DATE(aqbasket.closedate) AS orderdate,
-                    aqorders.quantity, aqorders.rrp AS unitpricesupplier,
-                    aqorders.ecost as unitpricelib,
-                    aqorders.quantity * aqorders.rrp AS subtotal
-                    aqbookfund.bookfundname AS budget,
-                    borrowers.branchcode AS branch,
-                    aqbooksellers.name AS supplier,
-                    biblio.title,
-                    biblio.author,
-                    biblioitems.publishercode AS publisher,
-                    biblioitems.publicationyear,
-                    (CURDATE -  closedate) AS latesince
-                    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
-                    WHERE aqorders.basketno = aqbasket.basketno
-                    AND (closedate < (CURDATE -(INTERVAL $delay DAY))
-                    AND ((datereceived = '' OR datereceived is null)
-                    OR (aqorders.quantityreceived < aqorders.quantity) ) ";
-        $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
-
-        $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'" if ($branch);
-        $strsth .=" AND borrowers.branchcode like \'". C4::Context->userenv->{branch} . "\'"
-            if (C4::Context->preference("IndependantBranches") && C4::Context->userenv->{flags} != 1 );
-        $strsth .=" ORDER BY latesince,basketno,borrowers.branchcode, supplier";
+    if (defined $supplierid) {
+        $from .= ' AND aqbasket.booksellerid = ? ';
+        push @query_params, $supplierid;
     }
-    my $sth = $dbh->prepare($strsth);
-    $sth->execute;
+    if (defined $branch) {
+        $from .= ' AND borrowers.branchcode LIKE ? ';
+        push @query_params, $branch;
+    }
+    if (C4::Context->preference("IndependantBranches")
+            && C4::Context->userenv
+            && C4::Context->userenv->{flags} != 1 ) {
+        $from .= ' AND borrowers.branchcode LIKE ? ';
+        push @query_params, C4::Context->userenv->{branch};
+    }
+    my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
+    $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@query_params);
     my @results;
-    my $hilighted = 1;
-    while ( my $data = $sth->fetchrow_hashref ) {
-        $data->{hilighted} = $hilighted if ( $hilighted > 0 );
-        $data->{orderdate} = format_date( $data->{orderdate} );
+    while (my $data = $sth->fetchrow_hashref) {
+        $data->{orderdate} = format_date($data->{orderdate});
+        $data->{claimed_date} = format_date($data->{claimed_date});
         push @results, $data;
-        $hilighted = -$hilighted;
     }
-    $sth->finish;
     return @results;
 }
 
@@ -1034,102 +1563,287 @@ sub GetLateOrders {
 
 =head3 GetHistory
 
-=over 4
-
-(\@order_loop, $total_qty, $total_price, $total_qtyreceived)=&GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on )
-
-this function get the search history.
-
-=back
+  (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( %params );
+
+Retreives some acquisition history information
+
+params:  
+  title
+  author
+  name
+  from_placed_on
+  to_placed_on
+  basket                  - search both basket name and number
+  booksellerinvoicenumber 
+
+returns:
+    $order_loop is a list of hashrefs that each look like this:
+            {
+                'author'           => 'Twain, Mark',
+                'basketno'         => '1',
+                'biblionumber'     => '215',
+                'count'            => 1,
+                'creationdate'     => 'MM/DD/YYYY',
+                'datereceived'     => undef,
+                'ecost'            => '1.00',
+                'id'               => '1',
+                'invoicenumber'    => undef,
+                'name'             => '',
+                'ordernumber'      => '1',
+                'quantity'         => 1,
+                'quantityreceived' => undef,
+                'title'            => 'The Adventures of Huckleberry Finn'
+            }
+    $total_qty is the sum of all of the quantities in $order_loop
+    $total_price is the cost of each in $order_loop times the quantity
+    $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
 
 =cut
 
 sub GetHistory {
-    my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
+# don't run the query if there are no parameters (list would be too long for sure !)
+    croak "No search params" unless @_;
+    my %params = @_;
+    my $title = $params{title};
+    my $author = $params{author};
+    my $isbn   = $params{isbn};
+    my $name = $params{name};
+    my $from_placed_on = $params{from_placed_on};
+    my $to_placed_on = $params{to_placed_on};
+    my $basket = $params{basket};
+    my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
+
     my @order_loop;
     my $total_qty         = 0;
     my $total_qtyreceived = 0;
     my $total_price       = 0;
 
-# don't run the query if there are no parameters (list would be too long for sure !)
-    if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
-        my $dbh   = C4::Context->dbh;
-        my $query ="
-            SELECT
-                biblio.title,
-                biblio.author,
-                aqorders.basketno,
-                name,aqbasket.creationdate,
-                aqorders.datereceived,
-                aqorders.quantity,
-                aqorders.quantityreceived,
-                aqorders.ecost,
-                aqorders.ordernumber
-            FROM aqorders,aqbasket,aqbooksellers,biblio";
-
-        $query .= ",borrowers "
-          if ( C4::Context->preference("IndependantBranches") );
+    my $dbh   = C4::Context->dbh;
+    my $query ="
+        SELECT
+            biblio.title,
+            biblio.author,
+           biblioitems.isbn,
+            aqorders.basketno,
+    aqbasket.basketname,
+    aqbasket.basketgroupid,
+    aqbasketgroups.name as groupname,
+            aqbooksellers.name,
+    aqbasket.creationdate,
+            aqorders.datereceived,
+            aqorders.quantity,
+            aqorders.quantityreceived,
+            aqorders.ecost,
+            aqorders.ordernumber,
+            aqorders.booksellerinvoicenumber as invoicenumber,
+            aqbooksellers.id as id,
+            aqorders.biblionumber
+        FROM aqorders
+        LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
+    LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
+        LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
+       LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
+        LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
+
+    $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
+    if ( C4::Context->preference("IndependantBranches") );
+
+    $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
+
+    my @query_params  = ();
+
+    if ( $title ) {
+        $query .= " AND biblio.title LIKE ? ";
+        $title =~ s/\s+/%/g;
+        push @query_params, "%$title%";
+    }
 
-        $query .="
-            WHERE aqorders.basketno=aqbasket.basketno
-            AND   aqbasket.booksellerid=aqbooksellers.id
-            AND   biblio.biblionumber=aqorders.biblionumber ";
+    if ( $author ) {
+        $query .= " AND biblio.author LIKE ? ";
+        push @query_params, "%$author%";
+    }
 
-        $query .= " AND aqbasket.authorisedby=borrowers.borrowernumber"
-          if ( C4::Context->preference("IndependantBranches") );
+    if ( $isbn ) {
+        $query .= " AND biblioitems.isbn LIKE ? ";
+        push @query_params, "%$isbn%";
+    }
 
-        $query .= " AND biblio.title LIKE " . $dbh->quote( "%" . $title . "%" )
-          if $title;
+    if ( $name ) {
+        $query .= " AND aqbooksellers.name LIKE ? ";
+        push @query_params, "%$name%";
+    }
 
-        $query .=
-          " AND biblio.author LIKE " . $dbh->quote( "%" . $author . "%" )
-          if $author;
+    if ( $from_placed_on ) {
+        $query .= " AND creationdate >= ? ";
+        push @query_params, $from_placed_on;
+    }
 
-        $query .= " AND name LIKE " . $dbh->quote( "%" . $name . "%" ) if $name;
+    if ( $to_placed_on ) {
+        $query .= " AND creationdate <= ? ";
+        push @query_params, $to_placed_on;
+    }
 
-        $query .= " AND creationdate >" . $dbh->quote($from_placed_on)
-          if $from_placed_on;
+    if ($basket) {
+        if ($basket =~ m/^\d+$/) {
+            $query .= " AND aqorders.basketno = ? ";
+            push @query_params, $basket;
+        } else {
+            $query .= " AND aqbasket.basketname LIKE ? ";
+            push @query_params, "%$basket%";
+        }
+    }
 
-        $query .= " AND creationdate<" . $dbh->quote($to_placed_on)
-          if $to_placed_on;
+    if ($booksellerinvoicenumber) {
+        $query .= " AND (aqorders.booksellerinvoicenumber LIKE ? OR aqbasket.booksellerinvoicenumber LIKE ?)";
+        push @query_params, "%$booksellerinvoicenumber%", "%$booksellerinvoicenumber%";
+    }
 
-        if ( C4::Context->preference("IndependantBranches") ) {
-            my $userenv = C4::Context->userenv;
-            if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
-                $query .=
-                    " AND (borrowers.branchcode = '"
-                  . $userenv->{branch}
-                  . "' OR borrowers.branchcode ='')";
-            }
-        }
-        $query .= " ORDER BY booksellerid";
-        my $sth = $dbh->prepare($query);
-        $sth->execute;
-        my $cnt = 1;
-        while ( my $line = $sth->fetchrow_hashref ) {
-            $line->{count} = $cnt++;
-            $line->{toggle} = 1 if $cnt % 2;
-            push @order_loop, $line;
-            $line->{creationdate} = format_date( $line->{creationdate} );
-            $line->{datereceived} = format_date( $line->{datereceived} );
-            $total_qty         += $line->{'quantity'};
-            $total_qtyreceived += $line->{'quantityreceived'};
-            $total_price       += $line->{'quantity'} * $line->{'ecost'};
+    if ( C4::Context->preference("IndependantBranches") ) {
+        my $userenv = C4::Context->userenv;
+        if ( $userenv && ($userenv->{flags} || 0) != 1 ) {
+            $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
+            push @query_params, $userenv->{branch};
         }
     }
+    $query .= " ORDER BY id";
+    my $sth = $dbh->prepare($query);
+    $sth->execute( @query_params );
+    my $cnt = 1;
+    while ( my $line = $sth->fetchrow_hashref ) {
+        $line->{count} = $cnt++;
+        $line->{toggle} = 1 if $cnt % 2;
+        push @order_loop, $line;
+        $total_qty         += $line->{'quantity'};
+        $total_qtyreceived += $line->{'quantityreceived'};
+        $total_price       += $line->{'quantity'} * $line->{'ecost'};
+    }
     return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
 }
 
-END { }    # module clean-up code here (global destructor)
+=head2 GetRecentAcqui
 
-1;
+  $results = GetRecentAcqui($days);
 
-__END__
+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 = $sth->fetchall_arrayref({});
+    return $results;
+}
+
+=head3 GetContracts
+
+  $contractlist = &GetContracts($booksellerid, $activeonly);
+
+Looks up the contracts that belong to a bookseller
+
+Returns a list of contracts
+
+=over
+
+=item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
+
+=item C<$activeonly> if exists get only contracts that are still active.
+
+=back
+
+=cut
+
+sub GetContracts {
+    my ( $booksellerid, $activeonly ) = @_;
+    my $dbh = C4::Context->dbh;
+    my $query;
+    if (! $activeonly) {
+        $query = "
+            SELECT *
+            FROM   aqcontract
+            WHERE  booksellerid=?
+        ";
+    } else {
+        $query = "SELECT *
+            FROM aqcontract
+            WHERE booksellerid=?
+                AND contractenddate >= CURDATE( )";
+    }
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $booksellerid );
+    my @results;
+    while (my $data = $sth->fetchrow_hashref ) {
+        push(@results, $data);
+    }
+    $sth->finish;
+    return @results;
+}
+
+#------------------------------------------------------------#
+
+=head3 GetContract
+
+  $contract = &GetContract($contractID);
+
+Looks up the contract that has PRIMKEY (contractnumber) value $contractID
+
+Returns a contract
+
+=cut
+
+sub GetContract {
+    my ( $contractno ) = @_;
+    my $dbh = C4::Context->dbh;
+    my $query = "
+        SELECT *
+        FROM   aqcontract
+        WHERE  contractnumber=?
+        ";
+
+    my $sth = $dbh->prepare($query);
+    $sth->execute( $contractno );
+    my $result = $sth->fetchrow_hashref;
+    return $result;
+}
+
+=head3 AddClaim
+
+=over 4
+
+&AddClaim($ordernumber);
+
+Add a claim for an order
 
 =back
 
+=cut
+sub AddClaim {
+    my ($ordernumber) = @_;
+    my $dbh          = C4::Context->dbh;
+    my $query        = "
+        UPDATE aqorders SET
+            claims_count = claims_count + 1,
+            claimed_date = CURDATE()
+        WHERE ordernumber = ?
+        ";
+    my $sth = $dbh->prepare($query);
+    $sth->execute($ordernumber);
+
+}
+
+1;
+__END__
+
 =head1 AUTHOR
 
-Koha Developement team <info@koha.org>
+Koha Development Team <http://koha-community.org/>
 
 =cut