Project

General

Profile

Bug #3128 » Rpt_Mega_WTRecap_Summary_New_Header.sql

Tri Rizqiaty, 10/14/2024 05:09 PM

 
1
CREATE PROCEDURE [dbo].[Rpt_Mega_WTRecap_Summary_New_Header]
2
	@landscape varchar(3),
3
	@payperiod varchar(6)
4

    
5
AS
6

    
7

    
8
 --declare @landscape varchar(3) = N'100'
9
 --declare @payperiod varchar(6) = '201102'
10

    
11

    
12

    
13
DECLARE @payPeriodStartDate varchar(8)
14
DECLARE @payPeriodEndDate varchar(8)
15
DECLARE @payPeriodEndDateDt datetime
16

    
17
DECLARE @payPeriodStr varchar(50)
18

    
19

    
20
SET @payPeriodStartDate = @payPeriod + '01'
21
SET @payPeriodEndDateDt = DATEADD(dd, -1, DATEADD(mm, 1, CONVERT(datetime, @payPeriodStartDate)))
22
SET @payPeriodEndDate = @payPeriod + CONVERT(varchar(2), DATEPART(dd, @payPeriodEndDateDt))
23

    
24
set @payPeriodStr = DATENAME(month,@payPeriodEndDateDt) + ' ' + left(@payPeriodEndDate,4)
25

    
26
 declare @logo varbinary(max)
27
 declare @companyname varchar(50)
28
 
29
 SELECT TOP 1
30
       @companyname = [description]
31
      ,@logo = [logo]
32
  FROM [dbo].[base_cust_ref_companycode]  
33
  where landscape = @landscape 
34
 
35
declare @group_by varchar(50)
36
declare @group_by_colname varchar(50)
37

    
38
select top 1 @group_by = col_id, @group_by_colname = descr
39
from dbo.hr_cust_py_report_additional 
40
where	landscape = @landscape 
41
  and	rep_id = 'WT03R' 
42
  and	start_date <= @payPeriodEndDate 
43
  and	end_date >= @payPeriodEndDate 
44
  and	is_grouped = 1
45

    
46
if @group_by is null
47
begin
48

    
49
set @group_by  = 'cost_center_descr'
50
set @group_by_colname  = 'Cost Center Descr'
51

    
52
end
53

    
54
  
55
  
56
  declare @hr_cust_py_report TABLE (
57
	[landscape] [varchar](4) NOT NULL,
58
	[rep_id] [varchar](50) NOT NULL,
59
	[row] [int] NOT NULL,
60
	[col_no] [int] NOT NULL,
61
	[seq_no] [int] NOT NULL,
62
	[wage_type] [varchar](5) NULL,
63
	[descr] [varchar](50) NULL
64
)
65
  
66
insert into @hr_cust_py_report
67
SELECT [landscape]
68
      ,[rep_id]
69
      ,[row]
70
      ,[col_no]
71
      ,[seq_no]
72
      ,[wage_type]
73
      ,[descr]
74
  FROM [dbo].[hr_cust_py_report] WHERE start_date <= @payPeriodEndDate
75
  AND end_date >= @payPeriodEndDate and rep_id = 'WT01R'
76
  
77
     declare @hr_cust_py_report_col_name TABLE (
78
	[landscape] [varchar](4)  NULL,
79
	[rep_id] [varchar](50) NULL,
80
	[row] [int]  NULL,
81
	[col_no] [int]  NULL,
82
	column_name varchar(6))
83

    
84
insert into @hr_cust_py_report_col_name	
85
select *,
86
'WT' + right('00' + convert(varchar,(ROW_NUMBER () OVER (ORDER BY landscape, rep_id, row, col_no) + 49) / 50) ,2) 
87
+ right('00' + convert(varchar,((ROW_NUMBER () OVER (ORDER BY landscape, rep_id, row, col_no) -1) % 50)+1) ,2) as column_name
88
 from(
89
  select distinct landscape, rep_id, row, col_no from @hr_cust_py_report )a ORDER BY landscape, rep_id, row, col_no
90
  
