View | Details | Raw Unified | Return to bug 11708
Collapse All | Expand All

(-)a/C4/Acquisition.pm (-8 / +28 lines)
Lines 453-459 sub CloseBasketgroup { Link Here
453
    my $dbh        = C4::Context->dbh;
453
    my $dbh        = C4::Context->dbh;
454
    my $sth = $dbh->prepare("
454
    my $sth = $dbh->prepare("
455
        UPDATE aqbasketgroups
455
        UPDATE aqbasketgroups
456
        SET    closed=1
456
        SET closeddate=CAST(NOW() AS DATE)
457
        WHERE  id=?
457
        WHERE  id=?
458
    ");
458
    ");
459
    $sth->execute($basketgroupno);
459
    $sth->execute($basketgroupno);
Lines 474-480 sub ReOpenBasketgroup { Link Here
474
    my $dbh        = C4::Context->dbh;
474
    my $dbh        = C4::Context->dbh;
475
    my $sth = $dbh->prepare("
475
    my $sth = $dbh->prepare("
476
        UPDATE aqbasketgroups
476
        UPDATE aqbasketgroups
477
        SET    closed=0
477
        SET    closeddate=NULL
478
        WHERE  id=?
478
        WHERE  id=?
479
    ");
479
    ");
480
    $sth->execute($basketgroupno);
480
    $sth->execute($basketgroupno);
Lines 909-915 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgr Link Here
909
909
910
$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
910
$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
911
911
912
$hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
912
$hashref->{'closed'} set 'closeddate' if true, or reset 'closeddate' if defined but false.
913
913
914
=cut
914
=cut
915
915
Lines 918-933 sub NewBasketgroup { Link Here
918
    die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
918
    die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
919
    my $query = "INSERT INTO aqbasketgroups (";
919
    my $query = "INSERT INTO aqbasketgroups (";
920
    my @params;
920
    my @params;
921
    foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
921
    foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment)) {
922
        if ( defined $basketgroupinfo->{$field} ) {
922
        if ( defined $basketgroupinfo->{$field} ) {
923
            $query .= "$field, ";
923
            $query .= "$field, ";
924
            push(@params, $basketgroupinfo->{$field});
924
            push(@params, $basketgroupinfo->{$field});
925
        }
925
        }
926
    }
926
    }
927
928
    if ($basketgroupinfo->{closed}) {
929
        $query .= "closeddate, ";
930
    }
927
    $query .= "booksellerid) VALUES (";
931
    $query .= "booksellerid) VALUES (";
928
    foreach (@params) {
932
    foreach (@params) {
929
        $query .= "?, ";
933
        $query .= "?, ";
930
    }
934
    }
935
    if ($basketgroupinfo->{closed}) {
936
        $query .= "CAST(NOW() AS DATE), ";
937
    }
931
    $query .= "?)";
938
    $query .= "?)";
932
    push(@params, $basketgroupinfo->{'booksellerid'});
939
    push(@params, $basketgroupinfo->{'booksellerid'});
933
    my $dbh = C4::Context->dbh;
940
    my $dbh = C4::Context->dbh;
Lines 966-972 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgr Link Here
966
973
967
$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
974
$hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
968
975
969
$hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
976
$hashref->{'closed'} set 'closeddate' if true, or reset 'closeddate' if defined but false.
970
977
971
=cut
978
=cut
972
979
Lines 976-982 sub ModBasketgroup { Link Here
976
    my $dbh = C4::Context->dbh;
983
    my $dbh = C4::Context->dbh;
977
    my $query = "UPDATE aqbasketgroups SET ";
984
    my $query = "UPDATE aqbasketgroups SET ";
978
    my @params;
985
    my @params;
979
    foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
986
987
    if ($basketgroupinfo->{closed}) {
988
        $query .= "closeddate = IF(closeddate IS NULL, CAST(NOW() AS DATE), closeddate), ";
989
    } elsif (defined $basketgroupinfo->{closed}) {
990
        $query .= "closeddate = NULL, ";
991
    }
992
993
    foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment)) {
980
        if ( defined $basketgroupinfo->{$field} ) {
994
        if ( defined $basketgroupinfo->{$field} ) {
981
            $query .= "$field=?, ";
995
            $query .= "$field=?, ";
982
            push(@params, $basketgroupinfo->{$field});
996
            push(@params, $basketgroupinfo->{$field});
Lines 1049-1055 sub GetBasketgroup { Link Here
1049
        { Slice => {} },
1063
        { Slice => {} },
1050
        $basketgroupid
1064
        $basketgroupid
1051
    );
1065
    );
1052
    return $result_set->[0];    # id is unique
1066
    my $basketgroup = $result_set->[0];    # id is unique
1067
    $basketgroup->{closed} = defined($basketgroup->{closeddate}) ? 1 : 0;
1068
    return $basketgroup;
1053
}
1069
}
1054
1070
1055
#------------------------------------------------------------#
1071
#------------------------------------------------------------#
Lines 1069-1075 sub GetBasketgroups { Link Here
1069
    my $dbh = C4::Context->dbh;
1085
    my $dbh = C4::Context->dbh;
1070
    my $sth = $dbh->prepare($query);
1086
    my $sth = $dbh->prepare($query);
1071
    $sth->execute($booksellerid);
1087
    $sth->execute($booksellerid);
1072
    return $sth->fetchall_arrayref({});
1088
    my $basketgroups = $sth->fetchall_arrayref({});
1089
    foreach my $bg (@$basketgroups) {
1090
        $bg->{closed} = defined($bg->{closeddate}) ? 1 : 0;
1091
    }
1092
    return $basketgroups;
1073
}
1093
}
1074
1094
1075
#------------------------------------------------------------#
1095
#------------------------------------------------------------#
(-)a/installer/data/mysql/atomicupdate/bug_11708.perl (+32 lines)
Line 0 Link Here
1
$DBversion = 'XXX';
2
if( CheckVersion( $DBversion ) ) {
3
    unless (column_exists('aqbasketgroups', 'closeddate')) {
4
        $dbh->do(q{
5
            ALTER TABLE aqbasketgroups
6
            ADD COLUMN closeddate DATE DEFAULT NULL AFTER name
7
        });
8
    }
9
10
    if (column_exists('aqbasketgroups', 'closed')) {
11
        # Set basketgroup's close date to the latest close date of its baskets,
12
        # or NOW() if it has no baskets
13
        $dbh->do(q{
14
            UPDATE aqbasketgroups
15
                LEFT JOIN (
16
                    SELECT basketgroupid, MAX(closedate) AS closedate
17
                    FROM aqbasket
18
                    GROUP BY basketgroupid
19
                ) AS aqbasket ON (aqbasketgroups.id = aqbasket.basketgroupid)
20
            SET aqbasketgroups.closeddate = COALESCE(aqbasket.closedate, CAST(NOW() AS DATE))
21
            WHERE closed = 1
22
              AND closeddate IS NULL
23
        });
24
        $dbh->do(q{
25
            ALTER TABLE aqbasketgroups
26
            DROP COLUMN closed
27
        });
28
    }
29
30
    SetVersion( $DBversion );
31
    print "Upgrade to $DBversion done (Bug 11708 - Change aqbasketgroups.closed to closeddate)\n";
32
}
(-)a/installer/data/mysql/kohastructure.sql (-1 / +1 lines)
Lines 2846-2852 DROP TABLE IF EXISTS `aqbasketgroups`; Link Here
2846
CREATE TABLE `aqbasketgroups` (
2846
CREATE TABLE `aqbasketgroups` (
2847
  `id` int(11) NOT NULL auto_increment,
2847
  `id` int(11) NOT NULL auto_increment,
2848
  `name` varchar(50) default NULL,
2848
  `name` varchar(50) default NULL,
2849
  `closed` tinyint(1) default NULL,
2849
  `closeddate` date default NULL,
2850
  `booksellerid` int(11) NOT NULL,
2850
  `booksellerid` int(11) NOT NULL,
2851
  `deliveryplace` varchar(10) default NULL,
2851
  `deliveryplace` varchar(10) default NULL,
2852
  `freedeliveryplace` MEDIUMTEXT default NULL,
2852
  `freedeliveryplace` MEDIUMTEXT default NULL,
(-)a/t/db_dependent/Acquisition/Basketgroups.t (-1 / +67 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use DateTime;
6
7
use Koha::Database;
8
9
use C4::Context;
10
use C4::Acquisition;
11
12
use Test::More tests => 13;
13
14
use t::lib::TestBuilder;
15
16
my $schema = Koha::Database->new->schema;
17
$schema->storage->txn_begin;
18
19
my $builder = t::lib::TestBuilder->new;
20
21
my $bookseller = $builder->build_object({
22
    class => 'Koha::Acquisition::Booksellers',
23
    value => {
24
        name => 'Bookseller test 1',
25
    }
26
});
27
28
my $booksellerid = $bookseller->id;
29
my $basketgroupid = NewBasketgroup({
30
    booksellerid => $booksellerid,
31
    name => "Basketgroup test 1",
32
});
33
34
my $basketgroup = GetBasketgroup($basketgroupid);
35
ok(!$basketgroup->{closed}, "basket group is open");
36
ok(!defined($basketgroup->{closeddate}), "basket group closed date is empty");
37
38
CloseBasketgroup($basketgroupid);
39
$basketgroup = GetBasketgroup($basketgroupid);
40
ok($basketgroup->{closed}, "basket group is closed");
41
ok(defined($basketgroup->{closeddate}), "basket group closed date is not empty");
42
my $today = DateTime->now(time_zone => C4::Context->tz);
43
is($basketgroup->{closeddate}, $today->ymd, "basket group closed date is correct");
44
45
ReOpenBasketgroup($basketgroupid);
46
$basketgroup = GetBasketgroup($basketgroupid);
47
ok(!$basketgroup->{closed}, "basket group is open");
48
ok(!defined($basketgroup->{closeddate}), "basket group closed date is empty");
49
50
$basketgroup->{closed} = 1;
51
ModBasketgroup($basketgroup);
52
$basketgroup = GetBasketgroup($basketgroupid);
53
ok($basketgroup->{closed}, "basket group is closed");
54
ok(defined($basketgroup->{closeddate}), "basket group closed date is not empty");
55
is($basketgroup->{closeddate}, $today->ymd, "basket group closed date is correct");
56
57
$basketgroupid = NewBasketgroup({
58
    booksellerid => $booksellerid,
59
    name => "Basketgroup test 1",
60
    closed => 1,
61
});
62
$basketgroup = GetBasketgroup($basketgroupid);
63
ok($basketgroup->{closed}, "basket group is closed");
64
ok(defined($basketgroup->{closeddate}), "basket group closed date is not empty");
65
is($basketgroup->{closeddate}, $today->ymd, "basket group closed date is correct");
66
67
$schema->storage->txn_rollback();

Return to bug 11708