x
1
DECLARE
2
v_Warehouse_ID numeric;
3
v_Quantity numeric := 99999; -- unlimited
4
v_IsBOM CHAR(1);
5
v_IsStocked CHAR(1);
6
v_ProductType CHAR(1);
7
v_ProductQty numeric;
8
v_StdPrecision int;
9
bom record;
10
BEGIN
11
-- Check Parameters
12
v_Warehouse_ID := p_Warehouse_ID;
13
IF (v_Warehouse_ID IS NULL) THEN
14
IF (p_Locator_ID IS NULL) THEN
15
RETURN 0;
16
ELSE
17
SELECT MAX(M_Warehouse_ID) INTO v_Warehouse_ID
18
FROM M_LOCATOR
19
WHERE M_Locator_ID=p_Locator_ID;
20
END IF;
21
END IF;
22
IF (v_Warehouse_ID IS NULL) THEN
23
RETURN 0;
24
END IF;
25
26
-- Check, if product exists and if it is stocked
27
BEGIN
28
SELECT IsBOM, ProductType, IsStocked
29
INTO v_IsBOM, v_ProductType, v_IsStocked
30
FROM M_PRODUCT
31
WHERE M_Product_ID=p_Product_ID;
32
--
33
EXCEPTION -- not found
34
WHEN OTHERS THEN
35
RETURN 0;
36
END;
37
38
-- No reservation for non-stocked
39
IF (v_IsBOM='N' AND (v_ProductType<>'I' OR v_IsStocked='N')) THEN
40
RETURN 0;
41
-- Stocked item
42
ELSIF (v_IsStocked='Y') THEN
43
-- Get ProductQty
44
SELECT COALESCE(SUM(Qty), 0)
45
INTO v_ProductQty
46
FROM M_StorageReservation
47
WHERE M_Product_ID=p_Product_ID
48
AND M_Warehouse_ID=v_Warehouse_ID
49
AND IsSOTrx='Y'
50
AND IsActive='Y';
51
--
52
RETURN v_ProductQty;
53
END IF;
54
55
-- Go though BOM
56
FOR bom IN
57
-- Get BOM Product info
58
SELECT b.M_ProductBOM_ID, b.BOMQty, p.IsBOM, p.IsStocked, p.ProductType
59
FROM M_PRODUCT_BOM b, M_PRODUCT p
60
WHERE b.M_ProductBOM_ID=p.M_Product_ID
61
AND b.M_Product_ID=p_Product_ID
62
AND b.M_ProductBOM_ID != p_Product_ID
63
AND p.IsBOM='Y'
64
AND p.IsVerified='Y'
65
AND b.IsActive='Y'
66
LOOP
67
-- Stocked Items "leaf node"
68
IF (bom.ProductType = 'I' AND bom.IsStocked = 'Y') THEN
69
-- Get ProductQty
70
SELECT COALESCE(SUM(Qty), 0)
71
INTO v_ProductQty
72
FROM M_StorageReservation
73
WHERE M_Product_ID=bom.M_ProductBOM_ID
74
AND M_Warehouse_ID =v_Warehouse_ID
75
AND IsSOTrx='Y'
76
AND IsActive='Y';
77
-- Get Rounding Precision
78
SELECT COALESCE(MAX(u.StdPrecision), 0)
79
INTO v_StdPrecision
80
FROM C_UOM u, M_PRODUCT p
81
WHERE u.C_UOM_ID=p.C_UOM_ID AND p.M_Product_ID=bom.M_ProductBOM_ID;
82
-- How much can we make with this product
83
v_ProductQty := ROUND (v_ProductQty/bom.BOMQty, v_StdPrecision);
84
-- How much can we make overall
85
IF (v_ProductQty < v_Quantity) THEN
86
v_Quantity := v_ProductQty;
87
END IF;
88
-- Another BOM
89
ELSIF (bom.IsBOM = 'Y') THEN
90
v_ProductQty := Bomqtyreserved (bom.M_ProductBOM_ID, v_Warehouse_ID, p_Locator_ID);
91
-- How much can we make overall
92
IF (v_ProductQty < v_Quantity) THEN
93
v_Quantity := v_ProductQty;
94
END IF;
95
END IF;
96
END LOOP; -- BOM
97
98
-- Unlimited (e.g. only services)
99
IF (v_Quantity = 99999) THEN
100
RETURN 0;
101
END IF;
102
103
IF (v_Quantity > 0) THEN
104
-- Get Rounding Precision for Product
105
SELECT COALESCE(MAX(u.StdPrecision), 0)
106
INTO v_StdPrecision
107
FROM C_UOM u, M_PRODUCT p
108
WHERE u.C_UOM_ID=p.C_UOM_ID AND p.M_Product_ID=p_Product_ID;
109
--
110
RETURN ROUND (v_Quantity, v_StdPrecision);
111
END IF;
112
RETURN 0;
113
END;