From 232286b0be78baa8d6afd77a4d040de7d8b5143c Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Sun, 7 Feb 2021 15:16:57 +0000 Subject: [PATCH] Add adjustment_total to spree_shipments --- ...151520_add_adjustment_total_to_shipment.rb | 31 +++++++++++++++++++ db/schema.rb | 1 + 2 files changed, 32 insertions(+) create mode 100644 db/migrate/20210207151520_add_adjustment_total_to_shipment.rb diff --git a/db/migrate/20210207151520_add_adjustment_total_to_shipment.rb b/db/migrate/20210207151520_add_adjustment_total_to_shipment.rb new file mode 100644 index 0000000000..aeee8eac43 --- /dev/null +++ b/db/migrate/20210207151520_add_adjustment_total_to_shipment.rb @@ -0,0 +1,31 @@ +class AddAdjustmentTotalToShipment < ActiveRecord::Migration + def up + add_column :spree_shipments, :adjustment_total, :decimal, + precision: 10, scale: 2, null: false, default: 0.0 + + populate_adjustment_totals + end + + def down + remove_column :spree_shipments, :adjustment_total + end + + def populate_adjustment_totals + # Populates the new `adjustment_total` field in the spree_shipments table. Sets the value + # to the shipment's (shipping fee) adjustment amount. + + adjustment_totals_sql = <<-SQL + UPDATE spree_shipments + SET adjustment_total = shipping_adjustment.fee_amount + FROM ( + SELECT spree_adjustments.source_id AS shipment_id, spree_adjustments.amount AS fee_amount + FROM spree_adjustments + WHERE spree_adjustments.source_type = 'Spree::Shipment' + AND spree_adjustments.amount <> 0 + ) shipping_adjustment + WHERE spree_shipments.id = shipping_adjustment.shipment_id + SQL + + ActiveRecord::Base.connection.execute(adjustment_totals_sql) + end +end diff --git a/db/schema.rb b/db/schema.rb index b50d299209..1ed302c3cf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -780,6 +780,7 @@ ActiveRecord::Schema.define(version: 20210320003951) do t.integer "stock_location_id" t.decimal "included_tax_total", precision: 10, scale: 2, default: "0.0", null: false t.decimal "additional_tax_total", precision: 10, scale: 2, default: "0.0", null: false + t.decimal "adjustment_total", precision: 10, scale: 2, default: "0.0", null: false t.index ["number"], name: "index_shipments_on_number", using: :btree t.index ["order_id"], name: "index_spree_shipments_on_order_id", unique: true, using: :btree end