Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. ALTER PROCEDURE [dbo].[FinanceSites_GetCapacityOpenRequests]
  2. (
  3. @site int, @cCenter int, @start datetime
  4. )
  5. AS
  6. BEGIN
  7. IF (SELECT object_id('TempDB..#tmp')) IS NOT NULL DROP TABLE #tmp
  8. IF (SELECT object_id('TempDB..#res')) IS NOT NULL DROP TABLE #res
  9. IF (SELECT object_id('TempDB..#edgeOfsetForResReq')) IS NOT NULL DROP TABLE #edgeOfsetForResReq
  10.  
  11. declare @periods table
  12. (
  13. id int identity(1,1) primary key,
  14. name varchar(300),
  15. value date
  16. )
  17.  
  18. declare @end datetime = DATEADD(mm,12,@start), -- default period 12 months
  19. @granularity nvarchar(10) = 'month';
  20.  
  21. insert into @periods select name, value from fGetPeriodsInDateSpan(@start, @end, @granularity);
  22. --select * from @periods
  23.  
  24. declare @tableSpecification HelperTableSpecification;
  25. create table #res (ptrrid int, person nvarchar(300), empid int,costcenter int, projectid int, projectname nvarchar (500), taskid int, startdate datetime, enddate datetime, task nvarchar(300),ltid int, labortype nvarchar(300), frole int)
  26.  
  27. insert into @tableSpecification select name,'float' from @periods
  28. declare @alterSql nvarchar(max) = dbo.fGetSqlByTableSpecification(@tableSpecification, '#res')
  29. exec(@alterSql)
  30.  
  31. select
  32. fs.id as fsid,
  33. ptm.id as ptmid,
  34. pt.id as ptid, pt.taskname, pt.startdate, pt.schedulinggranularity, pt.enddate,
  35. p.id as pid, p.projname as pname,
  36. ptrr.id as ptrrid,
  37. rra.*,
  38. rrrr.id as rrrrid,
  39. e.name, e.id as empid,
  40. ra.id as raid,
  41. plt.labortypelabel,
  42. plt.id as pltid,
  43. plt.functionalrole as frole,
  44. plt.costcenter
  45. into
  46. #tmp
  47. from
  48. FinanceSites as fs
  49. inner join projectteams as ptm on fs.id = ptm.site
  50. inner join project as p on ptm.projectid = p.id
  51. inner join projecttasks as pt on pt.projectid = p.id
  52. inner join projectTasks_ResourceRequirements as ptrr on ptrr.projecttask = pt.id
  53. left join ResourceAssignments as ra on ra.resourceRequirement = ptrr.id
  54. left join ResourceRequirements_Allocations as rra on rra.resourcerequirement = ptrr.id
  55. left join ResourceRequirements_RequestedResources as rrrr on rrrr.ResourceRequirement = ptrr.id
  56. left join Employees as e on e.id = rrrr.Resource
  57. left join projectLaborTypes as plt on plt.id = ptrr.projectlabortype
  58. where
  59. (ra.id is null) and fs.id = @site and ((plt.costcenter = @cCenter) or (e.costcenter = @cCenter) or (@cCenter = -1)) and (rra.allocation > 0)
  60. order by pid
  61.  
  62. alter table #tmp add nDate date
  63. update #tmp set nDate = dbo.fGetDateByDateAndOffset(startdate,schedulinggranularity, scheduleoffset);
  64. delete from #tmp where (nDate < @start) or (nDate > @end) or (schedulinggranularity = 0) -- for now delete all rows with task level granularity
  65. --select * from #tmp
  66. declare @tmpRowsCount int = (select count(*) from #tmp)
  67. if (@tmpRowsCount > 1)
  68. begin
  69. insert into #res (ptrrid, person, empid, costcenter, projectid, projectname, taskid, task, startdate, enddate, ltid, labortype, frole)
  70. values(0, null, null, null, null, null, null, null,null, null, null, 'Total', null) -- insert total row
  71. end
  72.  
  73. insert into #res
  74. (ptrrid, person, empid, costcenter, projectid, projectname, taskid, task, startdate, enddate, ltid, labortype, frole)
  75. select distinct ptrrid, name, empid, costcenter, pid, pname, ptid, taskname, startdate, enddate, pltid, labortypelabel, frole from #tmp
  76.  
  77. declare @name nvarchar(100), @value date
  78.  
  79. while (select Count(*) From @periods) > 0 -- loop through rows
  80. begin
  81. select top 1 @name = name From @periods order by id -- current columnName
  82. select top 1 @value = value From @periods order by id -- current columnType
  83. print(@value)
  84. exec('update #res set [' + @name + '] = 0');
  85. exec('update #res set [' + @name + '] = round(#tmp.allocation * 160, 0) from #res inner join #tmp on #tmp.ptrrid = #res.ptrrid where #tmp.nDate='''+@value+'''');
  86. exec('update #res set [' + @name + '] = (select sum('+@name+') from #res) where ptrrid=0');
  87. delete @periods where value = @value;
  88. end
  89.  
  90. ---- In this part we get start & end, excluding begin & end months with 0 allocation
  91. select
  92. rr.id as resReqId,
  93. dbo.fGetDateByDateAndOffset(startdate,planninggranularity, min(rra.scheduleoffset)) as start,
  94. dbo.fGetDateByDateAndOffset(startdate,planninggranularity, max(rra.scheduleoffset)) as 'end'
  95. into
  96. #edgeOfsetForResReq
  97. from
  98. #res as r
  99. left join projectTasks_ResourceRequirements as rr on r.ptrrid = rr.id
  100. left join ResourceRequirements_Allocations as rra on rra.resourcerequirement = rr.id
  101. where
  102. (r.ptrrid > 0) and (allocation > 0)
  103. group by
  104. rr.id, rr.planninggranularity, r.startdate --granularity & startdate should be the same for equal rr.id
  105.  
  106. update #res set startdate = edge.start, enddate = edge.[end] from #edgeOfsetForResReq as edge where #res.ptrrid = edge.resReqId
  107. ----
  108.  
  109. -- Start & enddate is NOT project task start/end date
  110. select * from #res order by ptrrid
  111.  
  112. IF (SELECT object_id('TempDB..#tmp')) IS NOT NULL DROP TABLE #tmp
  113. IF (SELECT object_id('TempDB..#res')) IS NOT NULL DROP TABLE #res
  114. IF (SELECT object_id('TempDB..#edgeOfsetForResReq')) IS NOT NULL DROP TABLE #edgeOfsetForResReq
  115. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement