Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Clear stale duty tax lines and address review feedback
Damian Legawiec694470c597de
diff --git a/docs/plans/decisions.md b/docs/plans/decisions.mdindex 06aecf7be7..4b8a2f39be 100644--- a/docs/plans/decisions.md+++ b/docs/plans/decisions.md@@ -2806,16 +2806,20 @@ guaranteed flag) in `metadata`, so re-classifying the catalog never rewritesorder history. Classification lives as three plain nullable columns on`spree_variants` (`hs_code`, `country_of_origin`, `customs_description`),shipped together with their first consumer: the EasyPost provider's customs-declaration on international label purchase (today it sends `customs_info:-null`). Fees become shopper-visible — the Store API cart/order serializers+declaration, built for international rate quotes as well as label purchases+(today it sends `customs_info: null`) — the two share one shipment builder, so+a label always carries the declaration and duty terms its quote was priced+with. Fees become shopper-visible — the Store API cart/order serializersgain `fees` + `fee_total`. Duty estimation enters the cart exclusively through`Spree.adjusters` (a provider gem's adjuster writes the duty fee rows duringrecalculation); core ships no calculation engine and no landed-cost guarantee— a guarantee requires being importer of record, which is a service business,-not a code path. The internal tax provider excludes `kind: 'duty'` from its-default fee-taxing set: import VAT on a duty exists only as provider-written-`TaxLine(fee_id: …)` rows, preventing domestic tax stacking and-double-counting. Plumbing ships in 6.0; the landed-cost provider gem and a+not a code path. Duties are excluded from the default taxable-item set in the+shared `Spree::Purchase::Taxation#taxable_items` (Cart and Order), so **every**+provider inherits the exclusion rather than each reimplementing it; a provider+that does tax duties passes its own item list. Import VAT on a duty therefore+exists only as provider-written `TaxLine(fee_id: …)` rows, preventing domestic+tax stacking and double-counting. Plumbing ships in 6.0; the landed-cost provider gem and a`Spree::Market` shipping-terms setting are 6.1.**Consequences:** No duty math against the live catalog on existing orders —diff --git a/docs/v6/developer/core-concepts/taxes-discounts-fees.mdx b/docs/v6/developer/core-concepts/taxes-discounts-fees.mdxindex 07449f7beb..8572d8bfc7 100644--- a/docs/v6/developer/core-concepts/taxes-discounts-fees.mdx+++ b/docs/v6/developer/core-concepts/taxes-discounts-fees.mdx@@ -163,7 +163,7 @@ await adminClient.orders.fees.create('or_xxx', {})```-Fees are always positive, and they're taxable — a fee can have its own tax lines. To take money off, create a discount instead. That way a refund never has to work out whether a negative fee was a charge or a credit.+Fees are always positive, and they're taxable by default — a fee can have its own tax lines. Customs duties are the one exception, described below. To take money off, create a discount instead. That way a refund never has to work out whether a negative fee was a charge or a credit.### Customs dutiesdiff --git a/packages/admin-sdk/src/types/generated/Order.ts b/packages/admin-sdk/src/types/generated/Order.tsindex e768267376..f5a679de56 100644--- a/packages/admin-sdk/src/types/generated/Order.ts+++ b/packages/admin-sdk/src/types/generated/Order.ts@@ -1,4 +1,4 @@-// Typelizer digest 62501e8b2114b0fadbaab2436ccca0d4+// Typelizer digest a858835c532b24ee10b21ddfb4d9ae8f//// DO NOT MODIFY: This file was automatically generated by Typelizer.import type {LineItem, Fulfillment, Payment, Address, GiftCard, Market, AppliedPromotion, Cart, Channel, StockLocation, PaymentMethod, Customer, Return, Exchange, Claim} from '@/types'@@ -38,8 +38,8 @@ interface Order {display_amount_due: string;delivery_total: string;display_delivery_total: string;- fee_total: string | null;- display_fee_total: string | null;+ fee_total: string;+ display_fee_total: string;store_credit_total: string;display_store_credit_total: string;covered_by_store_credit: boolean;diff --git a/packages/dashboard/src/components/spree/products/bulk-variants-dialog.tsx b/packages/dashboard/src/components/spree/products/bulk-variants-dialog.tsxindex e3efe98bcf..20494f08eb 100644--- a/packages/dashboard/src/components/spree/products/bulk-variants-dialog.tsx+++ b/packages/dashboard/src/components/spree/products/bulk-variants-dialog.tsx@@ -233,9 +233,9 @@ export function BulkVariantsDialog({ form, open, onOpenChange }: Props) {countryOfOrigin: t('admin.fields.variant.country_of_origin.label'),customsDescription: t('admin.fields.variant.customs_description.label'),variantDefault: t('admin.products.variants.default_variant'),- unitDefault: '—',+ unitDefault: t('admin.products.variants.bulk_edit.unset'),taxCategoryNone: t('admin.products.variants.sheet.tax_category_placeholder'),- countryOfOriginNone: '—',+ countryOfOriginNone: t('admin.products.variants.bulk_edit.unset'),gridAriaLabel: t('admin.products.variants.bulk_edit.grid_aria'),}}/>diff --git a/packages/dashboard/src/components/spree/products/variant-edit-sheet.tsx b/packages/dashboard/src/components/spree/products/variant-edit-sheet.tsxindex 99f1cb9cfc..0413ded670 100644--- a/packages/dashboard/src/components/spree/products/variant-edit-sheet.tsx+++ b/packages/dashboard/src/components/spree/products/variant-edit-sheet.tsx@@ -303,6 +303,9 @@ export function VariantEditSheet({ form, variantIndex, open, onOpenChange }: Pro/>)}/>+ <FieldError+ errors={[form.formState.errors.variants?.[variantIndex]?.country_of_origin]}+ /></Field></div><Field>@@ -316,6 +319,9 @@ export function VariantEditSheet({ form, variantIndex, open, onOpenChange }: ProsetValueAs: normalizeCustomsDescription,})}/>+ <FieldError+ errors={[form.formState.errors.variants?.[variantIndex]?.customs_description]}+ /></Field></Section>diff --git a/packages/dashboard/src/locales/ar.json b/packages/dashboard/src/locales/ar.jsonindex 26b01e9d3c..609296814f 100644--- a/packages/dashboard/src/locales/ar.json+++ b/packages/dashboard/src/locales/ar.json@@ -3311,7 +3311,8 @@"bulk_edit": {"grid_aria": "جدول التحرير الجماعي للمتغيرات","title": "تحرير المتغيرات جماعيًا",- "trigger": "تحرير جماعي"+ "trigger": "تحرير جماعي",+ "unset": "لا شيء"},"create_option_type": "إنشاء نوع خيار جديد","create_option_type_title": "نوع خيار جديد",diff --git a/packages/dashboard/src/locales/de.json b/packages/dashboard/src/locales/de.jsonindex c5787978e7..7151883f4b 100644--- a/packages/dashboard/src/locales/de.json+++ b/packages/dashboard/src/locales/de.json@@ -3295,7 +3295,8 @@"bulk_edit": {"grid_aria": "Tabelle zur Massenbearbeitung der Varianten","title": "Varianten massenbearbeiten",- "trigger": "Massenbearbeitung"+ "trigger": "Massenbearbeitung",+ "unset": "Keine"},"create_option_type": "Neuen Optionstyp erstellen","create_option_type_title": "Neuer Optionstyp",diff --git a/packages/dashboard/src/locales/en.json b/packages/dashboard/src/locales/en.jsonindex e3dcd2450f..530cf36656 100644--- a/packages/dashboard/src/locales/en.json+++ b/packages/dashboard/src/locales/en.json@@ -3295,7 +3295,8 @@"bulk_edit": {"grid_aria": "Variant bulk edit spreadsheet","title": "Bulk edit variants",- "trigger": "Bulk edit"+ "trigger": "Bulk edit",+ "unset": "None"},"create_option_type": "Create new option type","create_option_type_title": "New option type",diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.jsonindex a1c5f5ac23..91d03112ec 100644--- a/packages/dashboard/src/locales/fr.json+++ b/packages/dashboard/src/locales/fr.json@@ -3295,7 +3295,8 @@"bulk_edit": {"grid_aria": "Tableau d'édition groupée des variantes","title": "Modifier les variantes en masse",- "trigger": "Édition groupée"+ "trigger": "Édition groupée",+ "unset": "Aucun"},"create_option_type": "Créer un nouveau type d'option","create_option_type_title": "Nouveau type d'option",diff --git a/packages/dashboard/src/locales/pl.json b/packages/dashboard/src/locales/pl.jsonindex 61c3e0e43b..e8b7fcbb02 100644--- a/packages/dashboard/src/locales/pl.json+++ b/packages/dashboard/src/locales/pl.json@@ -3347,7 +3347,8 @@"bulk_edit": {"grid_aria": "Arkusz zbiorczej edycji wariantów","title": "Edytuj warianty zbiorczo",- "trigger": "Edycja zbiorcza"+ "trigger": "Edycja zbiorcza",+ "unset": "Brak"},"create_option_type": "Utwórz nowy typ opcji","create_option_type_title": "Nowy typ opcji",diff --git a/packages/dashboard/src/locales/zh-CN.json b/packages/dashboard/src/locales/zh-CN.jsonindex 24c3d54ba1..e33d70f197 100644--- a/packages/dashboard/src/locales/zh-CN.json+++ b/packages/dashboard/src/locales/zh-CN.json@@ -3291,7 +3291,8 @@"bulk_edit": {"grid_aria": "变体批量编辑表格","title": "批量编辑变体",- "trigger": "批量编辑"+ "trigger": "批量编辑",+ "unset": "无"},"create_option_type": "新建选项类型","create_option_type_title": "新选项类型",diff --git a/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rbindex 976d451111..ab20951f3d 100644--- a/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb+++ b/spree/api/app/serializers/spree/api/v3/admin/order_serializer.rb@@ -20,6 +20,7 @@ module Spreeadditional_tax_total: [:string, nullable: false], display_additional_tax_total: [:string, nullable: false],store_credit_total: [:string, nullable: false], display_store_credit_total: [:string, nullable: false],gift_card_total: [:string, nullable: false], display_gift_card_total: [:string, nullable: false],+ fee_total: [:string, nullable: false], display_fee_total: [:string, nullable: false],total: [:string, nullable: false], display_total: [:string, nullable: false],amount_due: [:string, nullable: false], display_amount_due: [:string, nullable: false]diff --git a/spree/core/app/models/spree/tax_provider/internal.rb b/spree/core/app/models/spree/tax_provider/internal.rbindex a875298ac0..5c2d7ee4f9 100644--- a/spree/core/app/models/spree/tax_provider/internal.rb+++ b/spree/core/app/models/spree/tax_provider/internal.rb@@ -21,7 +21,13 @@ module Spree# +tax_identifier+ likewise — reverse charge is on the unsupported list,# so a buyer registration changes nothing here.def estimate(owner, items = nil, tax_date: nil, tax_identifier: nil, exemptions: [], context: {})+ full_estimate = items.nil?items ||= owner.taxable_items+ # A duty this engine taxed before it was excluded — or through an+ # explicit item list — is never revisited by the per-item cleanup+ # below, since duties are absent from the default set. Left alone the+ # row would inflate the total for the life of the order.+ sweep_internal_duty_tax_lines(owner) if full_estimatereturn if items.empty?rates = Spree::TaxRate.for_store(owner.store).for_jurisdiction(owner.tax_country&.iso, owner.tax_address&.state_code).to_a@@ -63,6 +69,16 @@ module Spreeprivate+ # Scoped to this engine's own rows: a landed-cost provider writes the+ # import VAT that genuinely belongs on a duty, and clearing that would+ # be deleting another provider's work.+ def sweep_internal_duty_tax_lines(owner)+ duty_fee_ids = owner.fees.where(kind: 'duty').ids+ return if duty_fee_ids.empty?++ owner.tax_lines.where(fee_id: duty_fee_ids, provider_id: 'internal').delete_all+ end+def tax_line_foreign_key(klass)case klass.namewhen 'Spree::LineItem' then :line_item_iddiff --git a/spree/core/spec/models/spree/tax_provider/internal_spec.rb b/spree/core/spec/models/spree/tax_provider/internal_spec.rbindex 487fbdb0e4..d4a5d414ef 100644--- a/spree/core/spec/models/spree/tax_provider/internal_spec.rb+++ b/spree/core/spec/models/spree/tax_provider/internal_spec.rb@@ -299,6 +299,26 @@ describe Spree::TaxProvider::Internal, type: :model doexpect(order.tax_lines.reload.sole.fee).to eq(duty)end++ # Duties are absent from the default set, so the per-item cleanup never+ # revisits them — without an explicit sweep a row written once would+ # inflate the total for the life of the order.+ it 'clears a duty tax line it wrote earlier once the duty is excluded again' do+ provider.estimate(order, [duty])+ expect(order.tax_lines.reload.where(fee_id: duty.id)).to be_present++ provider.estimate(order)++ expect(order.tax_lines.reload.where(fee_id: duty.id)).to be_empty+ end++ it 'leaves import VAT another provider wrote on the duty alone' do+ create(:tax_line, order: order, fee: duty, line_item: nil, provider_id: 'landed_cost', amount: 4)++ provider.estimate(order)++ expect(order.tax_lines.reload.where(fee_id: duty.id).pluck(:provider_id)).to eq(['landed_cost'])+ endendend