src-db/database/model/triggers/M_RESERVATION_STOCK_TRG.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Thu, 25 Oct 2012 17:20:43 +0200
changeset 18663 157c92ecd0e2
parent 18656 bbe2cd86c639
child 18666 60f050d00bc0
permissions -rw-r--r--
[Reservations]Fix pg consistency issues.
gorkaion@18652
     1
<?xml version="1.0"?>
gorkaion@18652
     2
  <database name="TRIGGER M_RESERVATION_STOCK_TRG">
gorkaion@18652
     3
    <trigger name="M_RESERVATION_STOCK_TRG" table="M_RESERVATION_STOCK" fires="before" insert="true" update="true" delete="true" foreach="row">
gorkaion@18652
     4
      <body><![CDATA[
gorkaion@18652
     5
gorkaion@18652
     6
/*************************************************************************
gorkaion@18652
     7
* The contents of this file are subject to the Openbravo  Public  License
gorkaion@18652
     8
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
gorkaion@18652
     9
* Version 1.1  with a permitted attribution clause; you may not  use this
gorkaion@18652
    10
* file except in compliance with the License. You  may  obtain  a copy of
gorkaion@18652
    11
* the License at http://www.openbravo.com/legal/license.html
gorkaion@18652
    12
* Software distributed under the License  is  distributed  on  an "AS IS"
gorkaion@18652
    13
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
gorkaion@18652
    14
* License for the specific  language  governing  rights  and  limitations
gorkaion@18652
    15
* under the License.
gorkaion@18652
    16
* The Original Code is Openbravo ERP.
gorkaion@18652
    17
* The Initial Developer of the Original Code is Openbravo SLU
gorkaion@18652
    18
* All portions are Copyright (C) 2012 Openbravo SLU
gorkaion@18652
    19
* All Rights Reserved.
gorkaion@18652
    20
* Contributor(s):  ______________________________________.
gorkaion@18652
    21
************************************************************************/
gorkaion@18663
    22
  v_reservedqtydiff NUMBER :=0;
gorkaion@18663
    23
  v_releasedqtydiff NUMBER :=0;
gorkaion@18652
    24
  v_reservation_id      VARCHAR2(32);
gorkaion@18652
    25
BEGIN
gorkaion@18652
    26
  IF (AD_isTriggerEnabled()='N') THEN
gorkaion@18652
    27
    RETURN;
gorkaion@18652
    28
  END IF;
gorkaion@18652
    29
gorkaion@18652
    30
  IF (INSERTING OR UPDATING) THEN
gorkaion@18656
    31
    v_reservedqtydiff := COALESCE(:NEW.quantity, 0);
gorkaion@18656
    32
    v_releasedqtydiff := COALESCE(:NEW.releasedqty, 0);
gorkaion@18652
    33
    v_reservation_id := :NEW.m_reservation_id;
gorkaion@18652
    34
  END IF;
gorkaion@18652
    35
gorkaion@18652
    36
  IF (UPDATING OR DELETING) THEN
gorkaion@18656
    37
    v_reservedqtydiff := v_reservedqtydiff - COALESCE(:OLD.quantity, 0);
gorkaion@18656
    38
    v_releasedqtydiff := v_releasedqtydiff - COALESCE(:OLD.releasedqty, 0);
gorkaion@18652
    39
    v_reservation_id := :OLD.m_reservation_id;
gorkaion@18652
    40
  END IF;
gorkaion@18652
    41
gorkaion@18652
    42
  UPDATE m_reservation
gorkaion@18652
    43
  SET reservedqty = COALESCE(reservedqty, 0) + v_reservedqtydiff,
gorkaion@18652
    44
      releasedqty = COALESCE(releasedqty, 0) + v_releasedqtydiff
gorkaion@18652
    45
  WHERE m_reservation_id = v_reservation_id;
gorkaion@18655
    46
END M_RESERVATION_STOCK_TRG
gorkaion@18655
    47
]]></body>
gorkaion@18652
    48
    </trigger>
gorkaion@18652
    49
  </database>