K3Cloud 存货收发存汇总表二开优化

2023-12-05 14:16:21 浏览数 (1)

业务背景

  1. 相较于EAS或者苍穹,老的存货收发汇总表,不能显示按类型区分的出以及入数据,故按照EAS的样式对其进行优化。
  2. 此业务场景为固定税率,可更具现场实际情况确定。

示例代码

create procedure [dbo].[SP_hsd_sfchz] ( @datefrom datetime,@dateto datetime ) as BEGIN

Set nocount on

代码语言:javascript复制
CREATE TABLE #KD_SFC (
		[公司编码]           varchar(60)    null, 
		[公司名称]           varchar(60)    null, 
		[分组编码]           varchar(60)    null, 
		[分组名称]           varchar(60)    null, 
        [年]           varchar(60)    null, 
        [月]           varchar(60)    null,
        [存货类别产品码]           varchar(60)    null, 
        [产存货类别名称]           varchar(60)    null,
        [产品代码]           varchar(1000)    null, 
        [产品名称]           varchar(100)    null,
        [规格]           varchar(150)    null, 
        [单位编码]           varchar(60)    null,    
        [单位名称]           varchar(60)    null,             
        [换算]       decimal(18, 6) default 0,        
        [上月结存数量]   decimal(18, 6) default 0,       
        [上月结存不含税金额]   decimal(18, 6) default 0,           
        [上月结存含税金额]            decimal(18, 6) default 0,       
        [本期采购入库数量]   decimal(18, 6) default 0,
        [本期采购入库税率]   decimal(18, 6) default 0,                
        [本期采购入库不含税金额]   decimal(18, 6) default 0,           
        [本期采购入库含税金额]            decimal(18, 6) default 0,       
        [本期生产入库数量]   decimal(18, 6) default 0,        
        [本期生产入库不含税金额]   decimal(18, 6) default 0,           
        [本期生产入库含税金额]            decimal(18, 6) default 0,     
        [本期其他入库数量]   decimal(18, 6) default 0,        
        [本期其他入库不含税金额]   decimal(18, 6) default 0,           
        [本期其他入库含税金额]            decimal(18, 6) default 0,        
        [本期其他类型入库数量]   decimal(18, 6) default 0,        
        [本期其他类型入库不含税金额]   decimal(18, 6) default 0,           
        [本期其他类型入库含税金额]            decimal(18, 6) default 0,       
        [本期其入库数量合计]   decimal(18, 6) default 0,        
        [本期入库不含税金额合计]   decimal(18, 6) default 0,           
        [本期入库含税金额合计]            decimal(18, 6) default 0,       
        [本期销售出库数量]   decimal(18, 6) default 0,
        [本期销售出库税率]   decimal(18, 6) default 0,                
        [本期销售出库不含税金额]   decimal(18, 6) default 0,           
        [本期销售出库含税金额]            decimal(18, 6) default 0,
		[本期销售出库成本]            decimal(18, 6) default 0,
        [本期领料计划数量]   decimal(18, 6) default 0,            
        [本期领料出库数量]   decimal(18, 6) default 0, 
        [本期领料计划含税金额]            decimal(18, 6) default 0,        
        [本期领料出库不含税金额]   decimal(18, 6) default 0,           
        [本期领料出库含税金额]            decimal(18, 6) default 0,     
        [本期其他出库数量]   decimal(18, 6) default 0,
        [数量偏差]   decimal(18, 6) default 0, 
        [金额偏差]   decimal(18, 6) default 0,                
        [本期其他出库不含税金额]   decimal(18, 6) default 0,           
        [本期其他出库含税金额]            decimal(18, 6) default 0,        
        [本期其他类型出库数量]   decimal(18, 6) default 0,        
        [本期其他类型出库不含税金额]   decimal(18, 6) default 0,           
        [本期其他类型出库含税金额]            decimal(18, 6) default 0,       
        [本期出库数量合计]   decimal(18, 6) default 0,        
        [本期出库不含税金额合计]   decimal(18, 6) default 0,           
        [本期出库含税金额合计]            decimal(18, 6) default 0, 
        [期末结存数量]   decimal(18, 6) default 0,        
        [期末含税单价]   decimal(18, 6) default 0,        
        [期末结存不含税金额]   decimal(18, 6) default 0,           
        [期末结存含税金额]            decimal(18, 6) default 0,                                          
          )
