Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in calculation of tax_total on cart versus order #7456

Closed
stephanboersma opened this issue May 25, 2024 · 2 comments
Closed

Inconsistency in calculation of tax_total on cart versus order #7456

stephanboersma opened this issue May 25, 2024 · 2 comments

Comments

@stephanboersma
Copy link

Bug report

Describe the bug

The tax total on an order object is incorrect when gift cards are applied. The system provider for tax calculation calculates the tax from the amount after the gift card is subtracted and not before.

It looks like that I have to manually summarize tax_total and gift_card_tax_total but in the cart the tax_total includes the actual total tax.

Maybe related to #5005

Another question, will it be possible to allow discount codes to apply to shipping as well in 2.0?

Looking massively forward to check out the release candidate when it is ready. Awesome work on this project - kudos!

System information

Medusa version (including plugins): Latest
Node.js version: 18.19
Database: latest
Operating system: macOS
Browser (if relevant):

Steps to reproduce the behavior

  1. Create a cart with products in region with 25 % VAT with a total of 338 DKK
  2. apply gift card that subtracts 25DKK after VAT
  3. Complete cart
  4. Observe that tax_total is 6260 and not 6760

Expected behavior

Expect tax_total to equal item_tax_total + shipping_tax_total + gift_card_tax_total on the order object

Screenshots

In cart:
image

In order confirmation:
image

Code snippets

Cart Object

