From b7285e48b3aaa42bfa1c7f420ff7ba4542db74f2 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sat, 12 Oct 2024 03:50:11 +0500 Subject: [PATCH 1/5] 12878: update unit to full_name to display in variant column --- lib/reporting/reports/orders_and_distributors/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/reporting/reports/orders_and_distributors/base.rb b/lib/reporting/reports/orders_and_distributors/base.rb index 47a389de41a..73e49a2763a 100644 --- a/lib/reporting/reports/orders_and_distributors/base.rb +++ b/lib/reporting/reports/orders_and_distributors/base.rb @@ -14,8 +14,8 @@ def columns customer_phone: proc { |line_item| line_item.order.bill_address.phone }, customer_city: proc { |line_item| line_item.order.bill_address.city }, sku: proc { |line_item| line_item.product.sku }, - item_name: proc { |line_item| line_item.product.name }, - variant: proc { |line_item| line_item.unit_to_display }, + product: proc { |line_item| line_item.product.name }, + variant: proc { |line_item| line_item.full_name }, quantity: proc { |line_item| line_item.quantity }, max_quantity: proc { |line_item| line_item.max_quantity }, cost: proc { |line_item| line_item.price * line_item.quantity }, From 60e8db9adcc3a28afc9d7c3d1ae5983038e71574 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sat, 12 Oct 2024 03:52:00 +0500 Subject: [PATCH 2/5] 12878: add migration to show new column product column --- ...pdate_item_name_to_product_in_od_report.rb | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb diff --git a/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb b/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb new file mode 100644 index 00000000000..9cd5b6dbf58 --- /dev/null +++ b/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb @@ -0,0 +1,39 @@ +class UpdateItemNameToProductInODReport < ActiveRecord::Migration[7.0] + # OD: Orders and Distributors + def up + # adding subtype filter just to be safe + options = ReportRenderingOptions.where(report_type: 'orders_and_distributors', report_subtype: nil) + + options.find_each do |option| + begin + fields_to_show = option.options[:fields_to_show] + next if fields_to_show&.exclude?('item_name') + + fields_to_show.delete('item_name') + fields_to_show << 'product' + option.update(options: option.options) + rescue StandardError => e + puts "Failed to update rendering option with id: #{option.id}" + puts "Error: #{e.message}" + end + end + end + + def down + options = ReportRenderingOptions.where(report_type: 'orders_and_distributors', report_subtype: nil) + + options.find_each do |option| + begin + fields_to_show = option.options[:fields_to_show] + next if fields_to_show&.exclude?('product') + + fields_to_show.delete('product') + fields_to_show << 'item_name' + option.update(options: option.options) + rescue StandardError => e + puts "Failed to revert rendering option with id: #{option.id}" + puts "Error: #{e.message}" + end + end + end +end From 641b7beee3e6063596ee205e6a5148f69870063a Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Sat, 12 Oct 2024 04:47:45 +0500 Subject: [PATCH 3/5] add specs --- .../orders_and_distributors_report_spec.rb | 71 +++++++++++-------- .../reports/orders_and_distributors_spec.rb | 2 +- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/spec/lib/reports/orders_and_distributors_report_spec.rb b/spec/lib/reports/orders_and_distributors_report_spec.rb index 3edccd5ff6c..ba851aaf73a 100644 --- a/spec/lib/reports/orders_and_distributors_report_spec.rb +++ b/spec/lib/reports/orders_and_distributors_report_spec.rb @@ -11,7 +11,7 @@ [ 'Order date', 'Order Id', 'Customer Name', 'Customer Email', 'Customer Phone', 'Customer City', - 'SKU', 'Item name', 'Variant', 'Quantity', 'Max Quantity', 'Cost', 'Shipping Cost', + 'SKU', 'Product', 'Variant', 'Quantity', 'Max Quantity', 'Cost', 'Shipping Cost', 'Payment Method', 'Distributor', 'Distributor address', 'Distributor city', 'Distributor postcode', 'Shipping Method', 'Shipping instructions' @@ -37,7 +37,7 @@ } let(:payment_method) { create(:payment_method, distributors: [distributor]) } let(:payment) { create(:payment, payment_method:, order:) } - let(:line_item) { create(:line_item_with_shipment, product:, order:) } + let(:line_item) { create(:line_item_with_shipment, variant:, order:) } subject { described_class.new user } before do @@ -46,33 +46,35 @@ order.line_items << line_item end - it 'should denormalise order and distributor details for display as csv' do - allow(subject).to receive(:unformatted_render?).and_return(true) - table = subject.table_rows - - expect(table.size).to eq 1 - expect(table[0]).to eq([ - order.reload.completed_at.strftime("%F %T"), - order.id, - bill_address.full_name, - order.email, - bill_address.phone, - bill_address.city, - line_item.product.sku, - line_item.product.name, - line_item.unit_to_display, - line_item.quantity, - line_item.max_quantity, - line_item.price * line_item.quantity, - line_item.distribution_fee, - payment_method.name, - distributor.name, - distributor.address.address1, - distributor.address.city, - distributor.address.zipcode, - shipping_method.name, - shipping_instructions - ]) + context "without variant name" do + it 'should denormalise order and distributor details for display as csv' do + allow(subject).to receive(:unformatted_render?).and_return(true) + table = subject.table_rows + + expect(table.size).to eq 1 + expect(table[0]).to eq([ + order.reload.completed_at.strftime("%F %T"), + order.id, + bill_address.full_name, + order.email, + bill_address.phone, + bill_address.city, + line_item.product.sku, + line_item.product.name, + "1g", + line_item.quantity, + line_item.max_quantity, + line_item.price * line_item.quantity, + line_item.distribution_fee, + payment_method.name, + distributor.name, + distributor.address.address1, + distributor.address.city, + distributor.address.zipcode, + shipping_method.name, + shipping_instructions + ]) + end end it "prints one row per line item" do @@ -149,6 +151,17 @@ "Spree::ShippingMethod Load", ] end + + context "with variant name present" do + before do + variant.update_columns(display_name: 'Variant Name'); + end + let(:row) { subject.table_rows.first } + + it "should display variant name with unit" do + expect(row).to include("Variant Name (1g)") + end + end end end end diff --git a/spec/system/admin/reports/orders_and_distributors_spec.rb b/spec/system/admin/reports/orders_and_distributors_spec.rb index 3ee051923b5..8c3090fca51 100644 --- a/spec/system/admin/reports/orders_and_distributors_spec.rb +++ b/spec/system/admin/reports/orders_and_distributors_spec.rb @@ -27,7 +27,7 @@ context "as an enterprise user" do let(:header) { ["Order date", "Order Id", "Customer Name", "Customer Email", "Customer Phone", - "Customer City", "SKU", "Item name", "Variant", "Quantity", "Max Quantity", + "Customer City", "SKU", "Product", "Variant", "Quantity", "Max Quantity", "Cost", "Shipping Cost", "Payment Method", "Distributor", "Distributor address", "Distributor city", "Distributor postcode", "Shipping Method", "Shipping instructions"] From e10c3dc59b3c3d665388a9fd9547ee86a6507acc Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 18 Oct 2024 12:31:05 +0500 Subject: [PATCH 4/5] add specs for migration --- .../report_rendering_options_factory.rb | 7 +++ ..._item_name_to_product_in_od_report_spec.rb | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 spec/factories/report_rendering_options_factory.rb create mode 100644 spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb diff --git a/spec/factories/report_rendering_options_factory.rb b/spec/factories/report_rendering_options_factory.rb new file mode 100644 index 00000000000..928656af30e --- /dev/null +++ b/spec/factories/report_rendering_options_factory.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :orders_and_distributors_options, class: ReportRenderingOptions do + report_type { "orders_and_distributors" } + end +end diff --git a/spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb b/spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb new file mode 100644 index 00000000000..49c23f48b69 --- /dev/null +++ b/spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative '../../db/migrate/20241011071014_update_item_name_to_product_in_od_report' + +RSpec.describe UpdateItemNameToProductInODReport, type: :migration do + let!(:report_option_without_item_name_product) do + create( + :orders_and_distributors_options, + options: { fields_to_show: ['other_field'] } + ) + end + + describe '#up' do + let!(:report_option_with_item_name) do + create( + :orders_and_distributors_options, + options: { fields_to_show: ['item_name', 'other_field'] } + ) + end + before { subject.up } + + it 'updates fields_to_show from item_name to product only if options have item_name' do + report_option_with_item_name.reload + expect(fields_to_show(report_option_with_item_name)).to eq(['other_field', 'product']) + expect(fields_to_show(report_option_without_item_name_product)).to eq(['other_field']) + end + end + + describe '#down' do + let!(:report_option_with_product) do + create( + :orders_and_distributors_options, + options: { fields_to_show: ['product', 'other_field'] } + ) + end + before { subject.down } + + it 'reverts fields_to_show from product to item_name only if options have product' do + report_option_with_product.reload + expect(fields_to_show(report_option_with_product)).to eq(['other_field', 'item_name']) + expect(fields_to_show(report_option_without_item_name_product)).to eq(['other_field']) + end + end + + def fields_to_show(report_options) + report_options.options[:fields_to_show] + end +end From 355541e8dedabc92dcdf75cc9fed65b00133786a Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 18 Oct 2024 12:32:14 +0500 Subject: [PATCH 5/5] Update db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb Co-authored-by: David Cook --- .../20241011071014_update_item_name_to_product_in_od_report.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb b/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb index 9cd5b6dbf58..53cb967ea88 100644 --- a/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb +++ b/db/migrate/20241011071014_update_item_name_to_product_in_od_report.rb @@ -11,7 +11,7 @@ def up fields_to_show.delete('item_name') fields_to_show << 'product' - option.update(options: option.options) + option.save rescue StandardError => e puts "Failed to update rendering option with id: #{option.id}" puts "Error: #{e.message}"