src-db/database/model/functions/MA_MEASURE_TIME_EXPLODE.xml
author Asier Lostalé <asier.lostale@openbravo.com>
Mon, 05 May 2008 06:59:24 +0000
changeset 799 fef2c5e2feb7
parent 756 ae11e4610537
child 1178 9d1c8b3fd305
permissions -rw-r--r--
Merged cleanup branch (r3931) with trunk
<?xml version="1.0"?>
  <database name="FUNCTION MA_MEASURE_TIME_EXPLODE">
    <function name="MA_MEASURE_TIME_EXPLODE" type="NULL">
      <parameter name="p_pinstance_id" type="NUMERIC" mode="in">
        <default/>
      </parameter>
      <parameter name="p_measure_time_id" type="NUMERIC" mode="in">
        <default/>
      </parameter>
      <body><![CDATA[/*************************************************************************
* The contents of this file are subject to the Openbravo  Public  License
* Version  1.0  (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 SL
* All portions are Copyright (C) 2001-2006 Openbravo SL
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/
  --  Logistice
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Record_ID NUMBER;
  v_Result NUMBER:=1; --  Success
  --  Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    --  Record Info
    v_Client_ID NUMBER;
    v_Org_ID NUMBER;
    v_User_ID NUMBER;
    v_CCP_Group NUMBER;
    v_MeasureQty NUMBER;
    v_Line NUMBER;
    v_Processed CHAR;
    Cur_CCP RECORD;
    FINISH_PROCESS BOOLEAN:=false;
    v_NextNo NUMBER;
    v_DBA_ErrorLog_ID NUMBER;
  BEGIN
    --p_Result := 1;
    --  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,
        i.AD_Org_ID,
        i.AD_Client_ID
      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_User_ID:=Cur_Parameter.AD_User_ID;
      v_Org_ID:=Cur_Parameter.AD_Org_ID;
      v_Client_ID:=Cur_Parameter.AD_Client_ID;
    END LOOP; --  Get Parameter
    DBMS_OUTPUT.PUT_LINE('  v_Record_ID=' || v_Record_ID) ;
    IF(p_Measure_Time_ID IS 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) ;
    ELSE
      DBMS_OUTPUT.PUT_LINE('--<<MA_GlobalUse_Distribute>>') ;
      v_Record_ID:=p_Measure_Time_ID;
    END IF;
  BEGIN --BODY
    SELECT processed
    INTO v_Processed
    FROM MA_Measure_Time
    WHERE MA_Measure_Time_ID=v_Record_ID;
    IF v_Processed='Y' THEN
      v_ResultStr:='Document is Processed';
      DBMS_OUTPUT.PUT_LINE('Measure Time is already Processed') ;
      v_Message:='@MeasureTimeProcessed@';
      RAISE_APPLICATION_ERROR(-20000, v_Message);
    END IF;
    IF(NOT FINISH_PROCESS) THEN
      v_ResultStr:='Inserting Measure Values';
      v_Line:=0;
      FOR Cur_CCP IN
        (SELECT c.*
        FROM MA_CCP c,
          MA_Measure_Group g,
          MA_Measure_Time t
        WHERE t.MA_MEASURE_GROUP_ID=g.MA_MEASURE_GROUP_ID
          AND g.MA_CCP_GROUP_ID=c.MA_CCP_GROUP_ID
          AND t.MA_MEASURE_TIME_ID=v_Record_ID
        ORDER BY c.SEQNO
        )
      LOOP
        v_MeasureQty:=0;
        WHILE v_MeasureQty<Cur_CCP.VALUENUMBER
        LOOP
          v_Line:=v_Line + 10;
          Ad_Sequence_Next('MA_Measure_Values', v_Org_ID, v_NextNo);
          INSERT
          INTO MA_MEASURE_VALUES
            (
              MA_Measure_Values_ID, AD_Client_ID, AD_Org_ID, Isactive,
              Created, Createdby, Updated, Updatedby,
              MA_Measure_Time_ID, MA_Ccp_ID, Valuetype, SeqNo
            )
            VALUES
            (
              v_NextNo, v_Client_ID, v_Org_ID, 'Y',
              now(), v_User_ID, now(), V_User_ID,
              v_Record_ID, Cur_CCP.MA_Ccp_ID, Cur_CCP.VALUETYPE, v_Line
            )
            ;
          v_MeasureQty:=v_MeasureQty+1;
        END LOOP;
      END LOOP;
      v_ResultStr:='Update Processed Time';
      UPDATE MA_Measure_Time
        SET Processed='Y'
      WHERE MA_Measure_Time_ID=v_Record_ID;
    END IF; --FINISH_PROCESS
    --<<FINISH_PROCESS>>
    IF(p_Measure_Time_ID IS NULL) THEN
      --  Update AD_PInstance
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished - ' || v_Message) ;
      AD_UPDATE_PINSTANCE(p_PInstance_ID, v_User_ID, 'N', v_Result, v_Message) ;
    ELSE
      DBMS_OUTPUT.PUT_LINE('--<<MA_Measure_Time_Explode finished>> ' || v_Message) ;
    END IF;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  IF(p_Measure_Time_ID IS NULL) THEN
    v_ResultStr:= '@ERROR=' || SQLERRM;
    DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
    ROLLBACK;
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  ELSE
    DECLARE
      v_Code NUMBER:=SQLCODE;
    BEGIN
      AD_SEQUENCE_NEXT('DBA_ERRORLOG', v_Client_ID, v_DBA_ErrorLog_ID);
      INSERT
      INTO DBA_ERRORLOG
        (
          DBA_ErrorLog_ID, Created,
          Code, Msg,
          Info
        )
        VALUES
        (
          v_DBA_ErrorLog_ID, now(),
          v_Code, v_ResultStr,
           'MA_GlobalUse_Distribute'
        )
        ;
    END;
    RAISE;
  END IF;
END MA_MEASURE_TIME_EXPLODE
]]></body>
    </function>
  </database>