src-db/database/model/functions/R_REQUEST_PROCESS.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 785 8dba91261590
child 6701 690d2af86089
permissions -rw-r--r--
Merge r2.5x intro trunk
<?xml version="1.0"?>
  <database name="FUNCTION R_REQUEST_PROCESS">
    <function name="R_REQUEST_PROCESS" type="NULL">
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_r_request_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
  * The contents of this file are subject to the Compiere Public
  * License 1.1 ("License"); You may not use this file except in
  * compliance with the License. You may obtain a copy of the License in
  * the legal folder of your Openbravo installation.
  * 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  Compiere  ERP &  Business Solution
  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke,
  * parts created by ComPiere are Copyright (C) ComPiere, Inc.;
  * All Rights Reserved.
  * Contributor(s): Openbravo SL
  * Contributions are Copyright (C) 2001-2008 Openbravo, S.L.
  *
  * Specifically, this derivative work is based upon the following Compiere
  * file and version.
  *************************************************************************
  * $Id: R_Request_Process.sql,v 1.6 2003/05/26 22:31:17 jjanke Exp $
  ***
  * Title: Request Processing
  * Description:
  *   Copy Request into RequestAction
  *   Prepare for next action
  ************************************************************************/
  -- Logistice
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Result NUMBER:=1; -- 0=failure
  v_Record_ID VARCHAR2(32);
  v_AD_User_ID VARCHAR2(32);
  -- Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    -- Parameter Variables
    --
    v_NextNo VARCHAR2(32) ;
    v_Rows NUMBER(10) ;
    FINISH_PROCESS BOOLEAN:=false;

    rowcount NUMBER;
  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
        v_Record_ID:=Cur_Parameter.Record_ID;
        v_AD_User_ID:=Cur_Parameter.AD_User_ID;
      END LOOP; -- Get Parameter
    ELSE
      v_Record_ID:=p_R_Request_ID;
    END IF;
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
  BEGIN --BODY
    /**
    * Copy Request into RequestAction
    */
    AD_Sequence_Next('R_RequestAction', v_Record_ID, v_NextNo) ;
    INSERT
    INTO R_RequestAction
      (
        R_RequestAction_ID, R_Request_ID, AD_Client_ID, AD_Org_ID,
        IsActive, Created, CreatedBy, Updated,
        UpdatedBy, SalesRep_ID, ActionType, C_BPartner_ID,
        AD_User_ID, C_Order_ID, C_Invoice_ID, C_Debt_Payment_ID,
        M_Product_ID, C_Project_ID, R_MailText_ID, MailText,
        Result, DateNextAction, dateaction
      )
    SELECT v_NextNo, R_Request_ID, AD_Client_ID, AD_Org_ID,
      IsActive, now(), v_AD_User_ID, now(),
      v_AD_User_ID, SalesRep_ID, ActionType, C_BPartner_ID,
      AD_User_ID, C_Order_ID, C_Invoice_ID, C_Debt_Payment_ID,
      M_Product_ID, C_Project_ID, R_MailText_ID, MailText,
      Result, DateNextAction, now()
    FROM R_Request
    WHERE R_Request_ID=v_Record_ID;
    rowcount:=SQL%ROWCOUNT;
    v_Rows:=rowcount;
    v_Message:='@R_Request_ID@ @Updated@ ' || v_Rows;
    IF(v_Rows<>1) THEN
      v_Result:=0;
      FINISH_PROCESS:=true;
    END IF;
    IF(NOT FINISH_PROCESS) THEN
      -- Close Request
      UPDATE R_Request
        SET DateLastAction=Updated,
        LastResult=Result,
        DueType=R_Request_DueType(DateNextAction),
        Processed='Y',
        --
        Result=NULL,
        MailText=NULL,
        R_MailText_ID=NULL,
        ActionType=NULL
      WHERE R_Request_ID=v_Record_ID
        AND ActionType='X';
      rowcount:=SQL%ROWCOUNT;
      v_Rows:=rowcount;
      v_Message:='@R_Request_ID@ @Updated@ ' || v_Rows;
      IF(v_Rows=1) THEN
        FINISH_PROCESS:=true;
      END IF;
    END IF;--FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      /**
      * Prepare for next action
      */
      UPDATE R_Request
        SET DateLastAction=Updated,
        LastResult=Result,
        DueType=R_Request_DueType(DateNextAction),
        --
        Result=NULL,
        MailText=NULL,
        R_MailText_ID=NULL,
        ActionType=NULL
      WHERE R_Request_ID=v_Record_ID;
      rowcount:=SQL%ROWCOUNT;
      v_Rows:=rowcount;
      v_Message:='@R_Request_ID@ @Updated@ ' || v_Rows;
      IF(v_Rows<>1) THEN
        v_Result:=0;
      END IF;
    END IF;--FINISH_PROCESS
    --<<FINISH_PROCESS>>
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    IF(p_PInstance_ID IS NOT NULL) THEN
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', v_Result, v_Message) ;
    END IF;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  ROLLBACK;
  IF(p_PInstance_ID IS NOT NULL) THEN
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  END IF;
  RETURN;
END R_REQUEST_PROCESS
]]></body>
    </function>
  </database>