用友U8生产订单用料分析表包含开立,审核、锁定、关闭状态工单

---生产订单用料分析表包含开立,审核、锁定、关闭状态工单
CREATE Procedure [dbo].[P_Quick_MO0400601]

--[dbo].[Usp_MO_MO0400601]
              @sel nvarchar(1),  
          @diff bit =0, --是否只显示
              @ScrapFlag bit =1, --是否考虑损耗率
              @v_wherestr nvarchar(4000),
          @v_wherestr2 nvarchar(4000),    
              @v_user1 nvarchar(4000),--员工
          @v_user12 nvarchar(4000) = ' ',--员工
              @v_Dept2 nvarchar(4000),--部门
          @v_MoType3 nvarchar(4000),--类别
          @v_Factory4 nvarchar(4000), --工厂
              @v_tablename nvarchar(60)      
as  
    declare @MoClass1 nvarchar(40),@MoClass2 nvarchar(40),@MoClass3 nvarchar(40)
    select  @MoClass1 = max(case when enumindex = 1 then enumname else '' end),
            @MoClass2 = max(case when enumindex = 2 then enumname else '' end),
        @MoClass3 = max(case when enumindex = 3 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.MoClass'

    declare @Status1 nvarchar(40),@Status2 nvarchar(40),@Status3 nvarchar(40),@Status4 nvarchar(40)
    select  @Status1 = max(case when enumindex = 1 then enumname else '' end),
            @Status2 = max(case when enumindex = 2 then enumname else '' end),
        @Status3 = max(case when enumindex = 3 then enumname else '' end),
            @Status4 = max(case when enumindex = 4 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.Status'

    declare @OrderType0 nvarchar(40),@OrderType1 nvarchar(40),@OrderType2 nvarchar(40),@OrderType3 nvarchar(40),@OrderType7 nvarchar(40),@OrderType8 nvarchar(40)
    select  @OrderType0 = max(case when enumindex = 0 then enumname else '' end),
            @OrderType1 = max(case when enumindex = 1 then enumname else '' end),
            @OrderType2 = max(case when enumindex = 2 then enumname else '' end),
            @OrderType3 = max(case when enumindex = 3 then enumname else '' end),
        @OrderType7 = max(case when enumindex = 7 then enumname else '' end),
            @OrderType8 = max(case when enumindex = 8 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.DocType'

    declare @WIPType1 nvarchar(40),@WIPType2 nvarchar(40),@WIPType3 nvarchar(40),@WIPType4 nvarchar(40),@WIPType5 nvarchar(40)
    select  @WIPType1 = max(case when enumindex = 1 then enumname else '' end),
            @WIPType2 = max(case when enumindex = 2 then enumname else '' end),
        @WIPType3 = max(case when enumindex = 3 then enumname else '' end),
            @WIPType4 = max(case when enumindex = 4 then enumname else '' end),
            @WIPType5 = max(case when enumindex = 5 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.WIPType'

    declare @OpScheduleType1 nvarchar(40),@OpScheduleType2 nvarchar(40),@OpScheduleType3 nvarchar(40)
    select  @OpScheduleType1 = max(case when enumindex = 1 then enumname else '' end),
            @OpScheduleType2 = max(case when enumindex = 2 then enumname else '' end),
        @OpScheduleType3 = max(case when enumindex = 3 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.OpScheduleType'

    declare @BomType0 nvarchar(40),@BomType1 nvarchar(40),@BomType2 nvarchar(40),@BomType3 nvarchar(40),@BomType5 nvarchar(40),@BomType6 nvarchar(40)
    select  @BomType0 = max(case when enumindex = 0 then enumname else '' end),
            @BomType1 = max(case when enumindex = 1 then enumname else '' end),
            @BomType2 = max(case when enumindex = 2 then enumname else '' end),
        @BomType3 = max(case when enumindex = 3 then enumname else '' end),
            @BomType5 = max(case when enumindex = 5 then enumname else '' end),
            @BomType6 = max(case when enumindex = 6 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.BomType'

    declare @RountingType0 nvarchar(40),@RountingType1 nvarchar(40),@RountingType2 nvarchar(40),@RountingType5 nvarchar(40),@RountingType6 nvarchar(40)
    select  @RountingType0 = max(case when enumindex = 0 then enumname else '' end),
            @RountingType1 = max(case when enumindex = 1 then enumname else '' end),
            @RountingType2 = max(case when enumindex = 2 then enumname else '' end),
        @RountingType5 = max(case when enumindex = 5 then enumname else '' end),
            @RountingType6 = max(case when enumindex = 6 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.RountingType'


    declare @BoolType0 nvarchar(40),@BoolType1 nvarchar(40)
    select  @BoolType0 = max(case when enumindex = 0 then enumname else '' end),
            @BoolType1 = max(case when enumindex = 1 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.BoolType'

    declare @CollectiveFlag0 nvarchar(40),@CollectiveFlag1 nvarchar(40),@CollectiveFlag2 nvarchar(40)
    select  @CollectiveFlag0 = max(case when enumindex = 0 then enumname else '' end),
            @CollectiveFlag1 = max(case when enumindex = 1 then enumname else '' end),
        @CollectiveFlag2 = max(case when enumindex = 2 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.CollectiveFlag'

    declare @SoType0 nvarchar(40),@SoType1 nvarchar(40),@SoType3 nvarchar(40),@SoType4 nvarchar(40),@SoType5 nvarchar(40),@SoType6 nvarchar(40)
    select  @SoType0 = max(case when enumindex = 0 then enumname else '' end),
            @SoType1 = max(case when enumindex = 1 then enumname else '' end),
            @SoType3 = max(case when enumindex = 3 then enumname else '' end),
            @SoType4 = max(case when enumindex = 4 then enumname else '' end),
            @SoType5 = max(case when enumindex = 5 then enumname else '' end),
            @SoType6 = max(case when enumindex = 6 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MO.SoType'

    declare @DPartAttr1 nvarchar(40),@DPartAttr2 nvarchar(40),@DPartAttr3 nvarchar(40),@DPartAttr4 nvarchar(40),@DPartAttr5 nvarchar(40),@DPartAttr6 nvarchar(40),@DPartAttr7 nvarchar(40),@DPartAttr8 nvarchar(40)
    select  @DPartAttr1 = max(case when enumindex = 1 then enumname else '' end),
            @DPartAttr2 = max(case when enumindex = 2 then enumname else '' end),
            @DPartAttr3 = max(case when enumindex = 3 then enumname else '' end),
            @DPartAttr4 = max(case when enumindex = 4 then enumname else '' end),
            @DPartAttr5 = max(case when enumindex = 5 then enumname else '' end),
            @DPartAttr6 = max(case when enumindex = 6 then enumname else '' end),
        @DPartAttr7 = max(case when enumindex = 7 then enumname else '' end),
            @DPartAttr8 = max(case when enumindex = 8 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'MM.PartType'

    declare @ProductType1 nvarchar(40),@ProductType2 nvarchar(40),@ProductType3 nvarchar(40)
    select  @ProductType1 = max(case when enumindex = 1 then enumname else '' end),
            @ProductType2 = max(case when enumindex = 2 then enumname else '' end),
        @ProductType3 = max(case when enumindex = 3 then enumname else '' end)
    from aa_enum where localeid = dbo.UDF_GetLocaleID() and enumtype = 'BO.ProductType'
    
    declare @strSelect1 nvarchar(4000),@strSelect2 nvarchar(4000),@strSelect3 nvarchar(4000),@strSelect4 nvarchar(4000),@strOnhand nvarchar(4000),@strFrom1 nvarchar(4000),@strFrom2 nvarchar(4000),@strWhere nvarchar(4000),@strOrderBY nvarchar(4000)
 
    select @strSelect1='SELECT  distinct mom_order.MoCode AS 生产订单号码,mom_orderdetail.SortSeq AS 行号,
             CONVERT(nvarchar(40),case when coalesce(mom_remorder.MoDId,0) <> 0 then ''' + @MoClass3 +  ''' when mom_orderdetail.MoClass=1 then ''' + @MoClass1 +  ''' when  mom_orderdetail.MoClass=2 then ''' + @MoClass2 +  ''' else CONVERT(varchar(40),mom_orderdetail.MoClass)  end ) AS 类型, 
             mom_orderdetail.InvCode AS 物料编码,i.cInvAddCode AS 物料代号,i.cInvName AS 物料名称,i.cInvStd AS 物料规格,bb.cBasEngineerFigNo as 工程图号,mom_orderdetail.Free1 AS cFree1,mom_orderdetail.Free2 AS cFree2,mom_orderdetail.Free3 AS cFree3,mom_orderdetail.Free4 AS cFree4,mom_orderdetail.Free5 AS cFree5,mom_orderdetail.Free6 AS cFree6,mom_orderdetail.Free7 AS cFree7,mom_orderdetail.Free8 AS cFree8,mom_orderdetail.Free9 AS cFree9,mom_orderdetail.Free10 AS cFree10, i.cInvDefine1 AS cInvDefine1, i.cInvDefine2 AS cInvDefine2, i.cInvDefine3 AS cInvDefine3, i.cInvDefine4 AS cInvDefine4, i.cInvDefine5 AS cInvDefine5, i.cInvDefine6 AS cInvDefine6, i.cInvDefine7 AS cInvDefine7,i.cInvDefine8 AS cInvDefine8, i.cInvDefine9 AS cInvDefine9, i.cInvDefine10 AS cInvDefine10, i.cInvDefine11 AS cInvDefine11, i.cInvDefine12 AS cInvDefine12, i.cInvDefine13 AS cInvDefine13, i.cInvDefine14 AS cInvDefine14, i.cInvDefine15 AS cInvDefine15, i.cInvDefine16 AS cInvDefine16,  
             i.cComUnitCode AS 计量单位编码,  
             mom_motype.MotypeCode AS 订单类别, mom_motype.Description AS 类别说明,
             sfc_workcenter.WcCode AS 生产线,sfc_workcenter.Description AS 生产线名称, 
             mom_orderdetail.Qty AS 生产订单数量, mom_orderdetail.MrpQty AS MRP净算量,mom_orderdetail.ChangeRate as 换算率,mom_orderdetail.AuxQty as 辅助生产量,mom_orderdetail.QualifiedInQty AS 入库数量,  
             mom_orderdetail.MDeptCode AS 生产部门,Department.cDepName AS 部门名称, 
         coalesce(mom_morder.StartDate,mom_remorder.FUSD) as 开工日期,coalesce(mom_morder.DueDate,mom_remorder.LUCD) as 完工日期,
             mom_orderdetail.OrderCode AS 销售订单,mom_orderdetail.OrderSeq AS 销售订单行号,
         CONVERT(nvarchar(40),case mom_orderdetail.OrderType when 0 then ''' + @OrderType0 +  ''' when 1 then ''' + @OrderType1 +  ''' when 2 then ''' + @OrderType2 +  ''' when 3 then ''' + @OrderType3 + ''' when 7 then ''' + @OrderType7 + '''  when 8 then ''' + @OrderType8 + '''  end ) AS 销售订单类别,
             Customer.cCusCode AS 客户代号,Customer.ccusname AS 客户名称, 
             Reason.cReasonCode AS 原因码,Reason.cReasonName AS 原因说明, 
             CONVERT(nvarchar(40),(case mom_orderdetail.Status when 1 then ''' + @Status1 +  ''' when 2 then ''' + @Status2 +  ''' when 3 then ''' + @Status3 +  ''' when 4 then ''' + @Status4 +  ''' else CONVERT(varchar(40),mom_orderdetail.Status)  end)) AS 状态,
             mom_orderdetail.Remark AS 备注,mom_orderdetail.SourceMoCode as SourceMoCode,mom_orderdetail.SourceMoSeq as SourceMoSeq,mom_orderdetail.SourceQCCode as SourceQCCode,mom_orderdetail.MoLotCode AS 生产批号, 
             mom_orderdetail.DemandCode as DemandCode,DemandCodeDesc = AA_RequirementClass.cRClassName, mom_orderdetail.SoCode AS SoCode,mom_orderdetail.SoSeq AS SoSeq,
           CollectiveFlag = CONVERT(nvarchar(40),case mom_orderdetail.CollectiveFlag when 0 then ''' + @CollectiveFlag0 +  ''' when 1 then ''' + @CollectiveFlag1 +  ''' when 2 then ''' + @CollectiveFlag2 +  '''  end),
           SoType = CONVERT(nvarchar(40),case mom_orderdetail.SoType when 0 then ''' + @SoType0 +  ''' when 1 then ''' + @SoType1 +  ''' when 3 then ''' + @SoType3  +  ''' when 4 then ''' + @SoType4 + ''' when 5 then ''' + @SoType5 + ''' when 6 then ''' + @SoType6 + ''' end),
         mom_order.Define1 AS cDefine1,mom_order.Define2 AS cDefine2,mom_order.Define3 AS cDefine3,mom_order.Define4 AS cDefine4,mom_order.Define5 AS cDefine5,mom_order.Define6 AS cDefine6,mom_order.Define7 AS cDefine7,mom_order.Define8 AS cDefine8,mom_order.Define9 AS cDefine9,mom_order.Define10 AS cDefine10,mom_order.Define11 AS cDefine11,mom_order.Define12 AS cDefine12,mom_order.Define13 AS cDefine13,mom_order.Define14 AS cDefine14,mom_order.Define15 AS cDefine15,mom_order.Define16 AS cDefine16,'
    --U82021052000524
    --U82022111500894 md
    select @strSelect2='DCostWIPRelFlag = CONVERT(nvarchar(40),case mom_moallocate.CostWIPRel when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  '''  end),
         DRequisitionFlag = CONVERT(nvarchar(40),case mom_moallocate.RequisitionFlag when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  '''  end),
         DProductType = CONVERT(nvarchar(40),case mom_moallocate.ProductType when 1 then ''' + @ProductType1 +  ''' when 2 then ''' + @ProductType2 +  ''' when 3 then ''' + @ProductType3 +  '''  end),
         CONVERT(nvarchar(40),case mom_moallocate.ByproductFlag when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  ''' else CONVERT(varchar(40),mom_moallocate.ByproductFlag) end) AS 产出品,  
         mom_moallocate.ComponentId as ComponentId,coalesce(mom_moallocate.Remark,'''') AS 子件备注,mom_moallocate.InvCode AS 子件物料编码,
             mom_moallocate.Free1 AS 子件cFree1,mom_moallocate.Free2 AS 子件cFree2,mom_moallocate.Free3 AS 子件cFree3,mom_moallocate.Free4 AS 子件cFree4,mom_moallocate.Free5 AS 子件cFree5,mom_moallocate.Free6 AS 子件cFree6,mom_moallocate.Free7 AS 子件cFree7,mom_moallocate.Free8 AS 子件cFree8,mom_moallocate.Free9 AS 子件cFree9,mom_moallocate.Free10 AS 子件cFree10,
         OrderInvCode = case when mom_orderdetail.OrderType = 1 then so_sodetails.cInvCode when mom_orderdetail.OrderType = 3 then ex_orderdetail.cInvCode when mom_orderdetail.OrderType = 2 then v_mps_forecastdetail_rpt.InvCode when mom_orderdetail.OrderType = 7 then md.InvCode else null end,
         BomType = CONVERT(nvarchar(40),(case mom_orderdetail.BomType when 0 then ''' + @BomType0 +  ''' when 1 then ''' + @BomType1 +  ''' when 2 then ''' + @BomType2  +  ''' when 3 then ''' + @BomType3 + ''' when 5 then ''' + @BomType5 + ''' when 6 then ''' + @BomType6 + '''  else CONVERT(varchar(40),mom_orderdetail.BomType) end)),
         BomVersion = coalesce(bom_bom.Version,ecn_bom.Version),BomVersionDesc = coalesce(bom_bom.VersionDesc,ecn_bom.VersionDesc),BomIdentCode = coalesce(bom_bom.IdentCode,ecn_bom.IdentCode),BomIdentDesc = coalesce(bom_bom.IdentDesc,ecn_bom.IdentDesc), 
         mom_orderdetail.AuxUnitCode, mom_order.MoId as MoId,mom_orderdetail.MoDId as MoDId,mom_moallocate.AuxUnitCode as DAuxUnitCode,mom_moallocate.ChangeRate as DChangeRate, 
         isnull(mom_orderdetail.FactoryCode,'''') as FactoryCode, vf.FactoryName as FactoryName,isnull(mom_moallocate.FactoryCode,'''') as DFactoryCode, vf1.FactoryName as DFactoryName,
         DWIPType = CONVERT(nvarchar(40),case mom_moallocate.WIPType when 1 then ''' + @WIPType1 +  ''' when 2 then ''' + @WIPType2 +  ''' when 3 then ''' + @WIPType3  +  ''' when 4 then ''' + @WIPType4 + ''' when 5 then ''' + @WIPType5 + ''' end) '
             + ' into #tmp_t '
    --U82022030400594
    --U82022111500894 md
    select @strFrom1 = ' FROM mom_order with (nolock) inner join  mom_orderdetail with (nolock) on  mom_orderdetail.MoId = mom_order.MoId   
             left outer join mom_morder with (nolock) on mom_morder.MoDId = mom_orderdetail.MoDId  
         inner join mom_moallocate on mom_moallocate.MoDId = mom_orderdetail.MoDId 
             left outer join mom_motype with (nolock) on mom_motype.MoTypeId = mom_orderdetail.MoTypeId  
             left outer join Department with (nolock) on Department.cDepCode = mom_orderdetail.MDeptCode  
             left outer join mom_remorder with (nolock) on mom_remorder.MoDId = mom_orderdetail.MoDId               
             inner join Inventory as i with (nolock) on i.cInvCode = mom_orderdetail.InvCode                
         inner join bas_part as bb with (nolock) on bb.PartId = mom_orderdetail.PartId
         LEFT OUTER JOIN bom_bom with (nolock) on bom_bom.BomId = mom_orderdetail.BomId AND bom_bom.BomType = mom_orderdetail.BomType
              LEFT OUTER JOIN ecn_bom with (nolock) on ecn_bom.EBomId = mom_orderdetail.BomId AND ecn_bom.BomType = (mom_orderdetail.BomType - 4)
             LEFT OUTER JOIN so_sodetails with (nolock) on mom_orderdetail.OrderType = 1 and mom_orderdetail.OrderDId = so_sodetails.isosid 
             LEFT OUTER JOIN ex_orderdetail with (nolock) on mom_orderdetail.OrderType = 3 and mom_orderdetail.OrderDId = ex_orderdetail.autoid
         left outer join v_mps_forecastdetail_rpt with (nolock) on mom_orderdetail.OrderType = 2 and mom_orderdetail.OrderDId = v_mps_forecastdetail_rpt.ForecastDId
         left outer join mom_orderdetail as md with (nolock) on mom_orderdetail.OrderType = 7 and mom_orderdetail.OrderDId = md.MoDId 
             LEFT OUTER JOIN Customer  with (nolock) on Customer.ccuscode = mom_orderdetail.CustCode
             LEFT OUTER JOIN Reason with (nolock) on Reason.cReasonCode =mom_orderdetail.ReasonCode  
             left outer join sfc_workcenter with (nolock) on mom_remorder.WcId = sfc_workcenter.WcId 
         LEFT OUTER JOIN AA_RequirementClass with (nolock) on AA_RequirementClass.cRClassCode = mom_orderdetail.DemandCode 
         LEFT OUTER JOIN v_bas_Factory as vf with (nolock) on vf.FactoryCode = mom_orderdetail.FactoryCode 
         LEFT OUTER JOIN v_bas_Factory as vf1 with (nolock) on vf1.FactoryCode = mom_moallocate.FactoryCode '   
--              LEFT OUTER JOIN mom_mormk with (nolock) on mom_mormk.MoDId = mom_orderdetail.MoDId AND mom_mormk.SortSeq = 1  

     if @v_user1 in ('','<ALL>')  select @v_user1 = ' and 1 = 1 '
     else if @v_user1 = '<NONE>' select @v_user1 = ' and 1 = 2 '
     else 
        BEGIN 
           select @v_user1 = ' and mom_order.CreateUser in (' + @v_user1 
       select @v_user12 =  @v_user12 + ') '
        END 

     if @v_Dept2 in ('','<ALL>')  select @v_Dept2 = ' and 1 = 1 '
     else if @v_Dept2 = '<NONE>' select @v_Dept2 = ' and 1 = 2 '
     else select @v_Dept2 = ' and (isnull(mom_orderdetail.Mdeptcode,'''') = '''' or mom_orderdetail.Mdeptcode in (' + @v_Dept2 + ')) '

     if @v_MoType3 in ('','<ALL>')  select @v_MoType3 = ' and 1 = 1 '
     else if @v_MoType3 = '<NONE>' select @v_MoType3 = ' and 1 = 2 '
     else select @v_MoType3 = ' and (isnull(mom_motype.MoTypeCode,'''') = '''' or mom_motype.MoTypeCode  in (' + @v_MoType3 + ')) '

     if @v_Factory4 in ('','<ALL>')  select @v_Factory4 = ' and 1 = 1 '
     else if @v_Factory4 = '<NONE>' select @v_Factory4 = ' and 1 = 2 '
     else select @v_Factory4 = ' and (isnull(mom_orderdetail.FactoryCode,'''') = '''' or mom_orderdetail.FactoryCode in (' + @v_Factory4 + ')) '

     select @strWhere=' where (mom_orderdetail.Status in(1,2,3,4) ) ' 

     --U82021042000542
     --U82021052000524
     SELECT @strSelect3 = 'select mom_moallocate.MoDId,mom_moallocate.ComponentId,mom_moallocate.InvCode,mom_moallocate.Free1,mom_moallocate.Free2,mom_moallocate.Free3,mom_moallocate.Free4,mom_moallocate.Free5,mom_moallocate.Free6,mom_moallocate.Free7,mom_moallocate.Free8,mom_moallocate.Free9,mom_moallocate.Free10,t.产出品,coalesce(mom_moallocate.Remark,'''') as Remark,t.DCostWIPRelFlag,t.DRequisitionFlag,t.DWIPType,coalesce(t.DAuxUnitCode,'''') as DAuxUnitCode,coalesce(t.DChangeRate,0) as DChangeRate, TransQty = sum(mom_moallocate.TransQty),Qty = sum(case when ' + convert(nvarchar(1), @ScrapFlag) + ' = 1 or mom_moallocate.FVFlag = 0 then mom_moallocate.Qty else mom_moallocate.Qty / (1 + mom_moallocate.CompScrap / 100) end),IssQty=sum(mom_moallocate.IssQty) ,ReplenishQty=sum(mom_moallocate.ReplenishQty)                                   
                             into #tmp_moallocate
                             from mom_moallocate inner join #tmp_t t on mom_moallocate.MoDId = t.MoDId and mom_moallocate.ComponentId = t.ComponentId  
                      and t.子件cFree1 = mom_moallocate.Free1 and t.子件cFree2 = mom_moallocate.Free2 and t.子件cFree3 = mom_moallocate.Free3 and t.子件cFree4 = mom_moallocate.Free4 and t.子件cFree5 = mom_moallocate.Free5 
                      and t.子件cFree6 = mom_moallocate.Free6 and t.子件cFree7 = mom_moallocate.Free7 and t.子件cFree8 = mom_moallocate.Free8 and t.子件cFree9 = mom_moallocate.Free9 and t.子件cFree10 = mom_moallocate.Free10 
                  and coalesce(mom_moallocate.remark,'''') = coalesce(t.子件备注,'''') and CONVERT(nvarchar(40),case mom_moallocate.ByproductFlag when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  ''' else CONVERT(varchar(40),mom_moallocate.ByproductFlag) end) = t.产出品 and CONVERT(nvarchar(40),case mom_moallocate.CostWIPRel when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  '''  end) =t.DCostWIPRelFlag and CONVERT(nvarchar(40),case mom_moallocate.RequisitionFlag when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  '''  end) = t.DRequisitionFlag
                  and coalesce(mom_moallocate.AuxUnitCode,'''') = coalesce(t.DAuxUnitCode,'''') and coalesce(t.DChangeRate,0) = coalesce(mom_moallocate.ChangeRate,0)
                  and CONVERT(nvarchar(40),case mom_moallocate.WIPType when 1 then ''' + @WIPType1 +  ''' when 2 then ''' + @WIPType2 +  ''' when 3 then ''' + @WIPType3  +  ''' when 4 then ''' + @WIPType4 + ''' when 5 then ''' + @WIPType5 + ''' end) = t.DWIPType
                         group by mom_moallocate.MoDId,mom_moallocate.ComponentId,mom_moallocate.InvCode,mom_moallocate.Free1,mom_moallocate.Free2,mom_moallocate.Free3,mom_moallocate.Free4,mom_moallocate.Free5,mom_moallocate.Free6,mom_moallocate.Free7,mom_moallocate.Free8,mom_moallocate.Free9,mom_moallocate.Free10,t.产出品,coalesce(mom_moallocate.Remark,''''),t.DCostWIPRelFlag,t.DRequisitionFlag,t.DWIPType,coalesce(t.DAuxUnitCode,''''),coalesce(t.DChangeRate,0)  ' 
      --U82020071600240
      --U82021011800133 现存量不对
     SELECT @strOnhand = ' Select distinct t1.ComponentId,t1.Free1,t1.Free2,t1.Free3,t1.Free4,t1.Free5,t1.Free6,t1.free7,t1.free8,t1.free9,t1.free10,m.InspOnhFlag
                            into #tmp_onhandPart 
                from #tmp_moallocate t1
                inner join mom_parameter m on m.id = 1

                             Select t1.ComponentId,t1.Free1,t1.Free2,t1.Free3,t1.Free4,t1.Free5,t1.Free6,t1.free7,t1.free8,t1.free9,t1.free10,
                      sum(v.iQuantity) as onhand 
                                    into #tmp_onhand from v_fc_CurrentPartStock v,Warehouse  w , #tmp_onhandPart t1  where t1.ComponentId = v.partid   
                                           and (isnull(t1.Free1,'''') = '''' or t1.Free1=v.Free1) 
                           and (isnull(t1.Free2,'''') = '''' or t1.Free2=v.Free2) 
                           and (isnull(t1.Free3,'''') = '''' or t1.Free3=v.Free3) 
                           and (isnull(t1.Free4,'''') = '''' or t1.Free4=v.Free4) 
                           and (isnull(t1.Free5,'''') = '''' or t1.Free5=v.Free5) 
                           and (isnull(t1.Free6,'''') = '''' or t1.Free6=v.Free6) 
                           and (isnull(t1.Free7,'''') = '''' or t1.Free7=v.Free7) 
                           and (isnull(t1.Free8,'''') = '''' or t1.Free8=v.Free8) 
                           and (isnull(t1.Free9,'''') = '''' or t1.Free9=v.Free9) 
                           and (isnull(t1.Free10,'''') = '''' or t1.Free10=v.Free10) and w.cWhCode = v.cWhCode and w.bMRP=1
                          group by  t1.ComponentId,t1.Free1,t1.Free2,t1.Free3,t1.Free4,t1.Free5,t1.Free6,t1.free7,t1.free8,t1.free9,t1.free10 '
     --U82020071600240
     SELECT @strSelect4 = 'SELECT  t.*,OrderInvName = io.cInvName,OrderInvStd = io.cInvStd,a.cComUnitName AS 计量单位名称,b.cComUnitName as 辅助单位,ic.InvAddCode AS 子件物料代号,ic.InvName AS 子件物料名称,ic.InvStd AS 子件物料规格,bc.cBasEngineerFigNo as 子件工程图号,c.cComUnitName AS 子件计量单位,
                                   f.cComUnitName AS DAuxUnitName,AuxStandardQty = t1.Qty/t.DChangeRate,AuxActualQty = t1.IssQty/t.DChangeRate,
                  ic.InvDefine1 AS 子件cInvDefine1, ic.InvDefine2 AS 子件cInvDefine2, ic.InvDefine3 AS 子件cInvDefine3, ic.InvDefine4 AS 子件cInvDefine4, ic.InvDefine5 AS 子件cInvDefine5, ic.InvDefine6 AS 子件cInvDefine6, ic.InvDefine7 AS 子件cInvDefine7,ic.InvDefine8 AS 子件cInvDefine8, ic.InvDefine9 AS 子件cInvDefine9, ic.InvDefine10 AS 子件cInvDefine10, ic.InvDefine11 AS 子件cInvDefine11, ic.InvDefine12 AS 子件cInvDefine12, ic.InvDefine13 AS 子件cInvDefine13, ic.InvDefine14 AS 子件cInvDefine14, ic.InvDefine15 AS 子件cInvDefine15, ic.InvDefine16 AS 子件cInvDefine16,  
                 DPartAttr = CONVERT(nvarchar(40),case ic.InvAttr when 1 then ''' + @DPartAttr1 +  ''' when 2 then ''' + @DPartAttr2 +  ''' when 3 then ''' + @DPartAttr3  +  ''' when 4 then ''' + @DPartAttr4 + ''' when 5 then ''' + @DPartAttr5 + ''' when 6 then ''' + @DPartAttr6 + ''' when 7 then ''' + @DPartAttr7 + ''' when 8 then ''' + @DPartAttr8 + ''' end),                                  
                                  (case when ''' +  @sel + ''' = 1 then ic.InvRCost when ''' +  @sel + ''' = 2 then ic.InvSPrice else null end) AS 单位成本,   
                  t1.Qty AS 标准用量,t1.IssQty AS 实际用量,t1.ReplenishQty as ReplenishQty,t1.TransQty as TransQty, 
                              (case when ''' +  @sel + ''' = 1 then ic.InvRCost when ''' +  @sel + ''' = 2 then ic.InvSPrice else null end)*t1.Qty AS 标准成本,  
                          (case when ''' +  @sel + ''' = 1 then ic.InvRCost when ''' +  @sel + ''' = 2 then ic.InvSPrice else null end)*t1.IssQty AS 实际成本,  
                          (case when ''' +  @sel + ''' = 1 then ic.InvRCost when ''' +  @sel + ''' = 2 then ic.InvSPrice else null end)*(t1.Qty-t1.IssQty) AS 成本差异,
                 DiffRate = case when t1.Qty = 0 then null else (t1.IssQty - t1.Qty )/t1.Qty * 100 end,
                 DProCostFlag = CONVERT(nvarchar(40),case ic.ProCostFlag when 0 then ''' + @BoolType0 +  ''' when 1 then ''' + @BoolType1 +  '''  end),
                 DOnhand = th.Onhand
                             into ' + @v_tablename 
     --U82021042000542
     SELECT @strFrom2 =     ' from #tmp_t t 
               inner join #tmp_moallocate t1 on t.MoDId = t1.MoDId and 
                          t.ComponentId = t1.ComponentId and t.子件物料编码 = t1.InvCode and t.子件cFree1 = t1.Free1 and 
                  t.子件cFree2 = t1.Free2 and t.子件cFree3 = t1.Free3 and t.子件cFree4 = t1.Free4 and t.子件cFree5 = t1.Free5 and 
                  t.子件cFree6 = t1.Free6 and t.子件cFree7 = t1.Free7 and t.子件cFree8 = t1.Free8 and t.子件cFree9 = t1.Free9 and t.子件cFree10 = t1.Free10 and
                  coalesce(t.子件备注,'''') = t1.Remark and t.DCostWIPRelFlag = t1.DCostWIPRelFlag and t.DWIPType = t1.DWIPType and
                  t.DRequisitionFlag = t1.DRequisitionFlag and t.产出品 = t1.产出品 and coalesce(t.DAuxUnitCode,'''') = t1.DAuxUnitCode and coalesce(t.DChangeRate,0) = t1.DChangeRate
                  inner join v_bas_inventory as ic with (nolock) on ic.InvCode = t1.InvCode
          inner join bas_part as bc with (nolock) on bc.PartId = t1.ComponentId
          left outer join #tmp_onhand as th on th.ComponentId = t1.ComponentId and th.Free1 = t1.Free1 and th.Free2 = t1.Free2 and th.Free3 = t1.Free3 and th.Free4 = t1.Free4 and th.Free5 = t1.Free5 
                                                                               and th.Free6 = t1.Free6 and th.Free7 = t1.Free7 and th.Free8 = t1.Free8 and th.Free9 = t1.Free9 and  th.Free10 = t1.Free10
                  left outer join ComputationUnit as a with (nolock) on a.cComunitCode = t.计量单位编码  
                  left outer join ComputationUnit as b with (nolock) on b.cComunitCode = t.AuxUnitCode  
                  left outer join ComputationUnit as c with (nolock) on c.cComunitCode = ic.ComUnitCode
          left outer join ComputationUnit as f with (nolock) on f.cComunitCode = t.DAuxUnitCode 
           LEFT OUTER JOIN Inventory as io with (nolock) on io.cInvCode = t.OrderInvCode 
          where 1 = 1 '
    IF @diff = 1 --不显示全部
       SELECT @strFrom2 = @strFrom2 + ' and coalesce(t1.Qty,0) <> coalesce(t1.IssQty,0) ' 

    select @strOrderBY=' Order by t.生产订单号码,t.行号,t1.InvCode,t1.ComponentId '

    exec (@strSelect1 +@strSelect2 + @strFrom1 +@strWhere + @v_user1 + @v_user12 + @v_Dept2 + @v_MoType3 + @v_Factory4 + @v_wherestr + @strSelect3 + @strOnhand + @strSelect4 + @strFrom2 + @v_wherestr2 + @strOrderBY)

    if @sel = 3 --结存价
    begin
        select PartId,cInvCode=InvCode,cFree1 = Free1,cFree2 = Free2,cFree3 = Free3,cFree4 = Free4,cFree5 = Free5,cFree6 = Free6, 
                 cFree7 = Free7,cFree8 = Free8,cFree9 = Free9,cFree10 = Free10 into #tmp_invfree from bas_part where 1 = 2
        
        select @strSelect1 = 'insert into #tmp_invfree select distinct t.ComponentId, t.子件物料编码, cFree1 = case when i.bCheckFree1 = 1 then t.子件cFree1 else '''' end,
                                cFree2 = case when i.bCheckFree2 = 1 then t.子件cFree2 else '''' end,
                                cFree3 = case when i.bCheckFree3 = 1 then t.子件cFree3 else '''' end,
                                cFree4 = case when i.bCheckFree4 = 1 then t.子件cFree4 else '''' end,
                                cFree5 = case when i.bCheckFree5 = 1 then t.子件cFree5 else '''' end,
                                cFree6 = case when i.bCheckFree6 = 1 then t.子件cFree6 else '''' end,
                                cFree7 = case when i.bCheckFree7 = 1 then t.子件cFree7 else '''' end,
                                cFree8 = case when i.bCheckFree8 = 1 then t.子件cFree8 else '''' end,
                                cFree9 = case when i.bCheckFree9 = 1 then t.子件cFree9 else '''' end,
                                cFree10 = case when i.bCheckFree10 = 1 then t.子件cFree10 else '''' end  
                                from ' + @v_TableName + ' t, inventory i with (nolock) where t.子件物料编码 = i.cInvCode '
                                --and (case when i.BSelf = 1 then 3 when i.BSelf = 0 and i.bProxyForeign = 1 then 2 when i.BSelf = 0 and i.BPurchase = 1 then 1 when i.bPlanInv = 1 then 7 when i.bPTOModel = 1 then 5 when i.bCheckItem = 1 then 4 else 0 end) = 1 '
        exec sp_executesql @strSelect1
        select iunitprice as icost, imoney as FTotalPrice, inum as Fnum, cinvcode,cFree1,cFree2,cFree3,cFree4,cFree5,cFree6,cFree7,cFree8,cFree9,cFree10, PartId = iTrans into #tmp_invfreecost from ia_summary where 1 = 2
        exec IA_sp_GetConsumeData

        select @strSelect1 = 'update ' + @v_TableName + ' set 单位成本 = t.icost, 标准成本 = 标准用量 * t.icost, 实际成本 = 实际用量 * t.icost,成本差异 = (标准用量 - 实际用量 ) * t.icost from ' + @v_TableName + ' r, #tmp_invfreecost t where r.ComponentId = t.PartId '
        exec (@strSelect1)        
    end


    return 0
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值