src-db/database/model/functions/M_RESERVATION_POST.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Mon, 22 Oct 2012 18:36:20 +0200
changeset 18652 649b5e295f8c
parent 18649 0cd06065418f
child 18663 157c92ecd0e2
permissions -rw-r--r--
[Reservations]Implementation of procedures and several fixes.
Procedures to manage reservations have been implemented:
- M_Reservation_Consumption
- M_Reserve_Stock_Manual
- M_Reserva_Stock_Auto
- ...
Trigger to keep updated quantities on reservation header added.
Several bug fixes and other minor enhancements.
<?xml version="1.0"?>
  <database name="FUNCTION M_RESERVATION_POST">
    <function name="M_RESERVATION_POST" type="NULL">
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_reservation_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_resaction" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_user_id" type="VARCHAR" mode="in">
        <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):  ______________________________________.
************************************************************************/
  v_ResultStr       VARCHAR2(2000):='';
  v_Message         VARCHAR2(2000):='';
  v_reservation_id  VARCHAR2(32);
  v_user_id         VARCHAR2(32);
  v_Result          NUMBER:=1; --  Success
  v_resaction       VARCHAR2(60);
  v_resstatus       VARCHAR2(60);
  v_newstatus       VARCHAR2(60);
  v_newaction       VARCHAR2(60);

TYPE RECORD IS REF CURSOR;
  Cur_Parameter RECORD;

BEGIN
  IF (p_PInstance_ID IS NOT NULL) THEN
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Processing ' || p_pinstance_id) ;
    v_ResultStr:='PInstanceNotFound';
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'Y', NULL, NULL) ;
    --  Get Parameters
    v_ResultStr:='ReadingParameters';
    FOR Cur_Parameter IN (
        SELECT i.record_id, i.ad_user_id, p.parametername, p.p_string, p.p_number, p.p_date
        FROM ad_pinstance i
            LEFT JOIN ad_pinstance_para p ON i.ad_pinstance_id=p.ad_pinstance_id
        WHERE i.ad_pinstance_id=p_pinstance_id
        ORDER BY p.seqno
    ) LOOP
      IF (cur_parameter.parametername = 'RES_Action') THEN
        v_resaction := cur_parameter.p_string;
      END IF;
      v_reservation_id := cur_parameter.record_id;
      v_user_id := cur_parameter.ad_user_id;
    END LOOP; --  Get Parameter
    DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_reservation_id) ;
  ELSE
    v_reservation_id := p_reservation_id;
    v_resaction := p_resaction;
    v_user_id := p_user_id;
  END IF;

BEGIN
  SELECT res_status
    INTO v_resstatus
  FROM m_reservation
  WHERE m_reservation_id = v_reservation_id;
  
  IF (v_resaction = 'PR') THEN
  /*
    Reservation Complete Process
  */
    IF (v_resstatus != 'DR') THEN
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
    END IF;
    v_newstatus := 'CO';
    v_newaction := 'HO';
    
    -- ADD RESERVED STOCK
    M_RESERVE_STOCK_AUTO (v_reservation_id, v_user_id, v_message);

  ELSIF (v_resaction = 'HO') THEN
  /*
    Reservation Hold Process
  */
    IF (v_resstatus != 'CO') THEN
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
    END IF;
    v_newstatus := 'HO';
    v_newaction := 'UNHO';
  
  
  ELSIF (v_resaction = 'UNHO') THEN
  /*
    Reservation Un-Hold Process
  */
    IF (v_resstatus != 'HO') THEN
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
    END IF;
    v_newstatus := 'CO';
    v_newaction := 'HO';

  ELSIF (v_resaction = 'RE') THEN
  /*
    Reservation Reactivate Process
  */
    IF (v_resstatus != 'CO') THEN
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
    END IF;
    v_newstatus := 'DR';
    v_newaction := 'PR';
  
  ELSIF (v_resaction = 'CL') THEN
  /*
    Reservation Close Process
  */
    IF (v_resstatus != 'CO' AND v_resstatus != 'HO') THEN
      RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
    END IF;
    v_newstatus := 'CL';
    v_newaction := 'CL';

  ELSE
    RAISE_APPLICATION_ERROR(-20000, '@UnsupportedResAction@');
  END IF;

  UPDATE m_reservation
  SET res_status = v_newstatus,
      res_process = v_newaction,
      updated = now(),
      updatedby = v_user_id
  WHERE m_reservation_id = v_reservation_id;

  IF (p_pinstance_id IS NOT NULL) THEN
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
    DBMS_OUTPUT.PUT_LINE('--<<M_Reservation_Post finished>> ' || v_Message) ;
  END IF;
  RETURN;
END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  IF (p_pinstance_id IS NOT NULL) THEN
    ROLLBACK;
    AD_UPDATE_PINSTANCE(p_pinstance_id, NULL, 'N', 0, v_ResultStr) ;
  ELSE
    RAISE;
  END IF;
  RETURN;
END M_RESERVATION_POST
]]></body>
    </function>
  </database>