代码语言:javascript复制
      CREATE TABLE #KD_SF (
	[公司编码]           varchar(100)    null, 
	[公司名称]           varchar(100)    null, 
    [存货类别产品码]           varchar(100)    null, 
    [产存货类别名称]           varchar(100)    null,
    [产品代码]           varchar(100)    null, 
    [产品名称]           varchar(100)    null,
    [规格]           varchar(150)    null, 
    [单位编码]           varchar(100)    null,    
    [单位名称]           varchar(100)    null,  
    [本期采购入库数量]   decimal(18, 6) default 0,                   
    [本期采购入库税率]   decimal(18, 6) default 0,                
    [本期采购入库不含税金额]   decimal(18, 6) default 0,           
    [本期采购入库含税金额]            decimal(18, 6) default 0,       
    [本期生产入库数量]   decimal(18, 6) default 0,        
    [本期生产入库不含税金额]   decimal(18, 6) default 0,           
    [本期生产入库含税金额]            decimal(18, 6) default 0,     
    [本期其他入库数量]   decimal(18, 6) default 0,        
    [本期其他入库不含税金额]   decimal(18, 6) default 0,           
    [本期其他入库含税金额]            decimal(18, 6) default 0,          
    [本期销售出库数量]   decimal(18, 6) default 0,
    [本期销售出库税率]   decimal(18, 6) default 0,                
    [本期销售出库不含税金额]   decimal(18, 6) default 0,           
    [本期销售出库含税金额]            decimal(18, 6) default 0,
    [本期销售出库成本]            decimal(18, 6) default 0,
    [本期领料计划数量]   decimal(18, 6) default 0,            
    [本期领料出库数量]   decimal(18, 6) default 0, 
    [本期领料计划含税金额]            decimal(18, 6) default 0,        
    [本期领料出库不含税金额]   decimal(18, 6) default 0,           
    [本期领料出库含税金额]            decimal(18, 6) default 0,     
    [本期其他出库数量]   decimal(18, 6) default 0,      
    [本期其他出库不含税金额]   decimal(18, 6) default 0,           
    [本期其他出库含税金额]            decimal(18, 6) default 0                                                         
      )

– 期初计算 insert into #KD_SFC([公司编码],[公司名称],[存货类别产品码],[产存货类别名称],[分组编码],[分组名称],[单位编码],[单位名称],[年],[月],[产品代码], [产品名称],[规格],[上月结存数量],[上月结存不含税金额],[本期其入库数量合计],[本期入库含税金额合计],[本期出库数量合计],[本期出库不含税金额合计]) select gs.fnumber,gsa.fname,fz.FNUMBER fzno,fza.fname fzname,fzd.FNUMBER fzano,fze.fname fzaname,unit.fnumber,unita.fname,cha.fyear year,cha.FPERIOD mon ,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh ,sum(ch.fqty) qty,sum(ch.FAMOUNT) amount,sum(ch.FCurrentInQty) rkqty,sum(ch.FCurrentInAmount) rkamount,sum(ch.FOutStockQty) ckqty,sum(ch. FCurrentOutAmount) ckamount from T_HS_STOCKDIMENSION chb left join t_hs_balance_h ch on chb.FENTRYID=ch.FDIMEENTRYID left join T_HS_OUTACCTG cha on cha.fid=ch.fid left join T_BD_Material_L wl on chb.FMASTERID=wl.FMATERIALID left join T_BD_Material wla on chb.FMASTERID=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_BD_MATERIALGROUP fzd on substring(wla.fnumber,0,6)=fzd.fnumber left join T_BD_MATERIALGROUP_L fze on fzd.FID=fze.fid left join T_BD_MATERIALBASE wld on wld.FENTRYID=wl.FPKID left join t_BD_Stock kf on kf.FMASTERID=chb.FSTOCKID left join T_BD_UNIT unit on wld.FBASEUNITID=unit.FUNITID left join T_BD_UNIT_l unita on wld.FBaseUnitId=unita.FUNITID left join T_HS_CALDIMENSIONS org on cha.FDIMENSIONID=org.FDIMENSIONID left join T_ORG_ORGANIZATIONS gs on gs.FORGID=org.FFINORGID left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=org.FFINORGID and gsa.FNAME like ‘%华世%’ where cha.fyear=str(year(‘2023-09-01’)) and cha.FPERIOD=str(month(‘2023-09-01’)) and org.FFINORGID=1 and gsa.FNAME is not null and ch.FEndInitKey=0 group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,fzd.FNUMBER,fze.fname,unit.fnumber,unita.fname,cha.fyear ,cha.FPERIOD ,wla.fnumber ,wl.fname,wl.FSPECIFICATION