91
----//---- cek kolom ADF buat header dari py_report_additional
92
----select top 1000 'ADF' + right('00' + convert(varchar,ROW_NUMBER() OVER (ORDER BY col_no)),2) as column_id ,case is_visible when 1 then descr else '' end as description from dbo.hr_cust_py_report_additional where landscape = @landscape and rep_id = 'WT01R' and start_date <= @payPeriodEndDate and end_date >= @payPeriodEndDate order by col_no	
93
----//----
94

    
95
	
96
  
97
  declare @row int
98
  declare @col int
99
  declare @column_name varchar(6)
100
  set @row = 0
101
  set @col = 0
102
  while @row < 4
103
  begin
104
  set @row = @row + 1
105
  set @col = 0
106
    while @col < 50
107
		begin
108
		set @col = @col + 1
109
		set @column_name = null
110
		
111
		select @column_name=column_name  from @hr_cust_py_report_col_name
112
		where column_name = 'WT' + right('00' + convert(varchar, @row),2) + right('00' + convert(varchar, @col),2)
113
if @column_name is null or @column_name = ''
114
begin
115
insert into @hr_cust_py_report_col_name
116
select @landscape,'',0,0,'WT' + right('00' + convert(varchar, @row),2) + right('00' + convert(varchar, @col),2)
117
end
118

    
119

    
120

    
121
		end
122
		
123
	end
124
	
125
	    declare @hr_cust_py_report_desc TABLE (
126
	[landscape] [varchar](4) NOT NULL,
127
	[rep_id] [varchar](50) NOT NULL,
128
	[row] [int] NOT NULL,
129
	[col_no] [int] NOT NULL,
130
	column_name varchar(6),
131
	[description] [varchar](50) NULL)
132
	
133
	insert into @hr_cust_py_report_desc
134
	select a.*, b.descr from @hr_cust_py_report_col_name as a
135
	left join(
136
	select MAX(landscape) as landscape, MAX(rep_id) as rep_id, MAX(row) as row, MAX(col_no) as col_no,MAX(descr) as descr
137
	 from @hr_cust_py_report group by landscape, rep_id, row,col_no)as b
138
	 on a.landscape =b.landscape and 
139
	 a.rep_id=b.rep_id and
140
	 a.row=b.row and
141
	 a.col_no=b.col_no
142

    
143
  select * from (
144
  select  @companyname as companyname, @logo as logo, @payPeriodStr as payPeriodStr, @group_by as group_by, @group_by_colname as group_by_colname, * from (select column_name, description from @hr_cust_py_report_desc)as SourceTable
145
PIVOT
146
(
147
  max(description)
148
  for column_name in  ([WT0101],[WT0102],[WT0103],[WT0104],[WT0105],[WT0106],[WT0107],[WT0108],[WT0109],[WT0110],[WT0111],[WT0112],[WT0113],[WT0114],[WT0115],[WT0116],[WT0117],[WT0118],[WT0119],[WT0120],[WT0121],[WT0122],[WT0123],[WT0124],[WT0125],[WT0126],[WT0127],[WT0128],[WT0129],[WT0130],[WT0131],[WT0132],[WT0133],[WT0134],[WT0135],[WT0136],[WT0137],[WT0138],[WT0139],[WT0140],[WT0141],[WT0142],[WT0143],[WT0144],[WT0145],[WT0146],[WT0147],[WT0148],[WT0149],[WT0150],
149
			[WT0201],[WT0202],[WT0203],[WT0204],[WT0205],[WT0206],[WT0207],[WT0208],[WT0209],[WT0210],[WT0211],[WT0212],[WT0213],[WT0214],[WT0215],[WT0216],[WT0217],[WT0218],[WT0219],[WT0220],[WT0221],[WT0222],[WT0223],[WT0224],[WT0225],[WT0226],[WT0227],[WT0228],[WT0229],[WT0230],[WT0231],[WT0232],[WT0233],[WT0234],[WT0235],[WT0236],[WT0237],[WT0238],[WT0239],[WT0240],[WT0241],[WT0242],[WT0243],[WT0244],[WT0245],[WT0246],[WT0247],[WT0248],[WT0249],[WT0250],
150
			[WT0301],[WT0302],[WT0303],[WT0304],[WT0305],[WT0306],[WT0307],[WT0308],[WT0309],[WT0310],[WT0311],[WT0312],[WT0313],[WT0314],[WT0315],[WT0316],[WT0317],[WT0318],[WT0319],[WT0320],[WT0321],[WT0322],[WT0323],[WT0324],[WT0325],[WT0326],[WT0327],[WT0328],[WT0329],[WT0330],[WT0331],[WT0332],[WT0333],[WT0334],[WT0335],[WT0336],[WT0337],[WT0338],[WT0339],[WT0340],[WT0341],[WT0342],[WT0343],[WT0344],[WT0345],[WT0346],[WT0347],[WT0348],[WT0349],[WT0350],
151
			[WT0401],[WT0402],[WT0403],[WT0404],[WT0405],[WT0406],[WT0407],[WT0408],[WT0409],[WT0410],[WT0411],[WT0412],[WT0413],[WT0414],[WT0415],[WT0416],[WT0417],[WT0418],[WT0419],[WT0420],[WT0421],[WT0422],[WT0423],[WT0424],[WT0425],[WT0426],[WT0427],[WT0428],[WT0429],[WT0430],[WT0431],[WT0432],[WT0433],[WT0434],[WT0435],[WT0436],[WT0437],[WT0438],[WT0439],[WT0440],[WT0441],[WT0442],[WT0443],[WT0444],[WT0445],[WT0446],[WT0447],[WT0448],[WT0449],[WT0450]))AS PivotTable) as a cross join 
