From a120329aa695d0a42cbcf15c8921f14188e07cfe Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 23 Jan 2023 13:27:57 +0100 Subject: [PATCH 01/16] rename EnterpriseFeeSummary::Base to EnterpriseFeeSummary::FeeSummary --- config/locales/en.yml | 5 ++++- .../enterprise_fee_summary/{base.rb => fee_summary.rb} | 2 +- lib/reporting/reports/list.rb | 8 +++++++- .../enterprise_fee_summary_report_spec.rb | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) rename lib/reporting/reports/enterprise_fee_summary/{base.rb => fee_summary.rb} (98%) diff --git a/config/locales/en.yml b/config/locales/en.yml index 66f658e6c5..7543632cae 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2936,6 +2936,9 @@ 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_code: Customer Code report_header_product: Product @@ -2953,7 +2956,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/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..e1adc90f1c 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,12 @@ module Reporting ] end + def enterprise_fee_summary + [ + [i18n_translate('enterprise_fee_summary.name'), :fee_summary], + ] + 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. From 535f651c7ede61fd48b21927c68f39f622f7489e Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 2 Feb 2023 10:16:02 +0100 Subject: [PATCH 02/16] implement enterprise fee report filters --- app/helpers/reports_helper.rb | 21 ++++++++++++ .../filters/_enterprise_fee_summary.html.haml | 32 +------------------ .../reports/filters/_fee_summary.html.haml | 29 +++++++++++++++++ config/locales/en.yml | 2 ++ 4 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 app/views/admin/reports/filters/_fee_summary.html.haml 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/_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 7543632cae..8349a9018e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2645,6 +2645,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" From e9367b1f86f8463cf6a58a357aa9fe0d59b3b7f5 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 26 Jan 2023 09:00:00 +0100 Subject: [PATCH 03/16] add enterprise fees with tax report by order --- app/models/spree/order.rb | 7 +- config/locales/en.yml | 3 + ...nterprise_fees_with_tax_report_by_order.rb | 259 ++++++++++++++++++ lib/reporting/reports/list.rb | 4 + 4 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index d180a4b833..a1fed156dd 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -544,8 +544,11 @@ module Spree shipment_adjustments.reload.tax.sum(:amount) end - def enterprise_fee_tax - all_adjustments.tax.where(adjustable: all_adjustments.enterprise_fee).sum(:amount) + 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) end def total_tax diff --git a/config/locales/en.yml b/config/locales/en.yml index 8349a9018e..dd6ec616fe 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" @@ -2942,6 +2943,8 @@ See the %{link} to find out more about %{sitename}'s features and to start using 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 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..9aab131dfc --- /dev/null +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb @@ -0,0 +1,259 @@ +# frozen_string_literal: true + +module Reporting + module Reports + module EnterpriseFeeSummary + class EnterpriseFeesWithTaxReportByOrder < ReportTemplate + attr_accessor :permissions, :parameters + + 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 + 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)') + .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 + + 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 = order.all_adjustments.enterprise_fee.sum(:amount) + { + 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), + 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 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) + order_id = order(query_result_row).id + enterpise_fee_id = enterprise_fee_id(query_result_row) + amount = Spree::Adjustment.enterprise_fee + .where(order_id: order_id) + .where(originator_id: enterpise_fee_id) + .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_adjustemnt_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_adjustemnt_ids(query_result_row) + order_id = order(query_result_row).id + enterpise_fee_id = enterprise_fee_id(query_result_row) + Spree::Adjustment.enterprise_fee + .where(order_id: order_id) + .where(originator_id: enterpise_fee_id) + .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/list.rb b/lib/reporting/reports/list.rb index e1adc90f1c..910565fcda 100644 --- a/lib/reporting/reports/list.rb +++ b/lib/reporting/reports/list.rb @@ -63,6 +63,10 @@ module Reporting 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 From e9671e4e1fb74a2a2c90f5aff5a4170319244a40 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 26 Jan 2023 09:00:40 +0100 Subject: [PATCH 04/16] test enterprise fees with tax report by order --- ...se_summary_fee_with_tax_report_by_order.rb | 266 ++++++++++++++++++ 1 file changed, 266 insertions(+) create mode 100644 spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb diff --git a/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb new file mode 100644 index 0000000000..8db8dfa1d1 --- /dev/null +++ b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb @@ -0,0 +1,266 @@ +# 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) } + let!(:state_tax_rate){ create(:tax_rate, zone: state_zone, tax_category: tax_category) } + let!(:country_tax_rate){ create(:tax_rate, zone: country_zone, tax_category: tax_category) } + let!(:ship_address){ create(:ship_address) } + + let!(:variant){ create(:variant) } + let!(:product){ variant.product } + let!(:distributor){ create(:distributor_enterprise_with_tax) } + let!(:supplier){ create(:supplier_enterprise) } + let!(:payment_method){ create(:payment_method, :flat_rate) } + let!(:shipping_method){ create(:shipping_method, :flat_rate) } + + let!(:order){ create(:order_with_distributor, distributor: distributor) } + let!(:order_cycle){ + create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], + variants: [variant]) + } + + let(:admin){ create(:admin_user) } + + let!(:coordinator_fees){ + create(:enterprise_fee, :flat_rate, enterprise: distributor, amount: 20) + } + let!(:supplier_fees){ create(:enterprise_fee, :flat_rate, enterprise: supplier, amount: 15) } + let!(:distributor_fee){ create(:enterprise_fee, :flat_rate, enterprise: distributor, amount: 10) } + + 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 + state_tax_rate.update!({ name: 'State', amount: 0.015 }) + country_tax_rate.update!({ name: 'Country', amount: 0.025 }) + tax_category.update!({ name: 'tax_category' }) + + coordinator_fees.update!({ + name: 'Adminstration', + fee_type: 'admin', + tax_category: tax_category + }) + supplier_fees.update!({ + name: 'Transport', + fee_type: 'transport', + tax_category: tax_category + }) + distributor_fee.update!({ + name: 'Packing', + fee_type: 'packing', + tax_category: tax_category + }) + + 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) + order_cycle.update!(name: "oc1") + + distributor.update!({ name: 'Distributor' }) + distributor.shipping_methods << shipping_method + distributor.payment_methods << payment_method + + supplier.update!({ name: 'Supplier', charges_sales_tax: true }) + + product.update!({ + tax_category_id: tax_category.id, + supplier_id: supplier.id + }) + order.update!({ number: order_number }) + 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 From 83b951662afa2c80ebd156c2fe7f9c7d62190762 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 13 Feb 2023 11:24:00 +0100 Subject: [PATCH 05/16] add filters --- ...se_fees_with_tax_report_by_order.html.haml | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/views/admin/reports/filters/_enterprise_fees_with_tax_report_by_order.html.haml 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..1c19788df6 --- /dev/null +++ b/app/views/admin/reports/filters/_enterprise_fees_with_tax_report_by_order.html.haml @@ -0,0 +1,27 @@ +.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_producers) + .omega.fourteen.columns= select_tag(:supplier_id_in, options_from_collection_for_select(@data.orders_suppliers, :id, :name, params[:supplier_id_in]), {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}) From f66d9b9626e7c4d1faac5023b416b0d4d114d118 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 12 Jun 2023 14:41:37 +0100 Subject: [PATCH 06/16] remove producers filter --- .../_enterprise_fees_with_tax_report_by_order.html.haml | 4 ---- 1 file changed, 4 deletions(-) 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 index 1c19788df6..4a3af1ef5d 100644 --- 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 @@ -2,10 +2,6 @@ .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_producers) - .omega.fourteen.columns= select_tag(:supplier_id_in, options_from_collection_for_select(@data.orders_suppliers, :id, :name, params[:supplier_id_in]), {class: "select2 fullwidth", multiple: true}) - .row .alpha.two.columns= label_tag nil, t(:report_customers_cycle) .omega.fourteen.columns From 9d53d775a6fa4e7960ac30d23a671d8095957a8c Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 12 Jun 2023 15:20:00 +0100 Subject: [PATCH 07/16] 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 From 542811b9ad49bd4595c2834388fa4f92a9bc7782 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 12 Jun 2023 15:51:17 +0100 Subject: [PATCH 08/16] implement filtering by entreprise fee owner --- ...nterprise_fees_with_tax_report_by_order.rb | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) 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 ad54b9cf41..0a390fad00 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 @@ -42,7 +42,8 @@ module Reporting .enterprise_fee .group('originator_id') .pluck("originator_id", 'array_agg(id)') - .filter(&method(:filter_enterprise_fee)) + .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, @@ -54,13 +55,30 @@ module Reporting end # [enteperise_fee_id, [adjustment_ids]] - def filter_enterprise_fee(arg) - return true if ransack_params[:enterprise_fee_id_in].reject(&:blank?).empty? + 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( @@ -146,12 +164,20 @@ module Reporting def enterprise_fees(order) query = order.all_adjustments.enterprise_fee - unless ransack_params[:enterprise_fee_id_in].reject(&:blank?).empty? + 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 From 10fc816bba67caf97f75b9c44add1052f780a1e4 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 13 Jun 2023 14:55:07 +0100 Subject: [PATCH 09/16] update rubocop_todo list --- .rubocop_todo.yml | 2 ++ 1 file changed, 2 insertions(+) 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' From 3e61830b4011a2342f87a98ceb44271d821a71cb Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 15 Jun 2023 09:49:55 +0100 Subject: [PATCH 10/16] optimize total_excl_tax & enterprise_fee_adjustemnt_ids --- .../enterprise_fees_with_tax_report_by_order.rb | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) 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 0a390fad00..63bc28710f 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 @@ -222,11 +222,9 @@ module Reporting end def total_excl_tax(query_result_row) - order_id = order(query_result_row).id - enterpise_fee_id = enterprise_fee_id(query_result_row) amount = Spree::Adjustment.enterprise_fee - .where(order_id: order_id) - .where(originator_id: enterpise_fee_id) + .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 @@ -264,11 +262,9 @@ module Reporting end def enterprise_fee_adjustemnt_ids(query_result_row) - order_id = order(query_result_row).id - enterpise_fee_id = enterprise_fee_id(query_result_row) Spree::Adjustment.enterprise_fee - .where(order_id: order_id) - .where(originator_id: enterpise_fee_id) + .where(order: order(query_result_row)) + .where(originator_id: enterprise_fee_id(query_result_row)) .pluck(:id) end From 2cfa435b54568456ff55825caa93f6bfb25011c3 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 15 Jun 2023 08:49:29 +0100 Subject: [PATCH 11/16] Update spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb Co-authored-by: Maikel --- .../enterprise_summary_fee_with_tax_report_by_order.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb index 8db8dfa1d1..d46e5a4650 100644 --- a/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb +++ b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb @@ -67,9 +67,9 @@ describe "Enterprise Summary Fee with Tax Report By Order" do let!(:order_number){ "ORDER_NUMBER_1" } before do - state_tax_rate.update!({ name: 'State', amount: 0.015 }) - country_tax_rate.update!({ name: 'Country', amount: 0.025 }) - tax_category.update!({ name: 'tax_category' }) + state_tax_rate.update!(name: 'State', amount: 0.015) + country_tax_rate.update!(name: 'Country', amount: 0.025) + tax_category.update!(name: 'tax_category') coordinator_fees.update!({ name: 'Adminstration', From c8c3e654988ca668cef0a7dd561c043b3007be66 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Thu, 15 Jun 2023 08:50:21 +0100 Subject: [PATCH 12/16] Update lib/reporting/reports/list.rb Co-authored-by: Maikel --- lib/reporting/reports/list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/reporting/reports/list.rb b/lib/reporting/reports/list.rb index 910565fcda..23489baf00 100644 --- a/lib/reporting/reports/list.rb +++ b/lib/reporting/reports/list.rb @@ -64,7 +64,7 @@ module Reporting [ [i18n_translate('enterprise_fee_summary.name'), :fee_summary], [ - i18n_translate('enterprise_fees_with_tax_report_by_order',), + i18n_translate('enterprise_fees_with_tax_report_by_order'), :enterprise_fees_with_tax_report_by_order ] ] From c21a35a9fc98abd6fd322e8deb4bcc72c8285db1 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Fri, 16 Jun 2023 06:38:15 +0100 Subject: [PATCH 13/16] fix typo on lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb --- .../enterprise_fees_with_tax_report_by_order.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 63bc28710f..1649bbcb7a 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 @@ -231,7 +231,7 @@ module Reporting def tax(query_result_row, all: false, included: nil) order_id = order(query_result_row).id - adjustment_ids = enterprise_fee_adjustemnt_ids(query_result_row) + 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 @@ -261,7 +261,7 @@ module Reporting order(query_result_row).customer&.email end - def enterprise_fee_adjustemnt_ids(query_result_row) + 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)) From 2b79258fdcbf2cac1f16a0396ffd87da90c1e046 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Fri, 16 Jun 2023 06:39:38 +0100 Subject: [PATCH 14/16] renamed: spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb -> spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb --- ...rb => enterprise_summary_fee_with_tax_report_by_order_spec.rb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename spec/system/admin/reports/enterprise_summary_fees/{enterprise_summary_fee_with_tax_report_by_order.rb => enterprise_summary_fee_with_tax_report_by_order_spec.rb} (100%) diff --git a/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb b/spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb similarity index 100% rename from spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order.rb rename to spec/system/admin/reports/enterprise_summary_fees/enterprise_summary_fee_with_tax_report_by_order_spec.rb From 65bc8e23c1636ab2759ebbfbd9444570a95267be Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Fri, 16 Jun 2023 06:50:45 +0100 Subject: [PATCH 15/16] move update instructions to let blocks --- ...mmary_fee_with_tax_report_by_order_spec.rb | 63 +++++++++---------- 1 file changed, 28 insertions(+), 35 deletions(-) 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 index d46e5a4650..11f2fe8b5a 100644 --- 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 @@ -34,31 +34,49 @@ describe "Enterprise Summary Fee with Tax Report By Order" do let!(:state_zone){ create(:zone_with_state_member) } let!(:country_zone){ create(:zone_with_member) } - let!(:tax_category){ create(:tax_category) } - let!(:state_tax_rate){ create(:tax_rate, zone: state_zone, tax_category: tax_category) } - let!(:country_tax_rate){ create(:tax_rate, zone: country_zone, tax_category: tax_category) } + 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) } - let!(:supplier){ create(:supplier_enterprise) } + 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) } + let!(:order){ create(:order_with_distributor, distributor: distributor, number: order_number) } let!(:order_cycle){ - create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], + 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) + 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!(:supplier_fees){ create(:enterprise_fee, :flat_rate, enterprise: supplier, amount: 15) } - let!(:distributor_fee){ create(:enterprise_fee, :flat_rate, enterprise: distributor, amount: 10) } let!(:customer_first_name){ "Customer First Name" } let!(:customer_last_name){ "Customer Last Name" } @@ -67,42 +85,17 @@ describe "Enterprise Summary Fee with Tax Report By Order" do let!(:order_number){ "ORDER_NUMBER_1" } before do - state_tax_rate.update!(name: 'State', amount: 0.015) - country_tax_rate.update!(name: 'Country', amount: 0.025) - tax_category.update!(name: 'tax_category') - - coordinator_fees.update!({ - name: 'Adminstration', - fee_type: 'admin', - tax_category: tax_category - }) - supplier_fees.update!({ - name: 'Transport', - fee_type: 'transport', - tax_category: tax_category - }) - distributor_fee.update!({ - name: 'Packing', - fee_type: 'packing', - tax_category: tax_category - }) - 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) - order_cycle.update!(name: "oc1") - distributor.update!({ name: 'Distributor' }) distributor.shipping_methods << shipping_method distributor.payment_methods << payment_method - supplier.update!({ name: 'Supplier', charges_sales_tax: true }) - product.update!({ tax_category_id: tax_category.id, supplier_id: supplier.id }) - order.update!({ number: order_number }) end context 'added tax' do From 68503e4e8b6e0845453dfebce3ee9f775734d7b8 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Fri, 16 Jun 2023 06:54:11 +0100 Subject: [PATCH 16/16] remove useless variable --- .../enterprise_fees_with_tax_report_by_order.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 1649bbcb7a..de29622269 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 @@ -4,11 +4,10 @@ module Reporting module Reports module EnterpriseFeeSummary class EnterpriseFeesWithTaxReportByOrder < ReportTemplate - attr_accessor :permissions, :parameters + attr_accessor :parameters def initialize(user, params = {}, render: false) super(user, params, render: render) - @permissions = Permissions.new(user) end def search