src-db/database/model/functions/M_CREATE_RESERVE_FROM_SOL.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Thu, 25 Oct 2012 17:20:43 +0200
changeset 18663 157c92ecd0e2
parent 18652 649b5e295f8c
child 18721 852757ccfb86
permissions -rw-r--r--
[Reservations]Fix pg consistency issues.
<?xml version="1.0"?>
  <database name="FUNCTION M_CREATE_RESERVE_FROM_SOL">
    <function name="M_CREATE_RESERVE_FROM_SOL" type="NULL">
      <parameter name="p_orderline_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_process_reserve" type="CHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_user_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_reservation_id" type="VARCHAR" mode="out">
        <default/>
      </parameter>
      <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):  ______________________________________.
************************************************************************/
/*************
* Procedure to create reservations for the given Sales Order Line.
**************/
  v_product_id        VARCHAR2(32);
  v_uom_id            VARCHAR2(32);
  v_client_id         VARCHAR2(32);
  v_org_id            VARCHAR2(32);
  v_qtyordered        NUMBER;
  v_qtydelivered      NUMBER;
  v_issotrx           CHAR(1);
  v_reservation_id    VARCHAR2(32) := get_uuid();
BEGIN
  SELECT ol.m_product_id, ol.c_uom_id, ol.qtyordered, ol.qtydelivered, o.issotrx,
         ol.ad_client_id, ol.ad_org_id
    INTO v_product_id, v_uom_id, v_qtyordered, v_qtydelivered, v_issotrx,
         v_client_id, v_org_id
  FROM c_orderline ol JOIN c_order o ON ol.c_order_id = o.c_order_id
  WHERE ol.c_orderline_id = p_orderline_id;

  IF (v_issotrx = 'N') THEN
    RAISE_APPLICATION_ERROR(-20000,'@cannotReservePurchaseOrder@');
  END IF;
  IF (v_qtyordered < 0) THEN
    RAISE_APPLICATION_ERROR(-20000,'@cannotReservePurchaseOrder@');
  END IF;
  IF (v_qtyordered - v_qtydelivered <= 0) THEN
    RAISE_APPLICATION_ERROR(-20000,'@cannotReserveDeliveredSalesOrderLine@');
  END IF;

  INSERT INTO m_reservation(
    m_reservation_id, ad_client_id, ad_org_id, isactive,
    created, createdby, updated, updatedby,
    c_orderline_id,
    m_product_id, c_uom_id, quantity, reservedqty, releasedqty,
    res_status, res_process
  ) VALUES (
    v_reservation_id, v_client_id, v_org_id, 'Y',
    now(), p_user_id, now(), p_user_id,
    p_orderline_id,
    v_product_id, v_uom_id, v_qtyordered, 0, v_qtydelivered,
    'DR', 'CO'
  );

  IF (p_process_reserve = 'Y') THEN
    M_RESERVATION_POST(null, v_reservation_id, 'PR', p_user_id);
  END IF;

  p_reservation_id := v_reservation_id;
RETURN;
END M_CREATE_RESERVE_FROM_SOL
]]></body>
    </function>
  </database>