src-db/database/model/functions/M_RESERVE_STOCK_AUTO.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 18710 a48b80c8fbfe
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_AUTO">
gorkaion@18652
     3
    <function name="M_RESERVE_STOCK_AUTO" 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_user_id" type="VARCHAR" mode="in">
gorkaion@18652
     8
        <default/>
gorkaion@18652
     9
      </parameter>
gorkaion@18652
    10
      <parameter name="p_reservedqty" type="NUMERIC" mode="out">
gorkaion@18652
    11
        <default/>
gorkaion@18652
    12
      </parameter>
gorkaion@18652
    13
      <body><![CDATA[/*************************************************************************
gorkaion@18652
    14
* The contents of this file are subject to the Openbravo  Public  License
gorkaion@18652
    15
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
gorkaion@18652
    16
* Version 1.1  with a permitted attribution clause; you may not  use this
gorkaion@18652
    17
* file except in compliance with the License. You  may  obtain  a copy of
gorkaion@18652
    18
* the License at http://www.openbravo.com/legal/license.html
gorkaion@18652
    19
* Software distributed under the License  is  distributed  on  an "AS IS"
gorkaion@18652
    20
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
gorkaion@18652
    21
* License for the specific  language  governing  rights  and  limitations
gorkaion@18652
    22
* under the License.
gorkaion@18652
    23
* The Original Code is Openbravo ERP.
gorkaion@18652
    24
* The Initial Developer of the Original Code is Openbravo SLU
gorkaion@18652
    25
* All portions are Copyright (C) 2012 Openbravo SLU
gorkaion@18652
    26
* All Rights Reserved.
gorkaion@18652
    27
* Contributor(s):  ______________________________________.
gorkaion@18652
    28
************************************************************************/
gorkaion@18652
    29
/*************
gorkaion@18652
    30
* Function to reserve stock using the get stock function.
gorkaion@18652
    31
**************/
gorkaion@18652
    32
  v_product_id                  VARCHAR2(32);
gorkaion@18652
    33
  v_uom_id                      VARCHAR2(32);
gorkaion@18652
    34
  v_asi_id                      VARCHAR2(32);
gorkaion@18652
    35
  v_locator_id                  VARCHAR2(32);
gorkaion@18652
    36
  v_warehouse_id                VARCHAR2(32);
gorkaion@18652
    37
  v_client_id                   VARCHAR2(32);
gorkaion@18652
    38
  v_org_id                      VARCHAR2(32);
gorkaion@18652
    39
  v_soline_id                   VARCHAR2(32);
gorkaion@18652
    40
  v_qty                         NUMBER;
gorkaion@18652
    41
  v_pendingtoreserveqty         NUMBER;
gorkaion@18652
    42
  v_pendingqty                  NUMBER;
gorkaion@18652
    43
  v_reserveqty                  NUMBER;
gorkaion@18652
    44
  v_dummy                       VARCHAR2(2000) := '';
gorkaion@18652
    45
  v_pinstance_id                VARCHAR2(32) := get_uuid();
gorkaion@18652
    46
  
gorkaion@18652
    47
gorkaion@18652
    48
  TYPE RECORD IS REF CURSOR;
gorkaion@18652
    49
  cur_stock                     RECORD;
gorkaion@18652
    50
gorkaion@18652
    51
BEGIN
gorkaion@18652
    52
  SELECT quantity - COALESCE(reservedqty,0),
gorkaion@18652
    53
         m_product_id, c_uom_id, m_attributesetinstance_id, m_locator_id, m_warehouse_id,
gorkaion@18652
    54
         ad_client_id, ad_org_id, c_orderline_id, quantity
gorkaion@18652
    55
    INTO v_pendingtoreserveqty,
gorkaion@18652
    56
         v_product_id, v_uom_id, v_asi_id, v_locator_id, v_warehouse_id,
gorkaion@18652
    57
         v_client_id, v_org_id, v_soline_id, v_reserveqty
gorkaion@18652
    58
  FROM m_reservation
gorkaion@18652
    59
  WHERE m_reservation_id = p_reservation_id;
gorkaion@18652
    60
gorkaion@18652
    61
  IF (v_pendingtoreserveqty <= 0) THEN
gorkaion@18652
    62
    p_reservedqty := 0;
gorkaion@18652
    63
    RETURN;
gorkaion@18652
    64
  END IF;
gorkaion@18652
    65
gorkaion@18652
    66
  -- Call M_GET_STOCK
gorkaion@18652
    67
  DECLARE
gorkaion@18652
    68
    v_pinstance_result AD_PInstance.result%TYPE;
gorkaion@18652
    69
    v_pinstance_msg AD_PInstance.errormsg%TYPE;
gorkaion@18652
    70
    v_warehouse_rule_id VARCHAR2(32);
gorkaion@18652
    71
  BEGIN
gorkaion@18652
    72
    INSERT INTO ad_pinstance (
gorkaion@18652
    73
        ad_pinstance_id, ad_process_id, record_id, isactive, 
gorkaion@18652
    74
        ad_user_id, ad_client_id, ad_org_id, created, createdby,
gorkaion@18652
    75
        updated, updatedby
gorkaion@18652
    76
    ) VALUES (
gorkaion@18652
    77
        v_pinstance_id, 'FF80818132C964E30132C9747257002E', p_reservation_id, 'Y',
gorkaion@18652
    78
        p_User_ID, v_client_id, v_Org_ID, now(), p_user_ID,
gorkaion@18652
    79
        now(), p_user_ID
gorkaion@18652
    80
    );
gorkaion@18652
    81
gorkaion@18652
    82
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '10', 'AD_Client_ID', v_client_id, null, null, null, null, null);
gorkaion@18652
    83
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '20', 'AD_Org_ID', v_org_id, null, null, null, null, null);
gorkaion@18652
    84
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '30', 'M_Product_ID', v_product_id, null, null, null, null, null);
gorkaion@18652
    85
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '40', 'C_Uom_ID', v_uom_id, null, null, null, null, null);
gorkaion@18652
    86
    --AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '50', 'M_Product_Uom_ID', v_Product_UOM_old, null, null, null, null, null);
