Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report Orders and Distributors should display variant #12930

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
chahmedejaz marked this conversation as resolved.
Show resolved Hide resolved
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
4 changes: 2 additions & 2 deletions lib/reporting/reports/orders_and_distributors/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
71 changes: 42 additions & 29 deletions spec/lib/reports/orders_and_distributors_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/system/admin/reports/orders_and_distributors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
Loading