ALTER PROCEDURE [dbo].[_sp_morajeat_befor_Save_]

--  By Ali Chamani NeedSoft  1399/05/08 
-- select top 10 code_morajeh,code_bimar,code_pezeshk,tarikh,tedad_darman from morajeat order by code_morajeh desc
-- select check_sp_morajeat_befor_Save_,check_sp_morajeat_alram_befor_Save_,check_sp_morajeat_after_Save_ from settingc
-- update settingc set check_sp_morajeat_befor_Save_=1
-- update settingc set check_sp_morajeat_alram_befor_Save_=1
-- update settingc set check_sp_morajeat_after_Save_=1

/*
موارد قابل چک در حالت دباگ برنامه   
خیلی مهم اسم اس پی اشتباه نزنید 
با اندر لاین شروع و با اندر لاین پایان میابد 
select check_sp_morajeat_befor_Save_ from settingc
IS_Setting Table : settingc  Field :check_sp_morajeat_befor_Save_ is=-1
Begin _sp_morajeat_befor_Save_;1
EXEC  _sp_morajeat_befor_Save_;1    @code_bimar='129'  ,@code_bimeh='10'  ,@code_bakhsh='1'  ,@code_pezeshk='122879'  ,@code_darman='2593'  ,@tarikh_m='1399/05/08'  ,@code_yaraneh='1'  ,@code_mokammel='1'  ,@code_dastyar='1'  ,@code_shift='1'  ,@code_user_insert='1'  ,@allow_save=@allow_save  OUTPUT  ,@s_message=@s_message  OUTPUT
End  _sp_morajeat_befor_Save_;1
output sp  : _sp_morajeat_befor_Save_;1    @s_message:  تعداد مجاز است       @allow_save:1
*/ 

(
@code_morajeh varchar(10),
@code_bimar varchar(10),
@code_bimeh varchar(10),
@code_bakhsh varchar(10),
@code_pezeshk varchar(10),
@code_darman varchar(10),
@tarikh_m char(10),
-- new param
@r_azad varchar(10),
@r_bimar varchar(10),
@r_bimeh varchar(10),
@r_yaraneh varchar(10),
@r_mokammel varchar(10),
@r_takhfif varchar(10),
@r_takhfif_p varchar(10),
@r_labratvar_2 varchar(10),
@r_mabeh varchar(10),
@r_pezeshk varchar(10),
@r_bed varchar(10),
@r_bes varchar(10),
@sharh_darman nvarchar(250),
@tedad_darman varchar(10),
@code_shobeh varchar(10),

@code_yaraneh varchar(10),
@code_mokammel varchar(10),
@code_dastyar varchar(10),
@code_shift varchar(10),
@code_user_insert varchar(10),

@allow_save char(1) OUTPUT,
@s_message varchar(500) OUTPUT
)

