src-db/database/model/functions/M_UPDATE_PARETO_PRODUCT.xml
author Cristian Berner <cristian.berner@openbravo.com>
Mon, 31 Aug 2020 12:03:00 +0200
changeset 37516 743260848809
parent 29706 a8a6d8bd52e5
permissions -rw-r--r--
Fixes ISSUE-44863: Remove never thrown ServletException in OBScheduler methods

OBScheduler has been modified previously when quartz has been updated,
but reschedule and unschedule method declarations have not been updated.
ServletException is not thrown anymore in those methods, so it has been
removed from there.

Catch blocks that were catching this non-thrown exception have also been
removed in CallAcctServer and UnscheduleProcess classes.
<?xml version="1.0"?>
  <database name="FUNCTION M_UPDATE_PARETO_PRODUCT">
    <function name="M_UPDATE_PARETO_PRODUCT" type="NULL">
      <parameter name="p_pinstance_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_warehouse_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_org_id" type="VARCHAR" mode="in">
        <default/>
      </parameter>
      <parameter name="p_client_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-2016 Openbravo SLU
* All Rights Reserved.
* Contributor(s):  ______________________________________.
************************************************************************/

  v_ResultStr VARCHAR2(2000):='';
  v_Result NUMBER:= 1;
  v_Message VARCHAR2(2000):='';
  v_exist NUMBER:=0;
  v_M_Product_Org_ID VARCHAR2(32);
  v_warehouse_ID VARCHAR2(32);
  v_org_ID VARCHAR2(32);
  v_client_ID VARCHAR2(32);
  v_count_update NUMBER:= 0;
  v_count_insert NUMBER:= 0;
    v_currency VARCHAR2(32);
  TYPE RECORD IS REF CURSOR;
  Cur_Cursor RECORD;
  Cur_Parameter RECORD;
  v_totalCost 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, i.UpdatedBy
                          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
      IF (Cur_Parameter.ParameterName = 'm_warehouse_id') THEN
        v_warehouse_ID := Cur_Parameter.P_String;
        DBMS_OUTPUT.PUT_LINE('  m_warehouse_id=' || v_warehouse_ID) ;
      ELSIF (Cur_Parameter.ParameterName = 'ad_org_id') THEN
        v_org_ID := Cur_Parameter.P_String;
        DBMS_OUTPUT.PUT_LINE('  ad_org_id=' || v_org_ID) ;
      ELSIF (Cur_Parameter.ParameterName = 'ad_client_id') THEN
        v_client_ID := Cur_Parameter.P_String;
        DBMS_OUTPUT.PUT_LINE('  ad_client_id=' || v_client_ID) ;
      END IF;
    END LOOP; --Get Parameter

  ELSE
    DBMS_OUTPUT.PUT_LINE('--<<M_UPDATE_PARETO_PRODUCT>>') ;
    v_warehouse_ID:=p_warehouse_ID;
    v_org_ID:=p_org_ID;
    v_client_ID := p_client_id;
  END IF;
