Skip to content

Commit

Permalink
test sales_tax_totals_by_order
Browse files Browse the repository at this point in the history
  • Loading branch information
abdellani committed Jan 11, 2023
1 parent 27ae5b2 commit b2cc5a9
Showing 1 changed file with 394 additions and 0 deletions.
394 changes: 394 additions & 0 deletions spec/system/admin/reports/sales_tax/sales_tax_totals_by_order_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,394 @@
# frozen_string_literal: true

require 'system_helper'

describe "Sales Tax Totals By order" do
# Scenarion 1: added tax
# 1 producer
# 1 distributor
# product that costs 100$
# shipping costs 10$
# the packaging cost is 5$
# 1 order with 1 line item
# the line item match 2 tax rates: country (2.5%) and state (1.5%)
let!(:table_header){
[
"Distributor",
"Order Cycle",
"Order ID",
"Tax Category",
"Tax Rate Name",
"Tax Rate",
"Total excl. Tax ($)",
"Tax",
"Total incl. Tax ($)"
].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!(:supplier){ create(:supplier_enterprise) }
let!(:distributor){ create(:distributor_enterprise_with_tax) }
let!(:distributor_fee){ create(:enterprise_fee, :flat_rate, amount: 5) }
let!(:payment_method){ create(:payment_method, :flat_rate) }
let!(:shipping_method){ create(:shipping_method, :flat_rate, amount: 10) }

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) }

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' })

distributor_fee.update!({
name: "packing fees",
fee_type: "packing",
tax_category_id: tax_category.id,
enterprise_id: distributor.id
})

ship_address.update!({
state: state_zone.members.first.zoneable,
country: country_zone.members.first.zoneable
})

order_cycle.cached_outgoing_exchanges.first.enterprise_fees << distributor_fee

order_cycle.update!(name: "oc1")

shipping_method.update!({ tax_category_id: tax_category.id })

distributor.update!({ name: 'Distributor', charges_sales_tax: true })

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_1',
order_cycle_id: order_cycle.id,
ship_address_id: ship_address.id
})
# When an order instance is created, the create_tax_charge! will be called
# create_tax_charge! will initialize the instance variable fee_handler
# fee_handler is reponsible for creating the enterprise fees (among other tasks).
# To do those tasks, it depends on the order_cycle defined on the order.
#
# In this test, the order_cycle is not set when the order is created using the Factory.
# the fee_handler will be initialized with order_cycle == nil
# so we'll need to destroy the current fee_handler to force the order recreating a new fee_handler
order.instance_variable_set(:@fee_handler, nil)
end
context 'added tax' do
before do
order.line_items.create({ variant: variant, quantity: 1, price: 100 })
# the enterprise fees can be known only when the user selects the variants
# we'll need to create them by calling recreate_all_fees!
order.recreate_all_fees!

while !order.completed?
break unless order.next!
end
end
it "generates the report" do
login_as admin
visit admin_reports_path
click_on I18n.t("admin.reports.sales_tax_totals_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([
"Distributor",
"oc1",
"ORDER_NUMBER_1",
"tax_category",
"State",
"1.5 %",
"115.0",
"1.73",
"116.73"
].join(" "))

expect(page.find("table.report__table tbody").text).to have_content([
"Distributor",
"oc1",
"ORDER_NUMBER_1",
"tax_category",
"Country",
"2.5 %",
"115.0",
"2.88",
"117.88"
].join(" "))

expect(page.find("table.report__table tbody").text).to have_content([
"TOTAL",
"115.0",
"4.61",
"119.61"
].join(" "))
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.recreate_all_fees!

while !order.completed?
break unless order.next!
end
end
it "generates the report" do
login_as admin
visit admin_reports_path
click_on I18n.t("admin.reports.sales_tax_totals_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([
"Distributor",
"oc1",
"ORDER_NUMBER_1",
"tax_category",
"State",
"1.5 %",
"110.5",
"1.7",
"112.2"
].join(" "))

expect(page.find("table.report__table tbody").text).to have_content([
"Distributor",
"oc1",
"ORDER_NUMBER_1",
"tax_category",
"Country",
"2.5 %",
"110.5",
"2.8",
"113.3"
].join(" "))

expect(page.find("table.report__table tbody").text).to have_content([
"TOTAL",
"110.5",
"4.5",
"115.0"
].join(" "))
end
end

