From e6056bfc7c772e0a138e963361c6b7d1d0865a6b Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Fri, 7 Feb 2014 11:01:30 +0100 Subject: [PATCH] Bug 11708: Change aqbasketgroups.closed to closeddate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: juliette levast Signed-off-by: Paola Rossi Signed-off-by: Séverine QUEUNE --- C4/Acquisition.pm | 36 ++++++++++--- installer/data/mysql/atomicupdate/bug_11708.perl | 25 +++++++++ installer/data/mysql/kohastructure.sql | 2 +- t/db_dependent/Acquisition/Basketgroups.t | 67 ++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_11708.perl create mode 100755 t/db_dependent/Acquisition/Basketgroups.t diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index a257017dc7..dda9503a3f 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -476,7 +476,7 @@ sub CloseBasketgroup { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare(" UPDATE aqbasketgroups - SET closed=1 + SET closeddate=CAST(NOW() AS DATE) WHERE id=? "); $sth->execute($basketgroupno); @@ -497,7 +497,7 @@ sub ReOpenBasketgroup { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare(" UPDATE aqbasketgroups - SET closed=0 + SET closeddate=NULL WHERE id=? "); $sth->execute($basketgroupno); @@ -924,7 +924,7 @@ $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgr $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. +$hashref->{'closed'} set 'closeddate' if true, or reset 'closeddate' if defined but false. =cut @@ -933,16 +933,23 @@ sub NewBasketgroup { die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'}; my $query = "INSERT INTO aqbasketgroups ("; my @params; - foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) { + foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment)) { if ( defined $basketgroupinfo->{$field} ) { $query .= "$field, "; 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; @@ -981,7 +988,7 @@ $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgr $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. +$hashref->{'closed'} set 'closeddate' if true, or reset 'closeddate' if defined but false. =cut @@ -991,7 +998,14 @@ sub ModBasketgroup { my $dbh = C4::Context->dbh; my $query = "UPDATE aqbasketgroups SET "; my @params; - foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) { + + if ($basketgroupinfo->{closed}) { + $query .= "closeddate = IF(closeddate IS NULL, CAST(NOW() AS DATE), closeddate), "; + } elsif (defined $basketgroupinfo->{closed}) { + $query .= "closeddate = NULL, "; + } + + foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment)) { if ( defined $basketgroupinfo->{$field} ) { $query .= "$field=?, "; push(@params, $basketgroupinfo->{$field}); @@ -1064,7 +1078,9 @@ sub GetBasketgroup { { Slice => {} }, $basketgroupid ); - return $result_set->[0]; # id is unique + my $basketgroup = $result_set->[0]; # id is unique + $basketgroup->{closed} = defined($basketgroup->{closeddate}) ? 1 : 0; + return $basketgroup; } #------------------------------------------------------------# @@ -1084,7 +1100,11 @@ sub GetBasketgroups { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare($query); $sth->execute($booksellerid); - return $sth->fetchall_arrayref({}); + my $basketgroups = $sth->fetchall_arrayref({}); + foreach my $bg (@$basketgroups) { + $bg->{closed} = defined($bg->{closeddate}) ? 1 : 0; + } + return $basketgroups; } #------------------------------------------------------------# diff --git a/installer/data/mysql/atomicupdate/bug_11708.perl b/installer/data/mysql/atomicupdate/bug_11708.perl new file mode 100644 index 0000000000..35543cdac5 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_11708.perl @@ -0,0 +1,25 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + unless (column_exists('aqbasketgroups', 'closeddate')) { + $dbh->do(q{ + ALTER TABLE aqbasketgroups + ADD COLUMN closeddate DATE DEFAULT NULL AFTER name + }); + } + + if (column_exists('aqbasketgroups', 'closed')) { + $dbh->do(q{ + UPDATE aqbasketgroups + SET closeddate = CAST(NOW() AS DATE) + WHERE closed = 1 + AND closeddate IS NULL + }); + $dbh->do(q{ + ALTER TABLE aqbasketgroups + DROP COLUMN closed + }); + } + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 11708 - Change aqbasketgroups.closed to closeddate)\n"; +} diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 4b770d0637..23ddd0aa23 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -2814,7 +2814,7 @@ DROP TABLE IF EXISTS `aqbasketgroups`; CREATE TABLE `aqbasketgroups` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) default NULL, - `closed` tinyint(1) default NULL, + `closeddate` date default NULL, `booksellerid` int(11) NOT NULL, `deliveryplace` varchar(10) default NULL, `freedeliveryplace` MEDIUMTEXT default NULL, diff --git a/t/db_dependent/Acquisition/Basketgroups.t b/t/db_dependent/Acquisition/Basketgroups.t new file mode 100755 index 0000000000..856c864794 --- /dev/null +++ b/t/db_dependent/Acquisition/Basketgroups.t @@ -0,0 +1,67 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use DateTime; + +use Koha::Database; + +use C4::Context; +use C4::Acquisition; + +use Test::More tests => 13; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; + +my $bookseller = $builder->build_object({ + class => 'Koha::Acquisition::Booksellers', + value => { + name => 'Bookseller test 1', + } +}); + +my $booksellerid = $bookseller->id; +my $basketgroupid = NewBasketgroup({ + booksellerid => $booksellerid, + name => "Basketgroup test 1", +}); + +my $basketgroup = GetBasketgroup($basketgroupid); +ok(!$basketgroup->{closed}, "basket group is open"); +ok(!defined($basketgroup->{closeddate}), "basket group closed date is empty"); + +CloseBasketgroup($basketgroupid); +$basketgroup = GetBasketgroup($basketgroupid); +ok($basketgroup->{closed}, "basket group is closed"); +ok(defined($basketgroup->{closeddate}), "basket group closed date is not empty"); +my $today = DateTime->now(time_zone => C4::Context->tz); +is($basketgroup->{closeddate}, $today->ymd, "basket group closed date is correct"); + +ReOpenBasketgroup($basketgroupid); +$basketgroup = GetBasketgroup($basketgroupid); +ok(!$basketgroup->{closed}, "basket group is open"); +ok(!defined($basketgroup->{closeddate}), "basket group closed date is empty"); + +$basketgroup->{closed} = 1; +ModBasketgroup($basketgroup); +$basketgroup = GetBasketgroup($basketgroupid); +ok($basketgroup->{closed}, "basket group is closed"); +ok(defined($basketgroup->{closeddate}), "basket group closed date is not empty"); +is($basketgroup->{closeddate}, $today->ymd, "basket group closed date is correct"); + +$basketgroupid = NewBasketgroup({ + booksellerid => $booksellerid, + name => "Basketgroup test 1", + closed => 1, +}); +$basketgroup = GetBasketgroup($basketgroupid); +ok($basketgroup->{closed}, "basket group is closed"); +ok(defined($basketgroup->{closeddate}), "basket group closed date is not empty"); +is($basketgroup->{closeddate}, $today->ymd, "basket group closed date is correct"); + +$schema->storage->txn_rollback(); -- 2.14.2