src-db/database/model/functions/MA_WORKREQUIREMENT_PROCESS.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_WORKREQUIREMENT_PROCESS">
    <function name="MA_WORKREQUIREMENT_PROCESS" type="NULL">
      <parameter name="p_pinstance_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):='';
  --  Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    --
    v_Record_ID NUMBER:=NULL;
    v_User_ID NUMBER;
    v_Org_ID NUMBER;
    v_Client_ID NUMBER;
    v_WrPhase_ID NUMBER;
    v_WrPhaseProd_ID NUMBER;
    --  Sequences to process
    Cur_Sequence RECORD;
    --
    v_ProcessPlan_ID NUMBER;
    v_ExplodePhases CHAR;
    v_Quantity NUMBER;
    v_Multiplier NUMBER;
    v_Processed CHAR;
    v_LaunchDate DATE;
    -- WRPhases to process
    Cur_WRPhase RECORD;
    --  Sequence Products to process
    Cur_SeqProduct RECORD;
    --ptr_sp  Cur_SeqProduct%ROWTYPE;
    FINISH_PROCESS BOOLEAN:=false;
    v_DBA_ErrorLog_ID NUMBER;
  BEGIN
    --  Process Parameters
    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,
          i.AD_Org_ID,
          i.AD_Client_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_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
    ELSE
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkRequirement_Process>>') ;
    END IF;
  BEGIN --BODY
    -- Check if all the required fields are filled
    v_ResultStr:='CheckingArguments';
    SELECT MA_ProcessPlan_ID,
      Quantity,
      Processed,
      ExplodePhases,
      LaunchDate
    INTO v_ProcessPlan_ID,
      v_Quantity,
      v_Processed,
      v_ExplodePhases,
      v_LaunchDate
    FROM MA_WorkRequirement
    WHERE MA_WorkRequirement_ID=v_Record_ID;
    --Check if it hasn't already been processed
    IF v_Processed='Y' THEN
      v_ResultStr:='Work Requirement is Processed';
      DBMS_OUTPUT.PUT_LINE('Work Requirement is already Processed') ;
      v_Message:='@WorkRequirementProcessed@';
      FINISH_PROCESS:=true;
    END IF;
    IF(NOT FINISH_PROCESS) THEN
      IF(v_ProcessPlan_ID IS NULL OR v_Quantity IS NULL) THEN
        DBMS_OUTPUT.PUT_LINE('Faltan argumentos') ;
        FINISH_PROCESS:=true;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      IF(v_ExplodePhases='Y') THEN
        -- Data insertion begins
        FOR Cur_Sequence IN
          (SELECT s.MA_Sequence_ID,
            s.MA_Process_ID,
            s.Name,
            s.CostCenterUse,
            s.Preptime,
            s.Multiplier,
            s.SeqNo,
            s.NoQty,
            s.GroupUse,
            s.Outsourced
          FROM MA_Sequence s,
            MA_ProcessPlan_Version p
          WHERE s.MA_PROCESSPLAN_VERSION_ID=p.MA_PROCESSPLAN_VERSION_ID
            AND p.MA_ProcessPlan_ID=v_ProcessPlan_ID
            AND p.DATEFROM<=v_LaunchDate
            AND p.DATETO>v_LaunchDate
          )
        LOOP
          v_ResultStr:='InsertPhase';
          Ad_Sequence_Next('MA_WRPhase', v_Client_ID, v_WrPhase_ID) ;
          v_Multiplier:=Cur_Sequence.Multiplier;
          --OPEN Cur_SeqProduct (Cur_Sequence.MA_Sequence_ID);
          --FETCH Cur_SeqProduct INTO ptr_sp;
          INSERT
          INTO MA_WRPHASE
            (
              MA_Wrphase_ID, MA_Workrequirement_ID, MA_Sequence_ID, AD_Client_ID,
              AD_Org_ID, Isactive, Created, Createdby,
              Updated, Updatedby, MA_Process_ID, CostCenterUse,
              Preptime, Quantity, Donequantity,
              SeqNo, Closed, NoQty, GroupUse, Outsourced
            )
            VALUES
            (
              v_WrPhase_ID, v_Record_ID, Cur_Sequence.MA_Sequence_ID, v_Client_ID,
              v_Org_ID, 'Y', now(), v_User_ID,
              now(), v_User_ID, Cur_Sequence.MA_Process_ID, Cur_Sequence.CostCenterUse,
              Cur_Sequence.Preptime, v_Multiplier*v_Quantity, 0,
              Cur_Sequence.SeqNo, 'N', Cur_Sequence.NoQty, Cur_Sequence.GroupUse, Cur_Sequence.Outsourced
            )
            ;
          FOR Cur_SeqProduct IN
            (SELECT *
            FROM MA_SequenceProduct
            WHERE MA_Sequence_ID=Cur_Sequence.MA_Sequence_ID
            )
          LOOP
            v_ResultStr:='InsertProduct';
            Ad_Sequence_Next('MA_WRPhaseProduct', v_Client_ID, v_WrPhaseProd_ID) ;
            DBMS_OUTPUT.PUT_LINE('Valores:' || v_WrPhaseProd_ID ||' '|| v_WrPhase_ID||' '|| v_Client_ID||' '|| v_Org_ID||' '|| v_User_ID||' '|| Cur_SeqProduct.M_Product_ID||' '|| Cur_SeqProduct.Productiontype||' '|| Cur_SeqProduct.Quantity) ;
            v_ResultStr:='InsertProduct2';
            INSERT
            INTO MA_WRPHASEPRODUCT
              (
                MA_Wrphaseproduct_ID, MA_Wrphase_ID, AD_Client_ID, AD_Org_ID,
                Isactive, Created, Createdby, Updated,
                Updatedby, M_Product_ID, Productiontype, MovementQty,
                ComponentCost, C_UOM_ID,
                quantityorder, M_Product_UOM_ID
              )
              VALUES
              (
                v_WrPhaseProd_ID, v_WrPhase_ID, v_Client_ID, v_Org_ID,
                 'Y', now(), v_User_ID, now(),
                v_User_ID, Cur_SeqProduct.M_Product_ID, Cur_SeqProduct.Productiontype, Cur_SeqProduct.Quantity,
                Cur_SeqProduct.ComponentCost, Cur_SeqProduct.C_UOM_ID,
                Cur_SeqProduct.quantityorder, Cur_SeqProduct.M_Product_UOM_ID
              )
              ;
          END LOOP;
        END LOOP;
      ELSE
        -- Data insertion begins
        FOR Cur_WRPhase IN
          (SELECT MA_WRPhase_ID,
            MA_Sequence_ID,
            quantity
          FROM MA_WRPhase
          WHERE MA_WorkRequirement_ID=v_Record_ID
          )
        LOOP
          FOR Cur_SeqProduct IN
            (SELECT *
            FROM MA_SequenceProduct
            WHERE MA_Sequence_ID=Cur_WRPhase.MA_Sequence_ID
            )
          LOOP
            v_ResultStr:='NInsertProduct';
            Ad_Sequence_Next('MA_WRPhaseProduct', v_Client_ID, v_WrPhaseProd_ID) ;
            --DBMS_OUTPUT.PUT_LINE('Valores:' || v_WrPhaseProd_ID ||' '|| v_WrPhase_ID||' '|| v_Client_ID||' '|| v_Org_ID||' '|| v_User_ID||' '|| Cur_SeqProduct.Name||' '|| Cur_SeqProduct.M_Product_ID||' '|| Cur_SeqProduct.Productiontype||' '|| Cur_SeqProduct.Quantity);
            v_ResultStr:='NInsertProduct2';
            INSERT
            INTO MA_WRPHASEPRODUCT
              (
                MA_Wrphaseproduct_ID, MA_Wrphase_ID, AD_Client_ID, AD_Org_ID,
                Isactive, Created, Createdby, Updated,
                Updatedby, M_Product_ID, Productiontype, MovementQty,
                ComponentCost, C_UOM_ID,
                QUANTITYORDER, M_PRODUCT_UOM_ID
              )
              VALUES
              (
                v_WrPhaseProd_ID, Cur_WRPhase.MA_WrPhase_ID, v_Client_ID, v_Org_ID,
                 'Y', now(), v_User_ID, now(),
                v_User_ID, Cur_SeqProduct.M_Product_ID, Cur_SeqProduct.Productiontype, Cur_SeqProduct.Quantity,
                Cur_SeqProduct.ComponentCost, Cur_SeqProduct.C_UOM_ID,
                Cur_SeqProduct.quantityorder, Cur_SeqProduct.M_Product_UOM_ID
              )
              ;
          END LOOP;
        END LOOP;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      UPDATE MA_WorkRequirement
        SET Processed='Y'
      WHERE MA_WorkRequirement_ID=v_Record_ID;
    END IF; --FINISH_PROCESS
    --<<FINISH_PROCESS>>
    --v_Message := v_Message || '@Created@: ' || v_NoRecords;
    IF(p_PInstance_ID IS NOT NULL) THEN
      --  Update AD_PInstance
      DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
      AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
    ELSE
      DBMS_OUTPUT.PUT_LINE('--<<MA_WorkRequirement_Process 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
    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_WorkRequirement_Post'
        )
        ;
    END;
  END IF;
  --p_Invoice_ID := 0;        --  Error Indicator
  RETURN;
END MA_WORKREQUIREMENT_PROCESS
]]></body>
    </function>
  </database>