0028236: Payment schedule amount incorrectly calculated when adding a payment from reconcile window to a sales order

Description

Payment schedule amount incorrectly calculated when adding a payment from financial account reconcile window to a sales order

Steps To Reproduce

- Create a sales order from "F&B España - Región Norte" org to BP "Alimentos y Supermercados, S.A" for 1,000 and book it.
- Go to the Financial Account "Cuenta de Banco"
- Click on tab "Imported Bank Statement"
- Click on new, give it a name, and go to "Bank Statement Lines" tab.
- Click on new, give it a reference, choose "Alimentos y Supermercados, S.A" business partner, set amount in to 1,000.
- Click on save and process, and Match Statement.
- Click on "+" icon to create a transaction.
- Click on Add Payment In/Out
- Choose transaction type "Orders", set the amount to 1,000.
- Select just the 1,000 order created in the previous step.
- Click on process, and choose "Process Received payment/s and deposit", and click on Ok.
- Go to the sales order, and realize that payment schedule record says:
Expected Amount: 1,000
Received Amount: 2,000
Outstanding: -1,000

Proposed Solution

- fin_payment_schedule record is not correctly updated when running the described steps.
- fin_payment_schedule_detail records are ok. The issue is when updating the fin_payment_schedule record.
- Please notice that if you click on "Add Transaction" in Financial Account window, bug is not reproduced, only if AddTransaction servlet is invoqued from the automatic reconcile window.

Furthermore, if you delete the conciliation and transaction in financial account, and try to re-open the sales order, the "Related payments" error is shown, when is it false: the payment was deleted, but:
1- Due to the issue, instead of received amount=1,000 outstanding=0; the result was received amount=2,000 outstanding=-1,000
2- When deleting the payment in, then the information in the payment plan tab of the sales order is: received amount=1,000 outstanding=0. But the number of payments column says "0", and actually, the payment plan detail tab is empty.
This means that, in order to fix this issue, it's required a module script that updates all this amounts, as there is no way (through UI) to fix this :(

In FIN_AddPayment.java, at line 1127, this line:
paymentSchedule.setPaidAmount(paymentSchedule.getPaidAmount().add(amount));
sets the incorrect amount, as the paymentSchedule.PaidAmount value is already updated with the amount of the payment that is being created.
Following the example in steps to reproduce. When arriving this point, the values are:
paymentSchedule.getPaidAmount() <= 1,000
amount <= 1,000
This way, the paid amount that is set is 2,000 and not 1,000.
This line was set in the 3.0RC2 tagged changeset, so there is not the problem. It seems, this function is, now, called twice during the process, or something like that.

- Fin_PaymentProcess class (in line 409) calls FIN_AddPayment.updatePaymentScheduleAmounts which actually updates the paid amount of the fin_payment_schedule record.
- Afterwards, in line 447, OBDal.getInstance().flush() persists these changes.
- Once the execuction is over, and servlet finishes completely the execution, then PaidStatusEventHandler class is invoked and onUpdate function executed. As psd.isInvoicePaid() is true, then FIN_Utility.updatePaymentAmounts function is run again, duplicating the paid amount.

I've gone as further as I can. I'm not used at all to this new functionality in OB of EntityPersistenceEventObserver objects.
This one task I do several times per week, and it was always working fine. I've upgraded from MP20 to PR14Q2.6, and now this issue is faced. It seems to be related to the changes done in the windows of the advanced payables and receivables module.
Thanks in advance for your help!!