T0."DocNum" as "Invoice No.",T0."DocDate",T0."CardCode" as "Customer Code",
T0."CardName" as " Customer Name",T1."ItemCode", T1."Dscription" as "Item Description", T4."BatchNum" as " Batch" ,(T1."LineTotal"/ T1."Quantity") * T4."Quantity" as "Invoice Value",T4."Quantity" as "Inv.Quantity", T1."unitMsr" as "UOM",T1."NumPerMsr" as "Converter",T1."BaseRef" as "DN Number", (T2."StockValue"/ T1."Quantity") * T4."Quantity" as "Cost",T6."CalcPrice" as "CostPrice", T6."BASE_REF" as "PO", T8."PriceFOB" as "Price FOB", (T0."GrosProfit"/ T1."Quantity") * T4."Quantity" as "Gross Profit", ((T0."GrosProfit"/T1."LineTotal") * 100) as "GP %",
MAX(CASE WHEN T10."AlcCode" = '01' THEN (T10."CostSum"/(T8."Quantity" *
T8."NumPerMsr")) *T1."Quantity" END) AS "Freight" , MAX(CASE WHEN T10."AlcCode" = '02' THEN (T10."CostSum"/(T8."Quantity" * T8."NumPerMsr")) *T1."Quantity" END) AS "Custom Clearing" , MAX(CASE WHEN T10."AlcCode" = '03' THEN (T10."CostSum"/(T8."Quantity" * T8."NumPerMsr")) *T1."Quantity" END) AS "Inland Haulage" , MAX(CASE WHEN T10."AlcCode" = '04' THEN (T10."CostSum"/(T8."Quantity" * T8."NumPerMsr")) *T1."Quantity" END) AS "Transporting" , MAX(CASE WHEN T10."AlcCode" = '05' THEN (T10."CostSum"/(T8."Quantity" * T8."NumPerMsr")) *T1."Quantity" END) AS "Tax/Duties" , MAX(CASE WHEN T10."AlcCode" = '06' THEN (T10."CostSum"/(T8."Quantity" * T8."NumPerMsr")) *T1."Quantity" END) AS "Miscellenious",
CASE WHEN T5."BaseType" = 59 then (T2."StockValue"/ T1."Quantity") * T4."Quantity" as "Material Cost" WHEN T5."BaseType" = 20 THEN T6."CalcPrice" * T1."Quantity" as "Material Cost" END
FROM OINV T0 INNER JOIN INV1 T1 ON T0."DocEntry" = T1."DocEntry" and T1."ItemCode"
NOT IN (999999999, 39999999, 39999997) left outer join dln1 T2 on T1."BaseEntry" = T2."DocEntry" and T1."BaseLine" = T2."LineNum" and T1."BaseType"= 15 left outer join ODLN T3 on T2."DocEntry" = T3."DocEntry" left outer join ibt1 T4 on T3."DocNum"=T4."BaseNum" and T1."ItemCode" = T4."ItemCode" left outer join ibt1 T5 on T4."BatchNum" = T5."BatchNum" and T1."ItemCode" = T5."ItemCode" and T5."BaseType" = 20 and T5."DocDate" <> '20161031' and T5."Direction" = 0 or T4."BatchNum" = T5."BatchNum" and T1."ItemCode" = T5."ItemCode" and T5."BaseType" = 59 and T5."DocDate" <> '20161031' and T5."Direction" = 0
left outer join OINM T6 on T6."BASE_REF"=T5."BaseNum" and T6."ItemCode" =
T5."ItemCode" and T6."DocLineNum" = T5."BaseLinNum" and T6."CreatedBy" = T5."BaseEntry"
left outer join ipf1 T8 on T6."BASE_REF" = T8."Reference" and
T1."ItemCode"=T8."ItemCode" and T8."TargetDoc" is NULL
left outer join oipf T9 on T8."DocEntry" = T9."DocEntry" and T9."OpenForLaC" = 'Y'
left outer join ipf2 T10 on T8."DocEntry" = T10."DocEntry" left outer join oitm T11 on T1."ItemCode" = T11."ItemCode" WHERE T0."DocDate" > [%0] and T0."DocDate" <[%1] and T11."ItmsGrpCod" = [%2] GROUP BY T0."DocNum",T0."DocDate",T10."DocEntry",T0."CardCode", T0."CardName",T1."ItemCode",T1."Dscription", T1."LineTotal",T1."Price",T1."unitMsr",T1."NumPerMsr",T1."BaseRef",T4."BatchNum",T1 ."Quantity",T2."StockValue",T0."GrosProfit" ,T8."PriceFOB" ,T9."DocNum",T8."Quantity",T8."NumPerMsr",T8."FobValue",T4."Quantity",T6."BASE_REF" ,T6."CalcPrice"