BEGIN --BODY

  SELECT C_CURRENCY_ID INTO v_currency
  FROM AD_CLIENT
  WHERE AD_CLIENT_ID = v_client_ID;
    
  SELECT COALESCE(C_CURRENCY_ID, v_currency) INTO v_currency
  FROM AD_ORG
  WHERE AD_ORG_ID = (AD_GET_ORG_LE_BU (v_org_ID, 'LE'));

  -- Checks if Sum of all Product's cost are zero
  SELECT SUM(COST_PER_CURRENCY) INTO v_totalCost
  FROM (
    SELECT C_CURRENCY_CONVERT_PRECISION(SUM(CASE WHEN T.MOVEMENTQTY>=0 THEN TC.COST ELSE -TC.COST END),
              TC.C_CURRENCY_ID, v_currency, now(), NULL,v_client_ID, AD_GET_ORG_LE_BU (v_org_ID, 'LE')) AS COST_PER_CURRENCY
    FROM M_TRANSACTION_COST TC, M_TRANSACTION T
      LEFT JOIN M_LOCATOR L ON (T.M_LOCATOR_ID=L.M_LOCATOR_ID)
      LEFT JOIN M_WAREHOUSE W ON (L.M_WAREHOUSE_ID=W.M_WAREHOUSE_ID)
    WHERE TC.M_TRANSACTION_ID = T.M_TRANSACTION_ID
      AND T.ISCOSTCALCULATED = 'Y'
      AND T.TRANSACTIONCOST IS NOT NULL
      AND (v_warehouse_ID IS NULL OR L.M_WAREHOUSE_ID = v_warehouse_ID)
      AND ad_isorgincluded(W.AD_ORG_ID, v_org_ID, T.AD_CLIENT_ID) <> -1
      AND (v_client_ID IS NULL OR T.AD_CLIENT_ID = v_client_ID)
    GROUP BY TC.C_CURRENCY_ID
    HAVING SUM(T.MOVEMENTQTY) > 0
  ) A;
  IF (v_totalCost = 0) THEN
    RAISE_APPLICATION_ERROR(-20000, '@ZeroSumCost@');
  END IF;

  FOR Cur_Cursor IN (
      SELECT M_GET_PARETO_ABC(v_warehouse_ID, v_org_ID, v_client_ID, PERCENTAGE, v_currency) AS ISABC,
          AD_ORG_ID, AD_CLIENT_ID, M_PRODUCT_ID
      FROM (
          SELECT 100*(SUM(COST_PER_CURRENCY) / v_totalCost) AS PERCENTAGE, AD_ORG_ID, AD_CLIENT_ID, M_PRODUCT_ID
          FROM (
              SELECT C_CURRENCY_CONVERT_PRECISION(SUM(CASE WHEN T.MOVEMENTQTY>=0 THEN TC.COST ELSE -TC.COST END),
                    TC.C_CURRENCY_ID, v_currency, now(), NULL, v_client_ID, AD_GET_ORG_LE_BU (v_org_ID, 'LE')) AS COST_PER_CURRENCY,
                    T.M_PRODUCT_ID, T.AD_ORG_ID, T.AD_CLIENT_ID
              FROM M_TRANSACTION_COST TC, M_TRANSACTION T
                LEFT JOIN M_LOCATOR L ON (T.M_LOCATOR_ID=L.M_LOCATOR_ID)
                LEFT JOIN M_WAREHOUSE W ON (L.M_WAREHOUSE_ID=W.M_WAREHOUSE_ID)
              WHERE TC.M_TRANSACTION_ID = T.M_TRANSACTION_ID
                AND T.ISCOSTCALCULATED = 'Y'
                AND T.TRANSACTIONCOST IS NOT NULL
                AND (v_warehouse_ID IS NULL OR L.M_WAREHOUSE_ID = v_warehouse_ID)
                AND ad_isorgincluded(W.AD_ORG_ID, v_org_ID, T.AD_CLIENT_ID) <> -1
                AND (v_client_ID IS NULL OR T.AD_CLIENT_ID = v_client_ID)
              GROUP BY T.M_PRODUCT_ID, TC.C_CURRENCY_ID, T.AD_ORG_ID, T.AD_CLIENT_ID
              HAVING SUM(T.MOVEMENTQTY) > 0
          ) A
          GROUP BY M_PRODUCT_ID, AD_ORG_ID, AD_CLIENT_ID
          ORDER BY PERCENTAGE DESC
      ) B
  ) LOOP

    SELECT COUNT(*)
      INTO v_exist
    FROM M_PRODUCT_ORG
    WHERE M_PRODUCT_ID = Cur_Cursor.M_PRODUCT_ID
      AND AD_ORG_ID = Cur_Cursor.AD_ORG_ID;

    IF (v_exist > 0) THEN
      UPDATE M_PRODUCT_ORG
      SET ABC = Cur_Cursor.ISABC
      WHERE M_PRODUCT_ID = Cur_Cursor.M_PRODUCT_ID
        AND AD_ORG_ID = Cur_Cursor.AD_ORG_ID;
      v_count_update := v_count_update + 1;
    ELSE
      AD_Sequence_Next('M_Product_Org', Cur_Cursor.AD_CLIENT_ID, v_M_Product_Org_ID) ;
      INSERT INTO M_PRODUCT_ORG (
          M_PRODUCT_ORG_ID, AD_CLIENT_ID, AD_ORG_ID, ISACTIVE,
          CREATED, CREATEDBY, UPDATED, UPDATEDBY,
          M_PRODUCT_ID, ABC
      ) VALUES (
          v_M_Product_Org_ID, Cur_Cursor.AD_CLIENT_ID, Cur_Cursor.AD_ORG_ID,
          'Y', now(), '100', now(), '100',
          Cur_Cursor.M_PRODUCT_ID, Cur_Cursor.ISABC
      );
      v_count_insert := v_count_insert + 1;
    END IF;

  END LOOP;
  v_Message:='@Created@=' || v_count_insert || ', @Updated@=' || v_count_update;
--<<FINISH_PROCESS>>
  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', v_Result, v_Message) ;
  ELSE
    DBMS_OUTPUT.PUT_LINE('--<<M_UPDATE_PARETO_PRODUCT finished>>') ;
  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
    RAISE;
  END IF;
  RETURN;
END M_UPDATE_PARETO_PRODUCT
]]></body>
    </function>
  </database>