craftcms/commerce-sagepay

Tax not being applied to individual items but applied as line item

Opened this issue · 2 comments

Description

I have a VAT tax rule setup. The rule is set up to be applied to line items (not the cart total).

When the order details are submitted to SagePay, each item has a blank tax column and the VAT is added as a separate line item:

image

This isn't ideal as not all products will have tax applied to them and it also doesn't make sense from an accounting perspective.

Steps to reproduce

  1. Make an order with tax applied to items
  2. Items don't have tax applied to them in SagePage

Additional info

  • Craft version: 3.7.13
  • PHP version: 7.4.12
  • Plugins & versions: Commerce 3.4.3, SagePay 2.1.1

I feel like this should be an option/default behaviour when you have tax rules set to apply to individual items.

Essentially, I've had to hack around doing the following to get the data to go into SagePay:

Using the EVENT_AFTER_CREATE_ITEM_BAG event I'm having to completely rebuild the ItemBag by populating it with new Omnipay Items, specifically using the extended version from Omnipay\SagePay\Extend\Item so I can add VAT.

Essentially I'm doing the following:

  1. Looping over all the adjustments to find the shipping tax and storing it
  2. Looping over all the adjustments again to build up a new array with new Items. For the shipping adjustment, I am using setVat e.g. $newItem->setVat($shippingTax);. I'm also at this point discarding any tax adjustments
  3. Looping over all the order LineItems, storing their tax value from their adjustments and then creating a new Item and adding it to an array
  4. Merge the two arrays containing the LineItems and adjustments
  5. Using items->replace() to replace the ItemBag

This gives me the desired output:

image

Now, this is working. At the moment it's very much just some hacky code to test the theory so I won't post anything just yet. But I am wondering if there's a more sensible/robust way to achieve this?

For clarity, I should add I am using the legacy basket format (via the plugin settings) but I believe this happens later on and shouldn't affect this.