Add adjustment_total to spree_shipments

This commit is contained in:
Matt-Yorkley
2021-02-07 15:16:57 +00:00
parent 7e0aad7c7e
commit 232286b0be
2 changed files with 32 additions and 0 deletions

View File

@@ -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

View File

@@ -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