database/model/functions/M_INOUT_UPDATEQUANTITYES.xml
author Gorka Ion Damián <gorkaion.damian@openbravo.com>
Fri, 14 Dec 2007 16:21:33 +0000
changeset 239 a30acae847e1
parent 170 4b5493776a7f
child 735 daced7e311c9
permissions -rw-r--r--
Added CDATA to views, functions and triggers.
<?xml version="1.0"?>
  <database name="FUNCTION M_INOUT_UPDATEQUANTITYES">
    <function name="M_INOUT_UPDATEQUANTITYES" type="NULL">
      <parameter name="p_PInstance_ID" type="NUMERIC" mode="in"/>
      <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):  ______________________________________.
************************************************************************/
  v_ResultStr VARCHAR2(2000):='';
  v_Message VARCHAR2(2000):='';
  v_Record_ID NUMBER;
  v_Result NUMBER:=0; -- failure
  v_AD_User_ID NUMBER;
  v_AD_Org_ID NUMBER;
  v_AD_Client_ID NUMBER;
  -- Parameter
  TYPE RECORD IS REF CURSOR;
    Cur_Parameter RECORD;
    -- Parameter Variables
    v_M_InOut_ID NUMBER;
    v_Total NUMBER;
    FINISH_PROCESS BOOLEAN:=false;
  BEGIN
    --  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) ;
  BEGIN --BODY
    -- 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_AD_User_ID:=Cur_Parameter.AD_User_ID;
      v_AD_Org_ID:=Cur_Parameter.AD_Org_ID;
      v_AD_Client_ID:=Cur_Parameter.AD_Client_ID;
      IF(Cur_Parameter.ParameterName='M_InOut_ID') THEN
        v_M_InOut_ID:=Cur_Parameter.P_Number;
        DBMS_OUTPUT.PUT_LINE('  M_InOut_ID=' || v_M_InOut_ID) ;
      ELSE
        DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
      END IF;
    END LOOP; -- Get Parameter
    DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
    v_ResultStr:='CheckRestriction';
    SELECT COUNT(*)
    INTO v_Total
    FROM
      (SELECT M_PRODUCT_ID,
        C_UOM_ID,
        ROUND(MOVEMENTQTY, 2),
        COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
        COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
        COUNT(*)
      FROM M_INOUTLINE
      WHERE M_INOUT_ID=v_M_InOut_ID
        AND ISACTIVE='Y'
      GROUP BY M_PRODUCT_ID,
        C_UOM_ID,
        MOVEMENTQTY,
        COALESCE(M_PRODUCT_UOM_ID, 0),
        COALESCE(QUANTITYORDER, 0) MINUS
      SELECT M_PRODUCT_ID,
        C_UOM_ID,
        ROUND(MOVEMENTQTY, 2),
        COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
        COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
        COUNT(*)
      FROM M_INOUTLINE
      WHERE M_INOUT_ID=v_Record_ID
        AND ISACTIVE='Y'
      GROUP BY M_PRODUCT_ID,
        C_UOM_ID,
        MOVEMENTQTY,
        COALESCE(M_PRODUCT_UOM_ID, 0),
        COALESCE(QUANTITYORDER, 0)
      )
      A;
    IF(v_Total>0) THEN
      v_Message:='LinesNotMatch';
      v_Result:=0;
      FINISH_PROCESS:=true;
    END IF;
    IF(NOT FINISH_PROCESS) THEN
      v_ResultStr:='InsertingLines';
      DECLARE
        Cur_Lines RECORD;
        v_Lines VARCHAR2(4000):='';
        v_Lines_Aux NUMBER(10) ;
        v_StrSQL VARCHAR2(4000):='';
      BEGIN
        FOR Cur_Lines IN
          (SELECT M_PRODUCT_ID,
            C_UOM_ID,
            ROUND(MOVEMENTQTY, 2) AS MOVEMENTQTY,
            COALESCE(M_PRODUCT_UOM_ID, 0) AS M_PRODUCT_UOM_ID,
            COALESCE(QUANTITYORDER, 0) AS QUANTITYORDER,
            M_ATTRIBUTESETINSTANCE_ID
          FROM M_INOUTLINE
          WHERE M_INOUT_ID=v_M_InOut_ID
            AND ISACTIVE='Y'
          )
        LOOP
          DBMS_OUTPUT.PUT_LINE('SQL: ' || Cur_Lines.M_Product_ID) ;
          v_StrSQL:='SELECT MIN(M_INOUTLINE_ID) FROM M_INOUTLINE ';
          v_StrSQL:=v_StrSQL || 'WHERE M_INOUT_ID = ' || v_Record_ID || ' ';
          v_StrSQL:=v_StrSQL || 'AND ISACTIVE = ''Y'' ';
          v_StrSQL:=v_StrSQL || 'AND M_PRODUCT_ID = ' || Cur_Lines.M_PRODUCT_ID || ' ';
          v_StrSQL:=v_StrSQL || 'AND C_UOM_ID = ' || Cur_Lines.C_UOM_ID || ' ';
          v_StrSQL:=v_StrSQL || 'AND ROUND(MOVEMENTQTY, 2) = TO_NUMBER(''' || Cur_Lines.MOVEMENTQTY || ''') ';
          v_StrSQL:=v_StrSQL || 'AND COALESCE(M_PRODUCT_UOM_ID, 0) = ' || Cur_Lines.M_PRODUCT_UOM_ID || ' ';
          v_StrSQL:=v_StrSQL || 'AND COALESCE(QUANTITYORDER, 0) = ' || Cur_Lines.QUANTITYORDER || ' ';
          IF v_Lines IS NOT NULL THEN
            v_StrSQL:=v_StrSQL || 'AND M_INOUTLINE_ID NOT IN (' || v_Lines || ') ';
          END IF;
          EXECUTE IMMEDIATE v_StrSQL INTO v_Lines_Aux;
          IF v_Lines IS NULL THEN
            v_Lines:=v_Lines_Aux;
          ELSE
            v_Lines:=v_Lines || ', ' || v_Lines_Aux;
          END IF;
          DBMS_OUTPUT.PUT('LINES EXECUTED: ' || v_Lines || ' - ACTUAL LINE: ' || v_Lines_Aux) ;
          UPDATE M_INOUTLINE
            SET M_ATTRIBUTESETINSTANCE_ID=Cur_Lines.M_ATTRIBUTESETINSTANCE_ID
          WHERE M_INOUTLINE.M_INOUTLINE_ID=v_Lines_Aux;
        END LOOP;
      END;
    END IF;----<<FINISH_PROCESS>>
    --  Update AD_PInstance
    DBMS_OUTPUT.PUT_LINE('Updating PInstance - Finished ' || v_Message) ;
    AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 1, v_Message) ;
    RETURN;
  END; --BODY
EXCEPTION
WHEN OTHERS THEN
  v_ResultStr:= '@ERROR=' || SQLERRM;
  DBMS_OUTPUT.PUT_LINE(v_ResultStr) ;
  ROLLBACK;
  AD_UPDATE_PINSTANCE(p_PInstance_ID, NULL, 'N', 0, v_ResultStr) ;
  RETURN;
END M_InOut_UpdateQuantityes


]]></body>
    </function>
  </database>