Fix bug in ModReceiveOrder call
[koha_gimpoz] / C4 / Acquisition.pm
index 201f159..8270627 100644 (file)
@@ -17,12 +17,11 @@ 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;
 use C4::Context;
-use C4::Date;
+use C4::Dates qw(format_date);
 use MARC::Record;
 use C4::Suggestions;
 use Time::localtime;
@@ -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.01;
 
 # used in receiveorder subroutine
 # to provide library specific handling
@@ -95,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 );
 }
 
 #------------------------------------------------------------#
@@ -270,20 +270,20 @@ sub GetOrders {
     my $dbh   = C4::Context->dbh;
     my $query  ="
          SELECT  aqorderbreakdown.*,
-                biblio.*,biblioitems.*,
+                biblio.*,biblioitems.publishercode,
                 aqorders.*,
                 aqbookfund.bookfundname,
                 biblio.title
         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
+            LEFT JOIN biblio           ON biblio.biblionumber=aqorders.biblionumber
+            LEFT JOIN biblioitems      ON biblioitems.biblionumber=biblio.biblionumber
         WHERE   basketno=?
             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);
@@ -374,7 +374,7 @@ sub GetOrder {
   &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
     $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
     $ecost, $gst, $budget, $unitprice, $subscription,
-    $booksellerinvoicenumber);
+    $booksellerinvoicenumber, $purchaseorder);
 
 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
@@ -400,7 +400,7 @@ sub NewOrder {
         $listprice, $booksellerid, $authorisedby, $notes,
         $bookfund,  $bibitemnum,   $rrp,          $ecost,
         $gst,       $budget,       $cost,         $sub,
-        $invoice,   $sort1,        $sort2
+        $invoice,   $sort1,        $sort2,        $purchaseorder
       )
       = @_;
 
@@ -438,15 +438,15 @@ sub NewOrder {
     my $query = "
         INSERT INTO aqorders
            ( biblionumber,title,basketno,quantity,listprice,notes,
-           biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now() )
+           biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate,purchaseordernumber)
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now(),? )
     ";
     my $sth = $dbh->prepare($query);
 
     $sth->execute(
         $bibnum, $title,      $basketno, $quantity, $listprice,
         $notes,  $bibitemnum, $rrp,      $ecost,    $gst,
-        $cost,   $sub,        $sort1,    $sort2
+        $cost,   $sub,        $sort1,    $sort2,       $purchaseorder
     );
     $sth->finish;
 
@@ -490,7 +490,7 @@ sub ModOrder {
         $title,      $ordnum,   $quantity, $listprice, $bibnum,
         $basketno,   $supplier, $who,      $notes,     $bookfund,
         $bibitemnum, $rrp,      $ecost,    $gst,       $budget,
-        $cost,       $invoice,  $sort1,    $sort2
+        $cost,       $invoice,  $sort1,    $sort2,     $purchaseorder
       )
       = @_;
     my $dbh = C4::Context->dbh;
@@ -499,14 +499,15 @@ sub ModOrder {
         SET    title=?,
                quantity=?,listprice=?,basketno=?,
                rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
-               notes=?,sort1=?, sort2=?
+               notes=?,sort1=?, sort2=?, purchaseordernumber=?
         WHERE  ordernumber=? AND biblionumber=?
     ";
     my $sth = $dbh->prepare($query);
     $sth->execute(
         $title, $quantity, $listprice, $basketno, $rrp,
         $ecost, $cost,     $invoice,   $notes,    $sort1,
-        $sort2, $ordnum,   $bibnum
+        $sort2, $purchaseorder,
+               $ordnum,   $bibnum
     );
     $sth->finish;
     $query = "
@@ -570,6 +571,9 @@ 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>.
 
@@ -583,31 +587,21 @@ Also updates the book fund ID in the aqorderbreakdown table.
 sub ModReceiveOrder {
     my (
         $biblionumber,    $ordnum,  $quantrec, $user, $cost,
-        $invoiceno, $freight, $rrp, $bookfund, $daterecieved
+        $invoiceno, $freight, $rrp, $bookfund, $datereceived
       )
       = @_;
     my $dbh = C4::Context->dbh;
 #     warn "DATE BEFORE : $daterecieved";
-    $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
+#    $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
 #     warn "DATE REC : $daterecieved";
-    my $query = "
-        UPDATE aqorders
-        SET    quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
-               unitprice=?,freight=?,rrp=?
-        WHERE biblionumber=? AND ordernumber=?
-    ";
-    my $sth = $dbh->prepare($query);
+       $datereceived = C4::Dates->output('iso') unless $datereceived;
     my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
     if ($suggestionid) {
         ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
     }
-    $sth->execute( $quantrec,$daterecieved, $invoiceno, $cost, $freight, $rrp, $biblionumber,
-        $ordnum);
-    $sth->finish;
-
     # Allows libraries to change their bookfund during receiving orders
     # allows them to adjust budgets
-    if ( C4::Context->preferene("LooseBudgets") ) {
+    if ( C4::Context->preference("LooseBudgets") && $bookfund ) {
         my $query = "
             UPDATE aqorderbreakdown
             SET    bookfundid=?
@@ -617,9 +611,38 @@ sub ModReceiveOrder {
         $sth->execute( $bookfund, $ordnum );
         $sth->finish;
     }
-    return $daterecieved;
+   
+       my $sth=$dbh->prepare("SELECT * FROM aqorders  LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
+                                                       WHERE biblionumber=? AND aqorders.ordernumber=?");
+    $sth->execute($biblionumber,$ordnum);
+    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,$ordnum);
+        $sth->finish;
+        # create a new order for the remaining items, and set its bookfund.
+        my $newOrder = NewOrder($order->{'basketno'},$order->{'biblionumber'},$order->{'title'}, $order->{'quantity'} - $quantrec,    
+                    $order->{'listprice'},$order->{'booksellerid'},$order->{'authorisedby'},$order->{'notes'},   
+                    $order->{'bookfundid'},$order->{'biblioitemnumber'},$order->{'rrp'},$order->{'ecost'},$order->{'gst'},
+                    $order->{'budget'},$order->{'unitcost'},$order->{'sub'},'',$order->{'sort1'},$order->{'sort2'},$order->{'purchaseordernumber'});
+    
+        $sth=$dbh->prepare(" insert into aqorderbreakdown (ordernumber, branchcode, bookfundid) values (?,?,?)"); 
+        $sth->execute($newOrder,$order->{branch},$order->{bookfundid});
+    } 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,$ordnum);
+        $sth->finish;
+    }
+    return $datereceived;
 }
-
 #------------------------------------------------------------#
 
 =head3 SearchOrder
@@ -819,10 +842,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\'";
 
@@ -830,13 +854,14 @@ 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 ) {
@@ -891,7 +916,7 @@ sub GetParcels {
     my $dbh    = C4::Context->dbh;
     my $strsth ="
         SELECT  aqorders.booksellerinvoicenumber,
-                datereceived,
+                datereceived,purchaseordernumber,
                 count(DISTINCT biblionumber) AS biblio,
                 sum(quantity) AS itemsexpected,
                 sum(quantityreceived) AS itemsreceived
@@ -1070,10 +1095,11 @@ sub GetHistory {
                 aqorders.ecost,
                 aqorders.ordernumber,
                 aqorders.booksellerinvoicenumber as invoicenumber,
-                aqbooksellers.id as id
+                aqbooksellers.id as id,
+                aqorders.biblionumber
             FROM aqorders 
             LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno 
-            LEFT JOIN aqbooksellers 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"
@@ -1094,6 +1120,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;