Bug Fixing : C4::Charset source_encoding always set to iso-8859-1
[koha-ffzg.git] / C4 / Acquisition.pm
index 59cc2a5..eeb25e5 100644 (file)
@@ -25,6 +25,7 @@ use C4::Dates qw(format_date format_date_in_iso);
 use MARC::Record;
 use C4::Suggestions;
 use C4::Debug;
+use C4::SQLHelper qw(InsertInTable);
 
 use Time::localtime;
 use HTML::Entities;
@@ -37,7 +38,7 @@ BEGIN {
        require Exporter;
        @ISA    = qw(Exporter);
        @EXPORT = qw(
-               &GetBasket &NewBasket &CloseBasket &DelBasket &ModBasket
+               &GetBasket &NewBasket &CloseBasket &CloseBasketgroup &ReOpenBasketgroup &DelBasket &ModBasket
                &ModBasketHeader &GetBasketsByBookseller &GetBasketsByBasketgroup
                &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup
                &GetBasketgroups
@@ -53,6 +54,7 @@ BEGIN {
                &GetContracts &GetContract
 
         &GetOrderFromItemnumber
+        &GetItemnumbersFromOrder
        );
 }
 
@@ -80,6 +82,22 @@ sub GetOrderFromItemnumber {
 
 }
 
+# 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;
+
+}
 
 
 
@@ -201,6 +219,57 @@ sub CloseBasket {
 
 #------------------------------------------------------------#
 
+=head3 CloseBasketgroup
+
+=over 4
+
+&CloseBasketgroup($basketgroupno);
+
+close a basketgroup
+
+=back
+
+=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)
+
+=over 4
+
+&ReOpenBaskergroup($basketgroupno);
+
+reopen a basketgroup
+
+=back
+
+=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
 
 =over 4
@@ -492,12 +561,15 @@ sub ModBasketgroup {
     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'}}) {
-            my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
-            my $sth2 = $dbh->prepare($query2);
-            $sth2->execute($basketgroupinfo->{'id'}, $basketno);
-            $sth2->finish;
+            $sth->execute($basketgroupinfo->{'id'}, $basketno);
+            $sth->finish;
         }
     }
     $sth->finish;
@@ -839,6 +911,8 @@ table of the Koha database.
 
 =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
@@ -864,29 +938,9 @@ sub NewOrder {
     } else {
         $orderinfo->{'subscription'} = 0;
     }
+    $orderinfo->{'entrydate'} ||= C4::Dates->new()->output("iso");
 
-    my $query = "INSERT INTO aqorders (";
-    foreach my $orderinfokey (keys %{$orderinfo}) {
-        next if $orderinfokey =~ m/branchcode|entrydate/;   # skip branchcode and entrydate, branchcode isnt a vaild col, entrydate we add manually with NOW()
-        $query .= "$orderinfokey,";
-        push(@params, $orderinfo->{$orderinfokey});
-    }
-
-    $query .= "entrydate) VALUES (";
-    foreach (@params) {
-        $query .= "?,";
-    }
-    $query .= " NOW() )";  #ADDING CURRENT DATE TO  'budgetdate, entrydate, purchaseordernumber'...
-
-    my $sth = $dbh->prepare($query);
-
-    $sth->execute(@params);
-    $sth->finish;
-
-    #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
-    my $ordnum = $dbh->{'mysql_insertid'};
-
-    $sth->finish;
+       my $ordnum=InsertInTable("aqorders",$orderinfo);
     return ( $orderinfo->{'basketno'}, $ordnum );
 }
 
@@ -1153,39 +1207,40 @@ C<@results> is an array of references-to-hash with the following keys:
 
 sub SearchOrder {
 #### -------- SearchOrder-------------------------------
-    my ($ordernumber, $search) = @_;
+    my ($ordernumber, $search, $supplierid, $basket) = @_;
 
-    if ($ordernumber) {
-        my $dbh = C4::Context->dbh;
-        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)
-                AND (aqorders.ordernumber=?))";
-        my $sth = $dbh->prepare($query);
-        $sth->execute($ordernumber);
-        my $results = $sth->fetchall_arrayref({});
-        $sth->finish;
-        return $results;
-    } else {
-        my $dbh = C4::Context->dbh;
-        my $query =
+    my $dbh = C4::Context->dbh;
+    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)
-                AND (biblio.title like ? OR biblioitems.isbn like ?))";
-        my $sth = $dbh->prepare($query);
-        $sth->execute("%$search%","%$search%");
-        my $results = $sth->fetchall_arrayref({});
-        $sth->finish;
-        return $results;
+                WHERE  (datecancellationprinted is NULL)";
+                
+    if($ordernumber){
+        $query .= " AND (aqorders.ordernumber=?)";
+        push @args, $ordernumber;
     }
+    if($search){
+        $query .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
+        push @args, ("%$search%","%$search%","%$search%");
+    }
+    if($supplierid){
+        $query .= "AND aqbasket.booksellerid = ?";
+        push @args, $supplierid;
+    }
+    if($basket){
+        $query .= "AND aqorders.basketno = ?";
+        push @args, $basket;
+    }
+
+    my $sth = $dbh->prepare($query);
+    $sth->execute(@args);
+    my $results = $sth->fetchall_arrayref({});
+    $sth->finish;
+    return $results;
 }
 
 #------------------------------------------------------------#