I've created the following script for the purpose of retrieving:
- Current Inventory Item quantities On Hand by Bin # where:
a. quantities are greater than 0
b. Items are in active status - The Last Sale Date, Last Unit Cost of Sale, Last Unit Price of sale
The script is working with the following two exceptions:
- If the Inventory Item has no sales, it's excluded from the data. I need to see the current on hand quantities with a null value in the Last Sale column
- If there were two or more invoices on the last sale date, and the unit price is different on the Invoices, both sales are listed. I need to see the invoice with the highest sale price.
Any help appreciated.
Select DISTINCT
B.ITEMNMBR AS ITEM_NUMBER,
I.ITEMDESC AS ITEM_DESCRIPTION,
B.BIN,
B.QUANTITY,
(CASE
When B.ITEMNMBR = ' ' then ' '
ELSE MD.Last_Sale
END) AS Last_Sale,
MP.Max_Price AS Unit_Price,
I.CURRCOST AS Unit_Cost_FOB,
I.ITMCLSCD AS CLASS,
I.USCATVLS_1 AS SOURCE
from IV00112 B
INNER JOIN SOP30300 HH
ON B.ITEMNMBR = HH.ITEMNMBR
left outer JOIN sop30200 HL
ON HL.SOPTYPE = HH.SOPTYPE AND HH.SOPNUMBE = HL.SOPNUMBE
inner join
(select max(H.DOCDATE) Last_Sale, D.ITEMNMBR
from SOP30200 H
inner join SOP30300 D
on H.SOPTYPE = D.SOPTYPE AND H.SOPNUMBE = D.SOPNUMBE
where H.SOPTYPE = 3 AND H.VOIDSTTS = 0
group by D.ITEMNMBR) MD --MAX DATE
on HH.ITEMNMBR = MD.ITEMNMBR AND HL.DOCDATE = MD.Last_Sale
inner join
(select H.DOCDATE, D.ITEMNMBR, max(D.UNITPRCE) Max_Price
from SOP30200 H
inner join SOP30300 D
on H.SOPTYPE = D.SOPTYPE AND H.SOPNUMBE = D.SOPNUMBE
where H.SOPTYPE = 3 AND H.VOIDSTTS = 0
group by H.DOCDATE, D.ITEMNMBR) MP --max price
on HH.ITEMNMBR = MP.ITEMNMBR
and HH.UNITPRCE = MP.Max_Price
and MP.DOCDATE = MP.DOCDATE
LEFT OUTER JOIN IV00101 I
ON I.ITEMNMBR = B.ITEMNMBR
WHERE B.QUANTITY <> 0
order by B.ITEMNMBR, B.BIN, B.QUANTITY