USP_MAPPINGERROR_CREDIT_RECREATEDISTRIBUTION

Recreate the distribution for refunds if using the holding account.

Parameters

Parameter Parameter Type Mode Description
@CREDITID uniqueidentifier IN
@CHANGEAGENTID uniqueidentifier IN
@CURRENTDATE datetime IN
@PROCESSID uniqueidentifier IN

Definition

Copy


CREATE procedure dbo.USP_MAPPINGERROR_CREDIT_RECREATEDISTRIBUTION
(
    @CREDITID uniqueidentifier,
    @CHANGEAGENTID uniqueidentifier = null,
    @CURRENTDATE datetime = null,
    @PROCESSID uniqueidentifier = null
) as 
begin

    if @CHANGEAGENTID is null
        exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output;

    if @CURRENTDATE is null
        set @CURRENTDATE = getdate();

    declare @HASDEFAULTACCOUNT bit = 0;

    declare @DEFAULTGLACCOUNTID uniqueidentifier;

    select
        @DEFAULTGLACCOUNTID = S.DEFAULTGLACCOUNTID
    from dbo.UFN_PDACCOUNTSYSTEM_DEFAULTORSYSTEM() as S;

    if @DEFAULTGLACCOUNTID is not null
    begin
        if exists(select 1 
            from dbo.CREDITGLDISTRIBUTION D
            inner join dbo.GLTRANSACTION T on D.GLTRANSACTIONID = T.ID
            inner join dbo.CREDITPAYMENT CP on CP.ID = D.CREDITPAYMENTID
            where CP.CREDITID = @CREDITID and T.GLACCOUNTID = @DEFAULTGLACCOUNTID and T.POSTSTATUSCODE = 1)
        begin
            delete from dbo.GLTRANSACTION where ID in (select GLTRANSACTIONID from dbo.CREDITGLDISTRIBUTION D inner join dbo.CREDITPAYMENT CP on CP.ID = D.CREDITPAYMENTID where CP.CREDITID = @CREDITID and D.OUTDATED = 0) and POSTSTATUSCODE >= 1;
            delete from dbo.CREDITGLDISTRIBUTION where OUTDATED = 0 and CREDITPAYMENTID in (select ID from dbo.CREDITPAYMENT where CREDITID = @CREDITID);

            exec dbo.USP_REFUND_CREATEGLDISTRIBUTION @CREDITID, @CHANGEAGENTID, @CURRENTDATE;

            if exists(select 1 
                from dbo.CREDITGLDISTRIBUTION D
                inner join dbo.GLTRANSACTION T on D.GLTRANSACTIONID = T.ID
                inner join dbo.CREDITPAYMENT CP on CP.ID = D.CREDITPAYMENTID
                where CP.CREDITID = @CREDITID and T.GLACCOUNTID = @DEFAULTGLACCOUNTID)
                set @HASDEFAULTACCOUNT = 1;
        end

        if exists(select 1 
            from dbo.CREDITGLDISTRIBUTION D
            inner join dbo.GLTRANSACTION T on D.GLTRANSACTIONID = T.ID
            inner join dbo.CREDITITEM CI on CI.ID = D.CREDITITEMID
            where CI.CREDITID = @CREDITID and T.GLACCOUNTID = @DEFAULTGLACCOUNTID and T.POSTSTATUSCODE = 1)
        begin
            delete from dbo.GLTRANSACTION where ID in (select GLTRANSACTIONID from dbo.CREDITGLDISTRIBUTION D inner join dbo.CREDITITEM CI on CI.ID = D.CREDITITEMID where CI.CREDITID = @CREDITID and D.OUTDATED = 0) and POSTSTATUSCODE >= 1;
            delete from dbo.CREDITGLDISTRIBUTION where OUTDATED = 0 and CREDITITEMID in (select ID from dbo.CREDITITEM where CREDITID = @CREDITID);

            exec dbo.USP_REFUND_CREATEDISCOUNTGLDISTRIBUTION @CREDITID, @CHANGEAGENTID, @CURRENTDATE;

            if exists(select 1 
                from dbo.CREDITGLDISTRIBUTION D
                inner join dbo.GLTRANSACTION T on D.GLTRANSACTIONID = T.ID
                inner join dbo.CREDITITEM CI on CI.ID = D.CREDITITEMID
                where CI.CREDITID = @CREDITID and T.GLACCOUNTID = @DEFAULTGLACCOUNTID)
                set @HASDEFAULTACCOUNT = 1;
        end
    end

    if exists(select 1 from dbo.GLACCOUNTMAPPINGERROR where TRANSACTIONID = @CREDITID and DELETED = 0
        and ERRORMESSAGE in (select E.ERRORMESSAGE from dbo.GLACCOUNTMAPPINGERROR E where E.TRANSACTIONID = @CREDITID and E.GLACCOUNTMAPPINGERRORUPDATEPROCESSID = @PROCESSID))
        return 0
    else
    begin
        if @HASDEFAULTACCOUNT = 1
        begin
            update dbo.GLACCOUNTMAPPINGERROR set 
                DELETED = 0
                ,ADDRESSED = 0
                ,CHANGEDBYID = @CHANGEAGENTID
                ,DATECHANGED = @CURRENTDATE
            where TRANSACTIONID = @CREDITID;

            return 0
        end
        else
            return 1
    end

end