Cart {
  object: 'cart',
  id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
  created_at: '2024-05-25T16:43:06.097Z',
  updated_at: '2024-05-25T16:45:41.508Z',
  deleted_at: null,
  email: '[email protected]',
  billing_address_id: 'addr_01HYRA9MGAKRRGG2QCX7JB3TVB',
  shipping_address_id: 'addr_01HYRA4VHR8C1ZS8FHM7HVKDCP',
  region_id: 'reg_01HN8V2AXVQY1C8TE2VM5QYCXQ',
  customer_id: 'cus_01HYRA9KY9X4ZH3TEM4T5RJ9FM',
  payment_id: null,
  type: 'default',
  completed_at: null,
  payment_authorized_at: null,
  idempotency_key: null,
  context: { ip: '::1', user_agent: 'axios/0.24.0' },
  metadata: null,
  sales_channel_id: 'sc_01HN8TKNG9A8K6KHMBN56F2YPG',
  billing_address: {
    id: 'addr_01HYRA9MGAKRRGG2QCX7JB3TVB',
    created_at: '2024-05-25T16:45:41.508Z',
    updated_at: '2024-05-25T16:45:41.508Z',
    deleted_at: null,
    customer_id: null,
    company: '',
    first_name: 'Mary',
    last_name: 'Poppins',
    address_1: 'London Road',
    address_2: '',
    city: 'København',
    country_code: 'dk',
    province: null,
    postal_code: '1000',
    phone: '12345678',
    metadata: null
  },
  discounts: [
    {
      id: 'disc_01HX192EDDMFWQYHPYY80DXGDB',
      created_at: '2024-05-04T07:46:04.819Z',
      updated_at: '2024-05-25T16:38:45.953Z',
      deleted_at: null,
      code: 'RABAT10',
      is_dynamic: false,
      rule_id: 'dru_01HX192ED14HGYWSANFHPEH942',
      is_disabled: false,
      parent_discount_id: null,
      starts_at: '2024-05-04T07:46:04.819Z',
      ends_at: null,
      valid_duration: null,
      usage_limit: null,
      usage_count: 7,
      metadata: {},
      rule: [Object]
    }
  ],
  gift_cards: [
    {
      id: 'gift_01HYRA4K4QWG54ABV85X7J7WS4',
      created_at: '2024-05-25T16:42:57.541Z',
      updated_at: '2024-05-25T16:42:57.541Z',
      deleted_at: null,
      code: 'X2IX-H8EX-8C2D-1036',
      value: 2000,
      balance: 2000,
      region_id: 'reg_01HN8V2AXVQY1C8TE2VM5QYCXQ',
      order_id: null,
      is_disabled: false,
      ends_at: null,
      tax_rate: 25,
      metadata: [Object]
    }
  ],
  items: [
    {
      id: 'item_01HYRA4WMY3QF94HM4SS9620E4',
      created_at: '2024-05-25T16:43:07.073Z',
      updated_at: '2024-05-25T17:03:07.551Z',
      cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
      order_id: null,
      swap_id: null,
      claim_order_id: null,
      original_item_id: null,
      order_edit_id: null,
      title: 'Venom - Black Metal, Ltd Colored LP',
      description: 'Venom - Black Metal, Ltd Colored LP',
      thumbnail: 'https://metalvinyl.ams3.cdn.digitaloceanspaces.com/Venom-Black-Metal-silver-black-swirl-1706474965882.jpg',
      is_return: false,
      is_giftcard: false,
      should_merge: true,
      allow_discounts: true,
      has_shipping: true,
      unit_price: 12800,
      variant_id: 'variant_01HN8XB37TSRF2ZVGQZH5Q9ESV',
      quantity: 2,
      fulfilled_quantity: null,
      returned_quantity: null,
      shipped_quantity: null,
      metadata: {},
      adjustments: [Array],
      tax_lines: [Array],
      variant: [Object],
      subtotal: 25600,
      discount_total: 2560,
      total: 28800,
      original_total: 32000,
      original_tax_total: 6400,
      tax_total: 5760,
      raw_discount_total: 2560
    }
  ],
  payment: null,
  payment_sessions: [
    {
      id: 'ps_01HYRA7XWBDFTZQE9375XW5452',
      created_at: '2024-05-25T16:44:46.599Z',
      updated_at: '2024-05-25T17:14:18.334Z',
      cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
      provider_id: 'reepay',
      is_selected: true,
      is_initiated: true,
      status: 'pending',
      data: [Object],
      idempotency_key: null,
      amount: 31300,
      payment_authorized_at: null
    }
  ],
  payment_session: {
    id: 'ps_01HYRA7XWBDFTZQE9375XW5452',
    created_at: '2024-05-25T16:44:46.599Z',
    updated_at: '2024-05-25T17:14:18.334Z',
    cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
    provider_id: 'reepay',
    is_selected: true,
    is_initiated: true,
    status: 'pending',
    data: {
      id: '***',
      action: [Object],
      handle: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG'
    },
    idempotency_key: null,
    amount: 31300,
    payment_authorized_at: null
  },
  region: {
    id: 'reg_01HN8V2AXVQY1C8TE2VM5QYCXQ',
    created_at: '2024-01-28T20:09:42.048Z',
    updated_at: '2024-04-20T10:02:55.573Z',
    deleted_at: null,
    name: 'Denmark',
    currency_code: 'dkk',
    tax_rate: 25,
    tax_code: null,
    gift_cards_taxable: true,
    automatic_taxes: true,
    tax_provider_id: null,
    metadata: {},
    countries: [ [Object] ],
    payment_providers: [ [Object] ],
    tax_rates: []
  },
  sales_channel: {
    id: 'sc_01HN8TKNG9A8K6KHMBN56F2YPG',
    created_at: '2024-01-28T20:01:41.302Z',
    updated_at: '2024-01-28T20:10:06.764Z',
    deleted_at: null,
    name: 'Metalvinyl.dk Webshop',
    description: 'Standard Channel',
    is_disabled: false,
    metadata: null
  },
  shipping_address: {
    id: 'addr_01HYRA4VHR8C1ZS8FHM7HVKDCP',
    created_at: '2024-05-25T16:43:06.097Z',
    updated_at: '2024-05-25T16:45:41.508Z',
    deleted_at: null,
    customer_id: null,
    company: '',
    first_name: 'Mary',
    last_name: 'Poppins',
    address_1: 'London Road',
    address_2: '',
    city: 'København',
    country_code: 'dk',
    province: null,
    postal_code: '1000',
    phone: '12345678',
    metadata: null
  },
  shipping_methods: [
    {
      id: 'sm_01HYRB9HADRRYPEBH1EVAFWKCC',
      shipping_option_id: 'so_01HN8XDB6SNHN28RYPVZHR1AGS',
      order_id: null,
      claim_order_id: null,
      cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
      swap_id: null,
      return_id: null,
      price: 4000,
      data: {},
      shipping_option: [Object],
      tax_lines: [Array],
      original_total: 5000,
      total: 5000,
      subtotal: 4000,
      original_tax_total: 1000,
      tax_total: 1000
    }
  ],
  subtotal: 25600,
  discount_total: 2560,
  item_tax_total: 5760,
  shipping_total: 4000,
  shipping_tax_total: 1000,
  tax_total: 6760,
  gift_card_total: 2000,
  gift_card_tax_total: 500,
  total: 31300
}

