USE [Darmangah]
GO




declare @old_name varchar(200)
IF EXISTS (SELECT name FROM sysobjects
     WHERE name = '_sp_vaght_dehi_alarm_befor_Save_' AND type = 'P')
	 begin
	 set @old_name='_sp_vaght_dehi_alarm_befor_Save_'+'_old_'+[dbo].[Fn_Now_DateTime]()
	  EXEC sp_rename '_sp_vaght_dehi_alarm_befor_Save_',@old_name;
   end
Go


create PROCEDURE [dbo].[_sp_vaght_dehi_alarm_befor_Save_] (
-- 1399/02/17 by AliCh NeedSoft
/*
-- update settingc set check_sp_vaght_dehi_alarm_befor_Save_=1
--  select  check_sp_vaght_dehi_alarm_befor_Save_ from settingc
*/
@code_bimar varchar(10),
@code_pezeshk varchar(10),
@tarikh_vaght char(10),
@saat_vaght char(5),
@meghdar_vaght varchar(10),
@code_unit varchar(10),
@code_bimeh varchar(10),
@code_bakhsh varchar(10),
@code_darman varchar(10))
AS

  SET @code_bimar = LTRIM(RTRIM(@code_bimar));
  SET @code_bimeh = LTRIM(RTRIM(@code_bimeh));
  SET @code_pezeshk = LTRIM(RTRIM(@code_pezeshk));
  SET @tarikh_vaght = LTRIM(RTRIM(@tarikh_vaght));
  SET @saat_vaght = LTRIM(RTRIM(@saat_vaght));
  SET @meghdar_vaght = LTRIM(RTRIM(@meghdar_vaght));
  SET @code_unit = LTRIM(RTRIM(@code_unit));
  SET @code_bakhsh = LTRIM(RTRIM(@code_bakhsh));
  SET @code_darman = LTRIM(RTRIM(@code_darman));

  DECLARE @tedad_vaght_canceli int = 0
  DECLARE @s_msg nvarchar(1000)=''

  BEGIN

    IF (LEN(@code_bimar) > 0)
      AND (LEN(@code_pezeshk) > 0)
      AND (LEN(@tarikh_vaght) = 10)
    BEGIN

      DECLARE @tedad_vaght int = 0

      SELECT
        @tedad_vaght = COUNT(*)
      FROM vaght_dehi
      WHERE ((code_bimar = @code_bimar)
      AND (code_pezeshk = @code_pezeshk)
      AND (tarikh >= @tarikh_vaght))

	 set @s_msg=@s_msg+ ' تعدا د وقتهای داده شده به این بیمار '+CHAR(13)+'با این پزشک و تاریخ بزرگتر و مساوی این تاریخ  ' +CHAR(13)+ LTRIM(@tedad_vaght) +CHAR(13)+ '  مورد است  ' 



      SELECT
       @tedad_vaght_canceli = COUNT(*)
      FROM vaght_dehi
      WHERE (code_bimar = @code_bimar  and canceli=1)
  

     if @tedad_vaght_canceli>3
	 set @s_msg=@s_msg+'  این بیمار بیشتر از سه بار وقت کنسل شده دارد '


     SELECT
        @tedad_vaght+@tedad_vaght_canceli AS s_result,
       @s_msg   AS s_message,
        ' @code_bimar=' + LTRIM(@code_bimar) + ' @code_pezeshk=' + LTRIM(@code_pezeshk) + ' @tarikh_vaght=' + LTRIM(@tarikh_vaght) AS s_param



      --SELECT
      --  @tedad_vaght AS s_result,
      --  ':) Tedad_vaght=' + LTRIM(@tedad_vaght) + '   ' + OBJECT_NAME(@@PROCID) AS s_message,
      --  ' @code_bimar=' + LTRIM(@code_bimar) + ' @code_pezeshk=' + LTRIM(@code_pezeshk) + ' @tarikh_vaght=' + LTRIM(@tarikh_vaght) AS s_param


    END
    ELSE
      SELECT
        -100 AS s_result,
        ':( no condition to run sp ' + OBJECT_NAME(@@PROCID) AS s_message,
        ' @code_bimar=' + LTRIM(@code_bimar) + ' @code_pezeshk=' + LTRIM(@code_pezeshk) + ' @tarikh_vaght=' + LTRIM(@tarikh_vaght) AS s_param

  END;
-- 1398/10/12 by AliCh NeedSoft
-- EXEC
/*
exec _sp_vaght_dehi_alarm_befor_Save_ 
 @code_bimar='133'  ,
 @code_pezeshk='63749'  ,
 @tarikh_vaght='1398/07/15'  ,
 @saat_vaght='10:10' ,
 @meghdar_vaght=''  ,
 @code_unit='1'  ,
 @code_bimeh='2'  ,
 @code_bakhsh=''  ,
 @code_darman='1669'

*/

