USE [Darmangah]
GO
/****** Object:  StoredProcedure [dbo].[_2035_r_atten_all_per_sum_hours_period_01]    Script Date: 2020/05/06 15:36:10 ******/
declare @old_name varchar(200)
IF EXISTS (SELECT name FROM sysobjects
     WHERE name = '_2350_r_vaght_dehi_done_' AND type = 'P')
	 begin
	 set @old_name='_2350_r_vaght_dehi_done_'+'_old_'+[dbo].[Fn_Now_DateTime]()
	  EXEC sp_rename '_2350_r_vaght_dehi_done_',@old_name;
   end
Go

create PROCEDURE [dbo].[_2350_r_vaght_dehi_done_] (
-- By Alich NeedSoft 1399/02/17
-- exec _2350_r_vaght_dehi_done_  '1399/02/09','1399/02/12','','','','','','','','','','','1','','','','','2','','','','','',''
@fday char(10),
@tday char(10),
@ftime char(20),
@ttime char(20),
@code_user int,
@code_shift int,
@code_shobeh int,
@code_pay int,
@code_bimeh int,
@code_bakhsh int,
@code_pezeshk int,
@code_dastyar int,
@code_darman int,
@code_bimar int,
@code_ranandeh int,
@code_yaraneh int,
@code_mokammel int,
@sh_kargozini varchar(20),
@code_ertebat varchar(20),
@code_shift_str varchar(500),
@code_bimeh_str varchar(500),
@code_bakhsh_str varchar(500),
@code_pezeshk_str varchar(500),
@lucp int)
AS
BEGIN
  SET @fday = RTRIM(LTRIM(@fday))
  SET @tday = RTRIM(LTRIM(@tday))
  SET @ftime = RTRIM(LTRIM(@ftime))
  SET @ttime = RTRIM(LTRIM(@ttime))
  SET @sh_kargozini = RTRIM(LTRIM(@sh_kargozini))
  SET @code_ertebat = RTRIM(LTRIM(@code_ertebat))
  SET @code_shift_str = RTRIM(LTRIM(@code_shift_str))
  SET @code_bimeh_str = RTRIM(LTRIM(@code_bimeh_str))
  SET @code_bakhsh_str = RTRIM(LTRIM(@code_bakhsh_str))
  SET @code_pezeshk_str = RTRIM(LTRIM(@code_pezeshk_str))


  IF (LEN(@fday) = 10)
    AND (LEN(@tday) = 10)
    AND (@tday >= @fday) and (@code_darman>0)
  BEGIN

			with vaght_dehi_done_ 
			as
			(
			select code_bimar,
			(select COUNT(*) from vaght_dehi where code_darman=@code_darman) as tedad_vaght,
			(select COUNT(*) from morajeat where code_darman=@code_darman) as tedad_morajeh
			from vaght_dehi v
			where done<>0 and code_darman=@code_darman
			and tarikh between @fday and  @tday
			group by code_bimar
			)
			select * from vaght_dehi_done_ 
 
  

  END
  ELSE
    SELECT
      0 AS s_result,
      '  no condition    لطفا  محدوده تاریخ و یک درمان صحیح  انتخاب نمایید       ' AS s_meesage,
      '  no param' AS s_param

END