DBA Data[Home] [Help]

PACKAGE BODY: APPS.PJI_FM_PLAN_MAINT_PVT

Source


1 PACKAGE BODY PJI_FM_PLAN_MAINT_PVT AS
2 /* $Header: PJIPP02B.pls 120.52.12010000.3 2008/11/13 08:33:18 rthumma ship $ */
3 
4 
5   g_package_name VARCHAR2(100) := 'PJI_FM_PLAN_MAINT_PVT';
6 
7   g_prorating_format            VARCHAR2(30) := 'S';
8                                 -- S Start date, E End date, D (daily) Period.
9                                 -- Based on plan version.
10 
11   g_currency_conversion_rule   VARCHAR2(30) := 'S';
12                                 -- S Start date, E End date.
13                                 -- Based on plan version.
14 
15   g_global_curr_1_enabled       VARCHAR2(30) := 'T';
16   g_global_curr_2_enabled       VARCHAR2(30) := 'T';
17 
18   g_global1_currency_code       VARCHAR2(30) := 'USD';
19   g_global2_currency_code       VARCHAR2(30) := 'CAD';
20 
21   g_global1_currency_mau        NUMBER;
22   g_global2_currency_mau        NUMBER;
23 
24   g_labor_mau                   NUMBER := 0.01;
25 
26   g_ent_start_period_id         NUMBER        := NULL;
27   g_ent_start_period_name       VARCHAR2(100) := NULL;
28   g_ent_start_date              date := NULL;
29   g_ent_END_date                date := NULL;
30   g_global_start_date           date := NULL;
31 
32   g_global_start_J              NUMBER := NULL;
33   g_ent_start_J                 NUMBER := NULL;
34   g_ent_END_J                   NUMBER := NULL;
35 
36   g_worker_id                    NUMBER := 1; -- NULL;
37   g_default_prg_level           NUMBER := 0;
38 
39   g_people_resclass_code        VARCHAR2(20) := 'PEOPLE';
40   g_equip_resclass_code         VARCHAR2(20) := 'EQUIPMENT';
41 
42   g_yes                         VARCHAR2(1) := 'Y';
43   g_no                          VARCHAR2(1) := 'N';  -- Same as g_nontimeph_str.
44   g_pa_cal_str                  VARCHAR2(1) := 'P';
45   g_gl_cal_str                  VARCHAR2(1) := 'G';
46   -- 'N'ntimeph_str               VARCHAR2(1) := 'N';
47   g_ent_cal_str                 VARCHAR2(1) := 'E';
48   g_all                         VARCHAR2(1) := 'A';
49   g_start_str                   VARCHAR2(1) := 'S';
50   g_end_str                     VARCHAR2(1) := 'E';
51   g_lowest_level                VARCHAR2(1) := 'L';
52   g_top_level                   VARCHAR2(1) := 'T';
53   g_rolled_up                   VARCHAR2(1) := 'R';
54 
55   g_ntp_period_name             VARCHAR2(10) := 'XXX';
56 
57 -- g_full                        VARCHAR2(4) := 'FULL';
58 -- g_incr                        VARCHAR2(4) := 'INCREMENTAL';
59 g_cb_plans	 constant NUMBER := 2;
60 g_co_plans	 constant NUMBER := 4;
61 g_lp_plans	 constant NUMBER := 8;
62 g_wk_plans	 constant NUMBER := 16;
63 g_latest_plans	 constant NUMBER := 30;
64 g_all_plans	 constant NUMBER := 62;
65 
66 
67   -- 'P'            VARCHAR2(15) := 'P';
68   -- 'G'            VARCHAR2(15) := 'G';
69   -- 'N'       VARCHAR2(15) := 'N';
70   -- 'R'         VARCHAR2(15) := 'R';
71   -- 'X'       VARCHAR2(15) := 'X';
72 
73 
74 ------------------------------------------------------------------
75 ------------------------------------------------------------------
76 --              Helper  Apis Specification                      --
77 ------------------------------------------------------------------
78 ------------------------------------------------------------------
79 
80 PROCEDURE PRINT_TIME (p_tag IN VARCHAR2);
81 
82 PROCEDURE PRINT_NUM_WBSRLPRCDS_INPJP1;
83 
84 PROCEDURE INSERT_NTP_CAL_RECORD ( x_max_project_id OUT NOCOPY NUMBER );
85 
86 PROCEDURE DELETE_NTP_CAL_RECORD ( p_max_project_id IN NUMBER );
87 
88 PROCEDURE UPDATE_LOCKS (
89   p_context         IN VARCHAR2
90 , p_update_mode     IN VARCHAR2
91 , x_return_status   OUT NOCOPY VARCHAR2
92 , x_msg_code        OUT NOCOPY VARCHAR2 );
93 
94 --
95 -- Populate ver3 for summarization programs.
96 --
97 PROCEDURE VALIDATE_SET_PR_PARAMS(
98   p_rbs_version_id  IN NUMBER
99 , p_plan_type_id    IN NUMBER
100 , p_context         IN VARCHAR2      -- Valid values are 'RBS' or 'PLANTYPE'.
101 , x_num_rows        OUT NOCOPY NUMBER
102 , x_return_status   OUT NOCOPY  VARCHAR2
103 , x_msg_code        OUT NOCOPY  VARCHAR2 ) IS
104 
105   l_plan_type_id      NUMBER := NULL;
106   l_rbs_version_id    NUMBER := NULL;
107   l_rbs_header_id    NUMBER := NULL; --Added for bug#5728852
108   -- l_count  NUMBER;
109   l_return_status  VARCHAR2(1) := NULL;
110   l_fp_version_ids SYSTEM.pa_num_tbl_type := pji_empty_num_tbl ;
111   l_msg_code       VARCHAR2(10) := NULL;
112   l_refresh_code NUMBER;		 --  Bug#5099574
113   l_process           varchar2(30);		 --  Bug#5099574
114 
115   l_actual_ver NUMBER :=  -1;
116   l_cb_ver NUMBER := -3;
117   l_co_ver NUMBER := -4;
118   l_num_rows NUMBER :=0;
119     l_workplan_type_id number;
120 
121 BEGIN
122 
123   print_time('VALIDATE_SET_PR_PARAMS : Begin.. ');
124 
125   PJI_PJP_FP_CURR_WRAP.INIT_ERR_STACK
126   ( p_package_name   => g_package_name
127   , x_return_status  => x_return_status );
128 
129   x_num_rows := 0;
130 
131   g_worker_id  := PJI_PJP_FP_CURR_WRAP.GET_WORKER_ID;
132   print_time ( ' Worker id is.. ' || g_worker_id );
133 
134   print_time(' VALIDATE_SET_PR_PARAMS : plan type id is ' || p_plan_type_id );
135   print_time(' p_context is ' || p_context || ' and rbs ver id is ' || p_rbs_version_id );
136 
137   IF (p_context NOT IN ('RBS' , 'PLANTYPE', 'INCREMENTAL', 'FULL')) THEN
138     print_time(' Invalid p_context.. exitting. ' );
139     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
140     RETURN;
141   END IF;
142 
143 /* Added for bug#5728852 - START */
144   l_process := PJI_PJP_SUM_MAIN.g_process || to_char(g_worker_id);
145   l_rbs_header_id := PJI_PROCESS_UTIL.GET_PROCESS_PARAMETER(l_process,'RBS_HEADER_ID');
146 /* Added for bug#5728852 - END */
147 
148   IF (p_context = 'RBS' AND p_rbs_version_id IS NULL) THEN
149     print_time('Context is RBS and rbs version id is NULL. Pl. provide a rbs version id. Exitting.' );
150     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
151     RETURN;
152   END IF;
153 
154   IF (p_context = 'RBS') THEN
155 
156     l_plan_type_id  := NULL;
157     l_rbs_version_id  := p_rbs_version_id;
158 
159 
160     INSERT INTO PJI_FM_EXTR_PLNVER4
161     (
162       WORKER_ID                ,
163       PROJECT_ID               ,
164       PLAN_VERSION_ID          ,
165       WBS_STRUCT_VERSION_ID    ,
166       RBS_STRUCT_VERSION_ID    ,
167       PLAN_TYPE_CODE           ,
168       PLAN_TYPE_ID             ,
169       TIME_PHASED_TYPE_CODE    ,
170       TIME_DANGLING_FLAG       ,
171       RATE_DANGLING_FLAG       ,
172       PROJECT_TYPE_CLASS       ,
173       WP_FLAG                  ,
174 	CURRENT_FLAG             ,
175 	ORIGINAL_FLAG            ,
176 	CURRENT_ORIGINAL_FLAG    ,
177 	BASELINED_FLAG        	 ,
178 	SECONDARY_RBS_FLAG       ,
179       LP_FLAG
180     )
181       SELECT
182         DISTINCT
183             g_worker_id worker_id
184           , project_id
185           , plan_version_id
186           , wbs_struct_version_id
187           , rbs_struct_version_id
188           , plan_type_code
189           , plan_type_id
190           , time_phased_type_code
191           , NULL time_dangling_flag
192           , NULL rate_dangling_flag
193           , NULL PROJECT_TYPE_CLASS
194           , is_wp_flag
195           , current_flag
196           , original_flag
197           , current_original_flag
198           , baselined_flag
199           , SECONDARY_RBS_FLAG
200           , lp_flag
201      FROM
202      (
203 	 SELECT   -- RBS Push 1: This select for all primary RBSes that change from rbs version A to B.
204             /*+ ORDERED */
205             bv.project_id                      project_id
206           , bv.budget_version_id               plan_version_id
207           , DECODE ( NVL(bv.wp_version_flag, 'N')
208 		           , 'Y', bv.project_structure_version_id
209 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
210 				   )                           wbs_struct_version_id
211           , fpo.rbs_version_id                 rbs_struct_version_id -- extract for this rbs version id
212 --          , to_char(fpo.fin_plan_type_id)      plan_type_code
213            , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
214           , fpo.fin_plan_type_id               plan_type_id
215           , DECODE(bv.version_type
216                       , 'ALL',     fpo.all_time_phased_code
217                       , 'COST',    fpo.cost_time_phased_code
218                       , 'REVENUE', fpo.revenue_time_phased_code
219                      )                       time_phased_type_code
220 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
221 		  , bv.current_flag                  current_flag
222 		  , bv.original_flag                 original_flag
223 		  , bv.current_original_flag         current_original_flag
224 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
225 		  , 'N'  		                     SECONDARY_RBS_FLAG
226 		  , DECODE( NVL(bv.wp_version_flag, 'N')
227 		          , 'Y'
228 				  , DECODE(bv.project_structure_version_id
229 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
230 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
231 						 , 'Y'
232 						 , 'N')
233 				  , 'N'
234 				  ) lp_flag
235       FROM
236         pji_pjp_proj_batch_map map,
237         pji_pjp_rbs_header     rhd,
238         pa_budget_versions     bv,
239         pa_proj_fp_options     fpo,
240         pji_pa_proj_events_log pel,
241         pa_projects_all        ppa,
242 	pa_rbs_versions_b	rvb --Added for bug#5728852
243       WHERE 1=1
244           AND ppa.project_id = map.project_id
245           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
246           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
247           AND fpo.project_id = map.project_id
248           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
249           AND bv.budget_version_id = fpo.fin_plan_version_id
250           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
251           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
252           AND rhd.rbs_version_id = rvb.rbs_version_id --Added for bug#5728852
253 	  AND rvb.rbs_header_id = l_rbs_header_id --Added for bug#5728852
254 	    AND rhd.plan_version_id = bv.budget_version_id
255           AND rhd.project_id = map.project_id
256           AND pel.WORKER_ID = g_worker_id
257 	    AND pel.event_type = 'RBS_PUSH'
258 	    AND pel.event_object = fpo.rbs_version_id -- event object is the new primary rbs_version.
259   	    AND to_number(pel.attribute2) = p_rbs_version_id -- attribute2 is the old rbs_version.
260           AND pel.ATTRIBUTE19 = map.PROJECT_ID
261           AND map.project_id = bv.project_id
262           AND map.worker_id = g_worker_id
263           -- AND p_context = 'RBS'
264       UNION ALL
265       SELECT    -- RBS Push 2: This select for all secondary (reporting) RBSes that are changing from A to B.
266             /*+ ORDERED */
267             bv.project_id                      project_id
268           , bv.budget_version_id               plan_version_id
269           , DECODE ( NVL(bv.wp_version_flag, 'N')
270 		           , 'Y', bv.project_structure_version_id
271 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
272 				   )                           wbs_struct_version_id
273           , rpa.rbs_version_id                 rbs_struct_version_id
274 --          , to_char(fpo.fin_plan_type_id)      plan_type_code
275            , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
276           , fpo.fin_plan_type_id               plan_type_id
277           , DECODE(bv.version_type
278                       , 'ALL',     fpo.all_time_phased_code
279                       , 'COST',    fpo.cost_time_phased_code
280                       , 'REVENUE', fpo.revenue_time_phased_code
281                      )                       time_phased_type_code
282 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
283 		  , bv.current_flag                  current_flag
284 		  , bv.original_flag                 original_flag
285 		  , bv.current_original_flag         current_original_flag
286 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
287 		  , 'Y'  		                     SECONDARY_RBS_FLAG
288 		  , DECODE( NVL(bv.wp_version_flag, 'N')
289 		          , 'Y'
290 				  , DECODE(bv.project_structure_version_id
291 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
292 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
293 						 , 'Y'
294 						 , 'N')
295 				  , 'N'
296 				  ) lp_flag
297       FROM
298         PJI_PJP_PROJ_BATCH_MAP map,
299         PJI_PJP_RBS_HEADER     rhd,
300         PA_BUDGET_VERSIONS     bv,
301         PA_PROJ_FP_OPTIONS     fpo,
302         PJI_PA_PROJ_EVENTS_LOG pel,
303         PA_RBS_PRJ_ASSIGNMENTS rpa,
304         PA_PROJECTS_ALL        ppa,
305 	PA_RBS_VERSIONS_B	rvb --Added for bug#5728852
306       WHERE 1=1
307           AND ppa.project_id = map.project_id
308           AND bv.project_id = map.project_id
309           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
310           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
311           AND fpo.project_id = map.project_id
312           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
313           AND bv.budget_version_id = fpo.fin_plan_version_id
314           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
315           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
316           AND bv.budget_status_code = 'B'
317           AND NVL(bv.wp_version_flag, 'N') = 'N'
318           AND rhd.project_id = map.project_id
319 	    AND rhd.plan_version_id = bv.budget_version_id
320           AND rhd.rbs_version_id = rvb.rbs_version_id --Added for bug#5728852
321 	  AND rvb.rbs_header_id = l_rbs_header_id --Added for bug#5728852
322           AND rpa.assignment_status = 'ACTIVE'
323 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
324 	    AND rpa.project_id = map.project_id
325           AND pel.WORKER_ID = g_worker_id
326   	    AND to_number(pel.attribute2) = p_rbs_version_id
327 	    AND pel.event_type = 'RBS_PUSH'  -- When RBS version is 'frozen' from RBS definition screen.
328 	    AND pel.event_object = rpa.rbs_version_id
329           AND pel.ATTRIBUTE19 = map.PROJECT_ID
330           AND map.project_id = bv.project_id
331           -- AND map.PROJECT_ACTIVE_FLAG = 'Y'
332           AND map.worker_id = g_worker_id
333           -- AND p_context = 'RBS'
334        UNION ALL
335       SELECT
336         /*+ ORDERED */
337             bv.project_id                      project_id
338           , pln_ver.pvi                        plan_version_id
339           , DECODE ( NVL(bv.wp_version_flag, 'N')
340 		           , 'Y', bv.project_structure_version_id
341 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
342 				   )                           wbs_struct_version_id
343           , rpa.rbs_version_id                 rbs_struct_version_id
344 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
345           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
346           , fpo.fin_plan_type_id               plan_type_id
347           , DECODE(bv.version_type
348                       , 'ALL',     fpo.all_time_phased_code
349                       , 'COST',    fpo.cost_time_phased_code
350                       , 'REVENUE', fpo.revenue_time_phased_code
351                      )                       time_phased_type_code
352 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
353 		  , DECODE(pln_ver.pvi, -3, 'Y', -4, 'N') current_flag
354 		  , 'X'                          original_flag
355 		  , DECODE(pln_ver.pvi, -3, 'N', -4, 'Y') current_original_flag
356 		  , DECODE(pln_ver.pvi, -3, 'Y', -4, 'N') baselined_flag
357 		  , 'N'  		                     SECONDARY_RBS_FLAG
358 		  , 'Y'                                lp_flag
359       FROM
360         PJI_PJP_PROJ_BATCH_MAP map,
361         PJI_PJP_RBS_HEADER     rhd,
362         PA_BUDGET_VERSIONS     bv,
363         PA_PROJ_FP_OPTIONS     fpo,
364         PJI_PA_PROJ_EVENTS_LOG pel,
365         PA_RBS_PRJ_ASSIGNMENTS rpa,
366         PA_PROJECTS_ALL        ppa,
367 	PA_RBS_VERSIONS_B	rvb,
368         (
369         SELECT -3 pvi FROM dual UNION ALL
370         SELECT -4 pvi FROM dual
371         ) pln_ver
372       WHERE 1=1
373           AND ppa.project_id = bv.project_id
374           AND bv.version_type IS NOT NULL -- COST, REVENUE, etc. Should not be null.
375           AND bv.fin_plan_type_id IS NOT NULL -- Old budgets model data is not picked up with this condition.
376           AND fpo.project_id = bv.project_id
377           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
378           AND bv.budget_version_id = fpo.fin_plan_version_id
379           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
380           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
381           AND bv.budget_status_code = 'B'
382           AND NVL(bv.wp_version_flag, 'N') = 'N'
383           AND pel.WORKER_ID = g_worker_id
384           AND rhd.PROJECT_ID = map.PROJECT_ID
385   	    AND rhd.rbs_version_id = TO_NUMBER(pel.attribute2)
386 	    AND rhd.plan_version_id = bv.budget_version_id
387           AND rhd.rbs_version_id = rvb.rbs_version_id --Added for bug#5728852
388 	  AND rvb.rbs_header_id = l_rbs_header_id --Added for bug#5728852
389           AND rpa.assignment_status = 'ACTIVE'
390 	    -- AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
391 	    AND rpa.project_id = bv.project_id
392 	    AND pel.event_type = 'RBS_PUSH'  -- When RBS version is 'frozen' from RBS definition screen.
393 	    AND pel.event_object = rpa.rbs_version_id
394           AND pel.ATTRIBUTE19 = map.PROJECT_ID
395           AND map.project_id = bv.project_id
396           AND map.worker_id = g_worker_id
397           -- AND p_context = 'RBS'
398         ) ;
399 
400     l_num_rows := SQL%ROWCOUNT;
401 
402   print_time ( ' Number of plans with this rbs version id are.. x_num_rows = ' || l_num_rows);
403   ELSIF (p_context = 'PLANTYPE') THEN
404 
405     l_plan_type_id  := p_plan_type_id;
406     l_rbs_version_id  := NULL;
407     l_process := PJI_PJP_SUM_MAIN.g_process || to_char(g_worker_id);									  --  Bug#5099574
408     l_refresh_code     :=  PJI_PROCESS_UTIL.GET_PROCESS_PARAMETER (l_process, 'REFRESH_CODE');    --  Bug#5099574
409 
410     			--  Bug#	5208322  :  the workplan plan_type_id is stored in  l_workplan_type_id
411 			begin
412 			SELECT fin_plan_type_id into l_workplan_type_id
413 			FROM pa_fin_plan_types_b
414 			WHERE use_for_workplan_flag = 'Y';
415 			exception
416 			when no_data_found then
417 			l_workplan_type_id := NULL;
418 			end;
419 
420 
421 
422     INSERT INTO PJI_FM_EXTR_PLNVER4
423     (
424       WORKER_ID                ,
425       PROJECT_ID               ,
426       PLAN_VERSION_ID          ,
427       WBS_STRUCT_VERSION_ID    ,
428       RBS_STRUCT_VERSION_ID    ,
429       PLAN_TYPE_CODE           ,
430       PLAN_TYPE_ID             ,
431       TIME_PHASED_TYPE_CODE    ,
432       TIME_DANGLING_FLAG       ,
433       RATE_DANGLING_FLAG       ,
434       PROJECT_TYPE_CLASS       ,
435       WP_FLAG                  ,
436 	CURRENT_FLAG             ,
437 	ORIGINAL_FLAG            ,
438 	CURRENT_ORIGINAL_FLAG    ,
439 	BASELINED_FLAG        	 ,
440 	SECONDARY_RBS_FLAG       ,
441       LP_FLAG
442     )
443       SELECT
444             DISTINCT
445             g_worker_id worker_id
446           , project_id
447           , plan_version_id
448           , wbs_struct_version_id
449           , rbs_struct_version_id
450           , plan_type_code
451           , plan_type_id
452           , time_phased_type_code
453           , NULL time_dangling_flag
454           , NULL rate_dangling_flag
455           , NULL PROJECT_TYPE_CLASS
456           , is_wp_flag
457           , current_flag
458           , original_flag
459           , current_original_flag
460           , baselined_flag
461           , SECONDARY_RBS_FLAG
462           , lp_flag
463      FROM
464      (
465 	 SELECT   -- Partial refresh 1: This select for all plan versions with primary rbs and plan type id not null.
466 	     /*+ ORDERED */
467             bv.project_id                      project_id
468           , bv.budget_version_id               plan_version_id
469           , DECODE ( NVL(bv.wp_version_flag, 'N')
470 		           , 'Y', bv.project_structure_version_id
471 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
472 				   )                           wbs_struct_version_id
473           , fpo.rbs_version_id                 rbs_struct_version_id -- extract for this rbs version id
474 --          , to_char(fpo.fin_plan_type_id)      plan_type_code
475           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
476           , fpo.fin_plan_type_id               plan_type_id
477           , DECODE(bv.version_type
478                       , 'ALL',     fpo.all_time_phased_code
479                       , 'COST',    fpo.cost_time_phased_code
480                       , 'REVENUE', fpo.revenue_time_phased_code
481                      )                       time_phased_type_code
482 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
483 		  , bv.current_flag                  current_flag
484 		  , bv.original_flag                 original_flag
485 		  , bv.current_original_flag         current_original_flag
486 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
487 		  , 'N'  		                     SECONDARY_RBS_FLAG
488 		  , DECODE( NVL(bv.wp_version_flag, 'N')
489 		          , 'Y'
490 				  , DECODE(bv.project_structure_version_id
491 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
492 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
493 						 , 'Y'
494 						 , 'N')
495 				  , 'N'
496 				  ) lp_flag
497     FROM
498        pji_pjp_proj_batch_map map
499      , pa_projects_all  ppa
500      ,  pa_budget_versions bv
501      , pa_proj_fp_options  fpo
502        WHERE 1=1
503           AND ppa.project_id = bv.project_id
504           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
505           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
506           AND fpo.project_id = bv.project_id
507           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
508           AND bv.budget_version_id = fpo.fin_plan_version_id
509           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
510           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
511           AND map.project_id = bv.project_id
512           AND map.worker_id = g_worker_id
513           AND bv.fin_plan_type_id = nvl(l_plan_type_id,bv.fin_plan_type_id) and
514 			(
515 			    'Y' IN		-- Bug#5099574  Pull Reversals for CB / CO if refresh_code < 62 . Else pull for all plans ids >0 if refresh_code>=62
516 				(
517 						Select decode(
518 						  bitand(l_refresh_code,g_all_plans),g_all_plans,'Y',
519 							  decode(  bitand(l_refresh_code,g_cb_plans),g_cb_plans,
520 							  decode(decode(bv.baselined_date, NULL, 'N', 'Y')||bv.current_flag,'YY', 'Y', 'N'),'X')) from dual
521 					 UNION  ALL
522 						Select decode(
523 						  bitand(l_refresh_code,g_all_plans),g_all_plans,'Y',
524 						  decode( bitand(l_refresh_code,g_co_plans),g_co_plans,bv.current_original_flag,'X')) from dual
525 				)
526 			OR		-- Bug#5099574  Pull Reversals for Fin plan Working Versions when l_refresh_code=16,30. ignore if  l_refresh_code>=62
527 				(
528 				 bv.BUDGET_STATUS_CODE in ('W','S')	and
529 				 fpo.FIN_PLAN_TYPE_ID <> l_workplan_type_id 			and
530 				 DECODE(BITAND(l_refresh_code,g_all_plans),g_all_plans,'N',DECODE(BITAND(l_refresh_code,g_wk_plans),g_wk_plans,'Y','N'))='Y'
531 				 )
532 			OR 	 --Pull Reversals for Work plan Working Versions / LPub Vers / Baselined Versions when l_refresh_code=2,8,16,30. ignore if  l_refresh_code>=62
533 			EXISTS	 ( select 1 from PA_PROJ_ELEM_VER_STRUCTURE	ppevs where
534 				 bv.FIN_PLAN_TYPE_ID					=l_workplan_type_id				and
535 				 bv.PROJECT_STRUCTURE_VERSION_ID  = ppevs.ELEMENT_VERSION_ID	and
536 					(
537 					decode(BITAND(l_refresh_code,g_all_plans),g_all_plans,'N',
538 												decode(BITAND(l_refresh_code,g_lp_plans),g_lp_plans,LATEST_EFF_PUBLISHED_FLAG,'N'))='Y'
539 					or
540 					decode(BITAND(l_refresh_code,g_all_plans),g_all_plans,'N',
541 												decode(BITAND(l_refresh_code,g_wk_plans),g_wk_plans,STATUS_CODE,'N'))='STRUCTURE_WORKING'
542 					or
543     					decode(BITAND(l_refresh_code,g_all_plans ),g_all_plans ,'N',
544 											 	decode(BITAND(l_refresh_code,g_cb_plans),g_cb_plans,NVL2(CURRENT_BASELINE_DATE,'Y','N'),'N')) ='Y'
545 
546 					)
547 				)
548 			)
549        UNION ALL
550        SELECT    -- Partial refresh 2: This select for all secondary (reporting) RBSes .
551          /*+ ORDERED */
552             bv.project_id                      project_id
553           , bv.budget_version_id               plan_version_id
554           , DECODE ( NVL(bv.wp_version_flag, 'N')
555 		           , 'Y', bv.project_structure_version_id
556 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
557 				   )                           wbs_struct_version_id
558           , rpa.rbs_version_id                 rbs_struct_version_id
559           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
560           , fpo.fin_plan_type_id               plan_type_id
561           , DECODE(bv.version_type
562                       , 'ALL',     fpo.all_time_phased_code
563                       , 'COST',    fpo.cost_time_phased_code
564                       , 'REVENUE', fpo.revenue_time_phased_code
565                      )                       time_phased_type_code
566 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
567 		  , bv.current_flag                  current_flag
568 		  , bv.original_flag                 original_flag
569 		  , bv.current_original_flag         current_original_flag
570 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
571 		  , 'Y'  		                     SECONDARY_RBS_FLAG
572 		  , DECODE( NVL(bv.wp_version_flag, 'N')
573 		          , 'Y'
574 				  , DECODE(bv.project_structure_version_id
575 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
576 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
577 						 , 'Y'
578 						 , 'N')
579 				  , 'N'
580 				  ) lp_flag
581       FROM
582           pji_pjp_proj_batch_map map
583 	 , pa_projects_all  ppa -- @pjdev115    ppa
584          , pa_budget_versions bv -- @pjdev115  bv
585          , pa_proj_fp_options  fpo -- @pjdev115  fpo
586          , PA_RBS_PRJ_ASSIGNMENTS rpa
587       WHERE 1=1
588           AND ppa.project_id = bv.project_id
589           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
590           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
591           AND fpo.project_id = bv.project_id
592           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
593           AND bv.budget_version_id = fpo.fin_plan_version_id
594           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
595           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
596           AND bv.budget_status_code = 'B'
597           AND NVL(bv.wp_version_flag, 'N') = 'N'
598           AND rpa.assignment_status = 'ACTIVE'
599 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
600 	    AND rpa.project_id = bv.project_id
601           AND map.project_id = bv.project_id
602           AND map.worker_id = g_worker_id
603           AND bv.fin_plan_type_id = nvl(l_plan_type_id,bv.fin_plan_type_id) and
604 	   	 (
605 			 'Y' IN		-- Bug#5099574  Pull Reversals for CB / CO if refresh_code < 62 . Else pull for all plans ids >0 if refresh_code>=62
606 			(
607 							Select decode(
608 							  bitand(l_refresh_code,g_all_plans),g_all_plans,'Y',
609 								  decode(  bitand(l_refresh_code,g_cb_plans),g_cb_plans,
610 								  decode(decode(bv.baselined_date, NULL, 'N', 'Y')||bv.current_flag,'YY', 'Y', 'N'),'X')) from dual
611 						 UNION  ALL
612 							Select decode(
613 							  bitand(l_refresh_code,g_all_plans),g_all_plans,'Y',
614 							  decode( bitand(l_refresh_code,g_co_plans),g_co_plans,bv.current_original_flag,'X')) from dual
615 			)
616 		)
617         ) ;
618 
619     l_num_rows := SQL%ROWCOUNT;
620 
621 
622 
623 
624   ELSIF (p_context IN ( 'INCREMENTAL', 'FULL')) THEN -- Workplans only.
625 
626 
627     INSERT INTO PJI_FM_EXTR_PLNVER4
628     (
629       WORKER_ID                ,
630       PROJECT_ID               ,
631       PLAN_VERSION_ID          ,
632       WBS_STRUCT_VERSION_ID    ,
633       RBS_STRUCT_VERSION_ID    ,
634       PLAN_TYPE_CODE           ,
635       PLAN_TYPE_ID             ,
636       TIME_PHASED_TYPE_CODE    ,
637       TIME_DANGLING_FLAG       ,
638       RATE_DANGLING_FLAG       ,
639       PROJECT_TYPE_CLASS       ,
640       WP_FLAG                  ,
641 	CURRENT_FLAG             ,
642 	ORIGINAL_FLAG            ,
643 	CURRENT_ORIGINAL_FLAG    ,
644 	BASELINED_FLAG        	 ,
645 	SECONDARY_RBS_FLAG       ,
646       LP_FLAG
647     )
648       SELECT
649         DISTINCT
650             g_worker_id worker_id
651           , project_id
652           , plan_version_id
653           , wbs_struct_version_id
654           , rbs_struct_version_id
655           , plan_type_code
656           , plan_type_id
657           , time_phased_type_code
658           , NULL time_dangling_flag
659           , NULL rate_dangling_flag
660           , NULL PROJECT_TYPE_CLASS
661           , is_wp_flag
662           , current_flag
663           , original_flag
664           , current_original_flag
665           , baselined_flag
666           , SECONDARY_RBS_FLAG
667           , lp_flag
668      FROM
669      (
670 	 SELECT   -- Incr 1 , Full 1: This select for all plan versions with primary rbs and plan type id not null.
671 	   /*+ ORDERED */
672             pbv.project_id                      project_id
673           , pbv.budget_version_id               plan_version_id
674           , head.WBS_VERSION_ID               wbs_struct_version_id
675           , fpo.rbs_version_id                 rbs_struct_version_id -- extract for this rbs version id
676 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
677           , DECODE (pbv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
678           , fpo.fin_plan_type_id               plan_type_id
679           , DECODE(pbv.version_type
680                       , 'ALL',     fpo.all_time_phased_code
681                       , 'COST',    fpo.cost_time_phased_code
682                       , 'REVENUE', fpo.revenue_time_phased_code
683                      )                       time_phased_type_code
684 	    , NVL(pbv.wp_version_flag, 'N') is_wp_flag
685 	    , pbv.current_flag                  current_flag
686 	    , pbv.original_flag                 original_flag
687 	    , pbv.current_original_flag         current_original_flag
688 	    , DECODE(Pbv.baselined_date, NULL, 'N', 'Y') baselined_flag
689 	    , 'N'  		                     SECONDARY_RBS_FLAG
690 	    , 'Y'                              lp_flag
691     FROM
692             pji_pjp_proj_batch_map map,
693 	    PA_PROJECTS_ALL ppa,
694 	    PA_BUDGET_VERSIONS pbv,
695 	    pji_pjp_wbs_header head,
696             PA_PROJ_ELEM_VER_STRUCTURE ppevs,
697             PA_PROJ_FP_OPTIONS fpo
698     WHERE
699 	      -- map.PROJECT_ACTIVE_FLAG         = 'Y'                    AND
700 	      map.PROJECT_ID                  = head.PROJECT_ID           AND
701 	      head.WP_FLAG                    = 'Y'                       AND
702 	      head.WBS_VERSION_ID             = ppevs.ELEMENT_VERSION_ID  AND
703 	      ppevs.STATUS_CODE               = 'STRUCTURE_PUBLISHED'     AND
704 	      ppevs.LATEST_EFF_PUBLISHED_FLAG = 'Y'                       AND
705             head.project_id                 = ppevs.project_id          AND
706             map.PROJECT_ID                  = ppa.PROJECT_ID            AND
707             ppa.STRUCTURE_SHARING_CODE      = 'SHARE_FULL'              AND
708             pbv.BUDGET_VERSION_ID           = head.PLAN_VERSION_ID      AND
709             head.PLAN_VERSION_ID            = fpo.FIN_PLAN_VERSION_ID   AND
710             ppa.project_id                  = pbv.project_id            AND
711             pbv.version_type                IS NOT NULL                 AND
712             pbv.fin_plan_type_id            IS NOT NULL                 AND
713             fpo.project_id                  = pbv.project_id            AND
714             pbv.fin_plan_type_id            = fpo.fin_plan_type_id      AND
715             pbv.budget_version_id           = fpo.fin_plan_version_id   AND
716             fpo.fin_plan_option_level_code  = 'PLAN_VERSION'            AND
717             pbv.version_type                IN ('ALL','COST','REVENUE') AND
718             map.worker_id                   = g_worker_id               AND
719             p_context                       IN ('INCREMENTAL', 'FULL')
720         ) ;
721 
722     l_num_rows := SQL%ROWCOUNT;
723 
724     print_time ( 'Context of summarization p_context is : ' || p_context );
725     print_time ( 'GET_ACTUALS_SUMM : after insert to ver3 , # rows is  ' || l_num_rows );
726 
727   END IF;
728 
729 
730 
731   IF p_context = 'PLANTYPE' then
732     select decode(l_plan_type_id,NULL,decode(bitand (l_refresh_code,1),1,-1,-999),-999),
733     decode (bitand (l_refresh_code,2),2,-3,-999) ,
734     decode (bitand (l_refresh_code,4),4,-4,-999)
735     into l_actual_ver,l_cb_ver,l_co_ver from dual;
736     ELSE
737     l_actual_ver := -1;
738     l_cb_ver     := -3;
739     l_co_ver     := -4;
740   END IF;
741 
742     INSERT INTO PJI_FM_EXTR_PLNVER4
743     (
744       WORKER_ID                ,
745       PROJECT_ID               ,
746       PLAN_VERSION_ID          ,
747       WBS_STRUCT_VERSION_ID    ,
748       RBS_STRUCT_VERSION_ID    ,
749       PLAN_TYPE_CODE           ,
750       PLAN_TYPE_ID             ,
751       TIME_PHASED_TYPE_CODE    ,
752       TIME_DANGLING_FLAG       ,
753       RATE_DANGLING_FLAG       ,
754       PROJECT_TYPE_CLASS       ,
755       WP_FLAG                  ,
756 	CURRENT_FLAG             ,
757 	ORIGINAL_FLAG            ,
758 	CURRENT_ORIGINAL_FLAG    ,
759 	BASELINED_FLAG        	 ,
760 	SECONDARY_RBS_FLAG       ,
761       LP_FLAG
762     )
763       SELECT
764         DISTINCT
765             g_worker_id worker_id
766           , project_id
767           , plan_version_id
768           , wbs_struct_version_id
769           , rbs_struct_version_id
770           , plan_type_code
771           , plan_type_id
772           , time_phased_type_code
773           , NULL time_dangling_flag
774           , NULL rate_dangling_flag
775           , NULL PROJECT_TYPE_CLASS
776           , is_wp_flag
777           , current_flag
778           , original_flag
779           , current_original_flag
780           , baselined_flag
781           , SECONDARY_RBS_FLAG
782           , lp_flag
783      FROM
784      (
785 	 SELECT   -- RBS push and Partial refresh: All RBSes for -3, -4 slices.
786 	   /*+ ORDERED */
787             map.project_id                      project_id
788           , whd.plan_version_id               plan_version_id
789           , whd.wbs_version_id                 wbs_struct_version_id
790           , rhd.rbs_version_id                 rbs_struct_version_id -- extract for this rbs version id
791           , whd.plan_type_code                plan_type_code   /*4471527 */
792           , DECODE( whd.plan_version_id
793                   , -1
794                   , TO_NUMBER(NULL)
795                   , whd.plan_type_id)          plan_type_id
796           , 'G'                                time_phased_type_code
797 		  , 'N'                              is_wp_flag
798 		  , DECODE(whd.plan_version_id, -3, 'Y', -4, 'N') current_flag
799 		  , 'X'                          original_flag
800 		  , DECODE(whd.plan_version_id, -3, 'N', -4, 'Y') current_original_flag
801 		  , DECODE(whd.plan_version_id, -3, 'Y', -4, 'N') baselined_flag
802 		  , 'N'  		                     SECONDARY_RBS_FLAG
803 		  , 'Y'                                lp_flag
804       FROM
805             pji_pjp_proj_batch_map map
806           , PA_PROJECTS_ALL  ppa
807 	  , pji_pjp_wbs_header whd
808           , pji_pjp_rbs_header rhd
809       WHERE 1=1
810           AND ppa.project_id = map.project_id
811           -- AND map.PROJECT_ACTIVE_FLAG = 'Y'
812           AND map.worker_id = g_worker_id
813           AND p_context IN ( 'RBS', 'PLANTYPE')
814 		  AND whd.plan_version_id IN (l_cb_ver, l_co_ver)
815 		  AND rhd.plan_version_id IN (l_cb_ver, l_co_ver)
816 		  AND whd.plan_version_id = rhd.plan_version_id
817 		  AND whd.plan_type_code = rhd.plan_type_code   /*4471527 */
818 		  AND whd.project_id = map.project_id
819 		  AND rhd.project_id = map.project_id
820        UNION ALL
821 	 SELECT   -- Incr 2 , Full 2, RBS push 3, Partial refresh 4: All RBSes for -1, -3, -4 slices.
822 	   /*+ ORDERED */
823             map.project_id                      project_id
824           , whd.plan_version_id               plan_version_id
825           , whd.wbs_version_id                 wbs_struct_version_id
826           , rhd.rbs_version_id                 rbs_struct_version_id -- extract for this rbs version id
827           , whd.plan_type_code                 plan_type_code
828           , DECODE( whd.plan_version_id
829                   , -1
830                   , TO_NUMBER(NULL)
831                   , whd.plan_type_id)          plan_type_id
832           , 'G'                                time_phased_type_code
833 		  , 'N'                              is_wp_flag
834 		  , NULL                         current_flag
835 		  , 'X'                          original_flag
836 		  , NULL                         current_original_flag
837 		  , NULL                         baselined_flag
838 		  , 'N'  		                     SECONDARY_RBS_FLAG
839 		  , 'Y'                                lp_flag
840       FROM
841                  pji_pjp_proj_batch_map map
842                , PA_PROJECTS_ALL  ppa
843 	       , pji_pjp_wbs_header whd
844                , pji_pjp_rbs_header rhd
845       WHERE 1=1
846           AND ppa.project_id = map.project_id
847           -- AND map.PROJECT_ACTIVE_FLAG = 'Y'
848           AND map.worker_id = g_worker_id
849 		  AND whd.plan_version_id = l_actual_ver
850 		  AND rhd.plan_version_id = l_actual_ver
851 		  AND whd.plan_version_id = rhd.plan_version_id
852           AND whd.plan_type_code = rhd.plan_type_code
853 		  AND whd.project_id = map.project_id
854 		  AND rhd.project_id = map.project_id
855         )
856       WHERE (project_id, plan_version_id, NVL(plan_type_id, -1), plan_type_code, rbs_struct_version_id)
857             NOT IN
858             (SELECT project_id, plan_version_id, NVL(plan_type_id, -1), plan_type_code, rbs_struct_version_id
859              FROM   pji_fm_extr_plnver4
860              WHERE  worker_id = g_worker_id
861                AND  plan_version_id in (-1, -3, -4));
862 
863    l_num_rows := l_num_rows + SQL%ROWCOUNT;  -- Bug#5208250
864 
865   print_time(' Number of ver4 records inserted for -1/-3/-4 plan versions is.. ' || SQL%ROWCOUNT );
866 
867    x_num_rows := l_num_rows;
868 
869      print_time (' Number of records inserted in Ver4 Table is : '||l_num_rows);
870 
871     if (l_num_rows=0) then
872     return;
873     end if;
874 
875 
876 
877   IF (p_context = 'RBS') THEN
878 
879     DELETE FROM pji_pjp_rbs_header -- Old RBS versions are to be replaced with new ones.
880     WHERE 1=1
881       AND rbs_version_id in (select rbs_version_id
882 			     from pa_rbs_versions_b
883 			     where rbs_header_id = l_rbs_header_id) --Modified for Bug#5728852 by VVJOSHI
884       AND plan_version_id > 0;
885 
886     print_time ( ' Number of records in rbs hdr tbl that were deleted is.. = ' || SQL%ROWCOUNT );
887 
888     DELETE FROM pji_rbs_denorm
889     WHERE struct_version_id = p_rbs_version_id; --Modified for Bug#6884573 by VVJOSHI
890 
891     print_time ( ' Number of rows in pji rbs denorm tbl that were deleted is.. = ' || SQL%ROWCOUNT );
892 
893     DELETE FROM pa_rbs_denorm
894     WHERE struct_version_id = p_rbs_version_id; --Modified for Bug#6884573 by VVJOSHI
895 
896     print_time ( ' Number of rows in pa rbs denorm tbl that were deleted is.. = ' || SQL%ROWCOUNT );
897 
898     END IF;
899 
900   print_time('VALIDATE_SET_PR_PARAMS : end.. ');
901 
902 EXCEPTION
903   WHEN OTHERS THEN
904     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
905     ( p_package_name   => g_package_name
906     , p_procedure_name => ' VALIDATE_SET_PR_PARAMS '
907     , x_return_status  => l_return_status ) ;
908 
909     RAISE;
910 END;
911 
912 
913 --
914 -- bug 4863241
915 -- Lock mode: Lock during sumz='P', Unlock during sumz=NULL
916 --
917 PROCEDURE OBTAIN_RELEASE_LOCKS (
918   p_context         IN          VARCHAR2
919 , p_lock_mode       IN          VARCHAR2 -- NULL or 'P'
920 , x_return_status   OUT NOCOPY  VARCHAR2
921 , x_msg_code        OUT NOCOPY  VARCHAR2 ) IS
922 
923   l_pl_exists          VARCHAR2(1) := 'N'; -- plan lines count.
924   l_dangl_exists       VARCHAR2(1) := 'N'; -- dangling processing is happening.
925   l_rbs_assoc_exists   NUMBER := 0; -- rbs assoc event exists.
926   l_rbs_prg_exists     NUMBER := 0; -- rbs prg event exists.
927   l_prg_change_exists  NUMBER := 0; -- prg change event exists.
928 
929   l_context            VARCHAR2(1000) := NULL;
930 
931 BEGIN
932 
933   print_time (' OBTAIN_RELEASE_LOCKS start: p_context = ' || p_context || ' p_lock_mode ' || p_lock_mode || ' worker id = ' || g_worker_id );
934 
935   IF (p_context NOT IN ('RBS', 'PARTIAL', 'INCREMENTAL', 'FULL')) THEN -- , 'PULL_DANGLING') THEN
936     print_time ( ' OBTAIN_RELEASE_LOCKS : Invalid p_context ' || p_context );
937     RETURN;
938   END IF;
939 
940   --
941   -- RBS
942   --    No program rollups, TN/TY slices does not change.
943   --    New LNY data, so program rollup can happen.
944   --    Only baselined fin plans/cb/co lock.
945   -- PLANTYPE
946   --    Actuals may have come in. LNN slice could have changed. Program rollups happen. New LNY data.
947   --    All plans for this project, lat pub all the way up, one wpwv above, cb/co.
948   -- INCR/FULL
949   --   Only actuals change, if yes then do program rollup.
950   --   lat pub all the way up.
951   -- PULL_DANGLING
952   --   Dangling baselined fin plan for this proj/cb/co, all cb/cos above striped by plan type.
953   -- RBS_ASSOC
954   --   TN/TY slices don't change. New LNY data for baselined fps.
955   --   All baselined fin plan for this proj/cb/co.
956   -- RBS_PRG
957   --   TN/TY slices don't change. New LNY data for baselined fps.
958   --   All baselined fin plan for this proj/cb/co *below* and above.
959   -- PRG_CHANGE
960   --   Lock all projects in new prg based on event.
961   --
962 
963 
964   BEGIN
965     SELECT 'Y'
966     INTO   l_pl_exists
967     FROM   pji_fm_extr_plan_lines epl
968     WHERE EXISTS (
969       SELECT 1
970       FROM   pji_pjp_proj_batch_map map
971       WHERE  map.worker_id = g_worker_id
972         AND  epl.project_id = map.project_id
973                  )
974      AND ROWNUM <= 1;
975   EXCEPTION
976     WHEN NO_DATA_FOUND THEN
977       NULL;
978   END;
979 
980   SELECT NVL(SUM(DECODE(event_type, 'PRG_CHANGE', 1, 0)), 0)
981        , NVL(SUM(DECODE(event_type, 'RBS_ASSOC', 1, 0)) , 0)
982        , NVL(SUM(DECODE(event_type, 'RBS_PRG', 1, 0))   , 0)
983   INTO   l_prg_change_exists
984        , l_rbs_assoc_exists
985        , l_rbs_prg_exists
986   FROM pji_pa_proj_events_log
987   WHERE 1=1
988     AND worker_id = g_worker_id;
989 
990 
991   BEGIN
992     select /*+ ordered index(bv PA_BUDGET_VERSIONS_U2) */
993       'Y'
994     into
995       l_dangl_exists
996     from
997       PJI_PJP_PROJ_BATCH_MAP map,
998       PA_BUDGET_VERSIONS bv
999     where
1000       map.WORKER_ID                = g_worker_id    and
1001       bv.PROJECT_ID                = map.PROJECT_ID and
1002       nvl(bv.WP_VERSION_FLAG, 'N') = 'N'            and
1003       bv.BUDGET_STATUS_CODE        = 'B'            and
1004       bv.PJI_SUMMARIZED_FLAG       = 'P'            and
1005       rownum                       = 1;
1006   EXCEPTION
1007     WHEN NO_DATA_FOUND THEN
1008       NULL;
1009   END;
1010 
1011   print_time ( ' Do plan lines exist? ' || l_pl_exists );
1012   print_time ( ' Do prg_change events exist? ' || l_prg_change_exists );
1013   print_time ( ' Do rbs assoc events exist? ' || l_rbs_assoc_exists );
1014   print_time ( ' Do rbs prg events exist? ' || l_rbs_prg_exists );
1015   print_time ( ' Do dangling FP versions exist? ' || l_dangl_exists );
1016 
1017 
1018   IF (p_context = 'INCREMENTAL') THEN -- incr+rbsprg+rbsassoc+prgchange+dangling
1019 
1020     IF (l_pl_exists = 'Y') THEN
1021 
1022       --   Only actuals change, if yes then do program rollup.
1023       --   lat pub all the way up.
1024 
1025       l_context := l_context || 'c_incr_act_etc:';
1026 
1027     END IF;
1028 
1029     IF (l_rbs_assoc_exists > 0) THEN
1030 
1031       --   TN/TY slices don't change. New LNY data for baselined fps.
1032       --   All baselined fin plan for this proj/cb/co.
1033 
1034       l_context := l_context || 'c_all_baselined_fps_rbsassoc:' || 'c_all_cb_co_rbsassoc_above:';
1035 
1036     END IF;
1037 
1038     IF (l_rbs_prg_exists > 0) THEN
1039 
1040       --   TN/TY slices don't change. New LNY data for baselined fps.
1041       --   All baselined fin plan for this proj/cb/co *below* and above.
1042 
1043       l_context := l_context || 'c_all_bslnd_fps_rbsprg_below:'
1044                              || 'c_all_cb_co_rbsprg_above:' || 'c_all_cb_co_rbsprg_below:';
1045 
1046     END IF;
1047 
1048     IF (l_prg_change_exists > 0) THEN
1049 
1050       -- PRG_CHANGE
1051       --   Lock all projects in new prg based on event.
1052 
1053       l_context := l_context || 'c_all_plans_prg_chng:';
1054 
1055     END IF;
1056 
1057     IF (l_dangl_exists = 'Y') THEN
1058 
1059       -- PRG_CHANGE
1060       --   Lock all projects in new prg based on event.
1061 
1062       l_context := l_context || 'c_dangl_fps_self:' || 'c_dangl_cb_co_above:' ;
1063 
1064     END IF;
1065 
1066   ELSIF (p_context = 'FULL') THEN
1067 
1068     IF (l_pl_exists = 'Y') THEN
1069 
1070       --   Only actuals change, if yes then do program rollup.
1071       --   lat pub all the way up.
1072 
1073       l_context := l_context || 'c_incr_act_etc:';
1074 
1075     END IF;
1076 
1077   ELSIF (p_context = 'RBS') THEN
1078 
1079     --    No program rollups, TN/TY slices does not change.
1080     --    Act/etc can come in too.
1081     --    New LNY data, so program rollup can happen.
1082     --    Only baselined fin plans/cb/co lock. Also, lpv all the way up.
1083 
1084     IF (l_pl_exists = 'Y') THEN
1085 
1086       --   Only actuals change, if yes then do program rollup.
1087       --   lat pub all the way up.
1088 
1089       l_context := l_context || 'c_incr_act_etc:';
1090 
1091     END IF;
1092 
1093 
1094     l_context := l_context || 'c_all_baselined_fps_generic:' || 'c_all_cb_co_above:';
1095 
1096 
1097   ELSIF (p_context = 'PARTIAL') THEN
1098 
1099     --    Actuals may have come in. LNN slice could have changed. Program rollups happen. New LNY data.
1100     --    Act/etc can come in too.
1101     --    All plans for this project, lat pub all the way up, one wpwv above, cb/co.
1102     --    Only baselined fin plans/cb/co lock. Also, lpv all the way up.
1103 
1104     IF (l_pl_exists = 'Y') THEN
1105 
1106       --   Only actuals change, if yes then do program rollup.
1107       --   lat pub all the way up.
1108 
1109       l_context := l_context || 'c_incr_act_etc:';
1110 
1111     END IF;
1112 
1113     l_context := l_context || 'c_all_baselined_fps_generic:' || 'c_all_cb_co_above:' ;
1114 
1115   END IF;
1116 
1117   UPDATE_LOCKS (
1118     p_context       => l_context
1119   , p_update_mode   => p_lock_mode
1120   , x_return_status => x_return_status
1121   , x_msg_code      => x_msg_code );
1122 
1123   print_time(' Obtained lock flag in WBS header table. # rows is .. ' || SQL%ROWCOUNT );
1124 
1125 EXCEPTION
1126 
1127   WHEN OTHERS THEN
1128     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
1129     ( p_package_name   => g_package_name
1130     , p_procedure_name => ' OBTAIN_RELEASE_LOCKS '
1131     , x_return_status  => x_return_status ) ;
1132 
1133     RAISE;
1134 END;
1135 
1136 
1137 --
1138 -- bug 4863241
1139 -- Lock mode: Lock during sumz='P', Unlock during sumz=NULL
1140 --
1141 PROCEDURE UPDATE_LOCKS (
1142   p_context         IN VARCHAR2
1143 , p_update_mode     IN VARCHAR2 -- P => lock, NULL => unlock
1144 , x_return_status   OUT NOCOPY VARCHAR2
1145 , x_msg_code        OUT NOCOPY VARCHAR2 ) IS
1146 
1147   l_count_already_locked  NUMBER := 0;
1148   l_count_just_updated    NUMBER := 0;
1149 
1150   l_last_update_date     date   := SYSDATE;
1151   l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
1152   l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
1153 
1154   excp_resource_busy EXCEPTION;
1155   PRAGMA EXCEPTION_INIT(excp_resource_busy, -54);
1156 
1157   CURSOR c_get_hdrs_lock_ver3_t_cur IS
1158   SELECT 1
1159   FROM   pji_pjp_wbs_header
1160   WHERE (project_id, plan_type_id, plan_version_id) IN
1161         (SELECT DISTINCT project_id, plan_type_id, plan_version_id
1162          FROM   pji_fm_extr_plnver3_t)
1163   FOR UPDATE;
1164 
1165   CURSOR c_get_hdrs_lock_map_cur IS
1166   SELECT /*+ index(hd, pji_pjp_wbs_header_n1) */   1
1167   FROM   pji_pjp_wbs_header hd
1168   WHERE  (project_id, plan_version_id, plan_type_id) IN
1169   (  SELECT hd1.project_id, plan_version_id, plan_type_id
1170      FROM   pji_pjp_wbs_header hd1
1171           , pji_pjp_proj_batch_map map
1172      WHERE  hd1.project_id = map.project_id
1173        AND  map.worker_id = g_worker_id
1174        AND  (hd1.plan_version_id > 0 OR hd1.plan_version_id IN (-3, -4))
1175   )
1176   FOR UPDATE;
1177 
1178 BEGIN
1179 
1180 Pji_Pjp_Fp_Curr_Wrap.INIT_ERR_STACK
1181 ( p_package_name   => g_package_name
1182 , x_return_status  => x_return_status ) ;
1183 
1184 print_time ( ' UPDATE_LOCKS: p_context = ' || p_context || ' p_update_mode = ' || NVL(p_update_mode, 'X'));
1185 
1186 IF ( NVL(p_update_mode, 'X') NOT IN ( 'P', 'X') ) THEN
1187   print_time( ' Invalid context, returning: ' || p_update_mode);
1188   RETURN;
1189 END IF;
1190 
1191 IF (   p_context NOT LIKE '%c_incr_act_etc%'
1192    AND p_context NOT LIKE '%c_all_cb_co_above%'
1193    AND p_context NOT LIKE '%c_all_baselined_fps_generic%'
1194    AND p_context NOT LIKE '%c_all_plans_prg_chng%'
1195    AND p_context NOT LIKE '%c_all_cb_co_rbsprg_below%'
1196    AND p_context NOT LIKE '%c_all_cb_co_rbsprg_above%'
1197    AND p_context NOT LIKE '%c_all_bslnd_fps_rbsprg_below%'
1198    AND p_context NOT LIKE '%c_all_cb_co_rbsassoc_above%'
1199    AND p_context NOT LIKE '%c_all_baselined_fps_rbsassoc%'
1200    AND p_context NOT LIKE '%c_dangl_fps_self%'
1201    AND p_context NOT LIKE '%c_dangl_cb_co_above%'
1202 ) THEN
1203   print_time( ' Invalid p_context, returning: ' || p_context);
1204   RETURN;
1205 END IF;
1206 
1207 DELETE FROM pji_fm_extr_plnver3_t;
1208 
1209 /*
1210 IF (p_context LIKE '%c_incr_act_etc%' AND p_update_mode = 'P') THEN
1211 
1212   INSERT INTO pji_fm_extr_plnver3_t
1213   (project_id, plan_version_id, plan_type_id)
1214   SELECT / * + ORDERED * / -- INDEX(HD9, PJI_PJP_WBS_HEADER_N1)
1215         DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1216   FROM pji_pjp_proj_batch_map map
1217      , pji_fm_extr_plan_lines epl
1218      , pji_pjp_wbs_header hd2 -- SUB
1219      , pji_xbs_denorm den
1220      , pji_pjp_wbs_header hd9 -- SUP
1221      , PA_PROJ_ELEM_VER_STRUCTURE ppevs1
1222      , PA_PROJ_ELEM_VER_STRUCTURE ppevs2
1223   WHERE
1224         den.struct_version_id IS NULL
1225     AND hd2.plan_type_id = hd9.plan_type_id
1226     AND hd2.wbs_version_id = den.sub_id -- struct_version_id
1227     AND hd9.wbs_version_id = den.sup_id
1228     and hd9.project_id = den.sup_project_id
1229     -- AND den.sup_level < den.sub_level
1230     AND den.struct_type = 'PRG'
1231     AND NVL(den.relationship_type, 'WF') IN ('LW', 'WF') --  'LW',
1232     AND hd9.wp_flag = 'Y'
1233     AND ppevs1.project_id = hd9.project_id
1234     AND ppevs1.element_version_id = hd9.wbs_version_id
1235     AND ppevs1.latest_eff_published_flag = 'Y'
1236     AND hd2.wp_flag = 'Y'
1237     AND ppevs2.project_id = hd2.project_id
1238     AND ppevs2.element_version_id = hd2.wbs_version_id
1239     AND ppevs2.latest_eff_published_flag = 'Y'
1240     AND epl.project_id = hd2.project_id
1241     AND map.project_id = hd2.project_id
1242     AND map.worker_id = g_worker_id
1243     -- AND p_context LIKE '%c_incr_act_etc%'
1244    ;
1245 
1246 END IF;
1247 */
1248 
1249 IF (p_context LIKE '%c_all_cb_co_above%' AND p_update_mode = 'P') THEN
1250 
1251   INSERT INTO pji_fm_extr_plnver3_t
1252   (project_id, plan_version_id, plan_type_id)
1253   SELECT /*+ ORDERED */
1254         DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1255   FROM pji_pjp_proj_batch_map map
1256      , pji_pjp_wbs_header hd1 -- SUB
1257      , pji_xbs_denorm den
1258      , pji_pjp_wbs_header hd9 -- SUP
1259   WHERE
1260         den.struct_version_id IS NULL
1261     AND hd1.wbs_version_id = den.sub_id -- struct_version_id
1262     AND hd9.wbs_version_id = den.sup_id
1263     AND den.struct_type = 'PRG'
1264     AND hd9.project_id = den.sup_project_id
1265     AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')  --  lf- sp.fp.str, wf/null- fsh.str, lw- sp.wp.str
1266     AND hd1.plan_version_id IN (-3, -4)
1267     AND hd9.plan_version_id IN (-3, -4)
1268     AND hd9.plan_type_id = hd1.plan_type_id
1269     AND map.worker_id = g_worker_id
1270     AND hd1.project_id = map.project_id
1271     -- AND p_context LIKE '%c_all_cb_co_above%'
1272     ;
1273 
1274 END IF;
1275 
1276 IF (p_context LIKE '%c_all_baselined_fps_generic%' AND p_update_mode = 'P') THEN
1277 
1278   INSERT INTO pji_fm_extr_plnver3_t
1279   (project_id, plan_version_id, plan_type_id)
1280   SELECT
1281         DISTINCT bv.project_id, bv.budget_version_id, bv.fin_plan_type_id
1282   FROM  pji_pjp_proj_batch_map map
1283       , PA_BUDGET_VERSIONS bv
1284   WHERE
1285          map.worker_id = g_worker_id
1286      AND bv.project_id = map.project_id
1287      AND NVL(bv.wp_version_flag, 'N') = 'N'
1288      AND bv.budget_status_code = 'B'
1289      -- AND p_context LIKE '%c_all_baselined_fps_generic%'
1290      ;
1291 
1292 END IF;
1293 
1294 
1295 IF (p_context LIKE '%c_all_plans_prg_chng%') THEN -- !! ONLY FOR PRG_CHANGE
1296 
1297   INSERT INTO pji_fm_extr_plnver3_t
1298   (project_id, plan_version_id, plan_type_id)
1299   SELECT DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1300   FROM -- pji_pjp_wbs_header hd2
1301        -- pa_proj_elem_ver_structure hd2
1302        pji_pa_proj_events_log ev
1303      , Pa_XBS_DENORM den
1304      , pji_pjp_wbs_header hd9 -- SUP
1305   WHERE
1306         den.struct_version_id IS NULL
1307     AND hd9.wbs_version_id = den.sup_id -- struct_version_id
1308     -- AND hd2.element_version_id = den.sub_id
1309 	-- AND hd2.project_id > 0
1310 	AND hd9.project_id = den.sup_project_id
1311     AND den.struct_type = 'PRG'
1312     AND den.prg_group IN ( TO_NUMBER(ev.event_object), TO_NUMBER(ev.attribute1))
1313     AND ev.worker_id = g_worker_id
1314     AND ev.event_type = 'PRG_CHANGE'
1315     AND (  hd9.plan_version_id > 0
1316         OR hd9.plan_version_id IN (-3, -4) )
1317     -- AND p_context LIKE '%c_all_plans_prg_chng%'
1318     ;
1319 
1320 END IF;
1321 
1322 
1323 IF (p_context LIKE '%c_all_cb_co_rbsprg_below%' AND p_update_mode = 'P') THEN
1324 
1325   INSERT INTO pji_fm_extr_plnver3_t
1326   (project_id, plan_version_id, plan_type_id)
1327   SELECT /*+ ordered */
1328        DISTINCT hd2.project_id, hd2.plan_version_id, hd2.plan_type_id
1329   FROM
1330        pji_pjp_proj_batch_map map
1331      , pji_pa_proj_events_log ev
1332      , pji_pjp_wbs_header hd9 -- SUP
1333      , pji_xbs_denorm den
1334      , pji_pjp_wbs_header hd2 -- SUB
1335   WHERE
1336         den.struct_version_id IS NULL
1337     AND hd9.wbs_version_id = den.sub_id -- struct_version_id
1338     AND hd2.wbs_version_id = den.sup_id
1339     AND hd9.project_id = den.sup_project_id
1340     AND den.struct_type = 'PRG'
1341     AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF') --  lf- sp.fp.str, wf/null- fsh.str, lw- sp.wp.str
1342     AND hd9.plan_version_id IN (-3, -4)
1343     AND hd2.plan_version_id IN (-3, -4)
1344     AND map.worker_id = g_worker_id
1345     AND ev.worker_id = g_worker_id
1346     AND ev.event_type = 'RBS_PRG'
1347     AND TO_NUMBER(ev.attribute1) = map.project_id
1348     AND hd9.project_id = map.project_id
1349     AND hd9.plan_type_id = hd2.plan_type_id
1350     -- AND p_context LIKE '%c_all_cb_co_rbsprg_below%'
1351     ;
1352 
1353 END IF;
1354 
1355 
1356 IF (p_context LIKE '%c_all_baselined_fps_rbsassoc%' AND p_update_mode = 'P') THEN
1357 
1358   INSERT INTO pji_fm_extr_plnver3_t
1359   (project_id, plan_version_id, plan_type_id)
1360   SELECT DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1361   FROM  pji_pjp_proj_batch_map map
1362       , pji_pa_proj_events_log ev
1363       , pji_pjp_wbs_header hd9 -- SUP
1364   WHERE
1365          map.worker_id = g_worker_id
1366      AND ev.worker_id = g_worker_id
1367      AND ev.event_type = 'RBS_ASSOC'
1368      AND TO_NUMBER(ev.attribute1) = map.project_id
1369      AND hd9.wp_flag = 'N'
1370      AND hd9.cb_flag || hd9.co_flag LIKE '%Y%'
1371      AND hd9.project_id = map.project_id
1372      AND not exists(select null
1373      				from pji_fp_xbs_accum_f fact
1374      				where fact.project_id = hd9.project_id
1375      				and fact.plan_version_id = hd9.plan_version_id
1376      				and fact.plan_type_id  = hd9.plan_type_id
1377      				and fact.rbs_version_id = ev.event_object)
1378      -- AND p_context LIKE '%c_all_baselined_fps_rbsassoc%'
1379    ;
1380 
1381 END IF;
1382 
1383 
1384 IF (p_context LIKE '%c_all_cb_co_rbsassoc_above_cur%' AND p_update_mode = 'P') THEN
1385 
1386   INSERT INTO pji_fm_extr_plnver3_t
1387   (project_id, plan_version_id, plan_type_id)
1388   SELECT DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1389   FROM pji_xbs_denorm den
1390      , pji_pjp_wbs_header hd1 -- SUB
1391      , pji_pjp_proj_batch_map map
1392      , pji_pa_proj_events_log ev
1393      , pji_pjp_wbs_header hd9 -- SUP
1394   WHERE
1395         den.struct_version_id IS NULL
1396     AND hd1.wbs_version_id = den.sub_id -- struct_version_id
1397     AND hd9.wbs_version_id = den.sup_id
1398     AND hd9.project_id = den.sup_project_id
1399     AND den.struct_type = 'PRG'
1400     AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')
1401     --  lf- sp.fp.str, wf/null- fsh.str, lw- sp.wp.str
1402     AND hd1.plan_version_id IN (-3, -4)
1403     AND hd9.plan_version_id IN (-3, -4)
1404     AND map.worker_id = g_worker_id
1405     AND ev.worker_id = g_worker_id
1406     AND ev.event_type = 'RBS_ASSOC'
1407     AND TO_NUMBER(ev.attribute1) = map.project_id
1408     AND hd1.project_id = map.project_id
1409     AND hd9.plan_type_id = hd1.plan_type_id
1410     AND not exists(select null
1411      				from pji_fp_xbs_accum_f fact
1412      				where fact.project_id = hd9.project_id
1413      				and fact.plan_version_id = hd9.plan_version_id
1414      				and fact.plan_type_id  = hd9.plan_type_id
1415      				and fact.rbs_version_id = ev.event_object)
1416     -- AND p_context LIKE '%c_all_cb_co_rbsassoc_above%'
1417     ;
1418 
1419 END IF;
1420 
1421 
1422 IF (p_context LIKE '%c_all_bslnd_fps_rbsprg_below%' AND p_update_mode = 'P') THEN
1423 
1424   INSERT INTO pji_fm_extr_plnver3_t
1425   (project_id, plan_version_id, plan_type_id)
1426   SELECT /*+ ordered */
1427          DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1428   FROM   pji_pjp_proj_batch_map map
1429        , pji_pa_proj_events_log ev
1430        -- , PA_BUDGET_VERSIONS bv
1431        , pji_xbs_denorm den
1432        , pji_pjp_wbs_header wh1 -- sup
1433        , pji_pjp_wbs_header hd9 -- SUb!!
1434   WHERE
1435         den.struct_version_id IS NULL
1436     AND hd9.wbs_version_id = den.sub_id -- struct_version_id
1437     AND wh1.wbs_version_id = den.sup_id
1438 	AND wh1.project_id = den.sup_project_id
1439     AND den.struct_type = 'PRG'
1440     AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')
1441     AND map.worker_id = g_worker_id
1442     AND ev.worker_id = g_worker_id
1443     AND ev.event_type = 'RBS_PRG'
1444     AND TO_NUMBER(ev.attribute1) = map.project_id
1445     AND wh1.project_id = map.project_id
1446     AND hd9.wp_flag = 'N'
1447     AND wh1.wp_flag = 'N'
1448     AND hd9.plan_version_id > 0
1449     AND wh1.plan_version_id > 0
1450     AND hd9.plan_type_id = wh1.plan_type_id
1451     AND hd9.cb_flag || hd9.co_flag LIKE '%Y%'
1452     -- AND p_context LIKE '%c_all_bslnd_fps_rbsprg_below%'
1453     ;
1454 
1455 END IF;
1456 
1457 
1458 IF (p_context LIKE '%c_all_cb_co_rbsprg_above%' AND p_update_mode = 'P') THEN
1459 
1460   INSERT INTO pji_fm_extr_plnver3_t
1461   (project_id, plan_version_id, plan_type_id)
1462   SELECT DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1463   FROM pji_xbs_denorm den
1464      , pji_pjp_wbs_header hd2 -- SUB
1465      , pji_pjp_proj_batch_map map
1466      , pji_pa_proj_events_log ev
1467      , pji_pjp_wbs_header hd9 -- SUP
1468   WHERE
1469         den.struct_version_id IS NULL
1470     AND hd9.wbs_version_id = den.sup_id -- struct_version_id
1471     AND hd2.wbs_version_id = den.sub_id
1472     AND hd9.project_id = den.sup_project_id
1473     AND den.struct_type = 'PRG'
1474     AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF') --  lf- sp.fp.str, wf/null- fsh.str, lw- sp.wp.str
1475     AND hd9.plan_version_id IN (-3, -4)
1476     AND hd2.plan_version_id IN (-3, -4)
1477     AND map.worker_id = g_worker_id
1478     AND ev.worker_id = g_worker_id
1479     AND ev.event_type = 'RBS_PRG'
1480     AND TO_NUMBER(ev.attribute1) = map.project_id
1481     AND hd2.project_id = map.project_id
1482     AND hd9.plan_type_id = hd2.plan_type_id
1483     -- AND p_context LIKE '%c_all_cb_co_rbsprg_above%'
1484     ;
1485 
1486 END IF;
1487 
1488 
1489 IF (p_context LIKE '%c_dangl_fps_self%' AND p_update_mode = 'P') THEN
1490 
1491   INSERT INTO pji_fm_extr_plnver3_t
1492   (project_id, plan_version_id, plan_type_id)
1493   SELECT DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1494   FROM  pji_pjp_proj_batch_map map
1495       , PA_BUDGET_VERSIONS bv
1496       , pji_pjp_wbs_header hd9 -- SUP
1497   WHERE
1498          map.worker_id = g_worker_id
1499      AND hd9.project_id = map.project_id
1500      AND hd9.project_id = bv.project_id
1501      AND hd9.plan_version_id = bv.budget_version_id
1502      AND hd9.wp_flag = 'N'
1503      AND bv.budget_status_code = 'B'
1504      AND bv.pji_summarized_flag = 'P'
1505      -- AND hd.lock_flag IS NOT NULL
1506      -- AND p_context LIKE '%c_dangl_fps_self%'
1507      ;
1508 
1509 END IF;
1510 
1511 
1512 IF (p_context LIKE '%c_dangl_cb_co_above%' AND p_update_mode = 'P') THEN
1513 
1514   INSERT INTO pji_fm_extr_plnver3_t
1515   (project_id, plan_version_id, plan_type_id)
1516   SELECT DISTINCT hd9.project_id, hd9.plan_version_id, hd9.plan_type_id
1517   FROM pji_xbs_denorm den
1518      , pji_pjp_wbs_header hd1 -- SUB
1519      , pji_pjp_proj_batch_map map
1520      , pa_budget_versions bv
1521      , pji_pjp_wbs_header hd9 -- SUP
1522   WHERE
1523         den.struct_version_id IS NULL
1524     AND map.project_id = bv.project_id
1525 	AND map.project_id = hd1.project_id
1526     AND hd1.wp_flag = 'N'
1527     AND hd1.plan_type_id = bv.fin_plan_type_id
1528     AND bv.budget_status_code = 'B'
1529     AND bv.pji_summarized_flag = 'P'
1530     AND hd1.wbs_version_id = den.sub_id -- struct_version_id
1531     AND hd9.wbs_version_id = den.sup_id
1532     AND hd9.project_id = den.sup_project_id
1533     AND den.struct_type = 'PRG'
1534     AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')
1535     --  lf- sp.fp.str, wf/null- fsh.str, lw- sp.wp.str
1536     AND hd1.plan_version_id IN (-3, -4)
1537     AND hd9.plan_version_id IN (-3, -4)
1538     AND map.worker_id = g_worker_id
1539     AND hd1.project_id = map.project_id
1540     AND hd9.plan_type_id = bv.fin_plan_type_id
1541     AND hd9.plan_type_id = hd1.plan_type_id
1542     AND hd1.plan_version_id = hd9.plan_version_id
1543     -- AND p_context LIKE '%c_dangl_cb_co_above%'
1544     ;
1545 
1546 END IF;
1547 
1548 
1549   OPEN   c_get_hdrs_lock_ver3_t_cur;
1550   CLOSE  c_get_hdrs_lock_ver3_t_cur;
1551 
1552   UPDATE pji_pjp_wbs_header hd9 -- SUP
1553   SET    lock_flag         = p_update_mode
1554        , LAST_UPDATE_DATE  = l_last_update_date
1555        , LAST_UPDATED_BY   = l_last_updated_by
1556        , LAST_UPDATE_LOGIN = l_last_update_login
1557   WHERE (project_id, plan_type_id, plan_version_id) IN
1558         (SELECT DISTINCT project_id, plan_type_id, plan_version_id
1559          FROM   pji_fm_extr_plnver3_t);
1560 
1561   print_time ( ' UPDATE_LOCKS # updated hdrs = ' || SQL%ROWCOUNT );
1562 
1563   DELETE FROM pji_fm_extr_plnver3_t;
1564   print_time ( ' # of records deleted in plnver3 = ' || SQL%ROWCOUNT );
1565 
1566 
1567   IF (p_update_mode IS NULL) THEN
1568 
1569     OPEN   c_get_hdrs_lock_map_cur;
1570     CLOSE  c_get_hdrs_lock_map_cur;
1571 
1572     update PJI_PJP_WBS_HEADER hd9
1573     set    LOCK_FLAG         = null,
1574            LAST_UPDATE_DATE  = l_last_update_date,
1575            LAST_UPDATED_BY   = l_last_updated_by,
1576            LAST_UPDATE_LOGIN = l_last_update_login
1577     where  (hd9.PLAN_VERSION_ID > 0 or
1578             hd9.PLAN_VERSION_ID in (-3, -4)) and
1579            exists
1580            (
1581            select
1582              1
1583            from
1584              PJI_PJP_PROJ_BATCH_MAP map
1585            where
1586              map.WORKER_ID = g_worker_id and
1587              hd9.PROJECT_ID = map.PROJECT_ID
1588            );
1589 
1590     print_time ( ' UPDATE_LOCKS # additional hdrs updated in unlock hdrs for prg change = ' || SQL%ROWCOUNT );
1591 
1592   END IF;
1593 
1594 
1595 EXCEPTION
1596 
1597   WHEN excp_resource_busy THEN
1598 
1599     x_return_status := Fnd_Api.G_RET_STS_ERROR;
1600     x_msg_code := Fnd_Message.GET_STRING(  APPIN => 'PJI'
1601                                         , NAMEIN => 'PJI_LOCK_NOT_OBTAINED');
1602 
1603     -- PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PJI'
1604     --                    , p_msg_name       => 'PJI_LOCK_NOT_OBTAINED');
1605     Fnd_Message.SET_NAME('PJI', 'PJI_LOCK_NOT_OBTAINED');
1606     Fnd_Msg_Pub.add_detail(p_message_type=>Fnd_Api.G_RET_STS_ERROR);
1607 
1608     print_time ( ' UPDATE_LOCKS exception ' || SQLERRM);
1609 
1610     Pji_Pjp_Fp_Curr_Wrap.EXCP_HANDLER
1611     ( p_package_name   => g_package_name
1612     , p_procedure_name => ' UPDATE_LOCKS '
1613     , x_return_status => x_return_status ) ;
1614 
1615     RAISE;
1616 
1617   WHEN OTHERS THEN
1618     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
1619     ( p_package_name   => g_package_name
1620     , p_procedure_name => ' UPDATE_LOCKS '
1621     , x_return_status  => x_return_status ) ;
1622 
1623     RAISE;
1624 
1625 END;
1626 
1627 
1628 --
1629 -- Get the budget versions that need to be extracted INTO a temp table.
1630 -- This is to set scope for extraction as well as track time/curr dangling records
1631 --  in the case of secondary slice creation.
1632 --
1633 -- !!!! Add event handler for RBS_PRG.
1634 --
1635 PROCEDURE EXTRACT_FIN_PLAN_VERS_BULK(
1636   p_slice_type        IN   VARCHAR2 := NULL  -- 'PRI' or 'SEC' or 'SECRBS'
1637 ) IS
1638   l_prg_exists varchar2(1) := 'N';
1639 BEGIN
1640 
1641   print_time('EXTRACT_FIN_PLAN_VERS_BULK : Begin ' );
1642 
1643   IF ( p_slice_type NOT IN ('PRI', 'SEC', 'SECRBS', 'SEC_PROJ', 'SECRBS_PROJ') ) THEN
1644     print_time('EXTRACT_FIN_PLAN_VERSIONS : Invalid slice type. Exitting. ' );
1645     RETURN;
1646   END IF;
1647 
1648   --#bug 5356051
1649   begin
1650     select 'Y'
1651     into l_prg_exists
1652     from dual
1653     where exists (
1654       select null
1655       from
1656         pa_proj_element_versions proj
1657       where
1658         proj.prg_group is not null
1659         and rownum <=1
1660     );
1661   exception
1662     when no_data_found then
1663       l_prg_exists := 'N';
1664   end;
1665   --#bug 5356051
1666 
1667   IF ( p_slice_type = 'PRI') THEN
1668 
1669     INSERT INTO PJI_FM_EXTR_PLNVER4
1670     (
1671       WORKER_ID                ,
1672       PROJECT_ID               ,
1673       PLAN_VERSION_ID          ,
1674       WBS_STRUCT_VERSION_ID    ,
1675       RBS_STRUCT_VERSION_ID    ,
1676       PLAN_TYPE_CODE           ,
1677       PLAN_TYPE_ID             ,
1678       TIME_PHASED_TYPE_CODE    ,
1679       TIME_DANGLING_FLAG       ,
1680       RATE_DANGLING_FLAG       ,
1681       PROJECT_TYPE_CLASS       ,
1682       WP_FLAG                  ,
1683 	CURRENT_FLAG             ,
1684 	ORIGINAL_FLAG            ,
1685 	CURRENT_ORIGINAL_FLAG    ,
1686 	BASELINED_FLAG        	 ,
1687       SECONDARY_RBS_FLAG       ,
1688       LP_FLAG
1689     )
1690       SELECT
1691             g_worker_id
1692           , bv.project_id                      project_id
1693           , bv.budget_version_id               plan_version_id
1694           , DECODE ( NVL(bv.wp_version_flag, 'N')
1695 		           , 'Y', bv.project_structure_version_id
1696 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
1697 				   )                           wbs_struct_version_id
1698           , fpo.rbs_version_id                 rbs_struct_version_id
1699  --         , to_char(fpo.fin_plan_type_id)      plan_type_code
1700           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
1701           , fpo.fin_plan_type_id               plan_type_id
1702 	          , DECODE(bv.version_type
1703                       , 'ALL',     fpo.all_time_phased_code
1704                       , 'COST',    fpo.cost_time_phased_code
1705                       , 'REVENUE', fpo.revenue_time_phased_code
1706                      )                       time_phased_type_code
1707           , NULL                             time_dangling_flag   -- to be used for dangling check.
1708           , NULL                             rate_dangling_flag   -- to be used for dangling check.
1709           , NULL                             PROJECT_TYPE_CLASS
1710 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
1711 		  , bv.current_flag                  current_flag
1712 		  , bv.original_flag                 original_flag
1713 		  , bv.current_original_flag         current_original_flag
1714 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
1715   	        , 'N'  		                     SECONDARY_RBS_FLAG
1716 		  , DECODE( NVL(bv.wp_version_flag, 'N')
1717 		          , 'Y'
1718 				  , DECODE(bv.project_structure_version_id
1719 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
1720                                  -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
1721 						 , 'Y'
1722 						 , 'N')
1723 				  , 'N'
1724 				  ) lp_flag
1725       FROM
1726            pa_budget_versions bv -- @pjdev115  bv
1727          , pa_proj_fp_options  fpo -- @pjdev115  fpo
1728          , pa_projects_all  ppa -- @pjdev115    ppa
1729       WHERE 1=1
1730           AND ppa.project_id = bv.project_id
1731           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
1732           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
1733           AND fpo.project_id = bv.project_id
1734           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
1735           AND bv.budget_version_id = fpo.fin_plan_version_id
1736           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
1737           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
1738           ;
1739 
1740   --#bug 5356051
1741   If l_prg_exists = 'Y' then --Programs exist, need to insert -3/-4 for programs as well
1742 
1743 
1744     INSERT INTO PJI_FM_EXTR_PLNVER4
1745     (
1746       WORKER_ID                ,
1747       PROJECT_ID               ,
1748       PLAN_VERSION_ID          ,
1749       WBS_STRUCT_VERSION_ID    ,
1750       RBS_STRUCT_VERSION_ID    ,
1751       PLAN_TYPE_CODE           ,
1752       PLAN_TYPE_ID             ,
1753       TIME_PHASED_TYPE_CODE    ,
1754       TIME_DANGLING_FLAG       ,
1755       RATE_DANGLING_FLAG       ,
1756       PROJECT_TYPE_CLASS       ,
1757       WP_FLAG                  ,
1758 	  CURRENT_FLAG             ,
1759 	  ORIGINAL_FLAG            ,
1760 	  CURRENT_ORIGINAL_FLAG    ,
1761 	  BASELINED_FLAG        	 ,
1762       SECONDARY_RBS_FLAG       ,
1763       LP_FLAG
1764     )
1765       SELECT DISTINCT -- For insertion of -3/-4 header records.
1766             g_worker_id worker_id
1767           , den.sup_project_id                      project_id
1768           , cbco.plan_version_id               plan_version_id
1769           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(den.sup_project_id) wbs_struct_version_id
1770           , hd2.rbs_struct_version_id        rbs_version_id
1771           , hd2.plan_type_code plan_type_code  /*4471527 */
1772           , hd2.plan_type_id                 plan_type_id
1773           , hd2.time_phased_type_code        time_phased_type_code
1774           , NULL                             TIME_DANGLING_FLAG
1775           , NULL                             RATE_DANGLING_FLAG
1776           , NULL                             PROJECT_TYPE_CLASS
1777 		  , 'N'                              is_wp_flag
1778 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
1779 		  , NULL                             original_flag
1780 		  , DECODE(cbco.plan_version_id, -4, 'Y', 'N') current_original_flag
1781 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') baselined_flag
1782 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
1783 		  , 'Y'                              lp_flag
1784 	  FROM
1785            pji_fm_extr_plnver4 hd2 -- sub
1786          , pji_xbs_denorm den
1787          , ( SELECT -3 plan_version_id FROM dual
1788              UNION ALL
1789              SELECT -4 plan_version_id FROM dual ) cbco
1790       WHERE
1791               hd2.baselined_flag = 'Y'
1792 	      AND hd2.wp_flag = 'N'
1793           AND hd2.worker_id = g_worker_id
1794           AND den.struct_version_id IS NULL
1795           AND hd2.wbs_struct_version_id = den.sub_id
1796           AND den.struct_type = 'PRG'
1797           AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF');
1798 
1799   elsif l_prg_exists = 'N' then
1800 
1801     INSERT INTO PJI_FM_EXTR_PLNVER4
1802     (
1803       WORKER_ID                ,
1804       PROJECT_ID               ,
1805       PLAN_VERSION_ID          ,
1806       WBS_STRUCT_VERSION_ID    ,
1807       RBS_STRUCT_VERSION_ID    ,
1808       PLAN_TYPE_CODE           ,
1809       PLAN_TYPE_ID             ,
1810       TIME_PHASED_TYPE_CODE    ,
1811       TIME_DANGLING_FLAG       ,
1812       RATE_DANGLING_FLAG       ,
1813       PROJECT_TYPE_CLASS       ,
1814       WP_FLAG                  ,
1815       CURRENT_FLAG             ,
1816       ORIGINAL_FLAG            ,
1817       CURRENT_ORIGINAL_FLAG    ,
1818       BASELINED_FLAG        	 ,
1819       SECONDARY_RBS_FLAG       ,
1820       LP_FLAG
1821     )
1822       SELECT DISTINCT -- For insertion of -3/-4 header records.
1823             g_worker_id worker_id
1824           , hd2.project_id                     project_id
1825           , cbco.plan_version_id               plan_version_id
1826           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(hd2.project_id) wbs_struct_version_id
1827           , hd2.rbs_struct_version_id        rbs_version_id
1828           , hd2.plan_type_code plan_type_code  /*4471527 */
1829           , hd2.plan_type_id                 plan_type_id
1830           , hd2.time_phased_type_code        time_phased_type_code
1831           , NULL                             TIME_DANGLING_FLAG
1832           , NULL                             RATE_DANGLING_FLAG
1833           , NULL                             PROJECT_TYPE_CLASS
1834           , 'N'                              is_wp_flag
1835 	  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
1836 	  , NULL                             original_flag
1837 	  , DECODE(cbco.plan_version_id, -4, 'Y', 'N') current_original_flag
1838 	  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') baselined_flag
1839 	  , 'Y' 	  		                 SECONDARY_RBS_FLAG
1840           , 'Y'                              lp_flag
1841        FROM
1842            pji_fm_extr_plnver4 hd2 -- sub
1843          , ( SELECT -3 plan_version_id FROM dual
1844              UNION ALL
1845              SELECT -4 plan_version_id FROM dual ) cbco
1846       WHERE
1847               hd2.baselined_flag = 'Y'
1848 	  AND hd2.wp_flag = 'N'
1849           AND hd2.worker_id = g_worker_id;
1850 
1851   end if;
1852   --#bug 5356051
1853 
1854 
1855   ELSIF ( p_slice_type = 'SEC') THEN
1856 
1857     INSERT INTO PJI_FM_EXTR_PLNVER4
1858     (
1859       WORKER_ID                ,
1860       PROJECT_ID               ,
1861       PLAN_VERSION_ID          ,
1862       WBS_STRUCT_VERSION_ID    ,
1863       RBS_STRUCT_VERSION_ID    ,
1864       PLAN_TYPE_CODE           ,
1865       PLAN_TYPE_ID             ,
1866       TIME_PHASED_TYPE_CODE    ,
1867       TIME_DANGLING_FLAG       ,
1868       RATE_DANGLING_FLAG       ,
1869       PROJECT_TYPE_CLASS       ,
1870       WP_FLAG                  ,
1871 	CURRENT_FLAG             ,
1872 	ORIGINAL_FLAG            ,
1873 	CURRENT_ORIGINAL_FLAG    ,
1874 	BASELINED_FLAG        	 ,
1875       SECONDARY_RBS_FLAG       ,
1876       LP_FLAG
1877     )
1878       SELECT
1879             g_worker_id
1880           , bv.project_id                      project_id
1881           , bv.budget_version_id               plan_version_id
1882           , DECODE ( NVL(bv.wp_version_flag, 'N')
1883 		           , 'Y', bv.project_structure_version_id
1884 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
1885 				   )                           wbs_struct_version_id
1886           , fpo.rbs_version_id                 rbs_struct_version_id
1887 --          , to_char(fpo.fin_plan_type_id)      plan_type_code
1888               , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
1889           , fpo.fin_plan_type_id               plan_type_id
1890           , DECODE(bv.version_type
1891                       , 'ALL',     fpo.all_time_phased_code
1892                       , 'COST',    fpo.cost_time_phased_code
1893                       , 'REVENUE', fpo.revenue_time_phased_code
1894                      )                       time_phased_type_code
1895           , NULL                             time_dangling_flag   -- to be used for dangling check.
1896           , NULL                             rate_dangling_flag   -- to be used for dangling check.
1897           , NULL                             PROJECT_TYPE_CLASS
1898 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
1899 		  , bv.current_flag                  current_flag
1900 		  , bv.original_flag                 original_flag
1901 		  , bv.current_original_flag         current_original_flag
1902 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
1903   	      , 'N'  		                     SECONDARY_RBS_FLAG
1904 		  , DECODE( NVL(bv.wp_version_flag, 'N')
1905 		          , 'Y'
1906 				  , DECODE(bv.project_structure_version_id
1907 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
1908 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
1909 						 , 'Y'
1910 						 , 'N')
1911 				  , 'N'
1912 				  ) lp_flag
1913       FROM
1914            pa_budget_versions bv -- @pjdev115  bv
1915          , pa_proj_fp_options  fpo -- @pjdev115  fpo
1916          , pa_projects_all  ppa -- @pjdev115    ppa
1917       WHERE 1=1
1918           AND ppa.project_id = bv.project_id
1919           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
1920           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
1921           AND fpo.project_id = bv.project_id
1922           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
1923           AND bv.budget_version_id = fpo.fin_plan_version_id
1924           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
1925           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
1926           AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
1927           AND bv.budget_status_code = 'B'
1928           AND NVL(bv.wp_version_flag, 'N') = 'N';
1929 
1930 
1931   ELSIF ( p_slice_type = 'SECRBS') THEN
1932 
1933     INSERT INTO PJI_FM_EXTR_PLNVER4
1934     (
1935       WORKER_ID                ,
1936       PROJECT_ID               ,
1937       PLAN_VERSION_ID          ,
1938       WBS_STRUCT_VERSION_ID    ,
1939       RBS_STRUCT_VERSION_ID    ,
1940       PLAN_TYPE_CODE           ,
1941       PLAN_TYPE_ID             ,
1942       TIME_PHASED_TYPE_CODE    ,
1943       TIME_DANGLING_FLAG       ,
1944       RATE_DANGLING_FLAG       ,
1945       PROJECT_TYPE_CLASS       ,
1946       WP_FLAG                  ,
1947 	CURRENT_FLAG             ,
1948 	ORIGINAL_FLAG            ,
1949 	CURRENT_ORIGINAL_FLAG    ,
1950 	BASELINED_FLAG        	 ,
1951       SECONDARY_RBS_FLAG       ,
1952       LP_FLAG
1953     )
1954       SELECT
1955             g_worker_id
1956           , bv.project_id                      project_id
1957           , bv.budget_version_id               plan_version_id
1958           , DECODE ( NVL(bv.wp_version_flag, 'N')
1959 		           , 'Y', bv.project_structure_version_id
1960 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
1961 				   )                           wbs_struct_version_id
1962 		  , rpa.rbs_version_id                 rbs_struct_version_id
1963           -- , fpo.rbs_version_id                 rbs_struct_version_id
1964 --          , to_char(fpo.fin_plan_type_id)      plan_type_code
1965           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
1966           , fpo.fin_plan_type_id               plan_type_id
1967           , DECODE(bv.version_type
1968                       , 'ALL',     fpo.all_time_phased_code
1969                       , 'COST',    fpo.cost_time_phased_code
1970                       , 'REVENUE', fpo.revenue_time_phased_code
1971                      )                       time_phased_type_code
1972           , NULL                             time_dangling_flag   -- to be used for dangling check.
1973           , NULL                             rate_dangling_flag   -- to be used for dangling check.
1974           , NULL                             PROJECT_TYPE_CLASS
1975 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
1976 		  , bv.current_flag                  current_flag
1977 		  , bv.original_flag                 original_flag
1978 		  , bv.current_original_flag         current_original_flag
1979 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
1980 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
1981 		  , DECODE( NVL(bv.wp_version_flag, 'N')
1982 		          , 'Y'
1983 				  , DECODE(bv.project_structure_version_id
1984 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
1985 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
1986 						 , 'Y'
1987 						 , 'N')
1988 				  , 'N'
1989 				  ) lp_flag
1990       FROM
1991            pa_budget_versions bv -- @pjdev115  bv
1992          , pa_proj_fp_options  fpo -- @pjdev115  fpo
1993          , pa_projects_all  ppa -- @pjdev115    ppa
1994 		 , PA_RBS_PRJ_ASSIGNMENTS rpa
1995       WHERE 1=1
1996           AND ppa.project_id = bv.project_id
1997           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
1998           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
1999           AND fpo.project_id = bv.project_id
2000           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2001           AND bv.budget_version_id = fpo.fin_plan_version_id
2002           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2003           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2004           AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
2005           AND bv.budget_status_code = 'B'
2006 	    AND rpa.project_id = bv.project_id
2007 	    -- AND DECODE ( rpa.wp_usage_flag, 'Y', 'Y', 'X') = NVL(bv.wp_version_flag, 'N')
2008 	    -- AND DECODE ( rpa.fp_usage_flag, 'Y', 'N', 'X') = NVL(bv.wp_version_flag, 'N')
2009 	    AND rpa.assignment_status = 'ACTIVE'
2010 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
2011           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2012 	    AND rpa.reporting_usage_flag = 'Y';
2013 
2014 /*Plans are restricted by the projects chosen by the user*/
2015 
2016   ELSIF ( p_slice_type = 'SEC_PROJ') THEN
2017 
2018     INSERT INTO PJI_FM_EXTR_PLNVER4
2019     (
2020       WORKER_ID                ,
2021       PROJECT_ID               ,
2022       PLAN_VERSION_ID          ,
2023       WBS_STRUCT_VERSION_ID    ,
2024       RBS_STRUCT_VERSION_ID    ,
2025       PLAN_TYPE_CODE           ,
2026       PLAN_TYPE_ID             ,
2027       TIME_PHASED_TYPE_CODE    ,
2028       TIME_DANGLING_FLAG       ,
2029       RATE_DANGLING_FLAG       ,
2030       PROJECT_TYPE_CLASS       ,
2031       WP_FLAG                  ,
2032 	CURRENT_FLAG             ,
2033 	ORIGINAL_FLAG            ,
2034 	CURRENT_ORIGINAL_FLAG    ,
2035 	BASELINED_FLAG        	 ,
2036       SECONDARY_RBS_FLAG       ,
2037       LP_FLAG
2038     )
2039       SELECT
2040             g_worker_id
2041           , bv.project_id                      project_id
2042           , bv.budget_version_id               plan_version_id
2043           , DECODE ( NVL(bv.wp_version_flag, 'N')
2044 		           , 'Y', bv.project_structure_version_id
2045 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2046 				   )                           wbs_struct_version_id
2047           , fpo.rbs_version_id                 rbs_struct_version_id
2048 --          , to_char(fpo.fin_plan_type_id)      plan_type_code
2049           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2050           , fpo.fin_plan_type_id               plan_type_id
2051           , DECODE(bv.version_type
2052                       , 'ALL',     fpo.all_time_phased_code
2053                       , 'COST',    fpo.cost_time_phased_code
2054                       , 'REVENUE', fpo.revenue_time_phased_code
2055                      )                       time_phased_type_code
2056           , NULL                             time_dangling_flag   -- to be used for dangling check.
2057           , NULL                             rate_dangling_flag   -- to be used for dangling check.
2058           , NULL                             PROJECT_TYPE_CLASS
2059 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
2060 		  , bv.current_flag                  current_flag
2061 		  , bv.original_flag                 original_flag
2062 		  , bv.current_original_flag         current_original_flag
2063 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
2064   	      , 'N'  		                     SECONDARY_RBS_FLAG
2065 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2066 		          , 'Y'
2067 				  , DECODE(bv.project_structure_version_id
2068 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2069 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2070 						 , 'Y'
2071 						 , 'N')
2072 				  , 'N'
2073 				  ) lp_flag
2074       FROM
2075            pa_budget_versions bv -- @pjdev115  bv
2076          , pa_proj_fp_options  fpo -- @pjdev115  fpo
2077          , pa_projects_all  ppa -- @pjdev115    ppa
2078 		 , pji_pjp_proj_batch_map map
2079       WHERE 1=1
2080           AND ppa.project_id = bv.project_id
2081           AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
2082           AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
2083           AND fpo.project_id = bv.project_id
2084           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2085           AND bv.budget_version_id = fpo.fin_plan_version_id
2086           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2087           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2088           AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
2089           AND bv.budget_status_code = 'B'
2090           AND NVL(bv.wp_version_flag, 'N') = 'N'
2091 		AND ppa.project_id = map.project_id
2092 		AND map.worker_id = g_worker_id
2093       UNION ALL
2094       SELECT
2095             g_worker_id
2096           , bv.project_id                      project_id
2097           , cbco.plan_version_id               plan_version_id
2098           , DECODE ( NVL(bv.wp_version_flag, 'N')
2099 		           , 'Y', bv.project_structure_version_id
2100 		           , Pa_Project_Structure_Utils.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2101 				   )                           wbs_struct_version_id
2102           , fpo.rbs_version_id                 rbs_struct_version_id
2103 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2104           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2105           , fpo.fin_plan_type_id               plan_type_id
2106           , DECODE(bv.version_type
2107                       , 'ALL',     fpo.all_time_phased_code
2108                       , 'COST',    fpo.cost_time_phased_code
2109                       , 'REVENUE', fpo.revenue_time_phased_code
2110                      )                       time_phased_type_code
2111           , NULL                             time_dangling_flag   -- to be used for dangling check.
2112           , NULL                             rate_dangling_flag   -- to be used for dangling check.
2113           , NULL                             PROJECT_TYPE_CLASS
2114 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
2115 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
2116 		  , bv.original_flag                 original_flag
2117 		  , DECODE(cbco.plan_version_id, -4, 'Y', 'N') current_original_flag
2118 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') baselined_flag
2119   	      , 'N'  		                     SECONDARY_RBS_FLAG
2120 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2121 		          , 'Y'
2122 				  , DECODE(bv.project_structure_version_id
2123 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2124 				         -- , Pa_Project_Structure_Utils.GET_FIN_STRUC_VER_ID(bv.project_id)
2125 						 , 'Y'
2126 						 , 'N')
2127 				  , 'N'
2128 				  ) lp_flag
2129       FROM
2130            pa_budget_versions bv
2131          , pa_proj_fp_options  fpo
2132 		 , pji_pjp_proj_batch_map map
2133 		 , ( SELECT -3 plan_version_id FROM dual
2134              UNION ALL
2135              SELECT -4 plan_version_id FROM dual ) cbco
2136       WHERE 1=1
2137 		AND bv.project_id = map.project_id
2138 		AND map.worker_id = g_worker_id
2139           AND bv.version_type IS NOT NULL -- COST, REVENUE, etc. Should not be null.
2140           AND bv.fin_plan_type_id IS NOT NULL -- Old budgets model data is not picked up with this condition.
2141           AND fpo.project_id = bv.project_id
2142           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2143           AND bv.budget_version_id = fpo.fin_plan_version_id
2144           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2145           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2146           AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
2147           AND bv.budget_status_code = 'B'
2148           AND NVL(bv.wp_version_flag, 'N') = 'N';
2149 
2150   ELSIF ( p_slice_type = 'SECRBS_PROJ') THEN
2151 
2152     INSERT INTO PJI_FM_EXTR_PLNVER4
2153     (
2154       WORKER_ID                ,
2155       PROJECT_ID               ,
2156       PLAN_VERSION_ID          ,
2157       WBS_STRUCT_VERSION_ID    ,
2158       RBS_STRUCT_VERSION_ID    ,
2159       PLAN_TYPE_CODE           ,
2160       PLAN_TYPE_ID             ,
2161       TIME_PHASED_TYPE_CODE    ,
2162       TIME_DANGLING_FLAG       ,
2163       RATE_DANGLING_FLAG       ,
2164       PROJECT_TYPE_CLASS       ,
2165       WP_FLAG                  ,
2166 	CURRENT_FLAG             ,
2167 	ORIGINAL_FLAG            ,
2168 	CURRENT_ORIGINAL_FLAG    ,
2169 	BASELINED_FLAG        	 ,
2170       SECONDARY_RBS_FLAG       ,
2171       LP_FLAG
2172     )
2173      SELECT DISTINCT
2174 	g_worker_id              ,
2175       PROJECT_ID               ,
2176       PLAN_VERSION_ID          ,
2177       WBS_STRUCT_VERSION_ID    ,
2178       RBS_STRUCT_VERSION_ID    ,
2179       PLAN_TYPE_CODE           ,
2180       PLAN_TYPE_ID             ,
2181       TIME_PHASED_TYPE_CODE    ,
2182       NULL TIME_DANGLING_FLAG       ,
2183       NULL RATE_DANGLING_FLAG       ,
2184       NULL PROJECT_TYPE_CLASS       ,
2185       WP_FLAG                  ,
2186 	CURRENT_FLAG             ,
2187 	ORIGINAL_FLAG            ,
2188 	CURRENT_ORIGINAL_FLAG    ,
2189 	BASELINED_FLAG        	 ,
2190       SECONDARY_RBS_FLAG       ,
2191       LP_FLAG
2192 	  FROM
2193 	  (
2194       SELECT
2195             bv.project_id                      project_id
2196           , bv.budget_version_id               plan_version_id
2197           , DECODE ( NVL(bv.wp_version_flag, 'N')
2198 		           , 'Y', bv.project_structure_version_id
2199 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2200 				   )                           wbs_struct_version_id
2201 		  , rpa.rbs_version_id                 rbs_struct_version_id
2202 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2203           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2204           , fpo.fin_plan_type_id               plan_type_id
2205           , DECODE(bv.version_type
2206                       , 'ALL',     fpo.all_time_phased_code
2207                       , 'COST',    fpo.cost_time_phased_code
2208                       , 'REVENUE', fpo.revenue_time_phased_code
2209                      )                       time_phased_type_code
2210 
2211 
2212 
2213 		  , NVL(bv.wp_version_flag, 'N') wp_flag
2214 		  , bv.current_flag                  current_flag
2215 		  , bv.original_flag                 original_flag
2216 		  , bv.current_original_flag         current_original_flag
2217 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
2218 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2219 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2220 		          , 'Y'
2221 				  , DECODE(bv.project_structure_version_id
2222 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2223 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2224 						 , 'Y'
2225 						 , 'N')
2226 				  , 'N'
2227 				  ) lp_flag
2228       FROM
2229            pa_budget_versions bv -- @pjdev115  bv
2230          , pa_proj_fp_options  fpo -- @pjdev115  fpo
2231          , pa_projects_all  ppa -- @pjdev115    ppa
2232 		 , PA_RBS_PRJ_ASSIGNMENTS rpa
2233 		 , pji_pjp_proj_batch_map map
2234       WHERE   ppa.project_id = bv.project_id
2235           AND bv.version_type IS NOT NULL
2236           AND bv.fin_plan_type_id IS NOT NULL
2237           AND fpo.project_id = bv.project_id
2238           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2239           AND bv.budget_version_id = fpo.fin_plan_version_id
2240           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2241           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2242           AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
2243           AND bv.budget_status_code = 'B'
2244 	    AND rpa.project_id = bv.project_id
2245 	    -- AND DECODE ( rpa.wp_usage_flag, 'Y', 'Y', 'X') = NVL(bv.wp_version_flag, 'N')
2246 	    -- AND DECODE ( rpa.fp_usage_flag, 'Y', 'N', 'X') = NVL(bv.wp_version_flag, 'N')
2247 	    AND rpa.assignment_status = 'ACTIVE'
2248 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
2249           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2250 	    AND rpa.reporting_usage_flag = 'Y'
2251 		AND ppa.project_id = map.project_id
2252 		AND map.worker_id = g_worker_id
2253       UNION ALL
2254       SELECT
2255             bv.project_id                      project_id
2256           , cbco.plan_version_id               plan_version_id
2257           , DECODE ( NVL(bv.wp_version_flag, 'N')
2258 		           , 'Y', bv.project_structure_version_id
2259 		           , Pa_Project_Structure_Utils.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2260 				   )                           wbs_struct_version_id
2261 		  , rpa.rbs_version_id                 rbs_struct_version_id
2262 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2263           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2264           , fpo.fin_plan_type_id               plan_type_id
2265           , DECODE(bv.version_type
2266                       , 'ALL',     fpo.all_time_phased_code
2267                       , 'COST',    fpo.cost_time_phased_code
2268                       , 'REVENUE', fpo.revenue_time_phased_code
2269                      )                       time_phased_type_code
2270 		  , NVL(bv.wp_version_flag, 'N') wp_flag
2271 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
2272 		  , bv.original_flag                 original_flag
2273 		  , DECODE(cbco.plan_version_id, -4, 'Y', 'N') current_original_flag
2274 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') baselined_flag
2275 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2276 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2277 		          , 'Y'
2278 				  , DECODE(bv.project_structure_version_id
2279 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2280 				         -- , Pa_Project_Structure_Utils.GET_FIN_STRUC_VER_ID(bv.project_id)
2281 						 , 'Y'
2282 						 , 'N')
2283 				  , 'N'
2284 				  ) lp_flag
2285       FROM
2286            pa_budget_versions bv -- @pjdev115  bv
2287          , pa_proj_fp_options  fpo -- @pjdev115  fpo
2288          -- , pa_projects_all  ppa -- @pjdev115    ppa
2289 		 , PA_RBS_PRJ_ASSIGNMENTS rpa
2290 		 , pji_pjp_proj_batch_map map
2291 		 , ( SELECT -3 plan_version_id FROM dual
2292              UNION ALL
2293              SELECT -4 plan_version_id FROM dual ) cbco
2294       WHERE   map.project_id = bv.project_id
2295           AND bv.version_type IS NOT NULL
2296           AND bv.fin_plan_type_id IS NOT NULL
2297           AND fpo.project_id = bv.project_id
2298           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2299           AND bv.budget_version_id = fpo.fin_plan_version_id
2300           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2301           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2302           AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
2303           AND bv.budget_status_code = 'B'
2304 	    AND rpa.project_id = bv.project_id
2305 	    -- AND DECODE ( rpa.wp_usage_flag, 'Y', 'Y', 'X') = NVL(bv.wp_version_flag, 'N')
2306 	    -- AND DECODE ( rpa.fp_usage_flag, 'Y', 'N', 'X') = NVL(bv.wp_version_flag, 'N')
2307 	    AND rpa.assignment_status = 'ACTIVE'
2308 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
2309           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2310 	    AND rpa.reporting_usage_flag = 'Y'
2311 		-- AND ppa.project_id = map.project_id
2312 		AND map.worker_id = g_worker_id
2313       UNION ALL
2314       SELECT   -- 'RBS_ASSOC'
2315             bv.project_id                      project_id
2316           , bv.budget_version_id               plan_version_id
2317           , DECODE ( NVL(bv.wp_version_flag, 'N')
2318 		           , 'Y', bv.project_structure_version_id
2319 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2320 				   )                           wbs_struct_version_id
2321 		  , rpa.rbs_version_id                 rbs_struct_version_id
2322 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2323           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2324           , fpo.fin_plan_type_id               plan_type_id
2325           , DECODE(bv.version_type
2326                       , 'ALL',     fpo.all_time_phased_code
2327                       , 'COST',    fpo.cost_time_phased_code
2328                       , 'REVENUE', fpo.revenue_time_phased_code
2329                      )                       time_phased_type_code
2330 
2331 
2332 
2333 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
2334 		  , bv.current_flag                  current_flag
2335 		  , bv.original_flag                 original_flag
2336 		  , bv.current_original_flag         current_original_flag
2337 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
2338 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2339 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2340 		          , 'Y'
2341 				  , DECODE(bv.project_structure_version_id
2342 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2343 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2344 						 , 'Y'
2345 						 , 'N')
2346 				  , 'N'
2347 				  ) lp_flag
2348       FROM
2349            pa_budget_versions bv -- @pjdev115  bv
2350          , pa_proj_fp_options  fpo -- @pjdev115  fpo
2351          , pa_projects_all  ppa -- @pjdev115    ppa
2352 		 , PA_RBS_PRJ_ASSIGNMENTS rpa
2353 		 , pji_pjp_proj_batch_map map
2354 		 , pji_pa_proj_events_log ev
2355       WHERE   ppa.project_id = bv.project_id
2356           AND bv.version_type IS NOT NULL
2357           AND bv.fin_plan_type_id IS NOT NULL
2358           AND fpo.project_id = bv.project_id
2359           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2360           AND bv.budget_version_id = fpo.fin_plan_version_id
2361           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'
2362           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE')
2363           AND bv.pji_summarized_flag = 'Y'
2364           AND bv.budget_status_code = 'B'
2365 	    AND rpa.project_id = bv.project_id
2366 	    AND rpa.assignment_status = 'ACTIVE'
2367 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
2368           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2369 	    AND rpa.reporting_usage_flag = 'Y'
2370 		AND ppa.project_id = map.project_id
2371 		AND map.worker_id = g_worker_id
2372 		AND ev.worker_id = g_worker_id
2373 		AND ev.event_type = 'RBS_ASSOC'
2374 		AND TO_NUMBER(ev.attribute1) = map.project_id
2375 		AND rpa.rbs_version_id = TO_NUMBER(ev.event_object)
2376       UNION ALL
2377       SELECT   -- 'RBS_ASSOC'
2378             bv.project_id                      project_id
2379           , cbcov.plan_version_id              plan_version_id
2380           , DECODE ( NVL(bv.wp_version_flag, 'N')
2381 		           , 'Y', bv.project_structure_version_id
2382 		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2383 				   )                   wbs_struct_version_id
2384           , TO_NUMBER(ev.event_object)         rbs_struct_version_id
2385 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2386            , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2387           , fpo.fin_plan_type_id               plan_type_id
2388           , DECODE(bv.version_type
2389                       , 'ALL',     fpo.all_time_phased_code
2390                       , 'COST',    fpo.cost_time_phased_code
2391                       , 'REVENUE', fpo.revenue_time_phased_code
2392                      )                       time_phased_type_code
2393 
2394 
2395 
2396 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
2397 		  , DECODE(cbcov.plan_version_id, -3, 'Y', 'N') current_flag
2398 		  , bv.original_flag                 original_flag
2399 		  , DECODE(cbcov.plan_version_id, -4, 'Y', 'N') current_original_flag
2400 		  , DECODE(cbcov.plan_version_id, -3, 'Y', 'N') baselined_flag
2401 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2402 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2403 		          , 'Y'
2404 				  , DECODE(bv.project_structure_version_id
2405 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2406 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2407 						 , 'Y'
2408 						 , 'N')
2409 				  , 'N'
2410 				  ) lp_flag
2411       FROM
2412            pa_budget_versions bv
2413          , pa_proj_fp_options  fpo
2414          , PJI_PA_PROJ_EVENTS_LOG ev
2415          , (SELECT -3 plan_version_id FROM DUAL
2416             UNION ALL
2417             SELECT -4 plan_version_id FROM DUAL
2418            ) cbcov
2419       WHERE   1=1
2420           AND bv.version_type IS NOT NULL
2421           AND bv.fin_plan_type_id IS NOT NULL
2422           AND fpo.project_id = bv.project_id
2423           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2424           AND bv.budget_version_id = fpo.fin_plan_version_id
2425           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'
2426           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE')
2427           AND bv.pji_summarized_flag = 'Y'
2428           AND bv.budget_status_code = 'B'
2429           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2430           AND ev.worker_id = g_worker_id
2431           AND ev.event_type = 'RBS_ASSOC'
2432           AND bv.project_id = TO_NUMBER(ev.attribute1)
2433           AND NOT EXISTS -- Exclude plan version/RBS version combo for which plan data has already been extracted.
2434               (  SELECT 1
2435                  FROM pji_pjp_rbs_header rh
2436                  WHERE rh.project_id = bv.project_id
2437                    AND rh.plan_version_id = cbcov.plan_version_id
2438                    AND rh.rbs_version_id = TO_NUMBER(ev.event_object)
2439               )
2440       UNION ALL
2441       SELECT -- 'RBS_PRG' event handling.
2442             bv.project_id                      project_id
2443           , bv.budget_version_id               plan_version_id
2444           , DECODE ( NVL(bv.wp_version_flag, 'N')
2445                    , 'Y', bv.project_structure_version_id
2446                    , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2447                    )                           wbs_struct_version_id
2448           , TO_NUMBER(ev.event_object)         rbs_version_id
2449 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2450           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2451           , fpo.fin_plan_type_id               plan_type_id
2452           , DECODE(bv.version_type
2453                       , 'ALL',     fpo.all_time_phased_code
2454                       , 'COST',    fpo.cost_time_phased_code
2455                       , 'REVENUE', fpo.revenue_time_phased_code
2456                      )                       time_phased_type_code
2457 
2458 
2459 
2460 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
2461 		  , bv.current_flag                  current_flag
2462 		  , bv.original_flag                 original_flag
2463 		  , bv.current_original_flag         current_original_flag
2464 		  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
2465 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2466 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2467 		          , 'Y'
2468 				  , DECODE(bv.project_structure_version_id
2469 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2470 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2471 						 , 'Y'
2472 						 , 'N')
2473 				  , 'N'
2474 				  ) lp_flag
2475 	  FROM
2476            pa_budget_versions bv
2477          , pa_proj_fp_options  fpo
2478          , pji_pa_proj_events_log ev
2479          , pji_pjp_wbs_header hd1 -- sup
2480          , pji_pjp_wbs_header hd2 -- sub
2481          , pji_xbs_denorm den
2482       WHERE   bv.version_type IS NOT NULL
2483           AND bv.fin_plan_type_id IS NOT NULL
2484           AND fpo.project_id = bv.project_id
2485           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2486           AND bv.budget_version_id = fpo.fin_plan_version_id
2487           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'
2488           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE')
2489           AND bv.pji_summarized_flag = 'Y'
2490           AND bv.budget_status_code = 'B'
2491           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2492           AND NVL(fpo.rbs_version_id, -1) <> TO_NUMBER(ev.event_object) -- Non-pri RBSes.
2493           AND ev.worker_id = g_worker_id
2494           AND ev.event_type = 'RBS_PRG'
2495           AND den.struct_version_id IS NULL
2496           AND TO_NUMBER(ev.attribute1) = hd1.project_id
2497           AND hd1.project_id = den.sup_project_id
2498           AND hd2.wbs_version_id = den.sub_id -- struct_version_id
2499           AND hd1.wbs_version_id = den.sup_id
2500           AND den.struct_type = 'PRG'
2501           AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')
2502           AND hd2.wp_flag = 'N'
2503           AND hd1.wp_flag = 'N'
2504           AND hd2.plan_version_id = bv.budget_version_id
2505           AND hd1.plan_version_id > 0
2506           -- AND hd2.plan_version_id > 0
2507           AND NOT EXISTS -- Exclude plan version/RBS version combo for which plan data has already been extracted.
2508           (  SELECT 1
2509              FROM pji_pjp_rbs_header rh
2510              WHERE rh.project_id = bv.project_id
2511                AND rh.plan_version_id = bv.budget_version_id
2512                AND rh.rbs_version_id = TO_NUMBER(ev.event_object)
2513           )
2514       UNION ALL
2515       SELECT -- 'RBS_PRG' event handling.
2516             bv.project_id                      project_id
2517           , cbcov.plan_version_id               plan_version_id
2518           , DECODE ( NVL(bv.wp_version_flag, 'N')
2519                    , 'Y', bv.project_structure_version_id
2520                    , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
2521                    )                           wbs_struct_version_id
2522           , TO_NUMBER(ev.event_object)         rbs_version_id
2523 --          , TO_CHAR(fpo.fin_plan_type_id)      plan_type_code
2524           , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2525           , fpo.fin_plan_type_id               plan_type_id
2526           , DECODE(bv.version_type
2527                       , 'ALL',     fpo.all_time_phased_code
2528                       , 'COST',    fpo.cost_time_phased_code
2529                       , 'REVENUE', fpo.revenue_time_phased_code
2530                      )                       time_phased_type_code
2531 		  , NVL(bv.wp_version_flag, 'N') is_wp_flag
2532 		  , DECODE(cbcov.plan_version_id, -3, 'Y', 'N') current_flag
2533 		  , bv.original_flag                 original_flag
2534 		  , DECODE(cbcov.plan_version_id, -4, 'Y', 'N') current_original_flag
2535 		  , DECODE(cbcov.plan_version_id, -3, 'Y', 'N') baselined_flag
2536 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2537 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2538 		          , 'Y'
2539 				  , DECODE(bv.project_structure_version_id
2540 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2541 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2542 						 , 'Y'
2543 						 , 'N')
2544 				  , 'N'
2545 				  ) lp_flag
2546 	  FROM
2547            pa_budget_versions bv
2548          , pa_proj_fp_options  fpo
2549          , PJI_PA_PROJ_EVENTS_LOG ev
2550          , pji_pjp_wbs_header hd1 -- sup
2551          , pji_pjp_wbs_header hd2 -- sub
2552          , pji_xbs_denorm den
2553          , (SELECT -3 plan_version_id FROM DUAL
2554             UNION ALL
2555             SELECT -4 plan_version_id FROM DUAL
2556            ) cbcov
2557       WHERE   bv.version_type IS NOT NULL
2558           AND bv.fin_plan_type_id IS NOT NULL
2559           AND fpo.project_id = bv.project_id
2560           AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2561           AND bv.budget_version_id = fpo.fin_plan_version_id
2562           AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'
2563           AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE')
2564           AND bv.pji_summarized_flag = 'Y'
2565           AND bv.budget_status_code = 'B'
2566           AND NVL(bv.wp_version_flag, 'N')  = 'N'
2567           AND NVL(fpo.rbs_version_id, -1) <> TO_NUMBER(ev.event_object) -- rpa.rbs_version_id
2568           AND ev.worker_id = g_worker_id
2569           AND ev.event_type = 'RBS_PRG'
2570 		  AND den.struct_version_id IS NULL
2571 		  AND TO_NUMBER(ev.attribute1) = hd1.project_id
2572 		  AND hd1.project_id = den.sup_project_id
2573 		  AND hd2.wbs_version_id = den.sub_id -- struct_version_id
2574 		  AND hd1.wbs_version_id = den.sup_id
2575 		  AND den.struct_type = 'PRG'
2576 		  AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')
2577 		  AND hd2.wp_flag = 'N'
2578 		  AND hd1.wp_flag = 'N'
2579 		  AND hd2.plan_version_id = bv.budget_version_id
2580 		  AND hd1.plan_version_id > 0
2581 		  -- AND hd2.plan_version_id > 0
2582               AND NOT EXISTS -- Exclude plan version/RBS version combo for which plan data has already been extracted.
2583               (  SELECT 1
2584                  FROM pji_pjp_rbs_header rh
2585                  WHERE rh.project_id = bv.project_id
2586                    AND rh.plan_version_id = cbcov.plan_version_id
2587                    AND rh.rbs_version_id = TO_NUMBER(ev.event_object)
2588               )
2589       UNION ALL
2590       SELECT DISTINCT -- 'PRG_CHANGE' event.
2591             den.sup_project_id                 project_id
2592           , cbco.plan_version_id               plan_version_id      -- -3/-4
2593           , Pa_Project_Structure_Utils.GET_FIN_STRUC_VER_ID(den.sup_project_id) wbs_version_id
2594           , fpo.parvi                           rbs_version_id
2595 --          , TO_CHAR(whsub.plan_type_id)        plan_type_code
2596            , whsub.plan_type_code plan_type_code   /*4471527 */
2597           , whsub.plan_type_id                 plan_type_id
2598           , fpo.tptc                           time_phased_type_code
2599           , 'N'                                wp_flag
2600           , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
2601           , DECODE(cbco.plan_version_id, -4, 'Y', 'N') original_flag
2602           , DECODE(cbco.plan_version_id, -4, 'Y', 'N') curr_original_flag
2603           , 'Y' baselined_flag
2604           , DECODE(fpo.fprvi, NULL, 'Y', 'N') SECONDARY_RBS_FLAG
2605           , 'Y' lp_flag
2606 	  FROM
2607 	        (
2608               SELECT fpo.project_id fpi
2609         	       , rpa.project_id rpi
2610         	       , fpo.fin_plan_type_id pti
2611         		   , fpo.rbs_version_id fprvi
2612         		   , rpa.rbs_version_id parvi
2613         		   , fpo.fin_plan_option_level_code fpolc
2614         		   , NVL( fpo.all_time_phased_code
2615 				 || fpo.cost_time_phased_code
2616 				 || fpo.revenue_time_phased_code
2617 				   , 'G') tptc
2618         		   , fpo.all_time_phased_code atpc
2619         		   , fpo.cost_time_phased_code ctpc
2620         		   , fpo.revenue_time_phased_code rtpc
2621 			   , fpo.fin_plan_version_id fpopvi
2622 			   , bv.budget_version_id bvpvi
2623 			   , NVL(bv.wp_version_flag, 'N') wpf
2624 			   , bv.budget_status_code bsc
2625         	  FROM pa_proj_fp_options fpo
2626         	     , pa_rbs_prj_assignments rpa
2627 		     , pa_budget_versions bv
2628                  , PJI_PJP_PROJ_BATCH_MAP map
2629         	  WHERE 1=1
2630         		AND fpo.project_id (+)= rpa.project_id
2631         		AND fpo.rbs_version_id (+)= rpa.rbs_version_id
2632 			AND bv.project_id = rpa.project_id
2633 			AND NVL(bv.wp_version_flag, 'N') = 'N'
2634 			AND bv.budget_status_code = 'B'		-- Don't care if dangling plan.
2635                   AND map.worker_id = g_worker_id
2636                   AND map.project_id = bv.project_id
2637         	  ) fpo
2638 			  , pa_xbs_denorm den
2639 			  , pji_pjp_proj_batch_map map
2640 			  , pji_pjp_wbs_header whsub
2641 			  , ( SELECT -3 plan_version_id FROM dual
2642         		      UNION ALL
2643         		      SELECT -4 plan_version_id FROM dual ) cbco
2644        WHERE NVL(fpolc, 'PLAN_VERSION') = 'PLAN_VERSION'
2645          AND map.worker_id = g_worker_id
2646          AND map.project_id = whsub.project_id
2647          AND whsub.wp_flag = 'N'
2648          AND whsub.plan_version_id <> -1
2649          -- AND fpo.rpi = den.sup_project_id
2650          AND fpo.rpi = map.project_id
2651          AND den.sub_id = whsub.wbs_version_id
2652          AND den.struct_version_id IS NULL
2653          AND den.struct_type = 'PRG'
2654          AND den.sup_level < den.sub_level
2655          AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF') --  Excluding 'LW'
2656          AND whsub.project_id = map.project_id
2657          /*
2658          AND NOT EXISTS (
2659                SELECT 1
2660                FROM pji_fm_extr_plnver4 ver3
2661                WHERE ver3.project_id = den.sup_project_id
2662 		     AND ver3.plan_version_id = cbco.plan_version_id
2663 		     AND ver3.rbs_struct_version_id = fpo.parvi
2664 		     AND ver3.plan_type_id = whsub.plan_type_id
2665 		     AND ver3.worker_id = g_worker_id
2666 		 )
2667          */
2668 	   AND EXISTS ( SELECT DISTINCT 1
2669                       FROM pji_pa_proj_events_log
2670                       WHERE event_type = 'PRG_CHANGE'
2671                         AND worker_id = g_worker_id )
2672     );
2673 
2674 
2675 
2676     --#bug 5356051
2677     If l_prg_exists = 'Y' then --Programs exist, need to insert -3/-4 for programs as well
2678 
2679     INSERT INTO PJI_FM_EXTR_PLNVER4
2680     (
2681       WORKER_ID                ,
2682       PROJECT_ID               ,
2683       PLAN_VERSION_ID          ,
2684       WBS_STRUCT_VERSION_ID    ,
2685       RBS_STRUCT_VERSION_ID    ,
2686       PLAN_TYPE_CODE           ,
2687       PLAN_TYPE_ID             ,
2688       TIME_PHASED_TYPE_CODE    ,
2689       TIME_DANGLING_FLAG       ,
2690       RATE_DANGLING_FLAG       ,
2691       PROJECT_TYPE_CLASS       ,
2692       WP_FLAG                  ,
2693 	CURRENT_FLAG             ,
2694 	ORIGINAL_FLAG            ,
2695 	CURRENT_ORIGINAL_FLAG    ,
2696 	BASELINED_FLAG        	 ,
2697       SECONDARY_RBS_FLAG       ,
2698       LP_FLAG
2699     )
2700       SELECT DISTINCT -- For insertion of -3/-4 header records.
2701             g_worker_id worker_id
2702           , den.sup_project_id                      project_id
2703           , cbco.plan_version_id               plan_version_id
2704           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(den.sup_project_id) wbs_struct_version_id
2705           , hd2.rbs_struct_version_id        rbs_version_id
2706           ,hd2.plan_type_code plan_type_code   /*4471527 */
2707           , hd2.plan_type_id                 plan_type_id
2708           , hd2.time_phased_type_code        time_phased_type_code
2709           , NULL                             TIME_DANGLING_FLAG
2710           , NULL                             RATE_DANGLING_FLAG
2711           , NULL                             PROJECT_TYPE_CLASS
2712 		  , 'N'                              is_wp_flag
2713 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
2714 		  , NULL                             original_flag
2715 		  , DECODE(cbco.plan_version_id, -4, 'Y', 'N') current_original_flag
2716 		  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') baselined_flag
2717 		  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2718 		  , 'Y'                              lp_flag
2719 	  FROM
2720            pji_fm_extr_plnver4 hd2 -- sub
2721          , pji_xbs_denorm den
2722          , ( SELECT -3 plan_version_id FROM dual
2723              UNION ALL
2724              SELECT -4 plan_version_id FROM dual ) cbco
2725       WHERE   hd2.baselined_flag = 'Y'
2726 	    AND hd2.wp_flag = 'N'
2727           AND hd2.plan_version_id > 0
2728           AND hd2.worker_id = g_worker_id
2729           AND den.struct_version_id IS NULL
2730           AND hd2.wbs_struct_version_id = den.sub_id -- struct_version_id
2731           AND den.struct_type = 'PRG'
2732           AND NVL(den.relationship_type, 'WF') IN ('LF', 'WF')
2733           AND (den.sup_project_id, cbco.plan_version_id, hd2.plan_type_id,hd2.plan_type_code, hd2.rbs_struct_version_id) NOT IN
2734               ( SELECT project_id, plan_version_id, plan_type_id, plan_type_code,rbs_struct_version_id
2735                 FROM   pji_fm_extr_plnver4 ver3
2736                 WHERE  worker_id = g_worker_id );
2737 
2738   elsif l_prg_exists = 'N' then
2739 
2740     INSERT INTO PJI_FM_EXTR_PLNVER4
2741     (
2742       WORKER_ID                ,
2743       PROJECT_ID               ,
2744       PLAN_VERSION_ID          ,
2745       WBS_STRUCT_VERSION_ID    ,
2746       RBS_STRUCT_VERSION_ID    ,
2747       PLAN_TYPE_CODE           ,
2748       PLAN_TYPE_ID             ,
2749       TIME_PHASED_TYPE_CODE    ,
2750       TIME_DANGLING_FLAG       ,
2751       RATE_DANGLING_FLAG       ,
2752       PROJECT_TYPE_CLASS       ,
2753       WP_FLAG                  ,
2754 	CURRENT_FLAG             ,
2755 	ORIGINAL_FLAG            ,
2756 	CURRENT_ORIGINAL_FLAG    ,
2757 	BASELINED_FLAG        	 ,
2758       SECONDARY_RBS_FLAG       ,
2759       LP_FLAG
2760     )
2761       SELECT DISTINCT -- For insertion of -3/-4 header records.
2762             g_worker_id worker_id
2763           , hd2.project_id                      project_id
2764           , cbco.plan_version_id               plan_version_id
2765           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(hd2.project_id) wbs_struct_version_id
2766           , hd2.rbs_struct_version_id        rbs_version_id
2767           , hd2.plan_type_code plan_type_code   /*4471527 */
2768           , hd2.plan_type_id                 plan_type_id
2769           , hd2.time_phased_type_code        time_phased_type_code
2770           , NULL                             TIME_DANGLING_FLAG
2771           , NULL                             RATE_DANGLING_FLAG
2772           , NULL                             PROJECT_TYPE_CLASS
2773 	  , 'N'                              is_wp_flag
2774 	  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') current_flag
2775 	  , NULL                             original_flag
2776 	  , DECODE(cbco.plan_version_id, -4, 'Y', 'N') current_original_flag
2777 	  , DECODE(cbco.plan_version_id, -3, 'Y', 'N') baselined_flag
2778 	  , 'Y' 	  		                 SECONDARY_RBS_FLAG
2779 	  , 'Y'                              lp_flag
2780 	  FROM
2781            pji_fm_extr_plnver4 hd2 -- sub
2782          , ( SELECT -3 plan_version_id FROM dual
2783              UNION ALL
2784              SELECT -4 plan_version_id FROM dual ) cbco
2785       WHERE   hd2.baselined_flag = 'Y'
2786 	  AND hd2.wp_flag = 'N'
2787           AND hd2.plan_version_id > 0
2788           AND hd2.worker_id = g_worker_id
2789           AND (hd2.project_id, cbco.plan_version_id, hd2.plan_type_id,hd2.plan_type_code, hd2.rbs_struct_version_id) NOT IN
2790               ( SELECT project_id, plan_version_id, plan_type_id, plan_type_code,rbs_struct_version_id
2791                 FROM   pji_fm_extr_plnver4 ver3
2792                 WHERE  worker_id = g_worker_id );
2793 
2794   end if;
2795   --#bug 5356051
2796 
2797 
2798   END IF;
2799 
2800 
2801   print_time('EXTRACT_FIN_PLAN_VERS_BULK : End .. rows processed ' || SQL%ROWCOUNT );
2802 
2803 EXCEPTION
2804   WHEN OTHERS THEN
2805 
2806     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
2807                              p_procedure_name => 'EXTRACT_FIN_PLAN_VERS_BULK');
2808     print_time('EXTRACT_FIN_PLAN_VERS_BULK : Exception: ' || sqlerrm );
2809     RAISE;
2810 END;
2811 
2812 
2813 --
2814 -- Get the budget versions that need to be extracted INTO a temp table.
2815 -- This is to set scope for extraction as well as track time/curr dangling records
2816 --  in the case of secondary slice creation.
2817 --
2818 PROCEDURE EXTRACT_FIN_PLAN_VERSIONS(
2819   p_fp_version_ids    IN   SYSTEM.pa_num_tbl_type := pji_empty_num_tbl
2820 , p_slice_type        IN   VARCHAR2 := NULL -- 'PRI' or 'SEC' or 'SECRBS'
2821 ) IS
2822   l_count NUMBER;
2823 BEGIN
2824 
2825   print_time('EXTRACT_FIN_PLAN_VERSIONS : Begin ' );
2826 
2827   IF (p_slice_type NOT IN ( 'PRI', 'SEC', 'SECRBS' )) THEN
2828     print_time('EXTRACT_FIN_PLAN_VERSIONS : Invalid slice type. Exitting. ' );
2829     RETURN;
2830   END IF;
2831 
2832   print_time('EXTRACT_FIN_PLAN_VERSIONS : l_count is ... ' || p_fp_version_ids.COUNT );
2833 
2834   IF (p_slice_type = 'PRI') THEN
2835 
2836     FOR I IN 1..p_fp_version_ids.COUNT LOOP
2837 
2838           INSERT INTO PJI_FM_EXTR_PLNVER4 ver3
2839           (
2840             WORKER_ID                ,
2841             PROJECT_ID               ,
2842             PLAN_VERSION_ID          ,
2843             WBS_STRUCT_VERSION_ID    ,
2844             RBS_STRUCT_VERSION_ID    ,
2845             PLAN_TYPE_CODE           ,
2846             PLAN_TYPE_ID             ,
2847             TIME_PHASED_TYPE_CODE    ,
2848             TIME_DANGLING_FLAG       ,
2849             RATE_DANGLING_FLAG       ,
2850             PROJECT_TYPE_CLASS       ,
2851             WP_FLAG                  ,
2852             CURRENT_FLAG             ,
2853             ORIGINAL_FLAG            ,
2854             CURRENT_ORIGINAL_FLAG    ,
2855             BASELINED_FLAG           ,
2856 	      SECONDARY_RBS_FLAG       ,
2857             LP_FLAG
2858           )
2859             SELECT
2860                   g_worker_id
2861                 , bv.project_id                      project_id
2862                 , bv.budget_version_id               plan_version_id
2863                 , DECODE ( NVL(bv.wp_version_flag, 'N')
2864       		           , 'Y', bv.project_structure_version_id
2865       		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2866       				   )                           wbs_struct_version_id
2867                 , fpo.rbs_version_id                 rbs_struct_version_id
2868 --                , to_char(fpo.fin_plan_type_id)      plan_type_code
2869                 , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2870                 , fpo.fin_plan_type_id               plan_type_id
2871                 , DECODE(bv.version_type
2872                             , 'ALL',     fpo.all_time_phased_code
2873                             , 'COST',    fpo.cost_time_phased_code
2874                             , 'REVENUE', fpo.revenue_time_phased_code
2875                            )                       time_phased_type_code
2876                 , NULL                             time_dangling_flag   -- to be used for dangling check.
2877                 , NULL                             rate_dangling_flag   -- to be used for dangling check.
2878                 , NULL                             PROJECT_TYPE_CLASS
2879                 , NVL(bv.wp_version_flag, 'N') is_wp_flag
2880                 , bv.current_flag                  current_flag
2881                 , bv.original_flag                 original_flag
2882                 , bv.current_original_flag         current_original_flag
2883                 , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
2884 		    , 'N'  		                     SECONDARY_RBS_FLAG
2885 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2886 		          , 'Y'
2887 				  , DECODE(bv.project_structure_version_id
2888 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2889 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2890 						 , 'Y'
2891 						 , 'N')
2892 				  , 'N'
2893 				  ) lp_flag
2894             FROM
2895                  pa_budget_versions bv -- @pjdev115  bv
2896                , pa_proj_fp_options  fpo -- @pjdev115  fpo
2897                , pa_projects_all  ppa -- @pjdev115    ppa
2898             WHERE 1=1
2899                 AND ppa.project_id = bv.project_id
2900                 AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
2901                 AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
2902                                                     -- Ask VR: How about WP version.. are they picked up with this condition??
2903                 AND fpo.project_id = bv.project_id
2904                 AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2905                 AND bv.budget_version_id = fpo.fin_plan_version_id
2906                 AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2907                 AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2908                 -- AND bv.pji_summarized_flag = 'N'
2909                 AND bv.budget_version_id = p_fp_version_ids(i) ;
2910 
2911     END LOOP;
2912 
2913   ELSIF (p_slice_type = 'SEC') THEN
2914 
2915     FOR I IN 1..p_fp_version_ids.COUNT LOOP
2916 
2917           INSERT INTO PJI_FM_EXTR_PLNVER4 ver3
2918           (
2919             WORKER_ID                ,
2920             PROJECT_ID               ,
2921             PLAN_VERSION_ID          ,
2922             WBS_STRUCT_VERSION_ID    ,
2923             RBS_STRUCT_VERSION_ID    ,
2924             PLAN_TYPE_CODE           ,
2925             PLAN_TYPE_ID             ,
2926             TIME_PHASED_TYPE_CODE    ,
2927             TIME_DANGLING_FLAG       ,
2928             RATE_DANGLING_FLAG       ,
2929             PROJECT_TYPE_CLASS       ,
2930             WP_FLAG                  ,
2931             CURRENT_FLAG             ,
2932             ORIGINAL_FLAG            ,
2933             CURRENT_ORIGINAL_FLAG    ,
2934             BASELINED_FLAG           ,
2935   	      SECONDARY_RBS_FLAG       ,
2936             LP_FLAG
2937           )
2938             SELECT
2939                   g_worker_id
2940                 , bv.project_id                      project_id
2941                 , bv.budget_version_id               plan_version_id
2942                 , DECODE ( NVL(bv.wp_version_flag, 'N')
2943       		           , 'Y', bv.project_structure_version_id
2944       		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2945       				   )                           wbs_struct_version_id
2946                 , fpo.rbs_version_id                 rbs_struct_version_id
2947 --                , to_char(fpo.fin_plan_type_id)      plan_type_code
2948                 , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
2949                 , fpo.fin_plan_type_id               plan_type_id
2950                 , DECODE(bv.version_type
2951                             , 'ALL',     fpo.all_time_phased_code
2952                             , 'COST',    fpo.cost_time_phased_code
2953                             , 'REVENUE', fpo.revenue_time_phased_code
2954                            )                       time_phased_type_code
2955                 , NULL                             time_dangling_flag   -- to be used for dangling check.
2956                 , NULL                             rate_dangling_flag   -- to be used for dangling check.
2957                 , NULL                             PROJECT_TYPE_CLASS
2958                 , NVL(bv.wp_version_flag, 'N') is_wp_flag
2959                 , bv.current_flag                  current_flag
2960                 , bv.original_flag                 original_flag
2961                 , bv.current_original_flag         current_original_flag
2962                 , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
2963 		    , 'N'  		                     SECONDARY_RBS_FLAG
2964 		  , DECODE( NVL(bv.wp_version_flag, 'N')
2965 		          , 'Y'
2966 				  , DECODE(bv.project_structure_version_id
2967 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
2968 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
2969 						 , 'Y'
2970 						 , 'N')
2971 				  , 'N'
2972 				  ) lp_flag
2973             FROM
2974                  pa_budget_versions bv -- @pjdev115  bv
2975                , pa_proj_fp_options  fpo -- @pjdev115  fpo
2976                , pa_projects_all  ppa -- @pjdev115    ppa
2977             WHERE 1=1
2978                 AND ppa.project_id = bv.project_id
2979                 AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
2980                 AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
2981                 AND fpo.project_id = bv.project_id
2982                 AND bv.fin_plan_type_id = fpo.fin_plan_type_id
2983                 AND bv.budget_version_id = fpo.fin_plan_version_id
2984                 AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
2985                 AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
2986                 AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
2987                 AND bv.budget_status_code = 'B'
2988                 AND NVL(bv.wp_version_flag, 'N') = 'N'
2989                 AND bv.budget_version_id = p_fp_version_ids(i) ;
2990 
2991     END LOOP;
2992 
2993 
2994   ELSIF (p_slice_type = 'SECRBS') THEN
2995 
2996     FOR I IN 1..p_fp_version_ids.COUNT LOOP
2997 
2998           INSERT INTO PJI_FM_EXTR_PLNVER4 ver3
2999           (
3000             WORKER_ID                ,
3001             PROJECT_ID               ,
3002             PLAN_VERSION_ID          ,
3003             WBS_STRUCT_VERSION_ID    ,
3004             RBS_STRUCT_VERSION_ID    ,
3005             PLAN_TYPE_CODE           ,
3006             PLAN_TYPE_ID             ,
3007             TIME_PHASED_TYPE_CODE    ,
3008             TIME_DANGLING_FLAG       ,
3009             RATE_DANGLING_FLAG       ,
3010             PROJECT_TYPE_CLASS       ,
3011             WP_FLAG                  ,
3012             CURRENT_FLAG             ,
3013             ORIGINAL_FLAG            ,
3014             CURRENT_ORIGINAL_FLAG    ,
3015             BASELINED_FLAG           ,
3016   	      SECONDARY_RBS_FLAG       ,
3017             LP_FLAG
3018           )
3019             SELECT
3020                   g_worker_id
3021                 , bv.project_id                      project_id
3022                 , bv.budget_version_id               plan_version_id
3023                 , DECODE ( NVL(bv.wp_version_flag, 'N')
3024       		           , 'Y', bv.project_structure_version_id
3025       		           , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
3026       				   )                           wbs_struct_version_id
3027                 , rpa.rbs_version_id                 rbs_struct_version_id
3028 --                , to_char(fpo.fin_plan_type_id)      plan_type_code
3029                 , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
3030                 , fpo.fin_plan_type_id               plan_type_id
3031                 , DECODE(bv.version_type
3032                             , 'ALL',     fpo.all_time_phased_code
3033                             , 'COST',    fpo.cost_time_phased_code
3034                             , 'REVENUE', fpo.revenue_time_phased_code
3035                            )                       time_phased_type_code
3036                 , NULL                             time_dangling_flag   -- to be used for dangling check.
3037                 , NULL                             rate_dangling_flag   -- to be used for dangling check.
3038                 , NULL                             PROJECT_TYPE_CLASS
3039                 , NVL(bv.wp_version_flag, 'N') is_wp_flag
3040                 , bv.current_flag                  current_flag
3041                 , bv.original_flag                 original_flag
3042                 , bv.current_original_flag         current_original_flag
3043                 , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
3044 		    , 'Y'  		                     SECONDARY_RBS_FLAG
3045 		  , DECODE( NVL(bv.wp_version_flag, 'N')
3046 		          , 'Y'
3047 				  , DECODE(bv.project_structure_version_id
3048 				            , PA_PROJECT_STRUCTURE_UTILS.GET_LATEST_WP_VERSION( bv.project_id) --  4682341
3049 				         -- , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id)
3050 						 , 'Y'
3051 						 , 'N')
3052 				  , 'N'
3053 				  ) lp_flag
3054             FROM
3055                  pa_budget_versions bv -- @pjdev115  bv
3056                , pa_proj_fp_options  fpo -- @pjdev115  fpo
3057                , pa_projects_all  ppa -- @pjdev115    ppa
3058 		 , PA_RBS_PRJ_ASSIGNMENTS rpa
3059             WHERE 1=1
3060                 AND ppa.project_id = bv.project_id
3061                 AND bv.version_type is not NULL -- COST, REVENUE, etc. Should not be null.
3062                 AND bv.fin_plan_type_id is not NULL -- Old budgets model data is not picked up with this condition.
3063                 AND fpo.project_id = bv.project_id
3064                 AND bv.fin_plan_type_id = fpo.fin_plan_type_id
3065                 AND bv.budget_version_id = fpo.fin_plan_version_id
3066                 AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'  -- Other values are: plan type and project.
3067                 AND bv.version_type IN ( 'ALL' , 'COST' , 'REVENUE') -- Eg of other version type is ORG_FORECAST.
3068                 AND bv.pji_summarized_flag = 'P' -- Pri slice created or dangling records exist.
3069                 AND bv.budget_status_code = 'B'
3070 	    AND rpa.project_id = bv.project_id
3071 	    AND rpa.assignment_status = 'ACTIVE'
3072 	    AND NVL(fpo.rbs_version_id, -1) <> rpa.rbs_version_id
3073 	    AND rpa.reporting_usage_flag = 'Y'
3074                 AND NVL(bv.wp_version_flag, 'N') = 'N'
3075                 AND bv.budget_version_id = p_fp_version_ids(i)
3076 	          AND bv.project_id = rpa.project_id;
3077 
3078     END LOOP;
3079 
3080   END IF;
3081 
3082   l_count := SQL%ROWCOUNT;
3083 
3084   print_time('EXTRACT_FIN_PLAN_VERSIONS : l_count is ... ' || l_count );
3085 
3086   print_time('EXTRACT_FIN_PLAN_VERSIONS : End' );
3087 
3088 EXCEPTION
3089   WHEN OTHERS THEN
3090     print_time('EXTRACT_FIN_PLAN_VERSIONS : Exception: ' || sqlerrm );
3091     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
3092                              p_procedure_name => 'EXTRACT_FIN_PLAN_VERSIONS');
3093     RAISE;
3094 END;
3095 
3096 
3097 --
3098 -- Extract the period level plan amounts for PA/GL/non time phased entries from budget lines
3099 --  for the primary RBS for this plan version into pji_fp_aggr_pjp1.
3100 -- EXTRACT_PLAN_AMOUNTS_PRIRBS
3101 PROCEDURE EXTRACT_PLAN_AMOUNTS_PRIRBS IS
3102   l_count NUMBER;
3103   l_max_project_id  NUMBER := NULL;
3104 BEGIN
3105 
3106     print_time('EXTRACT_PLAN_AMOUNTS_PRIRBS : Begin' );
3107     print_time('EXTRACT_PLAN_AMOUNTS_PRIRBS : Begin worker id is ... ' || 1);
3108 
3109 
3110   INSERT_NTP_CAL_RECORD ( x_max_project_id => l_max_project_id );
3111 
3112   IF (l_max_project_id IS NULL) THEN
3113     RETURN;
3114   END IF;
3115 
3116 
3117 
3118 
3119     INSERT INTO PJI_FP_AGGR_PJP1
3120     (
3121          WORKER_ID
3122        , PROJECT_ID
3123        , PROJECT_ORG_ID
3124        , PROJECT_ORGANIZATION_ID
3125        , PROJECT_ELEMENT_ID
3126        , TIME_ID
3127        , PERIOD_TYPE_ID
3128        , CALENDAR_TYPE
3129        , RBS_AGGR_LEVEL
3130        , WBS_ROLLUP_FLAG
3131        , PRG_ROLLUP_FLAG
3132        , CURR_RECORD_TYPE_ID
3133        , CURRENCY_CODE
3134        , RBS_ELEMENT_ID
3135        , RBS_VERSION_ID
3136        , PLAN_VERSION_ID
3137        , PLAN_TYPE_ID
3138        , RAW_COST
3139        , BRDN_COST
3140        , REVENUE
3141        , BILL_RAW_COST
3142        , BILL_BRDN_COST
3143        , BILL_LABOR_RAW_COST
3144        , BILL_LABOR_BRDN_COST
3145        , BILL_LABOR_HRS
3146        , EQUIPMENT_RAW_COST
3147        , EQUIPMENT_BRDN_COST
3148        , CAPITALIZABLE_RAW_COST
3149        , CAPITALIZABLE_BRDN_COST
3150        , LABOR_RAW_COST
3151        , LABOR_BRDN_COST
3152        , LABOR_HRS
3153        , LABOR_REVENUE
3154        , EQUIPMENT_HOURS
3155        , BILLABLE_EQUIPMENT_HOURS
3156        , SUP_INV_COMMITTED_COST
3157        , PO_COMMITTED_COST
3158        , PR_COMMITTED_COST
3159        , OTH_COMMITTED_COST
3160        , ACT_LABOR_HRS
3161 	   , ACT_EQUIP_HRS
3162 	   , ACT_LABOR_BRDN_COST
3163 	   , ACT_EQUIP_BRDN_COST
3164 	   , ACT_BRDN_COST
3165 	   , ACT_RAW_COST
3166 	   , ACT_REVENUE
3167          , ACT_LABOR_RAW_COST
3168          , ACT_EQUIP_RAW_COST
3169 	   , ETC_LABOR_HRS
3170 	   , ETC_EQUIP_HRS
3171 	   , ETC_LABOR_BRDN_COST
3172 	   , ETC_EQUIP_BRDN_COST
3173 	   , ETC_BRDN_COST
3174          , ETC_RAW_COST
3175          , ETC_LABOR_RAW_COST
3176          , ETC_EQUIP_RAW_COST
3177        , CUSTOM1
3178        , CUSTOM2
3179        , CUSTOM3
3180        , CUSTOM4
3181        , CUSTOM5
3182        , CUSTOM6
3183        , CUSTOM7
3184        , CUSTOM8
3185        , CUSTOM9
3186        , CUSTOM10
3187        , CUSTOM11
3188        , CUSTOM12
3189        , CUSTOM13
3190        , CUSTOM14
3191        , CUSTOM15
3192        , LINE_TYPE
3193        , RATE_DANGLING_FLAG
3194        , TIME_DANGLING_FLAG
3195        -- , START_DATE
3196        -- , END_DATE
3197        , PRG_LEVEL
3198        ,PLAN_TYPE_CODE
3199 	)
3200        SELECT /*+ ordered no_merge(plr) */
3201          g_worker_id  WORKER_ID
3202        , plr.PROJECT_ID
3203        , plr.PROJECT_ORG_ID
3204        , plr.project_ORGANIZATION_ID
3205        , plr.WBS_ELEMENT_ID
3206        , DECODE(plr.time_phased_type_code
3207               , 'N', -1
3208               , pji_time.cal_period_id)  time_id
3209 
3210        , DECODE(plr.time_phased_type_code
3211                , 'P', 32
3212                , 'G', 32
3213                , 'N', 2048
3214                , -1) period_type_id -- period type id...
3215        , DECODE(plr.time_phased_type_code
3216                , 'P', 'P'
3217                , 'G', 'G'
3218                , 'N', 'A'
3219                , 'X')     --   CALENDAR_TYPE
3220        , 'L' RBS_AGGR_LEVEL
3221        , 'N' WBS_ROLLUP_FLAG
3222        , 'N' PRG_ROLLUP_FLAG
3223        , plr.CURR_RECORD_TYPE  -- curr code missing.
3224        , plr.currency_code
3225        , plr.RBS_ELEMENT_ID
3226        , plr.RBS_STRUCT_VERSION_ID
3227        , plr.BUDGET_VERSION_ID
3228        , plr.plan_type_id
3229        , SUM(plr.RAW_COST)
3230        , SUM(plr.BRDN_COST)
3231        , SUM(plr.REVENUE)
3232        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )  BILL_RAW_COST
3233        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )   BILL_BRDN_COST
3234        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YPEOPLE', plr.raw_cost, 0 ) )  BILL_LABOR_RAW_COST
3235        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YPEOPLE' , plr.BRDN_COST, 0 ) )   BILL_LABOR_BRDN_COST
3236        --, SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'YPEOPLE', plr.quantity, 0 ) )  BILL_LABOR_HRS  -- bug 6039785
3237        , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'YPEOPLE',
3238                                              DECODE ( plr.billable_flag , 'Y' , plr.quantity, 0 ) , 0 ) )  BILL_LABOR_HRS  -- bug 6039785
3239        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code , plr.raw_cost, 0 ) )  EQUIPMENT_RAW_COST
3240        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code , plr.BRDN_COST, 0 ) )   EQUIPMENT_BRDN_COST
3241        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )      CAPITALIZABLE_RAW_COST
3242        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )      CAPITALIZABLE_BRDN_COST
3243        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.raw_cost, 0 ) )  LABOR_RAW_COST
3244        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.BRDN_COST, 0 ) )   LABOR_BRDN_COST
3245        /* , SUM ( DECODE ( plr.resource_class, g_people_resclass_code,
3246                                             DECODE ( plr.wp_flag, 'N',
3247                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
3248                                                                    plr.quantity),
3249                                                     0 ) )   labor_hrs */ -- bug 6039785
3250        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code,
3251                                             DECODE( plr.billable_flag , 'Y' ,
3252                                                     DECODE ( plr.wp_flag, 'N',
3253                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
3254                                                                    plr.quantity),
3255 					     0 ),
3256                                                     0 ) )   labor_hrs -- bug 6039785
3257        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.revenue, 0 ) )  LABOR_REVENUE
3258        /* , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code,
3259                                             DECODE ( plr.wp_flag, 'N',
3260                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
3261                                                                    plr.quantity),
3262 
3263                                             0 ) )  EQUIPMENT_HOURS */ -- bug 6039785
3264        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code,
3265                                             DECODE( plr.billable_flag , 'Y' ,
3266                                                     DECODE ( plr.wp_flag, 'N',
3267                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
3268                                                                    plr.quantity),
3269 					     0 ),
3270                                                     0 ) )  EQUIPMENT_HOURS -- bug 6039785
3271        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YEQUIPMENT', plr.quantity, 0 ) )  BILLABLE_EQUIPMENT_HOURS */ -- bug 6039785
3272        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YEQUIPMENT',
3273                                                                  DECODE ( plr.billable_flag , 'Y' , plr.quantity, 0 ) , 0 ) )  BILLABLE_EQUIPMENT_HOURS -- bug 6039785
3274        , NULL  SUP_INV_COMMITTED_COST
3275        , NULL  PO_COMMITTED_COST
3276        , NULL  PR_COMMITTED_COST
3277        , NULL  OTH_COMMITTED_COST
3278        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
3279                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
3280                                                                                 0 ) ) ) ACT_LABOR_HRS */ -- bug 6039785
3281        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
3282                                                                               DECODE ( plr.billable_flag , 'Y',
3283                                                                                       DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
3284 									        0 ),
3285                                                                                 0 ) ) ) ACT_LABOR_HRS -- bug 6039785
3286        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
3287                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
3288                                                                                 0 ) ) ) ACT_EQUIP_HOURS */ -- bug 6039785
3289        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
3290                                                                               DECODE ( plr.billable_flag , 'Y',
3291                                                                                        DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
3292 									        0 ),
3293                                                                                 0 ) ) ) ACT_EQUIP_HOURS -- bug 6039785
3294        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.actual_BRDN_COST, 0 ) ) ) ACT_LABOR_BRDN_COST
3295        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.actual_BRDN_COST, 0 ) ) ) ACT_EQUIPMENT_BRDN_COST
3296        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_brdn_cost ) ) ACT_BRDN_COST
3297        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_raw_cost ) ) ACT_RAW_COST
3298        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_revenue ) ) ACT_REVENUE
3299        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.actual_RAW_COST, 0 ) ) ) ACT_LABOR_RAW_COST
3300        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.actual_RAW_COST, 0 ) ) ) ACT_EQUIPMENT_RAW_COST
3301        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
3302                                                                                 DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
3303                                                                                 0 ) ) ) ETC_LABOR_HRS */ -- bug 6039785
3304        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
3305                                                                              DECODE ( plr.billable_flag , 'Y',
3306                                                                                      DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
3307 									        0 ),
3308                                                                                 0 ) ) ) ETC_LABOR_HRS  -- bug 6039785
3309        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
3310                                                                                 DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
3311                                                                                 0 ) ) ) ETC_EQUIP_HOURS */ -- bug 6039785
3312        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
3313                                                                              DECODE ( plr.billable_flag , 'Y',
3314                                                                                       DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
3315 									        0 ),
3316                                                                                 0 ) ) ) ETC_EQUIP_HOURS -- bug 6039785
3317        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.etc_BRDN_COST, 0 ) ) ) ETC_LABOR_BRDN_COST
3318        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.etc_BRDN_COST, 0 ) ) ) ETC_EQUIP_BRDN_COST
3319        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.etc_brdn_cost ) ) ETC_BRDN_COST
3320        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.etc_raw_cost ) ) ETC_RAW_COST
3321        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.etc_raw_COST, 0 ) ) ) ETC_LABOR_raw_COST
3322        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.etc_raw_COST, 0 ) ) ) ETC_EQUIP_raw_COST
3323        , NULL CUSTOM1
3324        , NULL CUSTOM2
3325        , NULL CUSTOM3
3326        , NULL CUSTOM4
3327        , NULL CUSTOM5
3328        , NULL CUSTOM6
3329        , NULL CUSTOM7
3330        , NULL CUSTOM8
3331        , NULL CUSTOM9
3332        , NULL CUSTOM10
3333        , NULL CUSTOM11
3334        , NULL CUSTOM12
3335        , NULL CUSTOM13
3336        , NULL CUSTOM14
3337        , NULL CUSTOM15
3338        , DECODE(plr.time_phased_type_code, 'P', 'OF', 'G', 'OF', 'N', 'NTR', 'X') -- LINE_TYPE
3339        , NULL time_dangling_flag
3340        , NULL rate_dangling_flag
3341        -- , plr.start_date
3342   	 -- , plr.end_date
3343        , g_default_prg_level prg_level
3344        ,plr.PLAN_TYPE_CODE   PLAN_TYPE_CODE    /*4471527 */
3345        FROM
3346        (          ----- First inline view plr .............
3347             select /*+ no_merge(collapse_bl) */
3348               collapse_bl.PROJECT_ID      -- , 1 partition_id
3349             , collapse_bl.WBS_ELEMENT_ID  -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
3350             , SUM(collapse_bl.CURR_RECORD_TYPE_ID) CURR_RECORD_TYPE
3351             , collapse_bl.RBS_ELEMENT_ID
3352             , collapse_bl.RBS_STRUCT_VERSION_ID
3353 		    , collapse_bl.plan_type_id
3354             , collapse_bl.BUDGET_VERSION_ID
3355             , collapse_bl.PROJECT_ORGANIZATION_ID
3356             , collapse_bl.PROJECT_ORG_ID
3357             , collapse_bl.RESOURCE_ASSIGNMENT_ID
3358             , collapse_bl.BILLABLE_FLAG
3359             , collapse_bl.RESOURCE_CLASS  -- , CALENDAR_TYPE -- , CALENDAR_ID
3360             , collapse_bl.TIME_PHASED_TYPE_CODE
3361             , collapse_bl.CURRENCY_CODE
3362             , max(collapse_bl.raw_cost) raw_cost
3363             , max(collapse_bl.BRDN_COST) BRDN_COST
3364             , max(collapse_bl.revenue) revenue
3365             , max(collapse_bl.actual_raw_cost) actual_raw_cost
3366             , max(collapse_bl.actual_BRDN_COST) actual_BRDN_COST
3367             , max(collapse_bl.actual_revenue) actual_revenue
3368             , max(collapse_bl.etc_raw_cost) etc_raw_cost
3369             , max(collapse_bl.etc_BRDN_COST) etc_BRDN_COST
3370             , max(collapse_bl.etc_revenue) etc_revenue
3371             , max(collapse_bl.quantity) quantity
3372             , max(collapse_bl.actual_quantity) actual_quantity
3373             , max(collapse_bl.etc_quantity) etc_quantity
3374             , collapse_bl.start_date start_date
3375             , collapse_bl.end_date   end_date
3376             , collapse_bl.period_name period_name  -- , TRACK_AS_LABOR_FLAG track_as_labor_flag
3377             , collapse_bl.row_id
3378             , collapse_bl.plan_type_code
3379             , collapse_bl.WP_FLAG
3380             from
3381               (                  ----- Second inline view 'collapse_bl' begin .............
3382                select /*+ no_merge(spread_bl) */
3383                   spread_bl.row_id row_id
3384                 , spread_bl.PROJECT_ID
3385                 , spread_bl.BUDGET_VERSION_ID
3386                 , spread_bl.RESOURCE_ASSIGNMENT_ID
3387                 , spread_bl.WBS_ELEMENT_ID
3388                 , spread_bl.RBS_ELEMENT_ID
3389                 , spread_bl.WBS_STRUCT_VERSION_ID
3390                 , spread_bl.RBS_STRUCT_VERSION_ID
3391   	 	        , spread_bl.plan_type_id
3392                 , spread_bl.BILLABLE_FLAG
3393                 , spread_bl.RESOURCE_CLASS  -- , spread_bl.CALENDAR_TYPE  -- , pji_time.CALENDAR_ID
3394                 , spread_bl.PROJECT_ORGANIZATION_ID
3395                 , spread_bl.PROJECT_ORG_ID
3396                 , spread_bl.TIME_PHASED_TYPE_CODE
3397                 , DECODE( invert.INVERT_ID
3398                         , 4, spread_bl.func_CURRENCY_CODE
3399                         , 8, spread_bl.PRJ_CURRENCY_CODE
3400                         , 16, spread_bl.TXN_CURRENCY_CODE ) CURRENCY_CODE
3401                 , invert.INVERT_ID CURR_RECORD_TYPE_ID
3402                 , DECODE ( invert.INVERT_ID
3403                          , 4, spread_bl.func_raw_cost
3404                          , 8, spread_bl.PRJ_raw_cost
3405                          , 16, spread_bl.TXN_raw_cost) raw_cost
3406                 , DECODE ( invert.INVERT_ID
3407                          , 4, spread_bl.func_BRDN_COST
3408                          , 8, spread_bl.PRJ_BRDN_COST
3409                          , 16, spread_bl.TXN_BRDN_COST ) BRDN_COST
3410                 , DECODE ( invert.INVERT_ID
3411                          , 4, spread_bl.func_revenue
3412                          , 8, spread_bl.PRJ_revenue
3413                          , 16, spread_bl.TXN_revenue ) revenue
3414 				, DECODE ( invert.INVERT_ID
3415 				         , 4, spread_bl.func_actual_raw_cost
3416 						 , 8, spread_bl.prj_actual_raw_cost
3417 						 , 16, spread_bl.txn_actual_raw_cost ) actual_raw_cost
3418 				, DECODE ( invert.INVERT_ID
3419 				         , 4, spread_bl.func_actual_brdn_cost
3420 						 , 8, spread_bl.prj_actual_brdn_cost
3421 						 , 16, spread_bl.txn_actual_brdn_cost ) actual_brdn_cost
3422 				, DECODE ( invert.INVERT_ID
3423 				         , 4, spread_bl.func_actual_revenue
3424 						 , 8, spread_bl.prj_actual_revenue
3425 						 , 16, spread_bl.txn_actual_revenue ) actual_revenue
3426 				, DECODE ( invert.INVERT_ID
3427 				         , 4, spread_bl.func_etc_raw_cost
3428 						 , 8, spread_bl.prj_etc_raw_cost
3429 						 , 16, spread_bl.txn_etc_raw_cost ) etc_raw_cost
3430 				, DECODE ( invert.INVERT_ID
3431 				         , 4, spread_bl.func_etc_brdn_cost
3432 						 , 8, spread_bl.prj_etc_brdn_cost
3433 						 , 16, spread_bl.txn_etc_brdn_cost ) etc_brdn_cost
3434 				, DECODE ( invert.INVERT_ID
3435 				         , 4, spread_bl.func_etc_revenue
3436 						 , 8, spread_bl.prj_etc_revenue
3437 						 , 16, spread_bl.txn_etc_revenue ) etc_revenue
3438             	, spread_bl.quantity quantity
3439             	, spread_bl.actual_quantity actual_quantity
3440             	, spread_bl.etc_quantity etc_quantity
3441             	, spread_bl.start_date start_date
3442             	, spread_bl.end_date   end_date
3443             	, spread_bl.period_name period_name
3444             	-- , spread_bl.TRACK_AS_LABOR_FLAG track_as_labor_flag
3445                              , spread_bl.plan_type_code
3446                 , spread_bl.WP_FLAG
3447                 from
3448                   (     ----- Third inline view 'spread_bl'  .............
3449                     SELECT /*+ ordered */
3450                 	  ra.project_id
3451                 	, bl.rowid row_id
3452                 	, ra.budget_version_id
3453                 	, bl.resource_asSIGNment_id
3454                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
3455                 	, NVL(ra.rbs_element_id, -1)              rbs_element_id
3456                 	, ver.wbs_struct_version_id      wbs_struct_version_id
3457                 	, NVL(ver.rbs_struct_version_id, -1)      rbs_struct_version_id
3458 			, ver.plan_type_id               plan_type_id
3459 			, ra.rate_based_flag             billable_flag
3460 			, ra.resource_class_code         resource_class
3461                 	, bl.txn_currency_code           txn_currency_code
3462                 	, bl.txn_raw_cost                txn_raw_cost
3463                 	, bl.txn_burdened_COST           txn_brdn_COST
3464                 	, bl.txn_revenue                 txn_revenue
3465 			, DECODE(ver.wp_flag, 'N', bl.txn_init_raw_cost, NULL)                txn_actual_raw_cost  -- new
3466 			, DECODE(ver.wp_flag, 'N', bl.txn_init_burdened_cost, NULL)             txn_actual_brdn_cost  -- new
3467 			, DECODE(ver.wp_flag, 'N', bl.txn_init_revenue, NULL)                   txn_actual_revenue  -- new
3468 			, DECODE(ver.wp_flag, 'N', (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0)), NULL) txn_etc_raw_cost     -- new
3469 			, DECODE(ver.wp_flag, 'N', (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0)), NULL) txn_etc_brdn_cost     -- new
3470 			, DECODE(ver.wp_flag, 'N', (bl.txn_revenue - NVL(bl.txn_init_revenue, 0)), NULL) txn_etc_revenue     -- new
3471                 	, bl.project_currency_code          prj_currency_code
3472                 	, bl.project_raw_cost               prj_raw_cost
3473                 	, bl.project_burdened_COST          prj_BRDN_COST
3474                 	, bl.project_revenue                prj_revenue
3475 			, DECODE(ver.wp_flag, 'N', bl.project_init_raw_cost, NULL)          prj_actual_raw_cost  -- new
3476 			, DECODE(ver.wp_flag, 'N', bl.project_init_burdened_cost, NULL)     prj_actual_brdn_cost  -- new
3477 			, DECODE(ver.wp_flag, 'N', bl.project_init_revenue, NULL)           prj_actual_revenue  -- new
3478 			, DECODE(ver.wp_flag, 'N', (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0)), NULL) prj_etc_raw_cost     -- new
3479 			, DECODE(ver.wp_flag, 'N', (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0)), NULL) prj_etc_brdn_cost     -- new
3480 			, DECODE(ver.wp_flag, 'N', (bl.project_revenue - NVL(bl.project_init_revenue, 0)), NULL) prj_etc_revenue     -- new
3481                 	, bl.projfunc_currency_code         func_currency_code
3482                   , bl.raw_cost                       func_raw_cost
3483                 	, bl.burdened_COST                  func_BRDN_COST
3484                 	, bl.revenue                        func_revenue
3485 			, DECODE(ver.wp_flag, 'N', bl.init_raw_cost , NULL)                 func_actual_raw_cost  -- new
3486 			, DECODE(ver.wp_flag, 'N', bl.init_burdened_cost , NULL)            func_actual_brdn_cost  -- new
3487 			, DECODE(ver.wp_flag, 'N', bl.init_revenue , NULL)                  func_actual_revenue  -- new
3488 			, DECODE(ver.wp_flag, 'N', (bl.raw_cost - NVL(bl.init_raw_cost, 0)), NULL) func_etc_raw_cost     -- new
3489 			, DECODE(ver.wp_flag, 'N', (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)), NULL) func_etc_brdn_cost     -- new
3490 			, DECODE(ver.wp_flag, 'N', (bl.revenue - NVL(bl.init_revenue, 0)), NULL) func_etc_revenue     -- new
3491                 	, g_global1_currency_code        glb1_currency_code
3492                   , NULL                           glb1_raw_cost
3493                 	, NULL                           glb1_BRDN_COST
3494                 	, NULL                           glb1_revenue
3495                 	, g_global2_currency_code        glb2_currency_code
3496                   , NULL                           glb2_raw_cost
3497                 	, NULL                           glb1_BRDN_COST
3498                 	, NULL                           glb1_revenue
3499                 	, bl.quantity                       quantity
3500 			, DECODE(ver.wp_flag, 'N', bl.init_quantity, NULL)                  actual_quantity  -- new
3501 			, DECODE(ver.wp_flag, 'N', (bl.quantity - NVL(bl.init_quantity, 0)), NULL) etc_quantity  -- new
3502                 	, bl.start_date
3503                 	, bl.END_date
3504                 	, NVL(bl.period_name, g_ntp_period_name)  period_name
3505                 	, ver.time_phased_type_code time_phased_type_code
3506                 	, ppa.org_id project_org_id
3507                 	, ppa.carrying_out_organization_id project_organization_id
3508 	, ver.plan_type_code   /*4471527 */
3509                         , ver.WP_FLAG
3510                  FROM
3511                      PJI_FM_EXTR_PLNVER4           ver
3512                    , pa_resource_asSIGNments       ra
3513                    , PA_BUDGET_LINES               bl
3514                    , pa_projects_all               ppa
3515                    , PA_PROJ_ELEM_VER_STRUCTURE    pevs
3516                  WHERE
3517                          ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
3518                      AND ra.project_id = ver.PROJECT_ID
3519                      AND ra.budget_version_id = ver.plan_version_id
3520                      AND ver.project_id = ppa.project_id
3521                      AND txn_currency_code IS NOT NULL
3522                      AND bl.project_currency_code IS NOT NULL
3523                      AND bl.projfunc_currency_code IS NOT NULL
3524 			   AND pevs.element_version_id = ver.wbs_struct_version_id
3525 			   AND pevs.project_id = ver.project_id
3526 			   AND ver.worker_id = g_worker_id
3527                      AND    ver.time_phased_type_code IN ('P', 'G', 'N')
3528 				  ) spread_bl
3529 				   ---- end of third inline view 'spread_bl'...........
3530             	  ,
3531             	    (
3532                       select 4   INVERT_ID from dual union all
3533                       select 8   INVERT_ID from dual union all
3534                       select 16  INVERT_ID from dual where PJI_UTILS.GET_SETUP_PARAMETER('TXN_CURR_FLAG') = 'Y'
3535                     ) invert
3536 				)  collapse_bl
3537 				----  End of second inline view 'collapse_bl' ..........
3538 			GROUP BY
3539               collapse_bl.PROJECT_ID
3540             , collapse_bl.WBS_ELEMENT_ID
3541             -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
3542             , collapse_bl.RBS_ELEMENT_ID
3543             , collapse_bl.RBS_STRUCT_VERSION_ID
3544 			, collapse_bl.plan_type_id
3545             , collapse_bl.BUDGET_VERSION_ID
3546             , collapse_bl.PROJECT_ORGANIZATION_ID
3547             , collapse_bl.PROJECT_ORG_ID
3548             , collapse_bl.RESOURCE_ASSIGNMENT_ID
3549             , collapse_bl.BILLABLE_FLAG
3550             , collapse_bl.RESOURCE_CLASS
3551             , collapse_bl.TIME_PHASED_TYPE_CODE
3552             , collapse_bl.CURRENCY_CODE
3553             , collapse_bl.start_date
3554             , collapse_bl.end_date
3555             , collapse_bl.period_name
3556             , collapse_bl.row_id
3557            , collapse_bl.plan_type_code
3558             , collapse_bl.WP_FLAG
3559        ) plr
3560 				----  End of first inline view plr ..........
3561        , PJI_ORG_EXTR_INFO            orginfo
3562        , pji_time_cal_period_v    pji_time
3563          WHERE  1=1
3564            -- AND    orginfo.projfunc_currency_mau is not NULL
3565            AND    plr.project_org_id         = orginfo.org_id
3566            AND    DECODE(plr.time_phased_type_code
3567                    , 'P', orginfo.pa_calendar_id
3568                    , 'G', orginfo.gl_calendar_id
3569                    , -l_max_project_id  ) = pji_time.calendar_id
3570            AND    plr.period_name = pji_time.name
3571        GROUP BY
3572          plr.PROJECT_ID
3573        , plr.PROJECT_ORG_ID
3574        , plr.project_ORGANIZATION_ID
3575        , plr.WBS_ELEMENT_ID
3576        , DECODE(plr.time_phased_type_code
3577               , 'N', -1
3578               , pji_time.cal_period_id)
3579        , DECODE(plr.time_phased_type_code
3580                , 'P', 32
3581                , 'G', 32
3582                , 'N', 2048
3583                , -1) -- period type id...
3584        , DECODE(plr.time_phased_type_code
3585                , 'P', 'P'
3586                , 'G', 'G'
3587                , 'N', 'A'
3588                , 'X')     --   CALENDAR_TYPE
3589        , plr.CURR_RECORD_TYPE  -- curr code missing.
3590   	   , plr.currency_code
3591        , plr.RBS_ELEMENT_ID
3592        , plr.RBS_STRUCT_VERSION_ID
3593        , plr.BUDGET_VERSION_ID
3594   	 , plr.plan_type_id
3595        --  , plr.start_date
3596   	 -- , plr.end_date
3597        , DECODE(plr.time_phased_type_code, 'P', 'OF', 'G', 'OF', 'N', 'NTR', 'X')
3598        ,plr.plan_type_code ;
3599 
3600 
3601     print_time('EXTRACT_PLAN_AMOUNTS_PRIRBS : l_count ' || SQL%ROWCOUNT );
3602 
3603     DELETE_NTP_CAL_RECORD ( p_max_project_id => l_max_project_id );
3604 
3605 
3606     print_time('EXTRACT_PLAN_AMOUNTS_PRIRBS : End' );
3607 
3608 EXCEPTION
3609   WHEN OTHERS THEN
3610     print_time('EXTRACT_PLAN_AMOUNTS_PRIRBS : Exception ' || SQLERRM );
3611     print_time('EXTRACT_PLAN_AMOUNTS_PRIRBS : Begin worker id is ... ' || 1);
3612     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
3613                              p_procedure_name => 'EXTRACT_PLAN_AMOUNTS_PRIRBS');
3614     RAISE;
3615 END;
3616 
3617 
3618 
3619 
3620 ----------------------------------------------
3621 --- Overridden ETC Pull apis.
3622 ----------------------------------------------
3623 
3624 PROCEDURE RETRIEVE_OVERRIDDEN_WP_ETC IS
3625 
3626   l_return_status varchar2(1);
3627   l_msg_data varchar2(500);
3628   l_msg_count NUMBER;
3629 
3630   l_project_id NUMBER := 6842; -- 6976; -- 7185
3631   l_latest_pub_str_ver NUMBER := 34269; -- 34951; -- 34982
3632   l_count NUMBER := NULL;
3633 
3634   CURSOR c_lp_struct_ver IS
3635   SELECT ver.project_id, ver.wbs_struct_version_id
3636   FROM   pji_fm_extr_plnver4 ver
3637   WHERE  ver.wp_flag = 'Y'
3638     AND  ver.lp_flag = 'Y'
3639     AND  ver.worker_id = g_worker_id;
3640 
3641 BEGIN
3642 
3643   -- dbms_output.put_line ( ' Begin.. ');
3644 
3645   Pji_Pjp_Fp_Curr_Wrap.INIT_ERR_STACK
3646   ( p_package_name   => g_package_name
3647   , x_return_status  => l_return_status ) ;
3648 
3649   --
3650   -- Why the following code:
3651   -- When RETRIEVE_OVERRIDDEN_WP_ETC is called, we intend to process only overridden ETC values.
3652   -- Currently there is no locking mechanism for either wbs header or the plan lines in this flow.
3653   --  One of the following alternative solutions are considered.
3654   -- a. Implement locking.
3655   -- b.
3656   DELETE FROM pji_fm_extr_plan_lines;
3657   -- c. call PJI_FM_XBS_ACCUM_MAINT.plan_update_pvt; and then call delete plan lines.
3658   -- Going with b for now, for simplicity.
3659   --
3660 
3661 
3662   FOR i IN c_lp_struct_ver LOOP
3663 
3664     BEGIN
3665 
3666       print_time ( ' i.project_id = ' || i.project_id || ' i.wbs_struct_version_id ' || i.wbs_struct_version_id );
3667 
3668       pa_progress_pub.COPY_PROGRESS_ACT_ETC(
3669         p_project_id               => i.project_id
3670       , p_src_str_ver_id           => i.wbs_struct_version_id
3671       , p_dst_str_ver_id           => i.wbs_struct_version_id
3672       , p_pji_conc_prog_context   => 'Y'
3673       , p_calling_context          => 'PJI' -- IN      VARCHAR2        := 'PUBLISH'
3674       -- ,p_copy_actuals_flag        IN      VARCHAR2        := 'Y' -- We want the default value.
3675       -- ,p_copy_ETC_flag            IN      VARCHAR2        := 'Y' -- We want the default value.
3676       , x_return_status            => l_return_status
3677       , x_msg_count                => l_msg_count
3678       , x_msg_data                 => l_msg_data
3679       );
3680 
3681     EXCEPTION
3682       WHEN OTHERS THEN
3683         print_time ( ' RETRIEVE_OVERRIDDEN_WP_ETC ' || SQLERRM );
3684     END;
3685 
3686   END LOOP;
3687 
3688   --
3689   -- Delete plan lines that have no relevant overridden ETC info to avoid unnecessary processing overhead.
3690   --
3691   DELETE FROM pji_fm_extr_plan_lines
3692   WHERE
3693     (PROJECT_ID, PLAN_VERSION_ID) IN
3694 	 (SELECT DISTINCT project_id, plan_version_id
3695 	  FROM pji_fm_extr_plnver4)          AND
3696     ETC_QUANTITY    IS NULL                AND
3697     ETC_TXN_BURDENED_COST IS NULL          AND
3698     ETC_PRJ_BURDENED_COST IS NULL          AND
3699     ETC_PFC_BURDENED_COST IS NULL          AND
3700     ETC_TXN_RAW_COST IS NULL               AND
3701     ETC_PRJ_RAW_COST IS NULL               AND
3702     ETC_PFC_RAW_COST IS NULL;
3703 
3704 
3705   /*
3706   dbms_output.put_line ( ' After call.. l_msg_code ' || l_msg_data || ' l_return_status ' || l_return_status );
3707 
3708   SELECT COUNT(1)
3709   INTO   l_count
3710   FROM   pji_fm_extr_plan_lines
3711   WHERE  project_id = l_project_id;
3712 
3713   dbms_output.put_line ( ' Count in plan lines is.. ' || l_count );
3714   */
3715 
3716 EXCEPTION
3717   WHEN OTHERS THEN
3718     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
3719     ( p_package_name   => g_package_name
3720     , p_procedure_name => 'RETRIEVE_OVERRIDDEN_WP_ETC'
3721     , x_return_status  => l_return_status ) ;
3722 
3723     RAISE;
3724 END;
3725 
3726 
3727 PROCEDURE EXTRACT_PLAN_ETC_PRIRBS (
3728   p_slice_type      IN VARCHAR2 := 'PRI'
3729   -- Valid values are 'PRI' and 'SEC'.
3730   -- 'PRI' for RBS push and Partial refresh
3731   -- 'SEC' for Truncate
3732 ) IS
3733 
3734   l_max_project_id  NUMBER := NULL;
3735   l_sysdate         DATE := SYSDATE;
3736   l_return_status varchar2(1);
3737   l_msg_data varchar2(500);
3738   l_msg_count NUMBER;
3739 
3740 BEGIN
3741 
3742   Pji_Pjp_Fp_Curr_Wrap.INIT_ERR_STACK
3743   ( p_package_name   => g_package_name
3744   , x_return_status  => l_return_status ) ;
3745 
3746   print_time('EXTRACT_PLAN_ETC_PRIRBS : Begin' );
3747   print_time('EXTRACT_PLAN_ETC_PRIRBS : Begin worker id is ... ' || g_worker_id);
3748 
3749   IF (p_slice_type NOT IN ('PRI','SEC')) THEN
3750     print_time('EXTRACT_PLAN_ETC_PRIRBS : invalid p_slice_type ' || p_slice_type );
3751     RETURN;
3752   END IF;
3753 
3754 
3755   INSERT_NTP_CAL_RECORD ( x_max_project_id => l_max_project_id );
3756 
3757   IF (l_max_project_id IS NULL) THEN
3758     RETURN;
3759   END IF;
3760 
3761 
3762      INSERT INTO PJI_FP_AGGR_PJP1
3763     (
3764          WORKER_ID
3765        , PROJECT_ID
3766        , PROJECT_ORG_ID
3767        , PROJECT_ORGANIZATION_ID
3768        , PROJECT_ELEMENT_ID
3769        , TIME_ID
3770        , PERIOD_TYPE_ID
3771        , CALENDAR_TYPE
3772        , RBS_AGGR_LEVEL
3773        , WBS_ROLLUP_FLAG
3774        , PRG_ROLLUP_FLAG
3775        , CURR_RECORD_TYPE_ID
3776        , CURRENCY_CODE
3777        , RBS_ELEMENT_ID
3778        , RBS_VERSION_ID
3779        , PLAN_VERSION_ID
3780        , PLAN_TYPE_ID
3781        , RAW_COST
3782        , BRDN_COST
3783        , REVENUE
3784        , BILL_RAW_COST
3785        , BILL_BRDN_COST
3786        , BILL_LABOR_RAW_COST
3787        , BILL_LABOR_BRDN_COST
3788        , BILL_LABOR_HRS
3789        , EQUIPMENT_RAW_COST
3790        , EQUIPMENT_BRDN_COST
3791        , CAPITALIZABLE_RAW_COST
3792        , CAPITALIZABLE_BRDN_COST
3793        , LABOR_RAW_COST
3794        , LABOR_BRDN_COST
3795        , LABOR_HRS
3796        , LABOR_REVENUE
3797        , EQUIPMENT_HOURS
3798        , BILLABLE_EQUIPMENT_HOURS
3799        , SUP_INV_COMMITTED_COST
3800        , PO_COMMITTED_COST
3801        , PR_COMMITTED_COST
3802        , OTH_COMMITTED_COST
3803        , ACT_LABOR_HRS
3804 	   , ACT_EQUIP_HRS
3805 	   , ACT_LABOR_BRDN_COST
3806 	   , ACT_EQUIP_BRDN_COST
3807 	   , ACT_BRDN_COST
3808 	   , ACT_RAW_COST
3809 	   , ACT_REVENUE
3810          , ACT_LABOR_RAW_COST
3811          , ACT_EQUIP_RAW_COST
3812 	   , ETC_LABOR_HRS
3813 	   , ETC_EQUIP_HRS
3814 	   , ETC_LABOR_BRDN_COST
3815 	   , ETC_EQUIP_BRDN_COST
3816 	   , ETC_BRDN_COST
3817          , ETC_RAW_COST
3818          , ETC_LABOR_RAW_COST
3819          , ETC_EQUIP_RAW_COST
3820        , CUSTOM1
3821        , CUSTOM2
3822        , CUSTOM3
3823        , CUSTOM4
3824        , CUSTOM5
3825        , CUSTOM6
3826        , CUSTOM7
3827        , CUSTOM8
3828        , CUSTOM9
3829        , CUSTOM10
3830        , CUSTOM11
3831        , CUSTOM12
3832        , CUSTOM13
3833        , CUSTOM14
3834        , CUSTOM15
3835        , LINE_TYPE
3836        , RATE_DANGLING_FLAG
3837        , TIME_DANGLING_FLAG
3838        , PRG_LEVEL
3839        , PLAN_TYPE_CODE
3840 	)
3841 	   SELECT /*+ ordered no_merge(plr) */
3842          g_worker_id  WORKER_ID
3843        , plr.PROJECT_ID
3844        , plr.PROJECT_ORG_ID
3845        , plr.project_ORGANIZATION_ID
3846        , plr.WBS_ELEMENT_ID project_element_id
3847        , DECODE(plr.time_phased_type_code
3848               , 'N', -1
3849               , pji_time.cal_period_id)  time_id
3850        , DECODE(plr.time_phased_type_code
3851                , 'P', 32
3852                , 'G', 32
3853                , 'N', 2048
3854                , -1) period_type_id -- period type id...
3855        , DECODE(plr.time_phased_type_code
3856                , 'P', 'P'
3857                , 'G', 'G'
3858                , 'N', 'A'
3859                , 'X')    CALENDAR_TYPE
3860        , 'L' RBS_AGGR_LEVEL
3861        , 'N' WBS_ROLLUP_FLAG
3862        , 'N' PRG_ROLLUP_FLAG
3863        , plr.CURR_RECORD_TYPE  CURR_RECORD_TYPE_id
3864        , plr.currency_code
3865        , plr.RBS_ELEMENT_ID
3866        , plr.RBS_STRUCT_VERSION_ID  RBS_VERSION_ID
3867        , plr.BUDGET_VERSION_ID plan_version_id
3868        , plr.plan_type_id
3869        , SUM(plr.RAW_COST) RAW_COST
3870        , SUM(plr.BRDN_COST) BRDN_COST
3871        , SUM(plr.REVENUE) REVENUE
3872        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )  BILL_RAW_COST
3873        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )   BILL_BRDN_COST
3874        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YPEOPLE', plr.raw_cost, 0 ) )  BILL_LABOR_RAW_COST
3875        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YPEOPLE' , plr.BRDN_COST, 0 ) )   BILL_LABOR_BRDN_COST
3876        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'YPEOPLE', plr.quantity, 0 ) )  BILL_LABOR_HRS */ -- bug 6039785
3877        , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'YPEOPLE',
3878                                              DECODE ( plr.billable_flag , 'Y' , plr.quantity , 0 ) , 0 ) )  BILL_LABOR_HRS -- bug 6039785
3879        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.raw_cost, 0 ) )  EQUIPMENT_RAW_COST
3880        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.BRDN_COST, 0 ) )   EQUIPMENT_BRDN_COST
3881        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )      CAPITALIZABLE_RAW_COST
3882        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )      CAPITALIZABLE_BRDN_COST
3883        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.raw_cost, 0 ) )  LABOR_RAW_COST
3884        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.BRDN_COST, 0 ) )   LABOR_BRDN_COST
3885        /* , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.quantity, 0 ) )   labor_hrs */ -- bug 6039785
3886        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', DECODE(plr.billable_flag , 'Y', plr.quantity,0),
3887                                                     0 ) )   labor_hrs  -- bug 6039785
3888        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.revenue, 0 ) )  LABOR_REVENUE
3889        /* , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.quantity, 0 ) )  EQUIPMENT_HOURS */ -- bug 6039785
3890        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', DECODE(plr.billable_flag , 'Y', plr.quantity, 0),
3891                                                     0 ) )  EQUIPMENT_HOURS -- bug 6039785
3892        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YEQUIPMENT', plr.quantity, 0 ) )  BILLABLE_EQUIPMENT_HOURS */ -- bug 6039785
3893        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YEQUIPMENT', DECODE(plr.billable_flag , 'Y', plr.quantity, 0),
3894                                                     0 ) )  BILLABLE_EQUIPMENT_HOURS -- bug 6039785
3895        , TO_NUMBER(NULL)                SUP_INV_COMMITTED_COST
3896        , TO_NUMBER(NULL)                PO_COMMITTED_COST
3897        , TO_NUMBER(NULL)                PR_COMMITTED_COST
3898        , TO_NUMBER(NULL)                OTH_COMMITTED_COST
3899        /* , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_quantity, 0 ) ) ACT_LABOR_HRS */ -- bug 6039785
3900        , SUM ( DECODE ( plr.resource_class, 'PEOPLE' , DECODE (plr.billable_flag ,'Y', plr.actual_quantity,0),
3901                                                                                 0 ) ) ACT_LABOR_HRS -- bug 6039785
3902        /* , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.actual_quantity, 0 ) ) ACT_EQUIP_HRS */ -- bug 6039785
3903        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', DECODE (plr.billable_flag ,'Y', plr.actual_quantity, 0),
3904                                                                                 0 ) ) ACT_EQUIP_HRS -- bug 6039785
3905        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_BRDN_COST, 0 ) ) ACT_LABOR_BRDN_COST
3906        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.actual_BRDN_COST, 0 ) ) ACT_EQUIP_BRDN_COST
3907        , SUM ( plr.actual_brdn_cost ) ACT_BRDN_COST
3908        , SUM ( plr.actual_raw_cost ) ACT_RAW_COST
3909        , SUM ( plr.actual_revenue ) ACT_REVENUE
3910        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_RAW_COST, 0 ) ) ACT_LABOR_RAW_COST
3911        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.actual_RAW_COST, 0 ) ) ACT_EQUIP_RAW_COST
3912        /* , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.etc_quantity, 0 ) ) ETC_LABOR_HRS */ -- bug 6039785
3913        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', DECODE (plr.billable_flag , 'Y' , plr.ETC_quantity,0),
3914                                                                                 0 ) ) ETC_LABOR_HRS -- bug 6039785
3915        /* , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.etc_quantity, 0 ) ) ETC_EQUIP_HRS */ -- bug 6039785
3916        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', DECODE (plr.billable_flag , 'Y' , plr.etc_quantity, 0) ,
3917                                                                                 0 ) ) ETC_EQUIP_HRS -- bug 6039785
3918        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.etc_BRDN_COST, 0 ) ) ETC_LABOR_BRDN_COST
3919        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.etc_BRDN_COST, 0 ) ) ETC_EQUIP_BRDN_COST
3920        , SUM(plr.etc_BRDN_COST) ETC_BRDN_COST
3921        , SUM(plr.etc_RAW_COST) ETC_RAW_COST
3922        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.etc_raw_cost, 0 ) ) ETC_LABOR_raw_COST
3923        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.etc_raw_cost, 0 ) ) ETC_EQUIP_raw_COST
3924        , NULL CUSTOM1
3925        , NULL CUSTOM2
3926        , NULL CUSTOM3
3927        , NULL CUSTOM4
3928        , NULL CUSTOM5
3929        , NULL CUSTOM6
3930        , NULL CUSTOM7
3931        , NULL CUSTOM8
3932        , NULL CUSTOM9
3933        , NULL CUSTOM10
3934        , NULL CUSTOM11
3935        , NULL CUSTOM12
3936        , NULL CUSTOM13
3937        , NULL CUSTOM14
3938        , NULL CUSTOM15
3939        , DECODE(plr.time_phased_type_code, 'P', 'OF', 'G', 'OF', 'N', 'NTR', 'X') LINE_TYPE
3940        , NULL time_dangling_flag
3941        , NULL rate_dangling_flag
3942        , g_default_prg_level prg_level
3943        , plr.PLAN_TYPE_CODE
3944        FROM
3945        (          ----- First inline view plr .............
3946             SELECT /*+ no_merge(collapse_bl) */
3947               collapse_bl.PROJECT_ID      -- , 1 partition_id
3948             , collapse_bl.WBS_ELEMENT_ID  -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
3949             , SUM(collapse_bl.CURR_RECORD_TYPE_ID) CURR_RECORD_TYPE
3950             , collapse_bl.RBS_ELEMENT_ID
3951             , collapse_bl.RBS_STRUCT_VERSION_ID
3952 		    , collapse_bl.plan_type_id
3953             , collapse_bl.BUDGET_VERSION_ID
3954             , collapse_bl.PROJECT_ORGANIZATION_ID
3955             , collapse_bl.PROJECT_ORG_ID
3956             , collapse_bl.BILLABLE_FLAG
3957             , collapse_bl.RESOURCE_CLASS  -- , CALENDAR_TYPE -- , CALENDAR_ID
3958             , collapse_bl.CURRENCY_CODE
3959             , MAX(collapse_bl.raw_cost) raw_cost
3960             , MAX(collapse_bl.BRDN_COST) BRDN_COST
3961             , MAX(collapse_bl.revenue) revenue
3962             , MAX(collapse_bl.actual_raw_cost) actual_raw_cost
3963             , MAX(collapse_bl.actual_BRDN_COST) actual_BRDN_COST
3964             , MAX(collapse_bl.actual_revenue) actual_revenue
3965             , MAX(collapse_bl.etc_raw_cost) etc_raw_cost
3966             , MAX(collapse_bl.etc_BRDN_COST) etc_BRDN_COST
3967             , MAX(collapse_bl.quantity) quantity
3968             , MAX(collapse_bl.actual_quantity) actual_quantity
3969             , MAX(collapse_bl.etc_quantity) etc_quantity
3970             , collapse_bl.period_name period_name  -- , TRACK_AS_LABOR_FLAG track_as_labor_flag
3971             , collapse_bl.row_id
3972             , collapse_bl.plan_type_code
3973             , collapse_bl.time_phased_type_code
3974             FROM
3975               (                  ----- Second inline view 'collapse_bl' begin .............
3976                SELECT /*+ no_merge(spread_bl) */
3977                   spread_bl.row_id row_id
3978                 , spread_bl.PROJECT_ID
3979                 , spread_bl.BUDGET_VERSION_ID
3980                 , spread_bl.WBS_ELEMENT_ID
3981                 , spread_bl.RBS_ELEMENT_ID
3982                 , spread_bl.WBS_STRUCT_VERSION_ID
3983                 , spread_bl.RBS_STRUCT_VERSION_ID
3984   	 	        , spread_bl.plan_type_id
3985                 , spread_bl.BILLABLE_FLAG
3986                 , spread_bl.RESOURCE_CLASS  -- , spread_bl.CALENDAR_TYPE  -- , pji_time.CALENDAR_ID
3987                 , spread_bl.PROJECT_ORGANIZATION_ID
3988                 , spread_bl.PROJECT_ORG_ID
3989                 , DECODE( invert.INVERT_ID
3990                         , 4, spread_bl.func_CURRENCY_CODE
3991                         , 8, spread_bl.PRJ_CURRENCY_CODE
3992                         , 16, spread_bl.TXN_CURRENCY_CODE ) CURRENCY_CODE
3993                 , invert.INVERT_ID CURR_RECORD_TYPE_ID
3994                 , DECODE ( invert.INVERT_ID
3995                          , 4, spread_bl.func_raw_cost
3996                          , 8, spread_bl.PRJ_raw_cost
3997                          , 16, spread_bl.TXN_raw_cost) raw_cost
3998                 , DECODE ( invert.INVERT_ID
3999                          , 4, spread_bl.func_BRDN_COST
4000                          , 8, spread_bl.PRJ_BRDN_COST
4001                          , 16, spread_bl.TXN_BRDN_COST ) BRDN_COST
4002                 , DECODE ( invert.INVERT_ID
4003                          , 4, spread_bl.func_revenue
4004                          , 8, spread_bl.PRJ_revenue
4005                          , 16, spread_bl.TXN_revenue ) revenue
4006                 , DECODE ( invert.INVERT_ID
4007                          , 4, spread_bl.func_actual_raw_cost
4008                          , 8, spread_bl.prj_actual_raw_cost
4009                          , 16, spread_bl.txn_actual_raw_cost ) actual_raw_cost
4010 				, DECODE ( invert.INVERT_ID
4011 				         , 4, spread_bl.func_actual_brdn_cost
4012 						 , 8, spread_bl.prj_actual_brdn_cost
4013 						 , 16, spread_bl.txn_actual_brdn_cost ) actual_brdn_cost
4014 				, DECODE ( invert.INVERT_ID
4015 				         , 4, spread_bl.func_actual_revenue
4016 						 , 8, spread_bl.prj_actual_revenue
4017 						 , 16, spread_bl.txn_actual_revenue ) actual_revenue
4018 				, DECODE ( invert.INVERT_ID
4019 				         , 4, spread_bl.func_etc_raw_cost
4020 						 , 8, spread_bl.prj_etc_raw_cost
4021 						 , 16, spread_bl.txn_etc_raw_cost ) etc_raw_cost
4022 				, DECODE ( invert.INVERT_ID
4023 				         , 4, spread_bl.func_etc_brdn_cost
4024 						 , 8, spread_bl.prj_etc_brdn_cost
4025 						 , 16, spread_bl.txn_etc_brdn_cost ) etc_brdn_cost
4026             	, spread_bl.quantity quantity
4027             	, spread_bl.actual_quantity actual_quantity
4028             	, spread_bl.etc_quantity etc_quantity
4029             	, spread_bl.period_name period_name
4030                               ,spread_bl.plan_type_code
4031                 , spread_bl.time_phased_type_code
4032                 FROM
4033                   (     ----- Third inline view 'spread_bl'  .............
4034                     SELECT /*+ ordered */
4035                 	  ra.project_id
4036                 	, bl.ROWID row_id
4037                 	, ra.budget_version_id
4038                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
4039                 	, NVL(ra.rbs_element_id, -1)              rbs_element_id
4040                 	, ver.wbs_struct_version_id      wbs_struct_version_id
4041                 	, NVL(ver.rbs_struct_version_id, -1)      rbs_struct_version_id
4042 			, ver.plan_type_id               plan_type_id
4043 			, ra.rate_based_flag             billable_flag
4044 			, ra.resource_class_code         resource_class
4045                 	, bl.txn_currency_code           txn_currency_code
4046                 	, bl.txn_raw_cost                txn_raw_cost
4047                 	, bl.txn_burdened_COST           txn_brdn_COST
4048                 	, bl.txn_revenue                 txn_revenue
4049 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4050                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.txn_init_raw_cost),
4051                              bl.txn_init_raw_cost) txn_actual_raw_cost
4052 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4053                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.txn_init_burdened_cost),
4054                              bl.txn_init_burdened_cost) txn_actual_brdn_cost
4055 			, DECODE(p_slice_type||ver.wp_flag, 'SECY', TO_NUMBER(NULL), bl.txn_init_revenue) txn_actual_revenue
4056 			, DECODE(ver.wp_flag, 'N', bl.txn_raw_cost - bl.txn_init_raw_cost, bl.txn_raw_cost) txn_etc_raw_cost
4057 			, DECODE(ver.wp_flag, 'N', bl.txn_burdened_cost - bl.txn_init_burdened_cost, bl.txn_burdened_cost) txn_etc_brdn_cost
4058                 	, bl.project_currency_code          prj_currency_code
4059                 	, bl.project_raw_cost               prj_raw_cost
4060                 	, bl.project_burdened_COST          prj_BRDN_COST
4061                 	, bl.project_revenue                prj_revenue
4062 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4063                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.project_init_raw_cost),
4064                              bl.project_init_raw_cost)          prj_actual_raw_cost
4065 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4066                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.project_init_burdened_cost),
4067                              bl.project_init_burdened_cost)     prj_actual_brdn_cost
4068 			, DECODE(p_slice_type||ver.wp_flag, 'SECY', TO_NUMBER(NULL), bl.project_init_revenue)           prj_actual_revenue
4069 			, DECODE(ver.wp_flag, 'N', bl.project_raw_cost - bl.project_init_raw_cost, bl.project_raw_cost) prj_etc_raw_cost
4070 			, DECODE(ver.wp_flag, 'N', bl.project_burdened_cost - bl.project_init_burdened_cost, bl.project_burdened_cost) prj_etc_brdn_cost
4071                 	, bl.projfunc_currency_code         func_currency_code
4072                   , bl.raw_cost                       func_raw_cost
4073                 	, bl.burdened_COST                  func_BRDN_COST
4074                 	, bl.revenue                        func_revenue
4075 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4076                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.init_raw_cost),
4077                              bl.init_raw_cost)                  func_actual_raw_cost
4078 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4079                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.init_burdened_cost),
4080                              bl.init_burdened_cost)             func_actual_brdn_cost
4081 			, DECODE(p_slice_type||ver.wp_flag, 'SECY', TO_NUMBER(NULL), bl.init_revenue)                   func_actual_revenue
4082 			, DECODE(ver.wp_flag, 'N', bl.raw_cost - bl.init_raw_cost, bl.raw_cost) func_etc_raw_cost
4083 			, DECODE(ver.wp_flag, 'N', bl.burdened_cost - bl.init_burdened_cost, bl.burdened_cost) func_etc_brdn_cost
4084                   , bl.quantity                       quantity
4085 			, DECODE(p_slice_type||ver.wp_flag, 'SECY',
4086                              decode(ppa.structure_sharing_code, 'SHARE_FULL',TO_NUMBER(NULL),bl.init_quantity),
4087                              bl.init_quantity)                  actual_quantity
4088 			, DECODE(ver.wp_flag, 'N', bl.quantity - bl.init_quantity, bl.quantity) etc_quantity
4089                 	, NVL(bl.period_name, 'XXX')    period_name
4090                 	, ppa.org_id project_org_id
4091                 	, ppa.carrying_out_organization_id project_organization_id
4092                               , ver.plan_type_code
4093                         , ver.time_phased_type_code
4094                  FROM
4095                      PJI_FM_EXTR_PLNVER4           ver
4096                    , PA_RESOURCE_ASSIGNMENTS       ra
4097                    , PA_BUDGET_LINES               bl
4098                    , PA_PROJECTS_ALL               ppa
4099                    , PA_PROJ_ELEM_VER_STRUCTURE    pevs
4100                  WHERE
4101                          ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
4102 					 AND (
4103                            (
4104                                  ( p_slice_type = 'PRI' )
4105                              AND ( ( ver.wp_flag = 'Y' ) OR ( ver.wp_flag = 'N' AND ver.baselined_flag = 'N' ) )
4106                            )
4107                            OR
4108                            ( p_slice_type = 'SEC' )
4109                          )
4110                      AND ra.project_id = ver.PROJECT_ID
4111                      AND ra.budget_version_id = ver.plan_version_id
4112                      AND ver.project_id = ppa.project_id
4113                      AND bl.txn_currency_code IS NOT NULL
4114                      AND bl.project_currency_code IS NOT NULL
4115                      AND bl.projfunc_currency_code IS NOT NULL
4116 			   AND pevs.element_version_id = ver.wbs_struct_version_id
4117 			   AND pevs.project_id = ver.project_id
4118 			   AND ver.worker_id = g_worker_id
4119                      AND    ver.time_phased_type_code IN ('P', 'G', 'N')
4120 			   UNION ALL
4121                     SELECT /*+ ordered */
4122                 	  bl.project_id
4123                 	, bl.ROWID row_id
4124                 	, bl.plan_version_id budget_version_id
4125                 	, DECODE(bl.project_element_id, 0, pevs.proj_element_id, bl.project_element_id) wbs_element_id
4126                 	, NVL(bl.rbs_element_id, -1)   rbs_element_id
4127                 	, bl.struct_ver_id             wbs_struct_version_id
4128                 	, NVL(ver.rbs_struct_version_id, -1)   rbs_struct_version_id
4129 			       , bl.plan_type_id               plan_type_id -- ver.plan_type_id
4130                   , bl.rate_based_flag              billable_flag -- ra.rate_based_flag             billable_flag
4131                   , bl.resource_class_code          resource_class -- ra.resource_class_code         resource_class
4132                 	, bl.txn_currency_code               txn_currency_code
4133                 	, bl.txn_raw_cost                    txn_raw_cost
4134                 	, bl.txn_burdened_cost               txn_BRDN_COST
4135                 	, bl.txn_revenue                   txn_revenue
4136 			, bl.act_txn_raw_cost           act_txn_raw_cost
4137 			, bl.act_txn_burdened_cost           act_txn_brdn_cost
4138 			, bl.act_txn_revenue           act_txn_revenue
4139 			, bl.etc_txn_raw_cost           etc_txn_raw_cost
4140 			, bl.etc_txn_burdened_cost           etc_txn_brdn_cost
4141                 	, bl.prj_currency_code               prj_currency_code
4142                 	, bl.prj_raw_cost                    prj_raw_cost
4143                 	, bl.prj_burdened_cost               prj_BRDN_COST
4144                 	, bl.prj_revenue                   prj_revenue
4145 			, bl.act_prj_raw_cost           act_prj_raw_cost
4146 			, bl.act_prj_burdened_cost           act_prj_brdn_cost
4147 			, bl.act_prj_revenue           act_prj_revenue
4148 			, bl.etc_prj_raw_cost           etc_prj_raw_cost
4149 			, bl.etc_prj_burdened_cost           act_prj_brdn_cost
4150                 	, bl.pfc_currency_code               pfc_currency_code
4151                 	, bl.pfc_raw_cost                    prj_raw_cost
4152                 	, bl.pfc_burdened_cost               prj_BRDN_COST
4153                 	, bl.pfc_revenue                   prj_revenue
4154 			, bl.act_pfc_raw_cost           act_prj_raw_cost
4155 			, bl.act_pfc_burdened_cost           act_prj_brdn_cost
4156 			, bl.act_pfc_revenue           act_prj_revenue
4157 			, bl.etc_pfc_raw_cost           etc_prj_raw_cost
4158 			, bl.etc_pfc_burdened_cost           act_prj_brdn_cost
4159                 	, bl.quantity                       quantity
4160 			, bl.act_quantity                  act_quantity
4161 			, bl.etc_quantity                  etc_quantity
4162                 	, NVL(bl.period_name, 'XXX')         period_name
4163                 	, bl.project_org_id                       project_org_id
4164                 	, ppa.carrying_out_organization_id  project_organization_id
4165  	, ver.plan_type_code
4166                         , ver.time_phased_type_code
4167                 FROM
4168                     PJI_FM_EXTR_PLNVER4           ver
4169                   , PJI_FM_EXTR_PLAN_LINES        bl
4170                   , PA_PROJECTS_ALL               ppa
4171                   , PA_PROJ_ELEM_VER_STRUCTURE    pevs
4172                     WHERE 1=1
4173                                AND ver.wp_flag = 'Y'
4174                                AND bl.project_id = ver.project_id
4175                                AND bl.plan_version_id = ver.plan_version_id
4176                                AND bl.plan_type_id = ver.plan_type_id
4177                                AND ppa.project_id = ver.project_id
4178                                AND bl.project_id = ppa.project_id
4179                                AND bl.TXN_CURRENCY_CODE IS NOT NULL
4180                                AND bl.prj_currency_code IS NOT NULL
4181                                AND bl.pfc_currency_code IS NOT NULL
4182                                AND pevs.element_version_id = ver.wbs_struct_version_id
4183                                AND pevs.project_id = ver.project_id
4184                                AND ver.worker_id = g_worker_id
4185                                AND ver.time_phased_type_code IN ('P', 'G', 'N')
4186                                AND p_slice_type = 'SEC' -- 4682341
4187 				  ) spread_bl
4188 				   ---- end of third inline view 'spread_bl'...........
4189             	  ,
4190             	    (
4191                       SELECT 4   INVERT_ID FROM dual UNION ALL
4192                       SELECT 8   INVERT_ID FROM dual UNION ALL
4193                       SELECT 16  INVERT_ID FROM dual where PJI_UTILS.GET_SETUP_PARAMETER('TXN_CURR_FLAG') = 'Y'
4194                     ) invert
4195 				)  collapse_bl
4196 				----  End of second inline view 'collapse_bl' ..........
4197 			GROUP BY
4198               collapse_bl.PROJECT_ID
4199             , collapse_bl.WBS_ELEMENT_ID
4200             , collapse_bl.RBS_ELEMENT_ID
4201             , collapse_bl.RBS_STRUCT_VERSION_ID
4202 			, collapse_bl.plan_type_id
4203             , collapse_bl.BUDGET_VERSION_ID
4204             , collapse_bl.PROJECT_ORGANIZATION_ID
4205             , collapse_bl.PROJECT_ORG_ID
4206             , collapse_bl.BILLABLE_FLAG
4207             , collapse_bl.RESOURCE_CLASS
4208             , collapse_bl.CURRENCY_CODE
4209             , collapse_bl.period_name
4210             , collapse_bl.row_id
4211             , collapse_bl.plan_type_code
4212             , collapse_bl.time_phased_type_code
4213        ) plr
4214 				----  End of first inline view plr ..........
4215        , PJI_ORG_EXTR_INFO            orginfo
4216        , pji_time_cal_period_v    pji_time
4217          WHERE  1=1
4218            -- AND    orginfo.projfunc_currency_mau is not NULL
4219            AND    plr.project_org_id         = orginfo.org_id
4220            AND    DECODE(plr.time_phased_type_code
4221                    , 'P', orginfo.pa_calendar_id
4222                    , 'G', orginfo.gl_calendar_id
4223                    , - l_max_project_id ) = pji_time.calendar_id
4224            AND    plr.period_name = pji_time.name
4225        GROUP BY
4226          plr.PROJECT_ID
4227        , plr.PROJECT_ORG_ID
4228        , plr.project_ORGANIZATION_ID
4229        , plr.WBS_ELEMENT_ID
4230        , DECODE(plr.time_phased_type_code
4231               , 'N', -1
4232               , pji_time.cal_period_id)
4233        , DECODE(plr.time_phased_type_code
4234                , 'P', 32
4235                , 'G', 32
4236                , 'N', 2048
4237                , -1) -- period type id...
4238        , DECODE(plr.time_phased_type_code
4239                , 'P', 'P'
4240                , 'G', 'G'
4241                , 'N', 'A'
4242                , 'X')     --   CALENDAR_TYPE
4243        , plr.CURR_RECORD_TYPE  -- curr code missing.
4244   	   , plr.currency_code
4245        , plr.RBS_ELEMENT_ID
4246        , plr.RBS_STRUCT_VERSION_ID
4247        , plr.BUDGET_VERSION_ID
4248   	 , plr.plan_type_id
4249        , DECODE(plr.time_phased_type_code, 'P', 'OF', 'G', 'OF', 'N', 'NTR', 'X')
4250        , plr.PLAN_TYPE_CODE;
4251 
4252 
4253     print_time('EXTRACT_PLAN_ETC_PRIRBS : Finished. # rows extracted is ' || SQL%ROWCOUNT);
4254 
4255     DELETE_NTP_CAL_RECORD ( p_max_project_id => l_max_project_id );
4256 
4257 
4258 EXCEPTION
4259   WHEN OTHERS THEN
4260     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
4261     ( p_package_name   => g_package_name
4262     , p_procedure_name => 'EXTRACT_PLAN_ETC_PRIRBS'
4263     , x_return_status  => l_return_status ) ;
4264 
4265     RAISE;
4266 END;
4267 
4268 
4269 PROCEDURE DELETE_PLAN_LINES
4270 ( x_return_status OUT NOCOPY VARCHAR2 ) IS
4271 
4272 --Bug 6047966
4273 l_process                  varchar2(30);
4274 l_plan_ver_type_code       varchar2(30);
4275 
4276 BEGIN
4277 
4278   print_time ( 'DELETE_PLAN_LINES : begin ' );
4279 
4280   Pji_Pjp_Fp_Curr_Wrap.INIT_ERR_STACK
4281   ( p_package_name   => g_package_name
4282   , x_return_status  => x_return_status ) ;
4283 
4284  --Bug 6047966.
4285  l_process := PJI_PJP_SUM_MAIN.g_process || to_char(g_worker_id);
4286  l_plan_ver_type_code := PJI_PROCESS_UTIL.GET_PROCESS_PARAMETER(l_process, 'PLAN_VERSION');
4287 
4288   DELETE FROM pji_fm_extr_plan_lines
4289   WHERE (project_id, plan_version_id) IN
4290     (SELECT ver.project_id, ver.plan_version_id
4291      FROM   pji_fm_extr_plnver4 ver
4292      WHERE ver.worker_id = g_worker_id)
4293        AND txn_currency_code IS NOT NULL
4294        AND prj_currency_code IS NOT NULL
4295        AND pfc_currency_code IS NOT NULL;
4296 
4297   --Bug 6047966. In case actuals alone are summarized, pji_fm_extr_plnver4
4298   --contains only -1 record. Hence that case should be handled.
4299    IF l_plan_ver_type_code ='ACTUAL' THEN
4300 
4301      DELETE FROM pji_fm_extr_plan_lines
4302      WHERE (project_id) IN
4303        (SELECT ver.project_id
4304         FROM   pji_fm_extr_plnver4 ver
4305         WHERE ver.worker_id = g_worker_id
4306         AND   ver.plan_version_id=-1)
4307           AND txn_currency_code IS NOT NULL
4308           AND prj_currency_code IS NOT NULL
4309           AND pfc_currency_code IS NOT NULL;
4310 
4311    END IF;
4312 
4313   print_time ( 'DELETE_PLAN_LINES : after deletion of plan lines , # rows deleted is  ' || SQL%ROWCOUNT );
4314 
4315 EXCEPTION
4316   WHEN OTHERS THEN
4317     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
4318     ( p_package_name   => g_package_name
4319     , p_procedure_name => 'DELETE_PLAN_LINES'
4320     , x_return_status  => x_return_status ) ;
4321 
4322     RAISE;
4323 END;
4324 
4325 
4326 --
4327 -- Inserts into FP reporting lines fact the data in the interim pjp1 table.
4328 --
4329 PROCEDURE INSERT_INTO_FP_FACT ( p_slice_type IN VARCHAR2 := NULL) IS
4330     l_last_update_date     date   := SYSDATE;
4331     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
4332     l_creation_date        date   := SYSDATE;
4333     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
4334     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
4335 
4336 /*commenting for bug 7497672
4337 -- Bug 7010864
4338 	TYPE row_id_tab_type   IS TABLE OF rowid index by binary_integer;
4339     x_row_id               row_id_tab_type;
4340 
4341    cursor c1 is
4342       select rowid from pji_fp_aggr_pjp1
4343 	   where worker_id = g_worker_id;
4344 -- Bug 7010864
4345 */
4346 BEGIN
4347 
4348   IF ( p_slice_type IS NULL ) THEN
4349 /* commenting for bug 7497672
4350 -- Bug 7010864
4351   open c1;
4352   loop
4353 
4354 	 fetch c1 bulk collect into x_row_id limit 500000;
4355 
4356   If x_row_id.count > 0  then
4357     -- gather statistics for PJI metadata tables
4358     FND_STATS.GATHER_TABLE_STATS(ownname => PJI_UTILS.GET_PJI_SCHEMA_NAME,
4359                                  tabname => 'PJI_FP_AGGR_PJP1',
4360                                  percent => 5,
4361                                  degree  => PJI_UTILS.GET_DEGREE_OF_PARALLELISM());
4362 
4363     -- gather statistics for PJI metadata tables
4364     FND_STATS.GATHER_TABLE_STATS(ownname => PJI_UTILS.GET_PJI_SCHEMA_NAME,
4365                                  tabname => 'PJI_FP_XBS_ACCUM_F',
4366                                  percent => 5,
4367                                  degree  => PJI_UTILS.GET_DEGREE_OF_PARALLELISM());
4368 
4369 
4370 Forall i in x_row_id.first..x_row_id.last
4371 */
4372     INSERT /*+ append parallel(rl) */ INTO pji_fp_xbs_accum_f rl
4373     (
4374        PROJECT_ID
4375      , PROJECT_ORG_ID
4376      , PROJECT_ORGANIZATION_ID
4377      , PROJECT_ELEMENT_ID
4378      , TIME_ID
4379      , PERIOD_TYPE_ID
4380      , CALENDAR_TYPE
4381      , RBS_AGGR_LEVEL
4382      , WBS_ROLLUP_FLAG
4383      , PRG_ROLLUP_FLAG
4384      , CURR_RECORD_TYPE_ID
4385      , CURRENCY_CODE
4386      , RBS_ELEMENT_ID
4387      , RBS_VERSION_ID
4388      , PLAN_VERSION_ID
4389      , PLAN_TYPE_ID
4390      , LAST_UPDATE_DATE
4391      , LAST_UPDATED_BY
4392      , CREATION_DATE
4393      , CREATED_BY
4394      , LAST_UPDATE_LOGIN
4395      , RAW_COST
4396      , BRDN_COST
4397      , REVENUE
4398      , BILL_RAW_COST
4399      , BILL_BRDN_COST
4400      , BILL_LABOR_RAW_COST
4401      , BILL_LABOR_BRDN_COST
4402      , BILL_LABOR_HRS
4403      , EQUIPMENT_RAW_COST
4404      , EQUIPMENT_BRDN_COST
4405      , CAPITALIZABLE_RAW_COST
4406      , CAPITALIZABLE_BRDN_COST
4407      , LABOR_RAW_COST
4408      , LABOR_BRDN_COST
4409      , LABOR_HRS
4410      , LABOR_REVENUE
4411      , EQUIPMENT_HOURS
4412      , BILLABLE_EQUIPMENT_HOURS
4413      , SUP_INV_COMMITTED_COST
4414      , PO_COMMITTED_COST
4415      , PR_COMMITTED_COST
4416      , OTH_COMMITTED_COST
4417        , ACT_LABOR_HRS
4418 	   , ACT_EQUIP_HRS
4419 	   , ACT_LABOR_BRDN_COST
4420 	   , ACT_EQUIP_BRDN_COST
4421 	   , ACT_BRDN_COST
4422 	   , ACT_RAW_COST
4423 	   , ACT_REVENUE
4424          , ACT_LABOR_RAW_COST
4425          , ACT_EQUIP_RAW_COST
4426 	   , ETC_LABOR_HRS
4427 	   , ETC_EQUIP_HRS
4428 	   , ETC_LABOR_BRDN_COST
4429 	   , ETC_EQUIP_BRDN_COST
4430 	   , ETC_BRDN_COST
4431          , ETC_RAW_COST
4432          , ETC_LABOR_RAW_COST
4433          , ETC_EQUIP_RAW_COST
4434      , CUSTOM1
4435      , CUSTOM2
4436      , CUSTOM3
4437      , CUSTOM4
4438      , CUSTOM5
4439      , CUSTOM6
4440      , CUSTOM7
4441      , CUSTOM8
4442      , CUSTOM9
4443      , CUSTOM10
4444      , CUSTOM11
4445      , CUSTOM12
4446      , CUSTOM13
4447      , CUSTOM14
4448      , CUSTOM15
4449      , PLAN_TYPE_CODE
4450     )
4451      SELECT
4452        pjp1.PROJECT_ID
4453      , pjp1.PROJECT_ORG_ID
4454      , pjp1.PROJECT_ORGANIZATION_ID
4455      , pjp1.PROJECT_ELEMENT_ID
4456      , pjp1.TIME_ID
4457      , pjp1.PERIOD_TYPE_ID
4458      , pjp1.CALENDAR_TYPE
4459      , pjp1.RBS_AGGR_LEVEL
4460      , pjp1.WBS_ROLLUP_FLAG
4461      , pjp1.PRG_ROLLUP_FLAG
4462      , pjp1.CURR_RECORD_TYPE_ID
4463      , pjp1.CURRENCY_CODE
4464      , pjp1.RBS_ELEMENT_ID
4465      , pjp1.RBS_VERSION_ID
4466      , pjp1.PLAN_VERSION_ID
4467      , pjp1.PLAN_TYPE_ID
4468      , l_last_update_date -- sysdate --
4469      , l_last_updated_by  -- 1 --
4470      , l_creation_date  -- sysdate --
4471      , l_created_by  -- 1 --
4472      , l_last_update_login  -- 1 --
4473      , pjp1.RAW_COST
4474      , pjp1.BRDN_COST
4475      , pjp1.REVENUE
4476      , pjp1.BILL_RAW_COST
4477      , pjp1.BILL_BRDN_COST
4478      , pjp1.BILL_LABOR_RAW_COST
4479      , pjp1.BILL_LABOR_BRDN_COST
4480      , pjp1.BILL_LABOR_HRS
4481      , pjp1.EQUIPMENT_RAW_COST
4482      , pjp1.EQUIPMENT_BRDN_COST
4483      , pjp1.CAPITALIZABLE_RAW_COST
4484      , pjp1.CAPITALIZABLE_BRDN_COST
4485      , pjp1.LABOR_RAW_COST
4486      , pjp1.LABOR_BRDN_COST
4487      , pjp1.LABOR_HRS
4488      , pjp1.LABOR_REVENUE
4489      , pjp1.EQUIPMENT_HOURS
4490      , pjp1.BILLABLE_EQUIPMENT_HOURS
4491      , pjp1.SUP_INV_COMMITTED_COST
4492      , pjp1.PO_COMMITTED_COST
4493      , pjp1.PR_COMMITTED_COST
4494      , pjp1.OTH_COMMITTED_COST
4495        , pjp1.ACT_LABOR_HRS
4496 	   , pjp1.ACT_EQUIP_HRS
4497 	   , pjp1.ACT_LABOR_BRDN_COST
4498 	   , pjp1.ACT_EQUIP_BRDN_COST
4499 	   , pjp1.ACT_BRDN_COST
4500 	   , pjp1.ACT_RAW_COST
4501 	   , pjp1.ACT_REVENUE
4502          , pjp1.ACT_LABOR_RAW_COST
4503          , pjp1.ACT_EQUIP_RAW_COST
4504 	   , DECODE(SIGN(pjp1.ETC_LABOR_HRS), -1, 0, pjp1.ETC_LABOR_HRS)  ETC_LABOR_HRS
4505 	   , DECODE(SIGN(pjp1.ETC_EQUIP_HRS), -1, 0, pjp1.ETC_EQUIP_HRS)  ETC_EQUIP_HRS
4506 	   , DECODE(SIGN(pjp1.ETC_LABOR_BRDN_COST), -1, 0, pjp1.ETC_LABOR_BRDN_COST)   ETC_LABOR_BRDN_COST
4507 	   , DECODE(SIGN(pjp1.ETC_EQUIP_BRDN_COST), -1, 0, pjp1.ETC_EQUIP_BRDN_COST)   ETC_EQUIP_BRDN_COST
4508 	   , DECODE(SIGN(pjp1.ETC_BRDN_COST), -1, 0, pjp1.ETC_BRDN_COST)   ETC_BRDN_COST
4509          , DECODE(SIGN(pjp1.ETC_RAW_COST), -1, 0, pjp1.ETC_RAW_COST)    ETC_RAW_COST
4510          , DECODE(SIGN(pjp1.ETC_LABOR_RAW_COST), -1, 0, pjp1.ETC_LABOR_RAW_COST)  ETC_LABOR_RAW_COST
4511          , DECODE(SIGN(pjp1.ETC_EQUIP_RAW_COST), -1, 0, pjp1.ETC_EQUIP_RAW_COST)  ETC_EQUIP_RAW_COST
4512      , pjp1.CUSTOM1
4513      , pjp1.CUSTOM2
4514      , pjp1.CUSTOM3
4515      , pjp1.CUSTOM4
4516      , pjp1.CUSTOM5
4517      , pjp1.CUSTOM6
4518      , pjp1.CUSTOM7
4519      , pjp1.CUSTOM8
4520      , pjp1.CUSTOM9
4521      , pjp1.CUSTOM10
4522      , pjp1.CUSTOM11
4523      , pjp1.CUSTOM12
4524      , pjp1.CUSTOM13
4525      , pjp1.CUSTOM14
4526      , pjp1.CUSTOM15
4527      , pjp1.PLAN_TYPE_CODE
4528     FROM
4529          pji_fp_aggr_pjp1 pjp1
4530     WHERE 1=1
4531       AND worker_id = g_worker_id
4532       --AND pjp1.rowid = x_row_id(i)   commented for bug 7497672
4533     ORDER BY
4534        pjp1.PROJECT_ID
4535      , pjp1.PLAN_VERSION_ID
4536      , pjp1.PROJECT_ELEMENT_ID
4537      , pjp1.TIME_ID
4538      , pjp1.RBS_VERSION_ID;
4539 /*commenting for bug 7497672
4540 -- Bug 7010864
4541  Forall j in x_row_id.first..x_row_id.last
4542    delete from pji_fp_aggr_pjp1
4543 	  where worker_id = g_worker_id
4544 	    and rowid = x_row_id(j);
4545 
4546 	 commit;
4547 
4548 	 x_row_id.delete;
4549 
4550      exit when c1%notfound;
4551 	 Else
4552 	   Exit;
4553 	 End if;
4554 	 end loop;
4555 
4556   close c1;
4557 -- Bug 7010864
4558 */
4559   ELSIF (p_slice_type = 'PRI') THEN
4560 
4561     MERGE_INTO_FP_FACT;
4562 
4563   ELSIF (p_slice_type = 'SEC') THEN
4564 
4565     DELETE FROM pji_fp_aggr_pjp1
4566     WHERE line_type = 'OF'
4567       AND worker_id = g_worker_id;  -- Data in entered calendar should not be rolled up.
4568 
4569     MERGE_INTO_FP_FACT;
4570 
4571   END IF;
4572 
4573 EXCEPTION
4574   WHEN OTHERS THEN
4575     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
4576                              p_procedure_name => 'INSERT_INTO_FP_FACT');
4577     RAISE;
4578 END;
4579 
4580 
4581 PROCEDURE GET_PRI_SLICE_DATA(
4582   p_fp_version_ids    IN   SYSTEM.pa_num_tbl_type := pji_empty_num_tbl
4583 , p_commit            IN   VARCHAR2 := 'F') IS
4584 BEGIN
4585 
4586   FORALL I IN p_fp_version_ids.FIRST..p_fp_version_ids.LAST
4587     INSERT INTO pji_fp_aggr_pjp1
4588     (
4589        WORKER_ID
4590      , PROJECT_ID
4591      , PROJECT_ORG_ID
4592      , PROJECT_ORGANIZATION_ID
4593      -- , PARTITION_ID
4594      , PROJECT_ELEMENT_ID
4595      , TIME_ID
4596      , PERIOD_TYPE_ID
4597      , CALENDAR_TYPE
4598      , RBS_AGGR_LEVEL
4599      , WBS_ROLLUP_FLAG
4600      , PRG_ROLLUP_FLAG
4601      , CURR_RECORD_TYPE_ID
4602      , CURRENCY_CODE
4603      , RBS_ELEMENT_ID
4604      , RBS_VERSION_ID
4605      , PLAN_VERSION_ID
4606      , PLAN_TYPE_ID
4607      , RAW_COST
4608      , BRDN_COST
4609      , REVENUE
4610      , BILL_RAW_COST
4611      , BILL_BRDN_COST
4612      , BILL_LABOR_RAW_COST
4613      , BILL_LABOR_BRDN_COST
4614      , BILL_LABOR_HRS
4615      , EQUIPMENT_RAW_COST
4616      , EQUIPMENT_BRDN_COST
4617      , CAPITALIZABLE_RAW_COST
4618      , CAPITALIZABLE_BRDN_COST
4619      , LABOR_RAW_COST
4620      , LABOR_BRDN_COST
4621      , LABOR_HRS
4622      , LABOR_REVENUE
4623      , EQUIPMENT_HOURS
4624      , BILLABLE_EQUIPMENT_HOURS
4625      , SUP_INV_COMMITTED_COST
4626      , PO_COMMITTED_COST
4627      , PR_COMMITTED_COST
4628      , OTH_COMMITTED_COST
4629        , ACT_LABOR_HRS
4630 	   , ACT_EQUIP_HRS
4631 	   , ACT_LABOR_BRDN_COST
4632 	   , ACT_EQUIP_BRDN_COST
4633 	   , ACT_BRDN_COST
4634 	   , ACT_RAW_COST
4635 	   , ACT_REVENUE
4636          , ACT_LABOR_RAW_COST
4637          , ACT_EQUIP_RAW_COST
4638 	   , ETC_LABOR_HRS
4639 	   , ETC_EQUIP_HRS
4640 	   , ETC_LABOR_BRDN_COST
4641 	   , ETC_EQUIP_BRDN_COST
4642 	   , ETC_BRDN_COST
4643          , ETC_RAW_COST
4644          , ETC_LABOR_RAW_COST
4645          , ETC_EQUIP_RAW_COST
4646      , CUSTOM1
4647      , CUSTOM2
4648      , CUSTOM3
4649      , CUSTOM4
4650      , CUSTOM5
4651      , CUSTOM6
4652      , CUSTOM7
4653      , CUSTOM8
4654      , CUSTOM9
4655      , CUSTOM10
4656      , CUSTOM11
4657      , CUSTOM12
4658      , CUSTOM13
4659      , CUSTOM14
4660      , CUSTOM15
4661      , PRG_LEVEL
4662      , PLAN_TYPE_CODE
4663     )
4664     (
4665      SELECT
4666        g_worker_id
4667      , rl.PROJECT_ID
4668      , rl.PROJECT_ORG_ID
4669      , rl.PROJECT_ORGANIZATION_ID
4670      -- , rl.PARTITION_ID
4671      , rl.PROJECT_ELEMENT_ID
4672      , rl.TIME_ID
4673      , rl.PERIOD_TYPE_ID
4674      , rl.CALENDAR_TYPE
4675      , rl.RBS_AGGR_LEVEL
4676      , rl.WBS_ROLLUP_FLAG
4677      , rl.PRG_ROLLUP_FLAG
4678      , rl.CURR_RECORD_TYPE_ID
4679      , rl.CURRENCY_CODE
4680      , rl.RBS_ELEMENT_ID
4681      , rl.RBS_VERSION_ID
4682      , rl.PLAN_VERSION_ID
4683      , rl.PLAN_TYPE_ID
4684      , rl.RAW_COST
4685      , rl.BRDN_COST
4686      , rl.REVENUE
4687      , rl.BILL_RAW_COST
4688      , rl.BILL_BRDN_COST
4689      , rl.BILL_LABOR_RAW_COST
4690      , rl.BILL_LABOR_BRDN_COST
4691      , rl.BILL_LABOR_HRS
4692      , rl.EQUIPMENT_RAW_COST
4693      , rl.EQUIPMENT_BRDN_COST
4694      , rl.CAPITALIZABLE_RAW_COST
4695      , rl.CAPITALIZABLE_BRDN_COST
4696      , rl.LABOR_RAW_COST
4697      , rl.LABOR_BRDN_COST
4698      , rl.LABOR_HRS
4699      , rl.LABOR_REVENUE
4700      , rl.EQUIPMENT_HOURS
4701      , rl.BILLABLE_EQUIPMENT_HOURS
4702      , rl.SUP_INV_COMMITTED_COST
4703      , rl.PO_COMMITTED_COST
4704      , rl.PR_COMMITTED_COST
4705      , rl.OTH_COMMITTED_COST
4706        , rl.ACT_LABOR_HRS
4707 	   , rl.ACT_EQUIP_HRS
4708 	   , rl.ACT_LABOR_BRDN_COST
4709 	   , rl.ACT_EQUIP_BRDN_COST
4710 	   , rl.ACT_BRDN_COST
4711 	   , rl.ACT_RAW_COST
4712 	   , rl.ACT_REVENUE
4713          , rl.ACT_LABOR_RAW_COST
4714          , rl.ACT_EQUIP_RAW_COST
4715 	   , rl.ETC_LABOR_HRS
4716 	   , rl.ETC_EQUIP_HRS
4717 	   , rl.ETC_LABOR_BRDN_COST
4718 	   , rl.ETC_EQUIP_BRDN_COST
4719 	   , rl.ETC_BRDN_COST
4720          , rl.ETC_RAW_COST
4721          , rl.ETC_LABOR_RAW_COST
4722          , rl.ETC_EQUIP_RAW_COST
4723      , rl.CUSTOM1
4724      , rl.CUSTOM2
4725      , rl.CUSTOM3
4726      , rl.CUSTOM4
4727      , rl.CUSTOM5
4728      , rl.CUSTOM6
4729      , rl.CUSTOM7
4730      , rl.CUSTOM8
4731      , rl.CUSTOM9
4732      , rl.CUSTOM10
4733      , rl.CUSTOM11
4734      , rl.CUSTOM12
4735      , rl.CUSTOM13
4736      , rl.CUSTOM14
4737      , rl.CUSTOM15
4738      , g_default_prg_level
4739      , rl.PLAN_TYPE_CODE
4740     FROM
4741          pji_fp_xbs_accum_f rl
4742        , pa_budget_versions bv -- @pjdev115 bv
4743 	 , pa_proj_fp_options fpo -- @pjdev115 fpo
4744     WHERE
4745          rl.project_id = bv.project_id
4746      AND bv.version_type IS NOT NULL
4747      AND bv.fin_plan_type_id IS NOT NULL
4748      AND fpo.project_id = bv.project_id
4749      AND bv.fin_plan_type_id = fpo.fin_plan_type_id
4750      AND bv.budget_version_id = fpo.fin_plan_version_id
4751      AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'
4752      AND rl.calendar_type =
4753                 DECODE( bv.version_type
4754                       , 'ALL',     fpo.all_time_phased_code
4755                       , 'COST',    fpo.cost_time_phased_code
4756                       , 'REVENUE', fpo.revenue_time_phased_code
4757                      )
4758      AND rl.plan_version_id = p_fp_version_ids(i));
4759 
4760 
4761 EXCEPTION
4762   WHEN OTHERS THEN
4763     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
4764                              p_procedure_name => 'GET_PRI_SLICE_DATA');
4765     RAISE;
4766 END;
4767 
4768 
4769 
4770 PROCEDURE POPULATE_PLN_VER_TABLE IS
4771   l_count NUMBER := 0;
4772 BEGIN
4773 
4774   print_time('........POPULATE_PLN_VER_TABLE : Begin.' );
4775 
4776     INSERT INTO PJI_FM_EXTR_PLNVER4
4777     (
4778       WORKER_ID                ,
4779       PROJECT_ID               ,
4780       PLAN_VERSION_ID          ,
4781       WBS_STRUCT_VERSION_ID    ,
4782       RBS_STRUCT_VERSION_ID    ,
4783       PLAN_TYPE_CODE           ,
4784       PLAN_TYPE_ID             ,
4785       TIME_PHASED_TYPE_CODE    ,
4786       TIME_DANGLING_FLAG       ,
4787       RATE_DANGLING_FLAG       ,
4788       PROJECT_TYPE_CLASS       ,
4789       WP_FLAG                  ,
4790 	CURRENT_FLAG             ,
4791 	ORIGINAL_FLAG            ,
4792 	CURRENT_ORIGINAL_FLAG    ,
4793 	BASELINED_FLAG
4794     )
4795   SELECT DISTINCT
4796           g_worker_id
4797         , epl.project_id
4798 	  , epl.plan_version_id
4799 	  , DECODE ( NVL(bv.wp_version_flag, 'N')
4800 	           , 'Y', bv.project_structure_version_id
4801 		     , PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(bv.project_id) -- -9999 --
4802 		     )                           wbs_struct_version_id
4803 	  -- , epl.struct_ver_id
4804 	  , fpo.rbs_version_id -- epl.rbs_version_id
4805 --	  , to_char(epl.plan_type_id) -- pln type code
4806                , DECODE (bv.version_type, 'COST' ,'C' , 'REVENUE', 'R', 'A') plan_type_code       /* 4471527  */
4807 	  , epl.plan_type_id
4808         , DECODE(bv.version_type
4809                   , 'ALL',     fpo.all_time_phased_code
4810                   , 'COST',    fpo.cost_time_phased_code
4811                   , 'REVENUE', fpo.revenue_time_phased_code
4812                  )                       time_phased_type_code
4813 	  , null -- time dangling..
4814 	  , null -- time dangling..
4815 	  , null -- project type class is not used currently..
4816         , DECODE(bv.wp_version_flag, 'Y', 'Y', 'N') is_wp_flag
4817 	  , bv.current_flag                  current_flag
4818 	  , bv.original_flag                 original_flag
4819 	  , bv.current_original_flag         current_original_flag
4820 	  , DECODE(bv.baselined_date, NULL, 'N', 'Y') baselined_flag
4821   FROM PJI_FM_EXTR_PLAN_LINES epl
4822      , pa_budget_versions bv
4823      , pa_proj_fp_options fpo
4824   WHERE
4825         epl.plan_version_id = bv.budget_version_id
4826     AND fpo.project_id = bv.project_id
4827     AND bv.fin_plan_type_id = fpo.fin_plan_type_id
4828     AND bv.budget_version_id = fpo.fin_plan_version_id
4829     AND fpo.fin_plan_option_level_code = 'PLAN_VERSION'
4830     AND epl.rowid IN ( SELECT extr_lines_rowid FROM pji_fp_rmap_fpr_update_t);
4831 
4832   /*
4833   SELECT COUNT(1)
4834   INTO   l_count
4835   FROM   PJI_FM_EXTR_PLNVER4;
4836   */
4837 
4838   print_time('count is ... ' || SQL%ROWCOUNT );
4839 
4840   print_time('........POPULATE_PLN_VER_TABLE : End.' );
4841 
4842 EXCEPTION
4843   WHEN OTHERS THEN
4844     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
4845                              p_procedure_name => 'POPULATE_PLN_VER_TABLE');
4846     print_time('........POPULATE_PLN_VER_TABLE : Exception: ' || sqlerrm );
4847     RAISE;
4848 END;
4849 
4850 
4851 PROCEDURE GET_GLOBAL_EXCHANGE_RATES IS
4852 
4853   l_gl1_cur_code  VARCHAR2(15) := g_global1_currency_code;
4854   l_gl2_cur_code  VARCHAR2(15) := g_global2_currency_code;
4855   l_gl1_mau       NUMBER := g_global1_currency_mau;
4856   l_gl2_mau       NUMBER := g_global2_currency_mau;
4857 
4858 BEGIN
4859 
4860   print_time('........GET_GLOBAL_EXCHANGE_RATES: Begin. ' );
4861 
4862     PJI_PJP_FP_CURR_WRAP.get_global_currency_info (
4863       x_currency_conversion_rule => g_currency_conversion_rule
4864     , x_prorating_format         => g_prorating_format
4865     , x_global1_currency_code    => g_global1_currency_code
4866     , x_global2_currency_code    => g_global2_currency_code
4867     , x_global1_currency_mau     => g_global1_currency_mau
4868     , x_global2_currency_mau     => g_global2_currency_mau ) ;
4869 
4870   --
4871   -- Todo: Remove hardcoded currency codes after making code work if one of the global currencies is null.
4872   --
4873 
4874   l_gl1_cur_code  := g_global1_currency_code;
4875   l_gl2_cur_code  := g_global2_currency_code;
4876   l_gl1_mau       := g_global1_currency_mau;
4877   l_gl2_mau       := g_global2_currency_mau;
4878 
4879   print_time(' Got global currency settings. ');
4880   print_time(' g_currency_conversion_rule ' || g_currency_conversion_rule || ' g_prorating_format ' ||  g_prorating_format );
4881   print_time(' g_global1_currency_code ' || g_global1_currency_code || ' g_global2_currency_code ' || g_global2_currency_code );
4882   print_time(' g_global1_currency_mau ' || g_global1_currency_mau || ' g_global2_currency_mau ' || g_global2_currency_mau ) ;
4883 
4884 
4885       DELETE FROM PJI_FM_AGGR_DLY_RATES
4886       WHERE worker_id = g_worker_id;
4887 
4888       print_time('........GET_GLOBAL_EXCHANGE_RATES: # rows deleted is: ' || SQL%ROWCOUNT );
4889 
4890 PJI_UTILS.g_max_roll_days := 1500;  /*5155692 */
4891     -- Removed pa_resource_assignments join and Added joins with pji_org_extr_info
4892     -- for bug 4149422
4893       -- SQL for getting rates for time phased budgets.
4894   INSERT INTO PJI_FM_AGGR_DLY_RATES (
4895                 WORKER_ID	,
4896                 PF_CURRENCY_CODE	,
4897                 TIME_ID	,
4898                 RATE	,
4899                 MAU	,
4900                 RATE2	,
4901                 MAU2)
4902   select worker_id,
4903          projfunc_currency_code,
4904          time_id,
4905          PJI_UTILS.GET_GLOBAL_RATE_PRIMARY (temp.projfunc_currency_code
4906                       , DECODE ( g_currency_conversion_rule
4907                                , 'S'
4908                                , temp.start_date
4909                                , 'E'
4910                                , temp.end_date ))
4911        rate1,
4912        l_gl1_mau mau1,
4913        DECODE ( PJI_UTILS.GET_SETUP_PARAMETER('GLOBAL_CURR2_FLAG'),
4914                 'Y',
4915                 PJI_UTILS.GET_GLOBAL_RATE_SECONDARY(
4916 		temp.projfunc_currency_code,
4917                 DECODE ( g_currency_conversion_rule
4918                        , 'S'
4919                        , temp.start_date
4920                        , 'E'
4921                        , temp.end_date
4922 		       )
4923 	          ) ,
4924                NULL
4925               ) rate2,
4926        l_gl2_mau mau2
4927    FROM (
4928           SELECT DISTINCT
4929                  g_worker_id worker_id
4930                , ppa.projfunc_currency_code
4931                , cal_period_id time_id
4932                , DECODE( g_currency_conversion_rule
4933 	               , 'S'
4934 	               , prd.start_date
4935 		       , TO_DATE(NULL)
4936 		       ) start_date
4937 	       , DECODE( g_currency_conversion_rule
4938 	               , 'E'
4939 		       , prd.end_date
4940 		       , TO_DATE(NULL)
4941 	               ) end_date
4942           FROM pa_budget_lines  bl
4943            , pji_time_cal_period_v  prd
4944            , PJI_FM_EXTR_PLNVER4  ver
4945            , pa_projects_all ppa
4946            ,PJI_ORG_EXTR_INFO inf
4947       WHERE 1=1
4948             AND bl.budget_version_id = ver.plan_version_id
4949             AND ver.time_phased_type_code IN ('P', 'G')
4950             AND bl.period_name = prd.name
4951             AND ver.wp_flag = 'N'
4952             AND ver.baselined_flag = 'Y'
4953             AND ver.project_id = ppa.project_id
4954             --  Fix for bug : 4149422
4955             AND ppa.org_id = inf.org_id
4956             AND DECODE ( ver.time_phased_type_code
4957 	               , 'P'
4958 		       , inf.pa_calendar_id
4959 		       , 'G'
4960 		       , inf.gl_calendar_id
4961 		       ) = prd.calendar_id
4962             AND ver.worker_id = g_worker_id
4963           UNION ALL
4964           SELECT DISTINCT
4965                    g_worker_id worker_id
4966                  , ppa.projfunc_currency_code
4967                  , DECODE ( g_currency_conversion_rule
4968                           , 'S'
4969                           , TO_NUMBER(to_CHAR(ra.planning_start_date, 'J'))
4970                           , 'E'
4971                           , TO_NUMBER(to_CHAR(ra.planning_end_date, 'J')) ) time_id
4972 		  , DECODE( g_currency_conversion_rule
4973 	                  , 'S'
4974 		          , ra.planning_start_date
4975 		          , TO_DATE(NULL)
4976 			  ) start_date
4977 		  , DECODE( g_currency_conversion_rule
4978 		          , 'E'
4979 		          , ra.planning_end_date
4980                           , TO_DATE(NULL)
4981 		          ) end_date
4982           FROM pa_resource_assignments ra
4983              , PJI_FM_EXTR_PLNVER4  ver
4984              , pa_projects_all ppa
4985       WHERE 1=1
4986             AND ra.budget_version_id = ver.plan_version_id
4987             AND ver.wp_flag = 'N'
4988             AND ver.baselined_flag = 'Y'
4989             AND ver.time_phased_type_code = 'N'
4990             AND ver.project_id = ppa.project_id
4991             AND ver.worker_id = g_worker_id
4992          ) temp;
4993 
4994 PJI_UTILS.g_max_roll_days := NULL;  /*5155692 */
4995 
4996 	UPDATE /*+ INDEX(prd,pji_time_cal_period_u1) index(rates, PJI_FM_AGGR_DLY_RATES_N1) */
4997 	       PJI_FM_EXTR_PLNVER4 ver
4998 	   SET rate_dangling_flag = 'Y'
4999 	  WHERE (project_id,plan_version_id)  IN
5000 			(    SELECT project_id,budget_version_id
5001 			       FROM PA_BUDGET_LINES bl,
5002 			   	        pji_time_cal_period_v  prd,
5003 					    PJI_FM_AGGR_DLY_RATES rates
5004 			      WHERE rates.time_id=prd.cal_period_id
5005 			        AND bl.period_name = prd.name
5006 				AND (sign(rates.rate)=-1 OR sign(rates.rate2) = -1)
5007 				AND ver.time_phased_type_code IN ('P', 'G')
5008 			        AND rates.worker_id=g_worker_id /* Added for bug 4083581 */
5009 			  UNION ALL
5010 			     SELECT project_id,budget_version_id
5011 			       FROM pa_resource_assignments ra,
5012 					    PJI_FM_AGGR_DLY_RATES rates
5013 			      WHERE rates.time_id= DECODE ( g_currency_conversion_rule
5014                                		   		  	, 'S'
5015 					 				, TO_NUMBER(to_CHAR(ra.planning_start_date, 'J'))
5016 								 , 'E'
5017 									 , TO_NUMBER(to_CHAR(ra.planning_end_date, 'J')) )
5018 				AND (sign(rates.rate)=-1 OR sign(rates.rate2) = -1)
5019 				AND ver.time_phased_type_code = 'N'
5020 			        AND rates.worker_id=g_worker_id /* Added for bug 4083581 */
5021 			)
5022             AND ver.wp_flag = 'N'
5023             AND ver.baselined_flag = 'Y'
5024             AND ver.worker_id = g_worker_id;
5025 
5026   print_time('........GET_GLOBAL_EXCHANGE_RATES: End. # rows inserted is: ' || SQL%ROWCOUNT );
5027 
5028 EXCEPTION
5029   WHEN OTHERS THEN
5030     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5031                              p_procedure_name => 'GET_GLOBAL_EXCHANGE_RATES');
5032     print_time('........GET_GLOBAL_EXCHANGE_RATES: Exception: ' || sqlerrm );
5033     RAISE;
5034 END;
5035 
5036 
5037 PROCEDURE DELETE_GLOBAL_EXCHANGE_RATES IS
5038 BEGIN
5039 
5040   print_time('........DELETE_GLOBAL_EXCHANGE_RATES: Begin. ' );
5041 
5042   DELETE FROM PJI_FM_AGGR_DLY_RATES
5043   WHERE  worker_id = g_worker_id;
5044 
5045   print_time('........DELETE_GLOBAL_EXCHANGE_RATES: End. # rows is.. ' || SQL%ROWCOUNT );
5046 
5047 EXCEPTION
5048   WHEN OTHERS THEN
5049     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5050                              p_procedure_name => 'DELETE_GLOBAL_EXCHANGE_RATES');
5051     print_time('........DELETE_GLOBAL_EXCHANGE_RATES: Exception: ' || sqlerrm );
5052     RAISE;
5053 END;
5054 
5055 
5056 
5057 
5058 PROCEDURE CONV_TO_GLOBAL_CURRENCIES
5059 IS BEGIN
5060 
5061   print_time('........CONV_TO_GLOBAL_CURRENCIES: Begin. ' );
5062 
5063   NULL; -- Pull dangling implemented is changed.
5064 
5065 
5066   print_time('........CONV_TO_GLOBAL_CURRENCIES: End. ' );
5067 
5068 EXCEPTION
5069   WHEN OTHERS THEN
5070     print_time('........CONV_TO_GLOBAL_CURRENCIES: Exception ' || sqlerrm );
5071     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5072                              p_procedure_name => 'CONV_TO_GLOBAL_CURRENCIES');
5073     RAISE;
5074 END;
5075 
5076 
5077 PROCEDURE DELETE_DNGLRATE_PLNVER_DATA IS
5078 BEGIN
5079 
5080   DELETE FROM pji_fp_aggr_pjp1
5081   WHERE plan_version_id IN
5082     (
5083 	  SELECT DISTINCT plan_version_id
5084 	  FROM pji_fp_aggr_pjp1
5085 	  WHERE worker_id = g_worker_id
5086           AND rate_dangling_flag IS NOT NULL )
5087    AND worker_id = g_worker_id;
5088 
5089 EXCEPTION
5090   WHEN OTHERS THEN
5091     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5092                              p_procedure_name => 'DELETE_DNGLRATE_PLNVER_DATA');
5093     RAISE;
5094 END;
5095 
5096 
5097 
5098 PROCEDURE DO_CURRENCY_DANGLING_CHECK
5099 IS BEGIN
5100 
5101   NULL; -- Rate dangling check already done in get global exchange rates.
5102 
5103 EXCEPTION
5104   WHEN OTHERS THEN
5105     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5106                              p_procedure_name => 'DO_CURRENCY_DANGLING_CHECK');
5107     RAISE;
5108 END;
5109 
5110 
5111 PROCEDURE GET_PRORATE_FORMAT
5112 IS BEGIN
5113 
5114   NULL;
5115 
5116 EXCEPTION
5117   WHEN OTHERS THEN
5118     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5119                              p_procedure_name => 'GET_PRORATE_FORMAT');
5120     RAISE;
5121 END;
5122 
5123 
5124 PROCEDURE GET_SPREAD_DATE_RANGE_AMOUNTS
5125 IS BEGIN
5126 
5127   NULL;
5128 
5129 EXCEPTION
5130   WHEN OTHERS THEN
5131     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5132                              p_procedure_name => 'GET_SPREAD_DATE_RANGE_AMOUNTS');
5133     RAISE;
5134 END;
5135 
5136 
5137 PROCEDURE SPREAD_NON_TIME_PHASE_AMOUNTS
5138 IS BEGIN
5139 
5140   NULL;
5141 
5142 EXCEPTION
5143   WHEN OTHERS THEN
5144     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5145                              p_procedure_name => 'SPREAD_NON_TIME_PHASE_AMOUNTS');
5146     RAISE;
5147 END;
5148 
5149 
5150 
5151 
5152 PROCEDURE PRORATE_TO_OTHER_CALENDAR(
5153   p_calENDar_type    IN   VARCHAR2 := NULL -- Values can be GL, PA, ENT, ENTW.
5154 ) IS
5155 BEGIN
5156 
5157   --
5158   -- Logic to identify the records that are 'original'/'converted' format
5159   --   etc is similar to that for PJI. This is to make it easy for dev working
5160   --   on one module (PJI/PRP) to understand the other with minimum effort.
5161   --
5162   -- Logic is as follows:
5163   -------------------------------------------------------------------------
5164   -- 1. All 'entered' amounts (from pri slice) will have a line type *tag* 'OF'
5165   --     if calendar type is PA/GL. If no calendar type is mentioned,
5166   --     then, it will be 'F1'. Tag == line type column in tmp table.
5167   -- 2. Non time phased amounts if prorated to ENT cal will be tagged 'OF'.
5168   --     This has not been decided yet.
5169   -- 3. Amounts from PA/GL cal entries that have been converted to global will
5170   --     have a tag 'OG'.
5171   -- 4. Non PA calendar entries of OF/OG when converted to PA calendar will have
5172   --     a tag 'CF/CG' respectively.
5173   -- 5. Non GL calendar entries of OF/OG when converted to GL calendar will have
5174   --     a tag 'CF/CG' respectively.
5175   -- 6. Non ENT calendar entries of OF/OG when converted to ENT calendar will have
5176   --     a tag 'CF/CG' respectively.
5177   -- 7. Non ENTW calendar entries of OF/OG when converted to ENTW calendar will have
5178   --     a tag 'CF/CG' respectively.
5179   --
5180 
5181   IF (p_calENDar_type = 'PA') THEN
5182     PRORATE_TO_PA;
5183   ELSIF (p_calENDar_type = 'GL') THEN
5184     PRORATE_TO_GL;
5185   ELSIF (p_calENDar_type = 'ENT') THEN
5186     PRORATE_TO_ENT;
5187   END IF;
5188 
5189 EXCEPTION
5190   WHEN OTHERS THEN
5191     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5192                              p_procedure_name => 'PRORATE_TO_OTHER_CALENDAR');
5193     RAISE;
5194 END;
5195 
5196 
5197 PROCEDURE PRORATE_TO_PA IS
5198 BEGIN
5199 
5200   -- Refer to prorating logic in 'PRORATE_TO_OTHER_CALENDAR' proc.
5201   PRORATE('PA');
5202 
5203 EXCEPTION
5204   WHEN OTHERS THEN
5205     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5206                              p_procedure_name => 'PRORATE_TO_PA');
5207     RAISE;
5208 END;
5209 
5210 
5211 PROCEDURE PRORATE_TO_GL IS
5212 BEGIN
5213 
5214   -- Refer to prorating logic in 'PRORATE_TO_OTHER_CALENDAR' proc.
5215   PRORATE('GL');
5216 
5217 EXCEPTION
5218   WHEN OTHERS THEN
5219     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5220                              p_procedure_name => 'PRORATE_TO_GL');
5221     RAISE;
5222 END;
5223 
5224 
5225 --
5226 -- Prorate entries in RL fact and pjp1 table in
5227 --  1. GL cal and Non time phased entries into PA cal.
5228 --  2. PA cal and Non time phased entries into GL cal.
5229 --  3. PA, GL cals and Non time phased entries into Ent cal.
5230 --
5231 PROCEDURE PRORATE_TO_ALL_CALENDARS IS
5232 BEGIN
5233 
5234    print_time('PRORATE_TO_ALL_CALENDARS: Begin.. ');
5235 
5236    PRORATE('PA');                    -- Prorate entries in RL fact and pjp1 table
5237                                                            -- in GL cal and Non time phased entries into PA cal.
5238    PRORATE('GL');                    -- Prorate entries in RL fact and pjp1 table
5239                                                            -- in PA cal and Non time phased entries into GL cal.
5240    PRORATE_TO_ENT;                   -- Prorate entries in PA and GL cals into ENT cal.  /*for bug 3852901 */
5241 
5242    print_time('PRORATE_TO_ALL_CALENDARS: End.. ');
5243 
5244 EXCEPTION
5245   WHEN NO_DATA_FOUND THEN
5246 
5247         /* This issue will come only when PJI_PJP_FP_CURR_WRAP.get_ent_dates_info gives a no_data_found error.
5248            updating the plan versions as time dangling so that the plan will be marked as dangling. Not updating
5249            the existing records so that data for the primary calendar is not impacted because of issue in proration to
5250            other calendars */
5251 
5252         UPDATE PJI_FM_EXTR_PLNVER4
5253             SET time_dangling_flag='Y'
5254 	  WHERE wp_flag='N'
5255           AND baselined_flag = 'Y'
5256           AND worker_id = g_worker_id; -- Not raising any exception as we want the processing to happen properly.
5257   WHEN OTHERS THEN
5258     print_time('PRORATE_TO_ALL_CALENDARS: Exception.. ' || sqlerrm);
5259     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5260                              p_procedure_name => 'PRORATE_TO_ALL_CALENDARS');
5261     RAISE;
5262 END;
5263 
5264 
5265 PROCEDURE PRORATE_TO_ENT IS
5266     l_calendar_type  VARCHAR2(15) := 'E';
5267 BEGIN
5268 
5269    PJI_PJP_FP_CURR_WRAP.get_global_currency_info (
5270      x_currency_conversion_rule => g_currency_conversion_rule
5271    , x_prorating_format         => g_prorating_format
5272    , x_global1_currency_code    => g_global1_currency_code
5273    , x_global2_currency_code    => g_global2_currency_code
5274    , x_global1_currency_mau     => g_global1_currency_mau
5275    , x_global2_currency_mau     => g_global2_currency_mau ) ;
5276 
5277 
5278    PJI_PJP_FP_CURR_WRAP.get_ent_dates_info (
5279      x_global_start_date      => g_global_start_date
5280    , x_ent_start_period_id    => g_ent_start_period_id
5281    , x_ent_start_period_name  => g_ent_start_period_name
5282    , x_ent_start_date         => g_ent_start_date
5283    , x_ent_END_date           => g_ent_END_date
5284    , x_global_start_J         => g_global_start_J
5285    , x_ent_start_J            => g_ent_start_J
5286    , x_ent_END_J              => g_ent_END_J
5287   ) ;
5288 
5289   IF (g_prorating_format = 'D') THEN
5290 
5291     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_ENT_PG_PJP1_D;
5292     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_ENT_N_PJP1_D;
5293 
5294   ELSIF (g_prorating_format IN ( g_end_str, g_start_str ) ) THEN
5295 
5296     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_ENT_PG_PJP1_SE(g_prorating_format);
5297     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_ENT_N_PJP1_SE(g_prorating_format);
5298 
5299   ELSE
5300     NULL;
5301   END IF;
5302 
5303 EXCEPTION
5304   WHEN OTHERS THEN
5305     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5306                              p_procedure_name => 'PRORATE_TO_ENT');
5307     RAISE;
5308 END;
5309 
5310 
5311 PROCEDURE PRORATE(
5312   p_calENDar_type    IN   VARCHAR2 := NULL -- Values can be GL, PA, ENT, ENTW.
5313 ) IS
5314     l_calendar_type        VARCHAR2(15);
5315 
5316 BEGIN
5317 
5318   IF (p_calENDar_type NOT IN ('PA', 'GL')) THEN RETURN; END IF;
5319 
5320   IF (p_calENDar_type = 'PA') THEN l_calENDar_type := 'P';
5321   ELSIF (p_calENDar_type = 'GL') THEN l_calENDar_type := 'G';
5322   ELSE RETURN;
5323   END IF;
5324 
5325 
5326    PJI_PJP_FP_CURR_WRAP.get_global_currency_info (
5327      x_currency_conversion_rule => g_currency_conversion_rule
5328    , x_prorating_format         => g_prorating_format
5329    , x_global1_currency_code    => g_global1_currency_code
5330    , x_global2_currency_code    => g_global2_currency_code
5331    , x_global1_currency_mau     => g_global1_currency_mau
5332    , x_global2_currency_mau     => g_global2_currency_mau ) ;
5333 
5334 
5335    PJI_PJP_FP_CURR_WRAP.get_ent_dates_info (
5336      x_global_start_date      => g_global_start_date
5337    , x_ent_start_period_id    => g_ent_start_period_id
5338    , x_ent_start_period_name  => g_ent_start_period_name
5339    , x_ent_start_date         => g_ent_start_date
5340    , x_ent_END_date           => g_ent_END_date
5341    , x_global_start_J         => g_global_start_J
5342    , x_ent_start_J            => g_ent_start_J
5343    , x_ent_END_J              => g_ent_END_J
5344   ) ;
5345 
5346   print_time ( ' Prorating format is .. ' || g_prorating_format ) ;
5347 
5348   IF (g_prorating_format = 'D') THEN
5349 
5350     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_PAGL_PGE_PJP1_D( p_calendar_type => l_calendar_type );
5351     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_PAGL_N_PJP1_D( p_calendar_type => l_calendar_type );
5352 
5353   ELSIF (g_prorating_format IN ( 'E', 'S' ) ) THEN
5354 
5355     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_PAGL_PGE_PJP1_SE( p_calendar_type => l_calendar_type ,
5356                                                       p_prorating_format => g_prorating_format);
5357     PJI_FM_PLAN_CAL_RLPS.PRORATE_TO_PAGL_N_PJP1_SE( p_calendar_type => l_calendar_type ,
5358                                                       p_prorating_format => g_prorating_format);
5359 
5360   ELSE
5361     NULL;
5362   END IF;
5363 
5364 EXCEPTION
5365   WHEN OTHERS THEN
5366     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5367                              p_procedure_name => 'PRORATE');
5368     RAISE;
5369 END;
5370 
5371 
5372 PROCEDURE MERGE_INTO_FP_FACT IS
5373 BEGIN
5374 
5375   print_time ( ' MERGE_INTO_FP_FACT 2 ' ) ;
5376   GET_FP_ROW_IDS;
5377   print_time ( ' MERGE_INTO_FP_FACT 3 ' ) ;
5378   UPDATE_FP_ROWS;
5379   print_time ( ' MERGE_INTO_FP_FACT 4 ' ) ;
5380   INSERT_FP_ROWS;
5381   print_time ( ' MERGE_INTO_FP_FACT 5 ' ) ;
5382   CLEANUP_FP_RMAP_FPR;
5383   print_time ( ' MERGE_INTO_FP_FACT 6 ' ) ;
5384 
5385 EXCEPTION
5386   WHEN OTHERS THEN
5387     print_time ( ' MERGE_INTO_FP_FACT 6 ' ) ;
5388     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5389                              p_procedure_name => 'MERGE_INTO_FP_FACT');
5390     RAISE;
5391 END;
5392 
5393 
5394 PROCEDURE CLEANUP_FP_RMAP_FPR IS
5395 BEGIN
5396 
5397   print_time ( 'CLEANUP_FP_RMAP_FPR begin..');
5398 
5399   DELETE FROM PJI_FP_RMAP_FPR
5400   WHERE worker_id = g_worker_id;
5401 
5402   print_time ( 'CLEANUP_FP_RMAP_FPR end.' || g_worker_id );
5403 
5404 EXCEPTION
5405   WHEN OTHERS THEN
5406     print_time ( 'ccc' || sqlerrm );
5407     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5408                              p_procedure_name => 'CLEANUP_FP_RMAP_FPR');
5409     RAISE;
5410 END;
5411 
5412 
5413 PROCEDURE GET_FP_ROW_IDS IS
5414 BEGIN
5415 
5416   INSERT INTO PJI_FP_RMAP_FPR
5417   (
5418      worker_id
5419    , rl_rowid
5420    , tmp_rowid
5421   )
5422   SELECT  /*+ ORDERED full(tmp) index(rl,PJI_FP_XBS_ACCUM_F_N1) */
5423     g_worker_id WORKER_ID
5424   , rl.ROWID rl_rowid
5425   , tmp.ROWID TMP_ROWID
5426   FROM
5427     pji_fp_aggr_pjp1          tmp
5428   , pji_fp_xbs_accum_f          rl
5429   WHERE 1 = 1
5430 --   Removed outer join for bug 5927368
5431 --   AND tmp.WORKER_ID = g_worker_id
5432 --   AND tmp.PROJECT_ID = rl.PROJECT_ID (+)
5433 --   AND tmp.PLAN_VERSION_ID = rl.PLAN_VERSION_ID (+)
5434 --   AND tmp.PLAN_TYPE_ID = rl.PLAN_TYPE_ID (+)
5435 --   AND tmp.PLAN_TYPE_CODE = rl.PLAN_TYPE_CODE(+) /*4471527 */
5436 --   AND tmp.PROJECT_ORG_ID = rl.PROJECT_ORG_ID (+)
5437 --   AND tmp.PROJECT_ORGANIZATION_ID = rl.PROJECT_ORGANIZATION_ID (+)
5438 --   AND tmp.PROJECT_ELEMENT_ID = rl.PROJECT_ELEMENT_ID (+)
5439 --   AND tmp.TIME_ID = rl.TIME_ID (+)
5440 --   AND tmp.PERIOD_TYPE_ID = rl.PERIOD_TYPE_ID (+)
5441 --   AND tmp.CALENDAR_TYPE = rl.CALENDAR_TYPE (+)
5442 --   AND tmp.RBS_AGGR_LEVEL = rl.RBS_AGGR_LEVEL (+)
5443 --   AND tmp.WBS_ROLLUP_FLAG = rl.WBS_ROLLUP_FLAG (+)
5444 --   AND tmp.PRG_ROLLUP_FLAG = rl.PRG_ROLLUP_FLAG (+)
5445 --   AND tmp.CURR_RECORD_TYPE_ID = rl.CURR_RECORD_TYPE_ID (+)
5446 --   AND tmp.CURRENCY_CODE = rl.CURRENCY_CODE (+)
5447 --   AND tmp.RBS_ELEMENT_ID = rl.RBS_ELEMENT_ID (+)
5448 --   AND tmp.RBS_VERSION_ID = rl.RBS_VERSION_ID (+)
5449 --   AND tmp.RATE_DANGLING_FLAG IS NULL
5450 --   AND tmp.TIME_DANGLING_FLAG IS NULL;
5451    AND tmp.WORKER_ID = g_worker_id
5452    AND tmp.PROJECT_ID = rl.PROJECT_ID
5453    AND tmp.PLAN_VERSION_ID = rl.PLAN_VERSION_ID
5454    AND tmp.PLAN_TYPE_ID = rl.PLAN_TYPE_ID
5455    AND tmp.PLAN_TYPE_CODE = rl.PLAN_TYPE_CODE
5456    AND tmp.PROJECT_ORG_ID = rl.PROJECT_ORG_ID
5457    AND tmp.PROJECT_ORGANIZATION_ID = rl.PROJECT_ORGANIZATION_ID
5458    AND tmp.PROJECT_ELEMENT_ID = rl.PROJECT_ELEMENT_ID
5459    AND tmp.TIME_ID = rl.TIME_ID
5460    AND tmp.PERIOD_TYPE_ID = rl.PERIOD_TYPE_ID
5461    AND tmp.CALENDAR_TYPE = rl.CALENDAR_TYPE
5462    AND tmp.RBS_AGGR_LEVEL = rl.RBS_AGGR_LEVEL
5463    AND tmp.WBS_ROLLUP_FLAG = rl.WBS_ROLLUP_FLAG
5464    AND tmp.PRG_ROLLUP_FLAG = rl.PRG_ROLLUP_FLAG
5465    AND tmp.CURR_RECORD_TYPE_ID = rl.CURR_RECORD_TYPE_ID
5466    AND tmp.CURRENCY_CODE = rl.CURRENCY_CODE
5467    AND tmp.RBS_ELEMENT_ID = rl.RBS_ELEMENT_ID
5468    AND tmp.RBS_VERSION_ID = rl.RBS_VERSION_ID
5469    AND tmp.RATE_DANGLING_FLAG IS NULL
5470    AND tmp.TIME_DANGLING_FLAG IS NULL;
5471 
5472   print_time ( ' MERGE_INTO_FP_FACT 1.1 ' || SQL%ROWCOUNT ) ;
5473 
5474 EXCEPTION
5475   WHEN OTHERS THEN
5476     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5477                              p_procedure_name => 'GET_FP_ROW_IDS');
5478     RAISE;
5479 END;
5480 
5481 
5482 
5483 PROCEDURE UPDATE_FP_ROWS IS
5484 
5485   l_last_update_date  DATE   := SYSDATE;
5486   l_last_updated_by   NUMBER := FND_GLOBAL.USER_ID;
5487   l_last_update_login NUMBER := FND_GLOBAL.LOGIN_ID;
5488 
5489 BEGIN
5490 
5491 
5492 
5493     UPDATE /*+ ordered use_nl(rl) rowid(rl) */
5494       pji_fp_xbs_accum_f rl
5495     SET (
5496       rl.RAW_COST
5497     , rl.BRDN_COST
5498     , rl.REVENUE
5499     , rl.BILL_RAW_COST
5500     , rl.BILL_BRDN_COST
5501     , rl.BILL_LABOR_RAW_COST
5502     , rl.BILL_LABOR_BRDN_COST
5503     , rl.BILL_LABOR_HRS
5504     , rl.EQUIPMENT_RAW_COST
5505     , rl.EQUIPMENT_BRDN_COST
5506     , rl.CAPITALIZABLE_RAW_COST
5507     , rl.CAPITALIZABLE_BRDN_COST
5508     , rl.LABOR_RAW_COST
5509     , rl.LABOR_BRDN_COST
5510     , rl.LABOR_HRS
5511     , rl.LABOR_REVENUE
5512     , rl.EQUIPMENT_HOURS
5513     , rl.BILLABLE_EQUIPMENT_HOURS
5514     , rl.SUP_INV_COMMITTED_COST
5515     , rl.PO_COMMITTED_COST
5516     , rl.PR_COMMITTED_COST
5517     , rl.OTH_COMMITTED_COST
5518        , rl.ACT_LABOR_HRS
5519 	   , rl.ACT_EQUIP_HRS
5520 	   , rl.ACT_LABOR_BRDN_COST
5521 	   , rl.ACT_EQUIP_BRDN_COST
5522 	   , rl.ACT_BRDN_COST
5523 	   , rl.ACT_RAW_COST
5524 	   , rl.ACT_REVENUE
5525          , rl.ACT_LABOR_RAW_COST
5526          , rl.ACT_EQUIP_RAW_COST
5527 	   , rl.ETC_LABOR_HRS
5528 	   , rl.ETC_EQUIP_HRS
5529 	   , rl.ETC_LABOR_BRDN_COST
5530 	   , rl.ETC_EQUIP_BRDN_COST
5531 	   , rl.ETC_BRDN_COST
5532          , rl.ETC_RAW_COST
5533          , rl.ETC_LABOR_RAW_COST
5534          , rl.ETC_EQUIP_RAW_COST
5535     , rl.CUSTOM1
5536     , rl.CUSTOM2
5537     , rl.CUSTOM3
5538     , rl.CUSTOM4
5539     , rl.CUSTOM5
5540     , rl.CUSTOM6
5541     , rl.CUSTOM7
5542     , rl.CUSTOM8
5543     , rl.CUSTOM9
5544     , rl.CUSTOM10
5545     , rl.CUSTOM11
5546     , rl.CUSTOM12
5547     , rl.CUSTOM13
5548     , rl.CUSTOM14
5549     , rl.CUSTOM15
5550     , rl.last_update_date
5551     , rl.last_updated_by
5552     , rl.last_update_login
5553    	) =
5554     (
5555       SELECT  /*+  ORDERED ROWID(TMP) index(rwid,PJI_FP_RMAP_FPR_N1) */
5556          NVL(rl.RAW_COST, 0) + NVL(tmp.RAW_COST, 0)
5557        , NVL(rl.BRDN_COST, 0) + NVL(tmp.BRDN_COST, 0)
5558        , NVL(rl.REVENUE, 0) + NVL(tmp.REVENUE, 0)
5559        , NVL(rl.BILL_RAW_COST, 0) + NVL(tmp.BILL_RAW_COST, 0)
5560        , NVL(rl.BILL_BRDN_COST, 0) + NVL(tmp.BILL_BRDN_COST, 0)
5561        , NVL(rl.BILL_LABOR_RAW_COST, 0) + NVL(tmp.BILL_LABOR_RAW_COST, 0)
5562        , NVL(rl.BILL_LABOR_BRDN_COST, 0) + NVL(tmp.BILL_LABOR_BRDN_COST, 0)
5563        , NVL(rl.BILL_LABOR_HRS, 0) + NVL(tmp.BILL_LABOR_HRS, 0)
5564        , NVL(rl.EQUIPMENT_RAW_COST, 0) + NVL(tmp.EQUIPMENT_RAW_COST, 0)
5565        , NVL(rl.EQUIPMENT_BRDN_COST, 0) + NVL(tmp.EQUIPMENT_BRDN_COST, 0)
5566        , NVL(rl.CAPITALIZABLE_RAW_COST, 0) + NVL(tmp.CAPITALIZABLE_RAW_COST  , 0)
5567        , NVL(rl.CAPITALIZABLE_BRDN_COST, 0) + NVL(tmp.CAPITALIZABLE_BRDN_COST    , 0)
5568        , NVL(rl.LABOR_RAW_COST, 0) + NVL(tmp.LABOR_RAW_COST, 0)
5569        , NVL(rl.LABOR_BRDN_COST, 0) + NVL(tmp.LABOR_BRDN_COST, 0)
5570        , NVL(rl.LABOR_HRS, 0) + NVL(tmp.LABOR_HRS, 0)
5571        , NVL(rl.LABOR_REVENUE, 0) + NVL(tmp.LABOR_REVENUE, 0)
5572        , NVL(rl.EQUIPMENT_HOURS, 0) + NVL(tmp.EQUIPMENT_HOURS, 0)
5573        , NVL(rl.BILLABLE_EQUIPMENT_HOURS, 0) + NVL(tmp.BILLABLE_EQUIPMENT_HOURS, 0)
5574        , NVL(rl.SUP_INV_COMMITTED_COST, 0) + NVL(tmp.SUP_INV_COMMITTED_COST, 0)
5575        , NVL(rl.PO_COMMITTED_COST, 0) + NVL(tmp.PO_COMMITTED_COST, 0)
5576        , NVL(rl.PR_COMMITTED_COST, 0) + NVL(tmp.PR_COMMITTED_COST, 0)
5577        , NVL(rl.OTH_COMMITTED_COST, 0) + NVL(tmp.OTH_COMMITTED_COST, 0)
5578        , NVL(rl.ACT_LABOR_HRS, 0) + NVL(tmp.ACT_LABOR_HRS, 0)
5579 	   , NVL(rl.ACT_EQUIP_HRS, 0) + NVL(tmp.ACT_EQUIP_HRS, 0)
5580 	   , NVL(rl.ACT_LABOR_BRDN_COST, 0) + NVL(tmp.ACT_LABOR_BRDN_COST, 0)
5581 	   , NVL(rl.ACT_EQUIP_BRDN_COST, 0) + NVL(tmp.ACT_EQUIP_BRDN_COST, 0)
5582 	   , NVL(rl.ACT_BRDN_COST, 0) + NVL(tmp.ACT_BRDN_COST, 0)
5583 	   , NVL(rl.ACT_RAW_COST, 0) + NVL(tmp.ACT_RAW_COST, 0)
5584 	   , NVL(rl.ACT_REVENUE, 0) + NVL(tmp.ACT_REVENUE, 0)
5585          , NVL(rl.ACT_LABOR_RAW_COST, 0) + NVL(tmp.ACT_LABOR_RAW_COST, 0)
5586          , NVL(rl.ACT_EQUIP_RAW_COST, 0) + NVL(tmp.ACT_EQUIP_RAW_COST, 0)
5587 	   , DECODE ( ver3.wp_flag
5588                       , 'Y'
5589                       , DECODE(TO_CHAR(tmp.ETC_LABOR_HRS)
5590 	                       , NULL
5591                              , NVL(rl.ETC_LABOR_HRS, 0) + NVL(tmp.labor_hrs, 0)
5592 			                 , NVL(rl.ETC_LABOR_HRS, 0) + tmp.ETC_LABOR_HRS
5593 										     )
5594 	   , NVL(rl.ETC_LABOR_HRS, 0) + NVL(tmp.ETC_LABOR_HRS, 0)
5595 		      ) ETC_LABOR_HRS
5596 		 , DECODE ( ver3.wp_flag
5597                       , 'Y'
5598                       , DECODE(TO_CHAR(tmp.ETC_EQUIP_HRS)
5599 		                     , NULL
5600                              , NVL(rl.ETC_EQUIP_HRS, 0) + NVL(tmp.EQUIPMENT_hours, 0)
5601 		 		             , NVL(rl.ETC_EQUIP_HRS, 0) + tmp.ETC_EQUIP_HRS
5602 										     )
5603 	   , NVL(rl.ETC_EQUIP_HRS, 0) + NVL(tmp.ETC_EQUIP_HRS, 0)
5604 			    ) ETC_EQUIP_HRS
5605 		 , DECODE ( ver3.wp_flag
5606                       , 'Y'
5607                       , DECODE(TO_CHAR(tmp.ETC_LABOR_BRDN_COST)
5608 		                 , NULL
5609                              , NVL(rl.ETC_LABOR_BRDN_COST, 0) + NVL(tmp.labor_BRDN_COST, 0)
5610                              , NVL(rl.ETC_LABOR_BRDN_COST, 0) + tmp.ETC_LABOR_BRDN_COST
5611 										     )
5612 	   , NVL(rl.ETC_LABOR_BRDN_COST, 0) + NVL(tmp.ETC_LABOR_BRDN_COST, 0)
5613 				  ) ETC_LABOR_BRDN_COST
5614 		 , DECODE ( ver3.wp_flag
5615                       , 'Y'
5616                       , DECODE(TO_CHAR(tmp.ETC_EQUIP_BRDN_COST)
5617 		                 , NULL
5618                       , NVL(rl.ETC_equip_BRDN_COST, 0) + NVL(tmp.EQUIPment_BRDN_COST, 0)
5619  		              , NVL(rl.ETC_equip_BRDN_COST, 0) + tmp.ETC_equip_BRDN_COST
5620 										     )
5621 	   , NVL(rl.ETC_EQUIP_BRDN_COST, 0) + NVL(tmp.ETC_EQUIP_BRDN_COST, 0)
5622 				  ) ETC_EQUIP_BRDN_COST
5623 		 , DECODE ( ver3.wp_flag
5624                       , 'Y'
5625                       , DECODE(TO_CHAR(tmp.ETC_BRDN_COST)
5626 		                 , NULL
5627                              , NVL(rl.ETC_BRDN_COST, 0) + NVL(tmp.BRDN_COST, 0)
5628                              , NVL(rl.ETC_BRDN_COST, 0) + tmp.ETC_BRDN_COST
5629 
5630 										     )
5631 							    , NVL(rl.ETC_BRDN_COST, 0) + NVL(tmp.ETC_BRDN_COST, 0)
5632 				  ) ETC_BRDN_COST
5633 		 , DECODE ( ver3.wp_flag
5634                      , 'Y'
5635                      , DECODE(TO_CHAR(tmp.ETC_raw_COST)
5636 		                , NULL
5637                      , NVL(rl.ETC_raw_COST, 0) + NVL(tmp.raw_COST, 0)
5638                      , NVL(rl.ETC_raw_COST, 0) + tmp.ETC_raw_COST
5639 										     )
5640 							    , NVL(rl.ETC_raw_COST, 0) + NVL(tmp.ETC_raw_COST, 0)
5641 				  ) ETC_RAW_COST
5642 		 , DECODE ( ver3.wp_flag
5643                       , 'Y'
5644                       , DECODE(TO_CHAR(tmp.ETC_labor_raw_COST)
5645 		                 , NULL
5646                              , NVL(rl.ETC_labor_raw_COST, 0) + NVL(tmp.labor_raw_COST, 0)
5647   				             , NVL(rl.ETC_labor_raw_COST, 0) + tmp.ETC_labor_raw_COST
5648 										     )
5649 							    , NVL(rl.ETC_labor_raw_COST, 0) + NVL(tmp.ETC_labor_raw_COST, 0)
5650 				  ) ETC_LABOR_RAW_COST
5651 		 , DECODE ( ver3.wp_flag
5652                       , 'Y'
5653                       , DECODE(TO_CHAR(tmp.ETC_equip_raw_COST)
5654 		                 , NULL
5655                              , NVL(rl.ETC_equip_raw_COST, 0) + NVL(tmp.equipment_raw_COST, 0)
5656                              , NVL(rl.ETC_equip_raw_COST, 0) + tmp.ETC_equip_raw_COST
5657 										     )
5658 							    , NVL(rl.ETC_equip_raw_COST, 0) + NVL(tmp.ETC_equip_raw_COST, 0)
5659 			    ) ETC_EQUIP_RAW_COST
5660        , NVL(rl.CUSTOM1, 0) + NVL(tmp.CUSTOM1, 0)
5661        , NVL(rl.CUSTOM2, 0) + NVL(tmp.CUSTOM2, 0)
5662        , NVL(rl.CUSTOM3, 0) + NVL(tmp.CUSTOM3, 0)
5663        , NVL(rl.CUSTOM4, 0) + NVL(tmp.CUSTOM4, 0)
5664        , NVL(rl.CUSTOM5, 0) + NVL(tmp.CUSTOM5, 0)
5665        , NVL(rl.CUSTOM6, 0) + NVL(tmp.CUSTOM6, 0)
5666        , NVL(rl.CUSTOM7, 0) + NVL(tmp.CUSTOM7, 0)
5667        , NVL(rl.CUSTOM8, 0) + NVL(tmp.CUSTOM8, 0)
5668        , NVL(rl.CUSTOM9, 0) + NVL(tmp.CUSTOM9, 0)
5669        , NVL(rl.CUSTOM10, 0) + NVL(tmp.CUSTOM10, 0)
5670        , NVL(rl.CUSTOM11, 0) + NVL(tmp.CUSTOM11, 0)
5671        , NVL(rl.CUSTOM12, 0) + NVL(tmp.CUSTOM12, 0)
5672        , NVL(rl.CUSTOM13, 0) + NVL(tmp.CUSTOM13, 0)
5673        , NVL(rl.CUSTOM14, 0) + NVL(tmp.CUSTOM14, 0)
5674        , NVL(rl.CUSTOM15, 0) + NVL(tmp.CUSTOM15, 0)
5675        , SYSDATE -- l_last_update_date
5676        , l_last_updated_by
5677        , l_last_update_login
5678      FROM
5679          PJI_FP_RMAP_FPR           rwid
5680        , pji_fp_aggr_pjp1          tmp
5681 	 , pji_pjp_wbs_header        ver3  -- replaced ver3 with wbs header for project-to-program association event.
5682      WHERE  1 = 1
5683   	  AND tmp.rowid = rwid.tmp_rowid
5684 	  AND rl.rowid = rwid.rl_rowid
5685 --Commented for bug 5927368
5686 --	  AND rwid.rl_rowid IS NOT NULL
5687 	  AND ver3.plan_version_id = tmp.plan_version_id
5688         AND rwid.worker_id = g_worker_id
5689         AND tmp.worker_id = g_worker_id
5690         AND tmp.project_id = ver3.project_id
5691         AND NVL(tmp.plan_type_id,-1) = NVL(ver3.plan_type_id, -1) -- each plan type can have a different -3, -4 slice.
5692         AND ver3.plan_type_code = tmp.plan_type_code    /*4471527 */
5693 	)
5694      WHERE rl.rowid IN
5695              (
5696 		   SELECT rwid.rl_rowid
5697 		   FROM PJI_FP_RMAP_FPR rwid
5698 --commented for bug 5927368
5699 --		   WHERE rwid.rl_rowid IS NOT NULL
5700 --                 AND rwid.worker_id = g_worker_id
5701 		   WHERE rwid.worker_id = g_worker_id
5702 		   );
5703 
5704   print_time ( ' MERGE_INTO_FP_FACT 2.1 ' || sql%rowcount ) ;
5705 
5706 
5707 EXCEPTION
5708   WHEN OTHERS THEN
5709     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5710                              p_procedure_name => 'UPDATE_FP_ROWS');
5711     RAISE;
5712 END;
5713 
5714 
5715 PROCEDURE INSERT_FP_ROWS IS
5716 
5717     l_last_update_date     date   := SYSDATE;
5718     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
5719     l_creation_date        date   := SYSDATE;
5720     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
5721     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
5722 
5723 BEGIN
5724 
5725 --Start of addition for bug 5927368
5726   update /*+ use_nl(pjp1_u) rowid(pjp1_u) */
5727          PJI_FP_AGGR_PJP1 pjp1_u
5728   set    RECORD_TYPE = 'U'
5729   where  ROWID in (select rwid.TMP_ROWID
5730                    from   PJI_FP_RMAP_FPR rwid
5731                    where  rwid.WORKER_ID = g_worker_id) and
5732          nvl(RECORD_TYPE, 'X') <> 'U';
5733   commit;
5734 --End of addition for bug 5927368
5735 
5736   INSERT INTO pji_fp_xbs_accum_f  fact
5737   (
5738        PROJECT_ID
5739      , PROJECT_ORG_ID
5740      , PROJECT_ORGANIZATION_ID
5741      , PROJECT_ELEMENT_ID
5742      , TIME_ID
5743      , PERIOD_TYPE_ID
5744      , CALENDAR_TYPE
5745      , RBS_AGGR_LEVEL
5746      , WBS_ROLLUP_FLAG
5747      , PRG_ROLLUP_FLAG
5748      , CURR_RECORD_TYPE_ID
5749      , CURRENCY_CODE
5750      , RBS_ELEMENT_ID
5751      , RBS_VERSION_ID
5752      , PLAN_VERSION_ID
5753      , PLAN_TYPE_ID
5754      , LAST_UPDATE_DATE
5755      , LAST_UPDATED_BY
5756      , CREATION_DATE
5757      , CREATED_BY
5758      , LAST_UPDATE_LOGIN
5759      , RAW_COST
5760      , BRDN_COST
5761      , REVENUE
5762      , BILL_RAW_COST
5763      , BILL_BRDN_COST
5764      , BILL_LABOR_RAW_COST
5765      , BILL_LABOR_BRDN_COST
5766      , BILL_LABOR_HRS
5767      , EQUIPMENT_RAW_COST
5768      , EQUIPMENT_BRDN_COST
5769      , CAPITALIZABLE_RAW_COST
5770      , CAPITALIZABLE_BRDN_COST
5771      , LABOR_RAW_COST
5772      , LABOR_BRDN_COST
5773      , LABOR_HRS
5774      , LABOR_REVENUE
5775      , EQUIPMENT_HOURS
5776      , BILLABLE_EQUIPMENT_HOURS
5777      , SUP_INV_COMMITTED_COST
5778      , PO_COMMITTED_COST
5779      , PR_COMMITTED_COST
5780      , OTH_COMMITTED_COST
5781        , ACT_LABOR_HRS
5782 	   , ACT_EQUIP_HRS
5783 	   , ACT_LABOR_BRDN_COST
5784 	   , ACT_EQUIP_BRDN_COST
5785 	   , ACT_BRDN_COST
5786 	   , ACT_RAW_COST
5787 	   , ACT_REVENUE
5788          , ACT_LABOR_RAW_COST
5789          , ACT_EQUIP_RAW_COST
5790 	   , ETC_LABOR_HRS
5791 	   , ETC_EQUIP_HRS
5792 	   , ETC_LABOR_BRDN_COST
5793 	   , ETC_EQUIP_BRDN_COST
5794 	   , ETC_BRDN_COST
5795          , ETC_RAW_COST
5796          , ETC_LABOR_RAW_COST
5797          , ETC_EQUIP_RAW_COST
5798      , CUSTOM1
5799      , CUSTOM2
5800      , CUSTOM3
5801      , CUSTOM4
5802      , CUSTOM5
5803      , CUSTOM6
5804      , CUSTOM7
5805      , CUSTOM8
5806      , CUSTOM9
5807      , CUSTOM10
5808      , CUSTOM11
5809      , CUSTOM12
5810      , CUSTOM13
5811      , CUSTOM14
5812      , CUSTOM15
5813      , PLAN_TYPE_CODE   /*4471527 */
5814   )
5815  --Commented hint for bug 5927368
5816   SELECT  /*+ ordered index(ver3 PJI_PJP_WBS_HEADER_N1) */
5817 --  SELECT  / *+ ordered full(rwid) rowid(tmp) * /
5818        tmp.PROJECT_ID
5819      , tmp.PROJECT_ORG_ID
5820      , tmp.PROJECT_ORGANIZATION_ID
5821      -- , PARTITION_ID
5822      , tmp.PROJECT_ELEMENT_ID
5823      , tmp.TIME_ID
5824      , tmp.PERIOD_TYPE_ID
5825      , tmp.CALENDAR_TYPE
5826      , tmp.RBS_AGGR_LEVEL
5827      , tmp.WBS_ROLLUP_FLAG
5828      , tmp.PRG_ROLLUP_FLAG
5829      , tmp.CURR_RECORD_TYPE_ID
5830      , tmp.CURRENCY_CODE
5831      , tmp.RBS_ELEMENT_ID
5832      , tmp.RBS_VERSION_ID
5833      , ver3.PLAN_VERSION_ID
5834      , tmp.PLAN_TYPE_ID
5835      , l_last_update_date
5836      , l_last_updated_by
5837      , l_creation_date
5838      , l_created_by
5839      , l_last_update_login
5840      , RAW_COST
5841      , BRDN_COST
5842      , REVENUE
5843      , BILL_RAW_COST
5844      , BILL_BRDN_COST
5845      , BILL_LABOR_RAW_COST
5846      , BILL_LABOR_BRDN_COST
5847      , BILL_LABOR_HRS
5848      , EQUIPMENT_RAW_COST
5849      , EQUIPMENT_BRDN_COST
5850      , CAPITALIZABLE_RAW_COST
5851      , CAPITALIZABLE_BRDN_COST
5852      , LABOR_RAW_COST
5853      , LABOR_BRDN_COST
5854      , LABOR_HRS
5855      , LABOR_REVENUE
5856      , EQUIPMENT_HOURS
5857      , BILLABLE_EQUIPMENT_HOURS
5858      , SUP_INV_COMMITTED_COST
5859      , PO_COMMITTED_COST
5860      , PR_COMMITTED_COST
5861      , OTH_COMMITTED_COST
5862        , ACT_LABOR_HRS
5863 	   , ACT_EQUIP_HRS
5864 	   , ACT_LABOR_BRDN_COST
5865 	   , ACT_EQUIP_BRDN_COST
5866 	   , ACT_BRDN_COST
5867 	   , ACT_RAW_COST
5868 	   , ACT_REVENUE
5869          , ACT_LABOR_RAW_COST
5870          , ACT_EQUIP_RAW_COST
5871 	   , DECODE ( ver3.wp_flag
5872                       , 'Y'
5873                       , DECODE(TO_CHAR(tmp.ETC_LABOR_HRS)  -- For Workplan
5874 	                         , NULL
5875                              , NVL(tmp.labor_hrs, 0)
5876                              , NVL(tmp.ETC_LABOR_HRS, 0)
5877                               )
5878 				      , NVL(tmp.ETC_LABOR_HRS, 0)
5879 		       ) ETC_LABOR_HRS
5880 		 , DECODE ( ver3.wp_flag
5881                           , 'Y'
5882                           , DECODE(TO_CHAR(tmp.ETC_EQUIP_HRS)
5883 		                         , NULL
5884                                  , NVL(tmp.EQUIPMENT_hours, 0)
5885 					             , NVL(tmp.ETC_EQUIP_HRS, 0)
5886 					    )
5887 			       , NVL(tmp.ETC_EQUIP_HRS, 0)
5888 			    ) ETC_EQUIP_HRS
5889 		 , DECODE ( ver3.wp_flag
5890                       , 'Y'
5891                       , DECODE(TO_CHAR(tmp.ETC_LABOR_BRDN_COST)
5892 		                     , NULL
5893                              , NVL(tmp.labor_BRDN_COST, 0)
5894 				             , NVL(tmp.ETC_LABOR_BRDN_COST, 0)
5895 					 )
5896 			         , NVL(tmp.ETC_LABOR_BRDN_COST, 0)
5897 			   ) ETC_LABOR_BRDN_COST
5898 		 , DECODE ( ver3.wp_flag
5899                       , 'Y'
5900                       , DECODE(TO_CHAR(tmp.ETC_EQUIP_BRDN_COST)
5901 		                     , NULL
5902                              , NVL(tmp.EQUIPment_BRDN_COST, 0)
5903 	                         , NVL(tmp.ETC_equip_BRDN_COST, 0)
5904 				      )
5905 			          , NVL(tmp.ETC_EQUIP_BRDN_COST, 0)
5906 				  ) ETC_equip_BRDN_COST
5907 		 , DECODE ( ver3.wp_flag
5908                       , 'Y'
5909                       , DECODE(TO_CHAR(tmp.ETC_BRDN_COST)
5910 		                     , NULL
5911                              , NVL(tmp.BRDN_COST, 0)
5912 				             , NVL(tmp.ETC_BRDN_COST, 0)
5913 				      )
5914 			        , NVL(tmp.ETC_BRDN_COST, 0)
5915 				  ) ETC_BRDN_COST
5916 		 , DECODE ( ver3.wp_flag
5917                      , 'Y'
5918                      , DECODE(TO_CHAR(tmp.ETC_raw_COST)
5919 		                    , NULL
5920                             , NVL(tmp.raw_COST, 0)
5921 				            , NVL(tmp.ETC_raw_COST, 0)
5922 				     )
5923 			       , NVL(tmp.ETC_raw_COST, 0)
5924 				  ) ETC_raw_COST
5925 		 , DECODE ( ver3.wp_flag
5926                       , 'Y'
5927                       , DECODE(TO_CHAR(tmp.ETC_labor_raw_COST)
5928 		                     , NULL
5929                              , NVL(tmp.labor_raw_COST, 0)
5930 				             , NVL(tmp.ETC_labor_raw_COST, 0)
5931 			  	      )
5932 			        , NVL(tmp.ETC_labor_raw_COST, 0)
5933 				  ) ETC_labor_raw_COST
5934 		 , DECODE ( ver3.wp_flag
5935                       , 'Y'
5936                       , DECODE(TO_CHAR(tmp.ETC_equip_raw_COST)
5937 		                     , NULL
5938                              , NVL(tmp.equipment_raw_COST, 0)
5939                              ,  NVL(tmp.ETC_equip_raw_COST, 0)
5940 				      )
5941 			        , NVL(tmp.ETC_equip_raw_COST, 0)
5942 			    ) ETC_equip_raw_COST
5943 	 , CUSTOM1
5944      , CUSTOM2
5945      , CUSTOM3
5946      , CUSTOM4
5947      , CUSTOM5
5948      , CUSTOM6
5949      , CUSTOM7
5950      , CUSTOM8
5951      , CUSTOM9
5952      , CUSTOM10
5953      , CUSTOM11
5954      , CUSTOM12
5955      , CUSTOM13
5956      , CUSTOM14
5957      , CUSTOM15
5958      ,tmp.PLAN_TYPE_CODE  PLAN_TYPE_CODE  /*4471527 */
5959   FROM
5960 --Commented for 5927368
5961 --         PJI_FP_RMAP_FPR  rwid
5962          pji_fp_aggr_pjp1 tmp
5963        , pji_pjp_wbs_header ver3  -- replaced ver3 with wbs header for project-to-program association event.
5964   WHERE 1 = 1
5965    AND tmp.worker_id = g_worker_id
5966 --commented for 5927368 and added tmp.RECORD_TYPE <> 'U' condition
5967 --   AND rwid.worker_id = g_worker_id
5968 --   AND tmp.rowid = rwid.tmp_rowid
5969 --   AND rwid.rl_rowid IS NULL
5970    AND nvl(tmp.RECORD_TYPE, 'X') <> 'U'
5971 --
5972    AND ver3.plan_version_id = tmp.plan_version_id
5973    AND ver3.plan_type_code = tmp.plan_type_code     /*4471527 */
5974    AND tmp.project_id = ver3.project_id -- use index.
5975    AND NVL(tmp.plan_type_id,-1) = NVL(ver3.plan_type_id, -1) -- each plan type can have a different -3, -4 slice.
5976   ORDER BY
5977     tmp.PROJECT_ID
5978   , ver3.PLAN_VERSION_ID
5979   , tmp.PROJECT_ELEMENT_ID
5980   , tmp.TIME_ID
5981   , tmp.RBS_VERSION_ID;
5982 
5983   print_time ( ' MERGE_INTO_FP_FACT 3.1 worker id..' || g_worker_id || 'row count '  || SQL%ROWCOUNT) ;
5984 
5985 
5986 EXCEPTION
5987   WHEN OTHERS THEN
5988     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
5989                              p_procedure_name => 'INSERT_FP_ROWS');
5990     RAISE;
5991 END;
5992 
5993 
5994 PROCEDURE INSERT_INTO_AC_FACT IS
5995     l_last_update_date     date   := SYSDATE;
5996     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
5997     l_creation_date        date   := SYSDATE;
5998     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
5999     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
6000 BEGIN
6001 
6002   INSERT INTO PJI_AC_XBS_ACCUM_F
6003   (
6004      PROJECT_ID
6005    , PROJECT_ORG_ID
6006    , PROJECT_ORGANIZATION_ID
6007    , PROJECT_ELEMENT_ID
6008    , TIME_ID
6009    , PERIOD_TYPE_ID
6010    , CALENDAR_TYPE
6011    , WBS_ROLLUP_FLAG
6012    , PRG_ROLLUP_FLAG
6013    , CURR_RECORD_TYPE_ID
6014    , CURRENCY_CODE
6015    , REVENUE
6016    , INITIAL_FUNDING_AMOUNT
6017    , INITIAL_FUNDING_COUNT
6018    , ADDITIONAL_FUNDING_AMOUNT
6019    , ADDITIONAL_FUNDING_COUNT
6020    , CANCELLED_FUNDING_AMOUNT
6021    , CANCELLED_FUNDING_COUNT
6022    , FUNDING_ADJUSTMENT_AMOUNT
6023    , FUNDING_ADJUSTMENT_COUNT
6024    , REVENUE_WRITEOFF
6025    , AR_INVOICE_AMOUNT
6026    , AR_INVOICE_COUNT
6027    , AR_CASH_APPLIED_AMOUNT
6028    , AR_INVOICE_WRITE_OFF_AMOUNT
6029    , AR_INVOICE_WRITEOFF_COUNT
6030    , AR_CREDIT_MEMO_AMOUNT
6031    , AR_CREDIT_MEMO_COUNT
6032    , UNBILLED_RECEIVABLES
6033    , UNEARNED_REVENUE
6034    , AR_UNAPPR_INVOICE_AMOUNT
6035    , AR_UNAPPR_INVOICE_COUNT
6036    , AR_APPR_INVOICE_AMOUNT
6037    , AR_APPR_INVOICE_COUNT
6038    , AR_AMOUNT_DUE
6039    , AR_COUNT_DUE
6040    , AR_AMOUNT_OVERDUE
6041    , AR_COUNT_OVERDUE
6042    , DORMANT_BACKLOG_INACTIV
6043    , DORMANT_BACKLOG_START
6044    , LOST_BACKLOG
6045    , ACTIVE_BACKLOG
6046    , REVENUE_AT_RISK
6047    , LAST_UPDATE_DATE
6048    , LAST_UPDATED_BY
6049    , CREATION_DATE
6050    , CREATED_BY
6051    , LAST_UPDATE_LOGIN
6052    , CUSTOM1
6053    , CUSTOM2
6054    , CUSTOM3
6055    , CUSTOM4
6056    , CUSTOM5
6057    , CUSTOM6
6058    , CUSTOM7
6059    , CUSTOM8
6060    , CUSTOM9
6061    , CUSTOM10
6062    , CUSTOM11
6063    , CUSTOM12
6064    , CUSTOM13
6065    , CUSTOM14
6066    , CUSTOM15
6067   )
6068   SELECT
6069      PROJECT_ID
6070    , PROJECT_ORG_ID
6071    , PROJECT_ORGANIZATION_ID
6072    , PROJECT_ELEMENT_ID
6073    , TIME_ID
6074    , PERIOD_TYPE_ID
6075    , CALENDAR_TYPE
6076    , WBS_ROLLUP_FLAG
6077    , PRG_ROLLUP_FLAG
6078    , CURR_RECORD_TYPE_ID
6079    , CURRENCY_CODE
6080    , REVENUE
6081    , INITIAL_FUNDING_AMOUNT
6082    , INITIAL_FUNDING_COUNT
6083    , ADDITIONAL_FUNDING_AMOUNT
6084    , ADDITIONAL_FUNDING_COUNT
6085    , CANCELLED_FUNDING_AMOUNT
6086    , CANCELLED_FUNDING_COUNT
6087    , FUNDING_ADJUSTMENT_AMOUNT
6088    , FUNDING_ADJUSTMENT_COUNT
6089    , REVENUE_WRITEOFF
6090    , AR_INVOICE_AMOUNT
6091    , AR_INVOICE_COUNT
6092    , AR_CASH_APPLIED_AMOUNT
6093    , AR_INVOICE_WRITE_OFF_AMOUNT
6094    , AR_INVOICE_WRITEOFF_COUNT
6095    , AR_CREDIT_MEMO_AMOUNT
6096    , AR_CREDIT_MEMO_COUNT
6097    , UNBILLED_RECEIVABLES
6098    , UNEARNED_REVENUE
6099    , AR_UNAPPR_INVOICE_AMOUNT
6100    , AR_UNAPPR_INVOICE_COUNT
6101    , AR_APPR_INVOICE_AMOUNT
6102    , AR_APPR_INVOICE_COUNT
6103    , AR_AMOUNT_DUE
6104    , AR_COUNT_DUE
6105    , AR_AMOUNT_OVERDUE
6106    , AR_COUNT_OVERDUE
6107    , DORMANT_BACKLOG_INACTIV
6108    , DORMANT_BACKLOG_START
6109    , LOST_BACKLOG
6110    , ACTIVE_BACKLOG
6111    , REVENUE_AT_RISK
6112    , l_last_update_date
6113    , l_last_updated_by
6114    , l_creation_date
6115    , l_created_by
6116    , l_last_update_login
6117    , CUSTOM1
6118    , CUSTOM2
6119    , CUSTOM3
6120    , CUSTOM4
6121    , CUSTOM5
6122    , CUSTOM6
6123    , CUSTOM7
6124    , CUSTOM8
6125    , CUSTOM9
6126    , CUSTOM10
6127    , CUSTOM11
6128    , CUSTOM12
6129    , CUSTOM13
6130    , CUSTOM14
6131    , CUSTOM15
6132   FROM PJI_AC_AGGR_PJP1
6133   WHERE worker_id = g_worker_id
6134   ORDER BY
6135      PROJECT_ID
6136    , PROJECT_ELEMENT_ID
6137    , TIME_ID
6138    , CURRENCY_CODE;
6139 
6140 EXCEPTION
6141   WHEN OTHERS THEN
6142     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6143                              p_procedure_name => 'INSERT_INTO_AC_FACT');
6144     RAISE;
6145 END;
6146 
6147 
6148 PROCEDURE MERGE_INTO_AC_FACT IS
6149 BEGIN
6150 
6151   GET_AC_ROW_IDS;
6152 
6153   UPDATE_AC_ROWS;
6154 
6155   INSERT_AC_ROWS;
6156 
6157   CLEANUP_AC_RMAP_FPR;
6158 
6159 EXCEPTION
6160   WHEN OTHERS THEN
6161     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6162                              p_procedure_name => 'MERGE_INTO_AC_FACT');
6163     RAISE;
6164 END;
6165 
6166 
6167 PROCEDURE CLEANUP_AC_RMAP_FPR IS
6168 BEGIN
6169 
6170   DELETE FROM PJI_AC_RMAP_ACR
6171   WHERE worker_id = g_worker_id;
6172 
6173 EXCEPTION
6174   WHEN OTHERS THEN
6175     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6176                              p_procedure_name => 'CLEANUP_AC_RMAP_FPR');
6177     RAISE;
6178 END;
6179 
6180 
6181 PROCEDURE GET_AC_ROW_IDS IS
6182 BEGIN
6183 
6184   INSERT INTO PJI_AC_RMAP_ACR /*Not decided to keep this hint yet. + APPEND PARALLEL */
6185   (
6186      worker_id
6187    , rl_rowid
6188    , tmp_rowid
6189   )
6190   SELECT
6191     g_worker_id WORKER_ID
6192   , rl.ROWID pjp1_rowid
6193   , tmp.ROWID TMP1_ROWID
6194   FROM
6195     pji_ac_aggr_pjp1            tmp
6196   , PJI_AC_XBS_ACCUM_F          rl
6197   WHERE 1 = 1
6198    AND tmp.worker_id = g_worker_id
6199    AND tmp.PROJECT_ID = rl.PROJECT_ID (+)
6200    AND tmp.PROJECT_ORG_ID = rl.PROJECT_ORG_ID (+)
6201    AND tmp.PROJECT_ORGANIZATION_ID = rl.PROJECT_ORGANIZATION_ID (+)
6202    AND tmp.PROJECT_ELEMENT_ID = rl.PROJECT_ELEMENT_ID (+)
6203    AND tmp.TIME_ID = rl.TIME_ID (+)
6204    AND tmp.PERIOD_TYPE_ID = rl.PERIOD_TYPE_ID (+)
6205    AND tmp.CALENDAR_TYPE = rl.CALENDAR_TYPE (+)
6206    AND tmp.WBS_ROLLUP_FLAG = rl.WBS_ROLLUP_FLAG (+)
6207    AND tmp.PRG_ROLLUP_FLAG = rl.PRG_ROLLUP_FLAG (+)
6208    AND tmp.CURR_RECORD_TYPE_ID = rl.CURR_RECORD_TYPE_ID (+)
6209    AND tmp.CURRENCY_CODE = rl.CURRENCY_CODE (+) ;
6210 
6211 EXCEPTION
6212   WHEN OTHERS THEN
6213     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6214                              p_procedure_name => 'GET_AC_ROW_IDS');
6215     RAISE;
6216 END;
6217 
6218 
6219 PROCEDURE UPDATE_AC_ROWS IS
6220 
6221   l_last_update_date  DATE   := SYSDATE;
6222   l_last_updated_by   NUMBER := FND_GLOBAL.USER_ID;
6223   l_last_update_login NUMBER := FND_GLOBAL.LOGIN_ID;
6224 
6225 BEGIN
6226 
6227     UPDATE /*+ ordered use_nl(rl) rowid(rl) */
6228       PJI_AC_XBS_ACCUM_F rl
6229     SET (
6230       rl.REVENUE
6231     , rl.INITIAL_FUNDING_AMOUNT
6232     , rl.INITIAL_FUNDING_COUNT
6233     , rl.ADDITIONAL_FUNDING_AMOUNT
6234     , rl.ADDITIONAL_FUNDING_COUNT
6235     , rl.CANCELLED_FUNDING_AMOUNT
6236     , rl.CANCELLED_FUNDING_COUNT
6237     , rl.FUNDING_ADJUSTMENT_AMOUNT
6238     , rl.FUNDING_ADJUSTMENT_COUNT
6239     , rl.REVENUE_WRITEOFF
6240     , rl.AR_INVOICE_AMOUNT
6241     , rl.AR_INVOICE_COUNT
6242     , rl.AR_CASH_APPLIED_AMOUNT
6243     , rl.AR_INVOICE_WRITE_OFF_AMOUNT
6244     , rl.AR_INVOICE_WRITEOFF_COUNT
6245     , rl.AR_CREDIT_MEMO_AMOUNT
6246     , rl.AR_CREDIT_MEMO_COUNT
6247     , rl.UNBILLED_RECEIVABLES
6248     , rl.UNEARNED_REVENUE
6249     , rl.AR_UNAPPR_INVOICE_AMOUNT
6250     , rl.AR_UNAPPR_INVOICE_COUNT
6251     , rl.AR_APPR_INVOICE_AMOUNT
6252     , rl.AR_APPR_INVOICE_COUNT
6253     , rl.AR_AMOUNT_DUE
6254     , rl.AR_COUNT_DUE
6255     , rl.AR_AMOUNT_OVERDUE
6256     , rl.AR_COUNT_OVERDUE
6257     , rl.DORMANT_BACKLOG_INACTIV
6258     , rl.DORMANT_BACKLOG_START
6259     , rl.LOST_BACKLOG
6260     , rl.ACTIVE_BACKLOG
6261     , rl.REVENUE_AT_RISK
6262     , rl.CUSTOM1
6263     , rl.CUSTOM2
6264     , rl.CUSTOM3
6265     , rl.CUSTOM4
6266     , rl.CUSTOM5
6267     , rl.CUSTOM6
6268     , rl.CUSTOM7
6269     , rl.CUSTOM8
6270     , rl.CUSTOM9
6271     , rl.CUSTOM10
6272     , rl.CUSTOM11
6273     , rl.CUSTOM12
6274     , rl.CUSTOM13
6275     , rl.CUSTOM14
6276     , rl.CUSTOM15
6277     , rl.LAST_UPDATE_DATE
6278     , rl.LAST_UPDATED_BY
6279     , rl.LAST_UPDATE_LOGIN
6280 	) =
6281     (
6282       SELECT  /*+  ORDERED ROWID(TMP) index(rwid,PJI_AC_RMAP_ACR_N1) */
6283          NVL(rl.REVENUE, 0) + NVL(tmp.REVENUE, 0)
6284        , NVL(rl.INITIAL_FUNDING_AMOUNT, 0) + NVL(tmp.INITIAL_FUNDING_AMOUNT, 0)
6285        , NVL(rl.INITIAL_FUNDING_COUNT, 0) + NVL(tmp.INITIAL_FUNDING_COUNT, 0)
6286        , NVL(rl.ADDITIONAL_FUNDING_AMOUNT, 0) + NVL(tmp.ADDITIONAL_FUNDING_AMOUNT, 0)
6287        , NVL(rl.ADDITIONAL_FUNDING_COUNT, 0) + NVL(tmp.ADDITIONAL_FUNDING_COUNT, 0)
6288        , NVL(rl.CANCELLED_FUNDING_AMOUNT, 0) + NVL(tmp.CANCELLED_FUNDING_AMOUNT, 0)
6289        , NVL(rl.CANCELLED_FUNDING_COUNT, 0) + NVL(tmp.CANCELLED_FUNDING_COUNT, 0)
6290        , NVL(rl.FUNDING_ADJUSTMENT_AMOUNT, 0) + NVL(tmp.FUNDING_ADJUSTMENT_AMOUNT, 0)
6291        , NVL(rl.FUNDING_ADJUSTMENT_COUNT, 0) + NVL(tmp.FUNDING_ADJUSTMENT_COUNT, 0)
6292        , NVL(rl.REVENUE_WRITEOFF, 0) + NVL(tmp.REVENUE_WRITEOFF, 0)
6293        , NVL(rl.AR_INVOICE_AMOUNT, 0) + NVL(tmp.AR_INVOICE_AMOUNT, 0)
6294        , NVL(rl.AR_INVOICE_COUNT, 0) + NVL(tmp.AR_INVOICE_COUNT, 0)
6295        , NVL(rl.AR_CASH_APPLIED_AMOUNT, 0) + NVL(tmp.AR_CASH_APPLIED_AMOUNT, 0)
6296        , NVL(rl.AR_INVOICE_WRITE_OFF_AMOUNT, 0) + NVL(tmp.AR_INVOICE_WRITE_OFF_AMOUNT, 0)
6297        , NVL(rl.AR_INVOICE_WRITEOFF_COUNT, 0) + NVL(tmp.AR_INVOICE_WRITEOFF_COUNT, 0)
6298        , NVL(rl.AR_CREDIT_MEMO_AMOUNT, 0) + NVL(tmp.AR_CREDIT_MEMO_AMOUNT, 0)
6299        , NVL(rl.AR_CREDIT_MEMO_COUNT, 0) + NVL(tmp.AR_CREDIT_MEMO_COUNT, 0)
6300        , NVL(rl.UNBILLED_RECEIVABLES, 0) + NVL(tmp.UNBILLED_RECEIVABLES, 0)
6301        , NVL(rl.UNEARNED_REVENUE, 0) + NVL(tmp.UNEARNED_REVENUE, 0)
6302        , NVL(rl.AR_UNAPPR_INVOICE_AMOUNT, 0) + NVL(tmp.AR_UNAPPR_INVOICE_AMOUNT, 0)
6303        , NVL(rl.AR_UNAPPR_INVOICE_COUNT, 0) + NVL(tmp.AR_UNAPPR_INVOICE_COUNT, 0)
6304        , NVL(rl.AR_APPR_INVOICE_AMOUNT, 0) + NVL(tmp.AR_APPR_INVOICE_AMOUNT, 0)
6305        , NVL(rl.AR_APPR_INVOICE_COUNT, 0) + NVL(tmp.AR_APPR_INVOICE_COUNT, 0)
6306        , NVL(rl.AR_AMOUNT_DUE, 0) + NVL(tmp.AR_AMOUNT_DUE, 0)
6307        , NVL(rl.AR_COUNT_DUE, 0) + NVL(tmp.AR_COUNT_DUE, 0)
6308        , NVL(rl.AR_AMOUNT_OVERDUE, 0) + NVL(tmp.AR_AMOUNT_OVERDUE, 0)
6309        , NVL(rl.AR_COUNT_OVERDUE, 0) + NVL(tmp.AR_COUNT_OVERDUE, 0)
6310        , NVL(rl.DORMANT_BACKLOG_INACTIV, 0) + NVL(tmp.DORMANT_BACKLOG_INACTIV, 0)
6311        , NVL(rl.DORMANT_BACKLOG_START, 0) + NVL(tmp.DORMANT_BACKLOG_START, 0)
6312        , NVL(rl.LOST_BACKLOG, 0) + NVL(tmp.LOST_BACKLOG, 0)
6313        , NVL(rl.ACTIVE_BACKLOG, 0) + NVL(tmp.ACTIVE_BACKLOG, 0)
6314        , NVL(rl.REVENUE_AT_RISK, 0) + NVL(tmp.REVENUE_AT_RISK, 0)
6315        , NVL(rl.CUSTOM1, 0) + NVL(tmp.CUSTOM1, 0)
6316        , NVL(rl.CUSTOM2, 0) + NVL(tmp.CUSTOM2, 0)
6317        , NVL(rl.CUSTOM3, 0) + NVL(tmp.CUSTOM3, 0)
6318        , NVL(rl.CUSTOM4, 0) + NVL(tmp.CUSTOM4, 0)
6319        , NVL(rl.CUSTOM5, 0) + NVL(tmp.CUSTOM5, 0)
6320        , NVL(rl.CUSTOM6, 0) + NVL(tmp.CUSTOM6, 0)
6321        , NVL(rl.CUSTOM7, 0) + NVL(tmp.CUSTOM7, 0)
6322        , NVL(rl.CUSTOM8, 0) + NVL(tmp.CUSTOM8, 0)
6323        , NVL(rl.CUSTOM9, 0) + NVL(tmp.CUSTOM9, 0)
6324        , NVL(rl.CUSTOM10, 0) + NVL(tmp.CUSTOM10, 0)
6325        , NVL(rl.CUSTOM11, 0) + NVL(tmp.CUSTOM11, 0)
6326        , NVL(rl.CUSTOM12, 0) + NVL(tmp.CUSTOM12, 0)
6327        , NVL(rl.CUSTOM13, 0) + NVL(tmp.CUSTOM13, 0)
6328        , NVL(rl.CUSTOM14, 0) + NVL(tmp.CUSTOM14, 0)
6329        , NVL(rl.CUSTOM15, 0) + NVL(tmp.CUSTOM15, 0)
6330        , l_last_update_date
6331        , l_last_updated_by
6332        , l_last_update_login
6333      FROM
6334        PJI_AC_RMAP_ACR rwid
6335      , pji_ac_aggr_pjp1 tmp
6336      WHERE  1 = 1
6337         AND tmp.worker_id = g_worker_id
6338         AND rwid.worker_id = g_worker_id
6339   	  AND tmp.rowid = rwid.tmp_rowid
6340 	  AND rl.rowid = rwid.rl_rowid
6341 	  AND rwid.rl_rowid IS NOT NULL
6342 	)
6343      WHERE rl.rowid IN
6344              ( SELECT rl_rowid
6345 		   FROM PJI_AC_RMAP_ACR rwid
6346 		     WHERE 1=1
6347                    AND rwid.rl_rowid IS NOT NULL
6348                    AND rwid.worker_id = g_worker_id);
6349 
6350 EXCEPTION
6351   WHEN OTHERS THEN
6352     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6353                              p_procedure_name => 'UPDATE_AC_ROWS');
6354     RAISE;
6355 END;
6356 
6357 
6358 PROCEDURE INSERT_AC_ROWS IS
6359     l_last_update_date     date   := SYSDATE;
6360     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
6361     l_creation_date        date   := SYSDATE;
6362     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
6363     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
6364 BEGIN
6365 
6366   INSERT INTO PJI_AC_XBS_ACCUM_F
6367   (
6368      PROJECT_ID
6369    , PROJECT_ORG_ID
6370    , PROJECT_ORGANIZATION_ID
6371    , PROJECT_ELEMENT_ID
6372    , TIME_ID
6373    , PERIOD_TYPE_ID
6374    , CALENDAR_TYPE
6375    , WBS_ROLLUP_FLAG
6376    , PRG_ROLLUP_FLAG
6377    , CURR_RECORD_TYPE_ID
6378    , CURRENCY_CODE
6379    , REVENUE
6380    , INITIAL_FUNDING_AMOUNT
6381    , INITIAL_FUNDING_COUNT
6382    , ADDITIONAL_FUNDING_AMOUNT
6383    , ADDITIONAL_FUNDING_COUNT
6384    , CANCELLED_FUNDING_AMOUNT
6385    , CANCELLED_FUNDING_COUNT
6386    , FUNDING_ADJUSTMENT_AMOUNT
6387    , FUNDING_ADJUSTMENT_COUNT
6388    , REVENUE_WRITEOFF
6389    , AR_INVOICE_AMOUNT
6390    , AR_INVOICE_COUNT
6391    , AR_CASH_APPLIED_AMOUNT
6392    , AR_INVOICE_WRITE_OFF_AMOUNT
6393    , AR_INVOICE_WRITEOFF_COUNT
6394    , AR_CREDIT_MEMO_AMOUNT
6395    , AR_CREDIT_MEMO_COUNT
6396    , UNBILLED_RECEIVABLES
6397    , UNEARNED_REVENUE
6398    , AR_UNAPPR_INVOICE_AMOUNT
6399    , AR_UNAPPR_INVOICE_COUNT
6400    , AR_APPR_INVOICE_AMOUNT
6401    , AR_APPR_INVOICE_COUNT
6402    , AR_AMOUNT_DUE
6403    , AR_COUNT_DUE
6404    , AR_AMOUNT_OVERDUE
6405    , AR_COUNT_OVERDUE
6406    , DORMANT_BACKLOG_INACTIV
6407    , DORMANT_BACKLOG_START
6408    , LOST_BACKLOG
6409    , ACTIVE_BACKLOG
6410    , REVENUE_AT_RISK
6411    , LAST_UPDATE_DATE
6412    , LAST_UPDATED_BY
6413    , CREATION_DATE
6414    , CREATED_BY
6415    , LAST_UPDATE_LOGIN
6416    , CUSTOM1
6417    , CUSTOM2
6418    , CUSTOM3
6419    , CUSTOM4
6420    , CUSTOM5
6421    , CUSTOM6
6422    , CUSTOM7
6423    , CUSTOM8
6424    , CUSTOM9
6425    , CUSTOM10
6426    , CUSTOM11
6427    , CUSTOM12
6428    , CUSTOM13
6429    , CUSTOM14
6430    , CUSTOM15
6431   )
6432   SELECT
6433      PROJECT_ID
6434    , PROJECT_ORG_ID
6435    , PROJECT_ORGANIZATION_ID
6436    , PROJECT_ELEMENT_ID
6437    , TIME_ID
6438    , PERIOD_TYPE_ID
6439    , CALENDAR_TYPE
6440    , WBS_ROLLUP_FLAG
6441    , PRG_ROLLUP_FLAG
6442    , CURR_RECORD_TYPE_ID
6443    , CURRENCY_CODE
6444    , REVENUE
6445    , INITIAL_FUNDING_AMOUNT
6446    , INITIAL_FUNDING_COUNT
6447    , ADDITIONAL_FUNDING_AMOUNT
6448    , ADDITIONAL_FUNDING_COUNT
6449    , CANCELLED_FUNDING_AMOUNT
6450    , CANCELLED_FUNDING_COUNT
6451    , FUNDING_ADJUSTMENT_AMOUNT
6452    , FUNDING_ADJUSTMENT_COUNT
6453    , REVENUE_WRITEOFF
6454    , AR_INVOICE_AMOUNT
6455    , AR_INVOICE_COUNT
6456    , AR_CASH_APPLIED_AMOUNT
6457    , AR_INVOICE_WRITE_OFF_AMOUNT
6458    , AR_INVOICE_WRITEOFF_COUNT
6459    , AR_CREDIT_MEMO_AMOUNT
6460    , AR_CREDIT_MEMO_COUNT
6461    , UNBILLED_RECEIVABLES
6462    , UNEARNED_REVENUE
6463    , AR_UNAPPR_INVOICE_AMOUNT
6464    , AR_UNAPPR_INVOICE_COUNT
6465    , AR_APPR_INVOICE_AMOUNT
6466    , AR_APPR_INVOICE_COUNT
6467    , AR_AMOUNT_DUE
6468    , AR_COUNT_DUE
6469    , AR_AMOUNT_OVERDUE
6470    , AR_COUNT_OVERDUE
6471    , DORMANT_BACKLOG_INACTIV
6472    , DORMANT_BACKLOG_START
6473    , LOST_BACKLOG
6474    , ACTIVE_BACKLOG
6475    , REVENUE_AT_RISK
6476    , l_last_update_date
6477    , l_last_updated_by
6478    , l_creation_date
6479    , l_created_by
6480    , l_last_update_login
6481    , CUSTOM1
6482    , CUSTOM2
6483    , CUSTOM3
6484    , CUSTOM4
6485    , CUSTOM5
6486    , CUSTOM6
6487    , CUSTOM7
6488    , CUSTOM8
6489    , CUSTOM9
6490    , CUSTOM10
6491    , CUSTOM11
6492    , CUSTOM12
6493    , CUSTOM13
6494    , CUSTOM14
6495    , CUSTOM15
6496   FROM pji_ac_aggr_pjp1 tmp
6497      , PJI_AC_RMAP_ACR rwid
6498   WHERE 1 = 1
6499    AND tmp.worker_id = g_worker_id
6500    AND rwid.worker_id = g_worker_id
6501    AND tmp.rowid = rwid.tmp_rowid
6502    AND rwid.rl_rowid IS NULL
6503   ORDER BY
6504      PROJECT_ID
6505    , PROJECT_ELEMENT_ID
6506    , TIME_ID
6507    , CURRENCY_CODE;
6508 
6509 EXCEPTION
6510   WHEN OTHERS THEN
6511     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6512                              p_procedure_name => 'INSERT_AC_ROWS');
6513     RAISE;
6514 END;
6515 
6516 PROCEDURE MARK_DANGLING_PLAN_VERSIONS IS
6517 BEGIN
6518 
6519   UPDATE pa_budget_versions bv
6520   SET    pji_summarized_flag = 'P',
6521          record_version_number = nvl(record_version_number,0)+1
6522   WHERE  budget_version_id IN (
6523 	      SELECT plan_version_id
6524 		FROM
6525    	      (
6526 		  SELECT plan_version_id
6527                    , (   COUNT(DISTINCT time_dangling_flag)
6528                        + COUNT(DISTINCT rate_dangling_flag)
6529                      ) dangling
6530               FROM   pji_fp_aggr_pjp1
6531               WHERE worker_id = g_worker_id
6532       	  GROUP BY plan_version_id
6533 		) b
6534 		WHERE dangling > 2
6535    	   );
6536 
6537   UPDATE pa_budget_versions bv
6538   SET    pji_summarized_flag = 'Y',
6539          record_version_number = nvl(record_version_number,0)+1
6540   WHERE  budget_version_id IN (
6541 	      SELECT plan_version_id
6542 		FROM
6543    	      (
6544 		  SELECT plan_version_id
6545                    , (   COUNT(DISTINCT time_dangling_flag)
6546                        + COUNT(DISTINCT rate_dangling_flag)
6547                      ) dangling
6548               FROM   pji_fp_aggr_pjp1
6549               WHERE worker_id = g_worker_id
6550       	  GROUP BY plan_version_id
6551 		) b
6552 		WHERE dangling = 2
6553    	   );
6554 
6555 EXCEPTION
6556   WHEN OTHERS THEN
6557     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6558                              p_procedure_name => 'MARK_DANGLING_PLAN_VERSIONS');
6559     RAISE;
6560 END;
6561 
6562 
6563 --
6564 -- 0. Plans cannot have time / rate dangling records for primary slice.
6565 -- 1. Work plans have primary slice only.
6566 -- 2. Financial plans can have secondary slices only if the plan is baselined.
6567 -- 3. Only baselined financial plans can have time/rate dangling records.
6568 -- 4. Four states of pji_summarized_flag are N, Y, P, NULL.
6569 --      N - Not summarized..
6570 --      P - Have rate/time dangling records..
6571 --      Y - Project performance summaries fully created.
6572 --      NULL - PJI summaries fully created.
6573 --
6574 -- This api processes plans with pji_summarized_flag = N, i.e., secondary slice
6575 --  for dangling baselined financial plans.
6576 --
6577 PROCEDURE PULL_DANGLING_PLANS IS
6578 
6579   l_fp_wp_version_ids   SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
6580   l_fp_version_ids      SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
6581 
6582 BEGIN
6583 
6584   NULL; -- Pull dangling calls bulk "create secondary private" directly.
6585 
6586 EXCEPTION
6587   WHEN OTHERS THEN
6588     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6589                              p_procedure_name => 'PULL_DANGLING_PLANS');
6590     RAISE;
6591 END;
6592 
6593 
6594 PROCEDURE RETRIEVE_ENTERED_SLICE (
6595   p_pln_ver_id IN NUMBER := NULL ) IS
6596 BEGIN
6597 
6598     INSERT INTO pji_fp_aggr_pjp1
6599     (
6600        WORKER_ID
6601      , PROJECT_ID
6602      , PROJECT_ORG_ID
6603      , PROJECT_ORGANIZATION_ID
6604      -- , PARTITION_ID
6605      , PROJECT_ELEMENT_ID
6606      , TIME_ID
6607      , PERIOD_TYPE_ID
6608      , CALENDAR_TYPE
6609      , RBS_AGGR_LEVEL
6610      , WBS_ROLLUP_FLAG
6611      , PRG_ROLLUP_FLAG
6612      , CURR_RECORD_TYPE_ID
6613      , CURRENCY_CODE
6614      , RBS_ELEMENT_ID
6615      , RBS_VERSION_ID
6616      , PLAN_VERSION_ID
6617      , PLAN_TYPE_ID
6618      , RAW_COST
6619      , BRDN_COST
6620      , REVENUE
6621      , BILL_RAW_COST
6622      , BILL_BRDN_COST
6623      , BILL_LABOR_RAW_COST
6624      , BILL_LABOR_BRDN_COST
6625      , BILL_LABOR_HRS
6626      , EQUIPMENT_RAW_COST
6627      , EQUIPMENT_BRDN_COST
6628      , CAPITALIZABLE_RAW_COST
6629      , CAPITALIZABLE_BRDN_COST
6630      , LABOR_RAW_COST
6631      , LABOR_BRDN_COST
6632      , LABOR_HRS
6633      , LABOR_REVENUE
6634      , EQUIPMENT_HOURS
6635      , BILLABLE_EQUIPMENT_HOURS
6636      , SUP_INV_COMMITTED_COST
6637      , PO_COMMITTED_COST
6638      , PR_COMMITTED_COST
6639      , OTH_COMMITTED_COST
6640        , ACT_LABOR_HRS
6641 	   , ACT_EQUIP_HRS
6642 	   , ACT_LABOR_BRDN_COST
6643 	   , ACT_EQUIP_BRDN_COST
6644 	   , ACT_BRDN_COST
6645 	   , ACT_RAW_COST
6646 	   , ACT_REVENUE
6647          , ACT_LABOR_RAW_COST
6648          , ACT_EQUIP_RAW_COST
6649 	   , ETC_LABOR_HRS
6650 	   , ETC_EQUIP_HRS
6651 	   , ETC_LABOR_BRDN_COST
6652 	   , ETC_EQUIP_BRDN_COST
6653 	   , ETC_BRDN_COST
6654          , ETC_RAW_COST
6655          , ETC_LABOR_RAW_COST
6656          , ETC_EQUIP_RAW_COST
6657      , CUSTOM1
6658      , CUSTOM2
6659      , CUSTOM3
6660      , CUSTOM4
6661      , CUSTOM5
6662      , CUSTOM6
6663      , CUSTOM7
6664      , CUSTOM8
6665      , CUSTOM9
6666      , CUSTOM10
6667      , CUSTOM11
6668      , CUSTOM12
6669      , CUSTOM13
6670      , CUSTOM14
6671      , CUSTOM15
6672      , PRG_LEVEL
6673      ,PLAN_TYPE_CODE   /*4471527 */
6674     )
6675     (
6676      SELECT
6677        g_worker_id
6678      , PROJECT_ID
6679      , PROJECT_ORG_ID
6680      , PROJECT_ORGANIZATION_ID
6681      -- , PARTITION_ID
6682      , PROJECT_ELEMENT_ID
6683      , TIME_ID
6684      , PERIOD_TYPE_ID
6685      , CALENDAR_TYPE
6686      , RBS_AGGR_LEVEL
6687      , WBS_ROLLUP_FLAG
6688      , PRG_ROLLUP_FLAG
6689      , CURR_RECORD_TYPE_ID
6690      , CURRENCY_CODE
6691      , RBS_ELEMENT_ID
6692      , RBS_VERSION_ID
6693      , PLAN_VERSION_ID
6694      , PLAN_TYPE_ID
6695      , RAW_COST
6696      , BRDN_COST
6697      , REVENUE
6698      , BILL_RAW_COST
6699      , BILL_BRDN_COST
6700      , BILL_LABOR_RAW_COST
6701      , BILL_LABOR_BRDN_COST
6702      , BILL_LABOR_HRS
6703      , EQUIPMENT_RAW_COST
6704      , EQUIPMENT_BRDN_COST
6705      , CAPITALIZABLE_RAW_COST
6706      , CAPITALIZABLE_BRDN_COST
6707      , LABOR_RAW_COST
6708      , LABOR_BRDN_COST
6709      , LABOR_HRS
6710      , LABOR_REVENUE
6711      , EQUIPMENT_HOURS
6712      , BILLABLE_EQUIPMENT_HOURS
6713      , SUP_INV_COMMITTED_COST
6714      , PO_COMMITTED_COST
6715      , PR_COMMITTED_COST
6716      , OTH_COMMITTED_COST
6717        , ACT_LABOR_HRS
6718 	   , ACT_EQUIP_HRS
6719 	   , ACT_LABOR_BRDN_COST
6720 	   , ACT_EQUIP_BRDN_COST
6721 	   , ACT_BRDN_COST
6722 	   , ACT_RAW_COST
6723 	   , ACT_REVENUE
6724          , ACT_LABOR_RAW_COST
6725          , ACT_EQUIP_RAW_COST
6726 	   , ETC_LABOR_HRS
6727 	   , ETC_EQUIP_HRS
6728 	   , ETC_LABOR_BRDN_COST
6729 	   , ETC_EQUIP_BRDN_COST
6730 	   , ETC_BRDN_COST
6731          , ETC_RAW_COST
6732          , ETC_LABOR_RAW_COST
6733          , ETC_EQUIP_RAW_COST
6734      , CUSTOM1
6735      , CUSTOM2
6736      , CUSTOM3
6737      , CUSTOM4
6738      , CUSTOM5
6739      , CUSTOM6
6740      , CUSTOM7
6741      , CUSTOM8
6742      , CUSTOM9
6743      , CUSTOM10
6744      , CUSTOM11
6745      , CUSTOM12
6746      , CUSTOM13
6747      , CUSTOM14
6748      , CUSTOM15
6749      , g_default_prg_level
6750      ,PLAN_TYPE_CODE
6751      FROM pji_fp_xbs_accum_f
6752      WHERE plan_version_id = p_pln_ver_id
6753     );
6754 
6755 EXCEPTION
6756   WHEN OTHERS THEN
6757     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6758                              p_procedure_name => 'RETRIEVE_ENTERED_SLICE');
6759     RAISE;
6760 END;
6761 
6762 
6763 
6764 
6765 PROCEDURE ROLLUP_FPR_RBS IS
6766     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
6767     -- l_worker_id            NUMBER := 1;
6768 
6769 BEGIN
6770 
6771      INSERT INTO  pji_fp_aggr_pjp1
6772      (
6773        WORKER_ID
6774      , PROJECT_ID
6775      , PROJECT_ORG_ID
6776      , PROJECT_ORGANIZATION_ID
6777      , PROJECT_ELEMENT_ID
6778      , TIME_ID
6779      , PERIOD_TYPE_ID
6780      , CALENDAR_TYPE
6781      , RBS_AGGR_LEVEL
6782      , WBS_ROLLUP_FLAG
6783      , PRG_ROLLUP_FLAG
6784      , CURR_RECORD_TYPE_ID
6785      , CURRENCY_CODE
6786      , RBS_ELEMENT_ID
6787      , RBS_VERSION_ID
6788      , PLAN_VERSION_ID
6789      , PLAN_TYPE_ID
6790      , RAW_COST
6791      , BRDN_COST
6792      , REVENUE
6793      , BILL_RAW_COST
6794      , BILL_BRDN_COST
6795      , BILL_LABOR_RAW_COST
6796      , BILL_LABOR_BRDN_COST
6797      , BILL_LABOR_HRS
6798      , EQUIPMENT_RAW_COST
6799      , EQUIPMENT_BRDN_COST
6800      , CAPITALIZABLE_RAW_COST
6801      , CAPITALIZABLE_BRDN_COST
6802      , LABOR_RAW_COST
6803      , LABOR_BRDN_COST
6804      , LABOR_HRS
6805      , LABOR_REVENUE
6806      , EQUIPMENT_HOURS
6807      , BILLABLE_EQUIPMENT_HOURS
6808      , SUP_INV_COMMITTED_COST
6809      , PO_COMMITTED_COST
6810      , PR_COMMITTED_COST
6811      , OTH_COMMITTED_COST
6812        , ACT_LABOR_HRS
6813 	   , ACT_EQUIP_HRS
6814 	   , ACT_LABOR_BRDN_COST
6815 	   , ACT_EQUIP_BRDN_COST
6816 	   , ACT_BRDN_COST
6817 	   , ACT_RAW_COST
6818 	   , ACT_REVENUE
6819          , ACT_LABOR_RAW_COST
6820          , ACT_EQUIP_RAW_COST
6821 	   , ETC_LABOR_HRS
6822 	   , ETC_EQUIP_HRS
6823 	   , ETC_LABOR_BRDN_COST
6824 	   , ETC_EQUIP_BRDN_COST
6825 	   , ETC_BRDN_COST
6826          , ETC_RAW_COST
6827          , ETC_LABOR_RAW_COST
6828          , ETC_EQUIP_RAW_COST
6829      , CUSTOM1
6830      , CUSTOM2
6831      , CUSTOM3
6832      , CUSTOM4
6833      , CUSTOM5
6834      , CUSTOM6
6835      , CUSTOM7
6836      , CUSTOM8
6837      , CUSTOM9
6838      , CUSTOM10
6839      , CUSTOM11
6840      , CUSTOM12
6841      , CUSTOM13
6842      , CUSTOM14
6843      , CUSTOM15
6844      , LINE_TYPE
6845      , PRG_LEVEL
6846      , PLAN_TYPE_CODE   /*4471527 */
6847     )
6848      SELECT
6849         g_worker_id WORKER_ID
6850       , fact1.PROJECT_ID
6851 	, fact1.PROJECT_ORG_ID
6852 	, fact1.PROJECT_ORGANIZATION_ID
6853       , fact1.project_element_id
6854 	, fact1.TIME_ID
6855       , fact1.PERIOD_TYPE_ID
6856       , fact1.CALENDAR_TYPE
6857 	, 'R'
6858 	, fact1.WBS_ROLLUP_FLAG
6859 	, fact1.PRG_ROLLUP_FLAG
6860       , fact1.CURR_RECORD_TYPE_ID
6861       , fact1.CURRENCY_CODE
6862       , rbs.sup_id
6863       , fact1.RBS_VERSION_ID
6864       , fact1.PLAN_VERSION_ID
6865 	, fact1.PLAN_TYPE_ID
6866       , SUM(fact1.RAW_COST)
6867       , SUM(fact1.BRDN_COST)
6868       , SUM(fact1.REVENUE)
6869       , SUM(fact1.BILL_RAW_COST)
6870       , SUM(fact1.BILL_BRDN_COST )
6871       , SUM(fact1.BILL_LABOR_RAW_COST)
6872       , SUM(fact1.BILL_LABOR_BRDN_COST )
6873       , SUM(fact1.BILL_LABOR_HRS )
6874       , SUM(fact1.EQUIPMENT_RAW_COST )
6875       , SUM(fact1.EQUIPMENT_BRDN_COST )
6876       , SUM(fact1.CAPITALIZABLE_RAW_COST )
6877       , SUM(fact1.CAPITALIZABLE_BRDN_COST )
6878       , SUM(fact1.LABOR_RAW_COST )
6879       , SUM(fact1.LABOR_BRDN_COST )
6880       , SUM(fact1.LABOR_HRS)
6881       , SUM(fact1.LABOR_REVENUE)
6882       , SUM(fact1.EQUIPMENT_HOURS)
6883       , SUM(fact1.BILLABLE_EQUIPMENT_HOURS)
6884       , SUM(fact1.SUP_INV_COMMITTED_COST)
6885       , SUM(fact1.PO_COMMITTED_COST   )
6886       , SUM(fact1.PR_COMMITTED_COST  )
6887       , SUM(fact1.OTH_COMMITTED_COST)
6888        , SUM(fact1.ACT_LABOR_HRS)
6889 	 , SUM(fact1.ACT_EQUIP_HRS)
6890 	 , SUM(fact1.ACT_LABOR_BRDN_COST)
6891 	 , SUM(fact1.ACT_EQUIP_BRDN_COST)
6892 	 , SUM(fact1.ACT_BRDN_COST)
6893 	 , SUM(fact1.ACT_RAW_COST)
6894 	 , SUM(fact1.ACT_REVENUE)
6895        , SUM(fact1.ACT_LABOR_RAW_COST)
6896        , SUM(fact1.ACT_EQUIP_RAW_COST)
6897 	 , SUM(fact1.ETC_LABOR_HRS)
6898 	 , SUM(fact1.ETC_EQUIP_HRS)
6899 	 , SUM(fact1.ETC_LABOR_BRDN_COST)
6900 	 , SUM(fact1.ETC_EQUIP_BRDN_COST)
6901 	 , SUM(fact1.ETC_BRDN_COST )
6902        , SUM(fact1.ETC_RAW_COST )
6903        , SUM(fact1.ETC_LABOR_RAW_COST)
6904        , SUM(fact1.ETC_EQUIP_RAW_COST)
6905       , SUM(fact1.CUSTOM1 )
6906       , SUM(fact1.CUSTOM2 )
6907       , SUM(fact1.CUSTOM3 )
6908       , SUM(fact1.CUSTOM4 )
6909       , SUM(fact1.CUSTOM5 )
6910       , SUM(fact1.CUSTOM6 )
6911       , SUM(fact1.CUSTOM7 )
6912       , SUM(fact1.CUSTOM8 )
6913       , SUM(fact1.CUSTOM9 )
6914       , SUM(fact1.CUSTOM10 )
6915       , SUM(fact1.CUSTOM11 )
6916       , SUM(fact1.CUSTOM12 )
6917       , SUM(fact1.CUSTOM13 )
6918       , SUM(fact1.CUSTOM14 )
6919       , SUM(fact1.CUSTOM15 )
6920       , fact1.line_type
6921       , g_default_prg_level
6922       ,fact1.PLAN_TYPE_CODE   /*4471527 */
6923     FROM pji_fp_aggr_pjp1       fact1
6924        , Pji_RBS_DENORM         rbs
6925 	 , pji_rollup_level_status smart
6926 	 , pji_pjp_rbs_header      rhdr
6927     WHERE 1 = 1
6928      AND fact1.project_id  = rhdr.project_id
6929      AND fact1.plan_version_id  = rhdr.plan_version_id
6930     AND fact1.plan_type_code = rhdr.plan_type_code  /*4471527 */
6931      AND rbs.struct_version_id = rhdr.rbs_version_id
6932      AND fact1.rbs_ELEMENT_ID = rbs.sub_id
6933      AND rbs.sup_level <> rbs.sub_level
6934      AND rbs.sup_level <> 1
6935      AND fact1.RBS_AGGR_LEVEL = 'L'
6936      AND smart.rbs_version_id = rbs.struct_version_id
6937      AND smart.plan_version_id = fact1.plan_version_id
6938      AND smart.plan_type_code = fact1.plan_type_code  /*4471527 */
6939      AND fact1.worker_id = g_worker_id
6940     GROUP BY
6941         fact1.PROJECT_ID
6942 	, fact1.PROJECT_ORG_ID
6943 	, fact1.PROJECT_ORGANIZATION_ID
6944       , fact1.project_element_id
6945 	, fact1.TIME_ID
6946       , fact1.PERIOD_TYPE_ID
6947       , fact1.CALENDAR_TYPE
6948 	, fact1.WBS_ROLLUP_FLAG
6949 	, fact1.PRG_ROLLUP_FLAG
6950       , fact1.CURR_RECORD_TYPE_ID
6951       , fact1.CURRENCY_CODE
6952       , rbs.sup_id
6953       , fact1.RBS_VERSION_ID
6954       , fact1.PLAN_VERSION_ID
6955 	, fact1.PLAN_TYPE_ID
6956       , fact1.line_type
6957       , fact1.plan_type_code ;   /*4471527*/
6958 
6959 
6960 EXCEPTION
6961   WHEN OTHERS THEN
6962     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
6963                              p_procedure_name => 'ROLLUP_FPR_RBS');
6964     RAISE;
6965 END;
6966 
6967 
6968 PROCEDURE ROLLUP_FPR_RBS_T_SLICE IS
6969     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
6970 
6971 BEGIN
6972 
6973   print_time(' worker id is ... ' || 1);
6974 
6975     INSERT INTO  pji_fp_aggr_pjp1
6976      (
6977        WORKER_ID
6978      , PROJECT_ID
6979      , PROJECT_ORG_ID
6980      , PROJECT_ORGANIZATION_ID
6981      , PROJECT_ELEMENT_ID
6982      , TIME_ID
6983      , PERIOD_TYPE_ID
6984      , CALENDAR_TYPE
6985      , RBS_AGGR_LEVEL
6986      , WBS_ROLLUP_FLAG
6987      , PRG_ROLLUP_FLAG
6988      , CURR_RECORD_TYPE_ID
6989      , CURRENCY_CODE
6990      , RBS_ELEMENT_ID
6991      , RBS_VERSION_ID
6992      , PLAN_VERSION_ID
6993      , PLAN_TYPE_ID
6994      , RAW_COST
6995      , BRDN_COST
6996      , REVENUE
6997      , BILL_RAW_COST
6998      , BILL_BRDN_COST
6999      , BILL_LABOR_RAW_COST
7000      , BILL_LABOR_BRDN_COST
7001      , BILL_LABOR_HRS
7002      , EQUIPMENT_RAW_COST
7003      , EQUIPMENT_BRDN_COST
7004      , CAPITALIZABLE_RAW_COST
7005      , CAPITALIZABLE_BRDN_COST
7006      , LABOR_RAW_COST
7007      , LABOR_BRDN_COST
7008      , LABOR_HRS
7009      , LABOR_REVENUE
7010      , EQUIPMENT_HOURS
7011      , BILLABLE_EQUIPMENT_HOURS
7012      , SUP_INV_COMMITTED_COST
7013      , PO_COMMITTED_COST
7014      , PR_COMMITTED_COST
7015      , OTH_COMMITTED_COST
7016        , ACT_LABOR_HRS
7017 	   , ACT_EQUIP_HRS
7018 	   , ACT_LABOR_BRDN_COST
7019 	   , ACT_EQUIP_BRDN_COST
7020 	   , ACT_BRDN_COST
7021 	   , ACT_RAW_COST
7022 	   , ACT_REVENUE
7023          , ACT_LABOR_RAW_COST
7024          , ACT_EQUIP_RAW_COST
7025 	   , ETC_LABOR_HRS
7026 	   , ETC_EQUIP_HRS
7027 	   , ETC_LABOR_BRDN_COST
7028 	   , ETC_EQUIP_BRDN_COST
7029 	   , ETC_BRDN_COST
7030          , ETC_RAW_COST
7031          , ETC_LABOR_RAW_COST
7032          , ETC_EQUIP_RAW_COST
7033      , CUSTOM1
7034      , CUSTOM2
7035      , CUSTOM3
7036      , CUSTOM4
7037      , CUSTOM5
7038      , CUSTOM6
7039      , CUSTOM7
7040      , CUSTOM8
7041      , CUSTOM9
7042      , CUSTOM10
7043      , CUSTOM11
7044      , CUSTOM12
7045      , CUSTOM13
7046      , CUSTOM14
7047      , CUSTOM15
7048      , LINE_TYPE
7049      , PRG_LEVEL
7050      , PLAN_TYPE_CODE  /*4471527 */
7051     )
7052      SELECT
7053         g_worker_id WORKER_ID
7054       , fact1.PROJECT_ID
7055 	, fact1.PROJECT_ORG_ID
7056 	, fact1.PROJECT_ORGANIZATION_ID
7057       , fact1.project_element_id
7058 	, fact1.TIME_ID
7059       , fact1.PERIOD_TYPE_ID
7060       , fact1.CALENDAR_TYPE
7061 	, g_top_level
7062 	, fact1.WBS_ROLLUP_FLAG
7063 	, fact1.PRG_ROLLUP_FLAG
7064       , fact1.CURR_RECORD_TYPE_ID
7065       , fact1.CURRENCY_CODE
7066       , -1
7067       , -1
7068       , fact1.PLAN_VERSION_ID
7069 	, fact1.PLAN_TYPE_ID
7070       , SUM(fact1.RAW_COST)
7071       , SUM(fact1.BRDN_COST)
7072       , SUM(fact1.REVENUE)
7073       , SUM(fact1.BILL_RAW_COST)
7074       , SUM(fact1.BILL_BRDN_COST )
7075       , SUM(fact1.BILL_LABOR_RAW_COST)
7076       , SUM(fact1.BILL_LABOR_BRDN_COST )
7077       , SUM(fact1.BILL_LABOR_HRS )
7078       , SUM(fact1.EQUIPMENT_RAW_COST )
7079       , SUM(fact1.EQUIPMENT_BRDN_COST )
7080       , SUM(fact1.CAPITALIZABLE_RAW_COST )
7081       , SUM(fact1.CAPITALIZABLE_BRDN_COST )
7082       , SUM(fact1.LABOR_RAW_COST )
7083       , SUM(fact1.LABOR_BRDN_COST )
7084       , SUM(fact1.LABOR_HRS)
7085       , SUM(fact1.LABOR_REVENUE)
7086       , SUM(fact1.EQUIPMENT_HOURS)
7087       , SUM(fact1.BILLABLE_EQUIPMENT_HOURS)
7088       , SUM(fact1.SUP_INV_COMMITTED_COST)
7089       , SUM(fact1.PO_COMMITTED_COST   )
7090       , SUM(fact1.PR_COMMITTED_COST  )
7091       , SUM(fact1.OTH_COMMITTED_COST)
7092        , SUM(fact1.ACT_LABOR_HRS)
7093 	 , SUM(fact1.ACT_EQUIP_HRS)
7094 	 , SUM(fact1.ACT_LABOR_BRDN_COST)
7095 	 , SUM(fact1.ACT_EQUIP_BRDN_COST)
7096 	 , SUM(fact1.ACT_BRDN_COST)
7097 	 , SUM(fact1.ACT_RAW_COST)
7098 	 , SUM(fact1.ACT_REVENUE)
7099        , SUM(fact1.ACT_LABOR_RAW_COST)
7100        , SUM(fact1.ACT_EQUIP_RAW_COST)
7101 	 , SUM(fact1.ETC_LABOR_HRS)
7102 	 , SUM(fact1.ETC_EQUIP_HRS)
7103 	 , SUM(fact1.ETC_LABOR_BRDN_COST)
7104 	 , SUM(fact1.ETC_EQUIP_BRDN_COST)
7105 	 , SUM(fact1.ETC_BRDN_COST )
7106        , SUM(fact1.ETC_RAW_COST )
7107        , SUM(fact1.ETC_LABOR_RAW_COST)
7108        , SUM(fact1.ETC_EQUIP_RAW_COST)
7109       , SUM(fact1.CUSTOM1 )
7110       , SUM(fact1.CUSTOM2 )
7111       , SUM(fact1.CUSTOM3 )
7112       , SUM(fact1.CUSTOM4 )
7113       , SUM(fact1.CUSTOM5 )
7114       , SUM(fact1.CUSTOM6 )
7115       , SUM(fact1.CUSTOM7 )
7116       , SUM(fact1.CUSTOM8 )
7117       , SUM(fact1.CUSTOM9 )
7118       , SUM(fact1.CUSTOM10 )
7119       , SUM(fact1.CUSTOM11 )
7120       , SUM(fact1.CUSTOM12 )
7121       , SUM(fact1.CUSTOM13 )
7122       , SUM(fact1.CUSTOM14 )
7123       , SUM(fact1.CUSTOM15 )
7124       , fact1.line_type
7125       , g_default_prg_level
7126       , fact1.plan_type_code
7127     FROM pji_fp_aggr_pjp1       fact1
7128        , pji_fm_extr_plnver4    ver3
7129     WHERE
7130           fact1.RBS_AGGR_LEVEL = g_lowest_level
7131       AND fact1.worker_id = g_worker_id
7132       AND ver3.worker_id = g_worker_id
7133       AND fact1.plan_version_id = ver3.plan_version_id
7134      AND fact1.plan_type_code = ver3.plan_type_code    /*4471527 */
7135       AND ( fact1.rbs_version_id = ver3.rbs_struct_version_id
7136          OR fact1.rbs_version_id = -1)
7137       AND ver3.secondary_rbs_flag = 'N'
7138     GROUP BY
7139         fact1.PROJECT_ID
7140 	, fact1.PROJECT_ORG_ID
7141 	, fact1.PROJECT_ORGANIZATION_ID
7142       , fact1.project_element_id
7143 	, fact1.TIME_ID
7144       , fact1.PERIOD_TYPE_ID
7145       , fact1.CALENDAR_TYPE
7146 	, fact1.WBS_ROLLUP_FLAG
7147 	, fact1.PRG_ROLLUP_FLAG
7148       , fact1.CURR_RECORD_TYPE_ID
7149       , fact1.CURRENCY_CODE
7150       , fact1.RBS_VERSION_ID
7151       , fact1.PLAN_VERSION_ID
7152       , fact1.PLAN_TYPE_ID
7153       , fact1.line_type
7154       , fact1.plan_type_code;  /*4471527 */
7155 
7156 
7157 EXCEPTION
7158   WHEN OTHERS THEN
7159     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7160                              p_procedure_name => 'ROLLUP_FPR_RBS_T_SLICE');
7161     RAISE;
7162 END;
7163 
7164 
7165 PROCEDURE COMPUTE_XBS_UPDATED_ROLLUPS IS
7166 BEGIN
7167   NULL;
7168 EXCEPTION
7169   WHEN OTHERS THEN
7170     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7171                              p_procedure_name => 'COMPUTE_XBS_UPDATED_ROLLUPS');
7172     RAISE;
7173 END;
7174 
7175 
7176 PROCEDURE POPULATE_RBS_HDR IS
7177 
7178     l_last_update_date     date   := SYSDATE;
7179     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
7180     l_creation_date        date   := SYSDATE;
7181     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
7182     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
7183 
7184 BEGIN
7185 
7186   print_time ( ' Before update rbs header ' );
7187 
7188   INSERT INTO pji_pjp_rbs_header
7189   (
7190       project_id
7191     , plan_version_id
7192     , rbs_version_id
7193     , reporting_usage_flag
7194     , prog_rep_usage_flag
7195     , plan_usage_flag
7196     , LAST_UPDATE_DATE
7197     , LAST_UPDATED_BY
7198     , CREATION_DATE
7199     , CREATED_BY
7200     , LAST_UPDATE_LOGIN
7201     ,PLAN_TYPE_CODE    /*4471527 */
7202   )
7203   SELECT /*+ ordered */ DISTINCT
7204         rpa.project_id
7205       , bv.plan_version_id
7206       , rpa.rbs_version_id
7207       , rpa.reporting_usage_flag
7208       , rpa.prog_rep_usage_flag
7209       , DECODE(bv.wp_flag, 'Y', rpa.wp_usage_flag, rpa.fp_usage_flag)
7210       , l_last_update_date
7211       , l_last_updated_by
7212       , l_creation_date
7213       , l_created_by
7214       , l_last_update_login
7215       , bv.plan_type_code
7216   FROM
7217     PJI_FM_EXTR_PLNVER4    bv,
7218     PA_RBS_PRJ_ASSIGNMENTS rpa,
7219     PJI_PJP_RBS_HEADER     head
7220   WHERE bv.project_id = rpa.project_id
7221     AND bv.worker_id = g_worker_id
7222     AND bv.RBS_STRUCT_VERSION_ID  = rpa.RBS_VERSION_ID
7223     AND bv.PROJECT_ID = head.PROJECT_ID (+)
7224     AND bv.PLAN_VERSION_ID = head.PLAN_VERSION_ID (+)
7225     AND bv.PLAN_TYPE_CODE  = head.PLAN_TYPE_CODE (+)    /*4471527 */
7226     AND bv.RBS_STRUCT_VERSION_ID = head.RBS_VERSION_ID (+)
7227     AND head.PROJECT_ID IS NULL;
7228 
7229   print_time ( ' After update rbs header, # rows inserted is.. '|| SQL%ROWCOUNT );
7230 
7231 EXCEPTION
7232   WHEN OTHERS THEN
7233     print_time ( ' update rbs header exception ' || sqlerrm );
7234     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7235                              p_procedure_name => 'POPULATE_RBS_HDR');
7236     RAISE;
7237 END;
7238 
7239 
7240 PROCEDURE POPULATE_WBS_HDR IS
7241 
7242     l_last_update_date     date   := SYSDATE;
7243     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
7244     l_creation_date        date   := SYSDATE;
7245     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
7246     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
7247 
7248 BEGIN
7249 
7250   print_time ( ' Before populate wbs header ' );
7251 
7252   INSERT INTO PJI_PJP_WBS_HEADER
7253   (
7254     PROJECT_ID
7255   , PLAN_VERSION_ID
7256   , WBS_VERSION_ID
7257   , WP_FLAG
7258   , CB_FLAG
7259   , CO_FLAG
7260   , LOCK_FLAG
7261   , PLAN_TYPE_ID
7262   , MIN_TXN_DATE
7263   , MAX_TXN_DATE
7264   , LAST_UPDATE_DATE
7265   , LAST_UPDATED_BY
7266   , CREATION_DATE
7267   , CREATED_BY
7268   , LAST_UPDATE_LOGIN
7269   , PLAN_TYPE_CODE   /* 4471527 */
7270   )
7271   SELECT DISTINCT
7272          ver.project_id
7273        , ver.plan_version_id
7274        , ver.wbs_struct_version_id
7275        , ver.wp_flag
7276        , DECODE( (ver.current_flag || ver.baselined_flag) , 'YY', 'Y', 'N')
7277        , ver.current_original_flag
7278        , null
7279        , ver.plan_type_id
7280        , to_date('3000/01/01', 'YYYY/MM/DD') MIN_TXN_DATE
7281        , to_date('0001/01/01', 'YYYY/MM/DD') MAX_TXN_DATE
7282        , l_last_update_date
7283        , l_last_updated_by
7284        , l_creation_date
7285        , l_created_by
7286        , l_last_update_login
7287        , ver.plan_type_code
7288   FROM PJI_FM_EXTR_PLNVER4 ver
7289      , PJI_PJP_WBS_HEADER  whdr
7290   WHERE ver.worker_id = g_worker_id
7291     AND ver.plan_version_id = whdr.plan_version_id (+)
7292    AND ver.plan_type_code = whdr.plan_type_code(+)   /*4471527 */
7293     AND ver.project_id = whdr.project_id (+)
7294     AND ver.plan_type_id = whdr.plan_type_id (+)
7295     -- AND ver.wbs_struct_version_id = whdr.wbs_version_id (+)
7296     AND whdr.plan_version_id IS NULL
7297     AND ver.plan_version_id <> -1
7298   ORDER BY
7299         ver.project_id
7300       , ver.plan_version_id;
7301 
7302   print_time ( ' After populate wbs header ' );
7303 
7304 EXCEPTION
7305   WHEN OTHERS THEN
7306     print_time ( ' Populate wbs header exception ' || sqlerrm );
7307     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7308                              p_procedure_name => 'POPULATE_WBS_HDR');
7309     RAISE;
7310 END;
7311 
7312 
7313 
7314 PROCEDURE UPDATE_WBS_HDR  IS
7315 
7316     l_last_update_date     date   := SYSDATE;
7317     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
7318     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
7319 
7320 BEGIN
7321 
7322   print_time ( ' Before update wbs header ' );
7323 
7324   delete from pji_fp_aggr_pjp1_t; -- 5309891
7325 
7326   INSERT INTO pji_fp_aggr_pjp1_t
7327   ( WORKER_ID
7328   , PROJECT_ID, PROJECT_ORG_ID, PROJECT_ORGANIZATION_ID, PROJECT_ELEMENT_ID
7329   , TIME_ID, PERIOD_TYPE_ID, CALENDAR_TYPE, RBS_AGGR_LEVEL
7330   , WBS_ROLLUP_FLAG, PRG_ROLLUP_FLAG , CURR_RECORD_TYPE_ID ,CURRENCY_CODE
7331   , PLAN_VERSION_ID, PLAN_TYPE_ID, PLAN_TYPE_CODE
7332   , start_date, end_date)
7333   SELECT g_worker_id
7334        , whdr.project_id, 0, 0, 0
7335        , 0, 0, 'X', 'X'
7336 	   , 'X', 'X', 0, 'X'
7337        , whdr.plan_version_id, NVL(whdr.plan_type_id, -1)
7338        , whdr.PLAN_TYPE_CODE
7339        , MIN(LEAST(cal.start_date,  NVL(whdr.min_txn_date, cal.start_date))) start_date
7340        , MAX(GREATEST(cal.end_date, NVL(whdr.max_txn_date, cal.end_date))) end_date
7341     FROM PJI_FP_AGGR_PJP1    pjp1
7342        , pji_time_cal_period_v   cal
7343        , pji_pjp_wbs_header whdr
7344    WHERE
7345          pjp1.worker_id = g_worker_id
7346      AND pjp1.project_id = whdr.project_id
7347      AND pjp1.plan_type_id = NVL(whdr.plan_type_id, -1)
7348      AND pjp1.plan_version_id = whdr.plan_version_id
7349      AND pjp1.time_id = cal.cal_period_id
7350      AND pjp1.calendar_type IN ('P', 'G') -- Non time ph and ent cals don't need to be considered.
7351      AND pjp1.plan_type_code = whdr.plan_type_code
7352    GROUP BY whdr.project_id, whdr.plan_type_id
7353           , whdr.plan_version_id, whdr.plan_type_code;
7354 
7355   UPDATE /*+ index(whdr,PJI_PJP_WBS_HEADER_N1) */
7356       PJI_PJP_WBS_HEADER whdr
7357   SET ( MIN_TXN_DATE
7358       , MAX_TXN_DATE
7359       , LAST_UPDATE_DATE
7360       , LAST_UPDATED_BY
7361       , LAST_UPDATE_LOGIN ) = (
7362   SELECT start_date
7363        , end_date
7364        , l_last_update_date
7365        , l_last_updated_by
7366        , l_last_update_login
7367     FROM pji_fp_aggr_pjp1_t dates
7368    WHERE
7369          dates.plan_version_id = whdr.plan_version_id
7370      AND dates.project_id = whdr.project_id
7371      AND dates.plan_type_id = NVL(whdr.plan_type_id, -1)
7372      AND dates.plan_type_code = whdr.plan_type_code
7373                                       )
7374  WHERE (project_id, NVL(plan_type_id, -1), plan_version_id, plan_type_code) IN (
7375          SELECT project_id, plan_type_id, plan_version_id , plan_type_code
7376          FROM   PJI_FP_AGGR_PJP1_T ver
7377          );
7378 
7379   delete from pji_fp_aggr_pjp1_t;
7380 
7381 
7382   print_time ( ' After update wbs header ' );
7383 
7384 EXCEPTION
7385   WHEN OTHERS THEN
7386     print_time ( ' update wbs header exception ' || sqlerrm );
7387     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7388                              p_procedure_name => 'POPULATE_WBS_HDR');
7389     RAISE;
7390 END;
7391 
7392 
7393 
7394 
7395 PROCEDURE MARK_EXTRACTED_PLANS(p_slice_type IN VARCHAR2) IS
7396 BEGIN
7397 
7398   --
7399   -- On PA_BUDGET_VERSIONS, the PJI_SUMMARIZED_FLAG can have four values:
7400   --   'N' the plan version has not been summarized
7401   --   'P' the plan version has been partially summarized due to dangling currency or calendar records
7402   --   'Y' the plan version has been fully summarized in Project Performance and
7403   --   NULL the plan version has been fully summarized in both PJI and Project Performance.
7404   --
7405   -- Note that, in PJI data extraction, data that existed before PJI was installed
7406   -- has PJI_SUMMARIZED_FLAG = null and new transactions are inserted with value 'N'.
7407   -- A similar situation does not occur in PA_BUDGET_VERSIONS.  During the upgrade process
7408   -- will pull necessary data from PA_BUDGET_VERSIONS and set PJI_SUMMARIZATION_FLAG to 'P', 'N'
7409   -- or 'Y' accordingly.  New versions will be inserted with value 'N'.)
7410   --
7411 
7412   IF (p_slice_type = 'PRI') THEN
7413     UPDATE /*+ index(bv,pa_budget_versions_u1) */
7414            pa_budget_versions bv
7415     SET    pji_summarized_flag =
7416                        DECODE(wp_version_flag
7417                             , 'Y', 'Y'
7418                             , DECODE(budget_status_code
7419                                    , 'B', 'P'
7420                                    , 'Y')
7421                              ),
7422 	    record_version_number=nvl(record_version_number,0)+1
7423     WHERE  budget_version_id IN
7424                ( SELECT DISTINCT plan_version_id
7425                  FROM   pji_fm_extr_plnver4
7426                  WHERE  worker_id = g_worker_id );
7427 
7428   ELSE -- Secondary slice.
7429 /*  Modified the logic for bug 4039796 */
7430 UPDATE PJI_FM_EXTR_PLNVER4 ver3
7431     SET time_dangling_flag = 'Y'
7432   WHERE (project_id,plan_version_id,plan_type_code) IN
7433                 ( SELECT project_id,plan_version_id,plan_type_code   /*4471527 */
7434                     FROM pji_fp_aggr_pjp1 pjp1
7435                    WHERE pjp1.worker_id = g_worker_id
7436                      AND (    pjp1.time_dangling_flag IS NOT NULL
7437                            OR pjp1.rate_dangling_flag IS NOT NULL )
7438                 )
7439    AND worker_id = g_worker_id ;
7440 
7441 UPDATE  /*+ index( bv , pa_budget_versions_u1 ) */
7442         pa_budget_versions bv
7443    SET  pji_summarized_flag = 'Y',
7444         record_version_number=nvl(record_version_number,0)+1
7445  WHERE  budget_version_id IN (SELECT plan_version_id
7446                                 FROM  PJI_FM_EXTR_PLNVER4 ver3
7447                                WHERE ver3.worker_id=g_worker_id
7448                                  AND ver3.time_dangling_flag IS NULL);
7449 
7450 UPDATE  /*+ index( bv , pa_budget_versions_u1 ) */
7451         pa_budget_versions bv
7452    SET  pji_summarized_flag = 'P',
7453         record_version_number=nvl(record_version_number,0)+1
7454  WHERE  budget_version_id IN (SELECT plan_version_id
7455                                 FROM  PJI_FM_EXTR_PLNVER4 ver3
7456                                WHERE ver3.worker_id=g_worker_id
7457                                  AND ver3.time_dangling_flag IS NOT NULL);
7458 
7459   END IF;
7460 
7461 EXCEPTION
7462   WHEN OTHERS THEN
7463     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7464                              p_procedure_name => 'MARK_EXTRACTED_PLANS');
7465     RAISE;
7466 END;
7467 
7468 
7469 ------------------------------------------------------------------------------
7470 ---- WBS rollup api..
7471 ------------------------------------------------------------------------------
7472 
7473 PROCEDURE CREATE_WBSRLP IS
7474 BEGIN
7475 
7476   print_time('... Begin CREATE_WBSRLP ' );
7477 
7478   PRINT_NUM_WBSRLPRCDS_INPJP1;
7479 
7480   print_time('... Before call to rollup_fpr_wbs...' );
7481 
7482   PJI_PJP_SUM_ROLLUP.ROLLUP_FPR_WBS(g_worker_id);  /*Added for 3852901*/
7483 
7484   print_time('... after call to rollup_fpr_wbs. ' );
7485 
7486   PRINT_NUM_WBSRLPRCDS_INPJP1;
7487 
7488   print_time('... End CREATE_WBSRLP ' );
7489 
7490 EXCEPTION
7491   WHEN OTHERS THEN
7492     print_time('... Exception CREATE_WBSRLP ' );
7493     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7494                              p_procedure_name => 'CREATE_WBSRLP');
7495     RAISE;
7496 END;
7497 
7498 
7499 
7500 PROCEDURE PRINT_NUM_WBSRLPRCDS_INPJP1 IS
7501   l_count NUMBER;
7502   l_wbs_rollup_flag  VARCHAR2(1);
7503   l_prg_rollup_flag  VARCHAR2(1);
7504   l_return_status    VARCHAR2(1);
7505 
7506 BEGIN
7507 
7508   l_wbs_rollup_flag  := 'N';
7509   l_prg_rollup_flag  := 'N';
7510 
7511 /*
7512   select count(1)
7513   INTO   l_count
7514   FROM   pji_fp_aggr_pjp1
7515   WHERE  wbs_rollup_flag = l_wbs_rollup_flag
7516     AND  prg_rollup_flag = l_prg_rollup_flag;
7517 
7518   print_time ( ' l_worker_id = '|| g_worker_id || ' wbs rollup flag = ' || l_wbs_rollup_flag || ' prg rollup flag = ' || l_prg_rollup_flag || ' l_count ' || l_count);
7519 
7520 
7521   l_wbs_rollup_flag  := 'N';
7522   l_prg_rollup_flag  := 'Y';
7523 
7524   select count(1)
7525   INTO   l_count
7526   FROM   pji_fp_aggr_pjp1
7527   WHERE  wbs_rollup_flag = l_wbs_rollup_flag
7528     AND  prg_rollup_flag = l_prg_rollup_flag;
7529 
7530   print_time ( ' l_worker_id = '|| g_worker_id || ' wbs rollup flag = ' || l_wbs_rollup_flag || ' prg rollup flag = ' || l_prg_rollup_flag || ' l_count ' || l_count);
7531 
7532 
7533   l_wbs_rollup_flag  := 'Y';
7534   l_prg_rollup_flag  := 'N';
7535 
7536   select count(1)
7537   INTO   l_count
7538   FROM   pji_fp_aggr_pjp1
7539   WHERE  wbs_rollup_flag = l_wbs_rollup_flag
7540     AND  prg_rollup_flag = l_prg_rollup_flag;
7541 
7542   print_time ( ' l_worker_id = '|| g_worker_id || ' wbs rollup flag = ' || l_wbs_rollup_flag || ' prg rollup flag = ' || l_prg_rollup_flag || ' l_count ' || l_count);
7543 
7544 
7545   l_wbs_rollup_flag  := 'Y';
7546   l_prg_rollup_flag  := 'Y';
7547 
7548   select count(1)
7549   INTO   l_count
7550   FROM   pji_fp_aggr_pjp1
7551   WHERE  wbs_rollup_flag = l_wbs_rollup_flag
7552     AND  prg_rollup_flag = l_prg_rollup_flag;
7553 
7554   print_time ( ' l_worker_id = '|| g_worker_id || ' wbs rollup flag = ' || l_wbs_rollup_flag || ' prg rollup flag = ' || l_prg_rollup_flag || ' l_count ' || l_count);
7555 */
7556 
7557 EXCEPTION
7558   WHEN OTHERS THEN
7559     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
7560     ( p_package_name   => g_package_name
7561     , p_procedure_name => 'PRINT_NUM_WBSRLPRCDS_INPJP1'
7562     , x_return_status  => l_return_status ) ;
7563 
7564     RAISE;
7565 END;
7566 
7567 
7568 PROCEDURE CLEANUP_INTERIM_TABLES IS
7569   l_count NUMBER;
7570 BEGIN
7571 
7572   print_time('.......CLEANUP_INTERIM_TABLES: Begin. ');
7573 
7574   DELETE FROM PJI_FM_EXTR_PLNVER4
7575   WHERE worker_id = g_worker_id ;
7576 
7577   print_time('.......ver3 rows deleted: #= ' || sql%rowcount);
7578 
7579   DELETE FROM pji_fp_aggr_pjp1
7580   WHERE worker_id = g_worker_id;
7581 
7582   print_time('.......pjp1 rows deleted: #= ' || sql%rowcount);
7583 
7584   print_time('.......CLEANUP_INTERIM_TABLES: End. ');
7585 
7586 EXCEPTION
7587   WHEN OTHERS THEN
7588     print_time('.......CLEANUP_INTERIM_TABLES: Exception. ' || sqlerrm);
7589     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7590                              p_procedure_name => 'CLEANUP_INTERIM_TABLES');
7591     RAISE;
7592 END CLEANUP_INTERIM_TABLES;
7593 
7594 
7595 ----------
7596 -- Print time API to measure time taken by each api. Also useful for debugging.
7597 ----------
7598 PROCEDURE PRINT_TIME(p_tag IN VARCHAR2) IS
7599 BEGIN
7600   PJI_PJP_FP_CURR_WRAP.print_time(p_tag);
7601 EXCEPTION
7602   WHEN OTHERS THEN
7603     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
7604                              p_procedure_name => 'PRINT_TIME');
7605     RAISE;
7606 END;
7607 
7608 PROCEDURE EXTRACT_PLAN_AMTS_SECRBS_GLC12 (
7609   p_pull_dangling_flag IN VARCHAR2 := 'Y') -- Reversals to be computed only if pull_dangling flag is 'Y'
7610 IS
7611 BEGIN
7612 
7613     print_time ( ' EXTRACT_PLAN_AMTS_SECRBS_GLC12 begin. ' );
7614 
7615     PJI_PJP_FP_CURR_WRAP.get_global_currency_info (
7616       x_currency_conversion_rule => g_currency_conversion_rule
7617     , x_prorating_format         => g_prorating_format
7618     , x_global1_currency_code    => g_global1_currency_code
7619     , x_global2_currency_code    => g_global2_currency_code
7620     , x_global1_currency_mau     => g_global1_currency_mau
7621     , x_global2_currency_mau     => g_global2_currency_mau ) ;
7622 
7623 
7624   print_time(' Got global currency settings. ');
7625   print_time(' g_currency_conversion_rule ' || g_currency_conversion_rule || ' g_prorating_format ' ||  g_prorating_format );
7626   print_time(' g_global1_currency_code ' || g_global1_currency_code || ' g_global2_currency_code ' || g_global2_currency_code );
7627   print_time(' g_global1_currency_mau ' || g_global1_currency_mau || ' g_global2_currency_mau ' || g_global2_currency_mau ) ;
7628 
7629     INSERT INTO PJI_FP_AGGR_PJP1
7630     (
7631          WORKER_ID
7632        , PROJECT_ID
7633        , PROJECT_ORG_ID
7634        , PROJECT_ORGANIZATION_ID
7635        , PROJECT_ELEMENT_ID
7636        , TIME_ID
7637        , PERIOD_TYPE_ID
7638        , CALENDAR_TYPE
7639        , RBS_AGGR_LEVEL
7640        , WBS_ROLLUP_FLAG
7641        , PRG_ROLLUP_FLAG
7642        , CURR_RECORD_TYPE_ID
7643        , CURRENCY_CODE
7644        , RBS_ELEMENT_ID
7645        , RBS_VERSION_ID
7646        , PLAN_VERSION_ID
7647        , PLAN_TYPE_ID
7648        , RAW_COST
7649        , BRDN_COST
7650        , REVENUE
7651        , BILL_RAW_COST
7652        , BILL_BRDN_COST
7653        , BILL_LABOR_RAW_COST
7654        , BILL_LABOR_BRDN_COST
7655        , BILL_LABOR_HRS
7656        , EQUIPMENT_RAW_COST
7657        , EQUIPMENT_BRDN_COST
7658        , CAPITALIZABLE_RAW_COST
7659        , CAPITALIZABLE_BRDN_COST
7660        , LABOR_RAW_COST
7661        , LABOR_BRDN_COST
7662        , LABOR_HRS
7663        , LABOR_REVENUE
7664        , EQUIPMENT_HOURS
7665        , BILLABLE_EQUIPMENT_HOURS
7666        , SUP_INV_COMMITTED_COST
7667        , PO_COMMITTED_COST
7668        , PR_COMMITTED_COST
7669        , OTH_COMMITTED_COST
7670        , ACT_LABOR_HRS
7671 	   , ACT_EQUIP_HRS
7672 	   , ACT_LABOR_BRDN_COST
7673 	   , ACT_EQUIP_BRDN_COST
7674 	   , ACT_BRDN_COST
7675 	   , ACT_RAW_COST
7676 	   , ACT_REVENUE
7677          , ACT_LABOR_RAW_COST
7678          , ACT_EQUIP_RAW_COST
7679 	   , ETC_LABOR_HRS
7680 	   , ETC_EQUIP_HRS
7681 	   , ETC_LABOR_BRDN_COST
7682 	   , ETC_EQUIP_BRDN_COST
7683 	   , ETC_BRDN_COST
7684          , ETC_RAW_COST
7685          , ETC_LABOR_RAW_COST
7686          , ETC_EQUIP_RAW_COST
7687        , CUSTOM1
7688        , CUSTOM2
7689        , CUSTOM3
7690        , CUSTOM4
7691        , CUSTOM5
7692        , CUSTOM6
7693        , CUSTOM7
7694        , CUSTOM8
7695        , CUSTOM9
7696        , CUSTOM10
7697        , CUSTOM11
7698        , CUSTOM12
7699        , CUSTOM13
7700        , CUSTOM14
7701        , CUSTOM15
7702        , LINE_TYPE
7703        , RATE_DANGLING_FLAG
7704        , TIME_DANGLING_FLAG
7705        , START_DATE
7706        , END_DATE
7707        , PRG_LEVEL
7708        ,PLAN_TYPE_CODE   /*4471527 */
7709 	)
7710     SELECT
7711          WORKER_ID
7712        , PROJECT_ID
7713        , PROJECT_ORG_ID
7714        , project_ORGANIZATION_ID
7715        , WBS_ELEMENT_ID
7716        , time_id
7717        , period_type_id
7718        , CALENDAR_TYPE
7719        , g_lowest_level RBS_AGGR_LEVEL
7720        , 'N' WBS_ROLLUP_FLAG
7721        , 'N' PRG_ROLLUP_FLAG
7722        , CURR_RECORD_TYPE
7723        , currency_code
7724        , RBS_ELEMENT_ID
7725        , RBS_STRUCT_VERSION_ID
7726        , plan_version_id
7727        , plan_type_id
7728        , decode(rate_dangling_flag,null,SUM(RAW_COST),0)
7729        , decode(rate_dangling_flag,null,SUM(BRDN_COST),0)
7730        , decode(rate_dangling_flag,null,SUM(REVENUE),0)
7731        , decode(rate_dangling_flag,null,SUM ( BILL_RAW_COST ),0)  BILL_RAW_COST
7732        , decode(rate_dangling_flag,null,SUM (BILL_BRDN_COST ),0)   BILL_BRDN_COST
7733        , decode(rate_dangling_flag,null,SUM ( BILL_LABOR_RAW_COST),0) BILL_LABOR_RAW_COST
7734        , decode(rate_dangling_flag,null,SUM ( BILL_LABOR_BRDN_COST),0) BILL_LABOR_BRDN_COST
7735        , decode(rate_dangling_flag,null,SUM ( BILL_LABOR_HRS),0) BILL_LABOR_HRS
7736        , decode(rate_dangling_flag,null,SUM ( EQUIPMENT_RAW_COST),0) EQUIPMENT_RAW_COST
7737        , decode(rate_dangling_flag,null,SUM ( EQUIPMENT_BRDN_COST),0) EQUIPMENT_BRDN_COST
7738        , decode(rate_dangling_flag,null,SUM (CAPITALIZABLE_RAW_COST),0) CAPITALIZABLE_RAW_COST
7739        , decode(rate_dangling_flag,null,SUM ( CAPITALIZABLE_BRDN_COST),0)
7740        , decode(rate_dangling_flag,null,SUM ( LABOR_RAW_COST),0) LABOR_RAW_COST
7741        , decode(rate_dangling_flag,null,SUM ( LABOR_BRDN_COST),0) LABOR_BRDN_COST
7742        , decode(rate_dangling_flag,null,SUM ( labor_hrs),0)  labor_hrs
7743        , decode(rate_dangling_flag,null,SUM (LABOR_REVENUE),0)  LABOR_REVENUE
7744        , decode(rate_dangling_flag,null,SUM (EQUIPMENT_HOURS),0) EQUIPMENT_HOURS
7745        , decode(rate_dangling_flag,null,SUM ( BILLABLE_EQUIPMENT_HOURS),0) BILLABLE_EQUIPMENT_HOURS
7746        , decode(rate_dangling_flag,null,SUM(SUP_INV_COMMITTED_COST),0) SUP_INV_COMMITTED_COST
7747        , decode(rate_dangling_flag,null,SUM(PO_COMMITTED_COST),0) PO_COMMITTED_COST
7748        , decode(rate_dangling_flag,null,SUM(PR_COMMITTED_COST),0) PR_COMMITTED_COST
7749        , decode(rate_dangling_flag,null,SUM(OTH_COMMITTED_COST),0) PR_COMMITTED_COST
7750        , decode(rate_dangling_flag,null,SUM ( ACT_LABOR_HRS),0) ACT_LABOR_HRS
7751        , decode(rate_dangling_flag,null,SUM (ACT_EQUIP_HOURS),0) ACT_EQUIP_HOURS
7752        , decode(rate_dangling_flag,null,SUM ( ACT_LABOR_BRDN_COST),0) ACT_LABOR_BRDN_COST
7753        , decode(rate_dangling_flag,null,SUM ( ACT_EQUIPMENT_BRDN_COST),0) ACT_EQUIPMENT_BRDN_COST
7754        , decode(rate_dangling_flag,null,SUM ( ACT_BRDN_COST),0) ACT_BRDN_COST
7755        , decode(rate_dangling_flag,null,SUM ( ACT_RAW_COST),0) ACT_RAW_COST
7756        , decode(rate_dangling_flag,null,SUM ( ACT_REVENUE),0) ACT_REVENUE
7757        , decode(rate_dangling_flag,null,SUM ( ACT_LABOR_RAW_COST),0) ACT_LABOR_RAW_COST
7758        , decode(rate_dangling_flag,null,SUM ( ACT_EQUIPMENT_RAW_COST),0) ACT_EQUIPMENT_RAW_COST
7759        , decode(rate_dangling_flag,null,SUM ( ETC_LABOR_HRS),0) ETC_LABOR_HRS
7760        , decode(rate_dangling_flag,null,SUM ( ETC_EQUIP_HOURS),0) ETC_EQUIP_HOURS
7761        , decode(rate_dangling_flag,null,SUM ( ETC_LABOR_BRDN_COST),0) ETC_LABOR_BRDN_COST
7762        , decode(rate_dangling_flag,null,SUM ( ETC_EQUIP_BRDN_COST),0) ETC_EQUIP_BRDN_COST
7763        , decode(rate_dangling_flag,null,SUM ( ETC_BRDN_COST),0) ETC_BRDN_COST
7764        , decode(rate_dangling_flag,null,SUM ( ETC_RAW_COST),0) ETC_RAW_COST
7765        , decode(rate_dangling_flag,null,SUM ( ETC_LABOR_raw_COST),0) ETC_LABOR_raw_COST
7766        , decode(rate_dangling_flag,null,SUM ( ETC_EQUIP_raw_COST),0) ETC_EQUIP_raw_COST
7767        , decode(rate_dangling_flag,null,SUM(CUSTOM1),0) CUSTOM1
7768        , decode(rate_dangling_flag,null,SUM(CUSTOM2),0) CUSTOM2
7769        , decode(rate_dangling_flag,null,SUM(CUSTOM3),0) CUSTOM3
7770        , decode(rate_dangling_flag,null,SUM(CUSTOM4),0) CUSTOM4
7771        , decode(rate_dangling_flag,null,SUM(CUSTOM5),0) CUSTOM5
7772        , decode(rate_dangling_flag,null,SUM(CUSTOM6),0) CUSTOM6
7773        , decode(rate_dangling_flag,null,SUM(CUSTOM7),0) CUSTOM7
7774        , decode(rate_dangling_flag,null,SUM(CUSTOM8),0) CUSTOM8
7775        , decode(rate_dangling_flag,null,SUM(CUSTOM9),0) CUSTOM9
7776        , decode(rate_dangling_flag,null,SUM(CUSTOM10),0) CUSTOM10
7777        , decode(rate_dangling_flag,null,SUM(CUSTOM11),0) CUSTOM11
7778        , decode(rate_dangling_flag,null,SUM(CUSTOM12),0) CUSTOM12
7779        , decode(rate_dangling_flag,null,SUM(CUSTOM13),0) CUSTOM13
7780        , decode(rate_dangling_flag,null,SUM(CUSTOM14),0) CUSTOM14
7781        , decode(rate_dangling_flag,null,SUM(CUSTOM15),0) CUSTOM15
7782        , LINE_TYPE
7783        , rate_dangling_flag
7784        , time_dangling_flag
7785        , start_date
7786        , end_date
7787        , g_default_prg_level prg_level
7788        ,plan_type_code
7789  FROM
7790        (   SELECT
7791          g_worker_id WORKER_ID
7792        , plr.PROJECT_ID
7793        , plr.PROJECT_ORG_ID
7794        , plr.project_ORGANIZATION_ID
7795        , plr.WBS_ELEMENT_ID
7796        , plr.time_id
7797        , plr.period_type_id -- period type id...
7798        , plr.CALENDAR_TYPE
7799        , g_lowest_level RBS_AGGR_LEVEL
7800        , 'N' WBS_ROLLUP_FLAG
7801        , 'N' PRG_ROLLUP_FLAG
7802        , plr.CURR_RECORD_TYPE
7803        , plr.currency_code
7804        , plr.RBS_ELEMENT_ID
7805        , plr.RBS_STRUCT_VERSION_ID
7806        , plr.plan_version_id
7807        , plr.plan_type_id
7808        , SUM(plr.RAW_COST)  RAW_COST
7809        , SUM(plr.BRDN_COST)  BRDN_COST
7810        , SUM(plr.REVENUE)  REVENUE
7811        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )  BILL_RAW_COST
7812        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )   BILL_BRDN_COST
7813        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_people_resclass_code, plr.raw_cost, 0 ) )  BILL_LABOR_RAW_COST
7814        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_people_resclass_code , plr.BRDN_COST, 0 ) )   BILL_LABOR_BRDN_COST
7815        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'Y' || g_people_resclass_code, plr.quantity, 0 ) )  BILL_LABOR_HRS */ -- bug 6039785
7816        , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'Y' || g_people_resclass_code,
7817                                                                   DECODE ( plr.billable_flag , 'Y' , plr.quantity, 0 ) , 0 ) )  BILL_LABOR_HRS -- bug 6039785
7818        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code , plr.raw_cost, 0 ) )  EQUIPMENT_RAW_COST
7819        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code , plr.BRDN_COST, 0 ) )   EQUIPMENT_BRDN_COST
7820        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )      CAPITALIZABLE_RAW_COST
7821        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )      CAPITALIZABLE_BRDN_COST
7822        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.raw_cost, 0 ) )  LABOR_RAW_COST
7823        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.BRDN_COST, 0 ) )   LABOR_BRDN_COST
7824        /* , SUM ( DECODE ( plr.resource_class, g_people_resclass_code,
7825                                             DECODE ( plr.wp_flag, 'N',
7826                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
7827                                                                    plr.quantity),
7828                                                     0 ) )   labor_hrs */ -- bug 6039785
7829        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code,
7830                                             DECODE(plr.billable_flag, 'Y' ,
7831 					           DECODE ( plr.wp_flag, 'N',
7832                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
7833                                                                    plr.quantity),
7834 						    0 ),
7835                                                     0 ) )   labor_hrs -- bug 6039785
7836        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.revenue, 0 ) )  LABOR_REVENUE
7837       /* , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code,
7838                                             DECODE ( plr.wp_flag, 'N',
7839                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
7840                                                                    plr.quantity),
7841 
7842                                             0 ) )  EQUIPMENT_HOURS */ -- bug 6039785
7843       , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code,
7844                                             DECODE(plr.billable_flag, 'Y' ,
7845                                                    DECODE ( plr.wp_flag, 'N',
7846                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
7847                                                                    plr.quantity),
7848 					 	    0 ),
7849                                                     0 ) )  EQUIPMENT_HOURS -- bug 6039785
7850        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_equip_resclass_code, plr.quantity, 0 ) )  BILLABLE_EQUIPMENT_HOURS */ -- bug 6039785
7851        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_equip_resclass_code,
7852                                                                  DECODE ( plr.billable_flag , 'Y' , plr.quantity, 0 ) , 0 ) )  BILLABLE_EQUIPMENT_HOURS -- bug 6039785
7853        , TO_NUMBER(NULL)  SUP_INV_COMMITTED_COST
7854        , TO_NUMBER(NULL)  PO_COMMITTED_COST
7855        , TO_NUMBER(NULL)  PR_COMMITTED_COST
7856        , TO_NUMBER(NULL)  OTH_COMMITTED_COST
7857        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
7858                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
7859                                                                                 0 ) ) ) ACT_LABOR_HRS */ -- bug 6039785
7860        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
7861                                                                                 DECODE (plr.billable_flag ,'Y',
7862                                                                                         DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
7863 										0 ),
7864                                                                                 0 ) ) ) ACT_LABOR_HRS -- bug 6039785
7865        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
7866                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
7867                                                                                 0 ) ) ) ACT_EQUIP_HOURS */ -- bug 6039785
7868        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
7869                                                                                 DECODE (plr.billable_flag ,'Y',
7870                                                                                         DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
7871 										0 ),
7872                                                                                 0 ) ) ) ACT_EQUIP_HOURS -- bug 6039785
7873        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.actual_BRDN_COST, 0 ) ) ) ACT_LABOR_BRDN_COST
7874        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.actual_BRDN_COST, 0 ) ) ) ACT_EQUIPMENT_BRDN_COST
7875        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_brdn_cost ) ) ACT_BRDN_COST
7876        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_raw_cost ) ) ACT_RAW_COST
7877        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_revenue ) ) ACT_REVENUE
7878        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.actual_RAW_COST, 0 ) ) ) ACT_LABOR_RAW_COST
7879        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.actual_RAW_COST, 0 ) ) ) ACT_EQUIPMENT_RAW_COST
7880        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
7881                                                                                 DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
7882                                                                                 0 ) ) ) ETC_LABOR_HRS */ -- bug 6039785
7883        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
7884                                                                                 DECODE (plr.billable_flag ,'Y',
7885                                                                                         DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
7886                                                                                 0 ),
7887                                                                                 0 ) ) ) ETC_LABOR_HRS -- bug 6039785
7888        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
7889                                                                                 DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
7890                                                                                 0 ) ) ) ETC_EQUIP_HOURS */ -- bug 6039785
7891        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
7892                                                                                 DECODE (plr.billable_flag ,'Y',
7893                                                                                         DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
7894                                                                                 0 ),
7895                                                                                 0 ) ) ) ETC_EQUIP_HOURS -- bug 6039785
7896        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.etc_BRDN_COST, 0 ) ) ) ETC_LABOR_BRDN_COST
7897        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.etc_BRDN_COST, 0 ) ) ) ETC_EQUIP_BRDN_COST
7898        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.etc_brdn_cost ) ) ETC_BRDN_COST
7899        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.etc_raw_cost ) ) ETC_RAW_COST
7900        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.etc_raw_COST, 0 ) ) ) ETC_LABOR_raw_COST
7901        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.etc_raw_COST, 0 ) ) ) ETC_EQUIP_raw_COST
7902        , TO_NUMBER(NULL) CUSTOM1
7903        , TO_NUMBER(NULL) CUSTOM2
7904        , TO_NUMBER(NULL) CUSTOM3
7905        , TO_NUMBER(NULL) CUSTOM4
7906        , TO_NUMBER(NULL) CUSTOM5
7907        , TO_NUMBER(NULL) CUSTOM6
7908        , TO_NUMBER(NULL) CUSTOM7
7909        , TO_NUMBER(NULL) CUSTOM8
7910        , TO_NUMBER(NULL) CUSTOM9
7911        , TO_NUMBER(NULL) CUSTOM10
7912        , TO_NUMBER(NULL) CUSTOM11
7913        , TO_NUMBER(NULL) CUSTOM12
7914        , TO_NUMBER(NULL) CUSTOM13
7915        , TO_NUMBER(NULL) CUSTOM14
7916        , TO_NUMBER(NULL) CUSTOM15
7917        , plr.LINE_TYPE
7918        , decode(sign(plr.rate),-1,'Y',NULL) ||decode(sign(plr.rate2),-1,'Y',NULL) rate_dangling_flag
7919        , NULL time_dangling_flag
7920        , plr.start_date
7921   	 , plr.end_date
7922        , g_default_prg_level  prg_level
7923        , plr.plan_type_code PLAN_TYPE_CODE /*4471527 */
7924        FROM
7925        (          ----- First inline view plr .............
7926             select
7927               collapse_bl.PROJECT_ID      -- , 1 partition_id
7928             , collapse_bl.WBS_ELEMENT_ID  -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
7929             , SUM(collapse_bl.CURR_RECORD_TYPE_ID) CURR_RECORD_TYPE
7930             , collapse_bl.RBS_ELEMENT_ID
7931             , collapse_bl.RBS_STRUCT_VERSION_ID
7932 		    , collapse_bl.plan_type_id
7933             , collapse_bl.plan_version_id
7934 			, collapse_bl.time_id
7935             , collapse_bl.PROJECT_ORGANIZATION_ID
7936             , collapse_bl.PROJECT_ORG_ID
7937             , collapse_bl.RESOURCE_ASSIGNMENT_ID
7938             , collapse_bl.BILLABLE_FLAG
7939             , collapse_bl.RESOURCE_CLASS  -- , CALENDAR_TYPE -- , CALENDAR_ID
7940             , collapse_bl.TIME_PHASED_TYPE_CODE
7941             , collapse_bl.CURRENCY_CODE
7942             , max(collapse_bl.raw_cost) raw_cost
7943             , max(collapse_bl.BRDN_COST) BRDN_COST
7944             , max(collapse_bl.revenue) revenue
7945             , max(collapse_bl.actual_raw_cost) actual_raw_cost
7946             , max(collapse_bl.actual_BRDN_COST) actual_BRDN_COST
7947             , max(collapse_bl.actual_revenue) actual_revenue
7948             , max(collapse_bl.etc_raw_cost) etc_raw_cost
7949             , max(collapse_bl.etc_BRDN_COST) etc_BRDN_COST
7950             , max(collapse_bl.etc_revenue) etc_revenue
7951             , max(collapse_bl.quantity) quantity
7952             , max(collapse_bl.actual_quantity) actual_quantity
7953             , max(collapse_bl.etc_quantity) etc_quantity
7954             , collapse_bl.start_date start_date
7955             , collapse_bl.end_date   end_date
7956             -- , collapse_bl.period_name period_name  -- , TRACK_AS_LABOR_FLAG track_as_labor_flag
7957 			, collapse_bl.line_type
7958 			, collapse_bl.calendar_type
7959 			, collapse_bl.period_type_id
7960             , collapse_bl.row_id
7961             ,collapse_bl.rate rate
7962             ,collapse_bl.rate2 rate2
7963             , collapse_bl.plan_type_code
7964             , collapse_bl.WP_FLAG
7965             from
7966               (                  ----- Second inline view 'collapse_bl' begin .............
7967                select
7968                   spread_bl.row_id row_id
7969                 , spread_bl.PROJECT_ID
7970                 , spread_bl.BUDGET_VERSION_ID plan_version_id
7971 				, spread_bl.time_id
7972                 , spread_bl.RESOURCE_ASSIGNMENT_ID
7973                 , spread_bl.WBS_ELEMENT_ID
7974                 , spread_bl.RBS_ELEMENT_ID
7975                 , spread_bl.WBS_STRUCT_VERSION_ID
7976                 , spread_bl.RBS_STRUCT_VERSION_ID
7977   	 	        , spread_bl.plan_type_id
7978                 , spread_bl.BILLABLE_FLAG
7979                 , spread_bl.RESOURCE_CLASS
7980                 , spread_bl.PROJECT_ORGANIZATION_ID
7981                 , spread_bl.PROJECT_ORG_ID
7982                 , spread_bl.TIME_PHASED_TYPE_CODE
7983                 , DECODE( invert.INVERT_ID
7984                         , 1, spread_bl.glb1_CURRENCY_CODE
7985                         , 2, spread_bl.glb2_CURRENCY_CODE
7986                         , 4, spread_bl.func_CURRENCY_CODE
7987                         , 8, spread_bl.PRJ_CURRENCY_CODE
7988                         , 16, spread_bl.TXN_CURRENCY_CODE ) CURRENCY_CODE
7989                 , invert.INVERT_ID CURR_RECORD_TYPE_ID
7990                 , DECODE ( invert.INVERT_ID
7991                          , 1, spread_bl.glb1_raw_cost
7992                          , 2, spread_bl.glb2_raw_cost
7993                          , 4, spread_bl.func_raw_cost
7994                          , 8, spread_bl.PRJ_raw_cost
7995                          , 16, spread_bl.TXN_raw_cost) raw_cost
7996                 , DECODE ( invert.INVERT_ID
7997                          , 1, spread_bl.glb1_brdn_cost
7998                          , 2, spread_bl.glb2_brdn_cost
7999                          , 4, spread_bl.func_BRDN_COST
8000                          , 8, spread_bl.PRJ_BRDN_COST
8001                          , 16, spread_bl.TXN_BRDN_COST ) BRDN_COST
8002                 , DECODE ( invert.INVERT_ID
8003                          , 1, spread_bl.glb1_revenue
8004                          , 2, spread_bl.glb2_revenue
8005                          , 4, spread_bl.func_revenue
8006                          , 8, spread_bl.PRJ_revenue
8007                          , 16, spread_bl.TXN_revenue ) revenue
8008 				, DECODE ( invert.INVERT_ID
8009                          , 1, spread_bl.glb1_actual_raw_cost
8010                          , 2, spread_bl.glb2_actual_raw_cost
8011 				         , 4, spread_bl.func_actual_raw_cost
8012 						 , 8, spread_bl.prj_actual_raw_cost
8013 						 , 16, spread_bl.txn_actual_raw_cost ) actual_raw_cost
8014 				, DECODE ( invert.INVERT_ID
8015 				         , 1, spread_bl.glb1_actual_brdn_cost
8016 						 , 2, spread_bl.glb2_actual_brdn_cost
8017 				         , 4, spread_bl.func_actual_brdn_cost
8018 						 , 8, spread_bl.prj_actual_brdn_cost
8019 						 , 16, spread_bl.txn_actual_brdn_cost ) actual_brdn_cost
8020 				, DECODE ( invert.INVERT_ID
8021 				         , 1, spread_bl.glb1_actual_revenue
8022 						 , 2, spread_bl.glb2_actual_revenue
8023 				         , 4, spread_bl.func_actual_revenue
8024 						 , 8, spread_bl.prj_actual_revenue
8025 						 , 16, spread_bl.txn_actual_revenue ) actual_revenue
8026 				, DECODE ( invert.INVERT_ID
8027 				         , 1, spread_bl.glb1_etc_raw_cost
8028 						 , 2, spread_bl.glb2_etc_raw_cost
8029 				         , 4, spread_bl.func_etc_raw_cost
8030 						 , 8, spread_bl.prj_etc_raw_cost
8031 						 , 16, spread_bl.txn_etc_raw_cost ) etc_raw_cost
8032 				, DECODE ( invert.INVERT_ID
8033 				         , 1, spread_bl.glb1_etc_brdn_cost
8034 						 , 2, spread_bl.glb2_etc_brdn_cost
8035 				         , 4, spread_bl.func_etc_brdn_cost
8036 						 , 8, spread_bl.prj_etc_brdn_cost
8037 						 , 16, spread_bl.txn_etc_brdn_cost ) etc_brdn_cost
8038 				, DECODE ( invert.INVERT_ID
8039 				         , 1, spread_bl.glb1_etc_revenue
8040 						 , 2, spread_bl.glb2_etc_revenue
8041 				         , 4, spread_bl.func_etc_revenue
8042 						 , 8, spread_bl.prj_etc_revenue
8043 						 , 16, spread_bl.txn_etc_revenue ) etc_revenue
8044             	, spread_bl.quantity quantity
8045             	, spread_bl.actual_quantity actual_quantity
8046             	, spread_bl.etc_quantity etc_quantity
8047             	, spread_bl.start_date start_date
8048             	, spread_bl.end_date   end_date
8049             	, spread_bl.line_type line_type
8050 				, spread_bl.period_type_id
8051 				, spread_bl.calendar_type
8052  		,decode(invert.invert_id,1,spread_bl.rate,1) rate
8053  		,decode(invert.invert_id,2,spread_bl.rate2,1) rate2
8054                              , spread_bl.plan_type_code
8055                 , spread_bl.WP_FLAG
8056                 from
8057                   (     ----- Third inline view 'spread_bl'  .............
8058                     SELECT /*+ ordered */
8059                 	  ra.project_id
8060                 	, bl.rowid row_id
8061                 	, ra.budget_version_id
8062 					, prd.cal_period_id time_id
8063                 	, bl.resource_asSIGNment_id
8064                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
8065                 	, NVL(map.element_id, -1)              rbs_element_id  -- !! changed.
8066                 	, ver.wbs_struct_version_id      wbs_struct_version_id
8067                 	, NVL(ver.rbs_struct_version_id, -1)    rbs_struct_version_id  -- !! changed.
8068 			, ver.plan_type_id               plan_type_id
8069 			, ra.rate_based_flag             billable_flag
8070 			, ra.resource_class_code         resource_class
8071 			, DECODE(ver.time_phased_type_code
8072                          , g_pa_cal_str, 32
8073                          , g_gl_cal_str, 32
8074                          , 'N', 2048
8075                          , -1) period_type_id
8076 			   , DECODE(ver.time_phased_type_code
8077                             , g_pa_cal_str, g_pa_cal_str
8078                             , g_gl_cal_str, g_gl_cal_str
8079                             , 'N', g_all
8080                             , 'X')  CALENDAR_TYPE
8081                 	, bl.txn_currency_code           txn_currency_code
8082                 	, bl.txn_raw_cost                txn_raw_cost
8083                 	, bl.txn_burdened_COST           txn_brdn_COST
8084                 	, bl.txn_revenue                 txn_revenue
8085 			, DECODE(ver.wp_flag, 'N', bl.txn_init_raw_cost, TO_NUMBER(NULL))                txn_actual_raw_cost  -- new
8086 			, DECODE(ver.wp_flag, 'N', bl.txn_init_burdened_cost, TO_NUMBER(NULL))             txn_actual_brdn_cost  -- new
8087 			, DECODE(ver.wp_flag, 'N', bl.txn_init_revenue, TO_NUMBER(NULL))                   txn_actual_revenue  -- new
8088 			, DECODE(ver.wp_flag, 'N', (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0)), TO_NUMBER(NULL)) txn_etc_raw_cost     -- new
8089 			, DECODE(ver.wp_flag, 'N', (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0)), TO_NUMBER(NULL)) txn_etc_brdn_cost     -- new
8090 			, DECODE(ver.wp_flag, 'N', (bl.txn_revenue - NVL(bl.txn_init_revenue, 0)), TO_NUMBER(NULL)) txn_etc_revenue     -- new
8091                 	, bl.project_currency_code          prj_currency_code
8092                 	, bl.project_raw_cost               prj_raw_cost
8093                 	, bl.project_burdened_COST          prj_BRDN_COST
8094                 	, bl.project_revenue                prj_revenue
8095 			, DECODE(ver.wp_flag, 'N', bl.project_init_raw_cost, TO_NUMBER(NULL))          prj_actual_raw_cost  -- new
8096 			, DECODE(ver.wp_flag, 'N', bl.project_init_burdened_cost, TO_NUMBER(NULL))     prj_actual_brdn_cost  -- new
8097 			, DECODE(ver.wp_flag, 'N', bl.project_init_revenue, TO_NUMBER(NULL))           prj_actual_revenue  -- new
8098 			, DECODE(ver.wp_flag, 'N', (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0)), TO_NUMBER(NULL)) prj_etc_raw_cost     -- new
8099 			, DECODE(ver.wp_flag, 'N', (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0)), TO_NUMBER(NULL)) prj_etc_brdn_cost     -- new
8100 			, DECODE(ver.wp_flag, 'N', (bl.project_revenue - NVL(bl.project_init_revenue, 0)), TO_NUMBER(NULL)) prj_etc_revenue     -- new
8101                 	, bl.projfunc_currency_code         func_currency_code
8102                   , bl.raw_cost                       func_raw_cost
8103                 	, bl.burdened_COST                  func_BRDN_COST
8104                 	, bl.revenue                        func_revenue
8105 			, DECODE(ver.wp_flag, 'N', bl.init_raw_cost , TO_NUMBER(NULL))                 func_actual_raw_cost  -- new
8106 			, DECODE(ver.wp_flag, 'N', bl.init_burdened_cost , TO_NUMBER(NULL))            func_actual_brdn_cost  -- new
8107 			, DECODE(ver.wp_flag, 'N', bl.init_revenue , TO_NUMBER(NULL))                  func_actual_revenue  -- new
8108 			, DECODE(ver.wp_flag, 'N', (bl.raw_cost - NVL(bl.init_raw_cost, 0)), TO_NUMBER(NULL)) func_etc_raw_cost     -- new
8109 			, DECODE(ver.wp_flag, 'N', (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)), TO_NUMBER(NULL)) func_etc_brdn_cost     -- new
8110 			, DECODE(ver.wp_flag, 'N', (bl.revenue - NVL(bl.init_revenue, 0)), TO_NUMBER(NULL)) func_etc_revenue     -- new
8111                 	, g_global1_currency_code  glb1_currency_code -- g_global1_currency_code        glb1_currency_code
8112                   , DECODE (g_global1_currency_code
8113 				          , bl.txn_currency_code
8114 						  , bl.txn_raw_cost
8115 						  , bl.project_currency_code
8116 						  , bl.project_raw_cost
8117 						  , bl.projfunc_currency_code
8118 						  , bl.raw_cost
8119 						  , ROUND((rates.rate * raw_cost )/rates.mau)*rates.mau
8120 					) glb1_raw_cost
8121                   , DECODE (g_global1_currency_code
8122 				          , bl.txn_currency_code
8123 						  , bl.txn_burdened_cost
8124 						  , bl.project_currency_code
8125 						  , bl.project_burdened_cost
8126 						  , bl.projfunc_currency_code
8127 						  , bl.burdened_cost
8128 						  , ROUND((rates.rate * burdened_cost )/rates.mau)*rates.mau
8129 					) glb1_BRDN_COST
8130                   , DECODE (g_global1_currency_code
8131 				          , bl.txn_currency_code
8132 						  , bl.txn_revenue
8133 						  , bl.project_currency_code
8134 						  , bl.project_revenue
8135 						  , bl.projfunc_currency_code
8136 						  , bl.revenue
8137 						  , ROUND((rates.rate * revenue )/rates.mau)*rates.mau
8138 					)  glb1_revenue
8139 			      , DECODE(ver.wp_flag
8140 			             , 'N'
8141 						 , DECODE (g_global1_currency_code
8142 				                 , bl.txn_currency_code
8143 						         , bl.txn_init_raw_cost
8144 						         , bl.project_currency_code
8145 						         , bl.project_init_raw_cost
8146 						         , bl.projfunc_currency_code
8147 						         , bl.init_raw_cost
8148 						         , ROUND((rates.rate * bl.init_raw_cost )/rates.mau)*rates.mau
8149 					              )
8150 						 , TO_NUMBER(NULL))                 glb1_actual_raw_cost
8151    			      , DECODE(ver.wp_flag
8152 				         , 'N'
8153 						 , DECODE (g_global1_currency_code
8154 				                 , bl.txn_currency_code
8155 						         , bl.txn_init_burdened_cost
8156 						         , bl.project_currency_code
8157 						         , bl.project_init_burdened_cost
8158 						         , bl.projfunc_currency_code
8159 						         , bl.init_burdened_cost
8160 						         , ROUND((rates.rate * bl.init_burdened_cost )/rates.mau)*rates.mau
8161 					              )
8162 						 , TO_NUMBER(NULL))            glb1_actual_brdn_cost
8163 			      , DECODE(ver.wp_flag
8164 				         , 'N'
8165 						 , DECODE (g_global1_currency_code
8166 				                 , bl.txn_currency_code
8167 						         , bl.txn_init_revenue
8168 						         , bl.project_currency_code
8169 						         , bl.project_init_revenue
8170 						         , bl.projfunc_currency_code
8171 						         , bl.init_revenue
8172 						         , ROUND((rates.rate * bl.init_revenue )/rates.mau)*rates.mau
8173 					              )
8174 						 , TO_NUMBER(NULL))                  glb1_actual_revenue
8175 				  , DECODE(ver.wp_flag
8176 				         , 'N'
8177 						 , DECODE (g_global1_currency_code
8178 				                 , bl.txn_currency_code
8179 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
8180 						         , bl.project_currency_code
8181 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
8182 						         , bl.projfunc_currency_code
8183 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
8184 						         , ROUND((rates.rate * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau)*rates.mau
8185 					              )
8186 						 , TO_NUMBER(NULL))                glb1_etc_raw_cost
8187 				  , DECODE(ver.wp_flag
8188 				         , 'N'
8189 						 , DECODE (g_global1_currency_code
8190 				                 , bl.txn_currency_code
8191 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
8192 						         , bl.project_currency_code
8193 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
8194 						         , bl.projfunc_currency_code
8195 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
8196 						         , ROUND((rates.rate * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau)*rates.mau
8197 					              )
8198 						 , TO_NUMBER(NULL))    glb1_etc_brdn_cost
8199 				  , DECODE(ver.wp_flag
8200 				         , 'N'
8201 						 , DECODE (g_global1_currency_code
8202 				                 , bl.txn_currency_code
8203 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
8204 						         , bl.project_currency_code
8205 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
8206 						         , bl.projfunc_currency_code
8207 						         , (bl.revenue - NVL(bl.init_revenue, 0))
8208 						         , ROUND((rates.rate * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau)*rates.mau
8209 					              )
8210 						 , TO_NUMBER(NULL))    glb1_etc_revenue
8211                 	, g_global2_currency_code  glb2_currency_code -- g_global2_currency_code        glb2_currency_code
8212                   , DECODE (g_global2_currency_code
8213 				          , bl.txn_currency_code
8214 						  , bl.txn_raw_cost
8215 						  , bl.project_currency_code
8216 						  , bl.project_raw_cost
8217 						  , bl.projfunc_currency_code
8218 						  , bl.raw_cost
8219 						  , ROUND((rates.rate2 * raw_cost )/rates.mau2)*rates.mau2
8220 					) glb2_raw_cost
8221                   , DECODE (g_global2_currency_code
8222 				          , bl.txn_currency_code
8223 						  , bl.txn_burdened_cost
8224 						  , bl.project_currency_code
8225 						  , bl.project_burdened_cost
8226 						  , bl.projfunc_currency_code
8227 						  , bl.burdened_cost
8228 						  , ROUND((rates.rate2 * burdened_cost )/rates.mau2)*rates.mau2
8229 					) glb2_BRDN_COST
8230                   , DECODE (g_global2_currency_code
8231 				          , bl.txn_currency_code
8232 						  , bl.txn_revenue
8233 						  , bl.project_currency_code
8234 						  , bl.project_revenue
8235 						  , bl.projfunc_currency_code
8236 						  , bl.revenue
8237 						  , ROUND((rates.rate2 * revenue )/rates.mau2)*rates.mau2
8238 					)  glb2_revenue
8239 			      , DECODE(ver.wp_flag
8240 			             , 'N'
8241 						 , DECODE (g_global2_currency_code
8242 				                 , bl.txn_currency_code
8243 						         , bl.txn_init_raw_cost
8244 						         , bl.project_currency_code
8245 						         , bl.project_init_raw_cost
8246 						         , bl.projfunc_currency_code
8247 						         , bl.init_raw_cost
8248 						         , ROUND((rates.rate2 * bl.init_raw_cost )/rates.mau2)*rates.mau2
8249 					              )
8250 						 , TO_NUMBER(NULL))                 glb2_actual_raw_cost
8251    			      , DECODE(ver.wp_flag
8252 				         , 'N'
8253 						 , DECODE (g_global2_currency_code
8254 				                 , bl.txn_currency_code
8255 						         , bl.txn_init_burdened_cost
8256 						         , bl.project_currency_code
8257 						         , bl.project_init_burdened_cost
8258 						         , bl.projfunc_currency_code
8259 						         , bl.init_burdened_cost
8260 						         , ROUND((rates.rate2 * bl.init_burdened_cost )/rates.mau2)*rates.mau2
8261 					              )
8262 						 , TO_NUMBER(NULL))            glb2_actual_brdn_cost
8263 			      , DECODE(ver.wp_flag
8264 				         , 'N'
8265 						 , DECODE (g_global2_currency_code
8266 				                 , bl.txn_currency_code
8267 						         , bl.txn_init_revenue
8268 						         , bl.project_currency_code
8269 						         , bl.project_init_revenue
8270 						         , bl.projfunc_currency_code
8271 						         , bl.init_revenue
8272 						         , ROUND((rates.rate2 * bl.init_revenue )/rates.mau2)*rates.mau2
8273 					              )
8274 						 , TO_NUMBER(NULL))                  glb2_actual_revenue
8275 				  , DECODE(ver.wp_flag
8276 				         , 'N'
8277 						 , DECODE (g_global2_currency_code
8278 				                 , bl.txn_currency_code
8279 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
8280 						         , bl.project_currency_code
8281 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
8282 						         , bl.projfunc_currency_code
8283 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
8284 						         , ROUND((rates.rate2 * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau2)*rates.mau2
8285 					              )
8286 						 , TO_NUMBER(NULL))                glb2_etc_raw_cost
8287 				  , DECODE(ver.wp_flag
8288 				         , 'N'
8289 						 , DECODE (g_global2_currency_code
8290 				                 , bl.txn_currency_code
8291 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
8292 						         , bl.project_currency_code
8293 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
8294 						         , bl.projfunc_currency_code
8295 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
8296 						         , ROUND((rates.rate2 * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau2)*rates.mau2
8297 					              )
8298 						 , TO_NUMBER(NULL))    glb2_etc_brdn_cost
8299 				  , DECODE(ver.wp_flag
8300 				         , 'N'
8301 						 , DECODE (g_global2_currency_code
8302 				                 , bl.txn_currency_code
8303 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
8304 						         , bl.project_currency_code
8305 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
8306 						         , bl.projfunc_currency_code
8307 						         , (bl.revenue - NVL(bl.init_revenue, 0))
8308 						         , ROUND((rates.rate2 * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau2)*rates.mau2
8309 					              )
8310 						 , TO_NUMBER(NULL))    glb2_etc_revenue
8311                                      , bl.quantity                    quantity
8312 			                   , DECODE(ver.wp_flag, 'N', bl.init_quantity, TO_NUMBER(NULL))                  actual_quantity  -- new
8313 			                   , DECODE(ver.wp_flag, 'N', (bl.quantity - NVL(bl.init_quantity, 0)), TO_NUMBER(NULL)) etc_quantity  -- new
8314                 	                   , TO_DATE(NULL) START_DATE
8315                 	                   , TO_DATE(NULL) END_date
8316                 	                   , ver.time_phased_type_code time_phased_type_code
8317                 	                   , ppa.org_id project_org_id
8318                 	                   , ppa.carrying_out_organization_id project_organization_id
8319 					       , DECODE(ver.time_phased_type_code, g_pa_cal_str, 'OF', g_gl_cal_str, 'OF', 'N', 'NTP', 'X') line_type
8320  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate
8321  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate2
8322                                               ,ver.plan_type_code  plan_type_code /*4471527 */
8323                         , ver.WP_FLAG
8324 				 FROM
8325                   PJI_FM_EXTR_PLNVER4           ver
8326                 , pa_resource_asSIGNments       ra
8327                 , PA_BUDGET_LINES               bl
8328                 , pa_projects_all               ppa
8329                 , PJI_ORG_EXTR_INFO             oei
8330                 , pji_pjp_rbs_header            rhdr
8331                 , pji_time_cal_period_v         prd
8332                 , PJI_FM_AGGR_DLY_RATES         rates
8333                 , pji_fp_txn_accum_header       hdr
8334                 , pa_rbs_txn_accum_map          map
8335                 , PA_PROJ_ELEM_VER_STRUCTURE    pevs
8336                  WHERE 1=1
8337 				     AND ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
8338                              AND ra.project_id = ver.PROJECT_ID
8339                              AND ra.budget_version_id = ver.plan_version_id
8340                              AND ver.project_id = ppa.project_id
8341                              AND txn_currency_code IS NOT NULL
8342                              AND bl.project_currency_code IS NOT NULL
8343                              AND bl.projfunc_currency_code IS NOT NULL
8344 			                 AND pevs.element_version_id = ver.wbs_struct_version_id
8345                              AND ver.project_id = pevs.project_id
8346 	 		         AND ver.secondary_rbs_flag = 'Y'
8347 					 AND ver.wp_flag = 'N'
8348 					 AND ver.baselined_flag = 'Y'
8349 					 AND oei.org_id = ppa.org_id
8350 					 AND ver.time_phased_type_code IN (g_pa_cal_str, g_gl_cal_str)
8351 					 AND DECODE ( ver.time_phased_type_code
8352 					            , g_pa_cal_str, oei.pa_calendar_id
8353 					 			, g_gl_cal_str, oei.gl_calendar_id) = prd.calendar_id
8354 					 AND bl.period_name = prd.name
8355 					 AND rates.time_id = prd.cal_period_id
8356                       AND rates.worker_id = g_worker_id
8357 					 AND rates.pf_currency_code = bl.projfunc_currency_code -- 4764334
8358 					  AND ra.txn_accum_header_id = hdr.txn_accum_header_id
8359 					  AND ra.txn_accum_header_id = map.txn_accum_header_id
8360 					  AND map.struct_version_id = rhdr.rbs_version_id
8361 					  AND ra.budget_version_id = rhdr.plan_version_id
8362                              AND rhdr.project_id = ver.project_id
8363 					  AND ppa.project_id = ra.project_id
8364                                 AND ver.rbs_struct_version_id = rhdr.rbs_version_id
8365  					  AND ver.worker_id = g_worker_id
8366              UNION ALL
8367              SELECT /*+ ordered */
8368                 	  ra.project_id
8369                 	, bl.rowid row_id
8370                 	, ra.budget_version_id
8371 					, -1 time_id
8372                 	, bl.resource_asSIGNment_id
8373                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
8374                 	, NVL(map.element_id, -1)              rbs_element_id  -- !! changed.
8375                 	, ver.wbs_struct_version_id      wbs_struct_version_id
8376                 	, NVL(ver.rbs_struct_version_id, -1)    rbs_struct_version_id  -- !! changed.
8377 			, ver.plan_type_id               plan_type_id
8378 			, ra.rate_based_flag             billable_flag
8379 			, ra.resource_class_code         resource_class
8380 			, DECODE(ver.time_phased_type_code
8381                          , g_pa_cal_str, 32
8382                          , g_gl_cal_str, 32
8383                          , 'N', 2048
8384                          , -1) period_type_id
8385 		     , DECODE(ver.time_phased_type_code
8386                          , g_pa_cal_str, g_pa_cal_str
8387                          , g_gl_cal_str, g_gl_cal_str
8388                          , 'N', g_all
8389                          , 'X')  CALENDAR_TYPE
8390                 	, bl.txn_currency_code           txn_currency_code
8391                 	, bl.txn_raw_cost                txn_raw_cost
8392                 	, bl.txn_burdened_COST           txn_brdn_COST
8393                 	, bl.txn_revenue                 txn_revenue
8394 			, DECODE(ver.wp_flag, 'N', bl.txn_init_raw_cost, TO_NUMBER(NULL))                txn_actual_raw_cost  -- new
8395 			, DECODE(ver.wp_flag, 'N', bl.txn_init_burdened_cost, TO_NUMBER(NULL))             txn_actual_brdn_cost  -- new
8396 			, DECODE(ver.wp_flag, 'N', bl.txn_init_revenue, TO_NUMBER(NULL))                   txn_actual_revenue  -- new
8397 			, DECODE(ver.wp_flag, 'N', (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0)), TO_NUMBER(NULL)) txn_etc_raw_cost     -- new
8398 			, DECODE(ver.wp_flag, 'N', (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0)), TO_NUMBER(NULL)) txn_etc_brdn_cost     -- new
8399 			, DECODE(ver.wp_flag, 'N', (bl.txn_revenue - NVL(bl.txn_init_revenue, 0)), TO_NUMBER(NULL)) txn_etc_revenue     -- new
8400                 	, bl.project_currency_code          prj_currency_code
8401                 	, bl.project_raw_cost               prj_raw_cost
8402                 	, bl.project_burdened_COST          prj_BRDN_COST
8403                 	, bl.project_revenue                prj_revenue
8404 			, DECODE(ver.wp_flag, 'N', bl.project_init_raw_cost, TO_NUMBER(NULL))          prj_actual_raw_cost  -- new
8405 			, DECODE(ver.wp_flag, 'N', bl.project_init_burdened_cost, TO_NUMBER(NULL))     prj_actual_brdn_cost  -- new
8406 			, DECODE(ver.wp_flag, 'N', bl.project_init_revenue, TO_NUMBER(NULL))           prj_actual_revenue  -- new
8407 			, DECODE(ver.wp_flag, 'N', (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0)), TO_NUMBER(NULL)) prj_etc_raw_cost     -- new
8408 			, DECODE(ver.wp_flag, 'N', (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0)), TO_NUMBER(NULL)) prj_etc_brdn_cost     -- new
8409 			, DECODE(ver.wp_flag, 'N', (bl.project_revenue - NVL(bl.project_init_revenue, 0)), TO_NUMBER(NULL)) prj_etc_revenue     -- new
8410                 	, bl.projfunc_currency_code         func_currency_code
8411                   , bl.raw_cost                       func_raw_cost
8412                 	, bl.burdened_COST                  func_BRDN_COST
8413                 	, bl.revenue                        func_revenue
8414 			, DECODE(ver.wp_flag, 'N', bl.init_raw_cost , TO_NUMBER(NULL))                 func_actual_raw_cost  -- new
8415 			, DECODE(ver.wp_flag, 'N', bl.init_burdened_cost , TO_NUMBER(NULL))            func_actual_brdn_cost  -- new
8416 			, DECODE(ver.wp_flag, 'N', bl.init_revenue , TO_NUMBER(NULL))                  func_actual_revenue  -- new
8417 			, DECODE(ver.wp_flag, 'N', (bl.raw_cost - NVL(bl.init_raw_cost, 0)), TO_NUMBER(NULL)) func_etc_raw_cost     -- new
8418 			, DECODE(ver.wp_flag, 'N', (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)), TO_NUMBER(NULL)) func_etc_brdn_cost     -- new
8419 			, DECODE(ver.wp_flag, 'N', (bl.revenue - NVL(bl.init_revenue, 0)), TO_NUMBER(NULL)) func_etc_revenue     -- new
8420                 	, g_global1_currency_code   glb1_currency_code -- g_global1_currency_code         glb1_currency_code
8421                   , DECODE (g_global1_currency_code
8422 				          , bl.txn_currency_code
8423 						  , bl.txn_raw_cost
8424 						  , bl.project_currency_code
8425 						  , bl.project_raw_cost
8426 						  , bl.projfunc_currency_code
8427 						  , bl.raw_cost
8428 						  , ROUND((rates.rate * raw_cost )/rates.mau)*rates.mau
8429 					) glb1_raw_cost
8430                   , DECODE (g_global1_currency_code
8431 				          , bl.txn_currency_code
8432 						  , bl.txn_burdened_cost
8433 						  , bl.project_currency_code
8434 						  , bl.project_burdened_cost
8435 						  , bl.projfunc_currency_code
8436 						  , bl.burdened_cost
8437 						  , ROUND((rates.rate * burdened_cost )/rates.mau)*rates.mau
8438 					) glb1_BRDN_COST
8439                   , DECODE (g_global1_currency_code
8440 				          , bl.txn_currency_code
8441 						  , bl.txn_revenue
8442 						  , bl.project_currency_code
8443 						  , bl.project_revenue
8444 						  , bl.projfunc_currency_code
8445 						  , bl.revenue
8446 						  , ROUND((rates.rate * revenue )/rates.mau)*rates.mau
8447 					)  glb1_revenue
8448 			      , DECODE(ver.wp_flag
8449 			             , 'N'
8450 						 , DECODE (g_global1_currency_code
8451 				                 , bl.txn_currency_code
8452 						         , bl.txn_init_raw_cost
8453 						         , bl.project_currency_code
8454 						         , bl.project_init_raw_cost
8455 						         , bl.projfunc_currency_code
8456 						         , bl.init_raw_cost
8457 						         , ROUND((rates.rate * bl.init_raw_cost )/rates.mau)*rates.mau
8458 					              )
8459 						 , TO_NUMBER(NULL))                 glb1_actual_raw_cost
8460    			      , DECODE(ver.wp_flag
8461 				         , 'N'
8462 						 , DECODE (g_global1_currency_code
8463 				                 , bl.txn_currency_code
8464 						         , bl.txn_init_burdened_cost
8465 						         , bl.project_currency_code
8466 						         , bl.project_init_burdened_cost
8467 						         , bl.projfunc_currency_code
8468 						         , bl.init_burdened_cost
8469 						         , ROUND((rates.rate * bl.init_burdened_cost )/rates.mau)*rates.mau
8470 					              )
8471 						 , TO_NUMBER(NULL))            glb1_actual_brdn_cost
8472 			      , DECODE(ver.wp_flag
8473 				         , 'N'
8474 						 , DECODE (g_global1_currency_code
8475 				                 , bl.txn_currency_code
8476 						         , bl.txn_init_revenue
8477 						         , bl.project_currency_code
8478 						         , bl.project_init_revenue
8479 						         , bl.projfunc_currency_code
8480 						         , bl.init_revenue
8481 						         , ROUND((rates.rate * bl.init_revenue )/rates.mau)*rates.mau
8482 					              )
8483 						 , TO_NUMBER(NULL))                  glb1_actual_revenue
8484 				  , DECODE(ver.wp_flag
8485 				         , 'N'
8486 						 , DECODE (g_global1_currency_code
8487 				                 , bl.txn_currency_code
8488 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
8489 						         , bl.project_currency_code
8490 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
8491 						         , bl.projfunc_currency_code
8492 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
8493 						         , ROUND((rates.rate * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau)*rates.mau
8494 					              )
8495 						 , TO_NUMBER(NULL))                glb1_etc_raw_cost
8496 				  , DECODE(ver.wp_flag
8497 				         , 'N'
8498 						 , DECODE (g_global1_currency_code
8499 				                 , bl.txn_currency_code
8500 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
8501 						         , bl.project_currency_code
8502 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
8503 						         , bl.projfunc_currency_code
8504 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
8505 						         , ROUND((rates.rate * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau)*rates.mau
8506 					              )
8507 						 , TO_NUMBER(NULL))    glb1_etc_brdn_cost
8508 				  , DECODE(ver.wp_flag
8509 				         , 'N'
8510 						 , DECODE (g_global1_currency_code
8511 				                 , bl.txn_currency_code
8512 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
8513 						         , bl.project_currency_code
8514 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
8515 						         , bl.projfunc_currency_code
8516 						         , (bl.revenue - NVL(bl.init_revenue, 0))
8517 						         , ROUND((rates.rate * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau)*rates.mau
8518 					              )
8519 						 , TO_NUMBER(NULL))    glb1_etc_revenue
8520                 	, g_global2_currency_code   glb2_currency_code -- g_global2_currency_code         glb2_currency_code
8521                   , DECODE (g_global2_currency_code
8522 				          , bl.txn_currency_code
8523 						  , bl.txn_raw_cost
8524 						  , bl.project_currency_code
8525 						  , bl.project_raw_cost
8526 						  , bl.projfunc_currency_code
8527 						  , bl.raw_cost
8528 						  , ROUND((rates.rate2 * raw_cost )/rates.mau2)*rates.mau2
8529 					) glb2_raw_cost
8530                   , DECODE (g_global2_currency_code
8531 				          , bl.txn_currency_code
8532 						  , bl.txn_burdened_cost
8533 						  , bl.project_currency_code
8534 						  , bl.project_burdened_cost
8535 						  , bl.projfunc_currency_code
8536 						  , bl.burdened_cost
8537 						  , ROUND((rates.rate2 * burdened_cost )/rates.mau2)*rates.mau2
8538 					) glb2_BRDN_COST
8539                   , DECODE (g_global2_currency_code
8540 				          , bl.txn_currency_code
8541 						  , bl.txn_revenue
8542 						  , bl.project_currency_code
8543 						  , bl.project_revenue
8544 						  , bl.projfunc_currency_code
8545 						  , bl.revenue
8546 						  , ROUND((rates.rate2 * revenue )/rates.mau2)*rates.mau2
8547 					)  glb2_revenue
8548 			      , DECODE(ver.wp_flag
8549 			             , 'N'
8550 						 , DECODE (g_global2_currency_code
8551 				                 , bl.txn_currency_code
8552 						         , bl.txn_init_raw_cost
8553 						         , bl.project_currency_code
8554 						         , bl.project_init_raw_cost
8555 						         , bl.projfunc_currency_code
8556 						         , bl.init_raw_cost
8557 						         , ROUND((rates.rate2 * bl.init_raw_cost )/rates.mau2)*rates.mau2
8558 					              )
8559 						 , TO_NUMBER(NULL))                 glb2_actual_raw_cost
8560    			      , DECODE(ver.wp_flag
8561 				         , 'N'
8562 						 , DECODE (g_global2_currency_code
8563 				                 , bl.txn_currency_code
8564 						         , bl.txn_init_burdened_cost
8565 						         , bl.project_currency_code
8566 						         , bl.project_init_burdened_cost
8567 						         , bl.projfunc_currency_code
8568 						         , bl.init_burdened_cost
8569 						         , ROUND((rates.rate2 * bl.init_burdened_cost )/rates.mau2)*rates.mau2
8570 					              )
8571 						 , TO_NUMBER(NULL))            glb2_actual_brdn_cost
8572 			      , DECODE(ver.wp_flag
8573 				         , 'N'
8574 						 , DECODE (g_global2_currency_code
8575 				                 , bl.txn_currency_code
8576 						         , bl.txn_init_revenue
8577 						         , bl.project_currency_code
8578 						         , bl.project_init_revenue
8579 						         , bl.projfunc_currency_code
8580 						         , bl.init_revenue
8581 						         , ROUND((rates.rate2 * bl.init_revenue )/rates.mau2)*rates.mau2
8582 					              )
8583 						 , TO_NUMBER(NULL))                  glb2_actual_revenue
8584 				  , DECODE(ver.wp_flag
8585 				         , 'N'
8586 						 , DECODE (g_global2_currency_code
8587 				                 , bl.txn_currency_code
8588 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
8589 						         , bl.project_currency_code
8590 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
8591 						         , bl.projfunc_currency_code
8592 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
8593 						         , ROUND((rates.rate2 * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau2)*rates.mau2
8594 					              )
8595 						 , TO_NUMBER(NULL))                glb2_etc_raw_cost
8596 				  , DECODE(ver.wp_flag
8597 				         , 'N'
8598 						 , DECODE (g_global2_currency_code
8599 				                 , bl.txn_currency_code
8600 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
8601 						         , bl.project_currency_code
8602 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
8603 						         , bl.projfunc_currency_code
8604 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
8605 						         , ROUND((rates.rate2 * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau2)*rates.mau2
8606 					              )
8607 						 , TO_NUMBER(NULL))    glb2_etc_brdn_cost
8608 				  , DECODE(ver.wp_flag
8609 				         , 'N'
8610 						 , DECODE (g_global2_currency_code
8611 				                 , bl.txn_currency_code
8612 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
8613 						         , bl.project_currency_code
8614 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
8615 						         , bl.projfunc_currency_code
8616 						         , (bl.revenue - NVL(bl.init_revenue, 0))
8617 						         , ROUND((rates.rate2 * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau2)*rates.mau2
8618 					              )
8619 						 , TO_NUMBER(NULL))    glb2_etc_revenue
8620                           , bl.quantity                    quantity
8621 			        , DECODE(ver.wp_flag, 'N', bl.init_quantity, TO_NUMBER(NULL))                  actual_quantity  -- new
8622 			        , DECODE(ver.wp_flag, 'N', (bl.quantity - NVL(bl.init_quantity, 0)), TO_NUMBER(NULL)) etc_quantity  -- new
8623                 	        , ra.planning_start_date  start_date
8624                 	        , ra.planning_END_date    end_date
8625                 	        , ver.time_phased_type_code time_phased_type_code
8626                 	        , ppa.org_id project_org_id
8627                 	        , ppa.carrying_out_organization_id project_organization_id
8628 			  	  , 'NTP' line_type
8629  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate
8630  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate2
8631 	        , ver.plan_type_code plan_type_code   /*4471527 */
8632                         , ver.WP_FLAG
8633 			FROM
8634                    PJI_FM_EXTR_PLNVER4           ver
8635                  , pa_resource_asSIGNments       ra
8636                  , PA_BUDGET_LINES               bl
8637                  , pa_projects_all               ppa
8638                  , pji_pjp_rbs_header            rhdr
8639                  , PJI_FM_AGGR_DLY_RATES         rates
8640                  , pji_fp_txn_accum_header       hdr
8641                  , pa_rbs_txn_accum_map          map
8642                  , PA_PROJ_ELEM_VER_STRUCTURE    pevs
8643                  WHERE 1=1
8644 			   AND ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
8645                      AND ra.project_id = ver.PROJECT_ID
8646                      AND ra.budget_version_id = ver.plan_version_id
8647                      AND ver.project_id = ppa.project_id
8648                      AND txn_currency_code IS NOT NULL
8649                      AND bl.project_currency_code IS NOT NULL
8650                      AND bl.projfunc_currency_code IS NOT NULL
8651 			         AND pevs.element_version_id = ver.wbs_struct_version_id
8652                      AND ver.project_id = pevs.project_id
8653 	 		         AND ver.secondary_rbs_flag = 'Y'
8654 					 AND ver.wp_flag = 'N'
8655 					 AND ver.baselined_flag = 'Y'
8656 					 -- AND oei.org_id = ppa.org_id
8657 					 AND ver.time_phased_type_code = 'N' -- IN (g_pa_cal_str, g_gl_cal_str)
8658 					 AND rates.time_id = DECODE ( g_currency_conversion_rule
8659                                , g_start_str
8660 					 , TO_NUMBER(to_CHAR(ra.planning_start_date, 'J'))
8661 					 , g_end_str
8662 					 , TO_NUMBER(to_CHAR(ra.planning_end_date, 'J')) )
8663 					 AND rates.pf_currency_code = bl.projfunc_currency_code -- 4764334
8664                      AND rates.worker_id = g_worker_id
8665 					  AND ra.txn_accum_header_id = hdr.txn_accum_header_id
8666 					  AND ra.txn_accum_header_id = map.txn_accum_header_id
8667 					  AND map.struct_version_id = rhdr.rbs_version_id
8668 					  AND ra.budget_version_id = rhdr.plan_version_id
8669                              AND rhdr.project_id = ver.project_id
8670 					  AND ppa.project_id = ra.project_id
8671                                 AND ver.rbs_struct_version_id = rhdr.rbs_version_id
8672 					 AND ver.worker_id = g_worker_id
8673 				  ) spread_bl
8674 				   ---- end of third inline view 'spread_bl'...........
8675             	  ,
8676             	    (
8677                       select 1   INVERT_ID from dual union all
8678                       select 2   INVERT_ID from dual where PJI_UTILS.GET_SETUP_PARAMETER('GLOBAL_CURR2_FLAG') = 'Y' union all
8679                       select 4   INVERT_ID from dual union all
8680                       select 8   INVERT_ID from dual union all
8681                       select 16  INVERT_ID from dual where PJI_UTILS.GET_SETUP_PARAMETER('TXN_CURR_FLAG') = 'Y'
8682                     ) invert
8683 				)  collapse_bl
8684 				----  End of second inline view 'collapse_bl' ..........
8685 			GROUP BY
8686               collapse_bl.PROJECT_ID
8687 			, collapse_bl.time_id
8688             , collapse_bl.WBS_ELEMENT_ID
8689             -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
8690             , collapse_bl.RBS_ELEMENT_ID
8691             , collapse_bl.RBS_STRUCT_VERSION_ID
8692 			, collapse_bl.plan_type_id
8693             , collapse_bl.plan_version_id
8694             , collapse_bl.PROJECT_ORGANIZATION_ID
8695             , collapse_bl.PROJECT_ORG_ID
8696             , collapse_bl.RESOURCE_ASSIGNMENT_ID
8697             , collapse_bl.BILLABLE_FLAG
8698             , collapse_bl.RESOURCE_CLASS
8699             , collapse_bl.TIME_PHASED_TYPE_CODE
8700             , collapse_bl.CURRENCY_CODE
8701             , collapse_bl.start_date
8702             , collapse_bl.end_date
8703             , collapse_bl.row_id
8704 			, collapse_bl.line_type
8705 			, collapse_bl.calendar_type
8706 			, collapse_bl.period_type_id
8707 	    ,collapse_bl.rate
8708             ,collapse_bl.rate2
8709             , collapse_bl.plan_type_code
8710             , collapse_bl.WP_FLAG
8711        ) plr
8712 				----  End of first inline view plr ..........
8713 	  WHERE 1=1
8714           -- AND plr.CURR_RECORD_TYPE IS NOT NULL
8715           AND plr.currency_code IS NOT NULL
8716        GROUP BY
8717          plr.PROJECT_ID
8718        , plr.PROJECT_ORG_ID
8719        , plr.project_ORGANIZATION_ID
8720        , plr.WBS_ELEMENT_ID
8721        , plr.time_id
8722        , plr.period_type_id
8723        , plr.CALENDAR_TYPE
8724        , plr.CURR_RECORD_TYPE  -- curr code missing.
8725   	   , plr.currency_code
8726        , plr.RBS_ELEMENT_ID
8727        , plr.RBS_STRUCT_VERSION_ID
8728        , plr.plan_version_id
8729   	 , plr.plan_type_id
8730        , plr.start_date
8731   	 , plr.end_date
8732 	 , plr.line_type
8733 	 ,decode(sign(plr.rate),-1,'Y',NULL) ||decode(sign(plr.rate2),-1,'Y',NULL)
8734         ,plr.plan_type_code   /*4471527 */
8735   )
8736  GROUP BY
8737 	 WORKER_ID
8738 	,PROJECT_ID
8739 	,PROJECT_ORG_ID
8740 	,project_ORGANIZATION_ID
8741 	,WBS_ELEMENT_ID
8742 	,time_id
8743 	,period_type_id
8744 	,CALENDAR_TYPE
8745 	,RBS_AGGR_LEVEL
8746 	,WBS_ROLLUP_FLAG
8747 	,PRG_ROLLUP_FLAG
8748 	,CURR_RECORD_TYPE
8749 	,currency_code
8750 	,RBS_ELEMENT_ID
8751 	,RBS_STRUCT_VERSION_ID
8752 	,plan_version_id
8753 	,plan_type_id
8754         ,LINE_TYPE
8755         ,rate_dangling_flag
8756         ,time_dangling_flag
8757         ,start_date
8758         ,end_date
8759         ,prg_level
8760         , plan_type_code ;   /*4471527 */
8761 
8762   print_time ( ' EXTRACT_PLAN_AMTS_SECRBS_GLC12 end. Inserted rows # is: ' || SQL%ROWCOUNT );
8763 
8764 EXCEPTION
8765   WHEN OTHERS THEN
8766     print_time('EXTRACT_PLAN_AMOUNTS_SECRBS : Exception ' || SQLERRM );
8767     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
8768                              p_procedure_name => 'EXTRACT_PLAN_AMTS_SECRBS_GLC12');
8769     RAISE;
8770 END;
8771 
8772 
8773 PROCEDURE EXTRACT_PLAN_AMTS_PRIRBS_GLC12 (
8774   p_pull_dangling_flag IN VARCHAR2 := 'Y') -- Reversals to be computed only if pull_dangling flag is 'Y'
8775 IS
8776 BEGIN
8777 
8778     print_time ( ' EXTRACT_PLAN_AMTS_PRIRBS_GLC12 begin. ' );
8779 
8780     PJI_PJP_FP_CURR_WRAP.get_global_currency_info (
8781       x_currency_conversion_rule => g_currency_conversion_rule
8782     , x_prorating_format         => g_prorating_format
8783     , x_global1_currency_code    => g_global1_currency_code
8784     , x_global2_currency_code    => g_global2_currency_code
8785     , x_global1_currency_mau     => g_global1_currency_mau
8786     , x_global2_currency_mau     => g_global2_currency_mau ) ;
8787 
8788 
8789   print_time(' Got global currency settings. ');
8790   print_time(' g_currency_conversion_rule ' || g_currency_conversion_rule || ' g_prorating_format ' ||  g_prorating_format );
8791   print_time(' g_global1_currency_code ' || g_global1_currency_code || ' g_global2_currency_code ' || g_global2_currency_code );
8792   print_time(' g_global1_currency_mau ' || g_global1_currency_mau || ' g_global2_currency_mau ' || g_global2_currency_mau ) ;
8793 
8794     INSERT INTO PJI_FP_AGGR_PJP1
8795     (
8796          WORKER_ID
8797        , PROJECT_ID
8798        , PROJECT_ORG_ID
8799        , PROJECT_ORGANIZATION_ID
8800        , PROJECT_ELEMENT_ID
8801        , TIME_ID
8802        , PERIOD_TYPE_ID
8803        , CALENDAR_TYPE
8804        , RBS_AGGR_LEVEL
8805        , WBS_ROLLUP_FLAG
8806        , PRG_ROLLUP_FLAG
8807        , CURR_RECORD_TYPE_ID
8808        , CURRENCY_CODE
8809        , RBS_ELEMENT_ID
8810        , RBS_VERSION_ID
8811        , PLAN_VERSION_ID
8812        , PLAN_TYPE_ID
8813        , RAW_COST
8814        , BRDN_COST
8815        , REVENUE
8816        , BILL_RAW_COST
8817        , BILL_BRDN_COST
8818        , BILL_LABOR_RAW_COST
8819        , BILL_LABOR_BRDN_COST
8820        , BILL_LABOR_HRS
8821        , EQUIPMENT_RAW_COST
8822        , EQUIPMENT_BRDN_COST
8823        , CAPITALIZABLE_RAW_COST
8824        , CAPITALIZABLE_BRDN_COST
8825        , LABOR_RAW_COST
8826        , LABOR_BRDN_COST
8827        , LABOR_HRS
8828        , LABOR_REVENUE
8829        , EQUIPMENT_HOURS
8830        , BILLABLE_EQUIPMENT_HOURS
8831        , SUP_INV_COMMITTED_COST
8832        , PO_COMMITTED_COST
8833        , PR_COMMITTED_COST
8834        , OTH_COMMITTED_COST
8835        , ACT_LABOR_HRS
8836 	   , ACT_EQUIP_HRS
8837 	   , ACT_LABOR_BRDN_COST
8838 	   , ACT_EQUIP_BRDN_COST
8839 	   , ACT_BRDN_COST
8840 	   , ACT_RAW_COST
8841 	   , ACT_REVENUE
8842          , ACT_LABOR_RAW_COST
8843          , ACT_EQUIP_RAW_COST
8844 	   , ETC_LABOR_HRS
8845 	   , ETC_EQUIP_HRS
8846 	   , ETC_LABOR_BRDN_COST
8847 	   , ETC_EQUIP_BRDN_COST
8848 	   , ETC_BRDN_COST
8849          , ETC_RAW_COST
8850          , ETC_LABOR_RAW_COST
8851          , ETC_EQUIP_RAW_COST
8852        , CUSTOM1
8853        , CUSTOM2
8854        , CUSTOM3
8855        , CUSTOM4
8856        , CUSTOM5
8857        , CUSTOM6
8858        , CUSTOM7
8859        , CUSTOM8
8860        , CUSTOM9
8861        , CUSTOM10
8862        , CUSTOM11
8863        , CUSTOM12
8864        , CUSTOM13
8865        , CUSTOM14
8866        , CUSTOM15
8867        , LINE_TYPE
8868        , RATE_DANGLING_FLAG
8869        , TIME_DANGLING_FLAG
8870        , START_DATE
8871        , END_DATE
8872        , PRG_LEVEL
8873        , PLAN_TYPE_CODE   /*4471527 */
8874 	)
8875     SELECT     /* This select is no more required. Not removing it to minimize impact for nov-11 dhi one off.
8876                  We can remove it later, to improve performance */
8877          WORKER_ID
8878        , PROJECT_ID
8879        , PROJECT_ORG_ID
8880        , project_ORGANIZATION_ID
8881        , WBS_ELEMENT_ID
8882        , time_id
8883        , period_type_id
8884        , CALENDAR_TYPE
8885        , g_lowest_level RBS_AGGR_LEVEL
8886        , 'N' WBS_ROLLUP_FLAG
8887        , 'N' PRG_ROLLUP_FLAG
8888        , CURR_RECORD_TYPE
8889        , currency_code
8890        , RBS_ELEMENT_ID
8891        , RBS_STRUCT_VERSION_ID
8892        , plan_version_id
8893        , plan_type_id
8894        , decode(rate_dangling_flag,null,SUM(RAW_COST),0)
8895        , decode(rate_dangling_flag,null,SUM(BRDN_COST),0)
8896        , decode(rate_dangling_flag,null,SUM(REVENUE),0)
8897        , decode(rate_dangling_flag,null,SUM ( BILL_RAW_COST ),0)  BILL_RAW_COST
8898        , decode(rate_dangling_flag,null,SUM (BILL_BRDN_COST ),0)   BILL_BRDN_COST
8899        , decode(rate_dangling_flag,null,SUM ( BILL_LABOR_RAW_COST),0) BILL_LABOR_RAW_COST
8900        , decode(rate_dangling_flag,null,SUM ( BILL_LABOR_BRDN_COST),0) BILL_LABOR_BRDN_COST
8901        , decode(rate_dangling_flag,null,SUM ( BILL_LABOR_HRS),0) BILL_LABOR_HRS
8902        , decode(rate_dangling_flag,null,SUM ( EQUIPMENT_RAW_COST),0) EQUIPMENT_RAW_COST
8903        , decode(rate_dangling_flag,null,SUM ( EQUIPMENT_BRDN_COST),0) EQUIPMENT_BRDN_COST
8904        , decode(rate_dangling_flag,null,SUM (CAPITALIZABLE_RAW_COST),0) CAPITALIZABLE_RAW_COST
8905        , decode(rate_dangling_flag,null,SUM ( CAPITALIZABLE_BRDN_COST),0)
8906        , decode(rate_dangling_flag,null,SUM ( LABOR_RAW_COST),0) LABOR_RAW_COST
8907        , decode(rate_dangling_flag,null,SUM ( LABOR_BRDN_COST),0) LABOR_BRDN_COST
8908        , decode(rate_dangling_flag,null,SUM ( labor_hrs),0)  labor_hrs
8909        , decode(rate_dangling_flag,null,SUM (LABOR_REVENUE),0)  LABOR_REVENUE
8910        , decode(rate_dangling_flag,null,SUM (EQUIPMENT_HOURS),0) EQUIPMENT_HOURS
8911        , decode(rate_dangling_flag,null,SUM ( BILLABLE_EQUIPMENT_HOURS),0) BILLABLE_EQUIPMENT_HOURS
8912        , decode(rate_dangling_flag,null,SUM(SUP_INV_COMMITTED_COST),0) SUP_INV_COMMITTED_COST
8913        , decode(rate_dangling_flag,null,SUM(PO_COMMITTED_COST),0) PO_COMMITTED_COST
8914        , decode(rate_dangling_flag,null,SUM(PR_COMMITTED_COST),0) PR_COMMITTED_COST
8915        , decode(rate_dangling_flag,null,SUM(OTH_COMMITTED_COST),0) PR_COMMITTED_COST
8916        , decode(rate_dangling_flag,null,SUM ( ACT_LABOR_HRS),0) ACT_LABOR_HRS
8917        , decode(rate_dangling_flag,null,SUM (ACT_EQUIP_HOURS),0) ACT_EQUIP_HOURS
8918        , decode(rate_dangling_flag,null,SUM ( ACT_LABOR_BRDN_COST),0) ACT_LABOR_BRDN_COST
8919        , decode(rate_dangling_flag,null,SUM ( ACT_EQUIPMENT_BRDN_COST),0) ACT_EQUIPMENT_BRDN_COST
8920        , decode(rate_dangling_flag,null,SUM ( ACT_BRDN_COST),0) ACT_BRDN_COST
8921        , decode(rate_dangling_flag,null,SUM ( ACT_RAW_COST),0) ACT_RAW_COST
8922        , decode(rate_dangling_flag,null,SUM ( ACT_REVENUE),0) ACT_REVENUE
8923        , decode(rate_dangling_flag,null,SUM ( ACT_LABOR_RAW_COST),0) ACT_LABOR_RAW_COST
8924        , decode(rate_dangling_flag,null,SUM ( ACT_EQUIPMENT_RAW_COST),0) ACT_EQUIPMENT_RAW_COST
8925        , decode(rate_dangling_flag,null,SUM ( ETC_LABOR_HRS),0) ETC_LABOR_HRS
8926        , decode(rate_dangling_flag,null,SUM ( ETC_EQUIP_HOURS),0) ETC_EQUIP_HOURS
8927        , decode(rate_dangling_flag,null,SUM ( ETC_LABOR_BRDN_COST),0) ETC_LABOR_BRDN_COST
8928        , decode(rate_dangling_flag,null,SUM ( ETC_EQUIP_BRDN_COST),0) ETC_EQUIP_BRDN_COST
8929        , decode(rate_dangling_flag,null,SUM ( ETC_BRDN_COST),0) ETC_BRDN_COST
8930        , decode(rate_dangling_flag,null,SUM ( ETC_RAW_COST),0) ETC_RAW_COST
8931        , decode(rate_dangling_flag,null,SUM ( ETC_LABOR_raw_COST),0) ETC_LABOR_raw_COST
8932        , decode(rate_dangling_flag,null,SUM ( ETC_EQUIP_raw_COST),0) ETC_EQUIP_raw_COST
8933        , decode(rate_dangling_flag,null,SUM(CUSTOM1),0) CUSTOM1
8934        , decode(rate_dangling_flag,null,SUM(CUSTOM2),0) CUSTOM2
8935        , decode(rate_dangling_flag,null,SUM(CUSTOM3),0) CUSTOM3
8936        , decode(rate_dangling_flag,null,SUM(CUSTOM4),0) CUSTOM4
8937        , decode(rate_dangling_flag,null,SUM(CUSTOM5),0) CUSTOM5
8938        , decode(rate_dangling_flag,null,SUM(CUSTOM6),0) CUSTOM6
8939        , decode(rate_dangling_flag,null,SUM(CUSTOM7),0) CUSTOM7
8940        , decode(rate_dangling_flag,null,SUM(CUSTOM8),0) CUSTOM8
8941        , decode(rate_dangling_flag,null,SUM(CUSTOM9),0) CUSTOM9
8942        , decode(rate_dangling_flag,null,SUM(CUSTOM10),0) CUSTOM10
8943        , decode(rate_dangling_flag,null,SUM(CUSTOM11),0) CUSTOM11
8944        , decode(rate_dangling_flag,null,SUM(CUSTOM12),0) CUSTOM12
8945        , decode(rate_dangling_flag,null,SUM(CUSTOM13),0) CUSTOM13
8946        , decode(rate_dangling_flag,null,SUM(CUSTOM14),0) CUSTOM14
8947        , decode(rate_dangling_flag,null,SUM(CUSTOM15),0) CUSTOM15
8948        , LINE_TYPE
8949        , rate_dangling_flag
8950        , time_dangling_flag
8951        , start_date
8952        , end_date
8953        , g_default_prg_level  prg_level
8954        , plan_type_code
8955  FROM
8956        (   SELECT
8957          g_worker_id WORKER_ID
8958        , plr.PROJECT_ID
8959        , plr.PROJECT_ORG_ID
8960        , plr.project_ORGANIZATION_ID
8961        , plr.WBS_ELEMENT_ID
8962        , plr.time_id
8963        , plr.period_type_id -- period type id...
8964        , plr.CALENDAR_TYPE
8965        , g_lowest_level RBS_AGGR_LEVEL
8966        , 'N' WBS_ROLLUP_FLAG
8967        , 'N' PRG_ROLLUP_FLAG
8968        , plr.CURR_RECORD_TYPE
8969        , plr.currency_code
8970        , plr.RBS_ELEMENT_ID
8971        , plr.RBS_STRUCT_VERSION_ID
8972        , plr.plan_version_id
8973        , plr.plan_type_id
8974        , SUM(plr.RAW_COST)  RAW_COST
8975        , SUM(plr.BRDN_COST) BRDN_COST
8976        , SUM(plr.REVENUE)  REVENUE
8977        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )  BILL_RAW_COST
8978        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )   BILL_BRDN_COST
8979        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_people_resclass_code, plr.raw_cost, 0 ) )  BILL_LABOR_RAW_COST
8980        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_people_resclass_code , plr.BRDN_COST, 0 ) )   BILL_LABOR_BRDN_COST
8981        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'Y' || g_people_resclass_code, plr.quantity, 0 ) )  BILL_LABOR_HRS */ -- bug 6039785
8982        , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'Y' || g_people_resclass_code,
8983                                              DECODE ( plr.billable_flag , 'Y' , plr.quantity, 0 ) , 0 ) )  BILL_LABOR_HRS -- bug 6039785
8984        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code , plr.raw_cost, 0 ) )  EQUIPMENT_RAW_COST
8985        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code , plr.BRDN_COST, 0 ) )   EQUIPMENT_BRDN_COST
8986        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )      CAPITALIZABLE_RAW_COST
8987        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )      CAPITALIZABLE_BRDN_COST
8988        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.raw_cost, 0 ) )  LABOR_RAW_COST
8989        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.BRDN_COST, 0 ) )   LABOR_BRDN_COST
8990        /* , SUM ( DECODE ( plr.resource_class, g_people_resclass_code,
8991                                             DECODE ( plr.wp_flag, 'N',
8992                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
8993                                                                    plr.quantity),
8994                                                     0 ) )   labor_hrs */ -- bug 6039785
8995        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, DECODE( plr.billable_flag , 'Y',
8996                                             DECODE ( plr.wp_flag, 'N',
8997                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
8998                                                                    plr.quantity),
8999 						    0 ),
9000                                                     0 ) )   labor_hrs -- bug 6039785
9001        , SUM ( DECODE ( plr.resource_class, g_people_resclass_code, plr.revenue, 0 ) )  LABOR_REVENUE
9002        /* , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code,
9003                                             DECODE ( plr.wp_flag, 'N',
9004                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
9005                                                                    plr.quantity),
9006 
9007                                             0 ) )  EQUIPMENT_HOURS */ -- bug 6039785
9008        , SUM ( DECODE ( plr.resource_class, g_equip_resclass_code, DECODE( plr.billable_flag , 'Y',
9009                                             DECODE ( plr.wp_flag, 'N',
9010                                                                    DECODE ( plr.billable_flag, 'Y',plr.quantity,0),
9011                                                                    plr.quantity),
9012 				            0 ),
9013                                             0 ) )  EQUIPMENT_HOURS -- bug 6039785
9014        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_equip_resclass_code, plr.quantity, 0 ) )  BILLABLE_EQUIPMENT_HOURS */ -- bug 6039785
9015        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'Y' || g_equip_resclass_code,
9016                                                                  DECODE ( plr.billable_flag , 'Y' , plr.quantity, 0 ) , 0 ) )  BILLABLE_EQUIPMENT_HOURS -- bug 6039785
9017        , TO_NUMBER(NULL)  SUP_INV_COMMITTED_COST
9018        , TO_NUMBER(NULL)  PO_COMMITTED_COST
9019        , TO_NUMBER(NULL)  PR_COMMITTED_COST
9020        , TO_NUMBER(NULL)  OTH_COMMITTED_COST
9021        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
9022                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
9023                                                                                 0 ) ) ) ACT_LABOR_HRS */ -- bug 6039785
9024        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
9025                                                                              DECODE (plr.billable_flag ,'Y',
9026                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
9027 										0 ),
9028                                                                                 0 ) ) ) ACT_LABOR_HRS -- bug 6039785
9029        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
9030                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
9031                                                                                 0 ) ) ) ACT_EQUIP_HOURS */ -- bug 6039785
9032        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
9033                                                                              DECODE (plr.billable_flag ,'Y',
9034                                                                                 DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,0),
9035 										0 ),
9036                                                                                 0 ) ) ) ACT_EQUIP_HOURS -- bug 6039785
9037        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.actual_BRDN_COST, 0 ) ) ) ACT_LABOR_BRDN_COST
9038        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.actual_BRDN_COST, 0 ) ) ) ACT_EQUIPMENT_BRDN_COST
9039        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_brdn_cost ) ) ACT_BRDN_COST
9040        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_raw_cost ) ) ACT_RAW_COST
9041        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.actual_revenue ) ) ACT_REVENUE
9042        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.actual_RAW_COST, 0 ) ) ) ACT_LABOR_RAW_COST
9043        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.actual_RAW_COST, 0 ) ) ) ACT_EQUIPMENT_RAW_COST
9044        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
9045                                                                                 DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
9046                                                                                 0 ) ) ) ETC_LABOR_HRS */ -- bug 6039785
9047        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code,
9048                                                                                 DECODE (plr.billable_flag ,'Y',
9049                                                                                         DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
9050                                                                                 0 ),
9051                                                                                 0 ) ) ) ETC_LABOR_HRS -- bug 6039785
9052        /* , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
9053                                                                                 DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
9054                                                                                 0 ) ) ) ETC_EQUIP_HOURS */ -- bug 6039785
9055        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code,
9056                                                                                 DECODE (plr.billable_flag ,'Y',
9057                                                                                         DECODE ( plr.billable_flag, 'Y',plr.ETC_quantity,0),
9058                                                                                 0 ),
9059                                                                                 0 ) ) ) ETC_EQUIP_HOURS -- bug 6039785
9060        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.etc_BRDN_COST, 0 ) ) ) ETC_LABOR_BRDN_COST
9061        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.etc_BRDN_COST, 0 ) ) ) ETC_EQUIP_BRDN_COST
9062        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.etc_brdn_cost ) ) ETC_BRDN_COST
9063        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, plr.etc_raw_cost ) ) ETC_RAW_COST
9064        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_people_resclass_code, plr.etc_raw_COST, 0 ) ) ) ETC_LABOR_raw_COST
9065        , SUM ( DECODE ( plr.wp_flag, 'Y', NULL, DECODE ( plr.resource_class, g_equip_resclass_code , plr.etc_raw_COST, 0 ) ) ) ETC_EQUIP_raw_COST
9066        , TO_NUMBER(NULL) CUSTOM1
9067        , TO_NUMBER(NULL) CUSTOM2
9068        , TO_NUMBER(NULL) CUSTOM3
9069        , TO_NUMBER(NULL) CUSTOM4
9070        , TO_NUMBER(NULL) CUSTOM5
9071        , TO_NUMBER(NULL) CUSTOM6
9072        , TO_NUMBER(NULL) CUSTOM7
9073        , TO_NUMBER(NULL) CUSTOM8
9074        , TO_NUMBER(NULL) CUSTOM9
9075        , TO_NUMBER(NULL) CUSTOM10
9076        , TO_NUMBER(NULL) CUSTOM11
9077        , TO_NUMBER(NULL) CUSTOM12
9078        , TO_NUMBER(NULL) CUSTOM13
9079        , TO_NUMBER(NULL) CUSTOM14
9080        , TO_NUMBER(NULL) CUSTOM15
9081        , plr.LINE_TYPE
9082        , decode(sign(plr.rate),-1,'Y',NULL) ||decode(sign(plr.rate2),-1,'Y',NULL) rate_dangling_flag
9083        , NULL time_dangling_flag
9084        , plr.start_date
9085   	 , plr.end_date
9086        , g_default_prg_level prg_level
9087        ,plr.plan_type_code plan_type_code  /*4471527 */
9088        FROM
9089        (          ----- First inline view plr .............
9090             select
9091               collapse_bl.PROJECT_ID      -- , 1 partition_id
9092             , collapse_bl.WBS_ELEMENT_ID  -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
9093             , SUM(collapse_bl.CURR_RECORD_TYPE_ID) CURR_RECORD_TYPE
9094             , collapse_bl.RBS_ELEMENT_ID
9095             , collapse_bl.RBS_STRUCT_VERSION_ID
9096 		    , collapse_bl.plan_type_id
9097             , collapse_bl.plan_version_id
9098 			, collapse_bl.time_id
9099             , collapse_bl.PROJECT_ORGANIZATION_ID
9100             , collapse_bl.PROJECT_ORG_ID
9101             , collapse_bl.RESOURCE_ASSIGNMENT_ID
9102             , collapse_bl.BILLABLE_FLAG
9103             , collapse_bl.RESOURCE_CLASS  -- , CALENDAR_TYPE -- , CALENDAR_ID
9104             , collapse_bl.TIME_PHASED_TYPE_CODE
9105             , collapse_bl.CURRENCY_CODE
9106             , max(collapse_bl.raw_cost) raw_cost
9107             , max(collapse_bl.BRDN_COST) BRDN_COST
9108             , max(collapse_bl.revenue) revenue
9109             , max(collapse_bl.actual_raw_cost) actual_raw_cost
9110             , max(collapse_bl.actual_BRDN_COST) actual_BRDN_COST
9111             , max(collapse_bl.actual_revenue) actual_revenue
9112             , max(collapse_bl.etc_raw_cost) etc_raw_cost
9113             , max(collapse_bl.etc_BRDN_COST) etc_BRDN_COST
9114             , max(collapse_bl.etc_revenue) etc_revenue
9115             , max(collapse_bl.quantity) quantity
9116             , max(collapse_bl.actual_quantity) actual_quantity
9117             , max(collapse_bl.etc_quantity) etc_quantity
9118             , collapse_bl.start_date start_date
9119             , collapse_bl.end_date   end_date
9120             -- , collapse_bl.period_name period_name  -- , TRACK_AS_LABOR_FLAG track_as_labor_flag
9121 			, collapse_bl.line_type
9122 			, collapse_bl.calendar_type
9123 			, collapse_bl.period_type_id
9124             , collapse_bl.row_id
9125 	    ,collapse_bl.rate rate
9126 	    ,collapse_bl.rate2 rate2
9127             , collapse_bl.plan_type_code plan_type_code
9128             , collapse_bl.WP_FLAG
9129             from
9130               (                  ----- Second inline view 'collapse_bl' begin .............
9131                select
9132                   spread_bl.row_id row_id
9133                 , spread_bl.PROJECT_ID
9134                 , spread_bl.BUDGET_VERSION_ID plan_version_id
9135 				, spread_bl.time_id
9136                 , spread_bl.RESOURCE_ASSIGNMENT_ID
9137                 , spread_bl.WBS_ELEMENT_ID
9138                 , spread_bl.RBS_ELEMENT_ID
9139                 , spread_bl.WBS_STRUCT_VERSION_ID
9140                 , spread_bl.RBS_STRUCT_VERSION_ID
9141   	 	        , spread_bl.plan_type_id
9142                 , spread_bl.BILLABLE_FLAG
9143                 , spread_bl.RESOURCE_CLASS
9144                 , spread_bl.PROJECT_ORGANIZATION_ID
9145                 , spread_bl.PROJECT_ORG_ID
9146                 , spread_bl.TIME_PHASED_TYPE_CODE
9147                 , DECODE( invert.INVERT_ID
9148                         , 1, spread_bl.glb1_CURRENCY_CODE
9149                         , 2, spread_bl.glb2_CURRENCY_CODE
9150                         , 4, spread_bl.func_CURRENCY_CODE
9151                         , 8, spread_bl.PRJ_CURRENCY_CODE
9152                         , 16, spread_bl.TXN_CURRENCY_CODE ) CURRENCY_CODE
9153                 , invert.INVERT_ID CURR_RECORD_TYPE_ID
9154                 , DECODE ( invert.INVERT_ID
9155                          , 1, spread_bl.glb1_raw_cost
9156                          , 2, spread_bl.glb2_raw_cost
9157                          , 4, spread_bl.func_raw_cost
9158                          , 8, spread_bl.PRJ_raw_cost
9159                          , 16, spread_bl.TXN_raw_cost) raw_cost
9160                 , DECODE ( invert.INVERT_ID
9161                          , 1, spread_bl.glb1_brdn_cost
9162                          , 2, spread_bl.glb2_brdn_cost
9163                          , 4, spread_bl.func_BRDN_COST
9164                          , 8, spread_bl.PRJ_BRDN_COST
9165                          , 16, spread_bl.TXN_BRDN_COST ) BRDN_COST
9166                 , DECODE ( invert.INVERT_ID
9167                          , 1, spread_bl.glb1_revenue
9168                          , 2, spread_bl.glb2_revenue
9169                          , 4, spread_bl.func_revenue
9170                          , 8, spread_bl.PRJ_revenue
9171                          , 16, spread_bl.TXN_revenue ) revenue
9172 				, DECODE ( invert.INVERT_ID
9173                          , 1, spread_bl.glb1_actual_raw_cost
9174                          , 2, spread_bl.glb2_actual_raw_cost
9175 				         , 4, spread_bl.func_actual_raw_cost
9176 						 , 8, spread_bl.prj_actual_raw_cost
9177 						 , 16, spread_bl.txn_actual_raw_cost ) actual_raw_cost
9178 				, DECODE ( invert.INVERT_ID
9179 				         , 1, spread_bl.glb1_actual_brdn_cost
9180 						 , 2, spread_bl.glb2_actual_brdn_cost
9181 				         , 4, spread_bl.func_actual_brdn_cost
9182 						 , 8, spread_bl.prj_actual_brdn_cost
9183 						 , 16, spread_bl.txn_actual_brdn_cost ) actual_brdn_cost
9184 				, DECODE ( invert.INVERT_ID
9185 				         , 1, spread_bl.glb1_actual_revenue
9186 						 , 2, spread_bl.glb2_actual_revenue
9187 				         , 4, spread_bl.func_actual_revenue
9188 						 , 8, spread_bl.prj_actual_revenue
9189 						 , 16, spread_bl.txn_actual_revenue ) actual_revenue
9190 				, DECODE ( invert.INVERT_ID
9191 				         , 1, spread_bl.glb1_etc_raw_cost
9192 						 , 2, spread_bl.glb2_etc_raw_cost
9193 				         , 4, spread_bl.func_etc_raw_cost
9194 						 , 8, spread_bl.prj_etc_raw_cost
9195 						 , 16, spread_bl.txn_etc_raw_cost ) etc_raw_cost
9196 				, DECODE ( invert.INVERT_ID
9197 				         , 1, spread_bl.glb1_etc_brdn_cost
9198 						 , 2, spread_bl.glb2_etc_brdn_cost
9199 				         , 4, spread_bl.func_etc_brdn_cost
9200 						 , 8, spread_bl.prj_etc_brdn_cost
9201 						 , 16, spread_bl.txn_etc_brdn_cost ) etc_brdn_cost
9202 				, DECODE ( invert.INVERT_ID
9203 				         , 1, spread_bl.glb1_etc_revenue
9204 						 , 2, spread_bl.glb2_etc_revenue
9205 				         , 4, spread_bl.func_etc_revenue
9206 						 , 8, spread_bl.prj_etc_revenue
9207 						 , 16, spread_bl.txn_etc_revenue ) etc_revenue
9208             	, spread_bl.quantity quantity
9209             	, spread_bl.actual_quantity actual_quantity
9210             	, spread_bl.etc_quantity etc_quantity
9211             	, spread_bl.start_date start_date
9212             	, spread_bl.end_date   end_date
9213             	, spread_bl.line_type line_type
9214 				, spread_bl.period_type_id
9215 				, spread_bl.calendar_type
9216  		,decode(invert.invert_id,1,spread_bl.rate,1) rate
9217  		,decode(invert.invert_id,2,spread_bl.rate2,1) rate2
9218                             , spread_bl.plan_type_code plan_type_code   /*4471527 */
9219                 , spread_bl.WP_FLAG
9220                 from
9221                   (     ----- Third inline view 'spread_bl'  .............
9222                     SELECT /*+ ordered */
9223                 	  ra.project_id
9224                 	, bl.rowid row_id
9225                 	, ra.budget_version_id
9226 					, prd.cal_period_id time_id
9227                 	, bl.resource_asSIGNment_id
9228                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
9229                 	, NVL(ra.rbs_element_id, -1)              rbs_element_id
9230                 	, ver.wbs_struct_version_id      wbs_struct_version_id
9231                 	, NVL(ver.rbs_struct_version_id, -1)    rbs_struct_version_id
9232 			, ver.plan_type_id               plan_type_id
9233 			, ra.rate_based_flag             billable_flag
9234 			, ra.resource_class_code         resource_class
9235 			, DECODE(ver.time_phased_type_code
9236                , g_pa_cal_str, 32
9237                , g_gl_cal_str, 32
9238                , 'N', 2048
9239                , -1) period_type_id
9240 			   , DECODE(ver.time_phased_type_code
9241                , g_pa_cal_str, g_pa_cal_str
9242                , g_gl_cal_str, g_gl_cal_str
9243                , 'N', g_all
9244                , 'X')  CALENDAR_TYPE
9245                 	, bl.txn_currency_code           txn_currency_code
9246                 	, bl.txn_raw_cost                txn_raw_cost
9247                 	, bl.txn_burdened_COST           txn_brdn_COST
9248                 	, bl.txn_revenue                 txn_revenue
9249 			, DECODE(ver.wp_flag, 'N', bl.txn_init_raw_cost, TO_NUMBER(NULL))                txn_actual_raw_cost  -- new
9250 			, DECODE(ver.wp_flag, 'N', bl.txn_init_burdened_cost, TO_NUMBER(NULL))             txn_actual_brdn_cost  -- new
9251 			, DECODE(ver.wp_flag, 'N', bl.txn_init_revenue, TO_NUMBER(NULL))                   txn_actual_revenue  -- new
9252 			, DECODE(ver.wp_flag, 'N', (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0)), TO_NUMBER(NULL)) txn_etc_raw_cost     -- new
9253 			, DECODE(ver.wp_flag, 'N', (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0)), TO_NUMBER(NULL)) txn_etc_brdn_cost     -- new
9254 			, DECODE(ver.wp_flag, 'N', (bl.txn_revenue - NVL(bl.txn_init_revenue, 0)), TO_NUMBER(NULL)) txn_etc_revenue     -- new
9255                 	, bl.project_currency_code          prj_currency_code
9256                 	, bl.project_raw_cost               prj_raw_cost
9257                 	, bl.project_burdened_COST          prj_BRDN_COST
9258                 	, bl.project_revenue                prj_revenue
9259 			, DECODE(ver.wp_flag, 'N', bl.project_init_raw_cost, TO_NUMBER(NULL))          prj_actual_raw_cost  -- new
9260 			, DECODE(ver.wp_flag, 'N', bl.project_init_burdened_cost, TO_NUMBER(NULL))     prj_actual_brdn_cost  -- new
9261 			, DECODE(ver.wp_flag, 'N', bl.project_init_revenue, TO_NUMBER(NULL))           prj_actual_revenue  -- new
9262 			, DECODE(ver.wp_flag, 'N', (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0)), TO_NUMBER(NULL)) prj_etc_raw_cost     -- new
9263 			, DECODE(ver.wp_flag, 'N', (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0)), TO_NUMBER(NULL)) prj_etc_brdn_cost     -- new
9264 			, DECODE(ver.wp_flag, 'N', (bl.project_revenue - NVL(bl.project_init_revenue, 0)), TO_NUMBER(NULL)) prj_etc_revenue     -- new
9265                 	, bl.projfunc_currency_code         func_currency_code
9266                   , bl.raw_cost                       func_raw_cost
9267                 	, bl.burdened_COST                  func_BRDN_COST
9268                 	, bl.revenue                        func_revenue
9269 			, DECODE(ver.wp_flag, 'N', bl.init_raw_cost , TO_NUMBER(NULL))                 func_actual_raw_cost  -- new
9270 			, DECODE(ver.wp_flag, 'N', bl.init_burdened_cost , TO_NUMBER(NULL))            func_actual_brdn_cost  -- new
9271 			, DECODE(ver.wp_flag, 'N', bl.init_revenue , TO_NUMBER(NULL))                  func_actual_revenue  -- new
9272 			, DECODE(ver.wp_flag, 'N', (bl.raw_cost - NVL(bl.init_raw_cost, 0)), TO_NUMBER(NULL)) func_etc_raw_cost     -- new
9273 			, DECODE(ver.wp_flag, 'N', (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)), TO_NUMBER(NULL)) func_etc_brdn_cost     -- new
9274 			, DECODE(ver.wp_flag, 'N', (bl.revenue - NVL(bl.init_revenue, 0)), TO_NUMBER(NULL)) func_etc_revenue     -- new
9275                 	, g_global1_currency_code  glb1_currency_code -- g_global1_currency_code        glb1_currency_code
9276                   , DECODE (g_global1_currency_code
9277 				          , bl.txn_currency_code
9278 						  , bl.txn_raw_cost
9279 						  , bl.project_currency_code
9280 						  , bl.project_raw_cost
9281 						  , bl.projfunc_currency_code
9282 						  , bl.raw_cost
9283 						  , ROUND((rates.rate * raw_cost )/rates.mau)*rates.mau
9284 					) glb1_raw_cost
9285                   , DECODE (g_global1_currency_code
9286 				          , bl.txn_currency_code
9287 						  , bl.txn_burdened_cost
9288 						  , bl.project_currency_code
9289 						  , bl.project_burdened_cost
9290 						  , bl.projfunc_currency_code
9291 						  , bl.burdened_cost
9292 						  , ROUND((rates.rate * burdened_cost )/rates.mau)*rates.mau
9293 					) glb1_BRDN_COST
9294                   , DECODE (g_global1_currency_code
9295 				          , bl.txn_currency_code
9296 						  , bl.txn_revenue
9297 						  , bl.project_currency_code
9298 						  , bl.project_revenue
9299 						  , bl.projfunc_currency_code
9300 						  , bl.revenue
9301 						  , ROUND((rates.rate * revenue )/rates.mau)*rates.mau
9302 					)  glb1_revenue
9303 			      , DECODE(ver.wp_flag
9304 			             , 'N'
9305 						 , DECODE (g_global1_currency_code
9306 				                 , bl.txn_currency_code
9307 						         , bl.txn_init_raw_cost
9308 						         , bl.project_currency_code
9309 						         , bl.project_init_raw_cost
9310 						         , bl.projfunc_currency_code
9311 						         , bl.init_raw_cost
9312 						         , ROUND((rates.rate * bl.init_raw_cost )/rates.mau)*rates.mau
9313 					              )
9314 						 , NULL)                 glb1_actual_raw_cost
9315    			      , DECODE(ver.wp_flag
9316 				         , 'N'
9317 						 , DECODE (g_global1_currency_code
9318 				                 , bl.txn_currency_code
9319 						         , bl.txn_init_burdened_cost
9320 						         , bl.project_currency_code
9321 						         , bl.project_init_burdened_cost
9322 						         , bl.projfunc_currency_code
9323 						         , bl.init_burdened_cost
9324 						         , ROUND((rates.rate * bl.init_burdened_cost )/rates.mau)*rates.mau
9325 					              )
9326 						 , NULL)            glb1_actual_brdn_cost
9327 			      , DECODE(ver.wp_flag
9328 				         , 'N'
9329 						 , DECODE (g_global1_currency_code
9330 				                 , bl.txn_currency_code
9331 						         , bl.txn_init_revenue
9332 						         , bl.project_currency_code
9333 						         , bl.project_init_revenue
9334 						         , bl.projfunc_currency_code
9335 						         , bl.init_revenue
9336 						         , ROUND((rates.rate * bl.init_revenue )/rates.mau)*rates.mau
9337 					              )
9338 						 , NULL)                  glb1_actual_revenue
9339 				  , DECODE(ver.wp_flag
9340 				         , 'N'
9341 						 , DECODE (g_global1_currency_code
9342 				                 , bl.txn_currency_code
9343 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
9344 						         , bl.project_currency_code
9345 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
9346 						         , bl.projfunc_currency_code
9347 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
9348 						         , ROUND((rates.rate * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau)*rates.mau
9349 					              )
9350 						 , NULL)                glb1_etc_raw_cost
9351 				  , DECODE(ver.wp_flag
9352 				         , 'N'
9353 						 , DECODE (g_global1_currency_code
9354 				                 , bl.txn_currency_code
9355 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
9356 						         , bl.project_currency_code
9357 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
9358 						         , bl.projfunc_currency_code
9359 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
9360 						         , ROUND((rates.rate * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau)*rates.mau
9361 					              )
9362 						 , NULL)    glb1_etc_brdn_cost
9363 				  , DECODE(ver.wp_flag
9364 				         , 'N'
9365 						 , DECODE (g_global1_currency_code
9366 				                 , bl.txn_currency_code
9367 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
9368 						         , bl.project_currency_code
9369 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
9370 						         , bl.projfunc_currency_code
9371 						         , (bl.revenue - NVL(bl.init_revenue, 0))
9372 						         , ROUND((rates.rate * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau)*rates.mau
9373 					              )
9374 						 , NULL)    glb1_etc_revenue
9375                 	, g_global2_currency_code  glb2_currency_code -- g_global2_currency_code        glb2_currency_code
9376                   , DECODE (g_global2_currency_code
9377 				          , bl.txn_currency_code
9378 						  , bl.txn_raw_cost
9379 						  , bl.project_currency_code
9380 						  , bl.project_raw_cost
9381 						  , bl.projfunc_currency_code
9382 						  , bl.raw_cost
9383 						  , ROUND((rates.rate2 * raw_cost )/rates.mau2)*rates.mau2
9384 					) glb2_raw_cost
9385                   , DECODE (g_global2_currency_code
9386 				          , bl.txn_currency_code
9387 						  , bl.txn_burdened_cost
9388 						  , bl.project_currency_code
9389 						  , bl.project_burdened_cost
9390 						  , bl.projfunc_currency_code
9391 						  , bl.burdened_cost
9392 						  , ROUND((rates.rate2 * burdened_cost )/rates.mau2)*rates.mau2
9393 					) glb2_BRDN_COST
9394                   , DECODE (g_global2_currency_code
9395 				          , bl.txn_currency_code
9396 						  , bl.txn_revenue
9397 						  , bl.project_currency_code
9398 						  , bl.project_revenue
9399 						  , bl.projfunc_currency_code
9400 						  , bl.revenue
9401 						  , ROUND((rates.rate2 * revenue )/rates.mau2)*rates.mau2
9402 					)  glb2_revenue
9403 			      , DECODE(ver.wp_flag
9404 			             , 'N'
9405 						 , DECODE (g_global2_currency_code
9406 				                 , bl.txn_currency_code
9407 						         , bl.txn_init_raw_cost
9408 						         , bl.project_currency_code
9409 						         , bl.project_init_raw_cost
9410 						         , bl.projfunc_currency_code
9411 						         , bl.init_raw_cost
9412 						         , ROUND((rates.rate2 * bl.init_raw_cost )/rates.mau2)*rates.mau2
9413 					              )
9414 						 , TO_NUMBER(NULL))                 glb2_actual_raw_cost
9415    			      , DECODE(ver.wp_flag
9416 				         , 'N'
9417 						 , DECODE (g_global2_currency_code
9418 				                 , bl.txn_currency_code
9419 						         , bl.txn_init_burdened_cost
9420 						         , bl.project_currency_code
9421 						         , bl.project_init_burdened_cost
9422 						         , bl.projfunc_currency_code
9423 						         , bl.init_burdened_cost
9424 						         , ROUND((rates.rate2 * bl.init_burdened_cost )/rates.mau2)*rates.mau2
9425 					              )
9426 						 , TO_NUMBER(NULL))            glb2_actual_brdn_cost
9427 			      , DECODE(ver.wp_flag
9428 				         , 'N'
9429 						 , DECODE (g_global2_currency_code
9430 				                 , bl.txn_currency_code
9431 						         , bl.txn_init_revenue
9432 						         , bl.project_currency_code
9433 						         , bl.project_init_revenue
9434 						         , bl.projfunc_currency_code
9435 						         , bl.init_revenue
9436 						         , ROUND((rates.rate2 * bl.init_revenue )/rates.mau2)*rates.mau2
9437 					              )
9438 						 , TO_NUMBER(NULL))                  glb2_actual_revenue
9439 				  , DECODE(ver.wp_flag
9440 				         , 'N'
9441 						 , DECODE (g_global2_currency_code
9442 				                 , bl.txn_currency_code
9443 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
9444 						         , bl.project_currency_code
9445 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
9446 						         , bl.projfunc_currency_code
9447 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
9448 						         , ROUND((rates.rate2 * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau2)*rates.mau2
9449 					              )
9450 						 , TO_NUMBER(NULL))                glb2_etc_raw_cost
9451 				  , DECODE(ver.wp_flag
9452 				         , 'N'
9453 						 , DECODE (g_global2_currency_code
9454 				                 , bl.txn_currency_code
9455 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
9456 						         , bl.project_currency_code
9457 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
9458 						         , bl.projfunc_currency_code
9459 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
9460 						         , ROUND((rates.rate2 * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau2)*rates.mau2
9461 					              )
9462 						 , TO_NUMBER(NULL))    glb2_etc_brdn_cost
9463 				  , DECODE(ver.wp_flag
9464 				         , 'N'
9465 						 , DECODE (g_global2_currency_code
9466 				                 , bl.txn_currency_code
9467 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
9468 						         , bl.project_currency_code
9469 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
9470 						         , bl.projfunc_currency_code
9471 						         , (bl.revenue - NVL(bl.init_revenue, 0))
9472 						         , ROUND((rates.rate2 * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau2)*rates.mau2
9473 					              )
9474 						 , TO_NUMBER(NULL))    glb2_etc_revenue
9475                   , bl.quantity                    quantity
9476 			, DECODE(ver.wp_flag, 'N', bl.init_quantity, TO_NUMBER(NULL))                  actual_quantity  -- new
9477 			, DECODE(ver.wp_flag, 'N', (bl.quantity - NVL(bl.init_quantity, 0)), TO_NUMBER(NULL)) etc_quantity  -- new
9478                 	, TO_DATE(NULL) start_date -- bl.start_date
9479                 	, TO_DATE(NULL) end_date -- bl.END_date
9480                 	, ver.time_phased_type_code time_phased_type_code
9481                 	, ppa.org_id project_org_id
9482                 	, ppa.carrying_out_organization_id project_organization_id
9483 					, DECODE(ver.time_phased_type_code, g_pa_cal_str, 'OF', g_gl_cal_str, 'OF', 'N', 'NTP', 'X') line_type
9484  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate
9485  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate2
9486                                , ver.plan_type_code plan_type_code   /*4471527 */
9487                         , ver.WP_FLAG
9488 				 FROM
9489                    PJI_FM_EXTR_PLNVER4           ver
9490                  , pa_resource_asSIGNments       ra
9491                  , PA_BUDGET_LINES               bl
9492                  , pa_projects_all               ppa
9493                  , PJI_ORG_EXTR_INFO             oei
9494                  , pji_time_cal_period_v         prd
9495                  , PJI_FM_AGGR_DLY_RATES         rates
9496                  , PA_PROJ_ELEM_VER_STRUCTURE    pevs
9497                  WHERE 1=1
9498 				     AND ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
9499                      AND ra.project_id = ver.PROJECT_ID
9500                      AND ra.budget_version_id = ver.plan_version_id
9501                      AND ver.project_id = ppa.project_id
9502                      AND txn_currency_code IS NOT NULL
9503                      AND bl.project_currency_code IS NOT NULL
9504                      AND bl.projfunc_currency_code IS NOT NULL
9505 			         AND pevs.element_version_id = ver.wbs_struct_version_id
9506                      AND ver.project_id = pevs.project_id
9507 	 		         AND ver.secondary_rbs_flag = 'N'
9508 					 AND ver.wp_flag = 'N'
9509 					 AND ver.baselined_flag = 'Y'
9510 					 AND oei.org_id = ppa.org_id
9511 					 AND ver.time_phased_type_code IN (g_pa_cal_str, g_gl_cal_str)
9512 					 AND DECODE ( ver.time_phased_type_code
9513 					            , g_pa_cal_str, oei.pa_calendar_id
9514 								, g_gl_cal_str, oei.gl_calendar_id) = prd.calendar_id
9515 					 AND bl.period_name = prd.name
9516 					 AND rates.time_id = prd.cal_period_id
9517 					 AND rates.worker_id = g_worker_id
9518 					 AND rates.pf_currency_code = bl.projfunc_currency_code -- 4764334
9519 					 AND ver.worker_id = g_worker_id
9520                 UNION ALL
9521                     SELECT /*+ ordered */
9522                 	  ra.project_id
9523                 	, bl.rowid row_id
9524                 	, ra.budget_version_id
9525 					, -1 time_id
9526                 	, bl.resource_asSIGNment_id
9527                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
9528                 	, NVL(ra.rbs_element_id, -1)              rbs_element_id
9529                 	, ver.wbs_struct_version_id      wbs_struct_version_id
9530                 	, NVL(ver.rbs_struct_version_id, -1)    rbs_struct_version_id
9531 			, ver.plan_type_id               plan_type_id
9532 			, ra.rate_based_flag             billable_flag
9533 			, ra.resource_class_code         resource_class
9534 			, 2048 period_type_id
9535 			   , g_all CALENDAR_TYPE
9536                 	, bl.txn_currency_code           txn_currency_code
9537                 	, bl.txn_raw_cost                txn_raw_cost
9538                 	, bl.txn_burdened_COST           txn_brdn_COST
9539                 	, bl.txn_revenue                 txn_revenue
9540 			, DECODE(ver.wp_flag, 'N' , bl.txn_init_raw_cost, TO_NUMBER(NULL))                txn_actual_raw_cost  -- new
9541 			, DECODE(ver.wp_flag, 'N' , bl.txn_init_burdened_cost, TO_NUMBER(NULL))             txn_actual_brdn_cost  -- new
9542 			, DECODE(ver.wp_flag, 'N' , bl.txn_init_revenue, TO_NUMBER(NULL))                   txn_actual_revenue  -- new
9543 			, DECODE(ver.wp_flag, 'N' , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0)), TO_NUMBER(NULL)) txn_etc_raw_cost     -- new
9544 			, DECODE(ver.wp_flag, 'N' , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0)), TO_NUMBER(NULL)) txn_etc_brdn_cost     -- new
9545 			, DECODE(ver.wp_flag, 'N' , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0)), TO_NUMBER(NULL)) txn_etc_revenue     -- new
9546                 	, bl.project_currency_code          prj_currency_code
9547                 	, bl.project_raw_cost               prj_raw_cost
9548                 	, bl.project_burdened_COST          prj_BRDN_COST
9549                 	, bl.project_revenue                prj_revenue
9550 			, DECODE(ver.wp_flag, 'N' , bl.project_init_raw_cost, TO_NUMBER(NULL))          prj_actual_raw_cost  -- new
9551 			, DECODE(ver.wp_flag, 'N' , bl.project_init_burdened_cost, TO_NUMBER(NULL))     prj_actual_brdn_cost  -- new
9552 			, DECODE(ver.wp_flag, 'N' , bl.project_init_revenue, TO_NUMBER(NULL))           prj_actual_revenue  -- new
9553 			, DECODE(ver.wp_flag, 'N' , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0)), TO_NUMBER(NULL)) prj_etc_raw_cost     -- new
9554 			, DECODE(ver.wp_flag, 'N' , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0)), TO_NUMBER(NULL)) prj_etc_brdn_cost     -- new
9555 			, DECODE(ver.wp_flag, 'N' , (bl.project_revenue - NVL(bl.project_init_revenue, 0)), TO_NUMBER(NULL)) prj_etc_revenue     -- new
9556                 	, bl.projfunc_currency_code         func_currency_code
9557                   , bl.raw_cost                       func_raw_cost
9558                 	, bl.burdened_COST                  func_BRDN_COST
9559                 	, bl.revenue                        func_revenue
9560 			, DECODE(ver.wp_flag, 'N' , bl.init_raw_cost , TO_NUMBER(NULL))                 func_actual_raw_cost  -- new
9561 			, DECODE(ver.wp_flag, 'N' , bl.init_burdened_cost , TO_NUMBER(NULL))            func_actual_brdn_cost  -- new
9562 			, DECODE(ver.wp_flag, 'N' , bl.init_revenue , TO_NUMBER(NULL))                  func_actual_revenue  -- new
9563 			, DECODE(ver.wp_flag, 'N' , (bl.raw_cost - NVL(bl.init_raw_cost, 0)), TO_NUMBER(NULL)) func_etc_raw_cost     -- new
9564 			, DECODE(ver.wp_flag, 'N' , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)), TO_NUMBER(NULL)) func_etc_brdn_cost     -- new
9565 			, DECODE(ver.wp_flag, 'N' , (bl.revenue - NVL(bl.init_revenue, 0)), TO_NUMBER(NULL)) func_etc_revenue     -- new
9566                 	, g_global1_currency_code   glb1_currency_code -- g_global1_currency_code         glb1_currency_code
9567                   , DECODE (g_global1_currency_code
9568 				          , bl.txn_currency_code
9569 						  , bl.txn_raw_cost
9570 						  , bl.project_currency_code
9571 						  , bl.project_raw_cost
9572 						  , bl.projfunc_currency_code
9573 						  , bl.raw_cost
9574 						  , ROUND((rates.rate * raw_cost )/rates.mau)*rates.mau
9575 					) glb1_raw_cost
9576                   , DECODE (g_global1_currency_code
9577 				          , bl.txn_currency_code
9578 						  , bl.txn_burdened_cost
9579 						  , bl.project_currency_code
9580 						  , bl.project_burdened_cost
9581 						  , bl.projfunc_currency_code
9582 						  , bl.burdened_cost
9583 						  , ROUND((rates.rate * burdened_cost )/rates.mau)*rates.mau
9584 					) glb1_BRDN_COST
9585                   , DECODE (g_global1_currency_code
9586 				          , bl.txn_currency_code
9587 						  , bl.txn_revenue
9588 						  , bl.project_currency_code
9589 						  , bl.project_revenue
9590 						  , bl.projfunc_currency_code
9591 						  , bl.revenue
9592 						  , ROUND((rates.rate * revenue )/rates.mau)*rates.mau
9593 					)  glb1_revenue
9594 			      , DECODE(ver.wp_flag
9595 			             , 'N'
9596 						 , DECODE (g_global1_currency_code
9597 				                 , bl.txn_currency_code
9598 						         , bl.txn_init_raw_cost
9599 						         , bl.project_currency_code
9600 						         , bl.project_init_raw_cost
9601 						         , bl.projfunc_currency_code
9602 						         , bl.init_raw_cost
9603 						         , ROUND((rates.rate * bl.init_raw_cost )/rates.mau)*rates.mau
9604 					              )
9605 						 , NULL)                 glb1_actual_raw_cost
9606    			      , DECODE(ver.wp_flag
9607 				         , 'N'
9608 						 , DECODE (g_global1_currency_code
9609 				                 , bl.txn_currency_code
9610 						         , bl.txn_init_burdened_cost
9611 						         , bl.project_currency_code
9612 						         , bl.project_init_burdened_cost
9613 						         , bl.projfunc_currency_code
9614 						         , bl.init_burdened_cost
9615 						         , ROUND((rates.rate * bl.init_burdened_cost )/rates.mau)*rates.mau
9616 					              )
9617 						 , NULL)            glb1_actual_brdn_cost
9618 			      , DECODE(ver.wp_flag
9619 				         , 'N'
9620 						 , DECODE (g_global1_currency_code
9621 				                 , bl.txn_currency_code
9622 						         , bl.txn_init_revenue
9623 						         , bl.project_currency_code
9624 						         , bl.project_init_revenue
9625 						         , bl.projfunc_currency_code
9626 						         , bl.init_revenue
9627 						         , ROUND((rates.rate * bl.init_revenue )/rates.mau)*rates.mau
9628 					              )
9629 						 , NULL)                  glb1_actual_revenue
9630 				  , DECODE(ver.wp_flag
9631 				         , 'N'
9632 						 , DECODE (g_global1_currency_code
9633 				                 , bl.txn_currency_code
9634 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
9635 						         , bl.project_currency_code
9636 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
9637 						         , bl.projfunc_currency_code
9638 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
9639 						         , ROUND((rates.rate * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau)*rates.mau
9640 					              )
9641 						 , NULL)                glb1_etc_raw_cost
9642 				  , DECODE(ver.wp_flag
9643 				         , 'N'
9644 						 , DECODE (g_global1_currency_code
9645 				                 , bl.txn_currency_code
9646 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
9647 						         , bl.project_currency_code
9648 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
9649 						         , bl.projfunc_currency_code
9650 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
9651 						         , ROUND((rates.rate * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau)*rates.mau
9652 					              )
9653 						 , NULL)    glb1_etc_brdn_cost
9654 				  , DECODE(ver.wp_flag
9655 				         , 'N'
9656 						 , DECODE (g_global1_currency_code
9657 				                 , bl.txn_currency_code
9658 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
9659 						         , bl.project_currency_code
9660 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
9661 						         , bl.projfunc_currency_code
9662 						         , (bl.revenue - NVL(bl.init_revenue, 0))
9663 						         , ROUND((rates.rate * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau)*rates.mau
9664 					              )
9665 						 , NULL)    glb1_etc_revenue
9666                 	, g_global2_currency_code   glb2_currency_code -- g_global2_currency_code         glb2_currency_code
9667                   , DECODE (g_global2_currency_code
9668 				          , bl.txn_currency_code
9669 						  , bl.txn_raw_cost
9670 						  , bl.project_currency_code
9671 						  , bl.project_raw_cost
9672 						  , bl.projfunc_currency_code
9673 						  , bl.raw_cost
9674 						  , ROUND((rates.rate2 * raw_cost )/rates.mau2)*rates.mau2
9675 					) glb2_raw_cost
9676                   , DECODE (g_global2_currency_code
9677 				          , bl.txn_currency_code
9678 						  , bl.txn_burdened_cost
9679 						  , bl.project_currency_code
9680 						  , bl.project_burdened_cost
9681 						  , bl.projfunc_currency_code
9682 						  , bl.burdened_cost
9683 						  , ROUND((rates.rate2 * burdened_cost )/rates.mau2)*rates.mau2
9684 					) glb2_BRDN_COST
9685                   , DECODE (g_global2_currency_code
9686 				          , bl.txn_currency_code
9687 						  , bl.txn_revenue
9688 						  , bl.project_currency_code
9689 						  , bl.project_revenue
9690 						  , bl.projfunc_currency_code
9691 						  , bl.revenue
9692 						  , ROUND((rates.rate2 * revenue )/rates.mau2)*rates.mau2
9693 					)  glb2_revenue
9694 			      , DECODE(ver.wp_flag
9695 			             , 'N'
9696 						 , DECODE (g_global2_currency_code
9697 				                 , bl.txn_currency_code
9698 						         , bl.txn_init_raw_cost
9699 						         , bl.project_currency_code
9700 						         , bl.project_init_raw_cost
9701 						         , bl.projfunc_currency_code
9702 						         , bl.init_raw_cost
9703 						         , ROUND((rates.rate2 * bl.init_raw_cost )/rates.mau2)*rates.mau2
9704 					              )
9705 						 , TO_NUMBER(NULL))                 glb2_actual_raw_cost
9706    			      , DECODE(ver.wp_flag
9707 				         , 'N'
9708 						 , DECODE (g_global2_currency_code
9709 				                 , bl.txn_currency_code
9710 						         , bl.txn_init_burdened_cost
9711 						         , bl.project_currency_code
9712 						         , bl.project_init_burdened_cost
9713 						         , bl.projfunc_currency_code
9714 						         , bl.init_burdened_cost
9715 						         , ROUND((rates.rate2 * bl.init_burdened_cost )/rates.mau2)*rates.mau2
9716 					              )
9717 						 , TO_NUMBER(NULL))            glb2_actual_brdn_cost
9718 			      , DECODE(ver.wp_flag
9719 				         , 'N'
9720 						 , DECODE (g_global2_currency_code
9721 				                 , bl.txn_currency_code
9722 						         , bl.txn_init_revenue
9723 						         , bl.project_currency_code
9724 						         , bl.project_init_revenue
9725 						         , bl.projfunc_currency_code
9726 						         , bl.init_revenue
9727 						         , ROUND((rates.rate2 * bl.init_revenue )/rates.mau2)*rates.mau2
9728 					              )
9729 						 , TO_NUMBER(NULL))                  glb2_actual_revenue
9730 				  , DECODE(ver.wp_flag
9731 				         , 'N'
9732 						 , DECODE (g_global2_currency_code
9733 				                 , bl.txn_currency_code
9734 						         , (bl.txn_raw_cost - NVL(bl.txn_init_raw_cost, 0))
9735 						         , bl.project_currency_code
9736 						         , (bl.project_raw_cost - NVL(bl.project_init_raw_cost, 0))
9737 						         , bl.projfunc_currency_code
9738 						         , (bl.raw_cost - NVL(bl.init_raw_cost, 0))
9739 						         , ROUND((rates.rate2 * (bl.raw_cost - NVL(bl.init_raw_cost, 0)) )/rates.mau2)*rates.mau2
9740 					              )
9741 						 , TO_NUMBER(NULL))                glb2_etc_raw_cost
9742 				  , DECODE(ver.wp_flag
9743 				         , 'N'
9744 						 , DECODE (g_global2_currency_code
9745 				                 , bl.txn_currency_code
9746 						         , (bl.txn_burdened_cost - NVL(bl.txn_init_burdened_cost, 0))
9747 						         , bl.project_currency_code
9748 						         , (bl.project_burdened_cost - NVL(bl.project_init_burdened_cost, 0))
9749 						         , bl.projfunc_currency_code
9750 						         , (bl.burdened_cost - NVL(bl.init_burdened_cost, 0))
9751 						         , ROUND((rates.rate2 * (bl.burdened_cost - NVL(bl.init_burdened_cost, 0)) )/rates.mau2)*rates.mau2
9752 					              )
9753 						 , TO_NUMBER(NULL))    glb2_etc_brdn_cost
9754 				  , DECODE(ver.wp_flag
9755 				         , 'N'
9756 						 , DECODE (g_global2_currency_code
9757 				                 , bl.txn_currency_code
9758 						         , (bl.txn_revenue - NVL(bl.txn_init_revenue, 0))
9759 						         , bl.project_currency_code
9760 						         , (bl.project_revenue - NVL(bl.project_init_revenue, 0))
9761 						         , bl.projfunc_currency_code
9762 						         , (bl.revenue - NVL(bl.init_revenue, 0))
9763 						         , ROUND((rates.rate2 * (bl.revenue - NVL(bl.init_revenue, 0)) )/rates.mau2)*rates.mau2
9764 					              )
9765 						 , TO_NUMBER(NULL))    glb2_etc_revenue
9766                   , bl.quantity                    quantity
9767 			, DECODE(ver.wp_flag, 'N' , bl.init_quantity, TO_NUMBER(NULL))                  actual_quantity  -- new
9768 			, DECODE(ver.wp_flag, 'N' , (bl.quantity - NVL(bl.init_quantity, 0)), TO_NUMBER(NULL)) etc_quantity  -- new
9769                 	, ra.planning_start_date
9770                 	, ra.planning_END_date
9771                 	, ver.time_phased_type_code time_phased_type_code
9772                 	, ppa.org_id project_org_id
9773                 	, ppa.carrying_out_organization_id project_organization_id
9774 					, 'NTP' line_type
9775  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate
9776  			,decode(ver.rate_dangling_flag,'Y',-1,1) rate2
9777 	, ver.plan_type_code plan_type_code   /*4471527 */
9778                         , ver.WP_FLAG
9779 				 FROM
9780                        PJI_FM_EXTR_PLNVER4           ver
9781                      , pa_resource_asSIGNments       ra
9782                      , PA_BUDGET_LINES               bl
9783                      , pa_projects_all               ppa
9784                      , PJI_FM_AGGR_DLY_RATES         rates
9785                      , PA_PROJ_ELEM_VER_STRUCTURE    pevs
9786                  WHERE 1=1
9787 				     AND ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
9788                      AND ra.project_id = ver.PROJECT_ID
9789                      AND ra.budget_version_id = ver.plan_version_id
9790                      AND ver.project_id = ppa.project_id
9791                      AND txn_currency_code IS NOT NULL
9792                      AND bl.project_currency_code IS NOT NULL
9793                      AND bl.projfunc_currency_code IS NOT NULL
9794 			         AND pevs.element_version_id = ver.wbs_struct_version_id
9795                      AND ver.project_id = pevs.project_id
9796 	 		         AND ver.secondary_rbs_flag = 'N'
9797 					 AND ver.wp_flag = 'N'
9798 					 AND ver.baselined_flag = 'Y'
9799 					 AND ver.time_phased_type_code = 'N'
9800 					 AND rates.time_id = DECODE ( g_currency_conversion_rule
9801                                , 'S'
9802 					 , TO_NUMBER(to_CHAR(ra.planning_start_date, 'J'))
9803 					 , 'E'
9804 					 , TO_NUMBER(to_CHAR(ra.planning_end_date, 'J')) )
9805 					 AND rates.worker_id = g_worker_id
9806 					 AND rates.pf_currency_code = bl.projfunc_currency_code -- 4764334
9807 					 AND ver.worker_id = g_worker_id
9808 				  ) spread_bl
9809 				   ---- end of third inline view 'spread_bl'...........
9810             	  ,
9811             	    (
9812                       select 1   INVERT_ID from dual union all
9813                       select 2   INVERT_ID from dual where PJI_UTILS.GET_SETUP_PARAMETER('GLOBAL_CURR2_FLAG') = 'Y' union all
9814                       select 4   INVERT_ID from dual union all
9815                       select 8   INVERT_ID from dual union all
9816                       select 16  INVERT_ID from dual where PJI_UTILS.GET_SETUP_PARAMETER('TXN_CURR_FLAG') = 'Y'
9817                     ) invert
9818 				)  collapse_bl	-- WHERE wbs_element_id = 7474 -- and rbs_element_id = 10266 -- and budget_version_id = 2909
9819 				----  End of second inline view 'collapse_bl' ..........
9820 			GROUP BY
9821               collapse_bl.PROJECT_ID
9822 			, collapse_bl.time_id
9823             , collapse_bl.WBS_ELEMENT_ID
9824             -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
9825             , collapse_bl.RBS_ELEMENT_ID
9826             , collapse_bl.RBS_STRUCT_VERSION_ID
9827 			, collapse_bl.plan_type_id
9828             , collapse_bl.plan_version_id
9829             , collapse_bl.PROJECT_ORGANIZATION_ID
9830             , collapse_bl.PROJECT_ORG_ID
9831             , collapse_bl.RESOURCE_ASSIGNMENT_ID
9832             , collapse_bl.BILLABLE_FLAG
9833             , collapse_bl.RESOURCE_CLASS
9834             , collapse_bl.TIME_PHASED_TYPE_CODE
9835             , collapse_bl.CURRENCY_CODE
9836             , collapse_bl.start_date
9837             , collapse_bl.end_date
9838             , collapse_bl.row_id
9839 			, collapse_bl.line_type
9840 			, collapse_bl.calendar_type
9841 			, collapse_bl.period_type_id
9842 	    ,collapse_bl.rate
9843 	    ,collapse_bl.rate2
9844            , collapse_bl.plan_type_code  /*4471527 */
9845             , collapse_bl.WP_FLAG
9846        ) plr
9847 				----  End of first inline view plr ..........
9848 	  WHERE 1=1
9849           -- AND plr.CURR_RECORD_TYPE IS NOT NULL
9850           AND plr.currency_code IS NOT NULL
9851        GROUP BY
9852          plr.PROJECT_ID
9853        , plr.PROJECT_ORG_ID
9854        , plr.project_ORGANIZATION_ID
9855        , plr.WBS_ELEMENT_ID
9856        , plr.time_id
9857        , plr.period_type_id
9858        , plr.CALENDAR_TYPE
9859        , plr.CURR_RECORD_TYPE  -- curr code missing.
9860   	   , plr.currency_code
9861        , plr.RBS_ELEMENT_ID
9862        , plr.RBS_STRUCT_VERSION_ID
9863        , plr.plan_version_id
9864   	 , plr.plan_type_id
9865        , plr.start_date
9866   	 , plr.end_date
9867 	  , plr.line_type
9868 	  ,decode(sign(plr.rate),-1,'Y',NULL) ||decode(sign(plr.rate2),-1,'Y',NULL)
9869        ,plr.plan_type_code   /*4471527 */
9870   )
9871  GROUP BY
9872 	 WORKER_ID
9873 	,PROJECT_ID
9874 	,PROJECT_ORG_ID
9875 	,project_ORGANIZATION_ID
9876 	,WBS_ELEMENT_ID
9877 	,time_id
9878 	,period_type_id
9879 	,CALENDAR_TYPE
9880 	,RBS_AGGR_LEVEL
9881 	,WBS_ROLLUP_FLAG
9882 	,PRG_ROLLUP_FLAG
9883 	,CURR_RECORD_TYPE
9884 	,currency_code
9885 	,RBS_ELEMENT_ID
9886 	,RBS_STRUCT_VERSION_ID
9887 	,plan_version_id
9888 	,plan_type_id
9889         ,LINE_TYPE
9890         ,rate_dangling_flag
9891         ,time_dangling_flag
9892         ,start_date
9893         ,end_date
9894         ,prg_level
9895         ,plan_type_code ;    /*4471527 */
9896 
9897   print_time ( ' EXTRACT_PLAN_AMTS_PRIRBS_GLC12 end. Inserted rows # is: ' || SQL%ROWCOUNT );
9898 EXCEPTION
9899   WHEN OTHERS THEN
9900     print_time('EXTRACT_PLAN_AMTS_PRIRBS_GLC12 : Exception ' || SQLERRM );
9901     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
9902                              p_procedure_name => 'EXTRACT_PLAN_AMTS_PRIRBS_GLC12');
9903     RAISE;
9904 END;
9905 
9906 PROCEDURE EXTRACT_DANGL_REVERSAL IS
9907 BEGIN
9908 
9909  print_time ( ' EXTRACT_DANGL_REVERSAL begin. ' );
9910 
9911  INSERT INTO PJI_FP_AGGR_PJP1
9912     (
9913          WORKER_ID
9914        , PROJECT_ID
9915        , PROJECT_ORG_ID
9916        , PROJECT_ORGANIZATION_ID
9917        , PROJECT_ELEMENT_ID
9918        , TIME_ID
9919        , PERIOD_TYPE_ID
9920        , CALENDAR_TYPE
9921        , RBS_AGGR_LEVEL
9922        , WBS_ROLLUP_FLAG
9923        , PRG_ROLLUP_FLAG
9924        , CURR_RECORD_TYPE_ID
9925        , CURRENCY_CODE
9926        , RBS_ELEMENT_ID
9927        , RBS_VERSION_ID
9928        , PLAN_VERSION_ID
9929        , PLAN_TYPE_ID
9930        , RAW_COST
9931        , BRDN_COST
9932        , REVENUE
9933        , BILL_RAW_COST
9934        , BILL_BRDN_COST
9935        , BILL_LABOR_RAW_COST
9936        , BILL_LABOR_BRDN_COST
9937        , BILL_LABOR_HRS
9938        , EQUIPMENT_RAW_COST
9939        , EQUIPMENT_BRDN_COST
9940        , CAPITALIZABLE_RAW_COST
9941        , CAPITALIZABLE_BRDN_COST
9942        , LABOR_RAW_COST
9943        , LABOR_BRDN_COST
9944        , LABOR_HRS
9945        , LABOR_REVENUE
9946        , EQUIPMENT_HOURS
9947        , BILLABLE_EQUIPMENT_HOURS
9948        , SUP_INV_COMMITTED_COST
9949        , PO_COMMITTED_COST
9950        , PR_COMMITTED_COST
9951        , OTH_COMMITTED_COST
9952        , ACT_LABOR_HRS
9953 	   , ACT_EQUIP_HRS
9954 	   , ACT_LABOR_BRDN_COST
9955 	   , ACT_EQUIP_BRDN_COST
9956 	   , ACT_BRDN_COST
9957 	   , ACT_RAW_COST
9958 	   , ACT_REVENUE
9959          , ACT_LABOR_RAW_COST
9960          , ACT_EQUIP_RAW_COST
9961 	   , ETC_LABOR_HRS
9962 	   , ETC_EQUIP_HRS
9963 	   , ETC_LABOR_BRDN_COST
9964 	   , ETC_EQUIP_BRDN_COST
9965 	   , ETC_BRDN_COST
9966          , ETC_RAW_COST
9967          , ETC_LABOR_RAW_COST
9968          , ETC_EQUIP_RAW_COST
9969        , CUSTOM1
9970        , CUSTOM2
9971        , CUSTOM3
9972        , CUSTOM4
9973        , CUSTOM5
9974        , CUSTOM6
9975        , CUSTOM7
9976        , CUSTOM8
9977        , CUSTOM9
9978        , CUSTOM10
9979        , CUSTOM11
9980        , CUSTOM12
9981        , CUSTOM13
9982        , CUSTOM14
9983        , CUSTOM15
9984        , LINE_TYPE
9985        , RATE_DANGLING_FLAG
9986        , TIME_DANGLING_FLAG
9987         , START_DATE
9988         , END_DATE
9989        , PRG_LEVEL
9990        , PLAN_TYPE_CODE   /*4471527 */
9991 	)
9992 SELECT
9993   g_worker_id WORKER_ID
9994  ,fact.PROJECT_ID
9995  ,fact.PROJECT_ORG_ID
9996  ,fact.PROJECT_ORGANIZATION_ID
9997  ,fact.PROJECT_ELEMENT_ID
9998  ,fact.TIME_ID
9999  ,fact.PERIOD_TYPE_ID
10000  ,fact.CALENDAR_TYPE
10001  ,fact.RBS_AGGR_LEVEL
10002  ,fact.WBS_ROLLUP_FLAG
10003  ,fact.PRG_ROLLUP_FLAG
10004  ,fact.CURR_RECORD_TYPE_ID
10005  ,fact.CURRENCY_CODE
10006  ,fact.RBS_ELEMENT_ID
10007  ,fact.RBS_VERSION_ID
10008  ,fact.PLAN_VERSION_ID
10009  ,fact.PLAN_TYPE_ID
10010  ,-fact.RAW_COST
10011  ,-fact.BRDN_COST
10012  ,-fact.REVENUE
10013  ,-fact.BILL_RAW_COST
10014  ,-fact.BILL_BRDN_COST
10015  ,-fact.BILL_LABOR_RAW_COST
10016  ,-fact.BILL_LABOR_BRDN_COST
10017  ,-fact.BILL_LABOR_HRS
10018  ,-fact.EQUIPMENT_RAW_COST
10019  ,-fact.EQUIPMENT_BRDN_COST
10020  ,-fact.CAPITALIZABLE_RAW_COST
10021  ,-fact.CAPITALIZABLE_BRDN_COST
10022  ,-fact.LABOR_RAW_COST
10023  ,-fact.LABOR_BRDN_COST
10024  ,-fact.LABOR_HRS
10025  ,-fact.LABOR_REVENUE
10026  ,-fact.EQUIPMENT_HOURS
10027  ,-fact.BILLABLE_EQUIPMENT_HOURS
10028  ,-fact.SUP_INV_COMMITTED_COST
10029  ,-fact.PO_COMMITTED_COST
10030  ,-fact.PR_COMMITTED_COST
10031  ,-fact.OTH_COMMITTED_COST
10032  ,-fact.ACT_LABOR_HRS
10033  ,-fact.ACT_EQUIP_HRS
10034  ,-fact.ACT_LABOR_BRDN_COST
10035  ,-fact.ACT_EQUIP_BRDN_COST
10036  ,-fact.ACT_BRDN_COST
10037  ,-fact.ACT_RAW_COST
10038  ,-fact.ACT_REVENUE
10039  ,-fact.ACT_LABOR_RAW_COST
10040  ,-fact.ACT_EQUIP_RAW_COST
10041  ,-fact.ETC_LABOR_HRS
10042  ,-fact.ETC_EQUIP_HRS
10043  ,-fact.ETC_LABOR_BRDN_COST
10044  ,-fact.ETC_EQUIP_BRDN_COST
10045  ,-fact.ETC_BRDN_COST
10046  ,-fact.ETC_RAW_COST
10047  ,-fact.ETC_LABOR_RAW_COST
10048  ,-fact.ETC_EQUIP_RAW_COST
10049  ,-fact.CUSTOM1
10050  ,-fact.CUSTOM2
10051  ,-fact.CUSTOM3
10052  ,-fact.CUSTOM4
10053  ,-fact.CUSTOM5
10054  ,-fact.CUSTOM6
10055  ,-fact.CUSTOM7
10056  ,-fact.CUSTOM8
10057  ,-fact.CUSTOM9
10058  ,-fact.CUSTOM10
10059  ,-fact.CUSTOM11
10060  ,-fact.CUSTOM12
10061  ,-fact.CUSTOM13
10062  ,-fact.CUSTOM14
10063  ,-fact.CUSTOM15
10064  ,DECODE(ver.time_phased_type_code,'N',
10065 			DECODE(fact.calendar_type,'A','NTP','CF'),
10066 			fact.calendar_type,'OF','CF') LINE_TYPE
10067  ,NULL RATE_DANGLING_FLAG
10068  ,NULL TIME_DANGLING_FLAG
10069   ,TO_DATE(NULL) START_DATE
10070   ,TO_DATE(NULL) END_DATE
10071  ,g_default_prg_level PRG_LEVEL
10072  , fact.plan_type_code   plan_type_code
10073 FROM
10074     pji_fp_xbs_accum_f fact,
10075     pji_fm_extr_plnver4 ver
10076 WHERE fact.plan_version_id=ver.plan_version_id
10077    AND fact.plan_type_code = ver.plan_type_code   /*4471527 */
10078   AND fact.project_id = ver.project_id
10079   AND NVL(ver.rbs_struct_version_id,-1) = fact.rbs_version_id
10080   AND ver.worker_id = g_worker_id
10081   AND ver.wp_flag = 'N'
10082   AND ver.baselined_flag = 'Y'
10083   AND fact.rbs_aggr_level='L'
10084   AND fact.wbs_rollup_flag='N'
10085   AND fact.PRG_ROLLUP_FLAG ='N'
10086   AND ( (ver.time_phased_type_code='N' AND fact.calendar_type='A')
10087        OR (fact.calendar_type IN ('P','G','E') AND period_type_id=32));
10088 
10089   print_time ( ' EXTRACT_DANGL_REVERSAL end. Inserted rows # is: ' || SQL%ROWCOUNT );
10090 
10091 EXCEPTION
10092   WHEN OTHERS THEN
10093     print_time(' EXTRACT_DANGL_REVERSAL : Exception ' || SQLERRM );
10094     Fnd_Msg_Pub.add_exc_msg( p_pkg_name       => g_package_name ,
10095                              p_procedure_name => 'EXTRACT_DANGL_REVERSAL');
10096     RAISE;
10097 
10098 END;
10099 
10100 
10101 PROCEDURE EXTRACT_ACTUALS(
10102   p_extrn_type        IN   VARCHAR2 := NULL -- 'FULL' or 'INCREMENTAL'
10103 ) IS
10104 
10105   l_max_project_id         NUMBER;
10106 
10107 BEGIN
10108 
10109   print_time ( ' EXTRACT_ACTUALS BEGIN' ) ;
10110 
10111   IF (p_extrn_type NOT IN ('FULL', 'INCREMENTAL')) THEN
10112     RETURN;
10113   END IF;
10114 
10115   INSERT_NTP_CAL_RECORD ( x_max_project_id => l_max_project_id );
10116 
10117   IF (l_max_project_id IS NULL) THEN
10118     RETURN;
10119   END IF;
10120 
10121 
10122   INSERT INTO PJI_FP_AGGR_PJP1
10123     (
10124          WORKER_ID
10125        , PRG_LEVEL
10126        , PROJECT_ID
10127        , PROJECT_ORG_ID
10128        , PROJECT_ORGANIZATION_ID
10129        , PROJECT_ELEMENT_ID
10130        , TIME_ID
10131        , PERIOD_TYPE_ID
10132        , CALENDAR_TYPE
10133        , RBS_AGGR_LEVEL
10134        , WBS_ROLLUP_FLAG
10135        , PRG_ROLLUP_FLAG
10136        , CURR_RECORD_TYPE_ID
10137        , CURRENCY_CODE
10138        , RBS_ELEMENT_ID
10139        , RBS_VERSION_ID
10140        , PLAN_VERSION_ID
10141        , PLAN_TYPE_ID
10142        , RAW_COST
10143        , BRDN_COST
10144        , REVENUE
10145        , BILL_RAW_COST
10146        , BILL_BRDN_COST
10147        , BILL_LABOR_RAW_COST
10148        , BILL_LABOR_BRDN_COST
10149        , BILL_LABOR_HRS
10150        , EQUIPMENT_RAW_COST
10151        , EQUIPMENT_BRDN_COST
10152        , CAPITALIZABLE_RAW_COST
10153        , CAPITALIZABLE_BRDN_COST
10154        , LABOR_RAW_COST
10155        , LABOR_BRDN_COST
10156        , LABOR_HRS
10157        , LABOR_REVENUE
10158        , EQUIPMENT_HOURS
10159        , BILLABLE_EQUIPMENT_HOURS
10160        , SUP_INV_COMMITTED_COST
10161        , PO_COMMITTED_COST
10162        , PR_COMMITTED_COST
10163        , OTH_COMMITTED_COST
10164        , ACT_LABOR_HRS
10165 	 , ACT_EQUIP_HRS
10166 	 , ACT_LABOR_BRDN_COST
10167 	 , ACT_EQUIP_BRDN_COST
10168 	 , ACT_BRDN_COST
10169 	 , ACT_RAW_COST
10170 	 , ACT_REVENUE
10171        , ACT_LABOR_RAW_COST
10172        , ACT_EQUIP_RAW_COST
10173 	 , ETC_LABOR_HRS
10174 	 , ETC_EQUIP_HRS
10175 	 , ETC_LABOR_BRDN_COST
10176 	 , ETC_EQUIP_BRDN_COST
10177 	 , ETC_BRDN_COST
10178        , ETC_RAW_COST
10179        , ETC_LABOR_RAW_COST
10180        , ETC_EQUIP_RAW_COST
10181        , CUSTOM1
10182        , CUSTOM2
10183        , CUSTOM3
10184        , CUSTOM4
10185        , CUSTOM5
10186        , CUSTOM6
10187        , CUSTOM7
10188        , CUSTOM8
10189        , CUSTOM9
10190        , CUSTOM10
10191        , CUSTOM11
10192        , CUSTOM12
10193        , CUSTOM13
10194        , CUSTOM14
10195        , CUSTOM15
10196        ,PLAN_TYPE_CODE   /*4471527 */
10197 	)
10198        -- Get actuals from budget lines for extraction type is full.
10199 	 SELECT /*+ ordered no_merge(plr) */
10200          g_worker_id worker_id
10201        , g_default_prg_level prg_level
10202        , plr.PROJECT_ID
10203        , plr.PROJECT_ORG_ID
10204        , plr.project_ORGANIZATION_ID
10205        , plr.WBS_ELEMENT_ID
10206        , DECODE(plr.time_phased_type_code
10207               , 'N', -1
10208               , pji_time.cal_period_id) time_id
10209        , DECODE(plr.time_phased_type_code
10210                , 'P', 32
10211                , 'G', 32
10212                , 'N', 2048
10213                , -1) period_type_id -- period type id...
10214        , DECODE(plr.time_phased_type_code
10215                , 'P', 'P'
10216                , 'G', 'G'
10217                , 'N', 'A'
10218                , 'X')     --   CALENDAR_TYPE
10219        , 'L' RBS_AGGR_LEVEL
10220        , 'N' WBS_ROLLUP_FLAG
10221        , 'N' PRG_ROLLUP_FLAG
10222        , plr.CURR_RECORD_TYPE  -- curr code missing.
10223        , plr.currency_code
10224        , plr.RBS_ELEMENT_ID
10225        , plr.RBS_STRUCT_VERSION_ID
10226        , plr.BUDGET_VERSION_ID
10227        , plr.plan_type_id plan_type_id
10228        , TO_NUMBER(NULL) RAW_COST
10229        , TO_NUMBER(NULL) BRDN_COST
10230        , TO_NUMBER(NULL) REVENUE
10231        , TO_NUMBER(NULL)  BILL_RAW_COST
10232        , TO_NUMBER(NULL)  BILL_BRDN_COST
10233        , TO_NUMBER(NULL) BILL_LABOR_RAW_COST
10234        , TO_NUMBER(NULL) BILL_LABOR_BRDN_COST
10235        , TO_NUMBER(NULL) BILL_LABOR_HRS
10236        , TO_NUMBER(NULL)  EQUIPMENT_RAW_COST
10237        , TO_NUMBER(NULL)  EQUIPMENT_BRDN_COST
10238        , TO_NUMBER(NULL) CAPITALIZABLE_RAW_COST
10239        , TO_NUMBER(NULL)  CAPITALIZABLE_BRDN_COST
10240        , TO_NUMBER(NULL) LABOR_RAW_COST
10241        , TO_NUMBER(NULL) LABOR_BRDN_COST
10242        , TO_NUMBER(NULL) labor_hrs
10243        , TO_NUMBER(NULL)  LABOR_REVENUE
10244        , TO_NUMBER(NULL) EQUIPMENT_HOURS
10245        , TO_NUMBER(NULL) BILLABLE_EQUIPMENT_HOURS
10246        , TO_NUMBER(NULL) -- TO_NUMBER(NULL)  SUP_INV_COMMITTED_COST
10247        , TO_NUMBER(NULL) -- TO_NUMBER(NULL)  PO_COMMITTED_COST
10248        , TO_NUMBER(NULL) -- TO_NUMBER(NULL)  PR_COMMITTED_COST
10249        , TO_NUMBER(NULL) -- TO_NUMBER(NULL)  OTH_COMMITTED_COST
10250        , SUM ( DECODE (  plr.resource_class
10251                        , 'PEOPLE'
10252                        , DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,TO_NUMBER(NULL))
10253                        , TO_NUMBER(NULL) ) ) ACT_LABOR_HRS
10254        , SUM ( DECODE ( plr.resource_class
10255                       , 'EQUIPMENT'
10256                       , DECODE ( plr.billable_flag, 'Y',plr.actual_quantity,TO_NUMBER(NULL))
10257                       , TO_NUMBER(NULL) ) ) ACT_EQUIP_HOURS
10258        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_BRDN_COST, TO_NUMBER(NULL) ) ) ACT_LABOR_BRDN_COST
10259        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.actual_BRDN_COST, TO_NUMBER(NULL) ) ) ACT_EQUIPMENT_BRDN_COST
10260        , SUM ( plr.actual_brdn_cost ) ACT_BRDN_COST
10261        , SUM ( plr.actual_raw_cost ) ACT_RAW_COST
10262        , SUM ( plr.actual_revenue ) ACT_REVENUE
10263        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_RAW_COST, TO_NUMBER(NULL) ) ) ACT_LABOR_RAW_COST
10264        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.actual_RAW_COST, TO_NUMBER(NULL) ) ) ACT_EQUIPMENT_RAW_COST
10265        , TO_NUMBER(NULL) ETC_LABOR_HRS
10266        , TO_NUMBER(NULL) ETC_EQUIP_HOURS
10267        , TO_NUMBER(NULL) ETC_LABOR_BRDN_COST
10268        , TO_NUMBER(NULL) ETC_EQUIP_BRDN_COST
10269        , TO_NUMBER(NULL) ETC_BRDN_COST
10270        , TO_NUMBER(NULL) ETC_RAW_COST
10271        , TO_NUMBER(NULL) ETC_LABOR_raw_COST
10272        , TO_NUMBER(NULL) ETC_EQUIP_raw_COST
10273        , TO_NUMBER(NULL) CUSTOM1
10274        , TO_NUMBER(NULL) CUSTOM2
10275        , TO_NUMBER(NULL) CUSTOM3
10276        , TO_NUMBER(NULL) CUSTOM4
10277        , TO_NUMBER(NULL) CUSTOM5
10278        , TO_NUMBER(NULL) CUSTOM6
10279        , TO_NUMBER(NULL) CUSTOM7
10280        , TO_NUMBER(NULL) CUSTOM8
10281        , TO_NUMBER(NULL) CUSTOM9
10282        , TO_NUMBER(NULL) CUSTOM10
10283        , TO_NUMBER(NULL) CUSTOM11
10284        , TO_NUMBER(NULL) CUSTOM12
10285        , TO_NUMBER(NULL) CUSTOM13
10286        , TO_NUMBER(NULL) CUSTOM14
10287        , TO_NUMBER(NULL) CUSTOM15
10288        , plr.plan_type_code PLAN_TYPE_CODE   /*4471527 */
10289        FROM
10290        (          ----- First inline view plr .............
10291             SELECT /*+ no_merge(collapse_bl) */
10292               collapse_bl.PROJECT_ID
10293             , collapse_bl.WBS_ELEMENT_ID
10294             , SUM(collapse_bl.CURR_RECORD_TYPE_ID) CURR_RECORD_TYPE
10295             , collapse_bl.RBS_ELEMENT_ID
10296             , collapse_bl.RBS_STRUCT_VERSION_ID
10297 		    , collapse_bl.plan_type_id
10298             , collapse_bl.BUDGET_VERSION_ID
10299             , collapse_bl.PROJECT_ORGANIZATION_ID
10300             , collapse_bl.PROJECT_ORG_ID
10301             , collapse_bl.RESOURCE_ASSIGNMENT_ID
10302             , collapse_bl.BILLABLE_FLAG
10303             , collapse_bl.RESOURCE_CLASS  -- , CALENDAR_TYPE -- , CALENDAR_ID
10304             , collapse_bl.TIME_PHASED_TYPE_CODE
10305             , collapse_bl.CURRENCY_CODE
10306             , MAX(collapse_bl.raw_cost) raw_cost
10307             , MAX(collapse_bl.BRDN_COST) BRDN_COST
10308             , MAX(collapse_bl.revenue) revenue
10309             , MAX(collapse_bl.actual_raw_cost) actual_raw_cost
10310             , MAX(collapse_bl.actual_BRDN_COST) actual_BRDN_COST
10311             , MAX(collapse_bl.actual_revenue) actual_revenue
10312             , MAX(collapse_bl.etc_raw_cost) etc_raw_cost
10313             , MAX(collapse_bl.etc_BRDN_COST) etc_BRDN_COST
10314             , MAX(collapse_bl.etc_revenue) etc_revenue
10315             , MAX(collapse_bl.quantity) quantity
10316             , MAX(collapse_bl.actual_quantity) actual_quantity
10317             , MAX(collapse_bl.etc_quantity) etc_quantity
10318             , collapse_bl.start_date start_date
10319             , collapse_bl.end_date   end_date
10320             , collapse_bl.period_name period_name
10321             , collapse_bl.row_id
10322             , collapse_bl.plan_type_code
10323             FROM
10324               (                  ----- Second inline view 'collapse_bl' begin .............
10325                SELECT /*+ no_merge(spread_bl) */
10326                   spread_bl.row_id row_id
10327                 , spread_bl.PROJECT_ID
10328                 , spread_bl.BUDGET_VERSION_ID
10329                 , spread_bl.RESOURCE_ASSIGNMENT_ID
10330                 , spread_bl.WBS_ELEMENT_ID
10331                 , spread_bl.RBS_ELEMENT_ID
10332                 , spread_bl.WBS_STRUCT_VERSION_ID
10333                 , spread_bl.RBS_STRUCT_VERSION_ID
10334   	 	        , spread_bl.plan_type_id
10335                 , spread_bl.BILLABLE_FLAG
10336                 , spread_bl.RESOURCE_CLASS  -- , spread_bl.CALENDAR_TYPE  -- , pji_time.CALENDAR_ID
10337                 , spread_bl.PROJECT_ORGANIZATION_ID
10338                 , spread_bl.PROJECT_ORG_ID
10339                 , spread_bl.TIME_PHASED_TYPE_CODE
10340                 , DECODE( invert.INVERT_ID
10341                         , 4, spread_bl.func_CURRENCY_CODE
10342                         , 8, spread_bl.PRJ_CURRENCY_CODE
10343                         , 16, spread_bl.TXN_CURRENCY_CODE ) CURRENCY_CODE
10344                 , invert.INVERT_ID CURR_RECORD_TYPE_ID
10345                 , DECODE ( invert.INVERT_ID
10346                          , 4, spread_bl.func_raw_cost
10347                          , 8, spread_bl.PRJ_raw_cost
10348                          , 16, spread_bl.TXN_raw_cost) raw_cost
10349                 , DECODE ( invert.INVERT_ID
10350                          , 4, spread_bl.func_BRDN_COST
10351                          , 8, spread_bl.PRJ_BRDN_COST
10352                          , 16, spread_bl.TXN_BRDN_COST ) BRDN_COST
10353                 , DECODE ( invert.INVERT_ID
10354                          , 4, spread_bl.func_revenue
10355                          , 8, spread_bl.PRJ_revenue
10356                          , 16, spread_bl.TXN_revenue ) revenue
10357 				, DECODE ( invert.INVERT_ID
10358 				         , 4, spread_bl.func_actual_raw_cost
10359 						 , 8, spread_bl.prj_actual_raw_cost
10360 						 , 16, spread_bl.txn_actual_raw_cost ) actual_raw_cost
10361 				, DECODE ( invert.INVERT_ID
10362 				         , 4, spread_bl.func_actual_brdn_cost
10363 						 , 8, spread_bl.prj_actual_brdn_cost
10364 						 , 16, spread_bl.txn_actual_brdn_cost ) actual_brdn_cost
10365 				, DECODE ( invert.INVERT_ID
10366 				         , 4, spread_bl.func_actual_revenue
10367 						 , 8, spread_bl.prj_actual_revenue
10368 						 , 16, spread_bl.txn_actual_revenue ) actual_revenue
10369 				, DECODE ( invert.INVERT_ID
10370 				         , 4, spread_bl.func_etc_raw_cost
10371 						 , 8, spread_bl.prj_etc_raw_cost
10372 						 , 16, spread_bl.txn_etc_raw_cost ) etc_raw_cost
10373 				, DECODE ( invert.INVERT_ID
10374 				         , 4, spread_bl.func_etc_brdn_cost
10375 						 , 8, spread_bl.prj_etc_brdn_cost
10376 						 , 16, spread_bl.txn_etc_brdn_cost ) etc_brdn_cost
10377 				, DECODE ( invert.INVERT_ID
10378 				         , 4, spread_bl.func_etc_revenue
10379 						 , 8, spread_bl.prj_etc_revenue
10380 						 , 16, spread_bl.txn_etc_revenue ) etc_revenue
10381             	, spread_bl.quantity quantity
10382             	, spread_bl.actual_quantity actual_quantity
10383             	, spread_bl.etc_quantity etc_quantity
10384             	, spread_bl.start_date start_date
10385             	, spread_bl.end_date   end_date
10386             	, spread_bl.period_name period_name
10387             	-- , spread_bl.TRACK_AS_LABOR_FLAG track_as_labor_flag
10388                               , spread_bl.plan_type_code   plan_type_code  /*4471527 */
10389                 FROM
10390                   (     ----- Third inline view 'spread_bl'  .............
10391                     SELECT /*+ ordered */
10392                 	  ra.project_id
10393                 	, bl.ROWID row_id
10394                 	, ra.budget_version_id
10395                 	, bl.resource_asSIGNment_id
10396                 	, DECODE(ra.task_id, 0, pevs.proj_element_id, ra.task_id) wbs_element_id
10397                 	, NVL(ra.rbs_element_id, -1)              rbs_element_id
10398                 	, ver.wbs_struct_version_id      wbs_struct_version_id
10399                 	, NVL(ver.rbs_struct_version_id, -1)    rbs_struct_version_id
10400 			, ver.plan_type_id               plan_type_id
10401 			, ra.rate_based_flag             billable_flag
10402 			, ra.resource_class_code         resource_class
10403                 	, bl.txn_currency_code           txn_currency_code
10404                 	, TO_NUMBER(NULL)                              txn_raw_cost
10405                 	, TO_NUMBER(NULL)                              txn_brdn_COST
10406                 	, TO_NUMBER(NULL)                              txn_revenue
10407 			, bl.txn_init_raw_cost           txn_actual_raw_cost
10408 			, bl.txn_init_burdened_cost      txn_actual_brdn_cost
10409 			, bl.txn_init_revenue            txn_actual_revenue
10410 			, TO_NUMBER(NULL)                           txn_etc_raw_cost
10411                   , TO_NUMBER(NULL)                           txn_etc_brdn_cost
10412 			, TO_NUMBER(NULL)                           txn_etc_revenue
10413                 	, bl.project_currency_code          prj_currency_code
10414                 	, TO_NUMBER(NULL)                                 prj_raw_cost
10415                 	, TO_NUMBER(NULL)                                 prj_BRDN_COST
10416                 	, TO_NUMBER(NULL)                                 prj_revenue
10417 			, bl.project_init_raw_cost          prj_actual_raw_cost
10418 			, bl.project_init_burdened_cost     prj_actual_brdn_cost
10419 			, bl.project_init_revenue           prj_actual_revenue
10420 			, TO_NUMBER(NULL)                              prj_etc_raw_cost
10421 			, TO_NUMBER(NULL)                              prj_etc_brdn_cost
10422 			, TO_NUMBER(NULL)                              prj_etc_revenue
10423                 	, bl.projfunc_currency_code         func_currency_code
10424                   , TO_NUMBER(NULL)                                 func_raw_cost
10425                 	, TO_NUMBER(NULL)                                 func_BRDN_COST
10426                 	, TO_NUMBER(NULL)                                 func_revenue
10427 			, bl.init_raw_cost                  func_actual_raw_cost
10428 			, bl.init_burdened_cost             func_actual_brdn_cost
10429 			, bl.init_revenue                   func_actual_revenue
10430 			, TO_NUMBER(NULL)                              func_etc_raw_cost
10431 			, TO_NUMBER(NULL)                              func_etc_brdn_cost
10432 			, TO_NUMBER(NULL)                              func_etc_revenue
10433                 	, 'CAD'        glb1_currency_code
10434                   , TO_NUMBER(NULL)                           glb1_raw_cost
10435                 	, TO_NUMBER(NULL)                           glb1_BRDN_COST
10436                 	, TO_NUMBER(NULL)                           glb1_revenue
10437                 	, 'USD'        glb2_currency_code
10438                   , TO_NUMBER(NULL)                           glb2_raw_cost
10439                 	, TO_NUMBER(NULL)                           glb1_BRDN_COST
10440                 	, TO_NUMBER(NULL)                           glb1_revenue
10441                   , TO_NUMBER(NULL)                              quantity
10442 			, bl.init_quantity               actual_quantity
10443 			, TO_NUMBER(NULL)                           etc_quantity
10444                 	, TO_DATE(NULL)                           start_date
10445                 	, TO_DATE(NULL)                           end_date
10446                 	,  NVL(bl.period_name, 'XXX') period_name
10447                 	, ver.time_phased_type_code time_phased_type_code
10448                 	, ppa.org_id project_org_id
10449                 	, ppa.carrying_out_organization_id project_organization_id
10450                               , ver.plan_type_code   plan_type_code   /* 4471527*/
10451                  FROM
10452                      PJI_FM_EXTR_PLNVER4           ver
10453                    , PA_RESOURCE_ASSIGNMENTS       ra
10454                    , PA_BUDGET_LINES               bl
10455                    , PA_PROJECTS_ALL               ppa
10456                    , PA_PROJ_ELEM_VER_STRUCTURE    pevs
10457                  WHERE
10458                          ra.resource_asSIGNment_id = bl.resource_asSIGNment_id
10459                      AND ra.project_id = ver.PROJECT_ID
10460                      AND ra.budget_version_id = ver.plan_version_id
10461                      AND ver.project_id = ppa.project_id
10462                      AND txn_currency_code IS NOT NULL
10463                      AND bl.project_currency_code IS NOT NULL
10464                      AND bl.projfunc_currency_code IS NOT NULL
10465 			         AND pevs.element_version_id = ver.wbs_struct_version_id
10466 			         AND pevs.project_id = ver.project_id
10467 	 		         AND ver.secondary_rbs_flag = 'N'
10468                      AND p_extrn_type = 'FULL'
10469                      AND ver.worker_id = g_worker_id
10470                      AND ver.time_phased_type_code IN ('P', 'G', 'N')
10471 				  ) spread_bl
10472 				   ---- end of third inline view 'spread_bl'...........
10473             	  ,
10474             	    (
10475                       SELECT 4   INVERT_ID FROM dual UNION ALL
10476                       SELECT 8   INVERT_ID FROM dual UNION ALL
10477                       SELECT 16  INVERT_ID FROM dual where PJI_UTILS.GET_SETUP_PARAMETER('TXN_CURR_FLAG') = 'Y'
10478                     ) invert
10479 				)  collapse_bl
10480 				----  End of second inline view 'collapse_bl' ..........
10481 			GROUP BY
10482               collapse_bl.PROJECT_ID
10483             , collapse_bl.WBS_ELEMENT_ID
10484             -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
10485             , collapse_bl.RBS_ELEMENT_ID
10486             , collapse_bl.RBS_STRUCT_VERSION_ID
10487 			, collapse_bl.plan_type_id
10488             , collapse_bl.BUDGET_VERSION_ID
10489             , collapse_bl.PROJECT_ORGANIZATION_ID
10490             , collapse_bl.PROJECT_ORG_ID
10491             , collapse_bl.RESOURCE_ASSIGNMENT_ID
10492             , collapse_bl.BILLABLE_FLAG
10493             , collapse_bl.RESOURCE_CLASS
10494             , collapse_bl.TIME_PHASED_TYPE_CODE
10495             , collapse_bl.CURRENCY_CODE
10496             , collapse_bl.start_date
10497             , collapse_bl.end_date
10498             , collapse_bl.period_name
10499             , collapse_bl.row_id
10500              ,collapse_bl.plan_type_code
10501        ) plr
10502 				----  End of first inline view plr ..........
10503        , PJI_ORG_EXTR_INFO            orginfo
10504        , pji_time_cal_period_v      pji_time
10505          WHERE  1=1
10506            -- AND    orginfo.projfunc_currency_mau is not TO_NUMBER(NULL
10507            AND    plr.project_org_id           = orginfo.org_id
10508            AND    DECODE(plr.time_phased_type_code
10509                        , 'P', orginfo.pa_calendar_id
10510                        , 'G', orginfo.gl_calendar_id
10511                        , -l_max_project_id )   = pji_time.calendar_id
10512            AND    plr.period_name = pji_time.name
10513        GROUP BY
10514          plr.PROJECT_ID
10515        , plr.PROJECT_ORG_ID
10516        , plr.project_ORGANIZATION_ID
10517        , plr.WBS_ELEMENT_ID
10518        , DECODE(plr.time_phased_type_code
10519               , 'N', -1
10520               , pji_time.cal_period_id)
10521        , DECODE(plr.time_phased_type_code
10522                , 'P', 32
10523                , 'G', 32
10524                , 'N', 2048
10525                , -1) -- period type id...
10526        , DECODE(plr.time_phased_type_code
10527                , 'P', 'P'
10528                , 'G', 'G'
10529                , 'N', 'A'
10530                , 'X')     --   CALENDAR_TYPE
10531        , plr.CURR_RECORD_TYPE  -- curr code missing.
10532   	   , plr.currency_code
10533        , plr.RBS_ELEMENT_ID
10534        , plr.RBS_STRUCT_VERSION_ID
10535        , plr.BUDGET_VERSION_ID
10536   	 , plr.plan_type_id
10537        , plr.PLAN_TYPE_CODE;
10538 
10539 
10540   print_time ( ' EXTRACT_ACTUALS # records is ' || SQL%ROWCOUNT ) ;
10541 
10542   DELETE_NTP_CAL_RECORD ( p_max_project_id => l_max_project_id );
10543 
10544 EXCEPTION
10545   WHEN OTHERS THEN
10546     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
10547                              p_procedure_name => 'EXTRACT_ACTUALS');
10548     RAISE;
10549 END;
10550 
10551 
10552 --
10553 -- Temp fix until time table will have a record for non time phasing.
10554 --
10555 PROCEDURE INSERT_NTP_CAL_RECORD ( x_max_project_id OUT NOCOPY NUMBER ) IS
10556     l_last_update_date     date   := SYSDATE;
10557     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
10558     l_creation_date        date   := SYSDATE;
10559     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
10560     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
10561 
10562     l_start_end_date       DATE   := TRUNC(SYSDATE);
10563 BEGIN
10564 
10565   BEGIN
10566 
10567     SELECT MAX(project_id)
10568     INTO   x_max_project_id
10569     FROM   pji_pjp_proj_batch_map
10570     WHERE  worker_id = g_worker_id;
10571 
10572   EXCEPTION
10573     WHEN NO_DATA_FOUND THEN
10574       RETURN;
10575   END;
10576 
10577   IF (x_max_project_id IS NULL) THEN
10578     RETURN;
10579   END IF;
10580 
10581   print_time (' Inside INSERT_NTP_CAL_RECORD, max project_id is: ' || x_max_project_id);
10582 
10583 
10584   INSERT INTO pji_time_cal_period
10585   (
10586 		CAL_PERIOD_ID,
10587 		CAL_QTR_ID,
10588 		CALENDAR_ID,
10589 		SEQUENCE,
10590 		NAME ,
10591 		START_DATE,
10592 		END_DATE,
10593 		CREATION_DATE,
10594 		LAST_UPDATE_DATE,
10595 		LAST_UPDATED_BY,
10596 		CREATED_BY,
10597 		LAST_UPDATE_LOGIN
10598 )
10599   SELECT  -x_max_project_id   cal_period_id
10600         , -x_max_project_id   cal_qtr_id
10601         , -x_max_project_id   calendar_id
10602         , -x_max_project_id   SEQUENCE
10603         , g_ntp_period_name   name
10604         , l_start_end_date    start_date
10605         , l_start_end_date    end_date
10606         , l_creation_date     creation_date
10607         , l_last_update_date  last_update_date
10608         , l_last_updated_by   last_updated_by
10609         , l_created_by        created_by
10610         , l_last_update_login last_update_login
10611     FROM  DUAL;
10612 
10613     print_time (' INSERT_NTP_CAL_RECORD, # rows inserted is: ' || SQL%ROWCOUNT );
10614 
10615 EXCEPTION
10616   WHEN OTHERS THEN
10617     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
10618                              p_procedure_name => 'INSERT_NTP_CAL_RECORD');
10619     RAISE;
10620 END;
10621 
10622 
10623 PROCEDURE DELETE_NTP_CAL_RECORD ( p_max_project_id IN NUMBER ) IS
10624 BEGIN
10625 
10626     print_time (' DELETE_NTP_CAL_RECORD, max project_id is: ' || p_max_project_id);
10627 
10628     DELETE FROM pji_time_cal_period
10629     WHERE cal_period_id =  -p_max_project_id;
10630 
10631     print_time (' DELETE_NTP_CAL_RECORD, # rows deleted is: ' || SQL%ROWCOUNT );
10632 
10633 EXCEPTION
10634   WHEN OTHERS THEN
10635     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
10636                              p_procedure_name => 'DELETE_NTP_CAL_RECORD');
10637     RAISE;
10638 END;
10639 
10640 
10641 --
10642 -- Processes the plan lines that have not yet been processed through bulk summarization.
10643 --
10644 PROCEDURE PROCESS_PENDING_PLAN_UPDATES(
10645   p_extrn_type    IN         VARCHAR2,
10646   x_return_status OUT NOCOPY VARCHAR2,
10647   x_msg_data      OUT NOCOPY VARCHAR2) IS
10648 
10649   l_max_project_id NUMBER := NULL;
10650   l_extract_etc    VARCHAR2(1) := NULL; -- 4682341
10651 
10652 BEGIN
10653 
10654   PJI_PJP_FP_CURR_WRAP.INIT_ERR_STACK
10655   ( p_package_name   => g_package_name
10656   , x_return_status  => x_return_status );
10657 
10658   IF (p_extrn_type NOT IN ('FULL', 'INCREMENTAL', 'RBS', 'PARTIAL')) THEN
10659     RETURN;
10660   END IF;
10661 
10662   -- 4682341
10663   -- No data found error should not be thrown.
10664   --
10665   BEGIN
10666     SELECT value
10667     INTO   l_extract_etc
10668     FROM   pji_system_parameters
10669     WHERE  name = 'EXTRACT_ETC_FULLLOAD';
10670   EXCEPTION
10671     WHEN OTHERS THEN
10672       l_extract_etc := 'Y';
10673   END;
10674 
10675   IF (l_extract_etc = 'N') THEN
10676     UPDATE pji_system_parameters
10677     SET    value = 'Y'
10678     WHERE  name = 'EXTRACT_ETC_FULLLOAD';
10679   END IF;
10680 
10681   INSERT_NTP_CAL_RECORD ( x_max_project_id => l_max_project_id );
10682 
10683   IF (l_max_project_id IS NULL) THEN
10684     RETURN;
10685   END IF;
10686 
10687 
10688     INSERT INTO PJI_FP_AGGR_PJP1
10689     (
10690          WORKER_ID
10691        , PROJECT_ID
10692        , PROJECT_ORG_ID
10693        , PROJECT_ORGANIZATION_ID
10694        , PROJECT_ELEMENT_ID
10695        , TIME_ID
10696        , PERIOD_TYPE_ID
10697        , CALENDAR_TYPE
10698        , RBS_AGGR_LEVEL
10699        , WBS_ROLLUP_FLAG
10700        , PRG_ROLLUP_FLAG
10701        , CURR_RECORD_TYPE_ID
10702        , CURRENCY_CODE
10703        , RBS_ELEMENT_ID
10704        , RBS_VERSION_ID
10705        , PLAN_VERSION_ID
10706        , PLAN_TYPE_ID
10707        , RAW_COST
10708        , BRDN_COST
10709        , REVENUE
10710        , BILL_RAW_COST
10711        , BILL_BRDN_COST
10712        , BILL_LABOR_RAW_COST
10713        , BILL_LABOR_BRDN_COST
10714        , BILL_LABOR_HRS
10715        , EQUIPMENT_RAW_COST
10716        , EQUIPMENT_BRDN_COST
10717        , CAPITALIZABLE_RAW_COST
10718        , CAPITALIZABLE_BRDN_COST
10719        , LABOR_RAW_COST
10720        , LABOR_BRDN_COST
10721        , LABOR_HRS
10722        , LABOR_REVENUE
10723        , EQUIPMENT_HOURS
10724        , BILLABLE_EQUIPMENT_HOURS
10725        , SUP_INV_COMMITTED_COST
10726        , PO_COMMITTED_COST
10727        , PR_COMMITTED_COST
10728        , OTH_COMMITTED_COST
10729        , ACT_LABOR_HRS
10730 	   , ACT_EQUIP_HRS
10731 	   , ACT_LABOR_BRDN_COST
10732 	   , ACT_EQUIP_BRDN_COST
10733 	   , ACT_BRDN_COST
10734 	   , ACT_RAW_COST
10735 	   , ACT_REVENUE
10736          , ACT_LABOR_RAW_COST
10737          , ACT_EQUIP_RAW_COST
10738 	   , ETC_LABOR_HRS
10739 	   , ETC_EQUIP_HRS
10740 	   , ETC_LABOR_BRDN_COST
10741 	   , ETC_EQUIP_BRDN_COST
10742 	   , ETC_BRDN_COST
10743          , ETC_RAW_COST
10744          , ETC_LABOR_RAW_COST
10745          , ETC_EQUIP_RAW_COST
10746        , CUSTOM1
10747        , CUSTOM2
10748        , CUSTOM3
10749        , CUSTOM4
10750        , CUSTOM5
10751        , CUSTOM6
10752        , CUSTOM7
10753        , CUSTOM8
10754        , CUSTOM9
10755        , CUSTOM10
10756        , CUSTOM11
10757        , CUSTOM12
10758        , CUSTOM13
10759        , CUSTOM14
10760        , CUSTOM15
10761        , LINE_TYPE
10762        , RATE_DANGLING_FLAG
10763        , TIME_DANGLING_FLAG
10764        , PRG_LEVEL
10765        , PLAN_TYPE_CODE   /*4471527 */
10766 	)
10767 	   SELECT /*+ ordered no_merge(plr) */
10768          g_worker_id  WORKER_ID
10769        , plr.PROJECT_ID
10770        , plr.PROJECT_ORG_ID
10771        , plr.project_ORGANIZATION_ID
10772        , plr.WBS_ELEMENT_ID project_element_id
10773        , DECODE(plr.time_phased_type_code
10774               , 'N', -1
10775               , pji_time.cal_period_id)  time_id
10776        , DECODE(plr.time_phased_type_code
10777                , 'P', 32
10778                , 'G', 32
10779                , 'N', 2048
10780                , -1) period_type_id -- period type id...
10781        , DECODE(plr.time_phased_type_code
10782                , 'P', 'P'
10783                , 'G', 'G'
10784                , 'N', 'A'
10785                , 'X')    CALENDAR_TYPE
10786        , 'L' RBS_AGGR_LEVEL
10787        , 'N' WBS_ROLLUP_FLAG
10788        , 'N' PRG_ROLLUP_FLAG
10789        , plr.CURR_RECORD_TYPE  CURR_RECORD_TYPE_id
10790        , plr.currency_code
10791        , plr.RBS_ELEMENT_ID
10792        , plr.RBS_STRUCT_VERSION_ID  RBS_VERSION_ID
10793        , plr.BUDGET_VERSION_ID plan_version_id
10794        , plr.plan_type_id
10795        , SUM(plr.RAW_COST) RAW_COST
10796        , SUM(plr.BRDN_COST) BRDN_COST
10797        , SUM(plr.REVENUE) REVENUE
10798        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )  BILL_RAW_COST
10799        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )   BILL_BRDN_COST
10800        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YPEOPLE', plr.raw_cost, 0 ) )  BILL_LABOR_RAW_COST
10801        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YPEOPLE' , plr.BRDN_COST, 0 ) )   BILL_LABOR_BRDN_COST
10802        -- , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'YPEOPLE', plr.quantity, 0 ) )  BILL_LABOR_HRS -- bug 6039785
10803        , SUM ( DECODE ( plr.billable_flag || plr.resource_class , 'YPEOPLE',
10804                         DECODE ( plr.billable_flag ,'Y', plr.quantity, 0 ), 0 ) )  BILL_LABOR_HRS -- bug 6039785
10805        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.raw_cost, 0 ) )  EQUIPMENT_RAW_COST
10806        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.BRDN_COST, 0 ) )   EQUIPMENT_BRDN_COST
10807        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.raw_cost, 0 ) )      CAPITALIZABLE_RAW_COST
10808        , SUM ( DECODE ( plr.billable_flag, 'Y', plr.BRDN_COST, 0 ) )      CAPITALIZABLE_BRDN_COST
10809        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.raw_cost, 0 ) )  LABOR_RAW_COST
10810        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.BRDN_COST, 0 ) )   LABOR_BRDN_COST
10811        -- , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.quantity, 0 ) )   labor_hrs -- bug 6039785
10812        , SUM ( DECODE ( plr.resource_class, 'PEOPLE',
10813                         DECODE(plr.billable_flag , 'Y' , plr.quantity, 0 ),
10814 							  0 ) )   labor_hrs -- bug 6039785
10815        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.revenue, 0 ) )  LABOR_REVENUE
10816        /* , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.quantity, 0 ) )  EQUIPMENT_HOURS */ -- bug 6039785
10817        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT',
10818                         DECODE(plr.billable_flag , 'Y' , plr.quantity, 0 ),
10819 			                                  0 ) )  EQUIPMENT_HOURS -- bug 6039785
10820        /* , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YEQUIPMENT', plr.quantity, 0 ) )  BILLABLE_EQUIPMENT_HOURS */ -- bug 6039785
10821        , SUM ( DECODE ( plr.billable_flag || plr.resource_class, 'YEQUIPMENT',
10822                         DECODE ( plr.billable_flag ,'Y', plr.quantity, 0 ), 0 ) )  BILLABLE_EQUIPMENT_HOURS -- bug 6039785
10823        , TO_NUMBER(NULL)                SUP_INV_COMMITTED_COST
10824        , TO_NUMBER(NULL)                PO_COMMITTED_COST
10825        , TO_NUMBER(NULL)                PR_COMMITTED_COST
10826        , TO_NUMBER(NULL)                OTH_COMMITTED_COST
10827        --, SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_quantity, 0 ) ) ACT_LABOR_HRS -- bug 6039785
10828        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', DECODE ( plr.billable_flag, 'Y', plr.actual_quantity , 0 ),
10829                                                                                 0 ) ) ACT_LABOR_HRS -- bug 6039785
10830        /* , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.actual_quantity, 0 ) ) ACT_EQUIP_HRS */ -- bug 6039785
10831        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', DECODE ( plr.billable_flag, 'Y', plr.actual_quantity, 0 ),
10832                                                                                 0 ) ) ACT_EQUIP_HRS -- bug 6039785
10833        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_BRDN_COST, 0 ) ) ACT_LABOR_BRDN_COST
10834        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.actual_BRDN_COST, 0 ) ) ACT_EQUIP_BRDN_COST
10835        , SUM ( plr.actual_brdn_cost ) ACT_BRDN_COST
10836        , SUM ( plr.actual_raw_cost ) ACT_RAW_COST
10837        , SUM ( plr.actual_revenue ) ACT_REVENUE
10838        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.actual_RAW_COST, 0 ) ) ACT_LABOR_RAW_COST
10839        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.actual_RAW_COST, 0 ) ) ACT_EQUIP_RAW_COST
10840        --, SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.etc_quantity, 0 ) ) ETC_LABOR_HRS -- bug 6039785
10841        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', DECODE ( plr.billable_flag, 'Y', plr.etc_quantity, 0 ),
10842                                                                                 0 ) )  ETC_LABOR_HRS -- bug 6039785
10843        /* , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.etc_quantity, 0 ) ) ETC_EQUIP_HRS */ -- bug 6039785
10844        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', DECODE ( plr.billable_flag, 'Y', plr.etc_quantity, 0 ),
10845                                                                                 0 ) ) ETC_EQUIP_HRS -- bug 6039785
10846        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.etc_BRDN_COST, 0 ) ) ETC_LABOR_BRDN_COST
10847        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT' , plr.etc_BRDN_COST, 0 ) ) ETC_EQUIP_BRDN_COST
10848        , SUM(plr.etc_BRDN_COST) ETC_BRDN_COST
10849        , SUM(plr.etc_RAW_COST) ETC_RAW_COST
10850        , SUM ( DECODE ( plr.resource_class, 'PEOPLE', plr.etc_raw_cost, 0 ) ) ETC_LABOR_raw_COST
10851        , SUM ( DECODE ( plr.resource_class, 'EQUIPMENT', plr.etc_raw_cost, 0 ) ) ETC_EQUIP_raw_COST
10852        , NULL CUSTOM1
10853        , NULL CUSTOM2
10854        , NULL CUSTOM3
10855        , NULL CUSTOM4
10856        , NULL CUSTOM5
10857        , NULL CUSTOM6
10858        , NULL CUSTOM7
10859        , NULL CUSTOM8
10860        , NULL CUSTOM9
10861        , NULL CUSTOM10
10862        , NULL CUSTOM11
10863        , NULL CUSTOM12
10864        , NULL CUSTOM13
10865        , NULL CUSTOM14
10866        , NULL CUSTOM15
10867        , DECODE(plr.time_phased_type_code, 'P', 'OF', 'G', 'OF', 'N', 'NTR', 'X') LINE_TYPE
10868        , NULL time_dangling_flag
10869        , NULL rate_dangling_flag
10870        , g_default_prg_level prg_level
10871        , plr.plan_type_code plan_type_code   /*4471527 */
10872        FROM
10873        (          ----- First inline view plr .............
10874             SELECT /*+ no_merge(collapse_bl) */
10875               collapse_bl.PROJECT_ID      -- , 1 partition_id
10876             , collapse_bl.WBS_ELEMENT_ID  -- ,  time_id, period_type_id, calendar type.., slice type, rollpu flag...
10877             , SUM(collapse_bl.CURR_RECORD_TYPE_ID) CURR_RECORD_TYPE
10878             , collapse_bl.RBS_ELEMENT_ID
10879             , collapse_bl.RBS_STRUCT_VERSION_ID
10880 		    , collapse_bl.plan_type_id
10881             , collapse_bl.BUDGET_VERSION_ID
10882             , collapse_bl.PROJECT_ORGANIZATION_ID
10883             , collapse_bl.PROJECT_ORG_ID
10884             , collapse_bl.BILLABLE_FLAG
10885             , collapse_bl.RESOURCE_CLASS  -- , CALENDAR_TYPE -- , CALENDAR_ID
10886             , collapse_bl.CURRENCY_CODE
10887             , MAX(collapse_bl.raw_cost) raw_cost
10888             , MAX(collapse_bl.BRDN_COST) BRDN_COST
10889             , MAX(collapse_bl.revenue) revenue
10890             , MAX(collapse_bl.actual_raw_cost) actual_raw_cost
10891             , MAX(collapse_bl.actual_BRDN_COST) actual_BRDN_COST
10892             , MAX(collapse_bl.actual_revenue) actual_revenue
10893             , MAX(collapse_bl.etc_raw_cost) etc_raw_cost
10894             , MAX(collapse_bl.etc_BRDN_COST) etc_BRDN_COST
10895             , MAX(collapse_bl.quantity) quantity
10896             , MAX(collapse_bl.actual_quantity) actual_quantity
10897             , MAX(collapse_bl.etc_quantity) etc_quantity
10898             , collapse_bl.period_name period_name  -- , TRACK_AS_LABOR_FLAG track_as_labor_flag
10899             , collapse_bl.row_id
10900             , collapse_bl.plan_type_code   /*4471527 */
10901             , collapse_bl.time_phased_type_code
10902             FROM
10903               (                  ----- Second inline view 'collapse_bl' begin .............
10904                SELECT /*+ no_merge(spread_bl) */
10905                   spread_bl.row_id row_id
10906                 , spread_bl.PROJECT_ID
10907                 , spread_bl.BUDGET_VERSION_ID
10908                 , spread_bl.WBS_ELEMENT_ID
10909                 , spread_bl.RBS_ELEMENT_ID
10910                 , spread_bl.WBS_STRUCT_VERSION_ID
10911                 , spread_bl.RBS_STRUCT_VERSION_ID
10912   	 	        , spread_bl.plan_type_id
10913                 , spread_bl.BILLABLE_FLAG
10914                 , spread_bl.RESOURCE_CLASS  -- , spread_bl.CALENDAR_TYPE  -- , pji_time.CALENDAR_ID
10915                 , spread_bl.PROJECT_ORGANIZATION_ID
10916                 , spread_bl.PROJECT_ORG_ID
10917                 , DECODE( invert.INVERT_ID
10918                         , 4, spread_bl.func_CURRENCY_CODE
10919                         , 8, spread_bl.PRJ_CURRENCY_CODE
10920                         , 16, spread_bl.TXN_CURRENCY_CODE ) CURRENCY_CODE
10921                 , invert.INVERT_ID CURR_RECORD_TYPE_ID
10922                 , DECODE ( invert.INVERT_ID
10923                          , 4, spread_bl.func_raw_cost
10924                          , 8, spread_bl.PRJ_raw_cost
10925                          , 16, spread_bl.TXN_raw_cost) raw_cost
10926                 , DECODE ( invert.INVERT_ID
10927                          , 4, spread_bl.func_BRDN_COST
10928                          , 8, spread_bl.PRJ_BRDN_COST
10929                          , 16, spread_bl.TXN_BRDN_COST ) BRDN_COST
10930                 , DECODE ( invert.INVERT_ID
10931                          , 4, spread_bl.func_revenue
10932                          , 8, spread_bl.PRJ_revenue
10933                          , 16, spread_bl.TXN_revenue ) revenue
10934                 , DECODE ( invert.INVERT_ID
10935                          , 4, spread_bl.func_actual_raw_cost
10936                          , 8, spread_bl.prj_actual_raw_cost
10937                          , 16, spread_bl.txn_actual_raw_cost ) actual_raw_cost
10938 				, DECODE ( invert.INVERT_ID
10939 				         , 4, spread_bl.func_actual_brdn_cost
10940 						 , 8, spread_bl.prj_actual_brdn_cost
10941 						 , 16, spread_bl.txn_actual_brdn_cost ) actual_brdn_cost
10942 				, DECODE ( invert.INVERT_ID
10943 				         , 4, spread_bl.func_actual_revenue
10944 						 , 8, spread_bl.prj_actual_revenue
10945 						 , 16, spread_bl.txn_actual_revenue ) actual_revenue
10946 				, DECODE ( invert.INVERT_ID
10947 				         , 4, spread_bl.func_etc_raw_cost
10948 						 , 8, spread_bl.prj_etc_raw_cost
10949 						 , 16, spread_bl.txn_etc_raw_cost ) etc_raw_cost
10950 				, DECODE ( invert.INVERT_ID
10951 				         , 4, spread_bl.func_etc_brdn_cost
10952 						 , 8, spread_bl.prj_etc_brdn_cost
10953 						 , 16, spread_bl.txn_etc_brdn_cost ) etc_brdn_cost
10954             	, spread_bl.quantity quantity
10955             	, spread_bl.actual_quantity actual_quantity
10956             	, spread_bl.etc_quantity etc_quantity
10957             	, spread_bl.period_name period_name
10958                               , spread_bl.plan_type_code   plan_type_code   /*4471527 */
10959                 , spread_bl.time_phased_type_code
10960                 FROM
10961                   (     ----- Third inline view 'spread_bl'  .............
10962                   SELECT /*+ ordered */
10963                 	  bl.project_id
10964                 	, bl.ROWID row_id
10965                 	, bl.plan_version_id budget_version_id
10966                 	, DECODE(bl.project_element_id, 0, pevs.proj_element_id, bl.project_element_id) wbs_element_id
10967                 	, NVL(bl.rbs_element_id, -1)   rbs_element_id
10968                 	, bl.struct_ver_id             wbs_struct_version_id
10969                 	, NVL(ver.rbs_struct_version_id, -1)   rbs_struct_version_id
10970                 	, bl.plan_type_id               plan_type_id -- ver.plan_type_id
10971                   , bl.rate_based_flag              billable_flag -- ra.rate_based_flag             billable_flag
10972                   , bl.resource_class_code          resource_class -- ra.resource_class_code         resource_class
10973                 	, bl.txn_currency_code               txn_currency_code
10974                 	, TO_NUMBER(NULL)                               txn_raw_cost -- bl.txn_raw_cost
10975                 	, TO_NUMBER(NULL)                               txn_BRDN_COST  -- bl.txn_burdened_cost
10976                 	, TO_NUMBER(NULL)                               txn_revenue -- bl.txn_revenue
10977 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_txn_raw_cost)                txn_actual_raw_cost
10978 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_txn_burdened_cost)           txn_actual_brdn_cost
10979 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_txn_revenue)                 txn_actual_revenue
10980 			, DECODE(p_extrn_type||l_extract_etc
10981                             , 'FULLN'
10982                             , TO_NUMBER(NULL)
10983                             , bl.etc_txn_raw_cost)  txn_etc_raw_cost
10984 			, DECODE(p_extrn_type||l_extract_etc
10985                             , 'FULLN'
10986                             , TO_NUMBER(NULL)
10987                             , bl.etc_txn_burdened_cost)  txn_etc_brdn_cost
10988                 	, bl.prj_currency_code               prj_currency_code
10989                 	, TO_NUMBER(NULL)                               prj_raw_cost -- bl.prj_raw_cost
10990                 	, TO_NUMBER(NULL)                               prj_BRDN_COST -- bl.prj_burdened_cost
10991                 	, TO_NUMBER(NULL)                               prj_revenue -- bl.prj_revenue
10992 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_prj_raw_cost) prj_actual_raw_cost
10993 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_prj_burdened_cost)           prj_actual_brdn_cost
10994 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_prj_revenue)                 prj_actual_revenue
10995 			, DECODE(p_extrn_type||l_extract_etc
10996                             , 'FULLN'
10997                             , TO_NUMBER(NULL)
10998                             , bl.etc_prj_raw_cost)  prj_etc_raw_cost
10999 			, DECODE(p_extrn_type||l_extract_etc
11000                             , 'FULLN'
11001                             , TO_NUMBER(NULL)
11002                             , bl.etc_prj_burdened_cost)  prj_etc_brdn_cost
11003                 	, bl.pfc_currency_code               func_currency_code
11004                 	, TO_NUMBER(NULL)                               func_raw_cost -- bl.pfc_raw_cost
11005                 	, TO_NUMBER(NULL)                               func_BRDN_COST -- bl.pfc_burdened_cost
11006                 	, TO_NUMBER(NULL)                               func_revenue -- bl.pfc_revenue
11007 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_pfc_raw_cost)                func_actual_raw_cost
11008 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_pfc_burdened_cost)           func_actual_brdn_cost
11009 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_pfc_revenue)                 func_actual_revenue
11010 			, DECODE(p_extrn_type||l_extract_etc
11011                             , 'FULLN'
11012                             , TO_NUMBER(NULL)
11013                             , bl.etc_pfc_raw_cost)  func_etc_raw_cost
11014 			, DECODE(p_extrn_type||l_extract_etc
11015                             , 'FULLN'
11016                             , TO_NUMBER(NULL)
11017                             , bl.etc_pfc_burdened_cost)  func_etc_brdn_cost
11018                 	, TO_NUMBER(NULL)                               quantity -- bl.quantity
11019 			, DECODE(p_extrn_type, 'FULL', TO_NUMBER(NULL), bl.act_quantity)                    actual_quantity
11020 			, DECODE(p_extrn_type||l_extract_etc
11021                             , 'FULLN'
11022                             , TO_NUMBER(NULL)
11023                             , bl.etc_quantity)  etc_quantity
11024                 	, NVL(bl.period_name, 'XXX')         period_name
11025                 	, bl.project_org_id                  project_org_id
11026                 	, ppa.carrying_out_organization_id   project_organization_id
11027 	, ver.plan_type_code plan_type_code    /*4471527 */
11028                         , ver.time_phased_type_code
11029                 FROM
11030                     PJI_FM_EXTR_PLNVER4           ver
11031                   , PJI_FM_EXTR_PLAN_LINES        bl
11032                   , PA_PROJECTS_ALL               ppa
11033                   , PA_PROJ_ELEM_VER_STRUCTURE    pevs
11034                   WHERE 1=1
11035                                AND ver.wp_flag = 'Y'
11036                                AND bl.project_id = ver.project_id
11037                                AND bl.plan_version_id = ver.plan_version_id
11038                                AND bl.plan_type_id = ver.plan_type_id
11039                                AND ppa.project_id = ver.project_id
11040                                AND bl.project_id = ppa.project_id
11041                                AND bl.TXN_CURRENCY_CODE IS NOT NULL
11042                                AND bl.prj_currency_code IS NOT NULL
11043                                AND bl.pfc_currency_code IS NOT NULL
11044                                AND pevs.element_version_id = ver.wbs_struct_version_id
11045                                AND pevs.project_id = ver.project_id
11046                                AND ver.worker_id = g_worker_id
11047                                AND ver.time_phased_type_code IN ('P', 'G', 'N')
11048 				  ) spread_bl
11049 				   ---- end of third inline view 'spread_bl'...........
11050             	  ,
11051             	    (
11052                       SELECT 4   INVERT_ID FROM dual UNION ALL
11053                       SELECT 8   INVERT_ID FROM dual UNION ALL
11054                       SELECT 16  INVERT_ID FROM dual where PJI_UTILS.GET_SETUP_PARAMETER('TXN_CURR_FLAG') = 'Y'
11055                     ) invert
11056 				)  collapse_bl
11057 				----  End of second inline view 'collapse_bl' ..........
11058 			GROUP BY
11059               collapse_bl.PROJECT_ID
11060             , collapse_bl.WBS_ELEMENT_ID
11061             , collapse_bl.RBS_ELEMENT_ID
11062             , collapse_bl.RBS_STRUCT_VERSION_ID
11063 			, collapse_bl.plan_type_id
11064             , collapse_bl.BUDGET_VERSION_ID
11065             , collapse_bl.PROJECT_ORGANIZATION_ID
11066             , collapse_bl.PROJECT_ORG_ID
11067             , collapse_bl.BILLABLE_FLAG
11068             , collapse_bl.RESOURCE_CLASS
11069             , collapse_bl.CURRENCY_CODE
11070             , collapse_bl.period_name
11071             , collapse_bl.row_id
11072             , collapse_bl.plan_type_code
11073             , collapse_bl.time_phased_type_code
11074        ) plr
11075 				----  End of first inline view plr ..........
11076        , PJI_ORG_EXTR_INFO            orginfo
11077        , pji_time_cal_period_v    pji_time
11078          WHERE  1=1
11079            -- AND    orginfo.projfunc_currency_mau is not NULL
11080            AND    plr.project_org_id         = orginfo.org_id
11081            AND    DECODE(plr.time_phased_type_code
11082                    , 'P', orginfo.pa_calendar_id
11083                    , 'G', orginfo.gl_calendar_id
11084                    , - l_max_project_id ) = pji_time.calendar_id
11085            AND    plr.period_name = pji_time.name
11086        GROUP BY
11087          plr.PROJECT_ID
11088        , plr.PROJECT_ORG_ID
11089        , plr.project_ORGANIZATION_ID
11090        , plr.WBS_ELEMENT_ID
11091        , DECODE(plr.time_phased_type_code
11092               , 'N', -1
11093               , pji_time.cal_period_id)
11094        , DECODE(plr.time_phased_type_code
11095                , 'P', 32
11096                , 'G', 32
11097                , 'N', 2048
11098                , -1) -- period type id...
11099        , DECODE(plr.time_phased_type_code
11100                , 'P', 'P'
11101                , 'G', 'G'
11102                , 'N', 'A'
11103                , 'X')     --   CALENDAR_TYPE
11104        , plr.CURR_RECORD_TYPE  -- curr code missing.
11105   	   , plr.currency_code
11106        , plr.RBS_ELEMENT_ID
11107        , plr.RBS_STRUCT_VERSION_ID
11108        , plr.BUDGET_VERSION_ID
11109   	 , plr.plan_type_id
11110        , DECODE(plr.time_phased_type_code, 'P', 'OF', 'G', 'OF', 'N', 'NTR', 'X')
11111        , plr.plan_type_code ;    /*4471527 */
11112 
11113 
11114   print_time(' # of records from plan lines is : ' || SQL%ROWCOUNT );
11115 
11116 
11117   DELETE_NTP_CAL_RECORD ( p_max_project_id => l_max_project_id );
11118 
11119   POPULATE_RBS_HDR;
11120   print_time(' RBS Header Populated.');
11121 
11122   POPULATE_WBS_HDR;
11123   print_time('Populated new records into WBS Header Table.');
11124 
11125   UPDATE_WBS_HDR; -- To be moved to separate step.
11126   print_time('Updated the WBS header table with min max txn dates.');
11127 
11128   -- DELETE PLAN LINES.
11129 
11130 EXCEPTION
11131   WHEN OTHERS THEN
11132     PJI_PJP_FP_CURR_WRAP.EXCP_HANDLER
11133     ( p_package_name   => g_package_name
11134     , p_procedure_name => ' PROCESS_PENDING_PLAN_UPDATES '
11135     , x_return_status  => x_return_status ) ;
11136 
11137     RAISE;
11138 END PROCESS_PENDING_PLAN_UPDATES;
11139 
11140 -- bug 6063826
11141 PROCEDURE UPDATE_WBS_HDR (p_worker_id in number) is
11142   pragma autonomous_transaction;
11143 
11144     CURSOR wbs_cur IS
11145     SELECT  pjp1.project_id
11146        , pjp1.plan_version_id, NVL(pjp1.plan_type_id, -1) plan_type_id
11147        , pjp1.PLAN_TYPE_CODE
11148        , cal.start_date min_txn_date
11149        , cal.end_date max_txn_date
11150     FROM PJI_FP_AGGR_PJP1    pjp1
11151        , pji_time_cal_period_v   cal
11152    WHERE
11153          pjp1.worker_id = p_worker_id
11154      AND pjp1.time_id = cal.cal_period_id
11155      AND pjp1.calendar_type IN ('P', 'G') ; -- Non time ph and ent cals don't need to be considered.
11156 
11157     TYPE proj_id_tab_type IS TABLE OF pji_pjp_wbs_header.project_id%TYPE;
11158     TYPE plan_ver_id_tab_type IS TABLE OF pji_pjp_wbs_header.plan_version_id%TYPE;
11159     TYPE plan_type_id_tab_type IS TABLE OF pji_pjp_wbs_header.plan_type_id%TYPE;
11160     TYPE plan_type_code_tab_type IS TABLE OF pji_pjp_wbs_header.plan_type_code%TYPE;
11161     TYPE min_txn_date_tab_type IS TABLE OF pji_pjp_wbs_header.min_txn_date%TYPE;
11162     TYPE max_txn_date_tab_type IS TABLE OF pji_pjp_wbs_header.max_txn_date%TYPE;
11163 
11164     proj_id_tab proj_id_tab_TYPE;
11165     plan_ver_id_tab plan_ver_id_tab_TYPE;
11166     plan_type_id_tab plan_type_id_tab_TYPE;
11167     plan_type_code_tab plan_type_code_tab_TYPE;
11168     min_txn_date_tab  min_txn_date_tab_type;
11169     max_txn_date_tab  max_txn_date_tab_type;
11170 
11171 BEGIN
11172 
11173     OPEN wbs_cur;
11174 
11175     LOOP
11176         FETCH wbs_cur BULK COLLECT
11177         INTO proj_id_tab,plan_ver_id_tab,plan_type_id_tab,
11178               plan_type_code_tab,min_txn_date_tab,max_txn_date_tab LIMIT 50000;
11179 
11180         -- EXIT WHEN wbs_cur%NOTFOUND; -- bug 6316433
11181        If proj_id_tab.count > 0 then
11182         FORALL i IN proj_id_tab.FIRST .. proj_id_tab.LAST
11183         UPDATE /*+ index(whdr,PJI_PJP_WBS_HEADER_N1) */
11184         PJI_PJP_WBS_HEADER whdr
11185         SET  MIN_TXN_DATE = LEAST(min_txn_date_tab(i),  NVL(whdr.min_txn_date, min_txn_date_tab(i)))
11186         , MAX_TXN_DATE =  GREATEST(max_txn_date_tab(i),  NVL(whdr.max_txn_date, max_txn_date_tab(i)))
11187         , LAST_UPDATE_DATE = sysdate
11188         , LAST_UPDATED_BY = -9999
11189         , LAST_UPDATE_LOGIN = -9999
11190         WHERE     whdr.plan_version_id = plan_ver_id_tab(i)
11191         AND  whdr.project_id = proj_id_tab(i)
11192         AND  NVL(whdr.plan_type_id, -1) = plan_type_id_tab(i)
11193         AND  whdr.plan_type_code = plan_type_code_tab(i);
11194 
11195         commit;
11196         proj_id_tab.delete;
11197         plan_ver_id_tab.delete;
11198         plan_type_id_tab.delete;
11199         plan_type_code_tab.delete;
11200         min_txn_date_tab.delete;
11201         max_txn_date_tab.delete;
11202         EXIT WHEN wbs_cur%NOTFOUND;  -- bug 6316433
11203        Else
11204 	     Exit;
11205        End if;
11206     END LOOP;
11207     CLOSE wbs_cur;
11208 END UPDATE_WBS_HDR;
11209 
11210 
11211 -- Bug 6316433 : Added batch processing logic
11212 PROCEDURE MERGE_INTO_FP_FACTS IS
11213 
11214     l_last_update_date     date   := SYSDATE;
11215     l_last_updated_by      NUMBER := FND_GLOBAL.USER_ID;
11216     l_creation_date        date   := SYSDATE;
11217     l_created_by           NUMBER := FND_GLOBAL.USER_ID;
11218     l_last_update_login    NUMBER := FND_GLOBAL.LOGIN_ID;
11219 
11220 	TYPE row_id_tab_type   IS TABLE OF rowid index by binary_integer;
11221     x_row_id               row_id_tab_type;
11222 
11223    cursor c1 is
11224       select rowid from pji_fp_aggr_pjp1
11225 	   where worker_id = g_worker_id;
11226 BEGIN
11227 
11228   print_time ( 'SAM MERGE_INTO_FP_FACTS worker id..' || g_worker_id || ' sysdate '  || sysdate) ;
11229 
11230 
11231   open c1;
11232   loop
11233 
11234 	 fetch c1 bulk collect into x_row_id limit 500000;
11235 
11236   If x_row_id.count > 0  then
11237     -- gather statistics for PJI metadata tables
11238     FND_STATS.GATHER_TABLE_STATS(ownname => PJI_UTILS.GET_PJI_SCHEMA_NAME,
11239                                  tabname => 'PJI_FP_AGGR_PJP1',
11240                                  percent => 5,
11241                                  degree  => PJI_UTILS.GET_DEGREE_OF_PARALLELISM());
11242 
11243     -- gather statistics for PJI metadata tables
11244     FND_STATS.GATHER_TABLE_STATS(ownname => PJI_UTILS.GET_PJI_SCHEMA_NAME,
11245                                  tabname => 'PJI_FP_XBS_ACCUM_F',
11246                                  percent => 5,
11247                                  degree  => PJI_UTILS.GET_DEGREE_OF_PARALLELISM());
11248 
11249 
11250 Forall i in x_row_id.first..x_row_id.last
11251 MERGE INTO PJI_FP_XBS_ACCUM_F fact
11252 USING (  SELECT
11253        tmp.worker_id
11254      , tmp.PROJECT_ID
11255      , tmp.PROJECT_ORG_ID
11256      , tmp.PROJECT_ORGANIZATION_ID
11257      , tmp.PROJECT_ELEMENT_ID
11258      , tmp.TIME_ID
11259      , tmp.PERIOD_TYPE_ID
11260      , tmp.CALENDAR_TYPE
11261      , tmp.RBS_AGGR_LEVEL
11262      , tmp.WBS_ROLLUP_FLAG
11263      , tmp.PRG_ROLLUP_FLAG
11264      , tmp.CURR_RECORD_TYPE_ID
11265      , tmp.CURRENCY_CODE
11266      , tmp.RBS_ELEMENT_ID
11267      , tmp.RBS_VERSION_ID
11268      , ver3.PLAN_VERSION_ID
11269      , tmp.PLAN_TYPE_ID
11270      , tmp.RAW_COST
11271      , tmp.BRDN_COST
11272      , tmp.REVENUE
11273      , tmp.BILL_RAW_COST
11274      , tmp.BILL_BRDN_COST
11275      , tmp.BILL_LABOR_RAW_COST
11276      , tmp.BILL_LABOR_BRDN_COST
11277      , tmp.BILL_LABOR_HRS
11278      , tmp.EQUIPMENT_RAW_COST
11279      , tmp.EQUIPMENT_BRDN_COST
11280      , tmp.CAPITALIZABLE_RAW_COST
11281      , tmp.CAPITALIZABLE_BRDN_COST
11282      , tmp.LABOR_RAW_COST
11283      , tmp.LABOR_BRDN_COST
11284      , tmp.LABOR_HRS
11285      , tmp.LABOR_REVENUE
11286      , tmp.EQUIPMENT_HOURS
11287      , tmp.BILLABLE_EQUIPMENT_HOURS
11288      , tmp.SUP_INV_COMMITTED_COST
11289      , tmp.PO_COMMITTED_COST
11290      , tmp.PR_COMMITTED_COST
11291      , tmp.OTH_COMMITTED_COST
11292      , tmp.ACT_LABOR_HRS
11293      , tmp.ACT_EQUIP_HRS
11294      , tmp.ACT_LABOR_BRDN_COST
11295      , tmp.ACT_EQUIP_BRDN_COST
11296      , tmp.ACT_BRDN_COST
11297      , tmp.ACT_RAW_COST
11298      , tmp.ACT_REVENUE
11299      , tmp.ACT_LABOR_RAW_COST
11300      , tmp.ACT_EQUIP_RAW_COST
11301      , DECODE ( ver3.wp_flag
11302                     , 'Y'
11303                     , DECODE(TO_CHAR(tmp.ETC_LABOR_HRS)  -- For Workplan
11304                            , NULL
11305                            , NVL(tmp.labor_hrs, 0)
11306                            , NVL(tmp.ETC_LABOR_HRS, 0)
11307                             )
11308                     , NVL(tmp.ETC_LABOR_HRS, 0)
11309              ) ETC_LABOR_HRS
11310        , DECODE ( ver3.wp_flag
11311                         , 'Y'
11312                         , DECODE(TO_CHAR(tmp.ETC_EQUIP_HRS)
11313                                , NULL
11314                                , NVL(tmp.EQUIPMENT_hours, 0)
11315                                , NVL(tmp.ETC_EQUIP_HRS, 0)
11316                       )
11317                  , NVL(tmp.ETC_EQUIP_HRS, 0)
11318               ) ETC_EQUIP_HRS
11319        , DECODE ( ver3.wp_flag
11320                     , 'Y'
11321                     , DECODE(TO_CHAR(tmp.ETC_LABOR_BRDN_COST)
11322                            , NULL
11323                            , NVL(tmp.labor_BRDN_COST, 0)
11324                            , NVL(tmp.ETC_LABOR_BRDN_COST, 0)
11325                    )
11326                    , NVL(tmp.ETC_LABOR_BRDN_COST, 0)
11327              ) ETC_LABOR_BRDN_COST
11328        , DECODE ( ver3.wp_flag
11329                     , 'Y'
11330                     , DECODE(TO_CHAR(tmp.ETC_EQUIP_BRDN_COST)
11331                            , NULL
11332                            , NVL(tmp.EQUIPment_BRDN_COST, 0)
11333                            , NVL(tmp.ETC_equip_BRDN_COST, 0)
11334                     )
11335                     , NVL(tmp.ETC_EQUIP_BRDN_COST, 0)
11336                 ) ETC_equip_BRDN_COST
11337        , DECODE ( ver3.wp_flag
11338                     , 'Y'
11339                     , DECODE(TO_CHAR(tmp.ETC_BRDN_COST)
11340                            , NULL
11341                            , NVL(tmp.BRDN_COST, 0)
11342                            , NVL(tmp.ETC_BRDN_COST, 0)
11343                     )
11344                   , NVL(tmp.ETC_BRDN_COST, 0)
11345                 ) ETC_BRDN_COST
11346        , DECODE ( ver3.wp_flag
11347                    , 'Y'
11348                    , DECODE(TO_CHAR(tmp.ETC_raw_COST)
11349                           , NULL
11350                           , NVL(tmp.raw_COST, 0)
11351                           , NVL(tmp.ETC_raw_COST, 0)
11352                    )
11353                  , NVL(tmp.ETC_raw_COST, 0)
11354                 ) ETC_raw_COST
11355        , DECODE ( ver3.wp_flag
11356                     , 'Y'
11357                     , DECODE(TO_CHAR(tmp.ETC_labor_raw_COST)
11358                            , NULL
11359                            , NVL(tmp.labor_raw_COST, 0)
11360                            , NVL(tmp.ETC_labor_raw_COST, 0)
11361                     )
11362                   , NVL(tmp.ETC_labor_raw_COST, 0)
11363                 ) ETC_labor_raw_COST
11364        , DECODE ( ver3.wp_flag
11365                     , 'Y'
11366                     , DECODE(TO_CHAR(tmp.ETC_equip_raw_COST)
11367                            , NULL
11368                            , NVL(tmp.equipment_raw_COST, 0)
11369                            ,  NVL(tmp.ETC_equip_raw_COST, 0)
11370                     )
11371                   , NVL(tmp.ETC_equip_raw_COST, 0)
11372               ) ETC_equip_raw_COST
11373      , tmp.CUSTOM1
11374      , tmp.CUSTOM2
11375      , tmp.CUSTOM3
11376      , tmp.CUSTOM4
11377      , tmp.CUSTOM5
11378      , tmp.CUSTOM6
11379      , tmp.CUSTOM7
11380      , tmp.CUSTOM8
11381      , tmp.CUSTOM9
11382      , tmp.CUSTOM10
11383      , tmp.CUSTOM11
11384      , tmp.CUSTOM12
11385      , tmp.CUSTOM13
11386      , tmp.CUSTOM14
11387      , tmp.CUSTOM15
11388      , tmp.PLAN_TYPE_CODE
11389      FROM  pji_fp_aggr_pjp1      tmp
11390          , pji_pjp_wbs_header    ver3  -- replaced ver3 with wbs header for project-to-program association event.
11391      WHERE  1 = 1
11392        AND tmp.worker_id            = g_worker_id
11393        AND tmp.project_id           = ver3.project_id
11394        AND ver3.plan_version_id     = tmp.plan_version_id
11395        AND NVL(tmp.plan_type_id,-1) = NVL(ver3.plan_type_id, -1) -- each plan type can have a different -3, -4 slice.
11396        AND ver3.plan_type_code      = tmp.plan_type_code
11397 	   AND tmp.rowid = x_row_id(i)
11398        AND tmp.RATE_DANGLING_FLAG IS NULL
11399        AND tmp.TIME_DANGLING_FLAG IS NULL
11400       )  pjp1
11401 ON    (pjp1.WORKER_ID               = g_worker_id
11402    AND pjp1.PROJECT_ID              = fact.PROJECT_ID
11403    AND pjp1.PLAN_VERSION_ID         = fact.PLAN_VERSION_ID
11404    AND pjp1.PLAN_TYPE_ID            = fact.PLAN_TYPE_ID
11405    AND pjp1.PLAN_TYPE_CODE          = fact.PLAN_TYPE_CODE
11406    AND pjp1.PROJECT_ORG_ID          = fact.PROJECT_ORG_ID
11407    AND pjp1.PROJECT_ORGANIZATION_ID = fact.PROJECT_ORGANIZATION_ID
11408    AND pjp1.PROJECT_ELEMENT_ID      = fact.PROJECT_ELEMENT_ID
11409    AND pjp1.TIME_ID                 = fact.TIME_ID
11410    AND pjp1.PERIOD_TYPE_ID          = fact.PERIOD_TYPE_ID
11411    AND pjp1.CALENDAR_TYPE           = fact.CALENDAR_TYPE
11412    AND pjp1.RBS_AGGR_LEVEL          = fact.RBS_AGGR_LEVEL
11413    AND pjp1.WBS_ROLLUP_FLAG         = fact.WBS_ROLLUP_FLAG
11414    AND pjp1.PRG_ROLLUP_FLAG         = fact.PRG_ROLLUP_FLAG
11415    AND pjp1.CURR_RECORD_TYPE_ID     = fact.CURR_RECORD_TYPE_ID
11416    AND pjp1.CURRENCY_CODE           = fact.CURRENCY_CODE
11417    AND pjp1.RBS_ELEMENT_ID          = fact.RBS_ELEMENT_ID
11418    AND pjp1.RBS_VERSION_ID          = fact.RBS_VERSION_ID)
11419 WHEN MATCHED THEN
11420 UPDATE
11421 SET      fact.RAW_COST                 = NVL(fact.RAW_COST, 0) + NVL(pjp1.RAW_COST, 0)
11422        , fact.BRDN_COST                = NVL(fact.BRDN_COST, 0) + NVL(pjp1.BRDN_COST, 0)
11423        , fact.REVENUE                  = NVL(fact.REVENUE, 0) + NVL(pjp1.REVENUE, 0)
11424        , fact.BILL_RAW_COST            = NVL(fact.BILL_RAW_COST, 0) + NVL(pjp1.BILL_RAW_COST, 0)
11425        , fact.BILL_BRDN_COST           = NVL(fact.BILL_BRDN_COST, 0) + NVL(pjp1.BILL_BRDN_COST, 0)
11426        , fact.BILL_LABOR_RAW_COST      = NVL(fact.BILL_LABOR_RAW_COST, 0) + NVL(pjp1.BILL_LABOR_RAW_COST, 0)
11427        , fact.BILL_LABOR_BRDN_COST     = NVL(fact.BILL_LABOR_BRDN_COST, 0) + NVL(pjp1.BILL_LABOR_BRDN_COST, 0)
11428        , fact.BILL_LABOR_HRS           = NVL(fact.BILL_LABOR_HRS, 0) + NVL(pjp1.BILL_LABOR_HRS, 0)
11429        , fact.EQUIPMENT_RAW_COST       = NVL(fact.EQUIPMENT_RAW_COST, 0) + NVL(pjp1.EQUIPMENT_RAW_COST, 0)
11430        , fact.EQUIPMENT_BRDN_COST      = NVL(fact.EQUIPMENT_BRDN_COST, 0) + NVL(pjp1.EQUIPMENT_BRDN_COST, 0)
11431        , fact.CAPITALIZABLE_RAW_COST   = NVL(fact.CAPITALIZABLE_RAW_COST, 0) + NVL(pjp1.CAPITALIZABLE_RAW_COST, 0)
11432        , fact.CAPITALIZABLE_BRDN_COST  = NVL(fact.CAPITALIZABLE_BRDN_COST, 0) + NVL(pjp1.CAPITALIZABLE_BRDN_COST, 0)
11433        , fact.LABOR_RAW_COST           = NVL(fact.LABOR_RAW_COST, 0) + NVL(pjp1.LABOR_RAW_COST, 0)
11434        , fact.LABOR_BRDN_COST          = NVL(fact.LABOR_BRDN_COST, 0) + NVL(pjp1.LABOR_BRDN_COST, 0)
11435        , fact.LABOR_HRS                = NVL(fact.LABOR_HRS, 0) + NVL(pjp1.LABOR_HRS, 0)
11436        , fact.LABOR_REVENUE            = NVL(fact.LABOR_REVENUE, 0) + NVL(pjp1.LABOR_REVENUE, 0)
11437        , fact.EQUIPMENT_HOURS          = NVL(fact.EQUIPMENT_HOURS, 0) + NVL(pjp1.EQUIPMENT_HOURS, 0)
11438        , fact.BILLABLE_EQUIPMENT_HOURS = NVL(fact.BILLABLE_EQUIPMENT_HOURS, 0) + NVL(pjp1.BILLABLE_EQUIPMENT_HOURS, 0)
11439        , fact.SUP_INV_COMMITTED_COST   = NVL(fact.SUP_INV_COMMITTED_COST, 0) + NVL(pjp1.SUP_INV_COMMITTED_COST, 0)
11440        , fact.PO_COMMITTED_COST        = NVL(fact.PO_COMMITTED_COST, 0) + NVL(pjp1.PO_COMMITTED_COST, 0)
11441        , fact.PR_COMMITTED_COST        = NVL(fact.PR_COMMITTED_COST, 0) + NVL(pjp1.PR_COMMITTED_COST, 0)
11442        , fact.OTH_COMMITTED_COST       = NVL(fact.OTH_COMMITTED_COST, 0) + NVL(pjp1.OTH_COMMITTED_COST, 0)
11443        , fact.ACT_LABOR_HRS            = NVL(fact.ACT_LABOR_HRS, 0) + NVL(pjp1.ACT_LABOR_HRS, 0)
11444        , fact.ACT_EQUIP_HRS            = NVL(fact.ACT_EQUIP_HRS, 0) + NVL(pjp1.ACT_EQUIP_HRS, 0)
11445        , fact.ACT_LABOR_BRDN_COST      = NVL(fact.ACT_LABOR_BRDN_COST, 0) + NVL(pjp1.ACT_LABOR_BRDN_COST, 0)
11446        , fact.ACT_EQUIP_BRDN_COST      = NVL(fact.ACT_EQUIP_BRDN_COST, 0) + NVL(pjp1.ACT_EQUIP_BRDN_COST, 0)
11447        , fact.ACT_BRDN_COST            = NVL(fact.ACT_BRDN_COST, 0) + NVL(pjp1.ACT_BRDN_COST, 0)
11448        , fact.ACT_RAW_COST             = NVL(fact.ACT_RAW_COST, 0) + NVL(pjp1.ACT_RAW_COST, 0)
11449        , fact.ACT_REVENUE              = NVL(fact.ACT_REVENUE, 0) + NVL(pjp1.ACT_REVENUE, 0)
11450        , fact.ACT_LABOR_RAW_COST       = NVL(fact.ACT_LABOR_RAW_COST, 0) + NVL(pjp1.ACT_LABOR_RAW_COST, 0)
11451        , fact.ACT_EQUIP_RAW_COST       = NVL(fact.ACT_EQUIP_RAW_COST, 0) + NVL(pjp1.ACT_EQUIP_RAW_COST, 0)
11452        , fact.ETC_LABOR_HRS            = NVL(fact.ETC_LABOR_HRS, 0) + NVL(pjp1.ETC_LABOR_HRS, 0)
11453        , fact.ETC_EQUIP_HRS            = NVL(fact.ETC_EQUIP_HRS, 0) + NVL(pjp1.ETC_EQUIP_HRS, 0)
11454        , fact.ETC_LABOR_BRDN_COST      = NVL(fact.ETC_LABOR_BRDN_COST, 0) + NVL(pjp1.ETC_LABOR_BRDN_COST, 0)
11455        , fact.ETC_EQUIP_BRDN_COST      = NVL(fact.ETC_EQUIP_BRDN_COST, 0) + NVL(pjp1.ETC_EQUIP_BRDN_COST, 0)
11456        , fact.ETC_BRDN_COST            = NVL(fact.ETC_BRDN_COST, 0) + NVL(pjp1.ETC_BRDN_COST, 0)
11457        , fact.ETC_RAW_COST             = NVL(fact.ETC_RAW_COST, 0) + NVL(pjp1.ETC_RAW_COST, 0)
11458        , fact.ETC_LABOR_RAW_COST       = NVL(fact.ETC_LABOR_RAW_COST, 0) + NVL(pjp1.ETC_LABOR_RAW_COST, 0)
11459        , fact.ETC_EQUIP_RAW_COST       = NVL(fact.ETC_EQUIP_RAW_COST, 0) + NVL(pjp1.ETC_EQUIP_RAW_COST, 0)
11460        , fact.CUSTOM1                  = NVL(fact.CUSTOM1, 0)  + NVL(pjp1.CUSTOM1, 0)
11461        , fact.CUSTOM2                  = NVL(fact.CUSTOM2, 0)  + NVL(pjp1.CUSTOM2, 0)
11462        , fact.CUSTOM3                  = NVL(fact.CUSTOM3, 0)  + NVL(pjp1.CUSTOM3, 0)
11463        , fact.CUSTOM4                  = NVL(fact.CUSTOM4, 0)  + NVL(pjp1.CUSTOM4, 0)
11464        , fact.CUSTOM5                  = NVL(fact.CUSTOM5, 0)  + NVL(pjp1.CUSTOM5, 0)
11465        , fact.CUSTOM6                  = NVL(fact.CUSTOM6, 0)  + NVL(pjp1.CUSTOM6, 0)
11466        , fact.CUSTOM7                  = NVL(fact.CUSTOM7, 0)  + NVL(pjp1.CUSTOM7, 0)
11467        , fact.CUSTOM8                  = NVL(fact.CUSTOM8, 0)  + NVL(pjp1.CUSTOM8, 0)
11468        , fact.CUSTOM9                  = NVL(fact.CUSTOM9, 0)  + NVL(pjp1.CUSTOM9, 0)
11469        , fact.CUSTOM10                 = NVL(fact.CUSTOM10, 0) + NVL(pjp1.CUSTOM10, 0)
11470        , fact.CUSTOM11                 = NVL(fact.CUSTOM11, 0) + NVL(pjp1.CUSTOM11, 0)
11471        , fact.CUSTOM12                 = NVL(fact.CUSTOM12, 0) + NVL(pjp1.CUSTOM12, 0)
11472        , fact.CUSTOM13                 = NVL(fact.CUSTOM13, 0) + NVL(pjp1.CUSTOM13, 0)
11473        , fact.CUSTOM14                 = NVL(fact.CUSTOM14, 0) + NVL(pjp1.CUSTOM14, 0)
11474        , fact.CUSTOM15                 = NVL(fact.CUSTOM15, 0) + NVL(pjp1.CUSTOM15, 0)
11475        , fact.last_update_date         = SYSDATE
11476        , fact.last_updated_by          = l_last_updated_by
11477        , fact.last_update_login        = l_last_update_login
11478 WHEN NOT MATCHED THEN
11479 INSERT (
11480        fact.PROJECT_ID
11481      , fact.PROJECT_ORG_ID
11482      , fact.PROJECT_ORGANIZATION_ID
11483      , fact.PROJECT_ELEMENT_ID
11484      , fact.TIME_ID
11485      , fact.PERIOD_TYPE_ID
11486      , fact.CALENDAR_TYPE
11487      , fact.RBS_AGGR_LEVEL
11488      , fact.WBS_ROLLUP_FLAG
11489      , fact.PRG_ROLLUP_FLAG
11490      , fact.CURR_RECORD_TYPE_ID
11491      , fact.CURRENCY_CODE
11492      , fact.RBS_ELEMENT_ID
11493      , fact.RBS_VERSION_ID
11494      , fact.PLAN_VERSION_ID
11495      , fact.PLAN_TYPE_ID
11496      , fact.LAST_UPDATE_DATE
11497      , fact.LAST_UPDATED_BY
11498      , fact.CREATION_DATE
11499      , fact.CREATED_BY
11500      , fact.LAST_UPDATE_LOGIN
11501      , fact.RAW_COST
11502      , fact.BRDN_COST
11503      , fact.REVENUE
11504      , fact.BILL_RAW_COST
11505      , fact.BILL_BRDN_COST
11506      , fact.BILL_LABOR_RAW_COST
11507      , fact.BILL_LABOR_BRDN_COST
11508      , fact.BILL_LABOR_HRS
11509      , fact.EQUIPMENT_RAW_COST
11510      , fact.EQUIPMENT_BRDN_COST
11511      , fact.CAPITALIZABLE_RAW_COST
11512      , fact.CAPITALIZABLE_BRDN_COST
11513      , fact.LABOR_RAW_COST
11514      , fact.LABOR_BRDN_COST
11515      , fact.LABOR_HRS
11516      , fact.LABOR_REVENUE
11517      , fact.EQUIPMENT_HOURS
11518      , fact.BILLABLE_EQUIPMENT_HOURS
11519      , fact.SUP_INV_COMMITTED_COST
11520      , fact.PO_COMMITTED_COST
11521      , fact.PR_COMMITTED_COST
11522      , fact.OTH_COMMITTED_COST
11523      , fact.ACT_LABOR_HRS
11524      , fact.ACT_EQUIP_HRS
11525      , fact.ACT_LABOR_BRDN_COST
11526      , fact.ACT_EQUIP_BRDN_COST
11527      , fact.ACT_BRDN_COST
11528      , fact.ACT_RAW_COST
11529      , fact.ACT_REVENUE
11530      , fact.ACT_LABOR_RAW_COST
11531      , fact.ACT_EQUIP_RAW_COST
11532      , fact.ETC_LABOR_HRS
11533      , fact.ETC_EQUIP_HRS
11534      , fact.ETC_LABOR_BRDN_COST
11535      , fact.ETC_EQUIP_BRDN_COST
11536      , fact.ETC_BRDN_COST
11537      , fact.ETC_RAW_COST
11538      , fact.ETC_LABOR_RAW_COST
11539      , fact.ETC_EQUIP_RAW_COST
11540      , fact.CUSTOM1
11541      , fact.CUSTOM2
11542      , fact.CUSTOM3
11543      , fact.CUSTOM4
11544      , fact.CUSTOM5
11545      , fact.CUSTOM6
11546      , fact.CUSTOM7
11547      , fact.CUSTOM8
11548      , fact.CUSTOM9
11549      , fact.CUSTOM10
11550      , fact.CUSTOM11
11551      , fact.CUSTOM12
11552      , fact.CUSTOM13
11553      , fact.CUSTOM14
11554      , fact.CUSTOM15
11555      , fact.PLAN_TYPE_CODE
11556   )
11557 VALUES (
11558        pjp1.PROJECT_ID
11559      , pjp1.PROJECT_ORG_ID
11560      , pjp1.PROJECT_ORGANIZATION_ID
11561      , pjp1.PROJECT_ELEMENT_ID
11562      , pjp1.TIME_ID
11563      , pjp1.PERIOD_TYPE_ID
11564      , pjp1.CALENDAR_TYPE
11565      , pjp1.RBS_AGGR_LEVEL
11566      , pjp1.WBS_ROLLUP_FLAG
11567      , pjp1.PRG_ROLLUP_FLAG
11568      , pjp1.CURR_RECORD_TYPE_ID
11569      , pjp1.CURRENCY_CODE
11570      , pjp1.RBS_ELEMENT_ID
11571      , pjp1.RBS_VERSION_ID
11572      , pjp1.PLAN_VERSION_ID
11573      , pjp1.PLAN_TYPE_ID
11574      , sysdate
11575      , l_last_updated_by
11576      , sysdate
11577      , l_created_by
11578      , l_last_update_login
11579      , pjp1.RAW_COST
11580      , pjp1.BRDN_COST
11581      , pjp1.REVENUE
11582      , pjp1.BILL_RAW_COST
11583      , pjp1.BILL_BRDN_COST
11584      , pjp1.BILL_LABOR_RAW_COST
11585      , pjp1.BILL_LABOR_BRDN_COST
11586      , pjp1.BILL_LABOR_HRS
11587      , pjp1.EQUIPMENT_RAW_COST
11588      , pjp1.EQUIPMENT_BRDN_COST
11589      , pjp1.CAPITALIZABLE_RAW_COST
11590      , pjp1.CAPITALIZABLE_BRDN_COST
11591      , pjp1.LABOR_RAW_COST
11592      , pjp1.LABOR_BRDN_COST
11593      , pjp1.LABOR_HRS
11594      , pjp1.LABOR_REVENUE
11595      , pjp1.EQUIPMENT_HOURS
11596      , pjp1.BILLABLE_EQUIPMENT_HOURS
11597      , pjp1.SUP_INV_COMMITTED_COST
11598      , pjp1.PO_COMMITTED_COST
11599      , pjp1.PR_COMMITTED_COST
11600      , pjp1.OTH_COMMITTED_COST
11601      , pjp1.ACT_LABOR_HRS
11602      , pjp1.ACT_EQUIP_HRS
11603      , pjp1.ACT_LABOR_BRDN_COST
11604      , pjp1.ACT_EQUIP_BRDN_COST
11605      , pjp1.ACT_BRDN_COST
11606      , pjp1.ACT_RAW_COST
11607      , pjp1.ACT_REVENUE
11608      , pjp1.ACT_LABOR_RAW_COST
11609      , pjp1.ACT_EQUIP_RAW_COST
11610      , pjp1.ETC_LABOR_HRS
11611      , pjp1.ETC_EQUIP_HRS
11612      , pjp1.ETC_LABOR_BRDN_COST
11613      , pjp1.ETC_equip_BRDN_COST
11614      , pjp1.ETC_BRDN_COST
11615      , pjp1.ETC_RAW_COST
11616      , pjp1.ETC_LABOR_RAW_COST
11617      , pjp1.ETC_EQUIP_RAW_COST
11618      , pjp1.CUSTOM1
11619      , pjp1.CUSTOM2
11620      , pjp1.CUSTOM3
11621      , pjp1.CUSTOM4
11622      , pjp1.CUSTOM5
11623      , pjp1.CUSTOM6
11624      , pjp1.CUSTOM7
11625      , pjp1.CUSTOM8
11626      , pjp1.CUSTOM9
11627      , pjp1.CUSTOM10
11628      , pjp1.CUSTOM11
11629      , pjp1.CUSTOM12
11630      , pjp1.CUSTOM13
11631      , pjp1.CUSTOM14
11632      , pjp1.CUSTOM15
11633      , pjp1.PLAN_TYPE_CODE
11634        );
11635 
11636   print_time ( 'SAM MERGE_INTO_FP_FACTS worker id..' || g_worker_id || 'row count '  || SQL%ROWCOUNT) ;
11637 
11638  Forall j in x_row_id.first..x_row_id.last
11639      delete from pji_fp_aggr_pjp1
11640 	  where worker_id = g_worker_id
11641 	    and rowid = x_row_id(j);
11642 
11643 	 commit;
11644 
11645 	 x_row_id.delete;
11646 
11647      exit when c1%notfound;
11648 	 Else
11649 	   Exit;
11650 	 End if;
11651 	 end loop;
11652 
11653   close c1;
11654 
11655 EXCEPTION
11656   WHEN OTHERS THEN
11657     FND_MSG_PUB.add_exc_msg( p_pkg_name       => g_package_name ,
11658                              p_procedure_name => 'MERGE_INTO_FP_FACTS');
11659     RAISE;
11660 END MERGE_INTO_FP_FACTS;
11661 
11662 
11663 -- bug 6063826
11664 
11665 BEGIN  --  this portion is executed WHENever the package is initialized
11666 
11667   g_worker_id  := PJI_PJP_FP_CURR_WRAP.GET_WORKER_ID;
11668 
11669 END PJI_FM_PLAN_MAINT_PVT;