src-db/database/model/functions/C_GET_SERVICE_AMOUNT.xml
author Unai Martirena <unai.martirena@openbravo.com>
Mon, 18 Apr 2016 18:51:42 +0200
changeset 29855 956cd99a1937
parent 28634 b7ee95444dc0
child 29873 73a8cd831ca4
permissions -rw-r--r--
Fixes issue 32697: Applied Product Services Code Review
eduardo@27017
     1
<?xml version="1.0"?>
eduardo@27017
     2
  <database name="FUNCTION C_GET_SERVICE_AMOUNT">
eduardo@27017
     3
    <function name="C_GET_SERVICE_AMOUNT" type="NUMERIC">
eduardo@27017
     4
      <parameter name="v_c_orderline_id" type="VARCHAR" mode="in">
eduardo@27017
     5
        <default/>
eduardo@27017
     6
      </parameter>
eduardo@27017
     7
      <body><![CDATA[/*************************************************************************
eduardo@27017
     8
* The contents of this file are subject to the Openbravo  Public  License
eduardo@27017
     9
* Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
eduardo@27017
    10
* Version 1.1  with a permitted attribution clause; you may not  use this
eduardo@27017
    11
* file except in compliance with the License. You  may  obtain  a copy of
eduardo@27017
    12
* the License at http://www.openbravo.com/legal/license.html
eduardo@27017
    13
* Software distributed under the License  is  distributed  on  an "AS IS"
eduardo@27017
    14
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
eduardo@27017
    15
* License for the specific  language  governing  rights  and  limitations
eduardo@27017
    16
* under the License.
eduardo@27017
    17
* The Original Code is Openbravo ERP.
eduardo@27017
    18
* The Initial Developer of the Original Code is Openbravo SLU
eduardo@27017
    19
* All portions are Copyright (C) 2015 Openbravo SLU
eduardo@27017
    20
* All Rights Reserved.
eduardo@27017
    21
* Contributor(s):  ______________________________________.
eduardo@27017
    22
************************************************************************/
eduardo@27017
    23
  v_TotalAmount NUMBER;
eduardo@27017
    24
  v_TotalQantity NUMBER;
eduardo@27017
    25
  v_IsPriceRuleBased M_PRODUCT.ISPRICERULEBASED%TYPE;
eduardo@27017
    26
  v_M_Product_ID M_PRODUCT.M_PRODUCT_ID%TYPE;
eduardo@27017
    27
  v_Dateordered C_ORDER.dateordered%TYPE;
eduardo@27017
    28
  v_stdPrecision C_CURRENCY.STDPRECISION%TYPE;
eduardo@27017
    29
  v_PriceList m_productprice.pricelist%type;
eduardo@27017
    30
  v_OrderPricelist C_ORDER.M_PRICELIST_ID%TYPE;
eduardo@28634
    31
  -- Parameter
eduardo@28634
    32
  TYPE RECORD IS REF CURSOR;
eduardo@27017
    33
  Cur_Rule RECORD;
eduardo@27017
    34
  Cur_Range RECORD;
eduardo@27017
    35
  BEGIN
unai@29855
    36
    SELECT SUM(AMOUNT), SUM(QUANTITY), ISPRICERULEBASED, M_PRODUCT.M_PRODUCT_ID, C_ORDER.DATEORDERED, STDPRECISION, C_ORDER.M_PRICELIST_ID
unai@29855
    37
      INTO v_TotalAmount, v_TotalQantity, v_IsPriceRuleBased, v_M_Product_ID,  v_Dateordered, v_stdPrecision, v_OrderPricelist
unai@29855
    38
    FROM C_ORDERLINE_SERVICERELATION, C_ORDERLINE, M_PRODUCT, C_ORDER, C_CURRENCY
unai@29855
    39
    WHERE C_ORDERLINE_SERVICERELATION.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID
unai@29855
    40
    AND C_ORDER.C_ORDER_ID = C_ORDERLINE.C_ORDER_ID
unai@29855
    41
    AND C_ORDER.C_CURRENCY_ID =  C_CURRENCY.C_CURRENCY_ID
unai@29855
    42
    AND C_ORDERLINE.M_PRODUCT_id =  M_PRODUCT.M_PRODUCT_ID
unai@29855
    43
    AND C_ORDERLINE.C_ORDERLINE_ID =  v_c_orderline_id
unai@29855
    44
    GROUP BY ISPRICERULEBASED, M_PRODUCT.M_PRODUCT_ID, C_ORDER.DATEORDERED, STDPRECISION, C_ORDER.M_PRICELIST_ID;
eduardo@27017
    45
		
eduardo@27017
    46
    IF(v_IsPriceRuleBased = 'Y') THEN
unai@29855
    47
      FOR Cur_Rule IN
unai@29855
    48
	(SELECT  RULETYPE, AFTERDISCOUNTS, PERCENTAGE, M_SERVICEPRICERULE.M_SERVICEPRICERULE_ID
unai@29855
    49
	 FROM M_SERVICEPRICERULE_VERSION, M_SERVICEPRICERULE
unai@29855
    50
	 WHERE M_SERVICEPRICERULE_VERSION.M_SERVICEPRICERULE_ID =  M_SERVICEPRICERULE.M_SERVICEPRICERULE_ID
unai@29855
    51
	 AND M_PRODUCT_ID = v_M_Product_ID
unai@29855
    52
	 AND VALIDFROM <=  v_Dateordered
unai@29855
    53
	 ORDER BY VALIDFROM DESC
unai@29855
    54
	)
unai@29855
    55
      LOOP
unai@29855
    56
        IF(Cur_Rule.RULETYPE = 'P') THEN
unai@29855
    57
          RETURN  round(v_TotalAmount * Cur_Rule.percentage / 100, v_stdPrecision);
unai@29855
    58
        ELSE
unai@29855
    59
	  FOR Cur_Range IN
unai@29855
    60
            (SELECT  RULETYPE, AFTERDISCOUNTS, PERCENTAGE, M_PRICELIST_ID
unai@29855
    61
	     FROM m_servicepricerule_range
unai@29855
    62
	     WHERE m_servicepricerule_ID  = Cur_Rule.M_SERVICEPRICERULE_ID
unai@29855
    63
	     AND coalesce(amountupto, v_TotalAmount)  >=  v_TotalAmount
unai@29855
    64
	     ORDER BY amountupto
unai@29855
    65
	    )
unai@29855
    66
	  LOOP
unai@29855
    67
	    IF(Cur_Range.RULETYPE = 'P') THEN
unai@29855
    68
	      RETURN  ROUND(v_TotalAmount * Cur_Range.percentage / 100, v_stdPrecision);
unai@29855
    69
	    ELSE
unai@29855
    70
	      SELECT PRICELIST 
unai@29855
    71
		INTO v_PriceList
unai@29855
    72
	      FROM M_PRODUCTPRICE
unai@29855
    73
	      WHERE M_PRICELIST_VERSION_ID = M_GET_PRICELIST_VERSION(Cur_Range.M_PRICELIST_ID, v_Dateordered)
unai@29855
    74
	      AND M_PRODUCT_ID = v_M_Product_ID;
unai@29855
    75
              RETURN ROUND(v_PriceList * v_TotalQantity, v_stdPrecision);
unai@29855
    76
	    END IF;
unai@29855
    77
	  END LOOP;
unai@29855
    78
	  RAISE_APPLICATION_ERROR(-20000, '@NoMatchingRange@');
unai@29855
    79
        END IF;
unai@29855
    80
      END LOOP;
unai@29855
    81
      RAISE_APPLICATION_ERROR(-20000, '@NoMatchingPriceRule@');
unai@29855
    82
    ELSE
unai@29855
    83
      SELECT PRICELIST 
unai@29855
    84
        INTO v_PriceList
unai@29855
    85
      FROM M_PRODUCTPRICE
unai@29855
    86
      WHERE M_PRICELIST_VERSION_ID = M_GET_PRICELIST_VERSION(v_OrderPricelist, v_Dateordered)
unai@29855
    87
      AND M_PRODUCT_ID = v_M_Product_ID;
unai@29855
    88
      RETURN ROUND(v_PriceList * v_TotalQantity, v_stdPrecision);
eduardo@27017
    89
    END IF;
eduardo@27017
    90
END C_GET_SERVICE_AMOUNT
eduardo@27017
    91
]]></body>
eduardo@27017
    92
    </function>
eduardo@27017
    93
  </database>