From b8ec249b941229f2c4f24cfda824e570bd01b6dc 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

---
 C4/Acquisition.pm                      |   19 +++++++++++++++++--
 installer/data/mysql/kohastructure.sql |    1 +
 installer/data/mysql/updatedatabase.pl |   10 ++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm
index 2e013ac..a25bf82 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -424,7 +424,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);
@@ -445,7 +446,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);
@@ -877,10 +878,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;
@@ -929,6 +937,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/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index b46dc6a..fdc78c7 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2791,6 +2791,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` text default NULL,
diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl
index c68ba51..7603662 100755
--- a/installer/data/mysql/updatedatabase.pl
+++ b/installer/data/mysql/updatedatabase.pl
@@ -8560,6 +8560,16 @@ if ( CheckVersion($DBversion) ) {
     SetVersion($DBversion);
 }
 
+$DBversion = "XXX";
+if (CheckVersion($DBversion)) {
+    $dbh->do(q|
+        ALTER TABLE aqbasketgroups
+        ADD COLUMN closeddate DATE DEFAULT NULL AFTER closed
+    |);
+    print "Upgrade to $DBversion done (Bug XXXXX: Add aqbasketgroups.closeddate)\n";
+    SetVersion($DBversion);
+}
+
 =head1 FUNCTIONS
 
 =head2 TableExists($table)
-- 
1.7.10.4