diff --git a/config/locales/en.yml b/config/locales/en.yml index a209afda26..7136e06dab 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2697,10 +2697,13 @@ See the %{link} to find out more about %{sitename}'s features and to start using fee_calculated_on_transfer_through_all: "All" fee_type: payment_method: "Payment Transaction" + shipping_method: "Shipment" fee_placements: supplier: "Incoming" distributor: "Outgoing" coordinator: "Coordinator" + tax_category_name: + shipping_instance_rate: "Platform Rate" formats: csv: header: diff --git a/lib/order_management/reports/enterprise_fee_summary/enterprise_fee_type_total_summarizer.rb b/lib/order_management/reports/enterprise_fee_summary/enterprise_fee_type_total_summarizer.rb index 0c81ee1f3a..cd7875f758 100644 --- a/lib/order_management/reports/enterprise_fee_summary/enterprise_fee_type_total_summarizer.rb +++ b/lib/order_management/reports/enterprise_fee_summary/enterprise_fee_type_total_summarizer.rb @@ -11,6 +11,8 @@ module OrderManagement def fee_type if for_payment_method? i18n_translate("fee_type.payment_method") + elsif for_shipping_method? + i18n_translate("fee_type.shipping_method") else data["fee_type"].try(:capitalize) end @@ -18,7 +20,9 @@ module OrderManagement def enterprise_name if for_payment_method? - data["payment_hub_name"] + data["hub_name"] + elsif for_shipping_method? + data["hub_name"] else data["enterprise_name"] end @@ -27,6 +31,8 @@ module OrderManagement def fee_name if for_payment_method? data["payment_method_name"] + elsif for_shipping_method? + data["shipping_method_name"] else data["fee_name"] end @@ -37,13 +43,13 @@ module OrderManagement end def fee_placement - return if for_payment_method? + return if for_payment_method? || for_shipping_method? i18n_translate("fee_placements.#{data['placement_enterprise_role']}") end def fee_calculated_on_transfer_through_name - return if for_payment_method? + return if for_payment_method? || for_shipping_method? transfer_through_all_string = i18n_translate("fee_calculated_on_transfer_through_all") @@ -53,6 +59,7 @@ module OrderManagement def tax_category_name return if for_payment_method? + return i18n_translate("tax_category_name.shipping_instance_rate") if for_shipping_method? data["tax_category_name"] || data["product_tax_category_name"] end @@ -67,6 +74,10 @@ module OrderManagement data["payment_method_name"].present? end + def for_shipping_method? + data["shipping_method_name"].present? + end + def i18n_translate(translation_key) I18n.t("order_management.reports.enterprise_fee_summary.#{translation_key}") end diff --git a/lib/order_management/reports/enterprise_fee_summary/scope.rb b/lib/order_management/reports/enterprise_fee_summary/scope.rb index c9b160b8b0..533536b3b4 100644 --- a/lib/order_management/reports/enterprise_fee_summary/scope.rb +++ b/lib/order_management/reports/enterprise_fee_summary/scope.rb @@ -13,11 +13,12 @@ module OrderManagement protected def setup_default_scope - find_adjustments_for_enterprise_fees_and_payment_methods + find_supported_adjustments include_adjustment_metadata include_order_details include_payment_fee_details + include_shipping_fee_details include_enterprise_fee_details include_line_item_details include_incoming_exchange_details @@ -27,8 +28,8 @@ module OrderManagement select_attributes end - def find_adjustments_for_enterprise_fees_and_payment_methods - find_adjustments.for_orders.for_enterprise_fees_and_payment_methods + def find_supported_adjustments + find_adjustments.for_orders.for_supported_adjustments end def find_adjustments @@ -43,9 +44,10 @@ module OrderManagement end end - def for_enterprise_fees_and_payment_methods + def for_supported_adjustments chain_to_scope do - where(originator_type: ["EnterpriseFee", "Spree::PaymentMethod"]) + where(originator_type: ["EnterpriseFee", "Spree::PaymentMethod", + "Spree::ShippingMethod"]) end end @@ -61,6 +63,7 @@ module OrderManagement # Includes: # * Order # * Customer + # * Hub def include_order_details join_scope( <<-JOIN_STRING.strip_heredoc @@ -73,13 +76,19 @@ module OrderManagement ) join_scope("LEFT OUTER JOIN customers ON (customers.id = spree_orders.customer_id)") + + join_scope( + <<-JOIN_STRING.strip_heredoc + LEFT OUTER JOIN enterprises AS hubs + ON (hubs.id = spree_orders.distributor_id) + JOIN_STRING + ) end # If for payment fee # # Includes: # * Payment method - # * Hub def include_payment_fee_details join_scope( <<-JOIN_STRING.strip_heredoc @@ -102,6 +111,22 @@ module OrderManagement ) end + # If for shipping fee + # + # Includes: + # * Shipping method + def include_shipping_fee_details + join_scope( + <<-JOIN_STRING.strip_heredoc + LEFT OUTER JOIN spree_shipping_methods + ON ( + spree_adjustments.originator_type = 'Spree::ShippingMethod' + AND spree_shipping_methods.id = spree_adjustments.originator_id + ) + JOIN_STRING + ) + end + # Includes: # * Enterprise fee # * Enterprise @@ -252,8 +277,8 @@ module OrderManagement def group_data chain_to_scope do - group("enterprise_fees.id", "enterprises.id", "customers.id", - "spree_payment_methods.id", "payment_hubs.id", + group("enterprise_fees.id", "enterprises.id", "customers.id", "hubs.id", + "spree_payment_methods.id", "spree_shipping_methods.id", "adjustment_metadata.enterprise_role", "spree_tax_categories.id", "product_tax_categories.id", "incoming_exchange_enterprises.id", "outgoing_exchange_enterprises.id") @@ -265,11 +290,12 @@ module OrderManagement select( <<-JOIN_STRING.strip_heredoc 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, + payment_method_name, spree_shipping_methods.name AS shipping_method_name, + hubs.name AS 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 diff --git a/spec/lib/order_management/reports/enterprise_fee_summary/report_service_spec.rb b/spec/lib/order_management/reports/enterprise_fee_summary/report_service_spec.rb index 9f9345b276..e9e253170a 100644 --- a/spec/lib/order_management/reports/enterprise_fee_summary/report_service_spec.rb +++ b/spec/lib/order_management/reports/enterprise_fee_summary/report_service_spec.rb @@ -4,7 +4,10 @@ require "order_management/reports/enterprise_fee_summary/report_service" require "order_management/reports/enterprise_fee_summary/parameters" describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do - let!(:shipping_method) { create(:shipping_method) } + let!(:shipping_method) do + create(:shipping_method, name: "Sample Shipping Method", calculator: per_item_calculator(1.0)) + end + let!(:payment_method) do create(:payment_method, name: "Sample Payment Method", calculator: per_item_calculator(2.0)) end @@ -93,7 +96,7 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do totals = service.enterprise_fee_type_totals - expect(totals.list.length).to eq(14) + expect(totals.list.length).to eq(16) # Data is sorted by the following, in order: # * fee_type @@ -114,12 +117,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"], - ["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"], + ["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", @@ -133,7 +136,11 @@ describe OrderManagement::Reports::EnterpriseFeeSummary::ReportService do ["Sales", "Sample Producer", "Included Producer Fee 2", "Another Customer", "Incoming", "Sample Producer", "Sample Product Tax", "128.00"], ["Sales", "Sample Producer", "Included Producer Fee 2", "Sample Customer", - "Incoming", "Sample Producer", "Sample Product Tax", "256.00"] + "Incoming", "Sample Producer", "Sample Product Tax", "256.00"], + ["Shipment", "Sample Distributor", "Sample Shipping Method", "Another Customer", + nil, nil, "Platform Rate", "1.00"], + ["Shipment", "Sample Distributor", "Sample Shipping Method", "Sample Customer", + nil, nil, "Platform Rate", "2.00"] ] expected_result.each_with_index do |expected_attributes, row_index|