Bug 13323: Tax rate can change on receiving - DB changes
authorJonathan Druart <jonathan.druart@biblibre.com>
Wed, 19 Nov 2014 15:59:39 +0000 (16:59 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 28 Oct 2016 13:46:40 +0000 (13:46 +0000)
This patch adds 4 new DB fields to the aqorders table:
 * tax_rate_on_ordering
 * tax_rate_on_receiving
 * tax_value_on_ordering
 * tax_value_on_receiving

Signed-off-by: Laurence Rault <laurence.rault@biblibre.com>
Signed-off-by: Francois Charbonnier <francois.charbonnier@inlibro.com>
Signed-off-by: Sonia Bouis <sonia.bouis@univ-lyon3.fr>
Signed-off-by: Sonia Bouis <koha@univ-lyon3.fr>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
installer/data/mysql/atomicupdate/bug_13323.perl [new file with mode: 0644]
installer/data/mysql/kohastructure.sql

diff --git a/installer/data/mysql/atomicupdate/bug_13323.perl b/installer/data/mysql/atomicupdate/bug_13323.perl
new file mode 100644 (file)
index 0000000..e74c2f6
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+
+use Modern::Perl;
+use C4::Context;
+
+my $dbh = C4::Context->dbh;
+
+# Add the new columns
+$dbh->do(q|
+    ALTER TABLE aqorders
+        ADD COLUMN tax_rate_on_ordering   decimal(6,4) default NULL AFTER tax_rate,
+        ADD COLUMN tax_rate_on_receiving  decimal(6,4) default NULL AFTER tax_rate_on_ordering,
+        ADD COLUMN tax_value_on_ordering  decimal(28,6) default NULL AFTER tax_value,
+        ADD COLUMN tax_value_on_receiving decimal(28,6) default NULL AFTER tax_value_on_ordering
+|);
+
+my $orders = $dbh->selectall_arrayref(q|
+    SELECT * FROM aqorders
+|, { Slice => {} } );
+
+my $sth_update_order = $dbh->prepare(q|
+    UPDATE aqorders
+    SET tax_rate_on_ordering = tax_rate,
+        tax_rate_on_receiving = tax_rate,
+        tax_value_on_ordering = ?,
+        tax_value_on_receiving = ?
+    WHERE ordernumber = ?
+|);
+
+require Koha::Number::Price;
+for my $order (@$orders) {
+    my $tax_value_on_ordering =
+      $order->{quantity} *
+      $order->{ecost_tax_excluded} *
+      $order->{tax_rate};
+
+    my $tax_value_on_receiving =
+      ( defined $order->{unitprice_tax_excluded} )
+      ? $order->{quantity} * $order->{unitprice_tax_excluded} * $order->{tax_rate}
+      : undef;
+
+    $sth_update_order->execute( $tax_value_on_ordering,
+        $tax_value_on_receiving, $order->{ordernumber} );
+}
+
+# Remove the old columns
+$dbh->do(q|
+    ALTER TABLE aqorders
+        CHANGE COLUMN tax_value tax_value_bak  decimal(28,6) default NULL,
+        CHANGE COLUMN tax_rate tax_rate_bak decimal(6,4) default NULL
+|);
index d79a9fb..3201f23 100644 (file)
@@ -3143,8 +3143,12 @@ CREATE TABLE `aqorders` ( -- information related to the basket line items
   `ecost` decimal(13,2) DEFAULT NULL, -- the replacement cost for this line item
   `ecost_tax_excluded` decimal(28,6) default NULL, -- the estimated cost excluding tax
   `ecost_tax_included` decimal(28,6) default NULL, -- the estimated cost including tax
-  `tax_rate` decimal(6,4) DEFAULT NULL, -- the tax rate for this line item (%)
-  `tax_value` decimal(28,6) default NULL, -- the tax value for this line item
+  `tax_rate_bak` decimal(6,4) DEFAULT NULL, -- the tax rate for this line item (%)
+  `tax_rate_on_ordering` decimal(6,4) DEFAULT NULL, -- the tax rate on ordering for this line item (%)
+  `tax_rate_on_receiving` decimal(6,4) DEFAULT NULL, -- the tax rate on receiving for this line item (%)
+  `tax_value_bak` decimal(28,6) default NULL, -- the tax value for this line item
+  `tax_value_on_ordering` decimal(28,6) DEFAULT NULL, -- the tax value on ordering for this line item
+  `tax_value_on_receiving` decimal(28,6) DEFAULT NULL, -- the tax value on receiving for this line item
   `discount` float(6,4) default NULL, -- the discount for this line item (%)
   `budget_id` int(11) NOT NULL, -- the fund this order goes against (aqbudgets.budget_id)
   `budgetdate` date default NULL, -- not used? always NULL