Include payment fees in enterprise fee summary

This commit is contained in:
Kristina Lim
2018-10-12 17:13:03 +08:00
committed by luisramos0
parent 7d74ddc85b
commit 1273cc085b
4 changed files with 95 additions and 35 deletions

View File

@@ -2695,6 +2695,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using
enterprise_fee_summary:
date_end_before_start_error: "must be after start"
fee_calculated_on_transfer_through_all: "All"
fee_type:
payment_method: "Payment Transaction"
fee_placements:
supplier: "Incoming"
distributor: "Outgoing"

View File

@@ -9,15 +9,27 @@ module OrderManagement
end
def fee_type
data["fee_type"].capitalize if data["fee_type"]
if for_payment_method?
i18n_translate("fee_type.payment_method")
else
data["fee_type"].try(:capitalize)
end
end
def enterprise_name
data["enterprise_name"]
if for_payment_method?
data["payment_hub_name"]
else
data["enterprise_name"]
end
end
def fee_name
data["fee_name"]
if for_payment_method?
data["payment_method_name"]
else
data["fee_name"]
end
end
def customer_name
@@ -25,10 +37,14 @@ module OrderManagement
end
def fee_placement
return if for_payment_method?
i18n_translate("fee_placements.#{data['placement_enterprise_role']}")
end
def fee_calculated_on_transfer_through_name
return if for_payment_method?
transfer_through_all_string = i18n_translate("fee_calculated_on_transfer_through_all")
data["incoming_exchange_enterprise_name"] || data["outgoing_exchange_enterprise_name"] ||
@@ -36,6 +52,8 @@ module OrderManagement
end
def tax_category_name
return if for_payment_method?
data["tax_category_name"] || data["product_tax_category_name"]
end
@@ -45,6 +63,10 @@ module OrderManagement
private
def for_payment_method?
data["payment_method_name"].present?
end
def i18n_translate(translation_key)
I18n.t("order_management.reports.enterprise_fee_summary.#{translation_key}")
end

View File

