2013-09-20 09:52:10 +07:00
|
|
|
CREATE OR REPLACE FUNCTION bompricelimit (in product_id numeric, in pricelist_version_id numeric) RETURNS numeric AS
|
2007-08-24 14:02:10 +07:00
|
|
|
$BODY$
|
2008-10-30 03:28:55 +07:00
|
|
|
DECLARE
|
2013-09-20 09:52:10 +07:00
|
|
|
v_Price NUMERIC;
|
|
|
|
v_ProductPrice NUMERIC;
|
|
|
|
bom RECORD;
|
|
|
|
|
2008-10-30 03:28:55 +07:00
|
|
|
BEGIN
|
|
|
|
-- Try to get price from PriceList directly
|
|
|
|
SELECT COALESCE (SUM(PriceLimit), 0)
|
2013-09-20 09:52:10 +07:00
|
|
|
INTO v_Price
|
|
|
|
FROM M_ProductPrice
|
|
|
|
WHERE M_PriceList_Version_ID=PriceList_Version_ID AND M_Product_ID=Product_ID;
|
|
|
|
|
|
|
|
-- No Price - Check if BOM
|
2008-10-30 03:28:55 +07:00
|
|
|
IF (v_Price = 0) THEN
|
2013-09-20 09:52:10 +07:00
|
|
|
FOR bom IN
|
|
|
|
SELECT b.M_ProductBOM_ID, b.BOMQty, p.IsBOM
|
|
|
|
FROM M_Product_BOM b, M_Product p
|
|
|
|
WHERE b.M_ProductBOM_ID=p.M_Product_ID
|
|
|
|
AND b.M_Product_ID=Product_ID
|
2014-05-16 12:31:50 +07:00
|
|
|
AND b.M_ProductBOM_ID != Product_ID
|
2013-09-20 09:52:10 +07:00
|
|
|
AND p.IsBOM='Y'
|
|
|
|
AND p.IsVerified='Y'
|
2014-05-16 12:31:50 +07:00
|
|
|
AND b.IsActive='Y'
|
2008-10-30 03:28:55 +07:00
|
|
|
LOOP
|
2013-09-20 09:52:10 +07:00
|
|
|
v_ProductPrice := bomPriceLimit (bom.M_ProductBOM_ID, PriceList_Version_ID);
|
2008-10-30 03:28:55 +07:00
|
|
|
v_Price := v_Price + (bom.BOMQty * v_ProductPrice);
|
|
|
|
END LOOP;
|
|
|
|
END IF;
|
|
|
|
--
|
|
|
|
RETURN v_Price;
|
2013-09-20 09:52:10 +07:00
|
|
|
|
2008-10-30 03:28:55 +07:00
|
|
|
END;
|
2013-09-20 09:52:10 +07:00
|
|
|
|
2007-08-24 14:02:10 +07:00
|
|
|
$BODY$
|
2014-05-16 12:31:50 +07:00
|
|
|
LANGUAGE 'plpgsql' STABLE
|
2013-09-20 09:52:10 +07:00
|
|
|
;
|
|
|
|
|