Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.10 KB | None | 0 0
  1. -----------------------------------------------------------
  2. ---------Added by Mehul on 13-July-2016
  3. -----------------------------------------------------------
  4.  
  5. /****** Object: StoredProcedure [dbo].[proc_IncidentInjuryDetails_SelectByIncidentID] Script Date: 07/13/2016 18:47:54 ******/
  6. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_IncidentInjuryDetails_SelectByIncidentID]') AND type in (N'P', N'PC'))
  7. DROP PROCEDURE [dbo].[proc_IncidentInjuryDetails_SelectByIncidentID]
  8. GO
  9.  
  10. /****** Object: StoredProcedure [dbo].[proc_Incident_GetByID] Script Date: 07/13/2016 18:47:54 ******/
  11. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_Incident_GetByID]') AND type in (N'P', N'PC'))
  12. DROP PROCEDURE [dbo].[proc_Incident_GetByID]
  13. GO
  14.  
  15. /****** Object: StoredProcedure [dbo].[proc_Incident_GetByIDForDocumentTemplate] Script Date: 07/13/2016 18:47:54 ******/
  16. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_Incident_GetByIDForDocumentTemplate]') AND type in (N'P', N'PC'))
  17. DROP PROCEDURE [dbo].[proc_Incident_GetByIDForDocumentTemplate]
  18. GO
  19.  
  20.  
  21.  
  22. /****** Object: StoredProcedure [dbo].[proc_IncidentInjuryDetails_SelectByIncidentID] Script Date: 07/13/2016 18:47:54 ******/
  23. SET ANSI_NULLS ON
  24. GO
  25.  
  26. SET QUOTED_IDENTIFIER ON
  27. GO
  28.  
  29.  
  30. --[proc_IncidentInjuryDetails_SelectByIncidentID] 6723,1
  31. CREATE Procedure [dbo].[proc_IncidentInjuryDetails_SelectByIncidentID] --5442,1
  32. (
  33. @IncidentID int,
  34. @LanguageId int
  35. )
  36. AS
  37. BEGIN
  38.  
  39. OPEN SYMMETRIC KEY InjurdPerson_Key11
  40. DECRYPTION BY CERTIFICATE InjuredPerson;
  41.  
  42. Select
  43. IID.*,
  44. Case When QL.AllowEncryption=1 Then [dbo].[DecryptNVarchar](A.EncryptAnswer) Else A.Answer End as JobTitle,
  45. ISNULL(ICL.LupValue,InjuryClass.LupValue) InjuryClassificationName,
  46. ISNULL(PCL.LupValue,PC.LupValue) PersonClassName,
  47. Case U.Gender when 'M' Then 'Male' when 'F' Then 'Female' End as Gender,
  48. Case when isnull([dbo].[DecryptInt](IID.InjuredUserID),0) =0 Then
  49. [dbo].[DecryptNVarchar](IID.FirstName)
  50. Else
  51. LTRIM(RTRIM(CASE WHEN ISNULL(U.FirstName,'')='' THEN
  52. case when ISNULL(U.LastName,'')='' Then
  53. Case When ISNULL(U.Alias,'')='' then
  54. U.UserEmail
  55. else
  56. U.Alias
  57. End
  58. else
  59. U.LastName
  60. end
  61. Else
  62. ISNULL(U.FirstName,'') + ' ' + ISNULL(U.LastName,'')
  63. END))
  64. End PersonFirstName,
  65. IH.DaysLost,IH.DaysRestricted,
  66. I.ReportNum,
  67. ISNULL(U.UserEmail,'') as UserEmail,
  68. [dbo].[GetIndividualScoreByInjuryID](IID.IncidentInjuryID) as Score,
  69. U.PID AS UserID,
  70. ISNULL(CWL.LupValue,CW.LupValue) as WorkRelated,
  71. ISNULL(IFOL.LupValue,IFO.LupValue) as InsuranceForms,
  72. ISNULL(IITL.LupValue,IIT.LupValue) TypeName,
  73. [dbo].[DecryptNVarchar](IID.MedicalTreatment) as DecryptMedicalTreatment,
  74. UDS.FirstName + ' '+ISNULL(UDS.LastName,'') as DirectSupervisorName,
  75. ISNULL(IICL.LupValue,IIC.LupValue) InjuryIllnessCateName,
  76. ISNULL(IISCL.LupValue,IISC.LupValue) InjuryIllnessSubCateName,
  77. Case When S.ShiftCode Is Not Null Then S.ShiftName+' ('+S.ShiftCode+') '
  78. else S.ShiftName end as ShiftNameWithCode,
  79. ISNULL(OCL.LupValue,OC.LupValue) OshaSeverityClassName,
  80. Case When ISNULL(IID.IsDiffLocation,0)=0 Then 'No' Else 'Yes' End DiffLocation
  81. from
  82. tIncidentInjuryDetails IID
  83. Inner Join tIncident I ON IID.IncidentID = I.IncidentID
  84. LEFT Join tClientLup PC ON PC.ClientId = I.ClientID And PC.LupKey = 'PersonClass' And PC.LupId = IID.PersonClassID
  85. LEFT Join tClientLupLang PCL ON PCL.ClientId = PC.ClientId And PCL.LupKey = 'PersonClass' And PCL.LupId = PC.LupId AND PCL.LanguageId=@LanguageId
  86. Left Join tClientLup InjuryClass On InjuryClass.ClientId = I.ClientID And InjuryClass.LupKey = 'IncidentInjuryClass' And InjuryClass.LupId = IID.InjuryClassificationID
  87. LEFT JOIN tClientLupLang ICL ON ICL.ClientId = InjuryClass.ClientId AND ICL.LupKey = 'IncidentInjuryClass' AND ICL.LupId = InjuryClass.LupId AND ICL.LanguageId=@LanguageId
  88. Left Join tUsers U On U.ClientId = I.ClientID And U.UserID = [dbo].[DecryptInt](IID.InjuredUserID)
  89. Left Join tClientLup IFO on IFO.ClientId=I.ClientID AND IFO.LupKey='InsuranceCompany' AND IFO.LupId=IID.InsuranceCompanyId
  90. left join tClientLupLang IFOL On IFOL.ClientId=IFO.ClientId AND IFOL.LupKey='InsuranceCompany' AND IFOL.LupId=IFO.LupId AND IFOL.LanguageId=@LanguageId
  91. left join tQuestionLup QL on QL.ClientID=I.ClientID and QL.LupKey='OSHAQuestions' and QL.OshaReportID=21
  92. Left join tAnswerLup A on A.QuestionID=QL.QuestionID and A.Source='Incident' and A.SourceID=@IncidentID and A.SubSource='Injury' and A.SubSourceID=IID.IncidentInjuryID
  93. and A.ClientID=QL.ClientID
  94. Left Join tClientLup CW ON CW.LupId=IID.ISRelatedToWork AND CW.ClientId=I.ClientID AND CW.LupKey='WorkRelated'
  95. Left Join tClientLupLang CWL ON CWL.LupId=CW.LupId AND CWL.ClientId=CW.ClientId AND CWL.LupKey=CW.LupKey AND CWL.LanguageId=@LanguageId
  96. Left join tUsers UDS on UDS.ClientId=I.ClientID And UDS.UserID=IID.DirectSupervisor
  97. Left join tClientLup IIC on IIC.LupId=IID.InjIllCategoryID And IIC.ClientId=I.ClientID and IIC.LupKey='InjuryIllnessCategory'
  98. Left join tClientLupLang IICL on IICL.LupId=IIC.LupId and IICL.ClientId=IIC.ClientId and IICL.LupKey=IIC.LupKey And IICL.LanguageId=@LanguageId
  99. Left join tClientLup IISC on IISC.LupId=IID.InjIllSubCategoryID And IISC.ClientId=I.ClientID And IISC.LupKey='InjuryIllnessSubCategory'
  100. Left join tClientLupLang IISCL on IISCL.LupId=IISC.LupId And IISCL.ClientId=IISC.ClientId And IISCL.LupKey=IISC.LupKey And IISCL.LanguageId=@LanguageId
  101. Left Join tDLocationShift DS on DS.DLocationShiftID=IID.DLocationShiftID
  102. Left join tShift S on S.ShiftId=DS.ShiftID
  103. Left Join tClientLup OC on OC.ClientId = I.ClientID And OC.LupKey = 'OshaSeverityClass' And OC.LupId = IID.OshaClassificationID
  104. Left Join tClientLupLang OCL on OCL.ClientId = OC.ClientId And OCL.LupKey = 'OshaSeverityClass' And OCL.LupId = OC.LupId And OCL.LanguageId = @LanguageId
  105. Outer APPLY (
  106. --select SUM(DATEDIFF(day,StartDate,ISNULL(EndDate,GETUTCDATE()))) as DaysLost,
  107. --SUM(DATEDIFF(day,StartDate,ISNULL(EndDate,GETUTCDATE()))) as DaysRestricted
  108. --from tInjuryClassificationHistory
  109. --where IncidentInjuryID=IID.IncidentInjuryID and NewClassificationID=IID.InjuryClassificationID
  110. select SUM(case ISNULL(CL.DataColumn2,'') when 'LTC'
  111. then datediff(day,ICH.StartDate,isnull(ICH.EndDate,GETUTCDATE())) else 0 end) as DaysLost,
  112. SUM(case ISNULL(CL.DataColumn2,'') when 'RWC'
  113. then datediff(day,ICH.StartDate,isnull(ICH.EndDate,GETUTCDATE())) else 0 end) as DaysRestricted
  114. from tInjuryClassificationHistory ICH
  115. Inner join tClientLup CL On CL.ClientId = I.ClientID And ICH.NewClassificationID = CL.LupId And CL.LupKey = 'IncidentInjuryClass'
  116. where ICH.IncidentInjuryID=IID.IncidentInjuryID
  117. ) IH
  118. Inner Join tClientLup IIT ON IIT.ClientId = I.ClientID And IIT.LupKey = 'InjuryIllnessType' And IIT.LupId = IID.Type
  119. LEFT Join tClientLupLang IITL ON IITL.ClientId = IIT.ClientId And IITL.LupKey = 'InjuryIllnessType' And IITL.LupId = IIT.LupId AND IITL.LanguageId=@LanguageId
  120. where
  121. IID.IncidentID =@IncidentID
  122. Order by IID.InjuryNum asc
  123. END
  124. GO
  125.  
  126. /****** Object: StoredProcedure [dbo].[proc_Incident_GetByID] Script Date: 07/13/2016 18:47:54 ******/
  127. SET ANSI_NULLS ON
  128. GO
  129.  
  130. SET QUOTED_IDENTIFIER ON
  131. GO
  132.  
  133.  
  134. --[proc_Incident_GetByID] 6723,1
  135. CREATE Procedure [dbo].[proc_Incident_GetByID]
  136. (
  137. @incidentId int,
  138. @LanguageId int
  139. )
  140. AS
  141. BEGIN
  142.  
  143. OPEN SYMMETRIC KEY InjurdPerson_Key11
  144. DECRYPTION BY CERTIFICATE InjuredPerson;
  145.  
  146.  
  147. declare @OverDueDate datetime
  148. SET @OverDueDate = CONVERT(varchar(10),GETDATE(),101)
  149.  
  150. select
  151. I.*,
  152. ISNULL(I.ISPenaltyLoss,0) PenltyLoss,
  153. ISNULL(I.WasFatigueOccured,0) FatigueOccured,
  154. Convert(varchar(15),I.IncidentDate,106) IncDate,
  155. Convert(varchar(15),I.IncidentDate,108) IncidentTime,
  156. Convert(varchar(15),I.LocalIncidentDate,108) IncidentLocalTime,
  157. UP.FirstName +' '+ ISNULL(UP.LastName,'') UpdatedByName,
  158. C.FirstName +' '+ ISNULL(C.LastName,'') CreatedByName,
  159. ISNULL(LILL.LupValue,LIL.LupValue) IncidentLevelName,
  160. ISNULL(LIAL.LupValue,LIA.LupValue) IncidentActivityName,
  161. ISNULL(LICCL.LupValue,LICC.LupValue) IncidentComplaintCategoryName,
  162. ISNULL(LICML.LupValue,LICM.LupValue) IncidentInvestigationMethodName,
  163. dbo.ufn_LocationHierarchy(I.DLocationID) Location,
  164. dbo.GetShiftNameByIncidentID(@incidentId) as Shift,
  165. SUP.FirstName +' '+ ISNULL(SUP.LastName,'') Supervisor,
  166. SUP.UserID SupervisorID,
  167. (select COUNT(CASE WHEN CompDate is NOT NULL THEN A.ActionID ELSE NULL END)
  168. From tActionItem A where A.Source = 'Incident' ANd A.SourceID = I.IncidentID )as ActionCompleted,
  169. (select COUNT(CASE WHEN CompDate is NULL AND TargetDate >= @OverDueDate THEN A.ActionID ELSE NULL END)
  170. From tActionItem A where A.Source = 'Incident' ANd A.SourceID = I.IncidentID) as ActionPending,
  171. (Select COUNT(CASE WHEN CompDate is NULL AND TargetDate < @OverDueDate THEN A.ActionID ELSE NULL END)
  172. From tActionItem A where A.Source = 'Incident' ANd A.SourceID = I.IncidentID) as ActionOverdue,
  173. ISNULL(LRCL.LupValue,LRC.LupValue) RamClassificationName,
  174. ISNULL(CLLIL.LupValue,CLIL.LupValue) IncidentInvestigationLevel,
  175. Case When Q.AllowEncryption=1 Then ISNULL(cast([dbo].[DecryptFloat](A.EncryptValueNumber) as varchar),'')
  176. Else ISNULL(A.ValueNumber,'') End as PenaltyAmount,
  177. U.UnitName as CurrencyCode,
  178. T.WindowsTimeZoneName,
  179. CU.CustomerName
  180. from
  181. tIncident I
  182. LEFT JOIN tUsers C ON C.UserID = I.Created_UserID
  183. LEFT JOIN tUsers UP ON UP.UserID = I.LC_UserID
  184. Left Join tUsers SUP On SUP.UserID=I.WorkSupID
  185. Left Outer Join tClientLup LIL ON LIL.ClientId = I.ClientID And LIL.LupKey = 'IncidentLevel' And LIL.LupId = I.IncidentLevelID
  186. LEFT Outer JOIN tClientLupLang LILL ON LILL.ClientId = LIL.ClientId AND LILL.LupKey = 'IncidentLevel' AND LILL.LupId = LIL.LupId AND LILL.LanguageId=@LanguageId
  187. Left Outer join tClientLup LIA ON LIA.ClientId = I.ClientID And LIA.LupKey = 'IncidentActivity' And LIA.LupId = I.IncidentActivityID
  188. LEFT Outer JOIN tClientLupLang LIAL ON LIAL.ClientId = LIA.ClientId AND LIAL.LupKey = 'IncidentActivity' AND LIAL.LupId = LIA.LupId AND LIAL.LanguageId=@LanguageId
  189. Left Outer join tClientLup LICC ON LICC.ClientId = I.ClientID And LICC.LupKey = 'IncidentComplaintCategory' And LICC.LupId = I.ComplaintCategoryID
  190. Left Outer join tClientLupLang LICCL ON LICCL.ClientId = LICC.ClientId And LICCL.LupKey = 'IncidentComplaintCategory' And LICCL.LupId = LICC.LupId AND LICCL.LanguageId=@LanguageId
  191. Left Outer join tClientLup LICM ON LICM.ClientId = I.ClientID And LICM.LupKey = 'IncidentInvestigationMethod' And LICM.LupId = I.InvestigationMethodologyID
  192. Left Outer join tClientLupLang LICML ON LICML.ClientId = LICM.ClientId And LICML.LupKey = 'IncidentInvestigationMethod' And LICML.LupId = LICM.LupId AND LICML.LanguageId=@LanguageId
  193. Left Outer join tClientLup LRC ON LRC.ClientId = I.ClientID And LRC.LupKey = 'RAMClass' And LRC.LupId = I.RamClassId
  194. Left Outer join tClientLupLang LRCL ON LRCL.ClientId = LRC.ClientId And LRCL.LupKey = 'RAMClass' And LRCL.LupId = LRC.LupId AND LRCL.LanguageId=@LanguageId
  195. LEFT JOIN tClientSettings CS ON CS.ClientID = I.ClientID
  196. Left Outer join tClientLup CLIL ON CLIL.ClientId = I.ClientID And CLIL.LupKey = 'InvestigationLevel' And CLIL.LupId = I.InvestigationLevelID
  197. Left Outer join tClientLupLang CLLIL ON CLLIL.ClientId = CLIL.ClientId And CLLIL.LupKey = 'InvestigationLevel' And CLLIL.LupId = CLIL.LupId AND CLLIL.LanguageId=@LanguageId
  198. Left Outer join tQuestionLup Q on Q.ClientID=I.ClientID AND Q.LupKey='LossQuestion' AND Q.OshaReportID=1
  199. Left Outer join tAnswerLup A on A.QuestionID=Q.QuestionID AND A.Source='Incident' AND A.SourceID=I.IncidentID
  200. Left Outer join tUnits U on U.UnitID=Case When Q.AllowEncryption=1 Then [dbo].[DecryptInt](A.EncryptUnitID)Else A.UnitID End
  201. Left join tTimeZone T on T.TimeZoneID =I.TimeZoneID
  202. Left join tIncidentLinkOther ILO on ILO.Source='Customer' and ILO.IncidentId=I.IncidentID
  203. Left join tCustomers CU on CU.ClientID=I.ClientID and CU.CustomerId=ILO.SourceID
  204. where I.IncidentID=@incidentId
  205. END
  206. GO
  207.  
  208. /****** Object: StoredProcedure [dbo].[proc_Incident_GetByIDForDocumentTemplate] Script Date: 07/13/2016 18:47:54 ******/
  209. SET ANSI_NULLS ON
  210. GO
  211.  
  212. SET QUOTED_IDENTIFIER ON
  213. GO
  214.  
  215.  
  216. CREATE Procedure [dbo].[proc_Incident_GetByIDForDocumentTemplate]
  217. (
  218. @incidentId int,
  219. @LanguageId int
  220. )
  221. AS
  222. BEGIN
  223.  
  224. OPEN SYMMETRIC KEY InjurdPerson_Key11
  225. DECRYPTION BY CERTIFICATE InjuredPerson;
  226.  
  227.  
  228. declare @OverDueDate datetime
  229. SET @OverDueDate = CONVERT(varchar(10),GETDATE(),101)
  230.  
  231. select
  232. I.IncidentID
  233. ,I.ClientID
  234. ,I.ReportNum
  235. ,I.InvestigationMethodologyID
  236. ,I.IncidentCategoryID
  237. ,I.WhatHappened
  238. ,I.How
  239. ,I.Witness
  240. ,I.WhatOccurBefore
  241. ,I.WhereEvent
  242. ,I.Object
  243. ,I.OpsResumed
  244. ,I.ProblemStatement
  245. ,I.Created_UserID
  246. ,I.Created_TS
  247. ,I.LC_UserID
  248. ,I.LC_TS
  249. ,I.Title
  250. ,I.Description
  251. ,I.DLocationID
  252. ,I.IncidentDate
  253. ,I.IncidentLevelID
  254. ,I.IncidentActivityID
  255. ,I.ComplaintCategoryID
  256. ,I.Status
  257. ,I.ActionPrevention
  258. ,I.WasFatigueOccured
  259. ,I.InvestigationLevelID
  260. ,I.WorkSupID
  261. ,I.ISPenaltyLoss
  262. ,I.WHYHappened
  263. ,I.CorrectiveActions
  264. ,I.SubmittedOn
  265. ,I.LastModifiedOn
  266. ,I.ScoringperformedOn
  267. ,I.SubmittedForApprovalOn
  268. ,I.ChangesRequestedOn
  269. ,I.ApprovedOn
  270. ,I.ReOpenedOn
  271. ,I.ReviewNotRequired
  272. ,I.SubmittedForReviewOn
  273. ,I.ReviewedOn
  274. ,I.IsInjuryOccured
  275. ,I.IncidentInformedDate
  276. ,I.MarkConfidential
  277. ,I.PreliminaryInvCompleted
  278. ,I.PreInvCompletedDate
  279. ,I.IsAutoConfidentialChecked
  280. ,I.RamClassId
  281. ,I.MigrationID
  282. ,I.ReferenceNumber
  283. ,I.UrlField
  284. ,I.TimeZoneID
  285. ,I.StateOfMindLeft
  286. ,I.StateOfMindRight
  287. ,I.LocalIncidentDate
  288. ,Case When ISNULL(I.ISPenaltyLoss,0)=0 then 'No' Else 'Yes' End MonetaryLoss,
  289. Case When ISNULL(I.WasFatigueOccured,0)=0 then 'No' Else 'Yes' End FatigueOccured,
  290. Convert(varchar(15),I.LocalIncidentDate,108) IncidentLocalTime,
  291. UP.FirstName +' '+ ISNULL(UP.LastName,'') UpdatedByName,
  292. C.FirstName +' '+ ISNULL(C.LastName,'') CreatedByName,
  293. ISNULL(LILL.LupValue,LIL.LupValue) IncidentLevelName,
  294. ISNULL(LIAL.LupValue,LIA.LupValue) IncidentActivityName,
  295. ISNULL(LICCL.LupValue,LICC.LupValue) IncidentComplaintCategoryName,
  296. ISNULL(LICML.LupValue,LICM.LupValue) IncidentInvestigationMethodName,
  297. dbo.ufn_LocationHierarchy(I.DLocationID) Location,
  298. dbo.GetShiftNameByIncidentID(@incidentId) as Shift,
  299. SUP.FirstName +' '+ ISNULL(SUP.LastName,'') Supervisor,
  300. SUP.UserID SupervisorID,
  301. (select COUNT(CASE WHEN CompDate is NOT NULL THEN A.ActionID ELSE NULL END)
  302. From tActionItem A where A.Source = 'Incident' ANd A.SourceID = I.IncidentID )as ActionCompleted,
  303. (select COUNT(CASE WHEN CompDate is NULL AND TargetDate >= @OverDueDate THEN A.ActionID ELSE NULL END)
  304. From tActionItem A where A.Source = 'Incident' ANd A.SourceID = I.IncidentID) as ActionPending,
  305. (Select COUNT(CASE WHEN CompDate is NULL AND TargetDate < @OverDueDate THEN A.ActionID ELSE NULL END)
  306. From tActionItem A where A.Source = 'Incident' ANd A.SourceID = I.IncidentID) as ActionOverdue,
  307. ISNULL(LRCL.LupValue,LRC.LupValue) RamClassificationName,
  308. ISNULL(CLLIL.LupValue,CLIL.LupValue) IncidentInvestigationLevel,
  309. Case When Q.AllowEncryption=1 Then ISNULL(cast([dbo].[DecryptFloat](A.EncryptValueNumber) as varchar),'')
  310. Else ISNULL(A.ValueNumber,'') End as PenaltyAmount,
  311. U.UnitName as CurrencyCode,
  312. T.WindowsTimeZoneName,
  313. CU.CustomerName,
  314. (Select Case when COUNT(*) > 0 Then 'Yes' Else 'No' ENd
  315. from tIncidentInjuryDetails where IncidentID =I.IncidentID And [Type] =3) As ISNearMiss,
  316. (Select ISNULL(QAL.Answer,QA.Answer) from tQuestionLup Q
  317. Inner Join tAnswerLup A on A.ClientID=Q.ClientID AND A.QuestionID=Q.QuestionID
  318. AND A.Source='Incident' AND A.SourceID=@incidentId
  319. Inner Join tQuestionLupAnswers QA on QA.QuestionLupAnswerID=
  320. Case When Q.AllowEncryption=1 Then [dbo].[DecryptInt](A.EncryptAnswerId) Else A.AnswerId End
  321. Left Join tQuestionLupAnswersLang QAL on QAL.QuestionLupAnswerID=QA.QuestionLupAnswerID AND QAL.LanguageId=@LanguageId
  322. Where Q.QuestionID=13101) as SWOEnforced,
  323. Case When ISNULL(I.ISPenaltyLoss,0)=1 Then 'CAR Number: '+ ISNULL(
  324. (Select Case When Q.AllowEncryption=1 Then [dbo].[DecryptNVarchar](A.EncryptAnswer) Else A.Answer End
  325. from tQuestionLup Q
  326. Inner Join tAnswerLup A on A.ClientID=Q.ClientID AND A.QuestionID=Q.QuestionID
  327. AND A.Source='Incident' AND A.SourceID=@incidentId
  328. Where Q.QuestionID=13145),'') Else '' End as CARCreated
  329. from
  330. tIncident I
  331. LEFT JOIN tUsers C ON C.UserID = I.Created_UserID
  332. LEFT JOIN tUsers UP ON UP.UserID = I.LC_UserID
  333. Left Join tUsers SUP On SUP.UserID=I.WorkSupID
  334. Left Outer Join tClientLup LIL ON LIL.ClientId = I.ClientID And LIL.LupKey = 'IncidentLevel' And LIL.LupId = I.IncidentLevelID
  335. LEFT Outer JOIN tClientLupLang LILL ON LILL.ClientId = LIL.ClientId AND LILL.LupKey = 'IncidentLevel' AND LILL.LupId = LIL.LupId AND LILL.LanguageId=@LanguageId
  336. Left Outer join tClientLup LIA ON LIA.ClientId = I.ClientID And LIA.LupKey = 'IncidentActivity' And LIA.LupId = I.IncidentActivityID
  337. LEFT Outer JOIN tClientLupLang LIAL ON LIAL.ClientId = LIA.ClientId AND LIAL.LupKey = 'IncidentActivity' AND LIAL.LupId = LIA.LupId AND LIAL.LanguageId=@LanguageId
  338. Left Outer join tClientLup LICC ON LICC.ClientId = I.ClientID And LICC.LupKey = 'IncidentComplaintCategory' And LICC.LupId = I.ComplaintCategoryID
  339. Left Outer join tClientLupLang LICCL ON LICCL.ClientId = LICC.ClientId And LICCL.LupKey = 'IncidentComplaintCategory' And LICCL.LupId = LICC.LupId AND LICCL.LanguageId=@LanguageId
  340. Left Outer join tClientLup LICM ON LICM.ClientId = I.ClientID And LICM.LupKey = 'IncidentInvestigationMethod' And LICM.LupId = I.InvestigationMethodologyID
  341. Left Outer join tClientLupLang LICML ON LICML.ClientId = LICM.ClientId And LICML.LupKey = 'IncidentInvestigationMethod' And LICML.LupId = LICM.LupId AND LICML.LanguageId=@LanguageId
  342. Left Outer join tClientLup LRC ON LRC.ClientId = I.ClientID And LRC.LupKey = 'RAMClass' And LRC.LupId = I.RamClassId
  343. Left Outer join tClientLupLang LRCL ON LRCL.ClientId = LRC.ClientId And LRCL.LupKey = 'RAMClass' And LRCL.LupId = LRC.LupId AND LRCL.LanguageId=@LanguageId
  344. LEFT JOIN tClientSettings CS ON CS.ClientID = I.ClientID
  345. Left Outer join tClientLup CLIL ON CLIL.ClientId = I.ClientID And CLIL.LupKey = 'InvestigationLevel' And CLIL.LupId = I.InvestigationLevelID
  346. Left Outer join tClientLupLang CLLIL ON CLLIL.ClientId = CLIL.ClientId And CLLIL.LupKey = 'InvestigationLevel' And CLLIL.LupId = CLIL.LupId AND CLLIL.LanguageId=@LanguageId
  347. Left Outer join tQuestionLup Q on Q.ClientID=I.ClientID AND Q.LupKey='LossQuestion' AND Q.OshaReportID=1
  348. Left Outer join tAnswerLup A on A.QuestionID=Q.QuestionID AND A.Source='Incident' AND A.SourceID=I.IncidentID
  349. Left Outer join tUnits U on U.UnitID=Case When Q.AllowEncryption=1 Then [dbo].[DecryptInt](A.EncryptUnitID)Else A.UnitID End
  350. Left join tTimeZone T on T.TimeZoneID =I.TimeZoneID
  351. Left join tIncidentLinkOther ILO on ILO.Source='Customer' and ILO.IncidentId=I.IncidentID
  352. Left join tCustomers CU on CU.ClientID=I.ClientID and CU.CustomerId=ILO.SourceID
  353. where I.IncidentID=@incidentId
  354.  
  355. END
  356. GO
  357.  
  358. -----------------------------------------------------------
  359. ---------Added by Mehul on 15-July-2016
  360. -----------------------------------------------------------
  361.  
  362.  
  363. Update tApprovalsNew SET ApprovalStatus='Sent For Approval'
  364. from tApprovalsNew A
  365. Inner Join tRiskHazard H on H.HazardID=A.SourceID AND H.Status='Approvals In Progress'
  366. Where A.SourceType='Risk' AND A.ApprovalStatus='Pending'
  367. GO
  368.  
  369. ---------------------
  370.  
  371. Update tApprovalsNew SET ApprovalStatus='Sent For Approval'
  372. from tApprovalsNew A
  373. Inner Join t_Audit_Audit AU on AU.AuditId=A.SourceID AND AU.Status='Approvals In Progress'
  374. Where A.SourceType='Audit' AND A.ApprovalStatus='Pending'
  375. GO
  376.  
  377. ---------------------
  378.  
  379. Update tApprovalsNew SET ApprovalStatus='Sent For Approval'
  380. from tApprovalsNew A
  381. Inner Join tLegalComplianceGeneralInfo L on L.LegalComplianceID=A.SourceID AND L.Status='Approvals In Progress'
  382. Where A.SourceType='Legal' AND A.ApprovalStatus='Pending'
  383. GO
  384.  
  385. -------------------------------------
  386.  
  387.  
  388.  
  389. /****** Object: UserDefinedFunction [dbo].[ufn_Users_GetEnumUsersCommon] Script Date: 07/15/2016 17:25:27 ******/
  390. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ufn_Users_GetEnumUsersCommon]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  391. DROP FUNCTION [dbo].[ufn_Users_GetEnumUsersCommon]
  392. GO
  393.  
  394.  
  395.  
  396. /****** Object: UserDefinedFunction [dbo].[ufn_Users_GetEnumUsersCommon] Script Date: 07/15/2016 17:25:27 ******/
  397. SET ANSI_NULLS ON
  398. GO
  399.  
  400. SET QUOTED_IDENTIFIER ON
  401. GO
  402.  
  403.  
  404. --Select * from [dbo].[ufn_Users_GetEnumUsersCommon](8,6,0,'Incident',6724,-6,10,1)
  405. CREATE Function [dbo].[ufn_Users_GetEnumUsersCommon]
  406. (
  407. @EmailTemplateID int,
  408. @EnumID int,
  409. @LevelNo int,
  410. @Source varchar(50),
  411. @SourceID int,
  412. @SourceId2 int,
  413. @UserID int,
  414. @ClientID int
  415. )
  416. RETURNS @oUsers TABLE(UserID int)
  417. As
  418. BEGIN
  419.  
  420. IF(@EnumID=1)
  421. Begin
  422. Insert Into @oUsers(UserID)
  423. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  424. End
  425. Else If (@EnumID=2)
  426. Begin
  427. Insert Into @oUsers(UserID)
  428. select
  429. U.UserID
  430. from tEditors E
  431. INNER JOIN tUsers U on E.EditorID=U.UserID AND U.IsActive=1 AND U.IsFileOnlyUser=0
  432. where E.SourceID=@SourceId and E.Source=@Source
  433. End
  434. Else If(@EnumID=3)
  435. Begin
  436.  
  437. IF(@Source='Incident')
  438. Begin
  439.  
  440. Insert Into @oUsers(UserID)
  441. Select tUsers.UserID from tIncident I
  442. Inner Join tUsers On I.Created_UserID= tUsers.UserID AND tUsers.IsActive=1 AND tUsers.IsFileOnlyUser=0
  443. Where I.IncidentID=@SourceId
  444.  
  445. End
  446. Else IF(@Source='Audit')
  447. Begin
  448. Insert Into @oUsers(UserID)
  449. Select tUsers.UserID
  450. from t_Audit_Audit A
  451. Inner Join tUsers On A.CreatedBy= tUsers.UserID AND tUsers.IsActive=1 AND tUsers.IsFileOnlyUser=0
  452. Where A.AuditId=@SourceId
  453. End
  454. Else IF(@Source='Risk')
  455. Begin
  456. Insert Into @oUsers(UserID)
  457. Select tUsers.UserID
  458. from tRiskHazard RH
  459. Inner Join tUsers On RH.CreatedBy= tUsers.UserID AND tUsers.IsActive=1 AND tUsers.IsFileOnlyUser=0
  460. Where RH.HazardID=@SourceId
  461. End
  462. Else IF(@Source='DMS')
  463. Begin
  464. Insert Into @oUsers(UserID)
  465. Select tUsers.UserID
  466. from tDoc D
  467. Inner Join tUsers On D.CreatedByID= tUsers.UserID AND tUsers.IsActive=1 AND tUsers.IsFileOnlyUser=0
  468. Where D.DocID=@SourceId
  469. End
  470. Else IF(@Source='OTour')
  471. Begin
  472. Insert Into @oUsers(UserID)
  473. Select tUsers.UserID
  474. from tObservationTour OT
  475. Inner Join tUsers On OT.CreatedBy= tUsers.UserID AND tUsers.IsActive=1 AND tUsers.IsFileOnlyUser=0
  476. Where OT.SoTourId=@SourceId
  477. End
  478. Else IF(@Source='Legal')
  479. Begin
  480. Insert into @oUsers(UserID)
  481. select tUsers.UserID
  482. from tLegalComplianceGeneralInfo LCI
  483. Inner Join tUsers On LCI.Created_UserID= tUsers.UserID AND tUsers.IsActive=1 AND tUsers.IsFileOnlyUser=0
  484. where LCI.LegalComplianceID=@SourceId
  485. End
  486.  
  487. End
  488. Else if(@EnumID=4)
  489. Begin
  490.  
  491. Insert Into @oUsers(UserID)
  492. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  493. End
  494. Else If(@EnumID=5)
  495. Begin
  496. Insert Into @oUsers(UserID)
  497. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  498. End
  499. Else If(@EnumID=6)
  500. Begin
  501.  
  502. Declare @ApprovalID int
  503. Declare @LevelId int
  504.  
  505. SET @ApprovalID=@SourceId2
  506. SET @LevelId=0
  507.  
  508. IF(@Source='DMS')
  509. Begin
  510.  
  511. Insert Into @oUsers(UserID)
  512. SELECT
  513. U.UserID
  514. FROM tDocApprovals DA
  515. INNER JOIN tUsers U ON DA.UserID=U.UserID AND U.IsActive=1 AND U.IsFileOnlyUser=0
  516. WHERE DA.DocID=@SourceId and DA.Archive='N'
  517. End
  518. Else IF(@Source='Incident')
  519. Begin
  520.  
  521. IF (@ApprovalID < 0 AND @ApprovalID <> -5 AND @ApprovalID <> -6)
  522. Begin
  523.  
  524. SET @LevelId = (Select Abs(@ApprovalID));
  525. SET @ApprovalID = 0;
  526. End
  527.  
  528. IF(@ApprovalID=-5)
  529. Begin
  530.  
  531. SET @ApprovalID = 0;
  532. IF EXISTS(Select ClientSettingID from tClientSettings Where ClientID=@ClientId AND IsSequentialApproval=1)
  533. Begin
  534. SET @LevelId =1
  535. End
  536.  
  537. End
  538.  
  539. If(@ApprovalID=-6)
  540. Begin
  541. SET @ApprovalID = 0
  542. SET @LevelId = (Select ISNULL(Min(LevelNo),0) from tApprovalsNew Where SourceType=@Source AND SourceID=@SourceID AND LevelNo<>0)
  543. End
  544.  
  545. Insert Into @oUsers(UserID)
  546. SELECT
  547. U.UserID
  548. from
  549. tApprovalsNew an
  550. Inner Join tUsers u On u.UserID = an.ApproverID AND u.IsActive=1 AND u.IsFileOnlyUser=0
  551. where
  552. an.SourceType = @Source And an.SourceID = @SourceID
  553. AND ((@ApprovalID=0 AND ((ISNULL(LevelNo,0)=@LevelId OR ISNULL(LevelNo,0)=0) OR @LevelId=0)) OR ApprovalID=@ApprovalID)
  554. AND ((ApprovalStatus='Pending' OR @LevelId=0 OR @LevelId=1) AND ApprovalStatus<>'Delegated')
  555.  
  556. End
  557. Else
  558. Begin
  559. Insert Into @oUsers(UserID)
  560. SELECT
  561. U.UserID
  562. from
  563. tApprovalsNew an
  564. Inner Join tUsers u On u.UserID = an.ApproverID AND u.IsActive=1 AND u.IsFileOnlyUser=0
  565. where
  566. an.SourceType = @Source And an.SourceID = @SourceID
  567. AND ((@ApprovalID=0 OR ApprovalID=@ApprovalID) AND ApprovalStatus<>'Delegated')
  568.  
  569. End
  570.  
  571. End
  572. Else If(@EnumID=7)
  573. Begin
  574. Insert Into @oUsers(UserID)
  575. Select U.UserID
  576. from tClientSettings C
  577. Inner Join tUsers U on U.ClientId=@ClientId AND U.UserID=C.ComplianceSupervisorId AND U.IsActive=1 AND U.IsFileOnlyUser=0
  578. Where C.ClientID=@ClientId
  579. End
  580. Else If(@EnumID=9)
  581. Begin
  582. Insert Into @oUsers(UserID)
  583. Select UserID from [dbo].[ufn_Users_GetEnumUsersofAudit](@EnumID,@Source,@SourceId)
  584. End
  585. Else If(@EnumID=10)
  586. Begin
  587. Insert Into @oUsers(UserID)
  588. Select UserID from [dbo].[ufn_Users_GetEnumUsersofAudit](@EnumID,@Source,@SourceId)
  589. End
  590. Else If(@EnumID=11)
  591. Begin
  592. Insert Into @oUsers(UserID)
  593. Select UserID from [dbo].[ufn_Users_GetEnumUsersofAudit](@EnumID,@Source,@SourceId)
  594. End
  595. Else If(@EnumID=13)
  596. Begin
  597. Insert Into @oUsers(UserID)
  598. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  599. End
  600. Else If(@EnumID=14)
  601. Begin
  602. Insert Into @oUsers(UserID)
  603. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  604. End
  605. Else If(@EnumID=15)
  606. Begin
  607. Insert Into @oUsers(UserID)
  608. Select UserID from [dbo].[ufn_Users_GetEnumUsersofObservation](@EnumID,@SourceId)
  609. End
  610. Else If(@EnumID=16)
  611. Begin
  612. Insert Into @oUsers(UserID)
  613. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  614. End
  615. Else If(@EnumID=17)
  616. Begin
  617. Insert Into @oUsers(UserID)
  618. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  619. End
  620. Else If(@EnumID=18)
  621. Begin
  622. Insert Into @oUsers(UserID)
  623. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  624. End
  625. Else If(@EnumID=19)
  626. Begin
  627. Insert Into @oUsers(UserID)
  628. Select UserID from [dbo].[ufn_Users_GetEnumUsersofIncident](@EmailTemplateID,@EnumID,@LevelNo,@Source,@SourceId,@SourceId2)
  629. End
  630. Else If(@EnumID=20)
  631. Begin
  632. Insert Into @oUsers(UserID)
  633. Select UserID from [dbo].[ufn_Users_GetEnumUsersofDMS](@EnumID,@SourceId)
  634. End
  635. Else If(@EnumID=21)
  636. Begin
  637. Insert Into @oUsers(UserID)
  638. Select UserID from [dbo].[ufn_Users_GetEnumUsersofDMS](@EnumID,@SourceId)
  639. End
  640. Else If(@EnumID=22)
  641. Begin
  642. Insert Into @oUsers(UserID)
  643. Select UserID from [dbo].[ufn_Users_GetEnumUsersofDMS](@EnumID,@SourceId)
  644. End
  645. Else If(@EnumID=23)
  646. Begin
  647. Insert Into @oUsers(UserID)
  648. Select UserID from [dbo].[ufn_Users_GetEnumUsersofDMS](@EnumID,@SourceId)
  649. End
  650. Else If(@EnumID=24)
  651. Begin
  652. Insert Into @oUsers(UserID)
  653. Select UserID from tUsers U Where U.UserID=@UserID AND U.IsActive=1 AND U.IsFileOnlyUser=0
  654. End
  655. Else If(@EnumID=25)
  656. Begin
  657. Insert Into @oUsers(UserID)
  658. Select UserID from [dbo].[ufn_Users_GetEnumUsersofRisk](@EnumID,@SourceId)
  659. End
  660.  
  661. RETURN
  662. END
  663. GO
  664.  
  665.  
  666. ----------------------------------
  667.  
  668.  
  669.  
  670. /****** Object: StoredProcedure [dbo].[proc_ApprovalsNew_SelectAllApproversBySourceID] Script Date: 07/15/2016 17:28:14 ******/
  671. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_ApprovalsNew_SelectAllApproversBySourceID]') AND type in (N'P', N'PC'))
  672. DROP PROCEDURE [dbo].[proc_ApprovalsNew_SelectAllApproversBySourceID]
  673. GO
  674.  
  675. /****** Object: StoredProcedure [dbo].[proc_ApprovalsNew_ResetAllApprovalStatus] Script Date: 07/15/2016 17:28:14 ******/
  676. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_ApprovalsNew_ResetAllApprovalStatus]') AND type in (N'P', N'PC'))
  677. DROP PROCEDURE [dbo].[proc_ApprovalsNew_ResetAllApprovalStatus]
  678. GO
  679.  
  680. /****** Object: StoredProcedure [dbo].[proc_ApprovalsNew_DeleteByID] Script Date: 07/15/2016 17:28:14 ******/
  681. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[proc_ApprovalsNew_DeleteByID]') AND type in (N'P', N'PC'))
  682. DROP PROCEDURE [dbo].[proc_ApprovalsNew_DeleteByID]
  683. GO
  684.  
  685.  
  686.  
  687. /****** Object: StoredProcedure [dbo].[proc_ApprovalsNew_SelectAllApproversBySourceID] Script Date: 07/15/2016 17:28:14 ******/
  688. SET ANSI_NULLS ON
  689. GO
  690.  
  691. SET QUOTED_IDENTIFIER ON
  692. GO
  693.  
  694.  
  695. --[proc_ApprovalsNew_SelectAllApproversBySourceID] 'Audit',1136,16296
  696. CREATE PROCEDURE [dbo].[proc_ApprovalsNew_SelectAllApproversBySourceID]
  697. (
  698. @SourceType varchar(50),
  699. @SourceID int,
  700. @CurrentUserID int
  701. )
  702. AS SET NOCOUNT ON
  703.  
  704. Declare @SourceStatus as varchar(50)
  705.  
  706. IF(@SourceType = 'Audit')
  707. Select @SourceStatus = [Status] From t_Audit_Audit where AuditId = @SourceID
  708. ELSE IF(@SourceType = 'Incident')
  709. Select @SourceStatus = [Status] From tIncident where IncidentID = @SourceID
  710. ELSE IF(@SourceType = 'Risk')
  711. Select @SourceStatus = [Status] From tRiskHazard where HazardID = @SourceID
  712. ELSE IF(@SourceType = 'Legal')
  713. Select @SourceStatus = [Status] From tLegalComplianceGeneralInfo where LegalComplianceID = @SourceID
  714.  
  715. ;with cte1 as
  716. (
  717. SELECT
  718. an.*,
  719. u.UserEmail,
  720. CASE WHEN u.NotificationEmail IS NULL OR u.NotificationEmail='' THEN u.UserEmail ELSE u.NotificationEmail END NotificationEmail,
  721. u.IsActive,
  722. u.FirstName + ' ' + u.LastName UserName,
  723. Cast(
  724. Case --when @CurrentUserID = an.ApproverID And SourceType='Legal' And an.ApprovalStatus='Pending' And @SourceStatus = 'Changes Required' Then 1
  725. when @CurrentUserID = an.ApproverID And an.ApprovalStatus = 'Sent For Approval' And @SourceStatus = 'Approvals In Progress' Then 1
  726. Else 0 End as bit
  727. ) As ShowSignOff,U.UserID
  728. from
  729. tApprovalsNew an
  730. Inner Join tUsers u On u.UserID = an.ApproverID
  731. where
  732. an.SourceType = @SourceType And an.SourceID = @SourceID
  733. )
  734.  
  735. select cte1.* from cte1
  736. left Join tIncident I on I.IncidentID=@SourceId And @SourceType='Incident'
  737. Inner Join tUsers On cte1.UserID = tUsers.UserID
  738. LEFT Join tUserModule M On M.UserId=tUsers.UserID And M.ModuleId=2
  739. LEFT join tRole R On R.RoleID=M.RoleID
  740. where isnull(I.MarkConfidential,0) = 0 or R.I_AllowIncidentCFReporting = 1
  741. GO
  742.  
  743. /****** Object: StoredProcedure [dbo].[proc_ApprovalsNew_ResetAllApprovalStatus] Script Date: 07/15/2016 17:28:14 ******/
  744. SET ANSI_NULLS ON
  745. GO
  746.  
  747. SET QUOTED_IDENTIFIER ON
  748. GO
  749.  
  750.  
  751.  
  752. CREATE PROCedure [dbo].[proc_ApprovalsNew_ResetAllApprovalStatus]
  753. (
  754. @SourceType VARCHAR(50),
  755. @SourceID INT,
  756. @ApprovalStatus VARCHAR(50),
  757. @LC_UserID INT,
  758. @isReopen BIT
  759. )
  760. AS
  761. BEGIN
  762.  
  763. IF (@isReopen=1)
  764. BEGIN
  765.  
  766. INSERT INTO tApprovalsNewHistory (SourceType,SourceID,ApproverID,ApprovalStatus,Comments,
  767. LastNotifiedDate,SignedDate,Created_UserID,Created_TS,LC_UserID,LC_TS,GroupId,GroupType)
  768. SELECT SourceType,SourceID,ApproverID,ApprovalStatus,Comments,LastNotifiedDate,SignedDate,
  769. Created_UserID,Created_TS,LC_UserID,LC_TS,GroupId,GroupType
  770. FROM tApprovalsNew WHERE SourceType = @SourceType And SourceID =@SourceID
  771. And ApprovalStatus <> 'Delegated' And ApprovalStatus <> 'Pending'
  772.  
  773. END
  774.  
  775. UPDATE tApprovalsNew
  776. SET ApprovalStatus = @ApprovalStatus,LC_UserID =@LC_UserID ,LC_TS = GETUTCDATE(),SignedDate = null,Comments=''
  777. WHERE SourceType = @SourceType And SourceID =@SourceID And ApprovalStatus <> 'Delegated'
  778.  
  779. END
  780. GO
  781.  
  782. /****** Object: StoredProcedure [dbo].[proc_ApprovalsNew_DeleteByID] Script Date: 07/15/2016 17:28:14 ******/
  783. SET ANSI_NULLS ON
  784. GO
  785.  
  786. SET QUOTED_IDENTIFIER ON
  787. GO
  788.  
  789.  
  790. CREATE PROCedure [dbo].[proc_ApprovalsNew_DeleteByID]
  791. (
  792. @ApprovalID int
  793. )
  794. AS
  795.  
  796. declare @SourceType varchar(50), @SourceID int, @Status nvarchar(50)
  797. SELECT @SourceType = SourceType, @SourceID = SourceID FROM tApprovalsNew where ApprovalID =@ApprovalID
  798.  
  799. DELETE from tApprovalsNew where ApprovalID =@ApprovalID
  800.  
  801. IF @SourceType = 'Incident'
  802. BEGIN
  803. SELECT @Status=[Status] FROM tIncident WHERE IncidentID = @SourceID
  804. IF @Status = 'Approvals In Progress'
  805. BEGIN
  806. IF Not EXISTS(SELECT * FROM tApprovalsNew WHERE SourceType = @SourceType AND SourceID = @SourceID AND ApprovalStatus IN('Pending','Sent For Approval'))
  807. AND EXISTS(SELECT * FROM tApprovalsNew WHERE SourceType = @SourceType AND SourceID = @SourceID AND ApprovalStatus ='Approved')
  808. BEGIN
  809. UPDATE tIncident SET [Status] = 'Approved',ApprovedOn=GETUTCDATE() WHERE IncidentID = @SourceID
  810. select 1
  811. END
  812. Else
  813. Begin
  814. select 0
  815. End
  816. END
  817. Else
  818. Begin
  819. select 0
  820. End
  821. END
  822. Else
  823. BEGIN
  824. select 0
  825. END
  826.  
  827.  
  828. GO
  829.  
  830.  
  831. --------------------------------------------------------
  832. --Added by Jatin On 15-Jul-2016 Task 368: Fixed performance issue with new env report
  833. --------------------------------------------------------
  834.  
  835.  
  836. /****** Object: UserDefinedFunction [dbo].[ufn_IndicatorChildren] Script Date: 07/14/2016 19:27:50 ******/
  837. IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ufn_IndicatorChildren]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
  838. DROP FUNCTION [dbo].[ufn_IndicatorChildren]
  839. GO
  840.  
  841.  
  842. /****** Object: UserDefinedFunction [dbo].[ufn_IndicatorChildren] Script Date: 07/14/2016 19:27:50 ******/
  843. SET ANSI_NULLS ON
  844. GO
  845.  
  846. SET QUOTED_IDENTIFIER ON
  847. GO
  848.  
  849. CREATE FUNCTION [dbo].[ufn_IndicatorChildren]
  850. (
  851. @IndicatorGroupID int
  852. )
  853. RETURNS @otTemp TABLE(IndicatorGroupID int,IndicatorID int, ParentIndicatorGroupID int)
  854. AS
  855. BEGIN
  856.  
  857. Declare @ClientID int
  858. Select @ClientID = ClientID From tIndicatorGroups where IndicatorGroupID =@IndicatorGroupID
  859.  
  860. ;WITH ChildIndicators (ParentIndicatorGroupID, IndicatorID,ParentGroupID)
  861. AS
  862. (
  863. -- Anchor member definition
  864. SELECT e.IndicatorGroupID, e.ChildIndicatorID,e.ParentGroupID
  865. FROM tIndicatorGroups AS e
  866. WHERE ClientID = @ClientID And ParentGroupID = @IndicatorGroupID
  867. UNION ALL
  868. -- Recursive member definition
  869. SELECT e.IndicatorGroupID, e.ChildIndicatorID,e.ParentGroupID
  870. FROM tIndicatorGroups AS e
  871. INNER JOIN ChildIndicators AS d
  872. ON e.ClientID = @ClientID And e.ParentGroupID = d.ParentIndicatorGroupID
  873. )
  874.  
  875. INSERT INTO @otTemp(IndicatorGroupID,IndicatorID,ParentIndicatorGroupID)
  876. SELECT ParentIndicatorGroupID, IndicatorID,ParentGroupID FROM ChildIndicators
  877. UNION ALL
  878. SELECT IndicatorGroupID, ChildIndicatorID,ParentGroupID From tIndicatorGroups
  879. where IndicatorGroupID = @IndicatorGroupID
  880.  
  881.  
  882. RETURN
  883. END
  884.  
  885.  
  886. GO
  887.  
  888.  
  889. /****** Object: Index [NCL_IndicatorGroups] Script Date: 07/14/2016 19:24:39 ******/
  890. IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tIndicatorGroups]') AND name = N'NCL_IndicatorGroups')
  891. DROP INDEX [NCL_IndicatorGroups] ON [dbo].[tIndicatorGroups] WITH ( ONLINE = OFF )
  892. GO
  893.  
  894.  
  895. /****** Object: Index [NCL_IndicatorGroups] Script Date: 07/14/2016 19:24:51 ******/
  896. CREATE NONCLUSTERED INDEX [NCL_IndicatorGroups] ON [dbo].[tIndicatorGroups]
  897. (
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement