src-db/database/model/triggers/M_INOUTLINE_TRG.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 756 ae11e4610537
child 2078 cf88ca44cdd2
permissions -rw-r--r--
Merge r2.5x intro trunk
adrian@94
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="TRIGGER M_INOUTLINE_TRG">
adrian@94
     3
    <trigger name="M_INOUTLINE_TRG" table="M_INOUTLINE" fires="after" insert="true" update="true" delete="true" foreach="row">
gorkaion@239
     4
      <body><![CDATA[
gorkaion@239
     5
adrian@170
     6
adrian@94
     7
adrian@94
     8
juanpablo@1605
     9
  v_ID          varchar2(32);
adrian@94
    10
  v_RO    NUMBER;
adrian@94
    11
  v_movementtype  VARCHAR2(2);
adrian@94
    12
  v_qty    NUMBER;
adrian@94
    13
  v_qtyorder   NUMBER;
adrian@94
    14
  v_qtyold   NUMBER;
adrian@94
    15
  v_qtyorderold  NUMBER;
adrian@94
    16
  v_STOCKED   NUMBER;
juanpablo@1605
    17
  v_UOM_ID    varchar2(32);
adrian@94
    18
/******************************************************************************
adrian@94
    19
 * The contents of this file are subject to the   Compiere License  Version 1.1
adrian@94
    20
 * ("License"); You may not use this file except in compliance with the License
adrian@94
    21
 * You may obtain a copy of the License at http://www.compiere.org/license.html
adrian@94
    22
 * Software distributed under the License is distributed on an  "AS IS"  basis,
adrian@94
    23
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
adrian@94
    24
 * the specific language governing rights and limitations under the License.
gorkaion@239
    25
 * The Original Code is                  Compiere  ERP &  Business Solution
adrian@94
    26
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
adrian@94
    27
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
adrian@94
    28
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
adrian@94
    29
 * Contributor(s): Openbravo SL
adrian@94
    30
 * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
adrian@94
    31
 ******************************************************************************/
adrian@94
    32
 BEGIN
adrian@94
    33
 IF (INSERTING) THEN
adrian@94
    34
  IF (:NEW.M_PRODUCT_ID IS NOT NULL) THEN
adrian@94
    35
     SELECT C_UOM_ID INTO v_UOM_ID FROM M_PRODUCT WHERE M_PRODUCT_ID=:NEW.M_PRODUCT_ID;
juanpablo@1605
    36
     IF (COALESCE(v_UOM_ID,'0') <> COALESCE(:NEW.C_UOM_ID,'0')) THEN
adrian@94
    37
       IF (:NEW.C_ORDERLINE_ID IS NOT NULL) THEN
adrian@94
    38
         SELECT C_UOM_ID INTO v_UOM_ID FROM C_ORDERLINE WHERE C_ORDERLINE_ID = :NEW.C_ORDERLINE_ID;
juanpablo@1605
    39
           IF (COALESCE(v_UOM_ID,'0') <> COALESCE(:NEW.C_UOM_ID,'0')) THEN
adrian@94
    40
             RAISE_APPLICATION_ERROR(-20111, 'Unit of Measure mismatch (product/transaction)');
adrian@94
    41
           END IF;
adrian@94
    42
       ELSE
adrian@94
    43
         RAISE_APPLICATION_ERROR(-20111, 'Unit of Measure mismatch (product/transaction)');
adrian@94
    44
       END IF;
adrian@94
    45
     END IF;
adrian@94
    46
 END IF;
adrian@94
    47
adrian@94
    48
  v_ID := :new.M_InOut_ID;
adrian@94
    49
adrian@94
    50
  SELECT MOVEMENTTYPE INTO v_movementtype
adrian@94
    51
    FROM M_INOUT
adrian@94
    52
    WHERE M_INOUT_ID = :NEW.M_INOUT_ID;
adrian@94
    53
    IF v_movementtype = 'C-' THEN
adrian@94
    54
        v_qty := -:NEW.MOVEMENTQTY;
adrian@94
    55
      v_qtyorder := -:NEW.QUANTITYORDER;
adrian@94
    56
    ELSE
adrian@94
    57
        v_qty := :NEW.MOVEMENTQTY;
adrian@94
    58
        v_qtyorder := :NEW.QUANTITYORDER;
adrian@94
    59
    END IF;
adrian@94
    60
 END IF;
adrian@94
    61
 IF (UPDATING) THEN
juanpablo@1605
    62
  IF (COALESCE(:OLD.C_UOM_ID, '0') <> COALESCE(:NEW.C_UOM_ID, '0')) THEN
adrian@94
    63
    IF (:NEW.M_PRODUCT_ID IS NOT NULL) THEN
adrian@94
    64
      SELECT C_UOM_ID INTO v_UOM_ID FROM M_PRODUCT WHERE M_PRODUCT_ID=:NEW.M_PRODUCT_ID;
juanpablo@1605
    65
      IF (COALESCE(v_UOM_ID,'0') <> COALESCE(:NEW.C_UOM_ID,'0')) THEN
adrian@94
    66
        IF (:NEW.C_ORDERLINE_ID IS NOT NULL) THEN
adrian@94
    67
          SELECT C_UOM_ID INTO v_UOM_ID FROM C_ORDERLINE WHERE C_ORDERLINE_ID = :NEW.C_ORDERLINE_ID;
juanpablo@1605
    68
            IF (COALESCE(v_UOM_ID,'0') <> COALESCE(:NEW.C_UOM_ID,'0')) THEN
adrian@94
    69
              RAISE_APPLICATION_ERROR(-20111, 'Unit of Measure mismatch (product/transaction)');
adrian@94
    70
            END IF;
adrian@94
    71
        ELSE
adrian@94
    72
          RAISE_APPLICATION_ERROR(-20111, 'Unit of Measure mismatch (product/transaction)');
adrian@94
    73
        END IF;
adrian@94
    74
      END IF;
adrian@94
    75
  END IF;
adrian@94
    76
 END IF;
adrian@94
    77
   v_ID := :new.M_InOut_ID;
adrian@94
    78
adrian@94
    79
   SELECT MOVEMENTTYPE INTO v_movementtype
adrian@94
    80
     FROM M_INOUT
adrian@94
    81
     WHERE M_INOUT_ID = :NEW.M_INOUT_ID;
adrian@94
    82
     IF v_movementtype = 'C-' THEN
adrian@94
    83
           v_qty := -:NEW.MOVEMENTQTY;
adrian@94
    84
         v_qtyorder := -:NEW.QUANTITYORDER;
adrian@94
    85
        ELSE
adrian@94
    86
           v_qty := :NEW.MOVEMENTQTY;
adrian@94
    87
           v_qtyorder := :NEW.QUANTITYORDER;
adrian@94
    88
     END IF;
adrian@94
    89
adrian@94
    90
 END IF;
adrian@94
    91
adrian@94
    92
adrian@94
    93
 IF UPDATING OR DELETING THEN
adrian@94
    94
     v_ID := :old.M_InOut_ID;
adrian@94
    95
  SELECT MOVEMENTTYPE INTO v_movementtype
adrian@94
    96
  FROM M_INOUT
adrian@94
    97
  WHERE M_INOUT_ID = :OLD.M_INOUT_ID;
adrian@94
    98
  IF v_movementtype = 'C-' THEN
adrian@94
    99
       v_qtyold := :OLD.MOVEMENTQTY;
adrian@94
   100
     v_qtyorderold := :OLD.QUANTITYORDER;
adrian@94
   101
    ELSE
adrian@94
   102
       v_qtyold := -:OLD.MOVEMENTQTY;
adrian@94
   103
     v_qtyorderold := -:OLD.QUANTITYORDER;
adrian@94
   104
    END IF;
adrian@94
   105
 END IF;
adrian@94
   106
adrian@94
   107
 -- ReadOnly Check
adrian@94
   108
 SELECT  COUNT(*)
adrian@94
   109
   INTO v_RO
adrian@94
   110
 FROM M_InOut
adrian@94
   111
 WHERE M_InOut_ID=v_ID
adrian@94
   112
   AND (Processed='Y' OR Posted='Y');
gorkaion@239
   113
 IF (v_RO > 0) THEN
adrian@94
   114
  IF (INSERTING OR DELETING) THEN
adrian@94
   115
   RAISE_APPLICATION_ERROR(-20501, 'Document processed/posted');
gorkaion@239
   116
  ELSIF (:new.M_Product_ID<>:old.M_Product_ID OR :new.MovementQty<>:old.MovementQty
juanpablo@1605
   117
  OR COALESCE(:new.M_AttributeSetInstance_ID, '0') <> COALESCE(:old.M_AttributeSetInstance_ID, '0')
juanpablo@1605
   118
  OR COALESCE(:new.M_Locator_ID,'-1') <> COALESCE(:old.M_Locator_ID,'-1'))
adrian@94
   119
 THEN
adrian@94
   120
  RAISE_APPLICATION_ERROR(-20501, 'Document processed/posted');
adrian@94
   121
   END IF;
adrian@94
   122
 END IF;
adrian@94
   123
 -- UPDATING inventory
adrian@94
   124
 IF (UPDATING OR DELETING) THEN
adrian@94
   125
    IF (:old.M_PRODUCT_ID IS NOT NULL AND :OLD.M_LOCATOR_ID IS NOT NULL) THEN
adrian@94
   126
   SELECT COUNT(*) INTO V_STOCKED
adrian@94
   127
  FROM M_PRODUCT
adrian@94
   128
  WHERE M_Product_ID=:OLD.M_PRODUCT_ID
adrian@94
   129
  AND IsStocked = 'Y' AND ProductType = 'I';
gorkaion@239
   130
   IF V_STOCKED > 0 THEN
adrian@94
   131
       M_UPDATE_INVENTORY(:OLD.AD_CLIENT_ID, :OLD.AD_ORG_ID, :OLD.UPDATEDBY, :OLD.M_PRODUCT_ID, :OLD.M_LOCATOR_ID,
adrian@94
   132
    :OLD.M_ATTRIBUTESETINSTANCE_ID, :OLD.C_UOM_ID,
adrian@94
   133
       :OLD.M_PRODUCT_UOM_ID, NULL, NULL, NULL, v_qtyold, v_qtyorderold);
adrian@94
   134
   END IF;
adrian@94
   135
    END IF;
adrian@94
   136
  END IF;
adrian@94
   137
 IF (INSERTING OR UPDATING) THEN
adrian@94
   138
   IF (:NEW.M_PRODUCT_ID IS NOT NULL AND :NEW.M_LOCATOR_ID IS NOT NULL) THEN
adrian@94
   139
   SELECT COUNT(*) INTO V_STOCKED
adrian@94
   140
  FROM M_PRODUCT
adrian@94
   141
  WHERE M_Product_ID=:NEW.M_PRODUCT_ID
adrian@94
   142
  AND IsStocked = 'Y' AND ProductType = 'I';
gorkaion@239
   143
   IF V_STOCKED > 0 THEN
adrian@94
   144
       M_UPDATE_INVENTORY(:NEW.AD_CLIENT_ID, :NEW.AD_ORG_ID, :NEW.UPDATEDBY, :NEW.M_PRODUCT_ID, :NEW.M_LOCATOR_ID,
adrian@94
   145
    :NEW.M_ATTRIBUTESETINSTANCE_ID, :NEW.C_UOM_ID,
adrian@94
   146
       :NEW.M_PRODUCT_UOM_ID, NULL, NULL, NULL, v_qty, v_qtyorder);
adrian@94
   147
   END IF;
adrian@94
   148
    END IF;
adrian@94
   149
  END IF;
antonio@735
   150
END M_INOUTLINE_TRG
gorkaion@239
   151
]]></body>
adrian@94
   152
    </trigger>
adrian@94
   153
  </database>