Fixed issue 30082: Payment plan is updating wrong in some circumstances
authorJorge Garcia <jorge.garcia@openbravo.com>
Wed, 10 Jun 2015 13:09:56 +0200
changeset 26909 99cc73c54bf0
parent 26908 70a9d6732cc5
child 26910 ab5e14425861
Fixed issue 30082: Payment plan is updating wrong in some circumstances

Payment plan is updating wrong if Payment Method is set as Automatic Receipt
and a sales order is partially paid.

The problem was that the function duplicates the value of the received amount
created in the Complete process of the invoice, because the function searches
all the payment details of the payment plan that has an invoice paid
(invoicepaid column).

The solution is to search those payment details that are not pre-paid and then
update the payment plan.

Now, the values are filled correctly.
modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml
--- a/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml	Fri Jun 12 12:05:42 2015 +0200
+++ b/modules/org.openbravo.advpaymentmngt/src-db/database/model/functions/APRM_GEN_PAYMENTSCHEDULE_INV.xml	Wed Jun 10 13:09:56 2015 +0200
@@ -930,16 +930,15 @@
               UPDATE FIN_PAYMENT SET DESCRIPTION = v_finpayment_desc
                 WHERE FIN_PAYMENT_ID = v_FIN_PAYMENT_ID;
 
-              FOR cur_invoicepaid IN (
-                select isinvoicepaid from fin_payment_scheduledetail fpsd
-               left join fin_payment_detail fpd on fpsd.fin_payment_detail_id=fpd.fin_payment_detail_id
-               left join fin_payment fp on fpd.fin_payment_id=fp.fin_payment_id
-               left join fin_payment_schedule ps ON ps.fin_payment_schedule_id = fpsd.fin_payment_schedule_invoice
-                 WHERE ps.c_invoice_id =p_record_id
-              )
-             LOOP
+	      SELECT count(*) INTO v_count
+	      FROM fin_payment_schedule fps
+	      WHERE exists(select 1
+			   from fin_payment_scheduledetail fpsd
+		           where (fps.fin_payment_schedule_id = fpsd.fin_payment_schedule_order OR fps.fin_payment_schedule_id = fpsd.fin_payment_schedule_invoice)
+		           and isinvoicepaid='Y')
+		AND FIN_PAYMENT_SCHEDULE_ID = cur_paymentschedule.FIN_PAYMENT_SCHEDULE_ID;
 
-              IF ((cur_finaccpaymentmethod.ExecutionType<>'A') AND (cur_invoicepaid.isinvoicepaid='Y')) THEN 
+              IF (cur_finaccpaymentmethod.ExecutionType<>'A' AND v_count > 0) THEN
                 UPDATE FIN_PAYMENT_SCHEDULE
                 SET PAIDAMT = PAIDAMT + COALESCE(cur_paymentschedule.OUTSTANDINGAMT,0),
                     OUTSTANDINGAMT = OUTSTANDINGAMT - COALESCE(cur_paymentschedule.OUTSTANDINGAMT, 0)
@@ -988,7 +987,6 @@
                 SET SO_CREDITUSED = COALESCE(SO_CREDITUSED,0) - v_ConvertedAmount
                 WHERE C_BPARTNER_ID = v_BPartner_ID;
               END IF;
-            END LOOP;
               -- Automatic creation of financial transaction
               IF(cur_finaccpaymentmethod.CreateFinTransaction = 'Y' AND cur_finaccpaymentmethod.ExecutionType = 'M') THEN
                 v_ResultStr:='Generating FIN_FINACC_TRANSACTION';