src-db/database/model/functions/M_RESERVE_STOCK_MANUAL.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 18697 ad81b7840968
permissions -rw-r--r--
[Reservations]Fix pg consistency issues.
gorkaion@18652
     1
<?xml version="1.0"?>
gorkaion@18652
     2
  <database name="FUNCTION M_RESERVE_STOCK_MANUAL">
gorkaion@18652
     3
    <function name="M_RESERVE_STOCK_MANUAL" type="NULL">
gorkaion@18652
     4
      <parameter name="p_reservation_id" type="VARCHAR" mode="in">
gorkaion@18652
     5
        <default/>
gorkaion@18652
     6
      </parameter>
gorkaion@18652
     7
      <parameter name="p_type_id" type="VARCHAR" mode="in">
gorkaion@18652
     8
        <default/>
gorkaion@18652
     9
      </parameter>
gorkaion@18652
    10
      <parameter name="p_stock_id" type="VARCHAR" mode="in">
gorkaion@18652
    11
        <default/>
gorkaion@18652
    12
      </parameter>
gorkaion@18652
    13
      <parameter name="p_qty" type="NUMERIC" mode="in">
gorkaion@18652
    14
        <default/>
gorkaion@18652
    15
      </parameter>
gorkaion@18652
    16
      <parameter name="p_user_id" type="VARCHAR" mode="in">
gorkaion@18652
    17
        <default/>
gorkaion@18652
    18
      </parameter>
gorkaion@18652
    19
      <parameter name="p_reservation_stock_id" type="VARCHAR" mode="out">
gorkaion@18652
    20
        <default/>
gorkaion@18652
    21
      </parameter>
gorkaion@18652
    22
      <body><![CDATA[/*************************************************************************
gorkaion@18652
    23
* The contents of this file are subject to the Openbravo  Public  License
gorkaion@18652
    24
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
gorkaion@18652
    25
* Version 1.1  with a permitted attribution clause; you may not  use this
gorkaion@18652
    26
* file except in compliance with the License. You  may  obtain  a copy of
gorkaion@18652
    27
* the License at http://www.openbravo.com/legal/license.html
gorkaion@18652
    28
* Software distributed under the License  is  distributed  on  an "AS IS"
gorkaion@18652
    29
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
gorkaion@18652
    30
* License for the specific  language  governing  rights  and  limitations
gorkaion@18652
    31
* under the License.
gorkaion@18652
    32
* The Original Code is Openbravo ERP.
gorkaion@18652
    33
* The Initial Developer of the Original Code is Openbravo SLU
gorkaion@18652
    34
* All portions are Copyright (C) 2012 Openbravo SLU
gorkaion@18652
    35
* All Rights Reserved.
gorkaion@18652
    36
* Contributor(s):  ______________________________________.
gorkaion@18652
    37
************************************************************************/
gorkaion@18652
    38
/*************
gorkaion@18652
    39
* Function to reserve given stock or purchase order line.
gorkaion@18652
    40
* Available types are:
gorkaion@18652
    41
*   - 'SD': p_stock_id represents a m_storage_detail_id reserves stock in the warehouse.
gorkaion@18652
    42
*   - 'PO': p_stock_id represents a c_orderline_id of a pending to receipt purchase order line.
gorkaion@18652
    43
**************/
gorkaion@18652
    44
  v_reservation_stock_ID    VARCHAR2(32) := get_uuid();
gorkaion@18652
    45
  v_client_id               VARCHAR2(32);
gorkaion@18652
    46
  v_org_id                  VARCHAR2(32);
gorkaion@18652
    47
  v_locator_id              VARCHAR2(32);
gorkaion@18652
    48
  v_asi_id                  VARCHAR2(32);
gorkaion@18652
    49
  v_poline_id               VARCHAR2(32);
gorkaion@18652
    50
  v_count                   NUMBER;
gorkaion@18652
    51
BEGIN
gorkaion@18652
    52
  SELECT ad_client_id, ad_org_id
gorkaion@18652
    53
    INTO v_client_id, v_org_id
gorkaion@18652
    54
  FROM m_reservation
gorkaion@18652
    55
  WHERE m_reservation_id = p_reservation_id;
gorkaion@18652
    56
  IF (p_type_id = 'SD') THEN
gorkaion@18652
    57
    SELECT count(*) INTO v_count
gorkaion@18652
    58
    FROM m_storage_detail sd
gorkaion@18652
    59
      JOIN m_locator loc ON sd.m_locator_id = loc.m_locator_id
gorkaion@18652
    60
      JOIN m_reservation res ON res.m_reservation_id = p_reservation_id
gorkaion@18652
    61
    WHERE sd.m_storage_detail_id = p_stock_id
gorkaion@18652
    62
      AND sd.m_product_id = res.m_product_id
gorkaion@18652
    63
      AND sd.m_attributesetinstance_id = COALESCE(res.m_attributesetinstance_id, sd.m_attributesetinstance_id)
gorkaion@18652
    64
      AND sd.m_locator_id = COALESCE(res.m_locator_id, sd.m_locator_id)
gorkaion@18652
    65
      AND loc.m_warehouse_id = COALESCE(res.m_warehouse_id, loc.m_warehouse_id);
gorkaion@18652
    66
    IF (v_count = 0) THEN
gorkaion@18652
    67
      RAISE_APPLICATION_ERROR(-20000, '@GivenStorageDetailDoesNotMatchReservationRequirements@');
gorkaion@18652
    68
    END IF;
gorkaion@18652
    69
gorkaion@18652
    70
    SELECT m_locator_id, m_attributesetinstance_id
gorkaion@18652
    71
      INTO v_locator_id, v_asi_id
gorkaion@18652
    72
    FROM m_storage_detail
gorkaion@18652
    73
    WHERE m_storage_detail_id = p_stock_id;
gorkaion@18652
    74
  ELSIF (p_type_id = 'PO') THEN
gorkaion@18652
    75
    v_poline_id := p_stock_id;
gorkaion@18652
    76
  ELSE
gorkaion@18652
    77
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedReservationType@');
gorkaion@18652
    78
  END IF;
gorkaion@18652
    79
gorkaion@18652
    80
  INSERT INTO m_reservation_stock(
gorkaion@18652
    81
    m_reservation_stock_id, ad_client_id, ad_org_id, isactive,
gorkaion@18652
    82
    created, createdby, updated, updatedby,
gorkaion@18652
    83
    m_reservation_id, m_attributesetinstance_id, m_locator_id, c_orderline_id,
gorkaion@18652
    84
    quantity, releasedqty, isallocated
gorkaion@18652
    85
  ) VALUES (
gorkaion@18652
    86
    v_reservation_stock_id, v_client_id, v_org_id, 'Y',
gorkaion@18652
    87
    now(), p_user_id, now(), p_user_id,
gorkaion@18652
    88
    p_reservation_id, v_asi_id, v_locator_id, v_poline_id,
gorkaion@18652
    89
    p_qty, 0, 'N'
gorkaion@18652
    90
  );
gorkaion@18652
    91
gorkaion@18652
    92
  p_reservation_stock_id := v_reservation_stock_id;
gorkaion@18652
    93
  RETURN ;
gorkaion@18663
    94
END M_RESERVE_STOCK_MANUAL
gorkaion@18663
    95
]]></body>
gorkaion@18652
    96
    </function>
gorkaion@18652
    97
  </database>