UNion all

select gs.fnumber,gsa.fname,fz.FNUMBER fzno,fza.fname fzname,fzd.FNUMBER fzano,fze.fname fzaname,unit.fnumber,unita.fname,cha.fyear year,cha.FPERIOD mon ,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh ,sum(ch.fqty) qty,sum(ch.FAMOUNT) amount,sum(ch.FCurrentInQty) rkqty,sum(ch.FCurrentInAmount) rkamount,sum(ch.FOutStockQty) ckqty,sum(ch. FCurrentOutAmount) ckamount from T_HS_STOCKDIMENSION chb left join t_hs_balance ch on chb.FENTRYID=ch.FDIMEENTRYID left join T_HS_OUTACCTG cha on cha.fid=ch.fid left join T_BD_Material_L wl on chb.FMASTERID=wl.FMATERIALID left join T_BD_Material wla on chb.FMASTERID=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_BD_MATERIALGROUP fzd on substring(wla.fnumber,0,6)=fzd.fnumber left join T_BD_MATERIALGROUP_L fze on fzd.FID=fze.fid left join T_BD_MATERIALBASE wld on wld.FENTRYID=wl.FPKID left join t_BD_Stock kf on kf.FMASTERID=chb.FSTOCKID left join T_BD_UNIT unit on wld.FBASEUNITID=unit.FUNITID left join T_BD_UNIT_l unita on wld.FBaseUnitId=unita.FUNITID left join T_HS_CALDIMENSIONS org on cha.FDIMENSIONID=org.FDIMENSIONID left join T_ORG_ORGANIZATIONS gs on gs.FORGID=org.FFINORGID left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=org.FFINORGID and gsa.FNAME like ‘%华世%’ where cha.fyear=str(year(‘2023-09-01’)) and cha.FPERIOD=str(month(‘2023-09-01’)) and org.FFINORGID=1 and gsa.FNAME is not null and ch.FEndInitKey=0 group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,fzd.FNUMBER,fze.fname,unit.fnumber,unita.fname,cha.fyear ,cha.FPERIOD ,wla.fnumber ,wl.fname,wl.FSPECIFICATION

