diff --git a/backend/app/finders/spree/admin/reports/orders/top_products_by_line_item_totals.rb b/backend/app/finders/spree/admin/reports/orders/top_products_by_line_item_totals.rb index 99407f2954d..b40c020160e 100644 --- a/backend/app/finders/spree/admin/reports/orders/top_products_by_line_item_totals.rb +++ b/backend/app/finders/spree/admin/reports/orders/top_products_by_line_item_totals.rb @@ -13,13 +13,22 @@ def call variants = by_completed_at_min(variants) variants = by_completed_at_max(variants) - variants = variants.group('spree_variants.sku') - .select('spree_variants.sku, sum(spree_line_items.quantity * spree_prices.amount) as line_item_total') + variants = variants.group( + "#{Spree::Variant.table_name}.id", + "#{Spree::Variant.table_name}.is_master", + "#{Spree::Variant.table_name}.product_id" + ) + .select( + "#{Spree::Variant.table_name}.id", + "#{Spree::Variant.table_name}.is_master", + "#{Spree::Variant.table_name}.product_id", + "sum(#{Spree::LineItem.table_name}.quantity * #{Spree::LineItem.table_name}.price + #{Spree::LineItem.table_name}.adjustment_total) as line_item_total" + ) .order(line_item_total: :desc) variants = by_top(variants) - variants.to_a.map { |v| [v.sku, v.line_item_total] } + variants.to_a.map { |v| [v.descriptive_name, v.line_item_total] } end private diff --git a/backend/app/finders/spree/admin/reports/orders/top_products_by_unit_sold.rb b/backend/app/finders/spree/admin/reports/orders/top_products_by_unit_sold.rb index 6454ccc3628..cb3fecb4c00 100644 --- a/backend/app/finders/spree/admin/reports/orders/top_products_by_unit_sold.rb +++ b/backend/app/finders/spree/admin/reports/orders/top_products_by_unit_sold.rb @@ -12,13 +12,22 @@ def call variants = by_completed_at_min(variants) variants = by_completed_at_max(variants) - variants = variants.group('spree_variants.sku') - .select('spree_variants.sku, sum(spree_line_items.quantity) as total_quantity_sold') + variants = variants.group( + 'spree_variants.id', + 'spree_variants.is_master', + 'spree_variants.product_id' + ) + .select( + 'spree_variants.id', + 'spree_variants.is_master', + 'spree_variants.product_id', + 'sum(spree_line_items.quantity) as total_quantity_sold' + ) .order(total_quantity_sold: :desc) variants = by_top(variants) - variants.to_a.map { |v| [v.sku, v.total_quantity_sold] } + variants.to_a.map { |v| [v.descriptive_name, v.total_quantity_sold] } end private diff --git a/backend/spec/finders/spree/admin/reports/orders/top_products_by_line_item_totals_spec.rb b/backend/spec/finders/spree/admin/reports/orders/top_products_by_line_item_totals_spec.rb index 21346ffd2a1..7fc913124c1 100644 --- a/backend/spec/finders/spree/admin/reports/orders/top_products_by_line_item_totals_spec.rb +++ b/backend/spec/finders/spree/admin/reports/orders/top_products_by_line_item_totals_spec.rb @@ -27,6 +27,10 @@ order1.line_items.first.update(quantity: 3, variant: product1.master) order2.line_items.first.update(quantity: 2, variant: variant1) order3.line_items.first.update(quantity: 1, variant: product2.master) + + order1.line_items.each { |li| li.update_price; li.save! } + order2.line_items.each { |li| li.update_price; li.save! } + order3.line_items.each { |li| li.update_price; li.save! } end context 'when the date range is not present' do @@ -39,8 +43,8 @@ array = subject.call expect(array).to eq [ - [variant1.sku, 180], - [product2.master.sku, 20] + [variant1.descriptive_name, 180], + [product2.master.descriptive_name, 20] ] end end @@ -59,9 +63,9 @@ array = subject.call expect(array).to eq [ - [variant1.sku, 180], - [product1.master.sku, 30], - [product2.master.sku, 20] + [variant1.descriptive_name, 180], + [product1.master.descriptive_name, 30], + [product2.master.descriptive_name, 20] ] end end @@ -78,8 +82,8 @@ array = subject.call expect(array).to eq [ - [variant1.sku, 180], - [product2.master.sku, 20] + [variant1.descriptive_name, 180], + [product2.master.descriptive_name, 20] ] end end @@ -97,7 +101,7 @@ array = subject.call expect(array).to eq [ - [variant1.sku, 180] + [variant1.descriptive_name, 180] ] end end diff --git a/backend/spec/finders/spree/admin/reports/orders/top_products_by_unit_sold_spec.rb b/backend/spec/finders/spree/admin/reports/orders/top_products_by_unit_sold_spec.rb index 8b82557aebf..063c2e8c8ec 100644 --- a/backend/spec/finders/spree/admin/reports/orders/top_products_by_unit_sold_spec.rb +++ b/backend/spec/finders/spree/admin/reports/orders/top_products_by_unit_sold_spec.rb @@ -39,8 +39,8 @@ array = subject.call expect(array).to eq [ - [variant1.sku, 2], - [product2.master.sku, 1] + [variant1.descriptive_name, 2], + [product2.master.descriptive_name, 1] ] end end @@ -59,9 +59,9 @@ array = subject.call expect(array).to eq [ - [product1.master.sku, 3], - [variant1.sku, 2], - [product2.master.sku, 1] + [product1.master.descriptive_name, 3], + [variant1.descriptive_name, 2], + [product2.master.descriptive_name, 1] ] end end @@ -78,8 +78,8 @@ array = subject.call expect(array).to eq [ - [variant1.sku, 2], - [product2.master.sku, 1] + [variant1.descriptive_name, 2], + [product2.master.descriptive_name, 1] ] end end @@ -97,7 +97,7 @@ array = subject.call expect(array).to eq [ - [product1.master.sku, 3] + [product1.master.descriptive_name, 3] ] end end diff --git a/backend/spec/requests/spree/admin/reports/top_products_by_line_item_totals_spec.rb b/backend/spec/requests/spree/admin/reports/top_products_by_line_item_totals_spec.rb index e132797feaa..da60a42366e 100644 --- a/backend/spec/requests/spree/admin/reports/top_products_by_line_item_totals_spec.rb +++ b/backend/spec/requests/spree/admin/reports/top_products_by_line_item_totals_spec.rb @@ -20,6 +20,10 @@ order1.line_items.first.update(quantity: 3, variant: product1.master) order2.line_items.first.update(quantity: 2, variant: variant1) order3.line_items.first.update(quantity: 1, variant: product2.master) + + order1.line_items.each { |li| li.update_price; li.save! } + order2.line_items.each { |li| li.update_price; li.save! } + order3.line_items.each { |li| li.update_price; li.save! } end let(:params) do @@ -37,7 +41,12 @@ end it 'return JSON data for charts' do - expect(json_response['labels']).to eq [variant1.sku, product1.master.sku, product2.master.sku] + pp json_response['data'] + expect(json_response['labels']).to eq [ + variant1.descriptive_name, + product1.master.descriptive_name, + product2.master.descriptive_name + ] expect(json_response['data'].map(&:to_f)).to eq [180, 30, 20] end end diff --git a/backend/spec/requests/spree/admin/reports/top_products_by_unit_sold_spec.rb b/backend/spec/requests/spree/admin/reports/top_products_by_unit_sold_spec.rb index f4ee0409595..4161e0457fb 100644 --- a/backend/spec/requests/spree/admin/reports/top_products_by_unit_sold_spec.rb +++ b/backend/spec/requests/spree/admin/reports/top_products_by_unit_sold_spec.rb @@ -37,7 +37,11 @@ end it 'return JSON data for charts' do - expect(json_response['labels']).to eq [product1.master.sku, variant1.sku, product2.master.sku] + expect(json_response['labels']).to eq [ + product1.master.descriptive_name, + variant1.descriptive_name, + product2.master.descriptive_name + ] expect(json_response['data']).to eq [3, 2, 1] end end