src-db/database/model/functions/GET_PURCHASE_PRICE.xml
author Juan Pablo Aroztegi <juanpablo.aroztegi@openbravo.com>
Wed, 03 Sep 2008 17:55:37 +0000
changeset 1605 8a0fe0193bef
parent 756 ae11e4610537
permissions -rw-r--r--
Merge r2.5x intro trunk
carlos@0
     1
<?xml version="1.0"?>
adrian@94
     2
  <database name="FUNCTION GET_PURCHASE_PRICE">
adrian@94
     3
    <function name="GET_PURCHASE_PRICE" type="NUMERIC">
juanpablo@1605
     4
      <parameter name="p_product_id" type="VARCHAR" mode="in">
antonio@735
     5
        <default/>
antonio@735
     6
      </parameter>
gorkaion@239
     7
      <body><![CDATA[/*************************************************************************
carlos@0
     8
* The contents of this file are subject to the Openbravo  Public  License
carlos@0
     9
* Version  1.0  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
    10
* Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
    11
* file except in compliance with the License. You  may  obtain  a copy of
carlos@0
    12
* the License at http://www.openbravo.com/legal/license.html
carlos@0
    13
* Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
    14
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    15
* License for the specific  language  governing  rights  and  limitations
carlos@0
    16
* under the License.
carlos@0
    17
* The Original Code is Openbravo ERP.
carlos@0
    18
* The Initial Developer of the Original Code is Openbravo SL
carlos@0
    19
* All portions are Copyright (C) 2001-2006 Openbravo SL
carlos@0
    20
* All Rights Reserved.
carlos@0
    21
* Contributor(s):  ______________________________________.
carlos@0
    22
************************************************************************/
carlos@0
    23
  v_Price NUMBER;
carlos@0
    24
  TYPE RECORD IS REF CURSOR;
carlos@0
    25
    Cur_Orderline RECORD;
carlos@0
    26
    Cur_Invoiceline RECORD;
carlos@0
    27
    Cur_Pricelist_Version RECORD;
carlos@0
    28
  BEGIN
carlos@468
    29
    FOR Cur_Invoiceline IN
carlos@468
    30
      (SELECT COALESCE(C_INVOICELINE.PRICEACTUAL, 0) AS Price
carlos@468
    31
      FROM C_INVOICELINE,
carlos@468
    32
        C_INVOICE
carlos@468
    33
      WHERE C_INVOICELINE.C_INVOICE_ID = C_INVOICE.C_INVOICE_ID
carlos@468
    34
        AND C_INVOICE.ISSOTRX = 'N'
carlos@468
    35
        AND C_INVOICE.DOCSTATUS = 'CO'
carlos@468
    36
        AND C_INVOICE.PROCESSED = 'Y'
carlos@468
    37
        AND C_INVOICELINE.M_PRODUCT_ID = p_Product_ID
carlos@468
    38
      ORDER BY C_INVOICE.DATEINVOICED DESC
carlos@0
    39
      )
carlos@0
    40
    LOOP
carlos@468
    41
      v_Price := Cur_Invoiceline.PRICE;
carlos@0
    42
      EXIT;
carlos@0
    43
    END LOOP;
carlos@0
    44
    IF(v_Price IS NULL) THEN
carlos@468
    45
      FOR Cur_Orderline IN
carlos@468
    46
        (SELECT COALESCE(C_ORDERLINE.PRICEACTUAL, 0) AS Price
carlos@468
    47
        FROM C_ORDERLINE,
carlos@468
    48
          C_ORDER
carlos@468
    49
        WHERE C_ORDERLINE.C_ORDER_ID = C_ORDER.C_ORDER_ID
carlos@468
    50
          AND C_ORDER.ISSOTRX = 'N'
carlos@468
    51
          AND C_ORDER.DOCSTATUS = 'CO'
carlos@468
    52
          AND C_ORDER.PROCESSED = 'Y'
carlos@468
    53
          AND C_ORDERLINE.M_PRODUCT_ID = p_Product_ID
carlos@468
    54
        ORDER BY C_ORDER.DATEORDERED DESC
carlos@0
    55
        )
carlos@0
    56
      LOOP
carlos@468
    57
        v_Price := Cur_Orderline.PRICE;
carlos@0
    58
        EXIT;
antonio@737
    59
      END LOOP;
carlos@0
    60
    END IF;
carlos@0
    61
    IF(v_Price IS NULL) THEN
carlos@0
    62
      FOR Cur_Pricelist_Version IN
carlos@0
    63
        (SELECT COALESCE(M_PRODUCTPRICE.PRICESTD, 0) AS Price
carlos@0
    64
        FROM M_PRODUCTPRICE,
carlos@0
    65
          M_PRICELIST_VERSION,
carlos@0
    66
          M_PRICELIST
carlos@0
    67
        WHERE M_PRODUCTPRICE.M_PRICELIST_VERSION_ID = M_PRICELIST_VERSION.M_PRICELIST_VERSION_ID
carlos@0
    68
          AND M_PRICELIST_VERSION.M_PRICELIST_ID = M_PRICELIST.M_PRICELIST_ID
carlos@0
    69
          AND M_PRICELIST.ISSOPRICELIST = 'N'  --  AND M_PRICELIST_VERSION.ISACTIVE= 'Y'
gorkaion@239
    70
          AND M_PRICELIST_VERSION.VALIDFROM < now()
carlos@0
    71
          AND M_PRODUCTPRICE.M_PRODUCT_ID = p_Product_ID
carlos@0
    72
        ORDER BY M_PRICELIST_VERSION.VALIDFROM DESC
carlos@0
    73
        )
carlos@0
    74
      LOOP
carlos@0
    75
        v_Price := Cur_Pricelist_Version.PRICE;
carlos@0
    76
        EXIT;
carlos@0
    77
      END LOOP;
carlos@0
    78
    END IF;
carlos@0
    79
    IF(v_Price IS NULL) THEN
carlos@0
    80
      SELECT COALESCE(M_PRODUCT_PO.PRICEPO, 0)
carlos@0
    81
      INTO v_Price
carlos@0
    82
      FROM M_PRODUCT_PO
carlos@0
    83
      WHERE M_PRODUCT_PO.M_PRODUCT_ID = p_Product_ID;
carlos@0
    84
    END IF;
carlos@0
    85
    IF(v_Price IS NULL) THEN
carlos@0
    86
      v_Price := 0;
carlos@0
    87
    END IF;
carlos@0
    88
    RETURN v_Price;
antonio@735
    89
END GET_PURCHASE_PRICE
gorkaion@239
    90
]]></body>
adrian@94
    91
    </function>
adrian@94
    92
  </database>