USP_RECORDOPERATION_SELLSTOCKUNDO
Executes the "Sell Stock: Undo" record operation.
Parameters
Parameter | Parameter Type | Mode | Description |
---|---|---|---|
@ID | uniqueidentifier | IN | Input parameter indicating the ID of the record being updated. |
@CHANGEAGENTID | uniqueidentifier | IN | Input parameter indicating the ID of the change agent invoking the update. |
Definition
Copy
CREATE procedure dbo.USP_RECORDOPERATION_SELLSTOCKUNDO
(
@ID uniqueidentifier,
@CHANGEAGENTID uniqueidentifier
)
as begin
declare @ADJUSTMENTHISTORYSTOCKID uniqueidentifier;
if @CHANGEAGENTID is null
exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;
declare @CURRENTDATE datetime;
set @CURRENTDATE = getdate();
begin try
-- Remove the most recent stock sale
declare @STOCKSALETODELETE uniqueidentifier
select top 1 @STOCKSALETODELETE = ID
from dbo.STOCKSALE where STOCKDETAILID = @ID
order by SALEDATE desc, DATEADDED desc
if (select SALEPOSTSTATUSCODE from dbo.STOCKSALE where ID = @STOCKSALETODELETE) = 0
exec dbo.USP_ADJUSTMENTHISTORY_STOCK_FIXFORDELETE @ID, 1, @CHANGEAGENTID, @CURRENTDATE, @ADJUSTMENTHISTORYSTOCKID output, @STOCKSALETODELETE;
/* If stock details have been posted but not already adjusted */
if exists (select 1 from dbo.STOCKSALE where STOCKSALE.ID = @STOCKSALETODELETE and STOCKSALE.SALEPOSTSTATUSCODE = 0)
and not exists (select 1 from dbo.STOCKSALEADJUSTMENT where STOCKSALEID = @STOCKSALETODELETE and POSTSTATUSCODE = 1)
begin
declare @POSTDATE datetime;
declare @ERRORMESSAGE nvarchar(255);
select top 1 @POSTDATE = POSTDATE from dbo.STOCKSALEADJUSTMENT where STOCKSALEID = @STOCKSALETODELETE and POSTSTATUSCODE = 0 order by DATEADDED desc;
if @POSTDATE is null
select @POSTDATE = SALEPOSTDATE from dbo.STOCKSALE where ID = @STOCKSALETODELETE;
set @ERRORMESSAGE = dbo.UFN_GLDISTRIBUTION_VALIDPOSTDATE(@POSTDATE);
if @ERRORMESSAGE <> ''
raiserror(@ERRORMESSAGE, 13, 1);
--Log reversals in the GLTRANSACTION table
exec dbo.USP_GLTRANSACTION_ADDSTOCKSALEREVERSALS @STOCKSALETODELETE, @CHANGEAGENTID, @CURRENTDATE, @POSTDATE;
end
else --Update the post date of the existing stock detail reversal rows to today's date
begin
update dbo.GLTRANSACTION
set
POSTDATE = dbo.UFN_DATE_GETEARLIESTTIME(@CURRENTDATE),
CHANGEDBYID = @CHANGEAGENTID,
DATECHANGED = @CURRENTDATE
where ID in (
select
REVERSAL.ID
from
dbo.STOCKSALEGLDISTRIBUTION
inner join
dbo.GLTRANSACTION on STOCKSALEGLDISTRIBUTION.GLTRANSACTIONID = GLTRANSACTION.ID
inner join
dbo.GLTRANSACTION REVERSAL on GLTRANSACTION.ID = REVERSAL.REVERSEDGLTRANSACTIONID
where
STOCKSALEGLDISTRIBUTION.STOCKSALEID = @STOCKSALETODELETE
and
REVERSAL.POSTSTATUSCODE = 1);
end
--Update the new ADJUSTMENTHISTORYSTOCK record with one of the newly created GLTRANSACTION reversal IDs
update dbo.ADJUSTMENTHISTORYSTOCK
set GLTRANSACTIONID = (
select top 1 REVERSAL.ID from dbo.STOCKSALEGLDISTRIBUTION
inner join dbo.GLTRANSACTION on STOCKSALEGLDISTRIBUTION.GLTRANSACTIONID = GLTRANSACTION.ID
inner join dbo.GLTRANSACTION REVERSAL on GLTRANSACTION.ID = REVERSAL.REVERSEDGLTRANSACTIONID
where STOCKSALEGLDISTRIBUTION.STOCKSALEID = @STOCKSALETODELETE
order by REVERSAL.DATEADDED desc)
where ID = @ADJUSTMENTHISTORYSTOCKID;
/* Delete rows from Stock Detail GL Distribution */
--Cache CONTEXT INFO
declare @contextCache varbinary(128);
set @contextCache = CONTEXT_INFO();
if not @CHANGEAGENTID is null
set CONTEXT_INFO @CHANGEAGENTID;
delete from dbo.STOCKSALEGLDISTRIBUTION where STOCKSALEID = @STOCKSALETODELETE and OUTDATED = 0;
--reset CONTEXT_INFO to previous value
if not @contextCache is null
set CONTEXT_INFO @contextCache;
if @STOCKSALETODELETE is not null
begin
update FT
set DELETEDON = @CURRENTDATE
from
dbo.FINANCIALTRANSACTION FT
where FT.ID = @STOCKSALETODELETE
update LI
set DELETEDON = null
from
dbo.FINANCIALTRANSACTION FT
inner join dbo.FINANCIALTRANSACTIONLINEITEM LI on LI.FINANCIALTRANSACTIONID = FT.ID
where FT.ID = @STOCKSALETODELETE
and LI.TYPECODE = 1
delete from dbo.FINANCIALTRANSACTION
where POSTSTATUSCODE = 1 and ID = @STOCKSALETODELETE
if exists (select 1 from dbo.STOCKSALE_EXT where ID = @STOCKSALETODELETE)
begin
delete from dbo.STOCKSALE_EXT where ID = @STOCKSALETODELETE;
end
end
end try
begin catch
exec dbo.USP_RAISE_ERROR;
return 1;
end catch
return 0;
end