author Gorka Ion Damián <>
Thu, 25 Oct 2012 17:20:43 +0200
changeset 18663 157c92ecd0e2
parent 18652 649b5e295f8c
child 18697 ad81b7840968
permissions -rw-r--r--
[Reservations]Fix pg consistency issues.
<?xml version="1.0"?>
    <function name="M_RESERVE_STOCK_MANUAL" type="NULL">
      <parameter name="p_reservation_id" type="VARCHAR" mode="in">
      <parameter name="p_type_id" type="VARCHAR" mode="in">
      <parameter name="p_stock_id" type="VARCHAR" mode="in">
      <parameter name="p_qty" type="NUMERIC" mode="in">
      <parameter name="p_user_id" type="VARCHAR" mode="in">
      <parameter name="p_reservation_stock_id" type="VARCHAR" mode="out">
* 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
* 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):  ______________________________________.
* Function to reserve given stock or purchase order line.
* Available types are:
*   - 'SD': p_stock_id represents a m_storage_detail_id reserves stock in the warehouse.
*   - 'PO': p_stock_id represents a c_orderline_id of a pending to receipt purchase order line.
  v_reservation_stock_ID    VARCHAR2(32) := get_uuid();
  v_client_id               VARCHAR2(32);
  v_org_id                  VARCHAR2(32);
  v_locator_id              VARCHAR2(32);
  v_asi_id                  VARCHAR2(32);
  v_poline_id               VARCHAR2(32);
  v_count                   NUMBER;
  SELECT ad_client_id, ad_org_id
    INTO v_client_id, v_org_id
  FROM m_reservation
  WHERE m_reservation_id = p_reservation_id;
  IF (p_type_id = 'SD') THEN
    SELECT count(*) INTO v_count
    FROM m_storage_detail sd
      JOIN m_locator loc ON sd.m_locator_id = loc.m_locator_id
      JOIN m_reservation res ON res.m_reservation_id = p_reservation_id
    WHERE sd.m_storage_detail_id = p_stock_id
      AND sd.m_product_id = res.m_product_id
      AND sd.m_attributesetinstance_id = COALESCE(res.m_attributesetinstance_id, sd.m_attributesetinstance_id)
      AND sd.m_locator_id = COALESCE(res.m_locator_id, sd.m_locator_id)
      AND loc.m_warehouse_id = COALESCE(res.m_warehouse_id, loc.m_warehouse_id);
    IF (v_count = 0) THEN
      RAISE_APPLICATION_ERROR(-20000, '@GivenStorageDetailDoesNotMatchReservationRequirements@');
    END IF;

    SELECT m_locator_id, m_attributesetinstance_id
      INTO v_locator_id, v_asi_id
    FROM m_storage_detail
    WHERE m_storage_detail_id = p_stock_id;
  ELSIF (p_type_id = 'PO') THEN
    v_poline_id := p_stock_id;
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedReservationType@');

  INSERT INTO m_reservation_stock(
    m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
    created, createdby, updated, updatedby,
    m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
    quantity, releasedqty, isallocated
  ) VALUES (
    v_reservation_stock_id, v_client_id, v_org_id, 'Y',
    now(), p_user_id, now(), p_user_id,
    p_reservation_id, v_asi_id, v_locator_id, v_poline_id,
    p_qty, 0, 'N'

  p_reservation_stock_id := v_reservation_stock_id;