Queries to Check for (and Fix) Cost And Qty Problems
SQL queries that check whether inventory cost agrees with the ledger, and repair the usual causes when it does not: stale accounting configuration on inventory requests, zombie inventory requests, quantities that reached zero but still carry cost, and bad FIFO cost transactions. Run the checking queries first, and take a database backup before any statement that updates or deletes.
Check Cost and Ledger are consistent
Details
sql
with Costs as
(
select SUM(totalCost) as costValue,originId,originType from CostInTransLine group by originId ,originType
union all
select SUM(totalCost) as costValue,originId,originType from CostOutTransLine where originType <> 'StockTransfer' group by originId,originType
)
,
Acc as (
select SUM(debitLocalAmount) as ledgerValue,originId,originType from LedgerTransLine group by originId,originType
)
select c.originType,c.originId,ledgerValue,costValue,abs(ledgerValue-costValue) diff from Costs c left join Acc on acc.originId = c.originId where ABS(c.costValue-acc.ledgerValue)>0.1
order by diff descRefetch Accounting Config For a specific term code
Details
sql
update r set transStatus = 'Retry',regenerateLedgerReq = 1, reFetchAccConfig = 1
from InvTransReq r inner join EntitySystemEntry e on e.targetId = r.originId
left join DocumentTerm t on t.id = e.term_id
where r.transStatus = 'Processed' and r.requestType <> 'Delete'
and t.code = 'term_code_here'Refetch Accounting Config in Failed InvTransReq
Details
sql
update InvTransReq set transStatus = 'Retry',regenerateLedgerReq = 1, reFetchAccConfig = 1
where transStatus = 'Processed' and requestType <> 'Delete'Refetch Accounting Config in All Invoices, Expenses, Issues, Receipts, and Transfers
Details
sql
update InvTransReq set transStatus = 'Retry',regenerateLedgerReq = 1, reFetchAccConfig = 1
where transStatus = 'Processed' and requestType <> 'Delete'Find and Remove Zombie InvTransReq
Select Statement to review
sql
select origintype,origincode from QtyTransLine r left join EntitySystemEntry e on e.targetId = r.originId
where e.id is null or e.fileStatus = 'Cancelled'
union all
select origintype,origincode from ReservationTransLine r left join EntitySystemEntry e on e.targetId = r.originId
where e.id is null or e.fileStatus = 'Cancelled'
union all
select origintype,origincode from CostInTransLine r left join EntitySystemEntry e on e.targetId = r.originId
where e.id is null or e.fileStatus = 'Cancelled'
union all
select origintype,origincode from CostOutTransLine r left join EntitySystemEntry e on e.targetId = r.originId
where e.id is null or e.fileStatus = 'Cancelled'Update Query
sql
update r set requestType = 'Delete',costTransStatus = 'Retry',qtyTransStatus = 'Retry',transStatus = 'Retry'
from InvTransReq r inner join QtyTransLine l on l.requestId = r.id
left join EntitySystemEntry e on e.targetId = l.originId
where e.id is null or e.fileStatus = 'Cancelled'
go
update r set requestType = 'Delete',costTransStatus = 'Retry',qtyTransStatus = 'Retry',transStatus = 'Retry'
from InvTransReq r inner join ReservationTransLine l on l.requestId = r.id
left join EntitySystemEntry e on e.targetId = l.originId
where e.id is null or e.fileStatus = 'Cancelled'
go
update r set requestType = 'Delete',costTransStatus = 'Retry',qtyTransStatus = 'Retry',transStatus = 'Retry'
from InvTransReq r inner join costintransline l on l.requestId = r.id
left join EntitySystemEntry e on e.targetId = l.originId
where e.id is null or e.fileStatus = 'Cancelled'
go
update r set requestType = 'Delete',costTransStatus = 'Retry',qtyTransStatus = 'Retry',transStatus = 'Retry'
from InvTransReq r inner join costouttransline l on l.requestId = r.id
left join EntitySystemEntry e on e.targetId = l.originId
where e.id is null or e.fileStatus = 'Cancelled'Find Transactions with differences that led to "zero quantity but have cost"
Query To Review
sql
with x as (
select item_id,originType,originId,originCode,overdraftSatisCost,overdraftSatisQty,overdraftDetails,strSequence,sum(coalesce(netCost,0)-coalesce(overdraftSatisCost,0)) over(partition by dimensionCost order by strSequence ROWS UNBOUNDED PRECEDING) currentCost,coalesce(currentNetCost,0) currentNetCost,
coalesce(currentNetQty,0) currentNetQty ,dimensionCost
from CostInTransLine c left join LegalEntity le on le.id = c.legalEntity_id
--where valueDate < '20180101'
),
minByItem as (select min(strSequence) minStr,item_id from x
where abs(currentCost-currentNetCost) > 0.0001
group by dimensionCost,item_id
)
select distinct originType,originId,valueDate from minByItem mi left join CostInTransLine cin on cin.strSequence = mi.minStrQuery to Reprocess
sql
with x as (
select item_id,originType,originId,originCode,overdraftSatisCost,overdraftSatisQty,overdraftDetails,strSequence,sum(coalesce(netCost,0)-coalesce(overdraftSatisCost,0)) over(partition by dimensionCost order by strSequence ROWS UNBOUNDED PRECEDING) currentCost,coalesce(currentNetCost,0) currentNetCost,
coalesce(currentNetQty,0) currentNetQty ,dimensionCost
from CostInTransLine c left join LegalEntity le on le.id = c.legalEntity_id
--where valueDate < '20180101'
),
minByItem as (select min(strSequence) minStr,item_id from x
where abs(currentCost-currentNetCost) > 0.0001
group by dimensionCost,item_id
)
, reproceess as (select distinct originType,originId from minByItem mi left join CostInTransLine cin on cin.strSequence = mi.minStr)
update r set transStatus = 'Retry', costTransStatus = 'Retry' from reproceess rp inner join InvTransReq r on r.originId = rp.originIdFind Documents that need to be recommited to fix zero quantity but have cost
Details
sql
with x as (
select item_id,originType,originId,originCode,overdraftSatisCost,overdraftSatisQty,overdraftDetails,strSequence,sum(coalesce(netCost,0)-coalesce(overdraftSatisCost,0)) over(partition by dimensionCost order by strSequence ROWS UNBOUNDED PRECEDING) currentCost,coalesce(currentNetCost,0) currentNetCost,
coalesce(currentNetQty,0) currentNetQty
from CostInTransLine c left join LegalEntity le on le.id = c.legalEntity_id
-- where valueDate < '20180101'
-- item_id = 0xFFFF00015B5DDFD135000700FF106A84
),
minByItem as (select min(strSequence) minStr,item_id from x
where currentCost<>currentNetCost
group by item_id
)
select originType,originId, * from minByItem mi left join CostInTransLine cin on cin.strSequence = mi.minStr
/*
select currentCost-currentNetCost costDiff, *
from x
where currentCost<>currentNetCost
order by item_id,strSequence*/Find Inventory Transactions that do not affect on ledger transactions
Details
sql
declare @valueDate as date = '20211231';
declare @invnetoryAccountCode as nvarchar(255) = '1207%' ;
with costs as (
select w.code wcode,w.id wid,w.name1 wname,originType,originCode,originId, netCost,totalCost*-1 totalCost,c.netQty netQty ,i.id item,i.code itemCode,i.name1 itemName,b.name1 branch,leg.name1 legal,leg.id legalEntityId , valueDate
from CostInTransLine c
left join InvItem i on i.id = c.item_id
left join UOM U on U.id= i.primBaseUnit_id
left join Branch b on b.id = c.branch_id
left join LegalEntity leg on leg.id = c.legalEntity_id
left join warehouse w on w.id = c.warehouse_id
where c.valuedate <=@valueDate
union all
select w.code wcode,w.id wid,w.name1 wname,originType,originCode,originId,netCost*-1 netCost,totalCost*-1 totalCost,c.netQty*-1 netQty ,i.id item,i.code itemCode,i.name1 itemName,b.name1 branch,leg.name1 legal,leg.id legalEntityId, valueDate
from CostOutTransLine c
left join InvItem i on i.id = c.item_id
left join UOM U on U.id= i.primBaseUnit_id
left join Branch b on b.id = c.branch_id
left join LegalEntity leg on leg.id = c.legalEntity_id
left join warehouse w on w.id = c.warehouse_id
where c.valuedate <=@valueDate
union all
select w.code wcode,w.id wid,w.name1 wname,originType,originCode,originId,outCost*-1 netCost,outCost*-1 totalCost,c.outQty netQty ,i.id item,i.code itemCode,i.name1 itemName,b.name1 branch,leg.name1 legal,leg.id legalEntityId , valueDate
from fifocosttransline c
left join InvItem i on i.id = c.item_id
left join UOM U on U.id= i.primBaseUnit_id
left join Branch b on b.id = c.branch_id
left join LegalEntity leg on leg.id = c.legalEntity_id
left join warehouse w on w.id = c.warehouse_id
where c.valuedate <=@valueDate and outQty > 0
union all
select w.code wcode,w.id wid,w.name1 wname,originType,originCode,originId,inCost netCost,incost totalCost,c.inQty netQty ,i.id item,i.code itemCode,i.name1 itemName,b.name1 branch,leg.name1 legal,leg.id legalEntityId, valueDate
from fifocosttransline c
left join InvItem i on i.id = c.item_id
left join UOM U on U.id= i.primBaseUnit_id
left join Branch b on b.id = c.branch_id
left join LegalEntity leg on leg.id = c.legalEntity_id
left join warehouse w on w.id = c.warehouse_id
where c.valuedate <=@valueDate and inQty > 0
)
,
fromLedger as (
select sum(debitLocalAmount-creditLocalAmount) total,subsidiaryCode,subsidiaryId
,originId
from LedgerTransLine l
left join Account acc on acc.id = l.account_id
where acc.code like @invnetoryAccountCode
and l.valueDate <=@valueDate
group by subsidiaryCode,subsidiaryId
,originId
)
/*
select * from costs where item = 0xFFFF00016EEFACAFE2000000FF19F35D
order by strSequence,netQty
*/
, fromCost as (
select -- wcode, sum(netCost) cost,sum(netQty)qty,item,itemCode,itemName ,legalEntityId,min(valueDate) minDate,max(valueDate) maxDate,min(strSequence) minSeq,dimensionCost
wid,sum(totalCost) cost
,originId
from costs
group by wid
,originId
)
, costledger as (
select distinct wid,originId from fromCost
union
select distinct subsidiaryId,originId from fromLedger
)
select sum(coalesce(c.cost,0)) c,sum(coalesce(l.total,0)) l,abs(abs(sum(coalesce(c.cost,0)))-abs(sum(coalesce(l.total,0)))) diff,cl.wid,w.code,w.name1
,cl.originId,e.targetType,e.code
from costledger cl
left join fromCost c on c.wid = cl.wid and c.originId = cl.originId
left join fromLedger l on l.subsidiaryId = cl.wid and l.originId = cl.originId
left join Warehouse w on w.id = cl.wid
left join EntitySystemEntry e on e.targetId = cl.originId
group by cl.wid ,w.code,w.name1
,cl.originId,e.targetType,e.code
having abs(abs(sum(coalesce(c.cost,0)))-abs(sum(coalesce(l.total,0))))>1
order by diff descCheck Cost and Ledger are consistent (totals)
Details
sql
declare @onDate as date = '20231231'
declare @inventoryAccountsPrefix as nvarchar(30) = '1106'
;
with Costs1 as
(
select SUM(totalCost) as inCost,0 outCost
from CostInTransLine
where valueDate<=@onDate
union all
select 0 inCost, SUM(totalCost) as outCost
from CostOutTransLine
where valueDate<=@onDate
),
Costs as (
select SUM(inCost) -sum(outCost) costs
from Costs1
)
,
Acc as (
select SUM(debitLocalAmount) - sum(creditLocalAmount) acc
from LedgerTransLine l left join Account acc on acc.id = l.account_id
where acc.code like @inventoryAccountsPrefix + '%' and originType in ('StockIssue','StockReceipt','StockTransfer','CostRevaluation')
and valueDate<=@onDate
)
select costs,acc,costs-acc diff from acc inner join costs on 1 = 1Check Cost and Ledger are consistent per each document
Details
sql
declare @onDate as date = '20231231'
declare @inventoryAccountsPrefix as nvarchar(30) = '1106'
;
with Costs1 as
(
select SUM(totalCost) as inCost,0 outCost,originId,originType,valueDate
from CostInTransLine
where valueDate <=@onDate
group by originId ,originType,valueDate
union all
select 0 inCost, SUM(totalCost) as outCost,originId,originType,valueDate
from CostOutTransLine
where valueDate <=@onDate
group by originId,originType ,valueDate
),
Costs as (
select SUM(inCost) as inCost,sum(outCost) outCost,originId,originType,valueDate
from Costs1
group by originId ,originType,valueDate
)
,
Acc as (
select SUM(debitLocalAmount) as dr,sum(creditLocalAmount) cr,originId,originType
from LedgerTransLine l left join Account acc on acc.id = l.account_id
where acc.code like @inventoryAccountsPrefix+'%' and originType in ('StockIssue','StockReceipt','StockTransfer','CostRevaluation')
and valueDate <=@onDate
group by originId,originType
)
select case when c.originType <> 'CostRevaluation'
then abs( sum(dr-inCost))+abs(sum(cr-outCost))
else abs( sum(dr-cr-(inCost-outCost))) end
diff ,c.originId,c.originType,
sum(sum(case when c.originType <> 'CostRevaluation'
then dr-inCost+cr-outCost
else dr-cr-(inCost-outCost) end)) over (partition by 1) tt
from Costs c left join Acc on acc.originId = c.originId
left join InvTransReq r on r.originId = c.originId
group by c.originId,c.originType
having case when c.originType <> 'CostRevaluation'
then abs( sum(dr-inCost))+abs(sum(cr-outCost))
else abs( sum(dr-cr-(inCost-outCost))) end >= 0.01
order by diff descReGenerate Ledger Transactions for Inconsistent Cost-Ledger Requests
Details
sql
with Costs as
(
select SUM(totalCost) as costValue,originId,originType from CostInTransLine group by originId ,originType
union all
select SUM(totalCost) as costValue,originId,originType from CostOutTransLine where originType <> 'StockTransfer' group by originId,originType
)
,
Acc as (
select SUM(debitLocalAmount) as ledgerValue,originId,originType from LedgerTransLine group by originId,originType
)
update r set transStatus = 'Retry',regenerateLedgerReq = 1,priority = 3 from Costs c left join Acc on acc.originId = c.originId left join InvTransReq r on r.originId = c.originId
where ABS(c.costValue-acc.ledgerValue)>0.1 and r.requestType <> 'Delete'Find items with zero quantity but have cost
Details
sql
with costs as (
select w.code wcode,w.name1 wname, netCost,c.netQty netQty ,i.id item,i.code itemCode,b.name1 branch,leg.name1 legal,leg.id legalEntityId
from CostInTransLine c
left join InvItem i on i.id = c.item_id
left join UOM U on U.id= i.primBaseUnit_id
left join Branch b on b.id = c.branch_id
left join LegalEntity leg on leg.id = c.legalEntity_id
left join warehouse w on w.id = c.warehouse_id
--where c.valuedate <'20180101'
union all
select w.code wcode,w.name1 wname,netCost*-1 netCost,c.netQty*-1 netQty ,i.id item,i.code itemCode,b.name1 branch,leg.name1 legal,leg.id legalEntityId from CostOutTransLine c
left join InvItem i on i.id = c.item_id
left join UOM U on U.id= i.primBaseUnit_id
left join Branch b on b.id = c.branch_id
left join LegalEntity leg on leg.id = c.legalEntity_id
left join warehouse w on w.id = c.warehouse_id
--where c.valuedate <'20180101'
)
select sum(netCost) cost,sum(netQty)qty,item,itemCode ,legalEntityId from costs
group by item,itemCode,legalEntityId
having sum(netQty) = 0 and abs(sum(netCost)) > 0.01
order by abs(sum(netCost)) descFix Invalid CurrentNetQty fields
http://localhost:8080/erp/utils?util=com.namasoft.modules.supplychain.domain.utils.UpdateCurrentNetCostAndCurrentNetQty
Production Delivery Cost Problem (Cost Callback)
The query that lists the production orders that have an Order Close document and whose delivery cost differs by more than 1.5 from the cost of the materials issued plus the resources, less the materials returned, is on Manufacturing Utilities.
Reprocess Bad Fifo Cost Transactions
Details
sql
with toReproocess as (
select distinct l.originType,l.originId,l.valueDate
from FifoCostTransLine l
left join FifoCostMatcher mi on mi.inLine_id = l.id
left join FifoCostMatcher mo on mo.outLine_id = l.id
group by l.originType,l.originId,l.remainingInQty,l.remainingOutQty,l.inQty,l.outQty,l.item_id,l.id,l.valueDate
having l.remainingInQty <> l.inQty - sum(coalesce(mi.consumedQty,0))
or l.remainingOutQty <> l.outQty - sum(coalesce(mo.consumedQty,0))
)
update r set r.transstatus = 'Retry',r.costtransstatus = 'Retry' from toReproocess t inner join InvTransReq r on r.originId = t.originId