diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index b1eb094558..3f7f8e73e4 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -28,6 +28,7 @@ module Spree before_validation :adjust_quantity before_validation :copy_price before_validation :copy_tax_category + before_validation :copy_dimensions validates :variant, presence: true validates :quantity, numericality: { @@ -122,6 +123,15 @@ module Spree self.tax_category = variant.product.tax_category end + def copy_dimensions + return unless variant + + self.weight ||= computed_weight_from_variant + self.height ||= variant.height + self.width ||= variant.width + self.depth ||= variant.depth + end + def amount price * quantity end @@ -226,6 +236,14 @@ module Spree private + def computed_weight_from_variant + if variant.product.variant_unit == "weight" + variant.unit_value / variant.product.variant_unit_scale + else + variant.weight + end + end + def update_inventory return unless changed? diff --git a/config/locales/en.yml b/config/locales/en.yml index b618b535b6..fb1dea3834 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2840,6 +2840,9 @@ See the %{link} to find out more about %{sitename}'s features and to start using report_header_delivery_postcode: Delivery Postcode report_header_bulk_unit_size: Bulk Unit Size report_header_weight: Weight + report_header_height: Height + report_header_width: Width + report_header_depth: Depth report_header_sum_total: Sum Total report_header_date_of_order: Date of Order report_header_amount_owing: Amount Owing diff --git a/db/migrate/20220817022401_add_dimensions_to_line_items.rb b/db/migrate/20220817022401_add_dimensions_to_line_items.rb new file mode 100644 index 0000000000..2cbb793775 --- /dev/null +++ b/db/migrate/20220817022401_add_dimensions_to_line_items.rb @@ -0,0 +1,8 @@ +class AddDimensionsToLineItems < ActiveRecord::Migration[6.1] + def change + add_column :spree_line_items, :weight, :decimal, precision: 8, scale: 2 + add_column :spree_line_items, :height, :decimal, precision: 8, scale: 2 + add_column :spree_line_items, :width, :decimal, precision: 8, scale: 2 + add_column :spree_line_items, :depth, :decimal, precision: 8, scale: 2 + end +end diff --git a/db/schema.rb b/db/schema.rb index 5d3cdf0051..87d58b6eae 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -523,6 +523,10 @@ ActiveRecord::Schema.define(version: 2022_10_07_105052) do t.decimal "distribution_fee", precision: 10, scale: 2 t.decimal "final_weight_volume", precision: 10, scale: 2 t.integer "tax_category_id" + t.decimal "weight", precision: 8, scale: 2 + t.decimal "height", precision: 8, scale: 2 + t.decimal "width", precision: 8, scale: 2 + t.decimal "depth", precision: 8, scale: 2 t.index ["order_id"], name: "index_line_items_on_order_id" t.index ["variant_id"], name: "index_line_items_on_variant_id" end diff --git a/lib/reporting/reports/packing/base.rb b/lib/reporting/reports/packing/base.rb index e561f3a81b..65a81c53e2 100644 --- a/lib/reporting/reports/packing/base.rb +++ b/lib/reporting/reports/packing/base.rb @@ -38,7 +38,7 @@ module Reporting private - def select_fields + def select_fields # rubocop:disable Metrics/AbcSize lambda do { hub: distributor_alias[:name], @@ -49,6 +49,10 @@ module Reporting supplier: supplier_alias[:name], product: product_table[:name], variant: variant_full_name, + weight: line_item_table[:weight], + height: line_item_table[:height], + width: line_item_table[:width], + depth: line_item_table[:depth], quantity: line_item_table[:quantity], price: (line_item_table[:quantity] * line_item_table[:price]), temp_controlled: shipping_category_table[:temperature_controlled], diff --git a/lib/reporting/reports/packing/customer.rb b/lib/reporting/reports/packing/customer.rb index 8aa805829d..5e5b7addde 100644 --- a/lib/reporting/reports/packing/customer.rb +++ b/lib/reporting/reports/packing/customer.rb @@ -7,7 +7,8 @@ module Reporting def columns # Reorder default columns super.slice(:hub, :customer_code, :first_name, :last_name, :phone, - :supplier, :product, :variant, :quantity, :price, :temp_controlled) + :supplier, :product, :variant, :weight, :height, :width, :depth, :quantity, + :price, :temp_controlled) end def rules diff --git a/spec/controllers/api/v0/reports/packing_report_spec.rb b/spec/controllers/api/v0/reports/packing_report_spec.rb index 971b3d5832..e3ae5e9d27 100644 --- a/spec/controllers/api/v0/reports/packing_report_spec.rb +++ b/spec/controllers/api/v0/reports/packing_report_spec.rb @@ -66,16 +66,15 @@ describe Api::V0::ReportsController, type: :controller do { "hub" => line_item.order.distributor.name, "customer_code" => line_item.order.customer&.code, - "first_name" => line_item.order.bill_address.firstname, - "last_name" => line_item.order.bill_address.lastname, "supplier" => line_item.product.supplier.name, "product" => line_item.product.name, "variant" => line_item.full_name, "quantity" => line_item.quantity, "price" => (line_item.quantity * line_item.price).to_s, - "phone" => line_item.order.bill_address.phone, "temp_controlled" => line_item.product.shipping_category&.temperature_controlled - } + }. + merge(dimensions(line_item)). + merge(contacts(line_item.order.bill_address)) end def supplier_report_row(line_item) @@ -91,6 +90,23 @@ describe Api::V0::ReportsController, type: :controller do "quantity" => line_item.quantity, "price" => (line_item.quantity * line_item.price).to_s, "temp_controlled" => line_item.product.shipping_category&.temperature_controlled + }.merge(dimensions(line_item)) + end + + def dimensions(line_item) + { + "weight" => line_item.weight.to_s, + "height" => line_item.height.to_s, + "width" => line_item.width.to_s, + "depth" => line_item.depth.to_s + } + end + + def contacts(bill_address) + { + "first_name" => bill_address.firstname, + "last_name" => bill_address.lastname, + "phone" => bill_address.phone, } end diff --git a/spec/system/admin/reports/packing_report_spec.rb b/spec/system/admin/reports/packing_report_spec.rb index b5a1f4d1e5..2fe308b060 100644 --- a/spec/system/admin/reports/packing_report_spec.rb +++ b/spec/system/admin/reports/packing_report_spec.rb @@ -61,7 +61,8 @@ describe "Packing Reports" do table = rows.map { |r| r.all("th").map { |c| c.text.strip } } expect(table).to eq([ ["Hub", "Customer Code", "First Name", "Last Name", "Supplier", - "Product", "Variant", "Quantity", "TempControlled?"].map(&:upcase) + "Product", "Variant", "Weight", "Height", "Width", "Depth", + "Quantity", "TempControlled?"].map(&:upcase) ]) expect(page).to have_selector 'table.report__table tbody tr', count: 5 # Totals row per order end