@@ -13,11 +13,12 @@ module OrderManagement
protected
def setup_default_scope
find_adjustments_for_enterprise_fees
find_adjustments_for_enterprise_fees_and_payment_methods
include_adjustment_metadata
include_enterprise_fee_details
include_order_details
include_payment_fee_details
include_enterprise_fee_details
include_line_item_details
include_incoming_exchange_details
include_outgoing_exchange_details
@@ -26,8 +27,8 @@ module OrderManagement
select_attributes
end
def find_adjustments_for_enterprise_fees
find_adjustments.for_orders.for_enterprise_fees
def find_adjustments_for_enterprise_fees_and_payment_methods
find_adjustments.for_orders.for_enterprise_fees_and_payment_methods
end
def find_adjustments
@@ -42,9 +43,9 @@ module OrderManagement
end
end
def for_enterprise_fees
def for_enterprise_fees_and_payment_methods
chain_to_scope do
where(originator_type: "EnterpriseFee")
where(originator_type: ["EnterpriseFee", "Spree::PaymentMethod"])
end
end
@@ -57,6 +58,50 @@ module OrderManagement
)
end
# Includes:
# * Order
# * Customer
def include_order_details
join_scope(
<<-JOIN_STRING.strip_heredoc
LEFT OUTER JOIN spree_orders
ON (
spree_adjustments.adjustable_type = 'Spree::Order'
AND spree_orders.id = spree_adjustments.adjustable_id
)
JOIN_STRING
)
join_scope("LEFT OUTER JOIN customers ON (customers.id = spree_orders.customer_id)")
end
# If for payment fee
#
# Includes:
# * Payment method
# * Hub
def include_payment_fee_details
join_scope(
<<-JOIN_STRING.strip_heredoc
LEFT OUTER JOIN spree_payment_methods
ON (
spree_adjustments.originator_type = 'Spree::PaymentMethod'
AND spree_payment_methods.id = spree_adjustments.originator_id
)
JOIN_STRING
)
join_scope(
<<-JOIN_STRING.strip_heredoc
LEFT OUTER JOIN enterprises AS payment_hubs
ON (
spree_payment_methods.id IS NOT NULL
AND payment_hubs.id = spree_orders.distributor_id
)
JOIN_STRING
)
end
# Includes:
# * Enterprise fee
# * Enterprise
@@ -87,23 +132,6 @@ module OrderManagement
)
end
# Includes:
# * Order
# * Customer
def include_order_details
join_scope(
<<-JOIN_STRING.strip_heredoc
LEFT OUTER JOIN spree_orders
ON (
spree_adjustments.adjustable_type = 'Spree::Order'
AND spree_orders.id = spree_adjustments.adjustable_id
)
JOIN_STRING
)
join_scope("LEFT OUTER JOIN customers ON (customers.id = spree_orders.customer_id)")
end
# If for line item - Use data only if spree_line_items.id is present
#
# Includes:
@@ -225,6 +253,7 @@ module OrderManagement
def group_data
chain_to_scope do
group("enterprise_fees.id", "enterprises.id", "customers.id",
"spree_payment_methods.id", "payment_hubs.id",
"adjustment_metadata.enterprise_role", "spree_tax_categories.id",
"product_tax_categories.id", "incoming_exchange_enterprises.id",
"outgoing_exchange_enterprises.id")
@@ -235,11 +264,12 @@ module OrderManagement
chain_to_scope do
select(
<<-JOIN_STRING.strip_heredoc
SUM(spree_adjustments.amount) AS total_amount, enterprises.name AS enterprise_name,
enterprise_fees.fee_type AS fee_type, customers.name AS customer_name,
customers.email AS customer_email, enterprise_fees.fee_type AS fee_type,
enterprise_fees.name AS fee_name, spree_tax_categories.name AS tax_category_name,
product_tax_categories.name AS product_tax_category_name,
SUM(spree_adjustments.amount) AS total_amount, spree_payment_methods.name AS
payment_method_name, payment_hubs.name AS payment_hub_name, enterprises.name AS
enterprise_name, enterprise_fees.fee_type AS fee_type, customers.name AS
customer_name, customers.email AS customer_email, enterprise_fees.fee_type AS
fee_type, enterprise_fees.name AS fee_name, spree_tax_categories.name AS
tax_category_name, product_tax_categories.name AS product_tax_category_name,
adjustment_metadata.enterprise_role AS placement_enterprise_role,
incoming_exchange_enterprises.name AS incoming_exchange_enterprise_name,
outgoing_exchange_enterprises.name AS outgoing_exchange_enterprise_name

View File

@@ -5,7 +5,9 @@ require "order_management/reports/enterprise_fee_summary/parameters"
describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do
let!(:shipping_method) { create(:shipping_method) }
let!(:payment_method) { create(:payment_method) }
let!(:payment_method) do
create(:payment_method, name: "Sample Payment Method", calculator: per_item_calculator(2.0))
end
let!(:distributor) do
create(:distributor_enterprise, name: "Sample Distributor").tap do |enterprise|
@@ -91,7 +93,7 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do
totals = service.enterprise_fee_type_totals
expect(totals.list.length).to eq(12)
expect(totals.list.length).to eq(14)
# Data is sorted by the following, in order:
# * fee_type
@@ -112,8 +114,12 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do
"Outgoing", "Sample Coordinator", "Sample Distributor Tax", "4.00"],
["Admin", "Sample Distributor", "Included Distributor Fee 1", "Sample Customer",
"Outgoing", "Sample Coordinator", "Sample Distributor Tax", "8.00"],
["Sales", "Sample Coordinator", "Included Coordinator Fee 2", "Another Customer",
"Coordinator", "All", "Sample Product Tax", "1024.00"],
["Payment Transaction", "Sample Distributor", "Sample Payment Method",
"Another Customer", nil, nil, nil, "2.00"],
["Payment Transaction", "Sample Distributor", "Sample Payment Method",
"Sample Customer", nil, nil, nil, "4.00"],
["Sales", "Sample Coordinator", "Included Coordinator Fee 2",
"Another Customer", "Coordinator", "All", "Sample Product Tax", "1024.00"],
["Sales", "Sample Coordinator", "Included Coordinator Fee 2", "Sample Customer",
"Coordinator", "All", "Sample Product Tax", "2048.00"],
["Sales", "Sample Distributor", "Included Distributor Fee 2", "Another Customer",