From 9d53d775a6fa4e7960ac30d23a671d8095957a8c Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 12 Jun 2023 15:20:00 +0100 Subject: [PATCH] add filtering by fee name --- app/models/spree/order.rb | 7 +--- ...nterprise_fees_with_tax_report_by_order.rb | 39 +++++++++++++++---- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index a1fed156dd..d180a4b833 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -544,11 +544,8 @@ module Spree shipment_adjustments.reload.tax.sum(:amount) end - def enterprise_fee_tax(included: false,added: false) - query = all_adjustments.tax - query = query.inclusive if included == true - query = query.additional if added == true - query.where(adjustable: all_adjustments.enterprise_fee).sum(:amount) + def enterprise_fee_tax + all_adjustments.tax.where(adjustable: all_adjustments.enterprise_fee).sum(:amount) end def total_tax diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb index 9aab131dfc..ad54b9cf41 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb @@ -8,10 +8,7 @@ module Reporting def initialize(user, params = {}, render: false) super(user, params, render: render) - @parameters = Parameters.new(p || {}) - @parameters.validate! @permissions = Permissions.new(user) - @parameters.authorize!(@permissions) end def search @@ -45,6 +42,7 @@ module Reporting .enterprise_fee .group('originator_id') .pluck("originator_id", 'array_agg(id)') + .filter(&method(:filter_enterprise_fee)) .map do |enterprise_fee_id, enterprise_fee_adjustment_ids| { enterprise_fee_id: enterprise_fee_id, @@ -55,6 +53,14 @@ module Reporting end end + # [enteperise_fee_id, [adjustment_ids]] + def filter_enterprise_fee(arg) + return true if ransack_params[:enterprise_fee_id_in].reject(&:blank?).empty? + + enterprise_fee_id = arg.first.to_s + enterprise_fee_id.in?(ransack_params[:enterprise_fee_id_in]) + end + def join_tax_rate proc do |item| tax_rate_ids = item[:order].all_adjustments.tax.where( @@ -119,11 +125,11 @@ module Reporting summary_row: proc do |_key, items, _rows| item = items.first order = item.second - enterprise_fees = order.all_adjustments.enterprise_fee.sum(:amount) + enterprise_fees = enterprise_fees_sum(order) { - total_excl_tax: enterprise_fees - order.enterprise_fee_tax(included: true), - tax: order.enterprise_fee_tax, - total_incl_tax: enterprise_fees + order.enterprise_fee_tax(added: true), + total_excl_tax: enterprise_fees - enterprise_fee_tax(order, included: true), + tax: enterprise_fee_tax(order), + total_incl_tax: enterprise_fees + enterprise_fee_tax(order, added: true), customer_first_name: order.customer&.first_name, customer_last_name: order.customer&.last_name, customer_code: order.customer&.code, @@ -134,6 +140,25 @@ module Reporting ] end + def enterprise_fees_sum(order) + enterprise_fees(order).sum(:amount) + end + + def enterprise_fees(order) + query = order.all_adjustments.enterprise_fee + unless ransack_params[:enterprise_fee_id_in].reject(&:blank?).empty? + query = query.where(originator_id: ransack_params[:enterprise_fee_id_in]) + end + query + end + + def enterprise_fee_tax(order, included: false, added: false) + query = order.all_adjustments.tax + query = query.inclusive if included == true + query = query.additional if added == true + query.where(adjustable: enterprise_fees(order)).sum(:amount) + end + def distributor(query_result_row) order(query_result_row).distributor&.name end