Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagesql
/*Now check the avg cost, against the inventory valuation*/
	-- by WH.
	Select
		ItemCode =S.Code,
		WHCode = W.Code,
		ItemDescription1 =S.Description_1,
		ItemDescription2 =S.Description_2,
		ItemGroupCode =G.StGroup,
		ItemGroupDesc = G.Description,
		ItemWHGroupCode = WS.WHStockGroup,
		WS.fAverageCost as WHAvgCost,
		WS.WHQtyOnHand as WHQtyOnHand,
		Qty =
		 SUM(CASE WHEN P.Id = 'JCI' THEN 0 --JCI invoices directly invoice out, so doesn't affect the available stock qty
				  WHEN P.Id = 'JCS' THEN 0 --JCS is always 0 anyway on the quantity column, so this shouldn't affect anything.
				  /*JCM -> standard rule applies on qty */
				  WHEN P.Id NOT IN ('JCI','JCS') AND Debit > 0 THEN Quantity
				  ELSE -Quantity 		END)
		--,StockValue =SUM( ROUND(Debit,2)-ROUND(Credit,2))
		,SV = SUM(CASE
				   WHEN P.Id NOT IN ('OInv', 'Crn','JCS','JCM','OGrv','JCI') THEN ROUND(P.Debit,2)-ROUND(P.Credit,2)
				   WHEN P.Id = 'JCM' THEN P.Debit --note:exclude any credits! they are crediting the WIP acc, not the Stock acc.
				   WHEN P.Id = 'JCS' THEN ROUND(P.Credit,2)-ROUND(P.Debit,2)
				   WHEN P.Id = 'OGrv' AND P.JobCodeLink = 0 THEN ROUND(P.Debit,2)-ROUND(P.Credit,2)
				   WHEN P.Id = 'OGrv' AND P.JobCodeLink > 0 THEN 0 --if job code link is not 0, then the stock goes straight to WIP (so not stock account).
				   WHEN P.Debit > 0 AND P.Id IN ('OInv', 'Crn') THEN P.Quantity*P.Cost
				   WHEN P.Credit > 0 AND P.Id IN ('OInv', 'Crn') THEN -P.Quantity*P.Cost
				   WHEN P.Id = 'JCI' THEN 0 -- JCI invoices get their cost from the WIP account, therefore 0 influence on stock account
				   ELSE 0 END)

		, WS.fAverageCost*WS.WHQtyOnHand as WHValuation
		,[Difference] = WS.fAverageCost*WS.WHQtyOnHand - 
			SUM(CASE
				   WHEN P.Id NOT IN ('OInv', 'Crn','JCS','JCM','OGrv','JCI') THEN ROUND(P.Debit,2)-ROUND(P.Credit,2)
				   WHEN P.Id = 'JCM' THEN P.Debit --note:exclude any credits! they are crediting the WIP acc, not the Stock acc.
				   WHEN P.Id = 'JCS' THEN ROUND(P.Credit,2)-ROUND(P.Debit,2)
				   WHEN P.Id = 'OGrv' AND P.JobCodeLink = 0 THEN ROUND(P.Debit,2)-ROUND(P.Credit,2)
				   WHEN P.Id = 'OGrv' AND P.JobCodeLink > 0 THEN 0 --if job code link is not 0, then the stock goes straight to WIP (so not stock account).
				   WHEN P.Debit > 0 AND P.Id IN ('OInv', 'Crn') THEN P.Quantity*P.Cost
				   WHEN P.Credit > 0 AND P.Id IN ('OInv', 'Crn') THEN -P.Quantity*P.Cost
				   WHEN P.Id = 'JCI' THEN 0 -- JCI invoices get their cost from the WIP account, therefore 0 influence on stock account
				   ELSE 0 END)
	from PostST P
	LEFT JOIN StkItem S ON P.AccountLink = S.StockLink
	LEFT JOIN GrpTbl G ON S.ItemGroup = G.StGroup
	LEFT JOIN WhseMst W on WhseLink = P.WarehouseID
	LEFT JOIN WhseStk WS ON WS.WHStockLink = P.AccountLink AND WS.WHWhseID = P.WarehouseID
	WHERE P.TxDate <='2019/12/31'
		AND S.ServiceItem = 0
	GROUP BY
		S.Code,
		W.Code,
		S.Description_1,
		S.Description_2,
		S.ItemGroup,
		G.StGroup,
		G.Description
		,WS.fAverageCost,WS.WHQtyOnHand,  WS.WHStockGroup
	HAVING
		ABS(WS.fAverageCost*WS.WHQtyOnHand - 
			SUM(CASE
				   WHEN P.Id NOT IN ('OInv', 'Crn','JCS','JCM','OGrv','JCI') THEN ROUND(P.Debit,2)-ROUND(P.Credit,2)
				   WHEN P.Id = 'JCM' THEN P.Debit --note:exclude any credits! they are crediting the WIP acc, not the Stock acc.
				   WHEN P.Id = 'JCS' THEN ROUND(P.Credit,2)-ROUND(P.Debit,2)
				   WHEN P.Id = 'OGrv' AND P.JobCodeLink = 0 THEN ROUND(P.Debit,2)-ROUND(P.Credit,2)
				   WHEN P.Id = 'OGrv' AND P.JobCodeLink > 0 THEN 0 --if job code link is not 0, then the stock goes straight to WIP (so not stock account).
				   WHEN P.Debit > 0 AND P.Id IN ('OInv', 'Crn') THEN P.Quantity*P.Cost
				   WHEN P.Credit > 0 AND P.Id IN ('OInv', 'Crn') THEN -P.Quantity*P.Cost
				   WHEN P.Id = 'JCI' THEN 0 -- JCI invoices get their cost from the WIP account, therefore 0 influence on stock account
				   ELSE 0 END)
			) > 0.50
	Order by  S.Code

...