src-db/database/model/triggers/M_MOVEMENTLINE_TRG.xml
author Javi Armendáriz <katanagari@gmail.com>
Fri, 24 Jan 2020 08:55:59 +0100
changeset 37000 c5ff57ca6732
parent 36840 ebfdf075a6f7
permissions -rw-r--r--
FIX-42957: Update package-lock to fix npm vulnerabilities.

This also updates linter/formatter scripts to use npm ci instead of install.
This ensures that package-lock.json won't be modified by those executions.
<?xml version="1.0"?>
  <database name="TRIGGER M_MOVEMENTLINE_TRG">
    <trigger name="M_MOVEMENTLINE_TRG" table="M_MOVEMENTLINE" fires="after" insert="true" update="true" delete="true" foreach="row">
      <body><![CDATA[




  v_ID VARCHAR2(32);
  v_RO      NUMBER;
  V_STOCKED NUMBER;
  v_AttrSetValueType M_Product.AttrSetValueType%TYPE;
  v_productname m_product.name%TYPE;
  v_isgeneric   CHAR(1);
  v_DateNull DATE := TO_DATE('01-01-1900', 'DD-MM-YYYY');
  /******************************************************************************
  * The contents of this file are subject to the   Compiere License  Version 1.1
  * ("License"); You may not use this file except in compliance with the License
  * You may obtain a copy of the License at http://www.compiere.org/license.html
  * Software distributed under the License is distributed on an  "AS IS"  basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  * the specific language governing rights and limitations under the License.
  * The Original Code is                  Compiere  ERP &  Business Solution
  * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
  * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
  * Contributor(s): Openbravo SLU
  * Contributions are Copyright (C) 2001-2019 Openbravo S.L.U.
  ******************************************************************************/
    
BEGIN
    
    IF AD_isTriggerEnabled()='N' THEN RETURN;
    END IF;


  -- Get ID
  IF(UPDATING OR INSERTING) THEN
		-- Check if locator and locator_to are diferents
		IF  (:NEW.M_LOCATOR_ID = :NEW.M_LOCATORTO_ID 
                     AND :NEW.M_ATTRIBUTESETINSTANCETO_ID IS NULL) THEN
		  RAISE_APPLICATION_ERROR(-20000, '@MovementLineLocators@');
		END IF;
    v_ID:=:new.M_Movement_ID;
  ELSE
    v_ID:=:old.M_Movement_ID;
  END IF;
  -- ReadOnly Check
  SELECT COUNT(*)
  INTO v_RO
  FROM M_Movement
  WHERE M_Movement_ID=v_ID
    AND(Processed='Y'
    OR Posted='Y') ;

  IF (v_RO > 0 AND(
     (COALESCE(:old.M_MovementLine_ID, '0') <> COALESCE(:new.M_MovementLine_ID, '0'))
     OR(COALESCE(:old.AD_Client_ID, '0') <> COALESCE(:new.AD_Client_ID, '0'))
     OR(COALESCE(:old.AD_Org_ID, '0') <> COALESCE(:new.AD_Org_ID, '0'))
     OR(COALESCE(:old.isactive, '') <> COALESCE(:new.isactive, ''))
     OR(COALESCE(:old.created, v_DateNull) <> COALESCE(:new.created, v_DateNull))
     OR(COALESCE(:old.createdby, '0') <> COALESCE(:new.createdby, '0'))
     OR(COALESCE(:old.M_Movement_ID, '0') <> COALESCE(:new.M_Movement_ID, '0'))
     OR(COALESCE(:old.M_Locator_ID, '0') <> COALESCE(:new.M_Locator_ID, '0'))
     OR(COALESCE(:old.M_LocatorTo_ID, '0') <> COALESCE(:new.M_LocatorTo_ID, '0'))
     OR(COALESCE(:old.M_Product_ID, '0') <> COALESCE(:new.M_Product_ID, '0'))
     OR(COALESCE(:old.line, 0) <> COALESCE(:new.line, 0))
     OR(COALESCE(:old.movementqty, 0) <> COALESCE(:new.movementqty, 0))
     OR(COALESCE(:old.description, '.') <> COALESCE(:new.description, '.'))
     OR(COALESCE(:old.M_AttributeSetInstance_ID, '0') <> COALESCE(:new.M_AttributeSetInstance_ID, '0'))
     OR(COALESCE(:old.M_Product_Uom_ID, '0') <> COALESCE(:new.M_Product_Uom_ID, '0'))
     OR(COALESCE(:old.quantityorder, 0) <> COALESCE(:new.quantityorder, 0))
     OR(COALESCE(:old.C_Uom_ID, '0') <> COALESCE(:new.C_Uom_ID, '0'))
     OR(COALESCE(:old.M_Reservation_ID, '0') <> COALESCE(:new.M_Reservation_ID, '0'))
     OR(COALESCE(:old.C_Aum, '0') <> COALESCE(:new.C_Aum, '0'))
     OR(COALESCE(:old.aumqty, 0) <> COALESCE(:new.aumqty, 0))
     OR(COALESCE(:old.M_AttributeSetInstanceTo_ID, '0') <> COALESCE(:new.M_AttributeSetInstanceTo_ID, '0')))) THEN
    RAISE_APPLICATION_ERROR(-20000, '@20501@') ;
  END IF;
  -- Updating inventory
  IF DELETING THEN
    SELECT COUNT(*)
    INTO V_STOCKED
    FROM M_PRODUCT
    WHERE M_Product_ID=:OLD.M_PRODUCT_ID
      AND IsStocked='Y'
      AND ProductType='I';
    IF V_STOCKED > 0 THEN
      M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATOR_ID,  :OLD.M_ATTRIBUTESETINSTANCE_ID, :OLD.C_UOM_ID, :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, :OLD.MOVEMENTQTY, :OLD.QUANTITYORDER) ;
      M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATORTO_ID, COALESCE(:OLD.M_ATTRIBUTESETINSTANCETO_ID, :OLD.M_ATTRIBUTESETINSTANCE_ID), :OLD.C_UOM_ID, :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, -:OLD.MOVEMENTQTY, -:OLD.QUANTITYORDER) ;
    END IF;
  END IF;
  IF INSERTING THEN
    SELECT COUNT(*)
    INTO V_STOCKED
    FROM M_PRODUCT
    WHERE M_Product_ID=:NEW.M_PRODUCT_ID
      AND IsStocked='Y'
      AND ProductType='I';
    IF V_STOCKED > 0 THEN
      M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATOR_ID, :NEW.M_ATTRIBUTESETINSTANCE_ID, :NEW.C_UOM_ID, :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, -:NEW.MOVEMENTQTY, -:NEW.QUANTITYORDER) ;
      M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATORTO_ID, COALESCE(:NEW.M_ATTRIBUTESETINSTANCETO_ID, :NEW.M_ATTRIBUTESETINSTANCE_ID), :NEW.C_UOM_ID, :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, :NEW.MOVEMENTQTY, :NEW.QUANTITYORDER) ;
    END IF;
  END IF;
  IF UPDATING THEN
    SELECT COUNT(*)
    INTO V_STOCKED
    FROM M_PRODUCT
    WHERE M_Product_ID=:NEW.M_PRODUCT_ID
      AND IsStocked='Y'
      AND ProductType='I';
    IF V_STOCKED > 0 THEN
      IF (:Old.M_Product_ID <> :New.M_Product_ID OR :Old.C_UOM_ID <> :New.C_UOM_ID OR 
      :Old.M_AttributeSetInstance_ID <> :New.M_AttributeSetInstance_ID OR :Old.MovementQty <> :New.MovementQty OR 
      :Old.M_Product_UOM_ID <> :New.M_Product_UOM_ID OR :Old.QuantityOrder <> :New.QuantityOrder OR :Old.M_Reservation_ID <> :New.M_Reservation_ID) THEN
        M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATOR_ID, :OLD.M_ATTRIBUTESETINSTANCE_ID, :OLD.C_UOM_ID, :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, :OLD.MOVEMENTQTY, :OLD.QUANTITYORDER) ;
        M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATORTO_ID, :OLD.M_ATTRIBUTESETINSTANCE_ID, :OLD.C_UOM_ID, :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, -:OLD.MOVEMENTQTY, -:OLD.QUANTITYORDER) ;
        M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATOR_ID, :NEW.M_ATTRIBUTESETINSTANCE_ID, :NEW.C_UOM_ID, :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, -:NEW.MOVEMENTQTY, -:NEW.QUANTITYORDER) ;
        M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATORTO_ID, :NEW.M_ATTRIBUTESETINSTANCE_ID, :NEW.C_UOM_ID, :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, :NEW.MOVEMENTQTY, :NEW.QUANTITYORDER) ;
      ELSE
        IF (:Old.M_Locator_ID <> :New.M_Locator_ID) THEN
          M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATOR_ID, :OLD.M_ATTRIBUTESETINSTANCE_ID, :OLD.C_UOM_ID, :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, :OLD.MOVEMENTQTY, :OLD.QUANTITYORDER) ;
          M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATOR_ID, :NEW.M_ATTRIBUTESETINSTANCE_ID, :NEW.C_UOM_ID, :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, -:NEW.MOVEMENTQTY, -:NEW.QUANTITYORDER) ;
        END IF;
        IF (:Old.M_LocatorTo_ID <> :New.M_LocatorTo_ID) THEN
          M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATORTO_ID, :OLD.M_ATTRIBUTESETINSTANCE_ID, :OLD.C_UOM_ID, :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, -:OLD.MOVEMENTQTY, -:OLD.QUANTITYORDER) ;               
          M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATORTO_ID, :NEW.M_ATTRIBUTESETINSTANCE_ID, :NEW.C_UOM_ID, :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, :NEW.MOVEMENTQTY, :NEW.QUANTITYORDER) ; 
        END IF;     
      END IF;
    END IF;
  END IF;
  IF (INSERTING OR UPDATING) THEN
   --Does not allow to change the attribute set value
   -- for products which attribute set value type is Fixed
   IF (:NEW.M_PRODUCT_ID IS NOT NULL AND COALESCE(:new.M_AttributeSetInstance_ID,'0')!='0') THEN
    SELECT ATTRSETVALUETYPE 
    INTO v_AttrSetValueType
    FROM M_PRODUCT 
    WHERE M_PRODUCT_ID=:NEW.M_PRODUCT_ID;
    IF (v_AttrSetValueType='F') THEN
     RAISE_APPLICATION_ERROR(-20000, '@AttrSetValueFixed@');
    END IF;
   END IF;
  END IF;
  IF (INSERTING OR UPDATING) THEN
    IF (:NEW.m_product_id IS NOT NULL) THEN
      SELECT isgeneric, name INTO v_isgeneric, v_productname
      FROM m_product
      WHERE m_product_id = :NEW.m_product_id;
      IF (v_isgeneric = 'Y') THEN
        RAISE_APPLICATION_ERROR(-20000, '@CannotUseGenericProduct@ ' || v_productname);
      END IF;
    END IF;
  END IF;
END M_MOVEMENTLINE_TRG
]]></body>
    </trigger>
  </database>