Order Object

Cart {
  object: 'order',
  id: 'order_01HYRC8XVC2ET5AN9P7PNW1VHY',
  created_at: '2024-05-25T17:20:15.165Z',
  status: 'pending',
  fulfillment_status: 'not_fulfilled',
  payment_status: 'awaiting',
  display_id: 47,
  cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
  customer_id: 'cus_01HYRA9KY9X4ZH3TEM4T5RJ9FM',
  email: '[email protected]',
  region_id: 'reg_01HN8V2AXVQY1C8TE2VM5QYCXQ',
  currency_code: 'dkk',
  tax_rate: null,
  customer: {
    id: 'cus_01HYRA9KY9X4ZH3TEM4T5RJ9FM',
    created_at: '2024-05-25T16:45:41.508Z',
    updated_at: '2024-05-25T16:45:41.508Z',
    deleted_at: null,
    email: '[email protected]',
    first_name: null,
    last_name: null,
    billing_address_id: null,
    phone: null,
    has_account: false,
    metadata: null
  },
  discounts: [
    {
      id: 'disc_01HX192EDDMFWQYHPYY80DXGDB',
      created_at: '2024-05-04T07:46:04.819Z',
      updated_at: '2024-05-25T17:54:09.460Z',
      deleted_at: null,
      code: 'RABAT10',
      is_dynamic: false,
      rule_id: 'dru_01HX192ED14HGYWSANFHPEH942',
      is_disabled: false,
      parent_discount_id: null,
      starts_at: '2024-05-04T07:46:04.819Z',
      ends_at: null,
      valid_duration: null,
      usage_limit: null,
      usage_count: 10,
      metadata: {},
      rule: [Object]
    }
  ],
  fulfillments: [],
  items: [
    {
      id: 'item_01HYRA4WMY3QF94HM4SS9620E4',
      created_at: '2024-05-25T16:43:07.073Z',
      updated_at: '2024-05-25T17:20:20.753Z',
      cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
      order_id: 'order_01HYRC8XVC2ET5AN9P7PNW1VHY',
      swap_id: null,
      claim_order_id: null,
      original_item_id: null,
      order_edit_id: null,
      title: 'Venom - Black Metal, Ltd Colored LP',
      description: 'Venom - Black Metal, Ltd Colored LP',
      thumbnail: 'https://metalvinyl.ams3.cdn.digitaloceanspaces.com/Venom-Black-Metal-silver-black-swirl-1706474965882.jpg',
      is_return: false,
      is_giftcard: false,
      should_merge: true,
      allow_discounts: true,
      has_shipping: true,
      unit_price: 12800,
      variant_id: 'variant_01HN8XB37TSRF2ZVGQZH5Q9ESV',
      quantity: 2,
      fulfilled_quantity: null,
      returned_quantity: null,
      shipped_quantity: null,
      metadata: [Object],
      adjustments: [Array],
      tax_lines: [Array],
      variant: [Object],
      subtotal: 25600,
      discount_total: 2560,
      total: 28800,
      original_total: 32000,
      original_tax_total: 6400,
      tax_total: 5760,
      raw_discount_total: 2560,
      refundable: 28800
    }
  ],
  payments: [
    {
      id: 'pay_01HYRC8VTHVE40A6DSK5CMSYXS',
      created_at: '2024-05-25T17:20:13.147Z',
      updated_at: '2024-05-25T17:20:15.165Z',
      swap_id: null,
      cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
      order_id: 'order_01HYRC8XVC2ET5AN9P7PNW1VHY',
      amount: 31300,
      currency_code: 'dkk',
      amount_refunded: 0,
      provider_id: 'reepay',
      data: [Object],
      captured_at: null,
      canceled_at: null,
      metadata: null,
      idempotency_key: null
    }
  ],
  region: {
    id: 'reg_01HN8V2AXVQY1C8TE2VM5QYCXQ',
    created_at: '2024-01-28T20:09:42.048Z',
    updated_at: '2024-04-20T10:02:55.573Z',
    deleted_at: null,
    name: 'Denmark',
    currency_code: 'dkk',
    tax_rate: 25,
    tax_code: null,
    gift_cards_taxable: true,
    automatic_taxes: true,
    tax_provider_id: null,
    metadata: {}
  },
  shipping_address: {
    id: 'addr_01HYRA4VHR8C1ZS8FHM7HVKDCP',
    created_at: '2024-05-25T16:43:06.097Z',
    updated_at: '2024-05-25T16:45:41.508Z',
    deleted_at: null,
    customer_id: null,
    company: '',
    first_name: 'Mary',
    last_name: 'Poppins',
    address_1: 'London Road',
    address_2: '',
    city: 'København',
    country_code: 'dk',
    province: null,
    postal_code: '1000',
    phone: '12345678',
    metadata: null
  },
  shipping_methods: [
    {
      id: 'sm_01HYRB9HADRRYPEBH1EVAFWKCC',
      shipping_option_id: 'so_01HN8XDB6SNHN28RYPVZHR1AGS',
      order_id: 'order_01HYRC8XVC2ET5AN9P7PNW1VHY',
      claim_order_id: null,
      cart_id: 'cart_01HYRA4VHRA7KRHE8ZJ69R19DG',
      swap_id: null,
      return_id: null,
      price: 4000,
      data: {},
      shipping_option: [Object],
      tax_lines: [Array],
      original_total: 5000,
      total: 5000,
      subtotal: 4000,
      original_tax_total: 1000,
      tax_total: 1000
    }
  ],
  subtotal: 25600,
  discount_total: 2560,
  shipping_total: 4000,
  refunded_total: 0,
  paid_total: 31300,
  refundable_amount: 31300,
  item_tax_total: 5760,
  shipping_tax_total: 1000,
  tax_total: 6260,
  gift_card_total: 2000,
  gift_card_tax_total: 500,
  total: 31300
}

Additional context

Add any other context about the problem here

@stephanboersma
Copy link
Author

Query used to get the order:


await this.orderService_.retrieve(id, {
            select: [
                "shipping_total",
                "discount_total",
                "tax_total",
                "refunded_total",
                "gift_card_total",
                "subtotal",
                "total",
            ],
            relations: [
                "customer",
                "billing_address",
                "shipping_address",
                "discounts",
                "discounts.rule",
                "shipping_methods",
                "shipping_methods.shipping_option",
                "payments",
                "fulfillments",
                "returns",
                "gift_cards",
                "gift_card_transactions",
            ],
        })
```

@stephanboersma
Copy link
Author

I realize this is correct behavior given a "single purpose" gift card. At least according to danish rules.

The VAT of a single purpose gift card is due at time of selling a gift card and when the gift card is redeemed, the VAT is already paid. That is why the total tax of a completed order is lower when a gift card is applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant