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.
<?xml version="1.0"?>
  <database name="TRIGGER M_RESERVATION_STOCK_TRG">
    <trigger name="M_RESERVATION_STOCK_TRG" table="M_RESERVATION_STOCK" fires="before" insert="true" update="true" delete="true" foreach="row">
      <body><![CDATA[

/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
* Version 1.1  with a permitted attribution clause; you may not  use this
* file except in compliance with the License. You  may  obtain  a copy of
* the License at http://www.openbravo.com/legal/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 Openbravo ERP.
* The Initial Developer of the Original Code is Openbravo SLU
* All portions are Copyright (C) 2012 Openbravo SLU
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  v_reservedqtydiff NUMBER :=0;
  v_releasedqtydiff NUMBER :=0;
  v_reservation_id      VARCHAR2(32);
BEGIN
  IF (AD_isTriggerEnabled()='N') THEN
    RETURN;
  END IF;

  IF (INSERTING OR UPDATING) THEN
    v_reservedqtydiff := COALESCE(:NEW.quantity, 0);
    v_releasedqtydiff := COALESCE(:NEW.releasedqty, 0);
    v_reservation_id := :NEW.m_reservation_id;
  END IF;

  IF (UPDATING OR DELETING) THEN
    v_reservedqtydiff := v_reservedqtydiff - COALESCE(:OLD.quantity, 0);
    v_releasedqtydiff := v_releasedqtydiff - COALESCE(:OLD.releasedqty, 0);
    v_reservation_id := :OLD.m_reservation_id;
  END IF;

  UPDATE m_reservation
  SET reservedqty = COALESCE(reservedqty, 0) + v_reservedqtydiff,
      releasedqty = COALESCE(releasedqty, 0) + v_releasedqtydiff
  WHERE m_reservation_id = v_reservation_id;
END M_RESERVATION_STOCK_TRG
]]></body>
    </trigger>
  </database>