—计算本期收发 insert into #KD_SF([公司编码],[公司名称],[存货类别产品码],[产存货类别名称],[产品代码],[产品名称],[规格],[单位编码],[单位名称],[本期生产入库数量],[本期生产入库不含税金额],[本期采购入库数量],[本期采购入库不含税金额],[本期采购入库含税金额],[本期其他入库数量],[本期其他入库不含税金额],[本期领料出库数量],[本期领料出库不含税金额],[本期销售出库数量],[本期销售出库不含税金额],[本期销售出库含税金额],[本期销售出库成本],[本期其他出库数量],[本期其他出库不含税金额]) select tmp.gano, tmp.gsname, tmp.fzno, tmp.fzname, tmp.wlno, tmp.wlname, tmp.ggxh, tmp.unitno, tmp.unitname, sum(tmp.scrkbaseqty) ,sum(tmp.scrkamount), sum(tmp.cgrkqty),sum(tmp.cgrkamount),sum(tmp.cgrktaxamount), sum(tmp.qtrkqty),sum(tmp.qtrkamount), sum(tmp.llckqty),sum(tmp.llckamount), sum(tmp.xsckqty),sum(tmp.xsckamount),sum(tmp.xscktaxamount),sum(tmp. xsckcb), sum(tmp.qtckqty),sum(tmp.qtckamount) from( select ‘生产入库’ type,gs.fnumber gano,gsa.fname gsname,fz.FNUMBER fzno,fza.fname fzname,dep.fnumber cxno,dept.fname cxname,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh,unit.fnumber unitno,unita.fname unitname,sum(a.FBaseRealQty) scrkbaseqty,sum(a.FAMOUNT) scrkamount,0 cgrkqty,0 cgrkamount,0 cgrktaxamount,0 qtrkqty,0 qtrkamount,0 llckqty,0 llckamount,0 xsckqty,0 xsckamount,0 xscktaxamount,0 xsckcb,0 qtckqty,0 qtckamount from T_PRD_INSTOCKENTRY a left join T_PRD_INSTOCK b on a.fid =b.fid left join T_BD_UNIT unit on a.FBaseUnitId=unit.FUNITID left join T_BD_UNIT_l unita on a.FBaseUnitId=unita.FUNITID and unita.FLOCALEID=2052 left join T_BD_DEPARTMENT_L dept on dept.FDEPTID=a.FWorkShopId left join T_BD_DEPARTMENT dep on dep.FDEPTID=a.FWorkShopId left join T_BD_Material_L wl on a.FMaterialId=wl.FMATERIALID left join T_BD_Material wla on a.FMaterialId=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_ORG_ORGANIZATIONS gs on gs.FORGID=b.FPRDORGID left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=b.FPRDORGID and gsa.FNAME like ‘%华世%’

where b.fdate>=‘2023-09-01’ and b.fdate<='2023-09-30’and b.FPRDORGID like ‘1%’

group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,dep.fnumber,dept.fname,wla.fnumber,wl.fname,wl.FSPECIFICATION,unit.fnumber,unita.fname

union all

select ‘采购入库’ type,gs.fnumber gano,gsa.fname gsname,fz.FNUMBER fzno,fza.fname fzname,dep.fnumber cxno,dept.fname cxname,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh,unit.fnumber unitno,unita.fname unitname,0 scrkbaseqty,0 scrkamount,sum(a.FRealQty) cgrkqty,sum(c.FBillAmount) cgrkamount, sum(c.FBillAllAmount) cgrktaxamount,0 qtrkqty,0 qtrkamount,0 llckqty,0 llckamount,0 xsckqty,0 xsckamount,0 xscktaxamount,0 xsckcb,0 qtckqty,0 qtckamount from T_STK_INSTOCKENTRY a left join T_STK_INSTOCKFIN c on a.fid=c.fid left join T_STK_INSTOCK b on a.fid =b.fid left join T_BD_UNIT unit on a.FBaseUnitId=unit.FUNITID left join T_BD_UNIT_l unita on a.FBaseUnitId=unita.FUNITID and unita.FLOCALEID=2052 left join T_BD_DEPARTMENT_L dept on dept.FDEPTID=b.FStockDeptId left join T_BD_DEPARTMENT dep on dep.FDEPTID=b.FStockDeptId left join T_BD_Material_L wl on a.FMaterialId=wl.FMATERIALID left join T_BD_Material wla on a.FMaterialId=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_ORG_ORGANIZATIONS gs on gs.FORGID=b.FPurchaseOrgId left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=b.FPurchaseOrgId and gsa.FNAME like ‘%华世%’

where b.fdate>=‘2023-09-01’ and b.fdate<='2023-09-30’and b.FPurchaseOrgId=1

