Add/Apply charges on Invoice journal when posting Free text invoice and/or Sales invoice

Requirement

By law, stamp duty(charge) in some countries must be applied to all invoices above some threshold (in absolute value, for positive and negative documents) that use specific sales tax codes.

The requirement is to automatically apply stamp duty(charge) on the invoices (and credit notes) during posting of sales invoice or free text invoice.

The idea is to add new parameters to identify the threshold amount, the stamp duty value to apply, the sales tax codes subjected, and to define a charge code that will be added during the invoice process.

Although this might seem like simple requirement, in the background it took some time to figure out the correct place where to add the charge – and on which document to add it.
So, the focus would be on the technical part where and how to add the charge.

Free text invoice posting

The first part we are going to look at is Free text invoice (FTI) posting. Since the requirement was to add the charge on the posted journal, the focus was to find the place where to add a new charge on the journal itself – not on the FTI. The appropriate method to extend for this was: CustPostInvoice\processMarkup() where the other charges from the FTI were brought on the invoice journal.

With this implementation, there are a couple of issues we encountered:

1. Problem: The totals on the journal were not correct. The reason was simply because the code where we added the charge was too late in this process and the whole creation of the invoice journal was strictly dependent of the FTI, the charges and the taxes on it. When taxes were calculated later, we were missing tax transactions for the newly added charge – so nothing was created in the journal correctly and the totals were messed up.

Solution: The appropriate implementation was to add the charge on the FTI first and let the system take care of copying the charge on the invoice journal and handling the totals calculation. So, the code was changed to add the charge earlier in the process.

If we look at the CustPostInvoice\run() method, we can see the part where there is charge and tax calculation, which is executed and committed before our modification. This caused the tax to be not recalculated after adding the charge.

The code was adjusted and the correct method to use here was CustPostInvoice\prePostCustInvoice()

2.Problem: The tax lines related to the newly created charge were not calculated.
Solution: After adding our charge, delete taxes so that they can be recreated.

3.Problem: Payment schedule total does not match the invoice total.

Solution: After adding the charge, we needed to recreate the payment schedule for the invoice, because we encountered this error during posting:

Sales invoice posting

The second part we are going to look at is Sales invoice posting. Since the requirement was to add the charge on the posted journal, the focus was to find the place where to add a new charge on the journal itself – not on the sales invoice. But, because we already had a lot of issues with the FTI, we learned that the best approach is to add the charge on the original document. With debug, we found the correct place to add the charge, before posting the journal. The appropriate method to extend was: SalesInvoiceJournalCreate\initSourceTable().

Finally, the method which is adding the charge: