--exec cgrk_all 6
--采购入库全功能
alter proc cgrk_all(@hid int)
as
set nocount on
begin
--入库量
declare @rksl table(ph varchar(100),pm varchar(100),cgdh varchar(100),rksl float,itemid int,hid int,ck varchar(100),location varchar(100),fid int)
--未存在库存的物料
declare @ph table(ph varchar(100))
insert into @rksl(ph,pm,cgdh,rksl,itemid,hid,ck,location,fid)
select ph,pm,cgdddh,rksl,itemid,hid,ck,location,fid from dwc_279 where hid=@hid
--采购入库回填
update dwc_232 set ckdigit=isnull(ckdigit,0)+isnull(rksl,0)
from dwd_232 a inner join dwc_232 b on a.hid=b.hid inner join @rksl c on a.dh=c.cgdh and b.hpbm=c.ph
--更新到库存
update ditw set onHand=isnull(onHand,0)+isnull(rksl,0)
from @rksl a inner join ditw b on a.ph=b.fathercode
--库存未存在,插入
insert into @ph(ph)
select ph from @rksl where ph not in (select fathercode from ditw)
insert into ditw(whscode,fathercode,onhand)
select ck,a.ph,rksl from @rksl a inner join @ph b on a.ph=b.ph
--插入明细
insert into dwsd(insertTime,whouseID,location,doccode,doctype,docdate,itemcode,periodid,innum,fid,hid)
select getdate(),ck,location,cgdh,'采购入库',getdate(),ph,convert(varchar(7),getdate(),120),rksl,fid,hid from @rksl
end