src-db/database/model/functions/C_GET_SERVICE_AMOUNT.xml
author RM packaging bot <staff.rm@openbravo.com>
Thu, 16 Jun 2016 05:35:45 +0000
changeset 29873 73a8cd831ca4
parent 29855 956cd99a1937
child 29874 4a8a100af104
permissions -rw-r--r--
Merge temporary head for 3.0PR16Q2
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
staff@29873
    36
  
staff@29873
    37
    
staff@29873
    38
  
staff@29873
    39
		SELECT SUM(AMOUNT), SUM(QUANTITY), ISPRICERULEBASED, M_PRODUCT.M_PRODUCT_ID, C_ORDER.DATEORDERED, STDPRECISION, C_ORDER.M_PRICELIST_ID
staff@29873
    40
		INTO v_TotalAmount, v_TotalQantity, v_IsPriceRuleBased, v_M_Product_ID,  v_Dateordered, v_stdPrecision, 	v_OrderPricelist
staff@29873
    41
		FROM C_ORDERLINE_SERVICERELATION, C_ORDERLINE, M_PRODUCT, C_ORDER, C_CURRENCY
staff@29873
    42
		WHERE C_ORDERLINE_SERVICERELATION.C_ORDERLINE_ID = C_ORDERLINE.C_ORDERLINE_ID
staff@29873
    43
		AND C_ORDER.C_ORDER_ID = C_ORDERLINE.C_ORDER_ID
staff@29873
    44
		AND C_ORDER.C_CURRENCY_ID =  C_CURRENCY.C_CURRENCY_ID
staff@29873
    45
		AND C_ORDERLINE.M_PRODUCT_id =  M_PRODUCT.M_PRODUCT_ID
staff@29873
    46
		AND C_ORDERLINE.C_ORDERLINE_ID =  v_c_orderline_id
staff@29873
    47
		GROUP BY ISPRICERULEBASED, M_PRODUCT.M_PRODUCT_ID, C_ORDER.DATEORDERED, STDPRECISION, C_ORDER.M_PRICELIST_ID;
eduardo@27017
    48
		
eduardo@27017
    49
    IF(v_IsPriceRuleBased = 'Y') THEN
staff@29873
    50
          FOR Cur_Rule IN
staff@29873
    51
			      (
staff@29873
    52
						SELECT  RULETYPE, AFTERDISCOUNTS, PERCENTAGE, M_SERVICEPRICERULE.M_SERVICEPRICERULE_ID
staff@29873
    53
						FROM M_SERVICEPRICERULE_VERSION, M_SERVICEPRICERULE
staff@29873
    54
						WHERE M_SERVICEPRICERULE_VERSION.M_SERVICEPRICERULE_ID =  M_SERVICEPRICERULE.M_SERVICEPRICERULE_ID
staff@29873
    55
						AND M_PRODUCT_ID = v_M_Product_ID
staff@29873
    56
						AND VALIDFROM <=  v_Dateordered
staff@29873
    57
						ORDER BY VALIDFROM DESC
staff@29873
    58
			      )
staff@29873
    59
			    LOOP
staff@29873
    60
            IF(Cur_Rule.RULETYPE = 'P') THEN
staff@29873
    61
              RETURN  round(v_TotalAmount * Cur_Rule.percentage / 100, v_stdPrecision);
staff@29873
    62
            ELSE
staff@29873
    63
			          FOR Cur_Range IN
staff@29873
    64
						      (
staff@29873
    65
									SELECT  RULETYPE, AFTERDISCOUNTS, PERCENTAGE, M_PRICELIST_ID
staff@29873
    66
									FROM m_servicepricerule_range
staff@29873
    67
									WHERE m_servicepricerule_ID  = Cur_Rule.M_SERVICEPRICERULE_ID
staff@29873
    68
									AND coalesce(amountupto, v_TotalAmount)  >=  v_TotalAmount
staff@29873
    69
									ORDER BY amountupto
staff@29873
    70
						      )
staff@29873
    71
						    LOOP
staff@29873
    72
			            IF(Cur_Range.RULETYPE = 'P') THEN
staff@29873
    73
			              RETURN  ROUND(v_TotalAmount * Cur_Range.percentage / 100, v_stdPrecision);
staff@29873
    74
			            ELSE
staff@29873
    75
			              SELECT PRICELIST 
staff@29873
    76
			              INTO v_PriceList
staff@29873
    77
			              FROM M_PRODUCTPRICE
staff@29873
    78
			              WHERE M_PRICELIST_VERSION_ID = M_GET_PRICELIST_VERSION(Cur_Range.M_PRICELIST_ID, v_Dateordered)
staff@29873
    79
			              AND M_PRODUCT_ID = v_M_Product_ID;
staff@29873
    80
                    RETURN ROUND(v_PriceList * v_TotalQantity, v_stdPrecision);
staff@29873
    81
			            END IF;
staff@29873
    82
						    END LOOP;
staff@29873
    83
						    RAISE_APPLICATION_ERROR(-20000, '@NoMatchingRange@');
staff@29873
    84
            END IF;
staff@29873
    85
			    END LOOP;
staff@29873
    86
			    RAISE_APPLICATION_ERROR(-20000, '@NoMatchingPriceRule@');
staff@29873
    87
		ELSE
staff@29873
    88
			SELECT PRICELIST 
staff@29873
    89
			INTO v_PriceList
staff@29873
    90
			FROM M_PRODUCTPRICE
staff@29873
    91
			WHERE M_PRICELIST_VERSION_ID = M_GET_PRICELIST_VERSION(v_OrderPricelist, v_Dateordered)
staff@29873
    92
			AND M_PRODUCT_ID = v_M_Product_ID;
staff@29873
    93
			RETURN ROUND(v_PriceList * v_TotalQantity, v_stdPrecision);
eduardo@27017
    94
    END IF;
eduardo@27017
    95
END C_GET_SERVICE_AMOUNT
eduardo@27017
    96
]]></body>
eduardo@27017
    97
    </function>
eduardo@27017
    98
  </database>