152
	
153
	(
154
		
155
SELECT ADF01	,
156
ADF02	,
157
ADF03	,
158
ADF04	,
159
ADF05	,
160
ADF06	,
161
ADF07	,
162
ADF08	,
163
ADF09	,
164
ADF10	,
165
ADF11	,
166
ADF12	,
167
ADF13	,
168
ADF14	,
169
ADF15	,
170
ADF16	,
171
ADF17	,
172
ADF18	,
173
ADF19	,
174
ADF20	,
175
ADF21	,
176
ADF22	,
177
ADF23	,
178
ADF24	,
179
ADF25	,
180
ADF26	,
181
ADF27	,
182
ADF28	,
183
ADF29	,
184
ADF30	,
185
ADF31	,
186
ADF32	,
187
ADF33	,
188
ADF34	,
189
ADF53	,
190
ADF54	
191

    
192

    
193
FROM
194
(select top 1000 'ADF' + right('00' + convert(varchar,ROW_NUMBER() OVER (ORDER BY col_no)),2) as column_id ,case is_visible when 1 then descr else '' end as description from dbo.hr_cust_py_report_additional where landscape = @landscape and rep_id = 'WT03R' and start_date <= @payPeriodEndDate and end_date >= @payPeriodEndDate order by col_no
195
) AS SourceTable
196
PIVOT
197
(
198
max(description)
199
FOR column_id IN (
200
ADF01	,
201
ADF02	,
202
ADF03	,
203
ADF04	,
204
ADF05	,
205
ADF06	,
206
ADF07	,
207
ADF08	,
208
ADF09	,
209
ADF10	,
210
ADF11	,
211
ADF12	,
212
ADF13	,
213
ADF14	,
214
ADF15	,
215
ADF16	,
216
ADF17	,
217
ADF18	,
218
ADF19	,
219
ADF20	,
220
ADF21	,
221
ADF22	,
222
ADF23	,
223
ADF24	,
224
ADF25	,
225
ADF26	,
226
ADF27	,
227
ADF28	,
228
ADF29	,
229
ADF30	,
230
ADF31	,
231
ADF32	,
232
ADF33	,
233
ADF34	,
234
ADF53	,
235
ADF54		
236

    
237
	
238

    
239

    
240
)
241
) AS PivotTable
242
	
243
	)	as b	 
244
  
245
  
246
  
247

    
248

    
249

    
(6-6/10)