src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
changeset 31525 42227f2c47c3
parent 31524 e1c6deda8742
child 31526 0c31b06dd168
--- a/src-db/database/model/triggers/C_INVOICELINE_TRG2.xml	Tue Feb 14 15:52:41 2017 -0500
+++ b/src-db/database/model/triggers/C_INVOICELINE_TRG2.xml	Wed Feb 15 15:56:52 2017 -0500
@@ -105,17 +105,27 @@
    END IF;  
   IF(UPDATING OR DELETING) THEN
     v_oldLine:= COALESCE(:OLD.LineNetAmt,0);
-    IF(DELETING AND (:old.c_orderline_id IS NOT NULL OR :old.c_orderline_id <> '')) THEN
+    IF(UPDATING) THEN
+      v_newLineNetAmt := COALESCE(:new.LineNetAmt,0);
+    END IF;
+    
+    IF(:old.c_orderline_id IS NOT NULL AND (v_oldLine <> 0 OR v_newLineNetAmt <> 0)) THEN
       SELECT COALESCE(sum(fps.paidamt),0)
         INTO v_prepayment
       FROM fin_payment_schedule fps
         JOIN c_order ord ON ord.c_order_id=fps.c_order_id
         JOIN c_orderline ordline ON ordline.c_order_id = ord.c_order_id
       WHERE ordline.c_orderline_id = :old.c_orderline_id;
-
-      UPDATE c_invoice
-      SET prepaymentamt = prepaymentamt - v_prepayment
-      WHERE c_invoice_id = :old.c_invoice_id;
+        
+      IF (DELETING OR (UPDATING AND v_newLineNetAmt = 0 AND v_oldLine <> 0)) THEN
+        UPDATE c_invoice
+        SET prepaymentamt = prepaymentamt - v_prepayment
+        WHERE c_invoice_id = :old.c_invoice_id;
+      ELSEIF (UPDATING AND v_newLineNetAmt <> 0 AND v_oldLine = 0) THEN
+        UPDATE c_invoice
+        SET prepaymentamt = prepaymentamt + v_prepayment
+        WHERE c_invoice_id = :old.c_invoice_id;      
+      END IF;     
     END IF;
   END IF;  
   IF (INSERTING OR UPDATING) THEN