# HG changeset patch
# User Atul Gaware <atul.gaware@openbravo.com>
# Date 1508205651 -19800
#      Tue Oct 17 07:30:51 2017 +0530
# Node ID b15800463affc5f6b95bfc77a08075e02e255a6e
# Parent  72e4621d891a007930b8408261f725c53417afd7
Fixes Issue 35389: Concurrent modification of the same Sales Order by two different
users can lead to inconsistent data

While updating grandtotal and totalines in C_Order or C_Invoice, set Updated column
value to current date

diff -r 72e4621d891a -r b15800463aff src-db/database/model/triggers/C_INVOICELINE_TRG2.xml
--- a/src-db/database/model/triggers/C_INVOICELINE_TRG2.xml	Wed Oct 11 22:33:34 2017 +0530
+++ b/src-db/database/model/triggers/C_INVOICELINE_TRG2.xml	Tue Oct 17 07:30:51 2017 +0530
@@ -212,7 +212,8 @@
    -- Update Header
    UPDATE C_Invoice
    SET TotalLines = CASE WHEN v_istaxincluded = 'Y' THEN CASE WHEN v_taxAmt = 0 THEN grandtotal - v_oldgrossamt + v_newgrossamt ELSE v_taxBaseAmt END ELSE TotalLines - v_oldLine + v_newLineNetAmt END,
-   GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END
+   GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END,
+   Updated = now()
    WHERE C_Invoice_ID = :new.C_Invoice_ID;
    END IF;
   ELSE -- DELETING
@@ -234,7 +235,8 @@
    -- Update Header
    UPDATE C_Invoice
    SET TotalLines = CASE WHEN v_istaxincluded = 'Y' THEN CASE WHEN v_taxAmt = 0 THEN grandtotal - v_oldgrossamt ELSE v_taxBaseAmt END ELSE TotalLines - v_oldLine + v_newLineNetAmt END,
-   GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END
+   GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END,
+   Updated = now()
    WHERE C_Invoice_ID=:old.C_Invoice_ID;
   END IF;
 
diff -r 72e4621d891a -r b15800463aff src-db/database/model/triggers/C_INVOICETAX_TRG2.xml
--- a/src-db/database/model/triggers/C_INVOICETAX_TRG2.xml	Wed Oct 11 22:33:34 2017 +0530
+++ b/src-db/database/model/triggers/C_INVOICETAX_TRG2.xml	Tue Oct 17 07:30:51 2017 +0530
@@ -14,7 +14,7 @@
 * under the License.
 * The Original Code is Openbravo ERP.
 * The Initial Developer of the Original Code is Openbravo SLU
-* All portions are Copyright (C) 2001-2008 Openbravo SLU
+* All portions are Copyright (C) 2001-2017 Openbravo SLU
 * All Rights Reserved.
 * Contributor(s):  ______________________________________.
 ************************************************************************/
@@ -123,15 +123,18 @@
   IF(v_recalculate = 'N') THEN
     IF(INSERTING) THEN
       UPDATE c_invoice
-      SET grandtotal = grandtotal + taxamountnew
+      SET grandtotal = grandtotal + taxamountnew,
+          Updated = now()
       WHERE c_invoice_id = v_invoiceid;
     ELSIF(UPDATING) THEN
       UPDATE c_invoice
-      SET grandtotal = grandtotal - taxamountold + taxamountnew
+      SET grandtotal = grandtotal - taxamountold + taxamountnew,
+          Updated = now()
       WHERE c_invoice_id = v_invoiceid;
     ELSIF(DELETING) THEN
       UPDATE c_invoice
-      SET grandtotal = grandtotal - taxamountold
+      SET grandtotal = grandtotal - taxamountold,
+          Updated = now()
       WHERE c_invoice_id = v_invoiceid;
     END IF;
   END IF;
diff -r 72e4621d891a -r b15800463aff src-db/database/model/triggers/C_ORDERLINE_TRG2.xml
--- a/src-db/database/model/triggers/C_ORDERLINE_TRG2.xml	Wed Oct 11 22:33:34 2017 +0530
+++ b/src-db/database/model/triggers/C_ORDERLINE_TRG2.xml	Tue Oct 17 07:30:51 2017 +0530
@@ -215,7 +215,8 @@
     -- Update Header
     UPDATE C_Order
     SET TotalLines = CASE WHEN v_istaxincluded = 'Y' THEN CASE WHEN v_taxAmt = 0 THEN grandtotal - v_oldgrossamt + v_newgrossamt ELSE v_taxBaseAmt END ELSE TotalLines - v_oldLine + v_newLineNetAmt END,
-    GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END
+    GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt + v_newgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END,
+    Updated = now()
     WHERE C_Order_ID = :new.C_Order_ID;
     END IF;
   ELSE -- DELETING
@@ -232,7 +233,8 @@
     -- Update Header
     UPDATE C_Order
     SET TotalLines = CASE WHEN v_istaxincluded = 'Y' THEN CASE WHEN v_taxAmt = 0 THEN grandtotal - v_oldgrossamt ELSE v_taxBaseAmt END ELSE TotalLines - v_oldLine + v_newLineNetAmt END,
-    GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END
+    GrandTotal = CASE WHEN v_istaxincluded = 'Y' THEN grandtotal - v_oldgrossamt ELSE TotalLines - v_oldLine + v_newLineNetAmt + v_taxAmt END,
+    Updated = now()
     WHERE C_Order_ID=:old.C_Order_ID;
   END IF;
 