group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,dep.fnumber,dept.fname,wla.fnumber,wl.fname,wl.FSPECIFICATION,unit.fnumber,unita.fname

union all

select ‘其他入库’ type,gs.fnumber gano,gsa.fname gsname,fz.FNUMBER fzno,fza.fname fzname,dep.fnumber cxno,dept.fname cxname,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh,unit.fnumber unitno,unita.fname unitname,0 scrkbaseqty,0 scrkamount,0 cgrkqty,0 cgrkamount,0 cgrktaxamount,sum(a.FQty) qtrkqty,sum(a.FAMOUNT) qtrkamount,0 llckqty,0 llckamount,0 xsckqty,0 xsckamount,0 xscktaxamount,0 xsckcb,0 qtckqty,0 qtckamount from T_STK_MISCELLANEOUSENTRY a left join T_STK_MISCELLANEOUS b on a.fid =b.fid left join T_BD_UNIT unit on a.FBaseUnitId=unit.FUNITID left join T_BD_UNIT_l unita on a.FBaseUnitId=unita.FUNITID and unita.FLOCALEID=2052 left join T_BD_DEPARTMENT_L dept on dept.FDEPTID=b.FDEPTID left join T_BD_DEPARTMENT dep on dep.FDEPTID=b.FDEPTID left join T_BD_Material_L wl on a.FMaterialId=wl.FMATERIALID left join T_BD_Material wla on a.FMaterialId=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_ORG_ORGANIZATIONS gs on gs.FORGID=b.FStockOrgId left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=b.FStockOrgId and gsa.FNAME like ‘%华世%’

where b.fdate>=‘2023-09-01’ and b.fdate<='2023-09-30’and b.FStockOrgId like ‘1%’

group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,dep.fnumber,dept.fname,wla.fnumber,wl.fname,wl.FSPECIFICATION,unit.fnumber,unita.fname

union all

select ‘其他出库’ type,gs.fnumber gano,gsa.fname gsname,fz.FNUMBER fzno,fza.fname fzname,dep.fnumber cxno,dept.fname cxname,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh,unit.fnumber unitno,unita.fname unitname,0 scrkbaseqty,0 scrkamount,0 cgrkqty,0 cgrkamount,0 cgrktaxamount,0 qtrkqty,0 qtrkamount,0 llckqty,0 llckamount,0 xsckqty,0 xsckamount,0 xscktaxamount,0 xsckcb,sum(a.FQty) qtckqty,sum(a.FAMOUNT) qtckamount from T_STK_MISDELIVERYENTRY a left join T_STK_MISDELIVERY b on a.fid =b.fid left join T_BD_UNIT unit on a.FBaseUnitId=unit.FUNITID left join T_BD_UNIT_l unita on a.FBaseUnitId=unita.FUNITID and unita.FLOCALEID=2052 left join T_BD_DEPARTMENT_L dept on dept.FDEPTID=b.FDEPTID left join T_BD_DEPARTMENT dep on dep.FDEPTID=b.FDEPTID left join T_BD_Material_L wl on a.FMaterialId=wl.FMATERIALID left join T_BD_Material wla on a.FMaterialId=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_ORG_ORGANIZATIONS gs on gs.FORGID=b.FStockOrgId left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=b.FStockOrgId and gsa.FNAME like ‘%华世%’

where b.fdate>=‘2023-09-01’ and b.fdate<='2023-09-30’and b.FStockOrgId like ‘1%’

group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,dep.fnumber,dept.fname,wla.fnumber,wl.fname,wl.FSPECIFICATION,unit.fnumber,unita.fname

union all

