触发器:订单通知单加单

本文详细解析了一个SQL触发器的实现逻辑,该触发器用于维护数据库中订单通知单和销售订单之间的数据一致性。当更新特定状态时,触发器会检查是否存在重复的OrderNo和产品型号,防止数据冗余,并在满足条件时进行必要的数据插入和更新操作。
摘要由CSDN通过智能技术生成

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER trigger [GF_insert_ddtzd] on [dbo].[t_ddtzdIncrease]
for update 
as 


declare @FID int,@FStatus int,@FOldStatus int 

select @FID = FID,@FStatus = FMultiCheckStatus from inserted 
select @FOldStatus = FMultiCheckStatus from deleted 

if update(FMultiCheckStatus) and @FStatus = 4 and @FOldStatus = 2
begin
        if exists (select 1 from t_bosddtzd d1
    inner join t_bosddtzdentry d2 on d1.FID = d2.FID
    inner join (select FBillNo_SRC1,b.FOrderNo,b.FItemID from t_ddtzdIncrease  a
    inner join t_ddtzdIncreaseentry b on a.FID = b.FID
    where a.FID = @FID and a.FOrderType = '352190') t1 
    on d1.FBillNo = t1.FBillNo_SRC1 and d2.FOrderNo = t1.FOrderNo and d2.FItemID = t1.FItemID)
        begin
                RAISERROR('原订单通知单存在相同OrderNo相同产品型号,不允许加单',16,1) 
        end
end
 

if update(FMultiCheckStatus) and @FStatus = 16 and @FOldStatus = 4 
 
begin
declare @FID_1 int ,@FMAXindex int

select @FMAXindex = max(Findex),@FID_1 = FID from t_bosddtzdentry  
where FID in (select FID_SRC from t_ddtzdIncreaseentry where FID = @FID)
group by FID

declare @FinterID int
select @FinterID = a.FinterID from SEorder a
inner join SEorderentry b on a.FinterID = b.FinterID
where b.FSourceInterID in (select FID_SRC from t_ddtzdIncreaseentry where FID = @FID)


---插入订单通知单
    Insert Into t_bosddtzdentry (FID,Findex,ForderNo,FSBUpo,Fitem,FitemID,FSBusku,FitemDesc,FBrand,Funit,Fqty,FPrice,FAmount,
    FCasePack,FNoCartons,FDeliverTo,FFinalDest,FDateEstimate,FDateDue,FDateWG,FPackagCode,FLanguages,Fcontainer,Fstatus,FDateyh,FFree,FIncrease,FIncreaseID)

    select @FID_1,@FMAXindex+Findex,ForderNo,FSBUpo,Fitem,FitemID,FSBusku,FitemDesc,FBrand,Funit,Fqty,FPrice,FAmount,
    FCasePack,FNoCartons,FDeliverTo,FFinalDest,FDateEstimate,FDateDue,FDateWG,FPackagCode,FLanguages,Fcontainer,Fstatus,FDateyh,FFree,Findex,@FID
     from t_ddtzdIncrease  a
    inner join t_ddtzdIncreaseentry b on a.FID = b.FID
    where a.FID = @FID

---更新加单源单行号
    update b set b.FEntryID_SRC = t1.FEntryID,b.FID_SRC = t1.FID,b.FBillNO_SRC = t2.FBillNO,b.FClassID_SRC = t2.FClassTypeID from t_ddtzdIncrease  a
    inner join t_ddtzdIncreaseentry b on a.FID = b.FID
    inner join  t_bosddtzdentry  t1 on  b.Findex = t1.FIncrease and b.FID = t1.FIncreaseID
    inner join t_bosddtzd t2 on t1.FID = t2.FID
    where a.FID = @FID
---更新加单行号标志
--    update t_bosddtzdentry set  FIncrease = -1 from t_bosddtzdentry  
--    where FID in (select FID_SRC from t_ddtzdIncreaseentry where FID = @FID)
--    and FIncrease = 0


---插入销售订单 
 

         insert into  seorderentry (fbrno,fentryid,finterid,fitemid,fqty,funitid,FPrice,FAmount,fauxqty,fdate,fadviceconsigndate,
    fsourcetrantype,fsourceinterid,fsourcebillno,fsourceentryid,
    FauxPrice,FallAmount,FallstdAmount,FauxPriceDiscount,FPriceDiscount,FauxtaxPrice,FtaxPrice,FBOMCategory,fentryselfs0176,FEntrySelfS0177)

    select '0',a.findex,@FInterID,a.FitemID,a.fqty,a.funit,a.FPrice,a.FAmount,a.fqty,a.FDatewg,a.FDatewg, b.fclasstypeid,b.fid,b.fbillno,a.findex,
    a.FPrice,a.FAmount,a.FAmount*Fexchangerate,a.FPrice,a.FPrice,a.FPrice,a.FPrice,'36820',a.FDatewg,a.FForecastNo
    from t_bosddtzdentry  a
    inner join t_bosddtzd b on a.fid = b.fid
    inner join t_ddtzdIncreaseentry t1 on t1.Findex = a.FIncrease and t1.FID = a.FIncreaseID
    where a.FIncrease > 0 and a.FIncreaseID = @FID
    


