From f38661cdf1b6dd06f697dd8a47a3111d2a837318 Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Wed, 17 Aug 2022 08:02:14 +0530 Subject: [PATCH 1/6] Add weight, height, width, depth fields to line items --- db/migrate/20220817022401_add_dimensions_to_line_items.rb | 8 ++++++++ db/schema.rb | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 db/migrate/20220817022401_add_dimensions_to_line_items.rb 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 a6e1aa230a..ecdff3a9e9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -513,6 +513,10 @@ ActiveRecord::Schema.define(version: 2022_10_04_165343) 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 From f8657aaf41da5ea7c5d97d8061be580cc0529706 Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Wed, 17 Aug 2022 08:13:38 +0530 Subject: [PATCH 2/6] Copy dimensions from variants to line items before validation --- app/models/spree/line_item.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index b1eb094558..d597e5ae51 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 = variant.weight if weight.nil? + self.height = variant.height if height.nil? + self.width = variant.width if width.nil? + self.depth = variant.depth if depth.nil? + end + def amount price * quantity end From 5c0f3a2d4b55614ed816bab97d41148853c95a3b Mon Sep 17 00:00:00 2001 From: Nihal Mohammed Date: Wed, 17 Aug 2022 08:14:19 +0530 Subject: [PATCH 3/6] Add weight, height, width, depth fields to pack by customer reports + update specs by adding dimensions fields into reports specs --- config/locales/en.yml | 3 +++ lib/reporting/reports/packing/base.rb | 4 ++++ lib/reporting/reports/packing/customer.rb | 3 ++- .../controllers/api/v0/reports/packing_report_spec.rb | 11 ++++++++++- spec/system/admin/reports/packing_report_spec.rb | 3 ++- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/config/locales/en.yml b/config/locales/en.yml index 80079c11e7..a64d684b95 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2827,6 +2827,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/lib/reporting/reports/packing/base.rb b/lib/reporting/reports/packing/base.rb index 585029dd38..afe1a264b9 100644 --- a/lib/reporting/reports/packing/base.rb +++ b/lib/reporting/reports/packing/base.rb @@ -56,6 +56,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..e5462f3ce2 100644 --- a/spec/controllers/api/v0/reports/packing_report_spec.rb +++ b/spec/controllers/api/v0/reports/packing_report_spec.rb @@ -75,7 +75,7 @@ describe Api::V0::ReportsController, type: :controller do "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)) end def supplier_report_row(line_item) @@ -91,6 +91,15 @@ 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 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 From 7081e712e861fe4b9878862788ef80b4cc55de30 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 23 Aug 2022 13:52:58 +1000 Subject: [PATCH 4/6] Flag style issue on todo list for later The code needs some bigger refactoring to avoid this style issue. --- app/models/spree/line_item.rb | 8 ++++---- lib/reporting/reports/packing/base.rb | 2 +- spec/controllers/api/v0/reports/packing_report_spec.rb | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index d597e5ae51..512f381f36 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -126,10 +126,10 @@ module Spree def copy_dimensions return unless variant - self.weight = variant.weight if weight.nil? - self.height = variant.height if height.nil? - self.width = variant.width if width.nil? - self.depth = variant.depth if depth.nil? + self.weight ||= variant.weight + self.height ||= variant.height + self.width ||= variant.width + self.depth ||= variant.depth end def amount diff --git a/lib/reporting/reports/packing/base.rb b/lib/reporting/reports/packing/base.rb index afe1a264b9..ccac3ee921 100644 --- a/lib/reporting/reports/packing/base.rb +++ b/lib/reporting/reports/packing/base.rb @@ -45,7 +45,7 @@ module Reporting private - def select_fields + def select_fields # rubocop:disable Metrics/AbcSize lambda do { hub: distributor_alias[:name], diff --git a/spec/controllers/api/v0/reports/packing_report_spec.rb b/spec/controllers/api/v0/reports/packing_report_spec.rb index e5462f3ce2..4af9f32807 100644 --- a/spec/controllers/api/v0/reports/packing_report_spec.rb +++ b/spec/controllers/api/v0/reports/packing_report_spec.rb @@ -62,7 +62,7 @@ describe Api::V0::ReportsController, type: :controller do results end - def distributor_report_row(line_item) + def distributor_report_row(line_item) # rubocop:disable Metrics/AbcSize { "hub" => line_item.order.distributor.name, "customer_code" => line_item.order.customer&.code, From 23b5e73c97c0372c8071dcc20f28d12084d3f49e Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 24 Oct 2022 16:47:27 +0200 Subject: [PATCH 5/6] Weight is computed with `unit_value` and product `variant_unit_scale` --- app/models/spree/line_item.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/spree/line_item.rb b/app/models/spree/line_item.rb index 512f381f36..3f7f8e73e4 100644 --- a/app/models/spree/line_item.rb +++ b/app/models/spree/line_item.rb @@ -126,7 +126,7 @@ module Spree def copy_dimensions return unless variant - self.weight ||= variant.weight + self.weight ||= computed_weight_from_variant self.height ||= variant.height self.width ||= variant.width self.depth ||= variant.depth @@ -236,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? From ede38a417fcea01c1e543e220e8e9d45348c34db Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Tue, 25 Oct 2022 10:16:59 +0200 Subject: [PATCH 6/6] Respect rubocop Metrics/AbcSize by extracting a method --- .../api/v0/reports/packing_report_spec.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/spec/controllers/api/v0/reports/packing_report_spec.rb b/spec/controllers/api/v0/reports/packing_report_spec.rb index 4af9f32807..e3ae5e9d27 100644 --- a/spec/controllers/api/v0/reports/packing_report_spec.rb +++ b/spec/controllers/api/v0/reports/packing_report_spec.rb @@ -62,20 +62,19 @@ describe Api::V0::ReportsController, type: :controller do results end - def distributor_report_row(line_item) # rubocop:disable Metrics/AbcSize + def distributor_report_row(line_item) { "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(dimensions(line_item)). + merge(contacts(line_item.order.bill_address)) end def supplier_report_row(line_item) @@ -103,6 +102,14 @@ describe Api::V0::ReportsController, type: :controller do } end + def contacts(bill_address) + { + "first_name" => bill_address.firstname, + "last_name" => bill_address.lastname, + "phone" => bill_address.phone, + } + end + def i18n_scope "admin.reports" end