src-db/database/model/functions/MA_WORKREQUIREMENT_PROCESS.xml
author Cristian Berner <cristian.berner@openbravo.com>
Wed, 18 Sep 2019 16:50:13 +0200
changeset 36526 9030f6f7e659
parent 33169 3d2566d2bf35
permissions -rw-r--r--
fixes issue 41498: Removes obsolete WAD Implementation field from ReferenceView

Additionally it sets WAD_Impl column development status in ADReference table to Deprecated.
<?xml version="1.0"?>
  <database name="FUNCTION MA_WORKREQUIREMENT_PROCESS">
    <function name="MA_WORKREQUIREMENT_PROCESS" type="NULL">
      <parameter name="p_pinstance_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) 2001-2014 Openbravo SLU
* 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 VARCHAR2(32):=NULL;
    v_User_ID VARCHAR2(32);
    v_Org_ID VARCHAR2(32);
    v_Client_ID VARCHAR2(32);
    v_WrPhase_ID VARCHAR2(32);
    v_WrPhaseProd_ID VARCHAR2(32);
    --  Sequences to process
    Cur_Sequence RECORD;
    --
    v_ProcessPlan_ID VARCHAR2(32);
    v_ExplodePhases CHAR;
    v_Quantity NUMBER;
    v_Multiplier NUMBER;
    v_Processed CHAR;
    v_LaunchDate DATE;
    v_Startdate DATE;
    -- WRPhases to process
    Cur_WRPhase RECORD;
    --  Sequence Products to process
    Cur_SeqProduct RECORD;
    --ptr_sp  Cur_SeqProduct%ROWTYPE;
    FINISH_PROCESS BOOLEAN:=false;
    v_count 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_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,
      AD_Org_ID,
      Startdate
    INTO v_ProcessPlan_ID,
      v_Quantity,
      v_Processed,
      v_ExplodePhases,
      v_LaunchDate,
      v_Org_ID,
      v_Startdate
    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,
            s.Estimatedtime,
            s.Usedmaterial
          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
            AND s.ISACTIVE='Y'
            AND p.ISACTIVE='Y'
          )
        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,
              Startdate, Enddate, Estimatedtime, Usedmaterial
            )
            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,
              v_Startdate, ADD_HMS(v_Startdate, v_Multiplier*v_Quantity*Cur_Sequence.Estimatedtime, 0 , 0), v_Multiplier*v_Quantity*Cur_Sequence.Estimatedtime, Cur_Sequence.Usedmaterial
            )
            ;
          FOR Cur_SeqProduct IN
            (SELECT *
            FROM MA_SequenceProduct
            WHERE MA_Sequence_ID=Cur_Sequence.MA_Sequence_ID AND productiontype in ('+','-')
            )
          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, MA_SequenceProduct_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, Cur_SeqProduct.MA_SequenceProduct_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 AND productiontype in ('+','-')
            )
          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, MA_SequenceProduct_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, Cur_SeqProduct.MA_SequenceProduct_ID
              )
              ;
          END LOOP;
        END LOOP;
      END IF;
    END IF; --FINISH_PROCESS
    IF(NOT FINISH_PROCESS) THEN
      UPDATE MA_WorkRequirement
        SET Processed='Y', updated = now(), updatedby = v_User_ID
      WHERE MA_WorkRequirement_ID=v_Record_ID;
    END IF; --FINISH_PROCESS
    
	--MA_Workrequirement_Process - Finish Process Extension Point
	SELECT count(*) INTO v_count
	FROM DUAL
	where exists (select 1 from ad_ep_procedures where ad_extension_points_id = 'FF80818132FDD74F0132FDE095B10019');
	IF (v_count=1) THEN
	  DECLARE
	    v_ep_instance VARCHAR2(32);
	    v_extension_point_id VARCHAR2(32) := 'FF80818132FDD74F0132FDE095B10019';
	  BEGIN
	    v_ep_instance := get_uuid();
	    AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Record_ID',
	      v_Record_ID, NULL, NULL, NULL, NULL, NULL, NULL);
	    AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'User',
	      v_User_ID, NULL, NULL, NULL, NULL, NULL, NULL);
	    AD_EP_INSTANCE_PARA_INSERT(v_ep_instance, v_extension_point_id, 'Message',
	      NULL, NULL, NULL, NULL, NULL, NULL, v_Message);
	    AD_EXTENSION_POINT_HANDLER(v_ep_instance, v_extension_point_id);
	    SELECT p_text INTO v_Message
	    FROM ad_ep_instance_para
	    WHERE ad_ep_instance_id = v_ep_instance
	    AND parametername LIKE 'Message';
		  
	    DELETE FROM ad_ep_instance_para
	    WHERE ad_ep_instance_id = v_ep_instance;
	  END;
	END IF;
    --<<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) ;
  END IF;
  --p_Invoice_ID := 0;        --  Error Indicator
  RETURN;
END MA_WORKREQUIREMENT_PROCESS
]]></body>
    </function>
  </database>