src-db/database/model/triggers/M_REQUISITIONORDER_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
juanpablo@721
     1
<?xml version="1.0"?>
juanpablo@721
     2
  <database name="TRIGGER M_REQUISITIONORDER_TRG">
juanpablo@721
     3
    <trigger name="M_REQUISITIONORDER_TRG" table="M_REQUISITIONORDER" fires="before" insert="true" update="true" delete="true" foreach="row">
gorkaion@586
     4
      <body><![CDATA[
gorkaion@586
     5
gorkaion@586
     6
    /*************************************************************************
gorkaion@586
     7
    * The contents of this file are subject to the Openbravo  Public  License
gorkaion@586
     8
    * Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
gorkaion@586
     9
    * Version 1.1  with a permitted attribution clause; you may not  use this
gorkaion@586
    10
    * file except in compliance with the License. You  may  obtain  a copy of
gorkaion@586
    11
    * the License at http://www.openbravo.com/legal/license.html
gorkaion@586
    12
    * Software distributed under the License  is  distributed  on  an "AS IS"
gorkaion@586
    13
    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
gorkaion@586
    14
    * License for the specific  language  governing  rights  and  limitations
gorkaion@586
    15
    * under the License.
gorkaion@586
    16
    * The Original Code is Openbravo ERP.
gorkaion@586
    17
    * The Initial Developer of the Original Code is Openbravo SL
gorkaion@586
    18
    * All portions are Copyright (C) 2008 Openbravo SL
gorkaion@586
    19
    * All Rights Reserved.
gorkaion@586
    20
    * Contributor(s):  ______________________________________.
gorkaion@586
    21
    ************************************************************************/
gorkaion@586
    22
    v_DocStatus CHAR(2);
gorkaion@586
    23
    v_ReqStatus CHAR(1);
gorkaion@586
    24
    v_Count     NUMBER;
gorkaion@586
    25
BEGIN
gorkaion@586
    26
gorkaion@586
    27
IF INSERTING THEN
gorkaion@586
    28
  SELECT DocStatus INTO v_DocStatus
gorkaion@586
    29
  FROM M_Requisition, M_RequisitionLine
gorkaion@586
    30
  WHERE M_REquisitionLine_ID = :new.M_RequisitionLine_ID
gorkaion@586
    31
    AND M_Requisition.M_Requisition_ID = M_RequisitionLine.M_Requisition_ID;
gorkaion@586
    32
ELSE
gorkaion@586
    33
  SELECT DocStatus INTO v_DocStatus
gorkaion@586
    34
  FROM M_Requisition, M_RequisitionLine
gorkaion@586
    35
  WHERE M_REquisitionLine_ID = :old.M_RequisitionLine_ID
gorkaion@586
    36
    AND M_Requisition.M_Requisition_ID = M_RequisitionLine.M_Requisition_ID;
gorkaion@586
    37
END IF;
gorkaion@586
    38
gorkaion@586
    39
IF (INSERTING OR UPDATING) THEN
gorkaion@586
    40
  SELECT COUNT(*) INTO v_Count
gorkaion@586
    41
  FROM M_REQUISITIONLINE A, C_ORDERLINE B
gorkaion@586
    42
  WHERE A.M_REQUISITIONLINE_ID = :new.M_REQUISITIONLINE_ID
gorkaion@586
    43
    AND B.C_ORDERLINE_ID = :new.C_ORDERLINE_ID
gorkaion@586
    44
    AND A.M_PRODUCT_ID = B.M_PRODUCT_ID;
gorkaion@586
    45
  IF (v_Count = 0) THEN
gorkaion@586
    46
    RAISE_APPLICATION_ERROR(-20524, 'Different products');
gorkaion@586
    47
  END IF;
gorkaion@586
    48
END IF;
gorkaion@586
    49
gorkaion@586
    50
IF ((v_DocStatus <> 'CO') OR (v_ReqStatus <> 'O')) THEN
gorkaion@586
    51
  RAISE_APPLICATION_ERROR(-20501, 'Document processed/posted');
gorkaion@586
    52
END IF;
gorkaion@586
    53
gorkaion@586
    54
IF (UPDATING OR DELETING) THEN
gorkaion@586
    55
  UPDATE M_REQUISITIONLINE
gorkaion@586
    56
  SET ORDEREDQTY = COALESCE(ORDEREDQTY,0) - :OLD.QTY
gorkaion@586
    57
  WHERE M_REQUISITIONLINE_ID = :OLD.M_REQUISITIONLINE_ID;
gorkaion@586
    58
END IF;
gorkaion@586
    59
IF (INSERTING OR UPDATING) THEN
gorkaion@586
    60
  UPDATE M_REQUISITIONLINE
gorkaion@586
    61
  SET ORDEREDQTY = COALESCE(ORDEREDQTY,0) + :NEW.QTY
gorkaion@586
    62
  WHERE M_REQUISITIONLINE_ID = :NEW.M_REQUISITIONLINE_ID;
gorkaion@586
    63
END IF;
antonio@735
    64
END M_REQUISITIONORDER_TRG
antonio@735
    65
]]></body>
juanpablo@721
    66
    </trigger>
juanpablo@721
    67
  </database>