Hi Everyone,
I have a report which shows purchases for an Item. If an item has an associated 'Landed Cost' (any landed cost!) then it is treated as an 'Overseas Purchase Item' and one section of my query code runs. However if there is no associated 'Landed Cost' then the item is treated as a 'Local Purchase Item'. However my Manager has just pointed out a major flaw in my query logic; if an Item is purchased both locally and internationally then only international purchases are shown by the report.
Here is my query in its current form -
DECLARE @itemCode nvarchar(30), @choice int
SET @itemCode = 'PC3MF1458'
--'KR151120F0'
--'EG550.534'
--$[$5.0.0]
IF EXISTS(SELECT T0.ItemCode FROM IPF1 T0 WHERE T0.ItemCode = @itemCode)
BEGIN
SELECT DISTINCT
T5.DocNum AS 'Purchase Order #'
-- , T5.DocDate AS 'PO Date'
, T3.DocNum AS 'Goods Receipt PO #'
, T3.DocDate AS 'GRPO Date'
, DATEDIFF(DD, T5.DocDate, T3.DocDate) AS 'Lead Time'
, T2.Quantity
, T0.Currency
, T0.PriceFOB AS 'Buy Price'
, T0.Rate AS 'Ex.Rate'
, T0.PriceAtWH AS 'Landed Cost'
, T0.FactNoCust AS 'Import %'
, T0.CustRate AS 'Duty Rate'
, ISNULL(T4.U_INE_FreightType, '') AS 'Freight Type'
, ISNULL(T4.U_A1WMS_Comments, '') AS 'Warehouse Comments'
, T5.CardCode
, T5.CardName
FROM
IPF1 T0 -- Landed Cost (Rows)
INNER JOIN OIPF T1 ON T1.DocEntry = T0.DocEntry -- Landed Cost (Header)
INNER JOIN PDN1 T2 ON T2.DocEntry = T0.BaseEntry AND T2.ItemCode = @itemCode -- Goods Receipt PO (Rows)
INNER JOIN OPDN T3 on T3.DocEntry = T2.DocEntry -- Goods Receipt PO (Header)
INNER JOIN POR1 T4 ON T4.DocEntry = T2.BaseEntry AND T4.LineNum = T2.BaseLine -- Purchase Order (Rows)
INNER JOIN OPOR T5 ON T5.DocEntry = T4.DocEntry -- Purchase Order (Header)
WHERE T0.ItemCode = @itemCode
AND T5.Canceled = 'N'
ORDER BY T3.DocDate DESC
END
ELSE
BEGIN
SELECT
T5.DocNum AS 'Purchase Order #'
-- , T5.DocDate AS 'PO Date'
, T3.DocNum AS 'Goods Receipt PO #'
--, T1.DocNum AS 'A/P Invoice #'
, T3.DocDate AS 'GRPO Date'
, DATEDIFF(DD, T5.DocDate, T3.DocDate) AS 'Lead Time'
, T2.Quantity
, 'AU' AS 'Currency'
, T0.Price AS 'Buy Price'
, 'N/A' AS 'Ex.Rate'
, 'N/A' AS 'Landed Cost'
, 'N/A' AS 'Import %'
, 'N/A' AS 'Duty Rate'
, ISNULL(T4.U_INE_FreightType, '') AS 'Freight Type'
, ISNULL(T4.U_A1WMS_Comments, '') AS 'Warehouse Comments'
, T5.CardCode
, T5.CardName
FROM PCH1 T0
INNER JOIN OPCH T1 ON T1.DocEntry = T0.DocEntry
INNER JOIN PDN1 T2 ON T2.DocEntry = T0.BaseEntry AND T2.ItemCode = @itemCode
INNER JOIN OPDN T3 ON T3.DocEntry = T2.DocEntry
INNER JOIN POR1 T4 ON T4.DocEntry = T2.BaseEntry AND T4.LineNum = T2.BaseLine
INNER JOIN OPOR T5 ON T5.DocEntry = T4.DocEntry
WHERE T0.ItemCode = @itemCode
AND T5.Canceled = 'N'
ORDER BY T3.DocDate DESC
END
My initial reasoning for taking this approach to the query is that when a 'Landed Cost' exists it is not possible (to the best of my knowledge) to "work forward" to the IPF1/OIPF tables, e.g: to link from Purchase Order > Goods Receipt Purchase Order > Landed Cost, instead one must work backwards from the Landed Cost > Goods Receipt Purchase Order > Purchase Order!
If anybody can suggest any way that I can adjust the query above to take into consideration Items that are purchased locally and internationally it will be greatly appreciated.
Alternatively if I must start again suggestions on best practices here will be greatly appreciated (e.g.: Should I be working with a Journal table?).
Kind Regards,
David