taxjar / taxjar-magento2-extension

Magento 2 Sales Tax Extension by TaxJar
http://www.taxjar.com/guides/integrations/magento2/
Open Software License 3.0
22 stars 30 forks source link

Feature Request: Use Invoice date (not Order Placement) #294

Closed lbajsarowicz closed 2 years ago

lbajsarowicz commented 2 years ago

Prerequisites

Description

One of my Clients has quite a specific Order flow, where Invoices are issued when the Shipment is made (originally, Magento issues the Invoice when the payment is made).

I would like to use the Invoice date as the Transaction Date for the TaxJar reporting.

Unfortunately, changes to the \Taxjar\SalesTax\Model\Transaction\Order::build (actually, after plugin) replacing ['transaction_date'] does not provide expected result and TaxJar still reports the Order Placement date as Transaction Date.

Could you provide more insight (or guidelines for a valid solution, as we are willing to contribute if that's an interesting feature for your extension)?

Steps to Reproduce

  1. Place Order on a Day {n}
  2. Issue an Invoice on a Day {n+1}

Expected Result

  1. TaxJar reports Day {n+1} as Transaction Date

Actual Result

  1. TaxJar reports Day {n} as Transaction Date

Versions

sethobey commented 2 years ago

Hi @lbajsarowicz - It appears your issue with any implementation of a plugin/interceptor for that build method is due the fact that we are setting $this->request value in the Order::build method, and not actually using the returned value, so any modification to the return value in an "after" plugin implementation would be ignored.

I was able to work up a solution to this issue using an after plugin by also adding a public setter method Order::setRequest to the \Taxjar\SalesTax\Model\Transaction\Order::class which is then used in the after method. I will open a PR to add the setter-method to our Transaction classes in our next release so that you may implement your after-plugin for your client.

Here you can see that I had already synced this transaction, so the Date shown in the table is the original date that TaxJar received the transaction (on 2022-03-29):

image

But upon viewing the transaction detail, the invoice date (2022-04-01) is shown as order date:

image

Note: The reporting date will not change retroactively as shown in my example above (because I just modified an existing test order's invoice). If you wish to update any records that have already been sent to TaxJar for the current filing period, you'd have to manually delete the corresponding transactions in TaxJar via API call (maybe with Postman or cURL) before attempting to sync these transactions again to update the reporting period date.

While I'm sure you're aware, I would also caution that since one order can have potentially many invoices, take care as to which invoice date is being persisted to the TaxJar transaction!

Once the setter method is implemented, this is the sample class that I used to validate my solution.

class AfterBuildOrder
{
    /**
     * Use an order's first invoice created_at date instead of order created_at
     *
     * @param \Taxjar\SalesTax\Model\Transaction\Order $subject
     * @param array $request
     * @param \Magento\Sales\Api\Data\OrderInterface|\Magento\Sales\Model\Order $order
     * @throws \Exception
     */
    public function afterBuild(\Taxjar\SalesTax\Model\Transaction\Order $subject, array &$request, $order)
    {
        /** @var \Magento\Sales\Model\Order\Invoice $someInvoice */
        $firstInvoice = $order->getInvoiceCollection()->getFirstItem();
        $request['transaction_date'] = new \DateTime($firstInvoice->getCreatedAt());
        $subject->setRequest($request); // Method doesn't exist (yet)
        return $request;
    }
}
sethobey commented 2 years ago

I went ahead and validated my recommended solution for the use case of deleting the order via Postman API request and re-syncing from Magento 2 admin Sales Order view and you can see that the reporting month is now modified in TaxJar as well as the Order date.

image image