end

CREATE trigger icstockbill_check --实现对物料单据管控 on Icstockbill for insert as Declare @Ftrantype int --单据类别定义/生产领料单24/调拔单41/销售出库单21 Select @Ftrantype=Ftrantype from inserted --控制领料单的领料日期不能小于生产任务单的计划开工日期 倒扣物料只能车间仓库发料 if (@Ftrantype=24) begin declare @icmo varchar(20) declare @message varchar(200) declare @message0 varchar(200) declare @message00 varchar(200) declare @message000 varchar(200) declare @finterid240 int set @message='错误!领料日期不能小于生产任务单计划开工日期,请与生管人员联系!错误单号:' set @message0='生产领料单发料仓库不正确,倒扣物料不能从 原料仓/半成品仓/成品仓 发料' set @message00='已完工生产任务单不能跨月领料.请检查领料日期!' set @message000='生产领料单必需关联源生产任务单号,请重新录入单据!' if exists ( select t1.fbillno from inserted as t1 inner join icstockbillentry as t2 on t2.finterid=t1.finterid where t2.ficmointerid=0 ) begin RAISERROR(@message000,18,18) ROLLBACK end if exists( select t3.fbillno from inserted as t1 inner join icstockbillentry as t2 on t2.finterid=t1.finterid inner join icmo as t3 on t3.finterid=t2.fsourceinterid where t2.fsourceinterid>0 and t1.fdate0 and t1.fdateCast(year(max(t4.fdate)) as int(10))*100+Cast(month(max(t4.fdate)) as int(5)) ) begin RAISERROR(@message00,18,18) ROLLBACK end --更新领料单上的销售订单号 select @finterid240=finterid from inserted update t2 set t2.fentryselfb0445=t4.fbillno from icstockbill as t1 inner join icstockbillentry as t2 on t1.ftrantype=24 and t1.finterid=t2.finterid inner join icmo as t3 on t3.finterid=t2.ficmointerid left join seorder as t4 on t4.finterid=t3.forderinterid where isnull(t4.fbillno,'no')'no' and t2.finterid=@finterid240 return end --控制委外加工生产任务单领料日期不对小于计划开工日期 if (@Ftrantype=28) begin declare @icmo3 varchar(20) declare @message3 varchar(200) set @message3='错误!委外加工发出日期不能小于对应委外加工生产任务单计划开工日期,请与采购人员联系变更!错误单号:' if exists( select t3.fbillno from inserted as t1 inner join icstockbillentry as t2 on t2.finterid=t1.finterid inner join icmo as t3 on t3.finterid=t2.fsourceinterid where t2.fsourceinterid>0 and t1.fdate0 and t1.fdate0 and t1.fdate0 and t1.fdate<t3.FPlanCommitDate set @message4=@message4+@icmo4 RAISERROR(@message4,18,18) ROLLBACK end ; --更新产品入库单上的销售订单号 select @finterid20=finterid from inserted update t2 set t2.fentryselfa0236=t4.fbillno from icstockbill as t1 inner join icstockbillentry as t2 on t1.ftrantype=2 and t1.finterid=t2.finterid inner join icmo as t3 on t3.finterid=t2.ficmointerid left join seorder as t4 on t4.finterid=t3.forderinterid where isnull(t4.fbillno,'no')'no' and t2.finterid=@finterid20 return end ---更新委外加工入单销售订单号 if (@Ftrantype=5) begin update t2 set t2.fentryselfa0548=t4.fbillno from inserted as t1 inner join icstockbillentry as t2 on t2.finterid=t1.finterid inner join icmo as t3 on t3.finterid=t2.fsourceinterid inner join seorder as t4 on t4.finterid=t3.forderinterid return end ---控制出全部出货的销售订单所对应的采购订单不能退料(红字外购入库单) /*if (@Ftrantype=1) begin declare @message500 varchar(200) set @message500='已全部出货的销售订单所对应的采购订单不能退料!请检查您所退料的采购订单号码是否正确!' if exists (select t1.fbillno from inserted as t1 inner join icstockbillentry as t2 on t2.finterid=t1.finterid inner join seorder as t3 on t3.fbillno=t2.fentryselfa0152 where t2.fqty<0 and t3.fclosed=1 ) begin RAISERROR(@message500,18,18) ROLLBACK end return end */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值