Open POs By Warehouse
SELECT POM.POID as PO
, POM.Warehouseid
--, W.companyid
--, POM.POHL as HL
, DATEDIFF(day,(convert(date,POM.POOrderDate)),(convert(date,getdate()))) as OpenDays
, V.VendorName
,CAST(MONTH(POM.POOrderDate) AS VARCHAR(2))+'/'+CAST(DAY(POM.POOrderDate) AS VARCHAR(2))+'/'+CAST(YEAR(POM.POOrderDate)AS VARCHAR(4)) as Issued
, POM.POUser as [User]
, POD.PODQty as OrderQty
, POD.TotalOriginalPrice as OrderAmnt
,POD.TotalRequestedPrice AdjustedAmount
, isnull(CAST(POR.ReceivedDate_POR AS VARCHAR(40)),'') as Received
, isnull(POR.Qty_POR,0) as RcvdQty
, isnull(POR.Cost_POR,0) as RcvdAmt
, POD.PODQty - isnull(POR.Qty_POR,0) as OpenQty
, POD.TotalOriginalPrice - isnull(POR.Cost_POR,0) as OpenAmt
,CASE WHEN POM.POStatusAG = 2 THEN 'Excluded' ELSE 'Included' END [AG Status]
,POM.POComments
--, POM.POStatus
FROM gxWizmo_24.dbo.POMASTER AS POM
-- PO Details (with Request Quantity)
Left Join
(SELECT POID
, SUM(PODRequestQty) AS PODRequestQty -- Modified Order Qty
, SUM(PODQty) AS PODQty -- Original Order Qty
, SUM(PODRequestQty * PODPrice) AS TotalRequestedPrice -- Modified Order Amount
, SUM(PODQty * PODPrice) AS TotalOriginalPrice -- Original Order Amount
, SUM(CASE
WHEN PODQty >= PODRequestQty THEN PODQty
ELSE PODRequestQty
END) AS Qty_Expected_POD
, SUM(CASE
WHEN PODQty >= PODRequestQty THEN (PODQty * PODPrice)
ELSE (PODRequestQty * PODPrice)
END) AS Total_ExpectedCost_POD
FROM gxWizmo_24.dbo.POMASTERPODETAIL
GROUP BY POID) AS POD
ON POM.POID = POD.POID
-- PO Received Details
Left Join
(SELECT
POID
, MAX(convert(date,PORReceived)) AS ReceivedDate_POR
, COUNT(PORITEM) AS ItemDepth_POR
, SUM(PORQty) AS Qty_POR
, SUM(PORQty * PORCost) AS Cost_POR
FROM gxWizmo_24.dbo.PORECEIPT
GROUP BY POID) AS POR
ON POM.POID = POR.POID
-- Vendor Info
Left Join
gxWizmo_24.dbo.VENDOR AS V ON POM.VendorID = V.VendorId
-- Warehouse Info
Left Join _Boris..VW_Warehouse W
on POM.Warehouseid = W.warehouseid
Where POOrderDate >= GETDATE() - 365
AND ISNULL(POCancelDate,'1753-01-01 00:00:00.000') = '1753-01-01' -- Only include POs that have not been cancelled
AND POD.TotalRequestedPrice > ISNULL(POR.Cost_POR,0) -- Adjusted PO Amount is greater than Received PO Amount
and W.warehouseid = 111
ORDER BY POM.POID DESC