select ‘销售出库’ type,gs.fnumber gano,gsa.fname gsname,fz.FNUMBER fzno,fza.fname fzname,dep.fnumber cxno,dept.fname cxname,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh,unit.fnumber unitno,unita.fname unitname,0 scrkbaseqty,0 scrkamount,0 cgrkqty,0cgrkamount, 0 cgrktaxamount,0 qtrkqty,0 qtrkamount,0 llckqty,0 llckamount,sum(a.FRealQty) xsckqty,sum(c.FBillAmount) xsckamount,sum(c.FBillAllAmount) xscktaxamount,sum(c.FBillCostAmount_LC) xsckcb,0 qtckqty,0 qtckamount from T_SAL_OUTSTOCKENTRY a left join T_SAL_OUTSTOCKFIN c on a.fid=c.fid left join T_SAL_OUTSTOCK b on a.fid =b.fid left join T_BD_UNIT unit on a.FBaseUnitId=unit.FUNITID left join T_BD_UNIT_l unita on a.FBaseUnitId=unita.FUNITID and unita.FLOCALEID=2052 left join T_BD_DEPARTMENT_L dept on dept.FDEPTID=b.FSaleDeptID left join T_BD_DEPARTMENT dep on dep.FDEPTID=b.FSaleDeptID left join T_BD_Material_L wl on a.FMaterialId=wl.FMATERIALID left join T_BD_Material wla on a.FMaterialId=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_ORG_ORGANIZATIONS gs on gs.FORGID=b.FSaleOrgId left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=b.FSaleOrgId and gsa.FNAME like ‘%华世%’

where b.fdate>=‘2023-09-01’ and b.fdate<='2023-09-30’and b.FSaleOrgId=1

group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,dep.fnumber,dept.fname,wla.fnumber,wl.fname,wl.FSPECIFICATION,unit.fnumber,unita.fname

union all

select ‘领料出库’ type,gs.fnumber gano,gsa.fname gsname,fz.FNUMBER fzno,fza.fname fzname,dep.fnumber cxno,dept.fname cxname,wla.fnumber wlno,wl.fname wlname,wl.FSPECIFICATION ggxh,unit.fnumber unitno,unita.fname unitname,0 scrkbaseqty,0scrkamount,0 cgrkqty,0 cgrkamount,0 cgrktaxamount,0 qtrkqty,0 qtrkamount,sum(a.FActualQty) llckqty,sum(a.FAMOUNT) llckamount,0 xsckqty,0 xsckamount,0 xscktaxamount,0 xsckcb,0 qtckqty,0 qtckamount from T_PRD_PICKMTRLDATA a left join T_PRD_PICKMTRL b on a.fid =b.fid left join T_BD_UNIT unit on a.FBaseUnitId=unit.FUNITID left join T_BD_UNIT_l unita on a.FBaseUnitId=unita.FUNITID and unita.FLOCALEID=2052 left join T_BD_DEPARTMENT_L dept on dept.FDEPTID=a.FWorkShopId left join T_BD_DEPARTMENT dep on dep.FDEPTID=a.FWorkShopId left join T_BD_Material_L wl on a.FMaterialId=wl.FMATERIALID left join T_BD_Material wla on a.FMaterialId=wla.FMATERIALID left join T_BD_MATERIALGROUP fz on substring(wla.fnumber,0,3)=fz.fnumber left join T_BD_MATERIALGROUP_L fza on fz.FID=fza.fid left join T_ORG_ORGANIZATIONS gs on gs.FORGID=b.FPRDORGID left join T_ORG_ORGANIZATIONS_l gsa on gsa.FORGID=b.FPRDORGID and gsa.FNAME like ‘%华世%’

where b.fdate>=‘2023-09-01’ and b.fdate<='2023-09-30’and b.FPRDORGID like ‘1%’

group by gs.fnumber,gsa.fname,fz.FNUMBER,fza.fname,dep.fnumber,dept.fname,wla.fnumber,wl.fname,wl.FSPECIFICATION,unit.fnumber,unita.fname ) tmp

group by tmp.gano,tmp.gsname,tmp.fzno,tmp.fzname,tmp.wlno,tmp.wlname,tmp.ggxh,tmp.unitno,tmp.unitname

order by tmp.gano,tmp.fzno,tmp.wlno

update #KD_SF set[本期采购入库税率]=[本期采购入库含税金额]/[本期采购入库不含税金额] where [本期采购入库不含税金额]<>0

