CREATE OR REPLACE VIEW adempiere.JP_QtyReserved_Locator_RV AS
SELECT 
 q.AD_Client_ID AS AD_Client_ID 
 ,0 AS AD_Org_ID
 ,q.AD_Org_ID AS JP_Org_ID
 ,info.JP_Corporation_ID AS JP_Corporation_ID
 ,q.M_Locator_ID AS M_Locator_ID
 ,loc.M_Warehouse_ID AS M_Warehouse_ID
 ,loc.M_LocatorType_ID AS M_LocatorType_ID
 ,loc.JP_PhysicalWarehouse_ID AS JP_PhysicalWarehouse_ID
 ,loc.IsCommonLocatorJP AS IsCommonLocatorJP
 ,loc.x AS x
 ,loc.y AS y
 ,loc.z AS z
 ,q.M_Product_ID AS M_Product_ID
 ,p.C_UOM_ID AS C_UOM_ID
 ,p.Discontinued AS Discontinued
 ,p.ProductType AS ProductType
 ,p.IsStocked AS IsStocked
 ,p.IsBOM
 ,p.IsDropShip
 ,p.M_Product_Category_ID AS M_Product_Category_ID
 ,pc.JP_ProductCategoryL1_ID AS JP_ProductCategoryL1_ID
 ,pl1.JP_ProductCategoryL2_ID AS JP_ProductCategoryL2_ID
 ,q.QtyReserved
 ,q.QtyToDeliver
 ,s.QtyOnHand
 ,s.QtyOnHand - q.QtyReserved AS QtyAvailable
 ,s.QtyOnHand - q.QtyReserved + q.QtyToDeliver AS JP_ForecastAvailableQty
 ,CASE WHEN (s.QtyOnHand - q.QtyReserved + q.QtyToDeliver)*-1 >= 0 THEN (s.QtyOnHand - q.QtyReserved + q.QtyToDeliver)*-1 ELSE 0 END AS JP_ShortageQty
 ,CASE WHEN loc.M_Locator_ID IS NULL THEN 'NO'
       WHEN p.ProductType <> 'I' OR p.IsStocked <> 'Y' THEN 'UN'
       WHEN 
	(s.QtyOnHand - q.QtyReserved) >= 0 THEN 'OK' 
       WHEN
	(s.QtyOnHand - q.QtyReserved) < 0 AND (s.QtyOnHand - q.QtyReserved + q.QtyToDeliver) >= 0 THEN 'WT'
      WHEN 
	(s.QtyOnHand - q.QtyReserved) < 0 AND (s.QtyOnHand - q.QtyReserved + q.QtyToDeliver) < 0 THEN 'PO'
 
      ELSE
	'NO'
       END AS JP_ReservedStatus
FROM adempiere.JP_QtyReserved_Locator q
	INNER JOIN adempiere.M_Product p ON (q.M_Product_ID =p.M_Product_ID )
		INNER JOIN adempiere.M_Product_Category pc ON (p.M_Product_Category_ID=pc.M_Product_Category_ID)
		LEFT OUTER JOIN adempiere.JP_ProductCategoryL1 pl1 ON (pc.JP_ProductCategoryL1_ID=pl1.JP_ProductCategoryL1_ID)
	INNER JOIN adempiere.AD_OrgInfo info ON (q.AD_Org_ID = info.AD_Org_ID)
        LEFT OUTER JOIN adempiere.JP_StorageOnHand_Locator s on (q.M_Product_ID =s.M_Product_ID AND q.M_Locator_ID =s.M_Locator_ID)
        LEFT OUTER JOIN adempiere.M_Locator loc ON (q.M_Locator_ID = loc.M_Locator_ID)
      