Answer the question
In order to leave comments, you need to log in
How to correctly calculate the weighted average cost of goods after deleting the receipt record?
Hi everyone
, Can you help me calculate the weighted average cost of goods correctly?
When the goods arrive, it normally counts, but if it deletes the arrival, I can’t calculate it back here correctly ..
the journal table for there is a trigger after insert or after delete
and there are two more tables that update this trigger
, one for storing the balance of GOODSREMAINS and the reference book of goods TGOODS cost fields
Example
average cost = (current balance*current cost price+new.qty+new.price)/(current balance+new.qty)
1)ItemA arrival - 10units - $10 = GOODSREMAINS qty=10 TGOODS GDSAVGCOST=10
2)ItemA sale - 4units - 25$ = GOODSREMAINS qty=6 TGOODS GDSAVGCOST=10
3) ItemA arrival - 10units - $15 = GOODSREMAINS qty=16 TGOODS GDSAVGCOST=13.13
4) ItemA arrival - 10units - $20 = GOODSREMAINS qty=26 TGOODS GDSAVGCOST=15.77
Let's say if you delete the first arrival, how to calculate the cost?
declare variable CURRENT_QTY D_QTY;
declare variable CURRENT_COST D_AMT;
declare variable NEW_COST D_AMT;
begin
IF (INSERTING) THEN
begin
CURRENT_QTY =0;
CURRENT_COST =0;
NEW_COST =0;
select sum(coalesce(qty,0))
from GOODSREMAINS
where GOODSID=:NEW.GDSID
INTO :CURRENT_QTY;
select coalesce(GDSAVGCOST,0)
from TGOODS
where GDSID=:NEW.GDSID
INTO :CURRENT_COST;
if (exists(select goodsid from goodsremains where goodsid = :NEW.GDSID and storeid = :NEW.STOREID and boxnumber = :NEW.GDSCOD)) then
begin
UPDATE GOODSREMAINS
SET GOODSREMAINS.QTY = GOODSREMAINS.QTY + :NEW.RGSTRITEMQTY * :NEW.TRANSTYPE
WHERE GOODSREMAINS.GOODSID = :NEW.GDSID
AND GOODSREMAINS.BOXNUMBER = :NEW.GDSCOD
AND GOODSREMAINS.STOREID = :NEW.STOREID;
end
else
begin
insert into goodsremains (
goodsid,
storeid,
boxnumber,
qty)
VALUES (:NEW.GDSID,:NEW.STOREID,:NEW.GDSCOD,:NEW.RGSTRITEMQTY * :NEW.TRANSTYPE);
end
NEW_COST = (:CURRENT_QTY*:CURRENT_COST+:NEW.RGSTRITEMQTY*:NEW.RGTRITEMPURCHAMT)/(:NEW.RGSTRITEMQTY+:CURRENT_QTY);
update TGOODS
set GDSAVGCOST=:NEW_COST
where GDSID=:NEW.GDSID;
end
ELSE IF (DELETING) THEN
BEGIN
CURRENT_QTY =0;
CURRENT_COST =0;
NEW_COST =0;
select sum(coalesce(qty,0))
from GOODSREMAINS
where GOODSID=:OLD.GDSID
INTO :CURRENT_QTY;
select coalesce(GDSAVGCOST,0)
from TGOODS
where GDSID=:OLD.GDSID
INTO :CURRENT_COST;
UPDATE GOODSREMAINS
SET GOODSREMAINS.QTY = GOODSREMAINS.QTY - :OLD.RGSTRITEMQTY * :OLD.TRANSTYPE
WHERE GOODSREMAINS.GOODSID = :OLD.GDSID
AND GOODSREMAINS.BOXNUMBER = :OLD.GDSCOD
AND GOODSREMAINS.STOREID = :OLD.STOREID;
NEW_COST = ((:CURRENT_QTY-:OLD.RGSTRITEMQTY * :OLD.TRANSTYPE)*:CURRENT_COST-:OLD.RGSTRITEMQTY*:OLD.RGTRITEMPURCHAMT)/:CURRENT_QTY+:OLD.RGSTRITEMQTY;
update TGOODS
set GDSAVGCOST=:NEW_COST
where GDSID=:OLD.GDSID;
end
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question