update #KD_SF set[本期销售出库税率]=[本期销售出库含税金额]/[本期销售出库不含税金额] where [本期销售出库不含税金额]<>0

–数据更新

update #KD_SFC set [本期采购入库数量]=( select isnull([本期采购入库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库税率]= ( select isnull([本期采购入库税率],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库含税金额]=( select isnull([本期采购入库含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库不含税金额]= ( select isnull([本期采购入库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期生产入库数量]=( select isnull([本期生产入库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期生产入库不含税金额]= ( select isnull([本期生产入库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他入库数量]= ( select isnull([本期其他入库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他入库不含税金额]= ( select isnull([本期其他入库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库数量]= ( select isnull([本期销售出库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库税率]= ( select isnull([本期销售出库税率],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库不含税金额]= ( select isnull([本期销售出库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库含税金额]= ( select isnull([本期销售出库含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库成本]= ( select isnull([本期销售出库成本],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期领料出库数量]= ( select isnull([本期领料出库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期领料出库不含税金额]= ( select isnull([本期领料出库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他出库数量]= ( select isnull([本期其他出库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他出库不含税金额]= ( select isnull([本期其他出库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库数量]=( select isnull([本期采购入库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库税率]=( select isnull([本期采购入库税率],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库含税金额]= ( select isnull([本期采购入库含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期采购入库不含税金额]= ( select isnull([本期采购入库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期生产入库数量]=( select isnull([本期生产入库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期生产入库不含税金额]= (select isnull([本期生产入库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他入库数量]=( select isnull([本期其他入库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他入库不含税金额]=( select isnull([本期其他入库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库数量]=( select isnull([本期销售出库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库税率]=( select isnull([本期销售出库税率],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库不含税金额]=( select isnull([本期销售出库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库含税金额]= ( select isnull([本期销售出库含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期销售出库成本]= ( select isnull([本期销售出库成本],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期领料出库数量]= ( select isnull([本期领料出库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期领料出库不含税金额]=( select isnull([本期领料出库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他出库数量]=( select isnull([本期其他出库数量],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码]) update #KD_SFC set [本期其他出库不含税金额]= ( select isnull([本期其他出库不含税金额],0) from #KD_SF where #KD_SF.[公司编码] =#KD_SFC.[公司编码] and #KD_SF.[产品代码] =#KD_SFC.[产品代码])

– null值处理 update #KD_SFC set [本期采购入库数量]=isnull([本期采购入库数量],0) update #KD_SFC set [本期采购入库税率]= isnull([本期采购入库税率],0) update #KD_SFC set [本期采购入库含税金额]=isnull([本期采购入库含税金额],0) update #KD_SFC set [本期采购入库不含税金额]=isnull([本期采购入库不含税金额],0) update #KD_SFC set [本期生产入库数量]=isnull([本期生产入库数量],0) update #KD_SFC set [本期生产入库不含税金额]= isnull([本期生产入库不含税金额],0) update #KD_SFC set [本期其他入库数量]= isnull([本期其他入库数量],0) update #KD_SFC set [本期其他入库不含税金额]= isnull([本期其他入库不含税金额],0) update #KD_SFC set [本期销售出库数量]= isnull([本期销售出库数量],0) update #KD_SFC set [本期销售出库税率]= isnull([本期销售出库税率],0) update #KD_SFC set [本期销售出库不含税金额]= isnull([本期销售出库不含税金额],0) update #KD_SFC set [本期销售出库含税金额]=isnull([本期销售出库含税金额],0) update #KD_SFC set [本期销售出库成本]= isnull([本期销售出库成本],0) update #KD_SFC set [本期领料出库数量]= isnull([本期领料出库数量],0) update #KD_SFC set [本期领料出库不含税金额]= isnull([本期领料出库不含税金额],0) update #KD_SFC set [本期其他出库数量]=isnull([本期其他出库数量],0) update #KD_SFC set [本期其他出库不含税金额]= isnull([本期其他出库不含税金额],0) update #KD_SFC set [本期采购入库数量]=isnull([本期采购入库数量],0) update #KD_SFC set [本期采购入库税率]=isnull([本期采购入库税率],0) update #KD_SFC set [本期采购入库含税金额]= isnull([本期采购入库含税金额],0) update #KD_SFC set [本期采购入库不含税金额]=isnull([本期采购入库不含税金额],0) update #KD_SFC set [本期生产入库数量]=isnull([本期生产入库数量],0) update #KD_SFC set [本期生产入库不含税金额]= isnull([本期生产入库不含税金额],0) update #KD_SFC set [本期其他入库数量]=isnull([本期其他入库数量],0) update #KD_SFC set [本期其他入库不含税金额]= isnull([本期其他入库不含税金额],0) update #KD_SFC set [本期销售出库数量]=isnull([本期销售出库数量],0) update #KD_SFC set [本期销售出库税率]=isnull([本期销售出库税率],0) update #KD_SFC set [本期销售出库不含税金额]= isnull([本期销售出库不含税金额],0) update #KD_SFC set [本期销售出库含税金额]=isnull([本期销售出库含税金额],0) update #KD_SFC set [本期销售出库成本]= isnull([本期销售出库成本],0) update #KD_SFC set [本期领料出库数量]= isnull([本期领料出库数量],0) update #KD_SFC set [本期领料出库不含税金额]= isnull([本期领料出库不含税金额],0) update #KD_SFC set [本期其他出库数量]=isnull([本期其他出库数量],0) update #KD_SFC set [本期其他出库不含税金额]= isnull([本期其他出库不含税金额],0)

