diff --git a/src-db/database/model/functions/C_ORDER_POST1.xml b/src-db/database/model/functions/C_ORDER_POST1.xml
--- a/src-db/database/model/functions/C_ORDER_POST1.xml
+++ b/src-db/database/model/functions/C_ORDER_POST1.xml
@@ -107,6 +107,12 @@
   v_precision NUMBER;
   Cur_OrderLine RECORD;
   v_DiscountExist NUMBER;
+  v_Outstandingamt NUMBER;
+  v_Paidamt NUMBER;
+  v_Amount NUMBER;
+  v_OldGrandTotal NUMBER;
+  v_FinPaymentScheduleId VARCHAR2(32);
+
   BEGIN
     IF (p_PInstance_ID IS NOT NULL) THEN
       --  Update AD_PInstance
@@ -162,7 +168,10 @@
     -- Get the name of the org of the Order. Added by P.Sarobe
     SELECT name INTO v_Org_Name FROM AD_ORG WHERE ad_org_id = v_Org_ID;
     DBMS_OUTPUT.PUT_LINE('DocAction=' || v_DocAction || ', DocStatus=' || v_DocStatus || ', DocType_ID=' || v_DocType_ID || ', DocTypeTarget_ID=' || v_DocTypeTarget_ID || ', DocSubTypeSO=' || v_DocSubTypeSO) ;
-
+    SELECT co.grandtotal
+        INTO v_OldGrandTotal
+        FROM c_order co
+        WHERE C_Order_ID=v_Record_ID;
     /**
     * Check if order has lines
     */
@@ -458,8 +467,8 @@
         -- if there is no change, the tax calculation, etc. is not needed.
         -- potential problem, if posted (i.e. encumbered) for full amount
         -- and the rest then cancelled out.
-      END IF;
 
+      END IF;  
       /**
       *Update Product purchasing Plan Table
       * Return Material orders do not update the last price.
@@ -1452,7 +1461,40 @@
         WHERE C_Order_ID=v_Record_ID;
       END IF;
     END IF;--FINISH_PROCESS
+    IF (v_DocAction='CL') THEN
 
+        --This part of code Update the tables fin_payment_schedule and fin_payment_scheduledetail when one order is closed and the pyment is not
+        --the same of the expected amount when the order is closed
+        SELECT co.grandtotal,fps.outstandingamt,fps.paidamt,fps.amount,fps.fin_payment_schedule_id
+        INTO v_GrandTotal,v_Outstandingamt,v_Paidamt,v_Amount,v_FinPaymentScheduleId
+        FROM c_order co LEFT JOIN c_orderline col ON co.c_order_id=col.c_order_id
+        LEFT JOIN fin_payment_schedule fps ON fps.c_order_id = col.c_order_id
+        WHERE co.C_Order_ID=v_Record_ID
+        GROUP BY co.c_order_id,co.grandtotal,fps.outstandingamt,fps.paidamt,fps.amount,fps.fin_payment_schedule_id;
+
+        IF (v_OldGrandTotal <> v_GrandTotal) THEN
+           IF (v_Paidamt < v_OldGrandTotal) THEN
+	     UPDATE fin_payment_scheduledetail set amount = v_GrandTotal - v_Paidamt where fin_payment_detail_id IS NULL
+	       AND fin_payment_schedule_order = v_FinPaymentScheduleId;
+           ELSE
+              INSERT INTO fin_payment_scheduledetail (
+              fin_payment_scheduledetail_id,
+              ad_client_id,ad_org_id,created,createdby,updated,updatedby,fin_payment_detail_id,
+              fin_payment_schedule_order,fin_payment_schedule_invoice,amount,isactive,
+              writeoffamt,iscanceled,c_bpartner_id, c_activity_id,m_product_id,c_campaign_id,
+              c_project_id,c_salesregion_id
+              )
+              VALUES(
+              get_uuid(),v_Client_ID,v_Org_ID,now(),v_User,now(),
+              v_UpdatedBy,null,null,null,v_GrandTotal-v_OldGrandTotal,'Y',
+	      0 ,'N',v_c_Bpartner_Id, null,null,null,
+	      null,null
+              );
+           END IF;
+           UPDATE fin_payment_schedule SET amount = v_GrandTotal,outstandingamt = v_GrandTotal-v_Paidamt
+           WHERE fin_payment_schedule_id = v_FinPaymentScheduleId;
+        END IF;
+     END IF;
     --C_Order_Post - Finish_Process Extension Point
     SELECT count(*) INTO v_count
     FROM DUAL
