Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. ALTER PROC [dbo].[InsertPromotions]
  2. @Employee_ID int,
  3. @NotificaitonDate date,
  4. @FromBPS int,
  5. @ToBPS int,
  6. @FromDesignation_ID int,
  7. @ToDesignation_ID int,
  8. @Remarks varchar(1000),
  9. @User_ID int,
  10. @Organization_ID int,
  11. @EntryDateTime datetime = NULL,
  12. @IsPromoted bit,
  13. @RecentPK int OUTPUT,
  14. @ReturnStatus bit OUTPUT,
  15. @ReturnStatusMessage varchar(max) OUTPUT,
  16. @PageName varchar(50),
  17. @IP varchar(20)
  18. AS
  19. declare @recordID int;
  20.  
  21. BEGIN
  22.  
  23. BEGIN TRY
  24. BEGIN TRANSACTION tran1;
  25.  
  26. SET @EntryDateTime =(SELECT GETDATE());
  27. ----
  28. DECLARE @DistrictID int;
  29.  
  30. SET @DistrictID =ISNULL((SELECT orgs.District_ID
  31. FROM MC_MemberShip.Membership.Organization orgs
  32. WHERE orgs.OrganizationID = @Organization_ID
  33. ),0);
  34.  
  35. CREATE TABLE #EmployeeServiceInfo
  36. (
  37. BPS tinyint,
  38. Designation_ID int,
  39. Branch_ID int,
  40. Office_ID int,
  41. Cadre varchar(100),
  42. Remarks varchar(1000),
  43. ServiceType tinyint,
  44. ServiceDistrict int
  45. );
  46.  
  47. INSERT INTO #EmployeeServiceInfo
  48. EXEC SelectServiceBPSDesignationFromPersonalInfoByID @Employee_ID;
  49.  
  50. DECLARE @Designation_ID int;
  51. SET @Designation_ID = CASE WHEN @IsPromoted = 0 THEN
  52. (
  53. SELECT TempServiceInfo.Designation_ID
  54. FROM #EmployeeServiceInfo TempServiceInfo
  55. ) WHEN @IsPromoted = 1 AND
  56. @ToDesignation_ID = -1 THEN
  57. (
  58. SELECT TempServiceInfo.Designation_ID
  59. FROM #EmployeeServiceInfo TempServiceInfo
  60. ) WHEN @IsPromoted = 1 AND
  61. @ToDesignation_ID != -1 THEN @ToDesignation_ID END;
  62.  
  63. --Audit Entry
  64. set @recordID = SCOPE_IDENTITY();
  65. -- exec MC_MemberShip.dbo.AuditEntry @PageName , @IP , @User_ID , @recordID , null , 2
  66. --Audit Entry
  67.  
  68.  
  69. IF(@FromDesignation_ID = -1 AND
  70. @FromBPS = 0 AND
  71. @IsPromoted = 1
  72. ) -- In case if we are promoting all employees
  73. BEGIN
  74. SET @FromDesignation_ID =
  75. (
  76. SELECT TempServiceInfo.Designation_ID
  77. FROM #EmployeeServiceInfo TempServiceInfo
  78. );
  79. SET @FromBPS =
  80. (
  81. SELECT TempServiceInfo.BPS
  82. FROM #EmployeeServiceInfo TempServiceInfo
  83. );
  84. END;
  85.  
  86. DECLARE @BPS int;
  87. SET @BPS = CASE WHEN @ToBPS = 0 THEN
  88. (
  89. SELECT BPS
  90. FROM #EmployeeServiceInfo
  91. ) ELSE @ToBPS END;
  92.  
  93. DECLARE @Office_ID int;
  94. SET @Office_ID =
  95. (
  96. SELECT Office_ID
  97. FROM #EmployeeServiceInfo
  98. );
  99.  
  100. DECLARE @ServiceDistrict int;
  101. SET @ServiceDistrict =
  102. (
  103. SELECT ServiceDistrict
  104. FROM #EmployeeServiceInfo
  105. );
  106.  
  107. DECLARE @ServiceType tinyint;
  108. SET @ServiceType =
  109. (
  110. SELECT ServiceType
  111. FROM #EmployeeServiceInfo
  112. );
  113.  
  114. DECLARE @Department_ID int;
  115. SET @Department_ID =
  116. (
  117. SELECT Branch_ID
  118. FROM #EmployeeServiceInfo
  119. );
  120.  
  121. DECLARE @Cadre varchar;
  122. SET @Cadre =
  123. (
  124. SELECT Cadre
  125. FROM #EmployeeServiceInfo
  126. );
  127. ----
  128. IF(@IsPromoted = 0)
  129. BEGIN
  130.  
  131. INSERT INTO Employee.Promotions(Employee_ID, NotificaitonDate, FromBPS, ToBPS, FromDesignation_ID, ToDesignation_ID, Remarks, User_ID, Organization_ID, EntryDateTime, IsPromoted)
  132. VALUES(@Employee_ID, @NotificaitonDate, @FromBPS, @BPS, @Designation_ID, @Designation_ID, @Remarks, @User_ID, @Organization_ID, @EntryDateTime, @IsPromoted);
  133.  
  134. --Audit Entry
  135. set @recordID = SCOPE_IDENTITY();
  136. --exec MC_MemberShip.dbo.AuditEntry @PageName , @IP , @User_ID , @recordID , null , 2
  137. --Audit Entry
  138. END;
  139. ELSE
  140. BEGIN
  141. INSERT INTO Employee.Promotions(Employee_ID, NotificaitonDate, FromBPS, ToBPS, FromDesignation_ID, ToDesignation_ID, Remarks, User_ID, Organization_ID, EntryDateTime, IsPromoted)
  142. VALUES(@Employee_ID, @NotificaitonDate, @FromBPS, @BPS, @FromDesignation_ID, @Designation_ID, @Remarks, @User_ID, @Organization_ID, @EntryDateTime, @IsPromoted);
  143. --Audit Entry
  144. set @recordID = SCOPE_IDENTITY();
  145. --exec MC_MemberShip.dbo.AuditEntry @PageName , @IP , @User_ID , @recordID , null , 2
  146. --Audit Entry
  147. END;
  148.  
  149.  
  150.  
  151.  
  152. EXEC InsertServiceInfo @Designation_ID, @BPS, @ServiceDistrict, 3, @NotificaitonDate, NULL, '', @Employee_ID, @ServiceType, '', @Cadre, @User_ID, @Organization_ID, @DistrictID, NULL, @Office_ID, @Department_ID, @Remarks, 1, 1, '';
  153.  
  154. UPDATE PersonalInfo
  155. SET Designation_ID = @Designation_ID, BPS = @BPS
  156. WHERE pk_PersonalInfo_ID = @Employee_ID;
  157.  
  158. SET @RecentPK = 0 --IDENT_CURRENT('Employee.Promotions');
  159. --Audit Entry
  160. --set @recordID = IDENT_CURRENT('Employee.Promotions')
  161. --exec MC_MemberShip.dbo.AuditEntry @PageName , @IP , @User_ID , @recordID , null , 3
  162. --Audit Entry
  163. --Calculating new basic salary
  164.  
  165. IF(@ToBPS > 0) --Calculate new salary only if BPS is mentioned otherwise no new saalry will be calculated
  166. BEGIN
  167.  
  168. CREATE TABLE #SalaryChart --Temp table to store salary chart details
  169. (
  170. YearID int,
  171. InitialSalary money,
  172. Increment money
  173. );
  174.  
  175. INSERT INTO #SalaryChart(YearID, InitialSalary, Increment)
  176. SELECT sc.Year_ID, sc.InitialSalary, sc.Increment
  177. FROM Setup.SalaryChart sc
  178. WHERE sc.BPS = @BPS AND
  179. IsActive = 1;
  180.  
  181. DECLARE @EmpBasicSalary money;
  182. DECLARE @EmployeeMinSalary money;
  183. DECLARE @YearlyIncrement money;
  184. DECLARE @NewBasicSalary money;
  185. DECLARE @Year_ID int;
  186.  
  187.  
  188. SET @Year_ID =
  189. (
  190. SELECT sc.YearID
  191. FROM #SalaryChart sc
  192. );
  193.  
  194. SET @EmpBasicSalary =
  195. (
  196. SELECT pi.BasicSalary 'EmployeeBasicSalary'
  197. FROM PersonalInfo pi
  198. WHERE bps = @BPS AND
  199. pk_PersonalInfo_ID = @Employee_ID
  200. );
  201.  
  202. SET @EmployeeMinSalary =
  203. (
  204. SELECT sc.InitialSalary
  205. FROM #SalaryChart sc
  206. );
  207.  
  208. SET @YearlyIncrement =
  209. (
  210. SELECT sc.Increment
  211. FROM #SalaryChart sc
  212. );
  213.  
  214. WHILE(@EmployeeMinSalary < @EmpBasicSalary)
  215. BEGIN
  216. SET @EmployeeMinSalary = @EmployeeMinSalary + @YearlyIncrement;
  217. END;
  218.  
  219. SET @NewBasicSalary = @EmployeeMinSalary + @YearlyIncrement;
  220.  
  221. set @YearlyIncrement=@NewBasicSalary-@EmpBasicSalary;
  222.  
  223. INSERT INTO Employee.SalaryDetails(NotificationDate, Employee_ID, Year_ID, BasicSalary, IncrementAmount, NewBasicSalary, SystemUser_ID, Organization_ID, District_ID, EntryDateTime)
  224. VALUES(@NotificaitonDate, @Employee_ID, @Year_ID, @EmpBasicSalary, @YearlyIncrement, @NewBasicSalary, @User_ID, @Organization_ID, @DistrictID, @EntryDateTime);
  225. --Audit Entry
  226. set @recordID = SCOPE_IDENTITY();
  227. --exec MC_MemberShip.dbo.AuditEntry @PageName , @IP , @User_ID , @recordID , null , 2
  228. --Audit Entry
  229.  
  230. UPDATE PersonalInfo
  231. SET BasicSalary = @NewBasicSalary
  232. WHERE pk_PersonalInfo_ID = @Employee_ID;
  233. --Audit Entry
  234. set @recordID = @Employee_ID;
  235. --exec MC_MemberShip.dbo.AuditEntry @PageName , @IP , @User_ID , @recordID , null , 3
  236. --Audit Entry
  237. END;
  238. ------ Salary's Part
  239. SET @ReturnStatus = 1;
  240.  
  241. COMMIT TRANSACTION tran1;
  242. END TRY
  243. BEGIN CATCH
  244.  
  245. SET @ReturnStatus = 0;
  246. SET @ReturnStatusMessage =
  247. (
  248. SELECT ERROR_MESSAGE()
  249. );
  250. Set @RecentPK=0
  251. SEt @recordID=0
  252.  
  253. ROLLBACK TRANSACTION tran1;
  254. END CATCH;
  255.  
  256.  
  257. END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement