From 0bcc2eeb3791ccc204c5f94d5bb440038b107915 Mon Sep 17 00:00:00 2001 From: Vishal Date: Thu, 3 Aug 2023 15:58:02 +0530 Subject: [PATCH 1/3] upgraded with spree 4.6 and removed deface dependency --- .../shared/add_analysis_tab_to_main_menu.rb | 12 --- .../spree/admin/shared/_main_menu.html.erb | 99 +++++++++++++++++++ spree_analysis.gemspec | 3 +- 3 files changed, 100 insertions(+), 14 deletions(-) delete mode 100644 app/overrides/spree/admin/shared/add_analysis_tab_to_main_menu.rb create mode 100644 app/views/spree/admin/shared/_main_menu.html.erb diff --git a/app/overrides/spree/admin/shared/add_analysis_tab_to_main_menu.rb b/app/overrides/spree/admin/shared/add_analysis_tab_to_main_menu.rb deleted file mode 100644 index 009cbac..0000000 --- a/app/overrides/spree/admin/shared/add_analysis_tab_to_main_menu.rb +++ /dev/null @@ -1,12 +0,0 @@ -Deface::Override.new( - virtual_path: 'spree/admin/shared/_main_menu', - name: 'add_analysis_tab_to_admin_main_menu', - insert_before: "erb[silent]:contains('if can? :admin, current_store')", - text: <<-HTML - <% if can? :admin, current_store %> - - <% end %> - HTML -) diff --git a/app/views/spree/admin/shared/_main_menu.html.erb b/app/views/spree/admin/shared/_main_menu.html.erb new file mode 100644 index 0000000..ab7e220 --- /dev/null +++ b/app/views/spree/admin/shared/_main_menu.html.erb @@ -0,0 +1,99 @@ + diff --git a/spree_analysis.gemspec b/spree_analysis.gemspec index 5994c1d..c5d19a2 100644 --- a/spree_analysis.gemspec +++ b/spree_analysis.gemspec @@ -21,12 +21,11 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - spree_version = '>=4.4.0' + spree_version = '>=4.6.0' s.add_dependency 'spree', spree_version s.add_dependency 'spree_extension' s.add_dependency 'wicked_pdf' s.add_dependency 'wkhtmltopdf-binary' - s.add_development_dependency 'deface' s.add_development_dependency 'spree_dev_tools' end From 43c9b754a0df57dcc86f81bf3be6f788aacc7c67 Mon Sep 17 00:00:00 2001 From: arvind0143 Date: Thu, 5 Oct 2023 15:57:34 +0530 Subject: [PATCH 2/3] made analysis reports store specific. --- .../spree/admin/analysis_controller.rb | 5 ++ .../spree/best_selling_products_report.rb | 66 ++++++++++--------- app/reports/spree/cart_additions_report.rb | 2 +- ...hod_transactions_conversion_rate_report.rb | 19 +++--- .../payment_method_transactions_report.rb | 3 +- app/reports/spree/promotional_cost_report.rb | 3 +- app/reports/spree/report.rb | 3 +- app/reports/spree/returned_products_report.rb | 6 +- app/reports/spree/sales_performance_report.rb | 1 + app/reports/spree/sales_tax_report.rb | 1 + app/reports/spree/shipping_cost_report.rb | 1 + app/reports/spree/unique_purchases_report.rb | 2 +- .../spree/users_not_converted_report.rb | 2 + .../users_who_recently_purchased_report.rb | 1 + 14 files changed, 67 insertions(+), 48 deletions(-) diff --git a/app/controllers/spree/admin/analysis_controller.rb b/app/controllers/spree/admin/analysis_controller.rb index de5d153..c80dabf 100644 --- a/app/controllers/spree/admin/analysis_controller.rb +++ b/app/controllers/spree/admin/analysis_controller.rb @@ -4,6 +4,7 @@ class AnalysisController < Spree::Admin::BaseController before_action :ensure_report_exists, :set_default_pagination, only: [:show, :download] before_action :set_reporting_period, only: [:index, :show, :download] before_action :load_reports, only: [:index, :show] + before_action :set_current_store, only: [:show, :download] def index respond_to do |format| @@ -47,6 +48,10 @@ def download end private + def set_current_store + params[:store] = current_store + end + def ensure_report_exists @report_name = params[:id].to_sym unless ReportGenerationService.report_exists?(get_report_category, @report_name) diff --git a/app/reports/spree/best_selling_products_report.rb b/app/reports/spree/best_selling_products_report.rb index cc53f00..57440a9 100644 --- a/app/reports/spree/best_selling_products_report.rb +++ b/app/reports/spree/best_selling_products_report.rb @@ -30,41 +30,43 @@ def report_query end private def query_with_inventory_unit_quantities - Spree::LineItem - .joins(:order) - .joins(:variant) - .joins(:product) - .joins(:inventory_units) - .where(Spree::Product.arel_table[:name].matches(search_name)) - .where(spree_orders: { state: 'complete' }) - .where(spree_orders: { completed_at: reporting_period }) - .where.not(spree_inventory_units: { state: 'returned' }) - .group(:variant_id, :product_name, :product_slug, 'spree_variants.sku') - .select( - 'spree_products.name as product_name', - 'spree_products.slug as product_slug', - 'spree_variants.sku as sku', - 'sum(spree_inventory_units.quantity) as sold_count' - ) + ::Spree::LineItem + .joins(:order) + .joins(:variant) + .joins(:product) + .joins(:inventory_units) + .where(spree_orders: { store_id: @current_store.id }) + .where(::Spree::Product.arel_table[:name].matches(search_name)) + .where(spree_orders: { state: 'complete' }) + .where(spree_orders: { completed_at: reporting_period }) + .where.not(spree_inventory_units: { state: 'returned' }) + .group(:variant_id, :product_name, :product_slug, 'spree_variants.sku') + .select( + 'spree_products.name as product_name', + 'spree_products.slug as product_slug', + 'spree_variants.sku as sku', + 'sum(spree_inventory_units.quantity) as sold_count' + ) end private def query_without_inventory_unit_quantities - Spree::LineItem - .joins(:order) - .joins(:variant) - .joins(:product) - .joins(:inventory_units) - .where(Spree::Product.arel_table[:name].matches(search_name)) - .where(spree_orders: { state: 'complete' }) - .where(spree_orders: { completed_at: reporting_period }) - .where.not(spree_inventory_units: { state: 'returned' }) - .group(:variant_id, :product_name, :product_slug, 'spree_variants.sku') - .select( - 'spree_products.name as product_name', - 'spree_products.slug as product_slug', - 'spree_variants.sku as sku', - 'count(spree_line_items.id) as sold_count' - ) + ::Spree::LineItem + .joins(:order) + .joins(:variant) + .joins(:product) + .joins(:inventory_units) + .where(spree_orders: { store_id: @current_store.id }) + .where(::Spree::Product.arel_table[:name].matches(search_name)) + .where(spree_orders: { state: 'complete' }) + .where(spree_orders: { completed_at: reporting_period }) + .where.not(spree_inventory_units: { state: 'returned' }) + .group(:variant_id, :product_name, :product_slug, 'spree_variants.sku') + .select( + 'spree_products.name as product_name', + 'spree_products.slug as product_slug', + 'spree_variants.sku as sku', + 'count(spree_line_items.id) as sold_count' + ) end end end diff --git a/app/reports/spree/cart_additions_report.rb b/app/reports/spree/cart_additions_report.rb index 908ccc4..27126d4 100644 --- a/app/reports/spree/cart_additions_report.rb +++ b/app/reports/spree/cart_additions_report.rb @@ -18,7 +18,7 @@ def sku end def report_query - Spree::Order + Spree::Order.where(store_id: @current_store.id) .incomplete .joins(line_items: { variant: :product }) .where(created_at: reporting_period) diff --git a/app/reports/spree/payment_method_transactions_conversion_rate_report.rb b/app/reports/spree/payment_method_transactions_conversion_rate_report.rb index 2cc6531..3b419ca 100644 --- a/app/reports/spree/payment_method_transactions_conversion_rate_report.rb +++ b/app/reports/spree/payment_method_transactions_conversion_rate_report.rb @@ -60,15 +60,16 @@ def report_query end private def payment_methods - Spree::PaymentMethod - .joins(:payments) - .where(spree_payments: { created_at: reporting_period }) - .select( - 'spree_payment_methods.id as payment_method_id', - 'name as payment_method_name', - 'state as payment_state', - *time_scale_selects('spree_payments') - ) + ::Spree::PaymentMethod + .joins(:stores, payments: [:order]) + .where('spree_payment_methods_stores.store_id = ? AND spree_orders.store_id = ?', @current_store.id.to_s, @current_store.id.to_s) + .where(spree_payments: { created_at: reporting_period }) + .select( + 'spree_payment_methods.id as payment_method_id', + 'spree_payment_methods.name as payment_method_name', + 'spree_payments.state as payment_state', + *time_scale_selects('spree_payments') + ) end end end diff --git a/app/reports/spree/payment_method_transactions_report.rb b/app/reports/spree/payment_method_transactions_report.rb index 0c6e079..f099869 100644 --- a/app/reports/spree/payment_method_transactions_report.rb +++ b/app/reports/spree/payment_method_transactions_report.rb @@ -48,7 +48,8 @@ def report_query private def payments Spree::PaymentMethod - .joins(:payments) + .joins(:stores, payments: [:order]) + .where('spree_payment_methods_stores.store_id = ? AND spree_orders.store_id = ?', @current_store.id.to_s, @current_store.id.to_s) .where(spree_payments: { created_at: reporting_period }) .select( *time_scale_selects('spree_payments'), diff --git a/app/reports/spree/promotional_cost_report.rb b/app/reports/spree/promotional_cost_report.rb index caa22da..f5daf80 100644 --- a/app/reports/spree/promotional_cost_report.rb +++ b/app/reports/spree/promotional_cost_report.rb @@ -66,8 +66,9 @@ def report_query private def eligible_promotions Spree::PromotionAction - .joins(:promotion) + .joins(promotion: [order_promotions: :order]) .joins(:adjustment) + .where(spree_orders: { store_id: @current_store.id }) .where(spree_adjustments: { created_at: reporting_period }) .select( 'spree_promotions.starts_at as promotion_start_date', diff --git a/app/reports/spree/report.rb b/app/reports/spree/report.rb index d0cf257..48bc25a 100644 --- a/app/reports/spree/report.rb +++ b/app/reports/spree/report.rb @@ -2,7 +2,7 @@ module Spree class Report attr_accessor :sortable_attribute, :sortable_type, :total_records, - :records_per_page, :current_page, :paginate, :search, :reporting_period + :records_per_page, :current_page, :paginate, :search, :reporting_period, :current_store alias_method :sort_direction, :sortable_type alias_method :paginate?, :paginate @@ -44,6 +44,7 @@ def initialize(options) self.records_per_page = options[:records_per_page] self.current_page = options[:offset] self.paginate = options[:paginate] + self.current_store = options[:store] #current_store params in report instance extract_reporting_period determine_report_time_scale if self.class::SORTABLE_ATTRIBUTES.present? diff --git a/app/reports/spree/returned_products_report.rb b/app/reports/spree/returned_products_report.rb index 27f2f49..196df31 100644 --- a/app/reports/spree/returned_products_report.rb +++ b/app/reports/spree/returned_products_report.rb @@ -26,7 +26,8 @@ def report_query end private def query_with_inventory_unit_quantities - Spree::ReturnAuthorization.joins(return_items: { inventory_unit: { variant: :product } }) + Spree::ReturnAuthorization.joins(:order).joins(return_items: { inventory_unit: { variant: :product } }) + .where(spree_orders: { store_id: @current_store.id }) .where(spree_return_items: { created_at: reporting_period }) .group('spree_variants.id', 'spree_products.name', 'spree_products.slug', 'spree_variants.sku') .select( @@ -38,7 +39,8 @@ def report_query end private def query_without_inventory_unit_quantities - Spree::ReturnAuthorization.joins(return_items: { inventory_unit: { variant: :product } }) + Spree::ReturnAuthorization.joins(:order).joins(return_items: { inventory_unit: { variant: :product } }) + .where(spree_orders: { store_id: @current_store.id }) .where(spree_return_items: { created_at: reporting_period }) .group('spree_variants.id', 'spree_products.name', 'spree_products.slug', 'spree_variants.sku') .select( diff --git a/app/reports/spree/sales_performance_report.rb b/app/reports/spree/sales_performance_report.rb index d463961..0199f8e 100644 --- a/app/reports/spree/sales_performance_report.rb +++ b/app/reports/spree/sales_performance_report.rb @@ -92,6 +92,7 @@ def promotion_discount private def order_with_line_items line_item_ar = Spree::LineItem.arel_table Spree::Order + .where(store_id: @current_store.id) .where.not(completed_at: nil) .where(created_at: reporting_period) .joins(:line_items) diff --git a/app/reports/spree/sales_tax_report.rb b/app/reports/spree/sales_tax_report.rb index 88be2f9..a7e2383 100644 --- a/app/reports/spree/sales_tax_report.rb +++ b/app/reports/spree/sales_tax_report.rb @@ -49,6 +49,7 @@ def report_query Spree::TaxRate .joins(adjustments: :order) .joins(:zone) + .where(spree_orders: { store_id: @current_store.id }) .where(spree_adjustments: { adjustable_type: 'Spree::LineItem' }) .where(spree_orders: { completed_at: reporting_period }) .select( diff --git a/app/reports/spree/shipping_cost_report.rb b/app/reports/spree/shipping_cost_report.rb index abde44f..d0907cb 100644 --- a/app/reports/spree/shipping_cost_report.rb +++ b/app/reports/spree/shipping_cost_report.rb @@ -55,6 +55,7 @@ def report_query private def order_with_shipments Spree::Order + .where(store_id: @current_store.id) .where.not(completed_at: nil) .where(completed_at: reporting_period) .joins(:shipments) diff --git a/app/reports/spree/unique_purchases_report.rb b/app/reports/spree/unique_purchases_report.rb index 59ffe62..5dba3c7 100644 --- a/app/reports/spree/unique_purchases_report.rb +++ b/app/reports/spree/unique_purchases_report.rb @@ -24,7 +24,7 @@ def report_query .joins(:order) .joins(:variant) .joins(:product) - .where(spree_orders: { state: 'complete', completed_at: reporting_period }) + .where(spree_orders: { state: 'complete', completed_at: reporting_period, store_id: @current_store.id }) .group('variant_id', 'spree_variants.sku', 'spree_products.slug', 'spree_products.name') .select( 'spree_variants.sku as sku', diff --git a/app/reports/spree/users_not_converted_report.rb b/app/reports/spree/users_not_converted_report.rb index b5190ee..01fb264 100644 --- a/app/reports/spree/users_not_converted_report.rb +++ b/app/reports/spree/users_not_converted_report.rb @@ -34,6 +34,8 @@ def report_query orders = Spree::Order.arel_table Spree::User + .joins(orders: :store) + .where(spree_stores: { id: @current_store.id }) .where(created_at: reporting_period) .where(Spree::User.arel_table[:email].matches(email_search)) .select( diff --git a/app/reports/spree/users_who_recently_purchased_report.rb b/app/reports/spree/users_who_recently_purchased_report.rb index d561f0f..6b17a31 100644 --- a/app/reports/spree/users_who_recently_purchased_report.rb +++ b/app/reports/spree/users_who_recently_purchased_report.rb @@ -51,6 +51,7 @@ def paginated_report_query private def all_orders_with_users Spree::Order + .where(store_id: @current_store.id) .where(Spree::Order.arel_table[:email].matches(email_search)) .where(spree_orders: { completed_at: reporting_period }) .select( From a8a6586b9ece3fb74bc9e28a3c9d747179da7f96 Mon Sep 17 00:00:00 2001 From: arvind0143 Date: Fri, 6 Oct 2023 11:18:49 +0530 Subject: [PATCH 3/3] minor change. --- app/controllers/spree/admin/analysis_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/admin/analysis_controller.rb b/app/controllers/spree/admin/analysis_controller.rb index c80dabf..6eeb72a 100644 --- a/app/controllers/spree/admin/analysis_controller.rb +++ b/app/controllers/spree/admin/analysis_controller.rb @@ -4,7 +4,7 @@ class AnalysisController < Spree::Admin::BaseController before_action :ensure_report_exists, :set_default_pagination, only: [:show, :download] before_action :set_reporting_period, only: [:index, :show, :download] before_action :load_reports, only: [:index, :show] - before_action :set_current_store, only: [:show, :download] + before_action :fetch_current_store, only: [:show, :download] def index respond_to do |format| @@ -48,7 +48,7 @@ def download end private - def set_current_store + def fetch_current_store params[:store] = current_store end