USE EMSXDB
GO
IF EXISTS (SELECT 1 FROM dbo.sysobjects where xtype='FN' AND name ='FUN_GetJobBillDetailInnerCost')
DROP function FUN_GetJobBillDetailInnerCost
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <zhangyu>
-- Create date: <2017-11-13>
-- Description: <获取工单明细的内协成本>
-- =============================================
CREATE FUNCTION [dbo].[FUN_GetJobBillDetailInnerCost]
(
@JobBillCode nvarchar(100), --工单编号
@JobBillDetailId uniqueidentifier, --工单明细Id
@UserExecId uniqueidentifier --绩效明细Id
)
RETURNS decimal(18,3)
AS
BEGIN
DECLARE @DetailInnerCost decimal(18,4)
SET @DetailInnerCost=0.0000
--私有变量
Declare @InnerTotalAmount decimal(18,2),--工单关联的所有内协成本
@TotalDetailReceiveAmount decimal(18,3),--工单明细实收金额总计
@CurDetailReceiveAmount decimal(18,3),--当前工单明细的实收金额
@UserExecNumber decimal(18, 3),--绩效比例
@CurDetailNumber decimal(18,3)--当前工单明细的数量
IF(ISNULL(@JobBillCode,'')<>'' AND @JobBillDetailId IS NOT NULL)
BEGIN
--当前工单是否包含内协单才有必要计算
IF EXISTS(select 1 from PPM_InnerSourcingBill Where SourceJobBill_Code=@JobBillCode and Amount<>0)
Begin
--当前工单明细存在有内协标记的
IF EXISTS(select 1 from PPM_JobBillDetail where ParentBillCode=@JobBillCode and IsInnerSourcing='1')
Begin
--当前明细必须有内协标记
IF EXISTS(select 1 from PPM_JobBillDetail where ParentBillCode=@JobBillCode and IsInnerSourcing='1' and Id=@JobBillDetailId)
Begin
--获取工单关联的所有内协成本
select @InnerTotalAmount=SUM(Amount) FROM PPM_InnerSourcingBill Where SourceJobBill_Code=@JobBillCode
--获取标记内协且选择了绩效的所有实收金额
select @TotalDetailReceiveAmount=(SUM(ReceivePrice*Number)) FROM PPM_JobBillDetail where ParentBillCode=@JobBillCode and IsInnerSourcing='1' and Id In(select JobBillDetailId from PPM_JobBillDetailUserExec where ParentBillCode=@JobBillCode)
--获取当前明细的实收金额和数量
select @CurDetailReceiveAmount=ReceivePrice*Number,@CurDetailNumber=Number FROM PPM_JobBillDetail where ParentBillCode=@JobBillCode and IsInnerSourcing='1' and Id=@JobBillDetailId
--获取绩效比例
select @UserExecNumber=Number from PPM_JobBillDetailUserExec where JobBillCode=@JobBillCode and JobBillDetailId=@JobBillDetailId and Id=@UserExecId
--计算((当前明细(实收单价*数量)/工单实收)*所有内协单金额汇总))*绩效百分比
Set @DetailInnerCost=((@CurDetailReceiveAmount/@TotalDetailReceiveAmount)*@InnerTotalAmount)*(@UserExecNumber/@CurDetailNumber)
End
End
else
Begin--当前工单明细不存在有内协标记的
--获取工单关联的所有内协成本
select @InnerTotalAmount=SUM(Amount) FROM PPM_InnerSourcingBill Where SourceJobBill_Code=@JobBillCode
--获取选择了绩效的所有实收金额
select @TotalDetailReceiveAmount=(SUM(ReceivePrice*Number)) FROM PPM_JobBillDetail where ParentBillCode=@JobBillCode and Id In(select JobBillDetailId from PPM_JobBillDetailUserExec where ParentBillCode=@JobBillCode)
--获取当前明细的实收金额和数量
select @CurDetailReceiveAmount=ReceivePrice*Number,@CurDetailNumber=Number FROM PPM_JobBillDetail where ParentBillCode=@JobBillCode and Id=@JobBillDetailId
--获取绩效比例
select @UserExecNumber=Number from PPM_JobBillDetailUserExec where JobBillCode=@JobBillCode and JobBillDetailId=@JobBillDetailId and Id=@UserExecId
--计算
if(@TotalDetailReceiveAmount=0)
begin
set @DetailInnerCost=0
end
else
begin
Set @DetailInnerCost=((@CurDetailReceiveAmount/@TotalDetailReceiveAmount)*@InnerTotalAmount)*(@UserExecNumber/@CurDetailNumber)
end
End
End
END
RETURN @DetailInnerCost
END