
/****** Object:  StoredProcedure [dbo].[SP_BRC_Health_Endorse]    Script Date: 02/07/2012 14:15:57 ******/

Alter Procedure [dbo].[SP_BRC_Health_Endorse](
		@UserId int, 
		@unitID dtAgencyID, 
		@SystemName Varchar(20),
		@CtrlType Varchar(20),
		@PolicyKey as dtKey,
		@PolicyBranch smallint,
		@PolicyNo as dtPolicyID,
		@PolicyIssuanceDate as dtDate,
		@EndorseKey as dtKey,
		@EndorseIssuanceDate as dtDate,
		@EndorseRow as int,
		@EndorseNo as dtEndorseID,
		@EndorseType as int,
		@EndorseSpcType as Int,
		@EndorseAgencyID as dtAgencyID,
		@BackEndorseKey as dtKey,
		@EffectDate as dtDate,
		@CommissionEffectDate as dtDate,
		@TerminateType as int,
		@TerminateInsuredType as int,
		@CancelType as int,
		@PolicyHasEndDate as int,
		@CurShamsiDate as dtDate,
		@Signer1ID Int,
		@Signer2ID Int,
		@Permission_PGuid uniqueidentifier,
		@Permission_PName varchar(50),
		@Permission_PKey varchar(250)
		)
WITH ENCRYPTION
AS
Begin
/***************************/
	Declare @ErrCode int,	
		@Errmsg varchar(150)
	Declare @BeforePost as varchar(20), @BeforeCalc as varchar(20), @AfterCalc as varchar(20)
	Set @BeforePost = 'BeforePost'
	Set @BeforeCalc = 'BeforeCalc'
	Set @AfterCalc = 'AfterCalc'

	if @CtrlType not in (@BeforePost, @BeforeCalc, @AfterCalc)
		RaisError('خطاي نوع كنترل در الحاقيه عمومي. لطفا با واحد فن‌آوري اطلاعات تماس بگيريد.' , 16, 1)
------------------------------------------------------
	
	Declare @PrmCalcType as dtID,
		@BeginDate as dtDate,
		@EndDate as dtDate
	Select @BeginDate = BeginDate ,@EndDate  = EndDate ,@PrmCalcType = PrmCalcType  From  V_Health_Policy Where PolicyKey = @PolicyKey

	if @CtrlType in (@BeforeCalc,@BeforePost)
	Begin
		If (@EndorseSpcType IS Not Null) And (@EndorseType in (@TerminateType ,@TerminateInsuredType ,@CancelType))
			Raiserror('نوع خاص الحاقيه را تصحيح نمائيد', 16, 1)

		Exec SP_Health_CheckLeftAndRightEye  @PolicyKey
		--حذف این دستور به دلیل وجود آن در CMN می باشد.
		--If (@CurShamsiDate>@EndDate) And (@EndorseType IN (@TerminateType ,@TerminateInsuredType ,@CancelType))
		--	Raiserror('.اين بيمه نامه منقضي شده بنابر نميتوانيد بر روي آن الحاقيه صادر كنيد', 16, 1)
	
		Declare @FinalEqualization As int 
		Set @FinalEqualization   = dbo.Fn_Cmn_Dfn_ValNum(null,'Constants\EndorseSpcType\FinalEqualization')
	    IF Exists(Select Top 1 1 From V_CMN_Endorse Where PolicyKey=@PolicyKey And EndorseKey<>@EndorseKey And EndorseSpcType=@FinalEqualization )
        Begin
			Set @ErrMsg = 'اين بيمه نامه  داراي الحاقيه  تسويه نهايي ميباشد'+Char(13)+'بنابر اين نمي توانيد بر روي آن الحاقيه صادر نمائيد'
			RaisError(@ErrMsg,16,1)
		End
	
	End

/******************************/
	Exec dbo.SP_BRC_CMN_Endorse
		@UserID,
		@UnitID,
		@SystemName,
		@CtrlType,
		@PolicyKey,
		@PolicyBranch,
		@PolicyNo,
		@PolicyIssuanceDate,
		@EndorseRow,
		@EndorseNo,
		@EndorseType,
		@EndorseSpcType,
		@EndorseAgencyID,
		@BackEndorseKey,
		@EffectDate,
		@CommissionEffectDate,
		@TerminateType,
		@TerminateInsuredType,
		@CancelType,
		@PolicyHasEndDate,
		@CurShamsiDate,
		@Signer1ID,
		@Signer2ID,
		@Permission_PGuid,
		@Permission_PName ,
		@Permission_PKey 
End
GO


	--- !version script(100124)
--- for Health
Insert Into T_CMN_VERSIONLOG(AppID, CommonVersion, DBVersion, MinSysVersion, MinLibVersion)
Values(100124,324,132,267,480)
GO           
  




