USP_PROGRAMDISCOUNT_DELETE

Executes the "Program Discount Delete" record operation.

Parameters

Parameter Parameter Type Mode Description
@ID uniqueidentifier IN Input parameter indicating the ID of the record being deleted.
@CHANGEAGENTID uniqueidentifier IN Input parameter indicating the ID of the change agent invoking the delete.
@DISCOUNTGROUPID uniqueidentifier IN

Definition

Copy


CREATE procedure dbo.USP_PROGRAMDISCOUNT_DELETE
(
    @ID uniqueidentifier,        -- This is the DISCOUNTID for the DISCOUNTGROUP; platform needs something called ID, and the PostActionEvent on the Discount page won't find the new page context ID if the DISCOUNTGROUP's ID is the action's context ID.    

    @CHANGEAGENTID uniqueidentifier,
    @DISCOUNTGROUPID uniqueidentifier = null
)
as begin
    if @CHANGEAGENTID is null  
        exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output

    if not exists (
        select DISCOUNTGROUPDETAILPROGRAM.PROGRAMID
        from dbo.DISCOUNTGROUPDETAILPROGRAM
        inner join dbo.DISCOUNTGROUPDETAIL on DISCOUNTGROUPDETAIL.ID = DISCOUNTGROUPDETAILPROGRAM.ID
        where DISCOUNTGROUPDETAIL.DISCOUNTGROUPID = @DISCOUNTGROUPID)
    begin
        raiserror('BBERR_NOTAPROGRAMDISCOUNT', 13, 1);
        return 1;
    end

    declare @PROGRAMID uniqueidentifier;
    select
        @PROGRAMID = DISCOUNTGROUPDETAILPROGRAM.PROGRAMID
    from dbo.DISCOUNTGROUP
    inner join dbo.DISCOUNTGROUPDETAIL on DISCOUNTGROUPDETAIL.DISCOUNTGROUPID = DISCOUNTGROUP.ID
    inner join dbo.DISCOUNTGROUPDETAILPROGRAM on DISCOUNTGROUPDETAILPROGRAM.ID = DISCOUNTGROUPDETAIL.ID
    where DISCOUNTGROUP.ID = @DISCOUNTGROUPID;

    declare @CLONEID uniqueidentifier;
    exec dbo.USP_DISCOUNT_CLONE @ID, @CLONEID output;

    -- delete cloned version instead.

    set @DISCOUNTGROUPID = (
        select top 1 DISCOUNTGROUP.ID
        from dbo.DISCOUNTGROUP
        inner join dbo.DISCOUNTGROUPDETAIL on DISCOUNTGROUPDETAIL.DISCOUNTGROUPID = DISCOUNTGROUP.ID
        inner join dbo.DISCOUNTGROUPDETAILPROGRAM on DISCOUNTGROUPDETAILPROGRAM.ID = DISCOUNTGROUPDETAIL.ID
        where DISCOUNTGROUP.DISCOUNTID = @CLONEID
            and DISCOUNTGROUPDETAILPROGRAM.PROGRAMID = @PROGRAMID
    )

    exec USP_DISCOUNTGROUP_DELETEBYID_WITHCHANGEAGENTID @DISCOUNTGROUPID, @CHANGEAGENTID;

    -- Update the DISCOUNT record to invalidate the cache for sales.

    update dbo.DISCOUNT
    set CHANGEDBYID = @CHANGEAGENTID, DATECHANGED = getdate()
    where ID = @ID;

    return 0;
end