Separate summarizing of SQL data by source

This commit is contained in:
Kristina Lim
2019-01-22 16:32:35 +11:00
parent c4801aafd4
commit 7a76355cfe
8 changed files with 196 additions and 95 deletions

View File

@@ -0,0 +1,31 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class AssociatedEnterpriseFee
attr_reader :context
def initialize(context)
@context = context
end
def fee_type
context.data["fee_type"].try(:capitalize)
end
def enterprise_name
context.data["enterprise_name"]
end
def fee_name
context.data["fee_name"]
end
def fee_placement
context.i18n_translate("fee_placements.#{context.data['placement_enterprise_role']}")
end
end
end
end
end
end

View File

@@ -0,0 +1,22 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class CoordinatorFee < AssociatedEnterpriseFee
def fee_calculated_on_transfer_through_name
context.i18n_translate("fee_calculated_on_transfer_through_all")
end
def tax_category_name
return context.data["tax_category_name"] if context.data["tax_category_name"].present?
context.i18n_translate("tax_category_various") if inherits_tax_category?
end
def inherits_tax_category?
context.data["enterprise_fee_inherits_tax_category"] == "t"
end
end
end
end
end
end

View File

@@ -0,0 +1,18 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class ExchangeOrderFee < AssociatedEnterpriseFee
def fee_calculated_on_transfer_through_name
context.i18n_translate("fee_calculated_on_transfer_through_entire_orders",
distributor: context.data["adjustment_source_distributor_name"])
end
def tax_category_name
context.data["tax_category_name"] || context.i18n_translate("tax_category_various")
end
end
end
end
end
end

View File

@@ -0,0 +1,17 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class IncomingExchangeLineItemFee < AssociatedEnterpriseFee
def fee_calculated_on_transfer_through_name
context.data["incoming_exchange_enterprise_name"]
end
def tax_category_name
context.data["tax_category_name"] || context.data["product_tax_category_name"]
end
end
end
end
end
end

View File

@@ -0,0 +1,17 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class OutgoingExchangeLineItemFee < AssociatedEnterpriseFee
def fee_calculated_on_transfer_through_name
context.data["outgoing_exchange_enterprise_name"]
end
def tax_category_name
context.data["tax_category_name"] || context.data["product_tax_category_name"]
end
end
end
end
end
end

View File

@@ -0,0 +1,33 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class PaymentMethodFee
attr_reader :context
def initialize(context)
@context = context
end
def fee_type
context.i18n_translate("fee_type.payment_method")
end
def enterprise_name
context.data["hub_name"]
end
def fee_name
context.data["payment_method_name"]
end
def fee_placement; end
def fee_calculated_on_transfer_through_name; end
def tax_category_name; end
end
end
end
end
end

View File

@@ -0,0 +1,35 @@
module OrderManagement
module Reports
module EnterpriseFeeSummary
module DataRepresentations
class ShippingMethodFee
attr_reader :context
def initialize(context)
@context = context
end
def fee_type
context.i18n_translate("fee_type.shipping_method")
end
def enterprise_name
context.data["hub_name"]
end
def fee_name
context.data["shipping_method_name"]
end
def fee_placement; end
def fee_calculated_on_transfer_through_name; end
def tax_category_name
context.i18n_translate("tax_category_name.shipping_instance_rate")
end
end
end
end
end
end

View File