gorkaion@18652
    87
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '60', 'M_Warehouse_ID', v_warehouse_id, null, null, null, null, null);
gorkaion@18652
    88
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '70', 'M_Locator_ID', v_locator_id, null, null, null, null, null);
gorkaion@18652
    89
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '80', 'M_AttributesetInstance_ID', v_asi_id, null, null, null, null, null);
gorkaion@18652
    90
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '90', 'Quantity', null, null, v_pendingtoreserveqty, null, null, null);
gorkaion@18652
    91
    /*
gorkaion@18652
    92
    * ADD PROPER PROCESS ID!
gorkaion@18652
    93
    */
gorkaion@18652
    94
    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '100', 'ProcessID', '---', null, null, null, null, null);
gorkaion@18652
    95
gorkaion@18652
    96
    IF (v_soline_id IS NOT NULL) THEN
gorkaion@18652
    97
      SELECT m_warehouse_rule_id INTO v_warehouse_rule_id
gorkaion@18652
    98
      FROM c_orderline
gorkaion@18652
    99
      WHERE c_orderline_id = v_soline_id;
gorkaion@18652
   100
      AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '110', 'M_Warehouse_Rule_ID', v_warehouse_rule_id, null, null, null, null, null);
gorkaion@18652
   101
    END IF;
gorkaion@18652
   102
gorkaion@18652
   103
    M_GET_STOCK(v_pinstance_ID, 'N');
gorkaion@18652
   104
    -- Check result
gorkaion@18652
   105
    SELECT result, errormsg
gorkaion@18652
   106
      INTO v_pinstance_result, v_pinstance_msg
gorkaion@18652
   107
    FROM ad_pinstance
gorkaion@18652
   108
    WHERE ad_pinstance_id = v_pinstance_id;
gorkaion@18652
   109
    IF (v_pinstance_result = 0) THEN
gorkaion@18652
   110
      -- Error on m_get_stock
gorkaion@18652
   111
      RAISE_APPLICATION_ERROR(-20000, v_pinstance_msg);
gorkaion@18652
   112
    END IF;
gorkaion@18652
   113
  END; -- End Call M_GET_STOCK
gorkaion@18652
   114
  
gorkaion@18652
   115
  v_pendingqty := v_pendingtoreserveqty;
gorkaion@18652
   116
  FOR cur_stock IN (
gorkaion@18652
   117
    SELECT *
gorkaion@18652
   118
    FROM m_stock_proposed
gorkaion@18652
   119
    WHERE ad_pinstance_id = v_pinstance_id
gorkaion@18652
   120
  ) LOOP
gorkaion@18652
   121
    v_qty := LEAST(cur_stock.quantity, v_pendingqty);
gorkaion@18652
   122
    M_RESERVE_STOCK_MANUAL(p_reservation_id, 'SD', cur_stock.m_storage_detail_id, v_qty, p_user_id, v_dummy);
gorkaion@18652
   123
    v_pendingqty := v_pendingqty - v_qty;
gorkaion@18652
   124
    IF (v_pendingqty <= 0) THEN
gorkaion@18652
   125
      p_reservedqty := v_reserveqty;
gorkaion@18652
   126
      RETURN;
gorkaion@18652
   127
    END IF;
gorkaion@18652
   128
  END LOOP;
gorkaion@18652
   129
gorkaion@18652
   130
  p_reservedqty := v_reserveqty - v_pendingqty;
gorkaion@18652
   131
  RETURN;
gorkaion@18663
   132
END M_RESERVE_STOCK_AUTO
gorkaion@18663
   133
]]></body>
gorkaion@18652
   134
    </function>
gorkaion@18652
   135
  </database>