AS

  SET @code_morajeh = LTRIM(RTRIM(@code_morajeh));
  SET @code_bimar = LTRIM(RTRIM(@code_bimar));
  SET @code_bimeh = LTRIM(RTRIM(@code_bimeh));
  SET @code_pezeshk = LTRIM(RTRIM(@code_pezeshk));
  SET @code_bakhsh = LTRIM(RTRIM(@code_bakhsh));
  SET @code_darman = LTRIM(RTRIM(@code_darman));
  SET @tarikh_m = LTRIM(RTRIM(@tarikh_m));


  SET @r_azad = LTRIM(RTRIM(@r_azad));
  SET @r_bimar = LTRIM(RTRIM(@r_bimar));
  SET @r_bimeh = LTRIM(RTRIM(@r_bimeh));
  SET @r_yaraneh = LTRIM(RTRIM(@r_yaraneh));
  SET @r_mokammel = LTRIM(RTRIM(@r_mokammel));
  SET @r_takhfif = LTRIM(RTRIM(@r_takhfif));
  SET @r_takhfif_p = LTRIM(RTRIM(@r_takhfif_p));
  SET @r_labratvar_2 = LTRIM(RTRIM(@r_labratvar_2));
  SET @r_mabeh = LTRIM(RTRIM(@r_mabeh));
  SET @r_pezeshk = LTRIM(RTRIM(@r_pezeshk));
  SET @r_bed = LTRIM(RTRIM(@r_bed));
  SET @r_bes = LTRIM(RTRIM(@r_bes));
  SET @sharh_darman = LTRIM(RTRIM(@sharh_darman));
  SET @tedad_darman = LTRIM(RTRIM(@tedad_darman));
  SET @code_shobeh = LTRIM(RTRIM(@code_shobeh));


  SET @code_yaraneh = LTRIM(RTRIM(@code_yaraneh));
  SET @code_mokammel = LTRIM(RTRIM(@code_mokammel));
  SET @code_dastyar = LTRIM(RTRIM(@code_dastyar));
  SET @code_shift = LTRIM(RTRIM(@code_shift));
  SET @code_user_insert = LTRIM(RTRIM(@code_user_insert));

  SET @allow_save = '0';
  SET @s_message = '';

  --   مشخص کردن اینکه پزشک متخصص است 
  DECLARE @is_motakhases bit = 0
  DECLARE @code_takhasos int = 0

  if (LEN(@code_morajeh)='') or (@code_morajeh is null)
  set @code_morajeh=0

  IF (@code_pezeshk IS NULL)
    OR (@code_pezeshk = '')
    SET @code_pezeshk = 0

  SELECT
    @code_takhasos = code_takhasos
  FROM pezeshkan
  WHERE code_pezeshk = @code_pezeshk

  IF (@code_takhasos IS NULL)
    OR (@code_takhasos <= 1)
    SET @is_motakhases = 0
  ELSE
    SET @is_motakhases = 1

  --  تکلیف تخصص مشخص شد 

  -- select @is_motakhases as takhasos


  DECLARE @Tedad int = 0

  DECLARE @Tedad_mojaz int = 2

  BEGIN
    --- @code_morajeh=0  ثبت جدید
    IF ((LEN(@code_bimar) > 0)
      AND (LEN(@code_pezeshk) > 0)
      AND (LEN(@tarikh_m) = 10)
      AND (@is_motakhases = 0)
      AND (@tedad_darman > 0)
      AND (@r_azad > 0)
      AND (@code_morajeh = 0))
    BEGIN
      SELECT
        @Tedad = COUNT(*) + 1
      FROM morajeat
      WHERE (code_bimar = @code_bimar)
      AND (code_pezeshk = @code_pezeshk)
      AND (tarikh = @tarikh_m)
      AND (tedad_darman > 0)
      AND (r_azad > 0)

    END
    ELSE
    BEGIN

      ---   @code_morajeh>0 ویرایش قدیم
      IF ((LEN(@code_bimar) > 0)
        AND (LEN(@code_pezeshk) > 0)
        AND (LEN(@tarikh_m) = 10)
        AND (@is_motakhases = 0)
        AND (@tedad_darman > 0)
        AND (@r_azad > 0)
        AND (@code_morajeh > 0))
      BEGIN
        SELECT
          @Tedad = COUNT(*)-1
        FROM morajeat
        WHERE (code_bimar = @code_bimar)
        AND (code_pezeshk = @code_pezeshk)
        AND (tarikh = @tarikh_m)
        AND (tedad_darman > 0)
        AND (r_azad > 0)

      END
    END

    --  select @code_bimar,@code_pezeshk,@tarikh_m,@Tedad as Tedad

    IF @Tedad > @Tedad_mojaz
    BEGIN
      SET @allow_save = '0';
      SET @s_message = ' تعداد مجاز  درمان برای یک بیمار یک پزشک  یک بخش  بیش از حد مجاز است  ';

    END
    ELSE
    BEGIN
      SET @allow_save = '1';
      SET @s_message = '  تعداد مجاز است   ';

    END

  END;

/*



	else
	begin
	    IF( (LEN(@code_bimar) > 0)
      AND (LEN(@code_pezeshk) > 0)  and (LEN(@tarikh_m)=10) and (@is_motakhases=1))
	  begin
	  		SET @allow_save = '1';
			SET @s_message = '  بخاطر تخصص محدودیت ندارد   ';

	  end


	end

  -- select top 10 code_bimar,code_pezeshk,tarikh from morajeat order by code_morajeh desc
DECLARE	@return_value int,
		@allow_save char(1),
		@s_message varchar(500)

EXEC	@return_value = [dbo].[sp_morajeat_befor_Save]
		@code_bimar = 203037,
		@code_bimeh = 1,
		@code_bakhsh = 1,
		@code_pezeshk = 110119,
		@code_darman = 1,
		@tarikh_m ='1399/04/24',
		@code_yaraneh = 1,
		@code_mokammel = 1,
		@code_dastyar = 1,
		@code_shift = 1,
		@code_user_insert = 1,
		@allow_save = @allow_save OUTPUT,
		@s_message = @s_message OUTPUT

SELECT	@allow_save as N'@allow_save',
		@s_message as N'@s_message'

*/