src-db/database/model/triggers/C_ORDERLINE_TRG.xml
changeset 28646 7d8b06f475a6
parent 27263 1e97969be198
parent 28317 77f1cb4dd278
child 28403 885f451f4996
--- a/src-db/database/model/triggers/C_ORDERLINE_TRG.xml	Mon Nov 23 10:08:15 2015 +0100
+++ b/src-db/database/model/triggers/C_ORDERLINE_TRG.xml	Tue Nov 24 13:50:04 2015 +0100
@@ -32,10 +32,8 @@
   v_CountRelations NUMBER;
   v_reservation_id    VARCHAR2(32);
   v_prereservedqty    NUMBER;
-  v_IsWithholding_Tax C_Tax.IsWithholdingTax%TYPE;
   v_IsCashVAT_Tax C_Tax.IsCashVAT%TYPE;
   v_IsCashVAT_Order C_Order.IsCashVAT%TYPE;
-  v_Rate_Tax C_Tax.Rate%TYPE;
   v_istaxincluded CHAR(1);
   v_PriceActual NUMBER;
   
@@ -258,25 +256,29 @@
  -- Update C_Order.IsCashVAT flag from the line tax rate.
  -- We do it this way to force any process that creates Orders to properly set the C_Order.IsCashVAT
  -- If a user manually changes the tax rate when entering a line, the order's header IsCash VAT may change
- -- This will just work for not withholding tax rates
  IF (INSERTING OR UPDATING) THEN
-  IF (:NEW.C_TAX_ID IS NOT NULL) THEN
-   SELECT COALESCE(iswithholdingtax, 'N'), COALESCE(IsCashVAT, 'N'), COALESCE(rate, 0)
-   INTO v_IsWithholding_Tax, v_IsCashVAT_Tax, v_Rate_Tax
-   FROM C_Tax
-   WHERE C_Tax_ID=:NEW.C_Tax_ID;
+  IF (:NEW.C_TAX_ID IS NOT NULL) THEN  
+   SELECT CASE WHEN COUNT(*) > 0 THEN 'Y' ELSE 'N' END
+   INTO v_IsCashVAT_Tax
+   FROM dual
+   WHERE EXISTS (
+        select 1
+        from c_tax t
+        where C_TAX_ISMEMBER(:NEW.C_Tax_ID, t.c_tax_id) = 1
+        and t.isCashVAT = 'Y'
+        and t.rate <> 0
+        and t.isSummary = 'N'
+                 );
 
-   IF (v_IsWithholding_Tax='N' AND v_Rate_Tax<>0) THEN
-    SELECT COALESCE(IsCashVAT, 'N')
-    INTO v_IsCashVAT_Order
-    FROM C_Order
-    WHERE C_Order_ID=:NEW.C_Order_ID;
+   SELECT COALESCE(IsCashVAT, 'N')
+   INTO v_IsCashVAT_Order
+   FROM C_Order
+   WHERE C_Order_ID=:NEW.C_Order_ID;
 
-    IF (v_IsCashVAT_Tax <> v_IsCashVAT_Order) THEN
-      UPDATE C_Order
-      SET IsCashVAT = v_IsCashVAT_Tax
-      WHERE C_Order_ID=:NEW.C_Order_ID;
-    END IF;
+   IF (v_IsCashVAT_Tax <> v_IsCashVAT_Order) THEN
+     UPDATE C_Order
+     SET IsCashVAT = v_IsCashVAT_Tax
+     WHERE C_Order_ID=:NEW.C_Order_ID;
    END IF;
   END IF;
  END IF;