Add metadata to tax related voucher adjustment

In the scenario where you have tax excluded from price, when adding
a voucher to an order, we create 2 voucher adjusments. One of them
represent the tax part of the voucher, and has a label starting with
"Tax". To better differentiate them and allow a reliable way to
query it, we add a metadata entry.
This commit is contained in:
Gaetan Craig-Riou
2023-09-22 11:16:21 +02:00
parent 0a68300e40
commit 03ce39d5c5
3 changed files with 28 additions and 7 deletions

View File

@@ -70,6 +70,9 @@ module Spree
scope :credit, -> { where('amount < 0') }
scope :return_authorization, -> { where(originator_type: "Spree::ReturnAuthorization") }
scope :voucher, -> { where(originator_type: "Voucher") }
scope :voucher_tax, -> {
voucher.joins(:metadata).where(metadata: { fee_name: "Tax", fee_type: "Voucher" })
}
scope :non_voucher, -> { where.not(originator_type: "Voucher") }
scope :inclusive, -> { where(included: true) }
scope :additional, -> { where(included: false) }

View File

@@ -44,11 +44,9 @@ class VoucherAdjustmentsService
end
def voucher_excluded_tax
return 0.0 if @order.voucher_adjustments.empty?
return 0.0 if @order.voucher_adjustments.voucher_tax.empty?
return 0.0 if @order.voucher_adjustments.where("label LIKE 'Tax%'").empty?
@order.voucher_adjustments.where("label LIKE 'Tax%'").first.amount
@order.voucher_adjustments.voucher_tax.first.amount
end
private
@@ -86,6 +84,14 @@ class VoucherAdjustmentsService
tax_adjustment = @order.adjustments.find_or_initialize_by(adjustment_attributes)
tax_adjustment.amount = tax_amount
tax_adjustment.save
# Add metada so we know which voucher adjustment is Tax related
AdjustmentMetadata.create(
adjustment: tax_adjustment,
enterprise: adjustment.originator.enterprise,
fee_name: "Tax",
fee_type: "Voucher"
)
end
def handle_tax_included_in_price(amount, voucher)

View File

@@ -136,6 +136,12 @@ describe VoucherAdjustmentsService do
# -0.058479532 * 11 = -0.64
expect(tax_adjustment.amount.to_f).to eq(-0.64)
expect(tax_adjustment.label).to match("Tax")
expect(tax_adjustment.metadata.enterprise_id).to eq(
tax_adjustment.originator.enterprise.id
)
expect(tax_adjustment.metadata.fee_name).to eq("Tax")
expect(tax_adjustment.metadata.fee_type).to eq("Voucher")
end
context "when re calculating" do
@@ -330,9 +336,9 @@ describe VoucherAdjustmentsService do
it "returns the amount from the tax voucher adjustment" do
voucher_adjustment = voucher.create_adjustment(voucher.code, order)
# Manually add voucher tax adjustment, so we don't have to do a big data setup to be able to
# use VoucherAdjustmentsService.update
order.adjustments.create!(
# Manually add voucher tax adjustment and metadata, so we don't have to do a big data setup
# to be able to use VoucherAdjustmentsService.update
tax_adjustment = order.adjustments.create!(
originator: voucher_adjustment.originator,
order: order,
label: "Tax #{voucher_adjustment.label}",
@@ -342,6 +348,12 @@ describe VoucherAdjustmentsService do
included_tax: 0,
amount: 0.5
)
AdjustmentMetadata.create(
adjustment: tax_adjustment,
enterprise: tax_adjustment.originator.enterprise,
fee_name: "Tax",
fee_type: "Voucher"
)
expect(voucher_excluded_tax).to eq(0.5)
end