[Warehouse]Avoid using reserved stock.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Thu, 25 Oct 2012 11:34:12 +0200
changeset 18660 64b27bbfdd72
parent 18659 719f9dba5048
child 18661 2d0858b58e59
[Warehouse]Avoid using reserved stock.
src-db/database/model/triggers/M_TRANSACTION_TRG2.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src-db/database/model/triggers/M_TRANSACTION_TRG2.xml	Thu Oct 25 11:34:12 2012 +0200
@@ -0,0 +1,54 @@
+<?xml version="1.0"?>
+  <database name="TRIGGER M_TRANSACTION_TRG2">
+    <trigger name="M_TRANSACTION_TRG2" table="M_TRANSACTION" fires="before" insert="true" update="false" delete="false" 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_onhand      NUMBER;
+  v_reserved    NUMBER;
+
+BEGIN
+  IF (AD_isTriggerEnabled() = 'N') THEN RETURN;
+  END IF;
+
+  IF (INSERTING) THEN
+    IF (:NEW.movementqty < 0) THEN
+      -- Check reservations. Reserved stock must be released before inserting the transacion.
+      SELECT COALESCE(sum(rs.quantity -rs.releasedqty), 0), qtyonhand
+        INTO v_reserved, v_onhand
+      FROM m_reservation_stock rs
+          JOIN m_reservation r ON rs.m_reservation_id = r.m_reservation_id
+          JOIN m_storage_detail sd ON r.m_product_id = sd.m_product_id
+                                      AND r.c_uom_id = sd.c_uom_id
+                                      AND COALESCE(rs.m_attributesetinstance_id, '0') = sd.m_attributesetinstance_id
+                                      AND rs.m_locator_id = sd.m_locator_id
+                                      AND sd.m_product_uom_id IS NULL
+      WHERE res_status <> 'CL'
+        AND r.m_product_id = :NEW.m_product_id
+        AND r.c_uom_id = :NEW.c_uom_id
+        AND rs.m_attributesetinstance_id = :NEW.m_attributesetinstance_id
+        AND rs.m_locator_id = :NEW.m_locator_id;
+      IF (v_onhand - v_reserved < ABS(:NEW.movementqty)) THEN
+        RAISE_APPLICATION_ERROR(-20000, '@CannotTakeOutReservedStock@');
+      END IF;
+    END IF;
+  END IF;
+END M_TRANSACTION_TRG2
+]]></body>
+    </trigger>
+  </database>