premium-minds / billy

An opensource invoicing engine
https://premium-minds.github.io/billy/
GNU Lesser General Public License v3.0
41 stars 16 forks source link

Errors Encountered with Invoice Deletion API #528

Closed Leestex closed 6 months ago

Leestex commented 7 months ago

I’ve encountered issues with the Client.deleteInvoice(...) function in the Billy Portugal module, which is intended to facilitate the deletion of invoices from the Portugal Tax Service (AT). Specifically, there are two main problems that prevent the successful execution of this function.

Issue 1: Incorrect Tax Registration Number

The method mistakenly uses the application’s SoftwareCertificationNumber as the TaxRegistrationNumber in the SOAP request, leading to an error when interacting with the AT SOAP web service. The erroneous line is:

https://github.com/premium-minds/billy/blob/5387f05e175cd93ebd5bdfe0698ac662377aee21/billy-portugal/src/main/java/com/premiumminds/billy/portugal/services/export/webservice/Client.java#L117

The AT service responds with a validation error:

[com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: simple-type 1: element
{http://factemi.at.min/_financas.pt/documents}TaxRegistrationNumber is not a valid instance of type
{http://factemi.at.min/_financas.pt/documents}SAFPTPortugueseVatNumber. Value is '12' Please see the server log to find more detail regarding exact cause of the failure.

A potential fix could involve using the DeveloperCompanyTaxIdentifier as the TaxRegistrationNumber:

request.setTaxRegistrationNumber(Integer.parseInt(application.getDeveloperCompanyTaxIdentifier())); 

(Note: Further review may be needed regarding type casting.)

Issue 2: Missing CashVATSchemeIndicator

The CashVATSchemeIndicator is not set in the deleteInvoice SOAP request, unlike its presence in the sendInvoice request: https://github.com/premium-minds/billy/blob/5387f05e175cd93ebd5bdfe0698ac662377aee21/billy-portugal/src/main/java/com/premiumminds/billy/portugal/services/export/webservice/Client.java#L225-L227 resulting in another error from the AT service:

[com.sun.xml.ws.fault.ServerSOAPFaultException: Client received SOAP Fault from server: particle 2.1: in element
{http://factemi.at.min/_financas.pt/documents}invoice of type {http://factemi.at.min/_financas.pt/documents}InvoiceDataType,
found <CashVATSchemeIndicator> (in namespace http://factemi.at.min/_financas.pt/documents), but next item should be
{http://factemi.at.min/_financas.pt/documents}HashCharacters Please see the server log to find more detail regarding exact cause of the failure.

A possible solution involves adding a check and setting the CashVATSchemeIndicator similar to how it’s handled in the invoice sending process:

if (ptGenericInvoice.isCashVATEndorser() != null) {
    invoiceData.setCashVATSchemeIndicator(ptGenericInvoice.isCashVATEndorser() ? 1 : 0);
} else {
    invoiceData.setCashVATSchemeIndicator(0);
}

Are there any suggestions or fixes for these problems?

froque commented 7 months ago

Thanks for the report.

You are right about issue 1. This is just plain wrong https://github.com/premium-minds/billy/blob/5387f05e175cd93ebd5bdfe0698ac662377aee21/billy-portugal/src/main/java/com/premiumminds/billy/portugal/services/export/webservice/Client.java#L117

Your proposed solution, however is wrong. The Application Tax Number is not the same as the Business Tax Number.

Unfortunately, the solution requires a breaking change to the Client deleteInvoice methods.

I believe you are incorrect about issue 2. The DeleteInvoiceRequest does not need a CashVATSchemeIndicator.