context 'should filter by customer' do
let!(:order2){ create(:order_with_distributor, distributor: distributor) }
let!(:customer1){ create(:customer, enterprise: create(:enterprise), user: create(:user)) }
let!(:customer2){ create(:customer, enterprise: create(:enterprise), user: create(:user)) }
let!(:customer_email_dropdown_selector){ "#s2id_q_customer_id_in" }
let!(:table_raw_selector){"table.report__table tbody tr"}
let(:customer1_country_tax_rate_row){
[
"Distributor",
"oc1",
"ORDER_NUMBER_1",
"tax_category",
"Country",
"2.5 %",
"115.0",
"2.88",
"117.88"
].join(" ")
}
let(:customer1_state_tax_rate_row){
[
"Distributor",
"oc1",
"ORDER_NUMBER_1",
"tax_category",
"State",
"1.5 %",
"115.0",
"1.73",
"116.73"
].join(" ")
}
let(:customer1_summary_row){
[
"TOTAL",
"115.0",
"4.61",
"119.61"
].join(" ")
}

let(:customer2_country_tax_rate_row){
[
"Distributor",
"oc1",
"ORDER_NUMBER_2",
"tax_category",
"Country",
"2.5 %",
"215.0",
"5.38",
"220.38"
].join(" ")
}
let(:customer2_state_tax_rate_row){
[
"Distributor",
"oc1",
"ORDER_NUMBER_2",
"tax_category",
"State",
"1.5 %",
"215.0",
"3.23",
"218.23"
].join(" ")
}
let(:customer2_summary_row){
[
"TOTAL",
"215.0",
"8.61",
"223.61"
].join(" ")
}

before do
order.line_items.create({ variant: variant, quantity: 1, price: 100 })
order.update!({customer_id: customer1.id})
order.instance_variable_set(:@fee_handler, nil)
order.recreate_all_fees!

while !order.completed?
break unless order.next!
end

order2.line_items.create({ variant: variant, quantity: 1, price: 200 })
order2.update!({
order_cycle_id: order_cycle.id,
ship_address_id: customer2.bill_address_id,
customer_id: customer2.id,
number: 'ORDER_NUMBER_2'
})
order2.instance_variable_set(:@fee_handler, nil)
order2.recreate_all_fees!

while !order2.completed?
break unless order2.next!
end
login_as admin
visit admin_reports_path
click_on I18n.t("admin.reports.sales_tax_totals_by_order")
end

it "should load all the orders" do
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(customer1_country_tax_rate_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer1_state_tax_rate_row)
expect(page.find("table.report__table tbody").text).to have_content(customer1_summary_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer2_country_tax_rate_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer2_state_tax_rate_row)
expect(page.find("table.report__table tbody").text).to have_content(customer2_summary_row)
expect(page).to have_selector(table_raw_selector,count: 6)
end

it "should filter customer1 orders" do
page.find(customer_email_dropdown_selector).click
find('li', text: customer1.email).click

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(customer1_country_tax_rate_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer1_state_tax_rate_row)
expect(page.find("table.report__table tbody").text).to have_content(customer1_summary_row)
expect(page).to have_selector(table_raw_selector,count: 3)
end

it "should filter customer2 orders" do
page.find(customer_email_dropdown_selector).click
find('li', text: customer2.email).click

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(customer2_country_tax_rate_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer2_state_tax_rate_row)
expect(page.find("table.report__table tbody").text).to have_content(customer2_summary_row)
expect(page).to have_selector(table_raw_selector,count: 3)
end

it "should filter customer1 and customer2 orders" do
page.find(customer_email_dropdown_selector).click
find('li', text: customer1.email).click
page.find(customer_email_dropdown_selector).click
find('li', text: customer2.email).click
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(customer1_country_tax_rate_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer1_state_tax_rate_row)
expect(page.find("table.report__table tbody").text).to have_content(customer1_summary_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer2_country_tax_rate_row)
expect(
page.find("table.report__table tbody").text
).to have_content(customer2_state_tax_rate_row)
expect(page.find("table.report__table tbody").text).to have_content(customer2_summary_row)
expect(page).to have_selector(table_raw_selector,count: 6)
end
end
end

0 comments on commit b2cc5a9

Please sign in to comment.