@@ -2,108 +2,45 @@ module OrderManagement
module Reports
module EnterpriseFeeSummary
class EnterpriseFeeTypeTotalSummarizer
PAYMENT_METHOD_SOURCE_TYPE = 1
SHIPPING_METHOD_SOURCE_TYPE = 2
COORDINATOR_FEE_SOURCE_TYPE = 3
INCOMING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE = 4
OUTGOING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE = 5
INCOMING_EXCHANGE_ORDER_FEE_SOURCE_TYPE = 6
OUTGOING_EXCHANGE_ORDER_FEE_SOURCE_TYPE = 7
attr_reader :data
attr_accessor :data
delegate :fee_type, :enterprise_name, :fee_name, :fee_placement,
:fee_calculated_on_transfer_through_name, :tax_category_name, to: :representation
def initialize(data)
@data = data
end
def fee_type
case adjustment_source_type
when PAYMENT_METHOD_SOURCE_TYPE
i18n_translate("fee_type.payment_method")
when SHIPPING_METHOD_SOURCE_TYPE
i18n_translate("fee_type.shipping_method")
else
data["fee_type"].try(:capitalize)
end
end
def enterprise_name
case adjustment_source_type
when PAYMENT_METHOD_SOURCE_TYPE, SHIPPING_METHOD_SOURCE_TYPE
data["hub_name"]
else
data["enterprise_name"]
end
end
def fee_name
case adjustment_source_type
when PAYMENT_METHOD_SOURCE_TYPE
data["payment_method_name"]
when SHIPPING_METHOD_SOURCE_TYPE
data["shipping_method_name"]
else
data["fee_name"]
end
end
def customer_name
data["customer_name"]
end
def fee_placement
case adjustment_source_type
when PAYMENT_METHOD_SOURCE_TYPE, SHIPPING_METHOD_SOURCE_TYPE
nil
else
i18n_translate("fee_placements.#{data['placement_enterprise_role']}")
end
end
def fee_calculated_on_transfer_through_name
case adjustment_source_type
when COORDINATOR_FEE_SOURCE_TYPE
i18n_translate("fee_calculated_on_transfer_through_all")
when INCOMING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE
data["incoming_exchange_enterprise_name"]
when OUTGOING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE
data["outgoing_exchange_enterprise_name"]
when INCOMING_EXCHANGE_ORDER_FEE_SOURCE_TYPE, OUTGOING_EXCHANGE_ORDER_FEE_SOURCE_TYPE
i18n_translate("fee_calculated_on_transfer_through_entire_orders",
distributor: data["adjustment_source_distributor_name"])
end
end
def tax_category_name
case adjustment_source_type
when PAYMENT_METHOD_SOURCE_TYPE
nil
when SHIPPING_METHOD_SOURCE_TYPE
i18n_translate("tax_category_name.shipping_instance_rate")
when COORDINATOR_FEE_SOURCE_TYPE
data["tax_category_name"] \
|| (i18n_translate("tax_category_various") if enterprise_fee_inherits_tax_category?)
when INCOMING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE, OUTGOING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE
data["tax_category_name"] || data["product_tax_category_name"]
when INCOMING_EXCHANGE_ORDER_FEE_SOURCE_TYPE, OUTGOING_EXCHANGE_ORDER_FEE_SOURCE_TYPE
data["tax_category_name"] || i18n_translate("tax_category_various")
end
end
def total_amount
data["total_amount"]
end
def i18n_translate(translation_key, options = {})
I18n.t("order_management.reports.enterprise_fee_summary.#{translation_key}", options)
end
private
def adjustment_source_type
return PAYMENT_METHOD_SOURCE_TYPE if for_payment_method?
return SHIPPING_METHOD_SOURCE_TYPE if for_shipping_method?
return COORDINATOR_FEE_SOURCE_TYPE if for_coordinator_fee?
return INCOMING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE if for_incoming_exchange? && for_line_item_adjustment_source?
return OUTGOING_EXCHANGE_LINE_ITEM_FEE_SOURCE_TYPE if for_outgoing_exchange? && for_line_item_adjustment_source?
return INCOMING_EXCHANGE_ORDER_FEE_SOURCE_TYPE if for_incoming_exchange? && for_order_adjustment_source?
return OUTGOING_EXCHANGE_ORDER_FEE_SOURCE_TYPE if for_outgoing_exchange? && for_order_adjustment_source?
def representation
@representation ||= representation_klass.new(self)
end
def representation_klass
return DataRepresentations::PaymentMethodFee if for_payment_method?
return DataRepresentations::ShippingMethodFee if for_shipping_method?
enterprise_fee_adjustment_presentation_klass if for_enterprise_fee?
end
def enterprise_fee_adjustment_presentation_klass
return DataRepresentations::CoordinatorFee if for_coordinator_fee?
return DataRepresentations::ExchangeOrderFee if for_order_adjustment_source?
return unless for_line_item_adjustment_source?
return DataRepresentations::IncomingExchangeLineItemFee if for_incoming_exchange?
return DataRepresentations::OutgoingExchangeLineItemFee if for_outgoing_exchange?
end
def for_payment_method?
@@ -137,15 +74,6 @@ module OrderManagement
def for_line_item_adjustment_source?
data["adjustment_source_type"] == "Spree::LineItem"
end
def enterprise_fee_inherits_tax_category?
for_enterprise_fee? && data["tax_category_name"].blank? \
&& data["enterprise_fee_inherits_tax_category"] == "t"
end
def i18n_translate(translation_key, options = {})
I18n.t("order_management.reports.enterprise_fee_summary.#{translation_key}", options)
end
end
end
end