–更新税率

update #KD_SFC set [换算]= [本期采购入库税率] update #KD_SFC set [换算]= [本期销售出库税率] where [本期采购入库税率]=0

–计算含税 update #KD_SFC set [上月结存含税金额]= [上月结存不含税金额]*[换算] where [换算]<>0 update #KD_SFC set [上月结存含税金额]= [上月结存不含税金额] where [换算]=0

update #KD_SFC set [本期生产入库含税金额]= [本期生产入库不含税金额]*[换算] where [换算]<>0 update #KD_SFC set [本期生产入库含税金额]= [本期生产入库不含税金额] where [换算]=0

update #KD_SFC set [本期其他入库含税金额]= [本期其他入库不含税金额]*[换算] where [换算]<>0 update #KD_SFC set [本期其他入库含税金额]= [本期其他入库不含税金额] where [换算]=0

update #KD_SFC set [本期入库含税金额合计]= [本期入库不含税金额合计]*[换算] where [换算]<>0 update #KD_SFC set [本期入库含税金额合计]= [本期入库不含税金额合计] where [换算]=0

update #KD_SFC set [本期领料出库含税金额]= [本期领料出库不含税金额]*[换算] where [换算]<>0 update #KD_SFC set [本期领料出库含税金额]= [本期领料出库不含税金额] where [换算]=0

update #KD_SFC set [本期其他出库含税金额]= [本期其他出库不含税金额]*[换算] where [换算]<>0 update #KD_SFC set [本期其他出库含税金额]= [本期其他出库不含税金额] where [换算]=0

update #KD_SFC set [本期出库含税金额合计]= [本期出库不含税金额合计]*[换算] where [换算]<>0 update #KD_SFC set [本期出库含税金额合计]= [本期出库不含税金额合计] where [换算]=0

–计算期末结存 update #KD_SFC set [期末结存数量]= [上月结存数量] [本期其入库数量合计]-[本期出库数量合计] update #KD_SFC set [期末结存不含税金额]=[上月结存不含税金额] [本期入库不含税金额合计]-[本期出库不含税金额合计] update #KD_SFC set [期末结存含税金额]=[上月结存含税金额] [本期入库含税金额合计]-[本期出库含税金额合计] update #KD_SFC set [期末含税单价]=[期末结存含税金额]/[期末结存数量] where [期末结存数量]<>0 select * from #KD_SFC

end

0 人点赞