diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index de959163db..7c3be03ccc 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -634,6 +634,7 @@ Metrics/BlockLength: - 'spec/support/matchers/select2_matchers.rb' - 'spec/support/matchers/table_matchers.rb' - 'spec/swagger_helper.rb' + - 'spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb' - 'spec/system/consumer/shopping/checkout_spec.rb' - 'spec/system/consumer/shopping/checkout_stripe_spec.rb' - 'spec/system/consumer/shopping/variant_overrides_spec.rb' @@ -695,6 +696,7 @@ Metrics/ClassLength: - 'lib/open_food_network/order_cycle_form_applicator.rb' - 'lib/open_food_network/order_cycle_permissions.rb' - 'lib/open_food_network/permissions.rb' + - 'lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb' - 'lib/reporting/reports/enterprise_fee_summary/scope.rb' - 'lib/reporting/reports/xero_invoices/base.rb' diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 38ef7c9832..2809ac459a 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -32,7 +32,28 @@ module ReportsHelper end end + def fee_name_options(orders) + EnterpriseFee.where(id: enterprise_fee_ids(orders)) + .pluck(:name, :id) + end + + def fee_owner_options(orders) + Enterprise.where(id: enterprise_fee_owner_ids(orders)) + .pluck(:name, :id) + end + def currency_symbol Spree::Money.currency_symbol end + + def enterprise_fee_owner_ids(orders) + EnterpriseFee.where(id: enterprise_fee_ids(orders)) + .pluck(:enterprise_id) + end + + def enterprise_fee_ids(orders) + Spree::Adjustment.enterprise_fee + .where(order_id: orders.map(&:id)) + .pluck(:originator_id) + end end diff --git a/app/views/admin/reports/filters/_enterprise_fee_summary.html.haml b/app/views/admin/reports/filters/_enterprise_fee_summary.html.haml index 79998980cd..c452e4e942 100644 --- a/app/views/admin/reports/filters/_enterprise_fee_summary.html.haml +++ b/app/views/admin/reports/filters/_enterprise_fee_summary.html.haml @@ -1,31 +1 @@ -= render 'admin/reports/date_range_form', f: f - -.row - .alpha.two.columns= label_tag nil, t(:report_hubs) - .omega.fourteen.columns - = collection_select(:q, :distributor_ids, @report.permissions.allowed_distributors, :id, :name, {selected: params.dig(:q, :distributor_ids)}, {class: "select2 fullwidth", multiple: true}) - -.row - .alpha.two.columns= label_tag nil, t(:report_producers) - .omega.fourteen.columns - = collection_select(:q, :producer_ids, @report.permissions.allowed_producers, :id, :name, {selected: params.dig(:q, :producer_ids)}, {class: "select2 fullwidth", multiple: true}) - -.row - .alpha.two.columns= label_tag nil, t(:order_cycles) - .omega.fourteen.columns - = collection_select(:q, :order_cycle_ids, @report.permissions.allowed_order_cycles, :id, :name, {selected: params.dig(:q, :order_cycle_ids)}, {class: "select2 fullwidth", multiple: true}) - -.row - .alpha.two.columns= label_tag nil, t(:report_enterprise_fee) - .omega.fourteen.columns - = collection_select(:q, :enterprise_fee_ids, @report.permissions.allowed_enterprise_fees, :id, :name, {selected: params.dig(:q, :enterprise_fee_ids)}, {class: "select2 fullwidth", multiple: true}) - -.row - .alpha.two.columns= label_tag nil, t('spree.shipping_methods') - .omega.fourteen.columns - = collection_select(:q, :shipping_method_ids, @report.permissions.allowed_shipping_methods, :id, :name, {selected: params.dig(:q, :shipping_method_ids)}, {class: "select2 fullwidth", multiple: true}) - -.row - .alpha.two.columns= label_tag nil, t(:report_payment) - .omega.fourteen.columns - = collection_select(:q, :payment_method_ids, @report.permissions.allowed_payment_methods, :id, :name, {selected: params.dig(:q, :payment_method_ids)}, {class: "select2 fullwidth", multiple: true}) += render 'admin/reports/date_range_form', f: f \ No newline at end of file diff --git a/app/views/admin/reports/filters/_enterprise_fees_with_tax_report_by_order.html.haml b/app/views/admin/reports/filters/_enterprise_fees_with_tax_report_by_order.html.haml new file mode 100644 index 0000000000..4a3af1ef5d --- /dev/null +++ b/app/views/admin/reports/filters/_enterprise_fees_with_tax_report_by_order.html.haml @@ -0,0 +1,23 @@ +.row + .alpha.two.columns= label_tag nil, t(:report_hubs) + .omega.fourteen.columns= f.collection_select(:distributor_id_in, @data.distributors, :id, :name, {}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:report_customers_cycle) + .omega.fourteen.columns + = f.select(:order_cycle_id_in, report_order_cycle_options(@data.order_cycles), {selected: params.dig(:q, :order_cycle_id_in)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:fee_name) + .omega.fourteen.columns + = f.select(:enterprise_fee_id_in, fee_name_options(@report.search.result), {selected: params.dig(:q, :enterprise_fee_id_in)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:fee_owner) + .omega.fourteen.columns + = f.select(:enterprise_fee_owner_id_in, fee_owner_options(@report.search.result), {selected: params.dig(:q, :enterprise_fee_owner_id_in)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:report_customers) + .omega.fourteen.columns + = f.select(:customer_id_in, customer_email_options(@data.order_customers), {selected: params.dig(:q, :customer_id_in)}, {class: "select2 fullwidth", multiple: true}) diff --git a/app/views/admin/reports/filters/_fee_summary.html.haml b/app/views/admin/reports/filters/_fee_summary.html.haml new file mode 100644 index 0000000000..ea9f16f42d --- /dev/null +++ b/app/views/admin/reports/filters/_fee_summary.html.haml @@ -0,0 +1,29 @@ +.row + .alpha.two.columns= label_tag nil, t(:report_hubs) + .omega.fourteen.columns + = collection_select(:q, :distributor_ids, @report.permissions.allowed_distributors, :id, :name, {selected: params.dig(:q, :distributor_ids)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:report_producers) + .omega.fourteen.columns + = collection_select(:q, :producer_ids, @report.permissions.allowed_producers, :id, :name, {selected: params.dig(:q, :producer_ids)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:order_cycles) + .omega.fourteen.columns + = collection_select(:q, :order_cycle_ids, @report.permissions.allowed_order_cycles, :id, :name, {selected: params.dig(:q, :order_cycle_ids)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:report_enterprise_fee) + .omega.fourteen.columns + = collection_select(:q, :enterprise_fee_ids, @report.permissions.allowed_enterprise_fees, :id, :name, {selected: params.dig(:q, :enterprise_fee_ids)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t('spree.shipping_methods') + .omega.fourteen.columns + = collection_select(:q, :shipping_method_ids, @report.permissions.allowed_shipping_methods, :id, :name, {selected: params.dig(:q, :shipping_method_ids)}, {class: "select2 fullwidth", multiple: true}) + +.row + .alpha.two.columns= label_tag nil, t(:report_payment) + .omega.fourteen.columns + = collection_select(:q, :payment_method_ids, @report.permissions.allowed_payment_methods, :id, :name, {selected: params.dig(:q, :payment_method_ids)}, {class: "select2 fullwidth", multiple: true}) diff --git a/config/locales/en.yml b/config/locales/en.yml index 7138fa4759..e5d85f20fd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1514,6 +1514,7 @@ en: enterprise_fee_summary: name: "Enterprise Fee Summary" description: "Summary of Enterprise Fees collected" + enterprise_fees_with_tax_report_by_order: "Enterprise Fees With Tax Report By Order" errors: no_report_type: "Please specify a report type" report_not_found: "Report not found" @@ -2645,6 +2646,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using shipping_method_destroy_error: "That shipping method cannot be deleted as it is referenced by an order: %{number}." fees: "Fees" + fee_name: "Fee Name" + fee_owner: "Fee Owner" item_cost: "Item cost" bulk: "Bulk" shop_variant_quantity_min: "min" @@ -2936,7 +2939,12 @@ See the %{link} to find out more about %{sitename}'s features and to start using report_header_total_incl_tax: "Total incl. tax (%{currency_symbol})" report_header_total_orders: "Total Number of Orders" report_header_enterprise: Enterprise + report_header_enterprise_fee_name: Name + report_header_enterprise_fee_type: Type + report_header_enterprise_fee_owner: Owner report_header_customer: Customer + report_header_customer_first_name: First Name + report_header_customer_last_name: Last Name report_header_customer_code: Customer Code report_header_product: Product report_header_product_properties: Product Properties @@ -2953,7 +2961,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using report_header_supplier: Supplier report_header_producer: Producer report_header_producer_suburb: Producer Suburb - report_header_producer_tax_status: Producer Tax Status + report_header_producer_tax_status: Producer Tax Status report_header_producer_charges_sales_tax?: GST/VAT Registered report_header_unit: Unit report_header_group_buy_unit_quantity: Group Buy Unit Quantity 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 new file mode 100644 index 0000000000..de29622269 --- /dev/null +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb @@ -0,0 +1,305 @@ +# frozen_string_literal: true + +module Reporting + module Reports + module EnterpriseFeeSummary + class EnterpriseFeesWithTaxReportByOrder < ReportTemplate + attr_accessor :parameters + + def initialize(user, params = {}, render: false) + super(user, params, render: render) + end + + def search + report_line_items.orders + end + + def order_permissions + return @order_permissions unless @order_permissions.nil? + + @order_permissions = ::Permissions::Order.new(user, ransack_params) + end + + def report_line_items + @report_line_items ||= Reporting::LineItems.new(order_permissions, params) + end + + def query_result + # The objective is to group the orders by + # [enterpirse fees,tax_rate, order] + orders = search.result.to_a + orders.flat_map(&join_enterprise_fee) + .flat_map(&join_tax_rate) + .group_by(&group_keys) + .map(&change_root_to_order) + end + + def join_enterprise_fee + proc do |order| + order + .all_adjustments + .enterprise_fee + .group('originator_id') + .pluck("originator_id", 'array_agg(id)') + .filter(&method(:filter_enterprise_fee_by_id)) + .filter(&method(:filter_enterprise_fee_by_owner)) + .map do |enterprise_fee_id, enterprise_fee_adjustment_ids| + { + enterprise_fee_id: enterprise_fee_id, + enterprise_fee_adjustment_ids: enterprise_fee_adjustment_ids, + order: order + } + end + end + end + + # [enteperise_fee_id, [adjustment_ids]] + def filter_enterprise_fee_by_id(arg) + return true unless filter_enterprise_fee_by_id_active? + + enterprise_fee_id = arg.first.to_s + enterprise_fee_id.in?(ransack_params[:enterprise_fee_id_in]) + end + + def filter_enterprise_fee_by_owner(arg) + return true unless filter_enteprise_fee_by_owner_active? + + enterprise_fee_id = arg.first + + EnterpriseFee.exists?(id: enterprise_fee_id, + enterprise_id: ransack_params[:enterprise_fee_owner_id_in] ) + end + + def filter_enterprise_fee_by_id_active? + !ransack_params[:enterprise_fee_id_in].compact_blank.empty? + end + + def filter_enteprise_fee_by_owner_active? + !ransack_params[:enterprise_fee_owner_id_in].compact_blank.empty? + end + + def join_tax_rate + proc do |item| + tax_rate_ids = item[:order].all_adjustments.tax.where( + adjustable_id: item[:enterprise_fee_adjustment_ids], + adjustable_type: "Spree::Adjustment" + ).pluck(:originator_id) + + tax_rate_ids << nil if tax_rate_ids.empty? + tax_rate_ids.map do |tax_rate_id| + { + tax_rate_id: tax_rate_id, + enterprise_fee_id: item[:enterprise_fee_id], + order: item[:order], + } + end + end + end + + def group_keys + proc do |hash| + [ + hash[:tax_rate_id], + hash[:enterprise_fee_id], + hash[:order].id + ] + end + end + + def change_root_to_order + proc do |k, v| + [k, v.first[:order]] + end + end + + def columns + { + distributor: :distributor, + order_cycle: :order_cycle, + order_number: :order_number, + enterprise_fee_name: :enterprise_fee_name, + enterprise_fee_type: :enterprise_fee_type, + enterprise_fee_owner: :enterprise_fee_owner, + tax_category: :tax_category, + tax_rate_name: :tax_rate_name, + tax_rate: :tax_rate_amount, + total_excl_tax: :total_excl_tax, + tax: :tax, + total_incl_tax: :total_incl_tax, + customer_first_name: :customer_first_name, + customer_last_name: :customer_last_name, + customer_code: :customer_code, + customer_email: :customer_email + } + end + + def rules + [ + { group_by: :distributor }, + { group_by: :order_cycle }, + { + group_by: :order_number, + summary_row: proc do |_key, items, _rows| + item = items.first + order = item.second + enterprise_fees = enterprise_fees_sum(order) + { + 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, + customer_email: order.customer&.email + } + end + } + ] + end + + def enterprise_fees_sum(order) + enterprise_fees(order).sum(:amount) + end + + def enterprise_fees(order) + query = order.all_adjustments.enterprise_fee + if filter_enterprise_fee_by_id_active? + query = query.where(originator_id: ransack_params[:enterprise_fee_id_in]) + end + if filter_enteprise_fee_by_owner_active? + query = query.where(originator_id: enterprise_fee_ids_for_selected_owners) + end + query + end + + def enterprise_fee_ids_for_selected_owners + EnterpriseFee.where( enterprise_id: ransack_params[:enterprise_fee_owner_id_in] ) + .pluck(:id) + 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 + + def order_cycle(query_result_row) + order(query_result_row).order_cycle&.name + end + + def order_number(query_result_row) + order(query_result_row).number + end + + def enterprise_fee_name(query_result_row) + enterprise_fee(query_result_row).name + end + + def enterprise_fee_type(query_result_row) + enterprise_fee(query_result_row).fee_type + end + + def enterprise_fee_owner(query_result_row) + enterprise_fee(query_result_row).enterprise.name + end + + def tax_category(query_result_row) + tax_rate(query_result_row)&.tax_category&.name + end + + def tax_rate_name(query_result_row) + tax_rate(query_result_row)&.name + end + + def tax_rate_amount(query_result_row) + tax_rate(query_result_row)&.amount + end + + def total_excl_tax(query_result_row) + amount = Spree::Adjustment.enterprise_fee + .where(order: order(query_result_row)) + .where(originator_id: enterprise_fee_id(query_result_row)) + .pick("sum(amount)") || 0 + amount - tax(query_result_row, all: true, included: true) + end + + def tax(query_result_row, all: false, included: nil) + order_id = order(query_result_row).id + adjustment_ids = enterprise_fee_adjustment_ids(query_result_row) + query = Spree::Adjustment.tax + query = query.where(included: true) unless included.nil? + query = query.where(originator_id: tax_rate_id(query_result_row)) unless all == true + query.where(order_id: order_id) + .where(adjustable_type: 'Spree::Adjustment') + .where(adjustable_id: adjustment_ids) + .pick("sum(amount)") || 0 + end + + def total_incl_tax(query_result_row) + total_excl_tax(query_result_row) + tax(query_result_row, all: false) + end + + def customer_first_name(query_result_row) + order(query_result_row).customer&.first_name + end + + def customer_last_name(query_result_row) + order(query_result_row).customer&.last_name + end + + def customer_code(query_result_row) + order(query_result_row).customer&.code + end + + def customer_email(query_result_row) + order(query_result_row).customer&.email + end + + def enterprise_fee_adjustment_ids(query_result_row) + Spree::Adjustment.enterprise_fee + .where(order: order(query_result_row)) + .where(originator_id: enterprise_fee_id(query_result_row)) + .pluck(:id) + end + + def enterprise_fee(query_result_row) + order(query_result_row).all_adjustments + .enterprise_fee + .find_by(originator_id: enterprise_fee_id(query_result_row)) + .originator + end + + def tax_rate(query_result_row) + return nil if tax_rate_id(query_result_row).nil? + + Spree::TaxRate.find(tax_rate_id(query_result_row)) + end + + def order(query_result_row) + query_result_row.second + end + + def tax_rate_id(query_result_row) + keys(query_result_row)[0] + end + + def supplier_id(query_result_row) + keys(query_result_row)[2] + end + + def enterprise_fee_id(query_result_row) + keys(query_result_row)[1] + end + + def keys(query_result_row) + query_result_row.first + end + end + end + end +end diff --git a/lib/reporting/reports/enterprise_fee_summary/base.rb b/lib/reporting/reports/enterprise_fee_summary/fee_summary.rb similarity index 98% rename from lib/reporting/reports/enterprise_fee_summary/base.rb rename to lib/reporting/reports/enterprise_fee_summary/fee_summary.rb index f3c0be2dd8..a27aa154a2 100644 --- a/lib/reporting/reports/enterprise_fee_summary/base.rb +++ b/lib/reporting/reports/enterprise_fee_summary/fee_summary.rb @@ -3,7 +3,7 @@ module Reporting module Reports module EnterpriseFeeSummary - class Base < ReportTemplate + class FeeSummary < ReportTemplate attr_accessor :permissions, :parameters def initialize(user, params = {}, render: false) diff --git a/lib/reporting/reports/list.rb b/lib/reporting/reports/list.rb index a41527236a..23489baf00 100644 --- a/lib/reporting/reports/list.rb +++ b/lib/reporting/reports/list.rb @@ -16,7 +16,7 @@ module Reporting customers: customers_report_types, products_and_inventory: products_and_inventory_report_types, users_and_enterprises: [], - enterprise_fee_summary: [], + enterprise_fee_summary: enterprise_fee_summary, order_cycle_management: order_cycle_management_report_types, sales_tax: sales_tax_report_types, xero_invoices: xero_report_types, @@ -60,6 +60,16 @@ module Reporting ] end + def enterprise_fee_summary + [ + [i18n_translate('enterprise_fee_summary.name'), :fee_summary], + [ + i18n_translate('enterprise_fees_with_tax_report_by_order'), + :enterprise_fees_with_tax_report_by_order + ] + ] + end + def order_cycle_management_report_types [ [i18n_translate("payment_methods"), :payment_methods], diff --git a/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb b/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb index 58711f74ba..b7d8f0ab2c 100644 --- a/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb +++ b/spec/lib/reports/enterprise_fee_summary/enterprise_fee_summary_report_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" -describe Reporting::Reports::EnterpriseFeeSummary::Base do +describe Reporting::Reports::EnterpriseFeeSummary::FeeSummary do let(:report_module) { Reporting::Reports::EnterpriseFeeSummary } # Basic data. diff --git a/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb new file mode 100644 index 0000000000..11f2fe8b5a --- /dev/null +++ b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb @@ -0,0 +1,259 @@ +# frozen_string_literal: true + +require 'system_helper' + +describe "Enterprise Summary Fee with Tax Report By Order" do + # 1 order cycle the has: + # - coordinator fees price 20 + # - incoming exchange fees 15 + # - outgoing exchange fees 10 + # tax + # country: 2.5% + # state: 1.5% + + let!(:table_header){ + [ + "Distributor", + "Order Cycle", + "Order Number", + "Name", + "Type", + "Owner", + "Tax Category", + "Tax Rate Name", + "Tax Rate", + "Total excl. tax ($)", + "Tax", + "Total incl. tax ($)", + "First Name", + "Last Name", + "Customer Code", + "Customer Email" + ].join(" ").upcase + } + + let!(:state_zone){ create(:zone_with_state_member) } + let!(:country_zone){ create(:zone_with_member) } + let!(:tax_category){ create(:tax_category, name: 'tax_category') } + let!(:state_tax_rate){ + create(:tax_rate, zone: state_zone, tax_category: tax_category, name: 'State', amount: 0.015) + } + let!(:country_tax_rate){ + create(:tax_rate, zone: country_zone, tax_category: tax_category, name: 'Country', + amount: 0.025) + } + let!(:ship_address){ create(:ship_address) } + + let!(:variant){ create(:variant) } + let!(:product){ variant.product } + let!(:distributor){ create(:distributor_enterprise_with_tax, name: 'Distributor') } + let!(:supplier){ create(:supplier_enterprise, name: 'Supplier', charges_sales_tax: true) } + let!(:payment_method){ create(:payment_method, :flat_rate) } + let!(:shipping_method){ create(:shipping_method, :flat_rate) } + + let!(:order){ create(:order_with_distributor, distributor: distributor, number: order_number) } + let!(:order_cycle){ + create(:simple_order_cycle, name: "oc1", suppliers: [supplier], distributors: [distributor], + variants: [variant]) + } + + let(:admin){ create(:admin_user) } + + let!(:coordinator_fees){ + create(:enterprise_fee, :flat_rate, enterprise: distributor, amount: 20, + name: 'Adminstration', + fee_type: 'admin', + tax_category: tax_category) + } + let!(:supplier_fees){ + create(:enterprise_fee, :flat_rate, enterprise: supplier, amount: 15, + name: 'Transport', + fee_type: 'transport', + tax_category: tax_category) + } + let!(:distributor_fee){ + create(:enterprise_fee, :flat_rate, enterprise: distributor, amount: 10, + name: 'Packing', + fee_type: 'packing', + tax_category: tax_category) + } + + let!(:customer_first_name){ "Customer First Name" } + let!(:customer_last_name){ "Customer Last Name" } + let!(:customer_code){ "CUS_CODE" } + let!(:customer_email){ "customer1@example.com" } + let!(:order_number){ "ORDER_NUMBER_1" } + + before do + order_cycle.coordinator_fees << coordinator_fees + order_cycle.exchanges.incoming.first.exchange_fees.create!(enterprise_fee: supplier_fees) + order_cycle.exchanges.outgoing.first.exchange_fees.create!(enterprise_fee: distributor_fee) + + distributor.shipping_methods << shipping_method + distributor.payment_methods << payment_method + + product.update!({ + tax_category_id: tax_category.id, + supplier_id: supplier.id + }) + end + + context 'added tax' do + before do + order.line_items.create({ variant: variant, quantity: 1, price: 100 }) + order.update!({ + order_cycle_id: order_cycle.id, + ship_address_id: ship_address.id + }) + # This will load the enterprise fees from the order cycle. + # This is needed because the order instance was created + # independently of the order_cycle. + # order.reload + order.recreate_all_fees! + OrderWorkflow.new(order).complete! + + order.customer.update!({ + first_name: customer_first_name, + last_name: customer_last_name, + code: customer_code, + email: customer_email + }) + end + + let(:coordinator_state_tax){ + ["Distributor", "oc1", order_number, "Adminstration", "admin", "Distributor", + "tax_category", "State", "0.015", "20.0", "0.3", "20.3", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + let(:coordinator_country_tax){ + ["Distributor", "oc1", order_number, "Adminstration", "admin", "Distributor", + "tax_category", "Country", "0.025", "20.0", "0.5", "20.5", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + let(:supplier_state_tax){ + ["Distributor", "oc1", order_number, "Transport", "transport", "Supplier", + "tax_category", "State", "0.015", "15.0", "0.23", "15.23", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + let(:supplier_country_tax){ + ["Distributor", "oc1", order_number, "Transport", "transport", "Supplier", + "tax_category", "Country", "0.025", "15.0", "0.38", "15.38", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + let(:distributor_state_tax){ + ["Distributor", "oc1", order_number, "Packing", "packing", "Distributor", + "tax_category", "State", "0.015", "10.0", "0.15", "10.15", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + let(:distributor_country_tax){ + ["Distributor", "oc1", order_number, "Packing", "packing", "Distributor", + "tax_category", "Country", "0.025", "10.0", "0.25", "10.25", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + let(:summary_row){ + ["TOTAL", "45.0", "1.81", "46.81", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + it 'generates the report' do + login_as admin + visit admin_reports_path + click_on I18n.t("admin.reports.enterprise_fees_with_tax_report_by_order") + + expect(page).to have_button("Go") + click_on "Go" + + expect(page.find("table.report__table thead tr").text).to have_content(table_header) + + expect(page.find("table.report__table tbody").text).to have_content(supplier_state_tax) + expect(page.find("table.report__table tbody").text).to have_content(supplier_country_tax) + expect(page.find("table.report__table tbody").text).to have_content(distributor_state_tax) + expect(page.find("table.report__table tbody").text).to have_content(distributor_country_tax) + expect(page.find("table.report__table tbody").text).to have_content(coordinator_state_tax) + expect(page.find("table.report__table tbody").text).to have_content(coordinator_country_tax) + expect(page.find("table.report__table tbody").text).to have_content(summary_row) + end + end + + context 'included tax' do + before do + state_tax_rate.update!({ included_in_price: true }) + country_tax_rate.update!({ included_in_price: true }) + + order.line_items.create({ variant: variant, quantity: 1, price: 100 }) + order.update!({ + order_cycle_id: order_cycle.id, + ship_address_id: ship_address.id + }) + order.recreate_all_fees! + OrderWorkflow.new(order).complete! + + order.customer.update!({ + first_name: customer_first_name, + last_name: customer_last_name, + code: customer_code, + email: customer_email + }) + end + + let(:coordinator_state_tax){ + ["Distributor", "oc1", order_number, "Adminstration", "admin", "Distributor", + "tax_category", "State", "0.015", "19.21", "0.3", "19.51", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + let(:coordinator_country_tax){ + ["Distributor", "oc1", order_number, "Adminstration", "admin", "Distributor", + "tax_category", "Country", "0.025", "19.21", "0.49", "19.7", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + let(:supplier_state_tax){ + ["Distributor", "oc1", order_number, "Transport", "transport", "Supplier", + "tax_category", "State", "0.015", "14.41", "0.22", "14.63", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + let(:supplier_country_tax){ + ["Distributor", "oc1", order_number, "Transport", "transport", "Supplier", + "tax_category", "Country", "0.025", "14.41", "0.37", "14.78", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + let(:distributor_state_tax){ + ["Distributor", "oc1", order_number, "Packing", "packing", "Distributor", + "tax_category", "State", "0.015", "9.61", "0.15", "9.76", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + let(:distributor_country_tax){ + ["Distributor", "oc1", order_number, "Packing", "packing", "Distributor", + "tax_category", "Country", "0.025", "9.61", "0.24", "9.85", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + let(:summary_row){ + ["TOTAL", "43.23", "1.77", "45.0", + customer_first_name, customer_last_name, customer_code, customer_email].join(" ") + } + + it 'generates the report' do + login_as admin + visit admin_reports_path + click_on I18n.t("admin.reports.enterprise_fees_with_tax_report_by_order") + + expect(page).to have_button("Go") + click_on "Go" + + expect(page.find("table.report__table thead tr").text).to have_content(table_header) + + expect(page.find("table.report__table tbody").text).to have_content(supplier_state_tax) + expect(page.find("table.report__table tbody").text).to have_content(supplier_country_tax) + expect(page.find("table.report__table tbody").text).to have_content(distributor_state_tax) + expect(page.find("table.report__table tbody").text).to have_content(distributor_country_tax) + expect(page.find("table.report__table tbody").text).to have_content(coordinator_state_tax) + expect(page.find("table.report__table tbody").text).to have_content(coordinator_country_tax) + expect(page.find("table.report__table tbody").text).to have_content(summary_row) + end + end +end