From 9c771f3e4ea2022654ecab67c0ffb0a7ac7660e0 Mon Sep 17 00:00:00 2001
From: Julian Maurice <julian.maurice@biblibre.com>
Date: Fri, 7 Feb 2014 11:01:30 +0100
Subject: [PATCH] Bug 11708: Add aqbasketgroups.closeddate
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: juliette levast <juliette.levast@iepg.fr>
Signed-off-by: Paola Rossi <paola.rossi@cineca.it>

Patch updated: Use atomic update

Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>
---
 C4/Acquisition.pm                                     | 19 +++++++++++++++++--
 ...Bug-11708-add-column-aqbasketgroups-closeddate.sql |  1 +
 installer/data/mysql/kohastructure.sql                |  1 +
 3 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 installer/data/mysql/atomicupdate/Bug-11708-add-column-aqbasketgroups-closeddate.sql

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 202ed7e..613eab4 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -476,7 +476,8 @@ sub CloseBasketgroup {
     my $dbh        = C4::Context->dbh;
     my $sth = $dbh->prepare("
         UPDATE aqbasketgroups
-        SET    closed=1
+        SET    closed=1,
+               closeddate=CAST(NOW() AS DATE)
         WHERE  id=?
     ");
     $sth->execute($basketgroupno);
@@ -497,7 +498,7 @@ sub ReOpenBasketgroup {
     my $dbh        = C4::Context->dbh;
     my $sth = $dbh->prepare("
         UPDATE aqbasketgroups
-        SET    closed=0
+        SET    closed=0, closeddate=NULL
         WHERE  id=?
     ");
     $sth->execute($basketgroupno);
@@ -939,10 +940,17 @@ sub NewBasketgroup {
             push(@params, $basketgroupinfo->{$field});
         }
     }
+
+    if ($basketgroupinfo->{closed}) {
+        $query .= "closeddate, ";
+    }
     $query .= "booksellerid) VALUES (";
     foreach (@params) {
         $query .= "?, ";
     }
+    if ($basketgroupinfo->{closed}) {
+        $query .= "CAST(NOW() AS DATE), ";
+    }
     $query .= "?)";
     push(@params, $basketgroupinfo->{'booksellerid'});
     my $dbh = C4::Context->dbh;
@@ -991,6 +999,13 @@ sub ModBasketgroup {
     my $dbh = C4::Context->dbh;
     my $query = "UPDATE aqbasketgroups SET ";
     my @params;
+
+    if ($basketgroupinfo->{closed}) {
+        $query .= "closeddate = IF(closed = 0, CAST(NOW() AS DATE), closeddate), ";
+    } elsif (defined $basketgroupinfo->{closed}) {
+        $query .= "closeddate = NULL, ";
+    }
+
     foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
         if ( defined $basketgroupinfo->{$field} ) {
             $query .= "$field=?, ";
diff --git a/installer/data/mysql/atomicupdate/Bug-11708-add-column-aqbasketgroups-closeddate.sql b/installer/data/mysql/atomicupdate/Bug-11708-add-column-aqbasketgroups-closeddate.sql
new file mode 100644
index 0000000..ef6cbac
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/Bug-11708-add-column-aqbasketgroups-closeddate.sql
@@ -0,0 +1 @@
+ALTER TABLE aqbasketgroups ADD COLUMN closeddate DATE DEFAULT NULL AFTER closed;
\ No newline at end of file
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 8151a29..92f9c93 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2810,6 +2810,7 @@ CREATE TABLE `aqbasketgroups` (
   `id` int(11) NOT NULL auto_increment,
   `name` varchar(50) default NULL,
   `closed` tinyint(1) default NULL,
+  closeddate date default NULL, -- the date the basketgroup was closed
   `booksellerid` int(11) NOT NULL,
   `deliveryplace` varchar(10) default NULL,
   `freedeliveryplace` MEDIUMTEXT default NULL,
-- 
2.1.4