DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_FP_CI_MERGE

Source


1 PACKAGE BODY PA_FP_CI_MERGE AS
2 /* $Header: PAFPCIMB.pls 120.26.12020000.5 2013/06/11 12:22:49 bpottipa ship $ */
3 -- Bug Fix: 4569365. Removed MRC code.
4 -- g_mrc_exception EXCEPTION; /* FPB2 */
5 
6 l_module_name VARCHAR2(100) := 'pa.plsql.pa_fp_ci_merge';
7 P_PA_DEBUG_MODE varchar2(1) := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
8 
9 --This record type will contain items for budget version id, ci id and the code which indicates whether
10 --the type of impact that can be implemented into the target version. Bug 3550073
11 --The valid values for impact_type can be
12 ----COST
13 ----REVENUE
14 ----BOTH (If both cost and revenue ci versions can be implemented)
15 ----ALL(If the the ci id has ALL impact and if it can be implemented)
16 ----NONE(Impact can be implemented)
17 --impl_cost_flag and impl_rev_flag will be either Y or N indicating whether the ci id can be implemented in
18 --the budget version id or not
19 TYPE budget_ci_map_rec IS RECORD
20 (budget_version_id            pa_budget_versions.budget_version_id%TYPE
21 ,ci_id                        pa_control_items.ci_id%TYPE
22 ,impact_type_code             VARCHAR2(10)
23 ,impl_cost_flag               VARCHAR2(1)
24 ,impl_rev_flag                VARCHAR2(1));
25 
26 TYPE budget_ci_map_rec_tbl_type IS TABLE OF budget_ci_map_rec
27       INDEX BY BINARY_INTEGER;
28 
29 --This record type will be used in change order merge. This will help in identifying the target resource assignment
30 --id corresponding to a task id and resource list member id in the target. ra_dml_code can be either INSERT or UPDATE.
31 --INSERT indicates that the target resource assignment has to be inserted . UPDATE indicates that the target
32 --resource assignment already exists.Bug 3678314
33 TYPE res_assmt_map_rec IS RECORD
34 (task_id                      pa_tasks.task_id%TYPE
35 ,resource_list_member_id      pa_resource_list_members.resource_list_member_id%TYPE
36 ,resource_assignment_id       pa_resource_assignments.resource_assignment_id%TYPE
37 ,ra_dml_code                  VARCHAR2(30));
38 
39 TYPE res_assmt_map_rec_tbl_type IS TABLE OF res_assmt_map_rec
40       INDEX BY BINARY_INTEGER;
41 
42 --This record type will contain key and a value. A pl/sql tbl of this record type can be declared and it can be
43 --used for different purposes. One such case is : if its required to get the top task id for a task id at many
44 --places in the code then instead of firing a select each time we can fetch it and store in this record. The key
45 --will be the task id and the value will be top task id.
46 --Created for bug 3678314
47 TYPE key_value_rec IS RECORD
48 (key                          NUMBER
49 ,value                        NUMBER);
50 
51 TYPE key_value_rec_tbl_type IS TABLE OF key_value_rec
52       INDEX BY BINARY_INTEGER;
53 
54 --Tables for implement_ci_into_single_ver
55 l_src_targ_task_tbl        key_value_rec_tbl_type;
56 l_res_assmt_map_rec_tbl    res_assmt_map_rec_tbl_type;
57 
58 
59 --End of tables for implement_ci_into_single_ver
60 
61 /*
62   This API will be called when one control item has been
63   merged into another control item or plan version. This
64   API inserts records in the merged control items table
65   and links two control items that have already merged.
66   */
67 
68 PROCEDURE FP_CI_LINK_CONTROL_ITEMS
69 (
70   p_project_id           IN  NUMBER,
71   p_s_fp_version_id      IN  pa_budget_versions.budget_version_id%TYPE,
72   p_t_fp_version_id      IN  pa_budget_versions.budget_version_id%TYPE,
73   p_inclusion_method     IN  VARCHAR2,
74   p_included_by               IN  NUMBER,
75   --Added for bug 3550073
76   p_version_type        IN  pa_budget_versions.version_type%TYPE,
77   p_ci_id               IN  pa_control_items.ci_id%TYPE,
78   p_cost_ppl_qty        IN  pa_fp_merged_ctrl_items.impl_quantity%TYPE,
79   p_rev_ppl_qty         IN  pa_fp_merged_ctrl_items.impl_quantity%TYPE,
80   p_cost_equip_qty      IN  pa_fp_merged_ctrl_items.impl_equipment_quantity%TYPE,
81   p_rev_equip_qty       IN  pa_fp_merged_ctrl_items.impl_equipment_quantity%TYPE,
82   p_impl_pfc_raw_cost   IN  pa_fp_merged_ctrl_items.impl_proj_func_raw_cost%TYPE,
83   p_impl_pfc_revenue    IN  pa_fp_merged_ctrl_items.impl_proj_func_revenue%TYPE,
84   p_impl_pfc_burd_cost  IN  pa_fp_merged_ctrl_items.impl_proj_func_burdened_cost%TYPE,
85   p_impl_pc_raw_cost    IN  pa_fp_merged_ctrl_items.impl_proj_raw_cost%TYPE,
86   p_impl_pc_revenue     IN  pa_fp_merged_ctrl_items.impl_proj_revenue%TYPE,
87   p_impl_pc_burd_cost   IN  pa_fp_merged_ctrl_items.impl_proj_burdened_cost%TYPE,
88   p_impl_agr_revenue    IN  pa_fp_merged_ctrl_items.impl_agr_revenue%TYPE,
89   x_return_status        OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
90   x_msg_count            OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
91   x_msg_data             OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
92  )
93  IS
94 -- Local Variable Declaration
95 
96       l_last_updated_by       NUMBER := FND_GLOBAL.USER_ID;
97       l_created_by            NUMBER := FND_GLOBAL.USER_ID;
98       l_creation_date         DATE   := SYSDATE;
99       l_last_update_date      DATE   := l_creation_date;
100       l_last_update_login   NUMBER := FND_GLOBAL.LOGIN_ID;
101 
102       l_s_ci_id                   pa_budget_versions.ci_id%TYPE;
103       l_included_by          NUMBER := 0;
104       l_party_id             NUMBER := 0;
105       l_debug_mode       VARCHAR2(30);
106       l_module_name         VARCHAR2(100):='PAFPCIMB.FP_CI_LINK_CONTROL_ITEMS';
107 
108 BEGIN
109     IF p_pa_debug_mode = 'Y' THEN
110          pa_debug.init_err_stack('PAFPCIMB.FP_CI_LINK_CONTROL_ITEMS');
111     END IF;
112      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
113      l_debug_mode := NVL(l_debug_mode, 'Y');
114    IF p_pa_debug_mode = 'Y' THEN
115     PA_DEBUG.Set_Curr_Function( p_function   => l_module_name,
116                                 p_debug_mode => l_debug_mode );
117    END IF;
118      x_return_status := FND_API.G_RET_STS_SUCCESS;
119      x_msg_count := 0;
120     --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS - 1');
121     --Check for nulls for mandatory parameters
122     IF p_s_fp_version_id IS NULL THEN
123         PA_UTILS.ADD_MESSAGE
124         ( p_app_short_name => 'PA',
125           p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
126 
127         /*PA_UTILS.ADD_MESSAGE
128           ( p_app_short_name => 'PA',
129             p_msg_name       => 'The source budget version id passed is null');
130           */
131 
132     IF p_pa_debug_mode = 'Y' THEN
133          PA_DEBUG.g_err_stage := 'The source budget version id passed is null';
134          PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
135     END IF;
136           x_return_status := FND_API.G_RET_STS_ERROR;
137           --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS - ***2');
138      IF p_pa_debug_mode = 'Y' THEN
139             PA_DEBUG.Reset_Curr_Function;
140      END IF;
141           RETURN;
142      END IF;
143 
144      IF p_t_fp_version_id IS NULL THEN
145         PA_UTILS.ADD_MESSAGE
146         ( p_app_short_name => 'PA',
147         p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
148           /*PA_UTILS.ADD_MESSAGE
149           ( p_app_short_name => 'PA',
150             p_msg_name       => 'The target budget version id passed is null');
151             */
152     IF p_pa_debug_mode = 'Y' THEN
153         PA_DEBUG.g_err_stage := 'The target budget version id passed is null';
154         PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
155     END IF;
156         x_return_status := FND_API.G_RET_STS_ERROR;
157           --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS - ***3');
158       IF p_pa_debug_mode = 'Y' THEN
159         PA_DEBUG.Reset_Curr_Function;
160       END IF;
161           RETURN;
162      END IF;
163 
164      IF p_project_id IS NULL THEN
165           PA_UTILS.ADD_MESSAGE
166           ( p_app_short_name => 'PA',
167             p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
168           /*PA_UTILS.ADD_MESSAGE
169           ( p_app_short_name => 'PA',
170             p_msg_name       => 'The project id passed is null');
171           */
172     IF p_pa_debug_mode = 'Y' THEN
173              PA_DEBUG.g_err_stage := 'The project id passed is null';
174              PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
175     END IF;
176           x_return_status := FND_API.G_RET_STS_ERROR;
177           --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS -*** 4');
178       IF p_pa_debug_mode = 'Y' THEN
179         PA_DEBUG.Reset_Curr_Function;
180      END IF;
181           RETURN;
182      END IF;
183 
184     IF p_version_type IS NULL  OR
185        p_version_type NOT IN ('COST', 'REVENUE', 'BOTH') THEN
186 
187         IF l_debug_mode = 'Y' THEN
188             pa_debug.g_err_stage:='Invalid p_version_type passed is '||p_version_type;
189             pa_debug.write(L_module_name,pa_debug.g_err_stage,5);
190         END IF;
191         PA_UTILS.ADD_MESSAGE
192           ( p_app_short_name => 'PA',
193             p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
194           x_return_status := FND_API.G_RET_STS_ERROR;
195           --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS -*** 4');
196        IF p_pa_debug_mode = 'Y' THEN
197         PA_DEBUG.Reset_Curr_Function;
198        END IF;
199           RETURN;
200 
201 
202     END IF;
203 
204     -- Get the control item id for the source version
205     -- from the budget version table
206     IF p_ci_id IS NULL THEN
207         BEGIN
208             SELECT
209                   bv.ci_id
210             INTO
211                   l_s_ci_id
212             FROM  pa_budget_versions bv
213             WHERE bv.budget_version_id = p_s_fp_version_id
214             AND   bv.project_id = p_project_id;
215         EXCEPTION
216             WHEN NO_DATA_FOUND THEN
217                 PA_UTILS.ADD_MESSAGE
218                 ( p_app_short_name => 'PA',
219                   p_msg_name       => 'PA_FP_CI_NO_CI_ID_FOUND');
220                 --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS - 5****');
221                 x_return_status := FND_API.G_RET_STS_ERROR;
222         END;
223     ELSE
224         l_s_ci_id := p_ci_id;
225     END IF;
226 
227      IF p_included_by IS NOT NULL THEN
228           l_included_by := p_included_by;
229      ELSE
230           l_included_by := FND_GLOBAL.USER_ID;
231      END IF;
232 
233      BEGIN
234           SELECT hp.party_id
235           INTO
236                l_party_id
237           FROM      fnd_user fu,
238                hz_parties hp
239           WHERE
240              user_id = l_included_by
241              and employee_id IS NOT NULL
242              and hp.orig_system_reference = 'PER:' || fu.employee_id;
243     EXCEPTION
244     WHEN NO_DATA_FOUND THEN
245         SELECT hp.party_id
246         INTO   l_party_id
247         FROM   fnd_user fu,
248                hz_parties hp
249         WHERE
250               user_id = l_included_by
251         AND   employee_id IS NULL
252         -- Bug 4931044: R12 ATG Mandate: Moving customer_id to
253         --   person_party_id in fnd_user
254         AND   hp.party_id =  fu.person_party_id;
255         -- AND   hp.party_id =  fu.customer_id;
256     END;
257 
258      -- Insert in the merged control items table
259      IF p_version_type IN ('COST', 'BOTH') THEN
260         INSERT INTO pa_fp_merged_ctrl_items
261         (
262              PROJECT_ID
263             ,PLAN_VERSION_ID
264             ,CI_ID
265             ,CI_PLAN_VERSION_ID
266             ,RECORD_VERSION_NUMBER
267             ,last_update_date
268             ,last_updated_by
269             ,creation_date
270             ,created_by
271             ,last_update_login
272             ,inclusion_method_code
273             ,included_by_person_id
274             --Included for bug 3550073
275             ,version_type
276             ,impl_proj_func_raw_cost
277             ,impl_proj_func_burdened_cost
278             ,impl_proj_func_revenue
279             ,impl_proj_raw_cost
280             ,impl_proj_burdened_cost
281             ,impl_proj_revenue
282             ,impl_quantity
283             ,impl_equipment_quantity
284             ,impl_agr_revenue
285         )
286         VALUES
287         (
288              p_project_id
289             ,p_t_fp_version_id
290             ,l_s_ci_id
291             ,p_s_fp_version_id
292             ,1
293             ,l_last_update_date
294             ,l_last_updated_by
295             ,l_creation_date
296             ,l_created_by
297             ,l_last_update_login
298             ,p_inclusion_method
299             ,l_party_id
300             --Included for bug 3550073
301             ,'COST'
302             ,p_impl_pfc_raw_cost
303             ,p_impl_pfc_burd_cost
304             ,p_impl_pfc_revenue -- Bug 5845142
305             ,p_impl_pc_raw_cost
306             ,p_impl_pc_burd_cost
307             ,p_impl_pc_revenue -- Bug 5845142
308             ,p_cost_ppl_qty
309             ,p_cost_equip_qty
310             ,NULL
311         );
312     END IF;
313 
314     IF p_version_type IN ('REVENUE', 'BOTH') THEN
315         INSERT INTO pa_fp_merged_ctrl_items
316         (
317              PROJECT_ID
318             ,PLAN_VERSION_ID
319             ,CI_ID
320             ,CI_PLAN_VERSION_ID
321             ,RECORD_VERSION_NUMBER
322             ,last_update_date
323             ,last_updated_by
324             ,creation_date
325             ,created_by
326             ,last_update_login
327             ,inclusion_method_code
328             ,included_by_person_id
329             --Included for bug 3550073
330             ,version_type
331             ,impl_proj_func_raw_cost
332             ,impl_proj_func_burdened_cost
333             ,impl_proj_func_revenue
334             ,impl_proj_raw_cost
335             ,impl_proj_burdened_cost
336             ,impl_proj_revenue
337             ,impl_quantity
338             ,impl_equipment_quantity
339             ,impl_agr_revenue
340         )
341         VALUES
342         (
343              p_project_id
344             ,p_t_fp_version_id
345             ,l_s_ci_id
346             ,p_s_fp_version_id
347             ,1
348             ,l_last_update_date
349             ,l_last_updated_by
350             ,l_creation_date
351             ,l_created_by
352             ,l_last_update_login
353             ,p_inclusion_method
354             ,l_party_id
355             --Included for bug 3550073
356             ,'REVENUE'
357             ,NULL
358             ,NULL
359             ,p_impl_pfc_revenue
360             ,NULL
361             ,NULL
362             ,p_impl_pc_revenue
363             ,p_rev_ppl_qty
364             ,p_rev_equip_qty
365             ,p_impl_agr_revenue
366         );
367 
368     END IF;
369  IF p_pa_debug_mode = 'Y' THEN
370     PA_DEBUG.Reset_Curr_Function;
371 END IF;
372 EXCEPTION
373      WHEN OTHERS THEN
374          FND_MSG_PUB.add_exc_msg
375                   ( p_pkg_name       => 'PA_FP_CI_MERGE.' ||
376                    'fp_ci_link_control_items'
377                    ,p_procedure_name => PA_DEBUG.G_Err_Stack);
378     IF p_pa_debug_mode = 'Y' THEN
379          PA_DEBUG.g_err_stage := 'Unexpected error in fp_ci_link_control_items';
380          PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
381     END IF;
382          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
383          --------dbms_output.put_line('FP_CI_LINK_CONTROL_ITEMS - 6****');
384     IF p_pa_debug_mode = 'Y' THEN
385          PA_DEBUG.Reset_Curr_Function;
386      END IF;
387          RAISE;
388 
389 END FP_CI_LINK_CONTROL_ITEMS;
390 -- end of fp_ci_link_control_items
391 
392 /****************************************************************
393   This API is called to update the estimated amounts for control
394   items budget versions. The main updation in this API is to update
395   the estimated amounts for the target budget version of control item
396   ***************************************************************/
397 
398   PROCEDURE FP_CI_UPDATE_EST_AMOUNTS
399   (
400     p_project_id         IN pa_budget_versions.project_id%TYPE,
401     p_source_version_id       IN pa_budget_versions.budget_version_id%TYPE,
402     p_target_version_id       IN pa_budget_versions.budget_version_id%TYPE,
403     p_merge_unmerge_mode IN VARCHAR2 ,
404     p_commit_flag        IN VARCHAR2 ,
405     p_init_msg_list      IN VARCHAR2 ,
406     p_update_agreement        IN VARCHAR2,
407     x_return_status      OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
408     x_msg_count          OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
409     x_msg_data           OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
410    )
411    IS
412   -- Local Variable Declaration
413 
414         l_last_updated_by     NUMBER := FND_GLOBAL.USER_ID;
415      l_last_update_date       DATE := SYSDATE;
416         l_last_update_login     NUMBER := FND_GLOBAL.LOGIN_ID;
417 
418         l_debug_mode          VARCHAR2(30);
419 
420         l_target_ver_type     pa_budget_versions.version_type%TYPE;
421 
422      l_est_project_raw_cost        pa_budget_versions.est_project_raw_cost%TYPE;
423      l_est_project_burdened_cost   pa_budget_versions.est_project_burdened_cost%TYPE;
424      l_est_project_revenue         pa_budget_versions.est_project_revenue%TYPE;
425      l_est_quantity           pa_budget_versions.est_quantity%TYPE;
426      l_est_projfunc_raw_cost       pa_budget_versions.est_projfunc_raw_cost%TYPE;
427      l_est_projfunc_burdened_cost  pa_budget_versions.est_projfunc_burdened_cost%TYPE;
428      l_est_projfunc_revenue        pa_budget_versions.est_projfunc_revenue%TYPE;
429      l_agreement_id           pa_budget_versions.agreement_id%TYPE;
430     l_est_equipment_quantity  pa_budget_versions.est_equipment_quantity%TYPE;
431 
432   BEGIN
433     IF p_pa_debug_mode = 'Y' THEN
434          pa_debug.init_err_stack('PAFPCIMB.FP_CI_UPDATE_EST_AMOUNTS');
435     END IF;
436      IF NVL(p_init_msg_list,'N') = 'Y' THEN
437                FND_MSG_PUB.initialize;
438      END IF;
439      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
440      l_debug_mode := NVL(l_debug_mode, 'Y');
441    IF p_pa_debug_mode = 'Y' THEN
442      pa_debug.set_process('PLSQL','LOG',l_debug_mode);
443    END IF;
444      x_return_status := FND_API.G_RET_STS_SUCCESS;
445           x_msg_count := 0;
446           --------dbms_output.put_line('FP_CI_UPDATE_EST_AMOUNTS - 1');
447           --Check for nulls for mandatory parameters
448      IF p_source_version_id IS NULL THEN
449           PA_UTILS.ADD_MESSAGE
450           ( p_app_short_name => 'PA',
451             p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
452     IF p_pa_debug_mode = 'Y' THEN
453           PA_DEBUG.g_err_stage := 'The source budget version id passed is null';
454           PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
455     END IF;
456           x_return_status := FND_API.G_RET_STS_ERROR;
457           RETURN;
458      END IF;
459 
460      IF p_target_version_id IS NULL THEN
461           PA_UTILS.ADD_MESSAGE
462           ( p_app_short_name => 'PA',
463             p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
464     IF p_pa_debug_mode = 'Y' THEN
465           PA_DEBUG.g_err_stage := 'The target budget version id passed is null';
466           PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
467     END IF;
468           x_return_status := FND_API.G_RET_STS_ERROR;
469           RETURN;
470      END IF;
471 
472      IF p_project_id IS NULL THEN
473           PA_UTILS.ADD_MESSAGE
474           ( p_app_short_name => 'PA',
475             p_msg_name       => 'PA_FP_CI_NULL_PARAM_PASSED');
476     IF p_pa_debug_mode = 'Y' THEN
477           PA_DEBUG.g_err_stage := 'The project id passed is null';
478           PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
479     END IF;
480           x_return_status := FND_API.G_RET_STS_ERROR;
481           RETURN;
482      END IF;
483 
484           -- Get the target version type
485           BEGIN
486           SELECT bv.version_type
487           INTO l_target_ver_type
488           FROM pa_budget_versions bv
489           WHERE
490           bv.project_id = p_project_id
491           AND bv.budget_version_id = p_target_version_id;
492      EXCEPTION
493           WHEN NO_DATA_FOUND THEN
494               PA_UTILS.ADD_MESSAGE
495                ( p_app_short_name => 'PA',
496                  p_msg_name       => 'PA_FP_CI_NO_VERSION_DATA_FOUND');
497               ------dbms_output.put_line('FP_CI_UPDATE_EST_AMOUNTS - 2***');
498               x_return_status := FND_API.G_RET_STS_ERROR;
499      END;
500 
501      -- Get the estimated amounts for the source version
502 
503      BEGIN
504           SELECT
505                NVL(est_project_raw_cost,0),
506                NVL(est_project_burdened_cost,0),
507                NVL(est_project_revenue,0),
508                NVL(est_quantity,0),
509                NVL(est_projfunc_raw_cost,0),
510                NVL(est_projfunc_burdened_cost,0),
511                NVL(est_projfunc_revenue,0),
512             NVL(est_equipment_quantity,0),
513                agreement_id
514           INTO
515                l_est_project_raw_cost,
516                l_est_project_burdened_cost,
517                l_est_project_revenue,
518                l_est_quantity,
519                l_est_projfunc_raw_cost,
520                l_est_projfunc_burdened_cost,
521                l_est_projfunc_revenue,
522             l_est_equipment_quantity,
523                l_agreement_id
524           FROM PA_BUDGET_VERSIONS bv
525           WHERE
526           bv.project_id = p_project_id
527           AND bv.budget_version_id = p_source_version_id;
528 
529      EXCEPTION
530           WHEN NO_DATA_FOUND THEN
531               PA_UTILS.ADD_MESSAGE
532                ( p_app_short_name => 'PA',
533                  p_msg_name       => 'PA_FP_CI_NO_VERSION_DATA_FOUND');
534               ------dbms_output.put_line('FP_CI_UPDATE_EST_AMOUNTS - 2***');
535               x_return_status := FND_API.G_RET_STS_ERROR;
536      END;
537 
538      -- Update the budget versions table for the target version
539      -- based on version type
540      IF(l_target_ver_type = 'ALL') THEN
541           UPDATE PA_BUDGET_VERSIONS bv
542           SET
543           est_project_raw_cost = NVL(est_project_raw_cost,0) + DECODE
544                                              (p_merge_unmerge_mode,
545                                              'MERGE', l_est_project_raw_cost,
546                                              'UNMERGE', (-1 * l_est_project_raw_cost)),
547           est_project_burdened_cost = NVL(est_project_burdened_cost,0) + DECODE
548                                              (p_merge_unmerge_mode,
549                                              'MERGE', l_est_project_burdened_cost,
550                                              'UNMERGE', (-1 * l_est_project_burdened_cost)),
551           est_project_revenue = NVL(est_project_revenue,0) + DECODE
552                                              (p_merge_unmerge_mode,
553                                              'MERGE', l_est_project_revenue,
554                                              'UNMERGE', (-1 * l_est_project_revenue)),
555           est_quantity = NVL(est_quantity,0) + DECODE
556                                              (p_merge_unmerge_mode,
557                                              'MERGE', l_est_quantity,
558                                              'UNMERGE', (-1 * l_est_quantity)),
559         est_equipment_quantity=NVL(est_equipment_quantity,0) + DECODE
560                                              (p_merge_unmerge_mode,
561                                              'MERGE', l_est_equipment_quantity,
562                                              'UNMERGE', (-1 * l_est_equipment_quantity)),
563           est_projfunc_raw_cost = NVL(est_projfunc_raw_cost,0) + DECODE
564                                              (p_merge_unmerge_mode,
565                                              'MERGE', l_est_projfunc_raw_cost,
566                                              'UNMERGE', (-1 * l_est_projfunc_raw_cost)),
567           est_projfunc_burdened_cost = NVL(est_projfunc_burdened_cost,0) + DECODE
568                                              (p_merge_unmerge_mode,
569                                              'MERGE', l_est_projfunc_burdened_cost,
570                                              'UNMERGE', (-1 * l_est_projfunc_burdened_cost)),
571           est_projfunc_revenue = NVL(est_projfunc_revenue,0) + DECODE
572                                              (p_merge_unmerge_mode,
573                                              'MERGE', l_est_projfunc_revenue,
574                                              'UNMERGE', (-1 * l_est_projfunc_revenue))
575           WHERE
576                bv.project_id = p_project_id
577           AND bv.budget_version_id = p_target_version_id;
578           IF (SQL%ROWCOUNT = 0) THEN
579                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
580                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
581           END IF;
582      ELSIF(l_target_ver_type = 'COST') THEN
583           UPDATE PA_BUDGET_VERSIONS bv
584           SET
585           est_project_raw_cost = NVL(est_project_raw_cost,0) + DECODE
586                                              (p_merge_unmerge_mode,
587                                              'MERGE', l_est_project_raw_cost,
588                                              'UNMERGE', (-1 * l_est_project_raw_cost)),
589           est_project_burdened_cost = NVL(est_project_burdened_cost,0) + DECODE
590                                              (p_merge_unmerge_mode,
591                                              'MERGE', l_est_project_burdened_cost,
592                                              'UNMERGE', (-1 * l_est_project_burdened_cost)),
593           est_quantity = NVL(est_quantity,0) + DECODE
594                                              (p_merge_unmerge_mode,
595                                              'MERGE', l_est_quantity,
596                                              'UNMERGE', (-1 * l_est_quantity)),
597           est_projfunc_raw_cost = NVL(est_projfunc_raw_cost,0) + DECODE
598                                              (p_merge_unmerge_mode,
599                                              'MERGE', l_est_projfunc_raw_cost,
600                                              'UNMERGE', (-1 * l_est_projfunc_raw_cost)),
601           est_projfunc_burdened_cost = NVL(est_projfunc_burdened_cost,0) + DECODE
602                                              (p_merge_unmerge_mode,
603                                              'MERGE', l_est_projfunc_burdened_cost,
604                                              'UNMERGE', (-1 * l_est_projfunc_burdened_cost))
605           WHERE
606                bv.project_id = p_project_id
607           AND bv.budget_version_id = p_target_version_id;
608           IF (SQL%ROWCOUNT = 0) THEN
609                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
610                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
611           END IF;
612      ELSIF(l_target_ver_type = 'REVENUE') THEN
613           UPDATE PA_BUDGET_VERSIONS bv
614           SET
615           est_project_revenue = NVL(est_project_revenue,0) + DECODE
616                                              (p_merge_unmerge_mode,
617                                              'MERGE', l_est_project_revenue,
618                                              'UNMERGE', (-1 * l_est_project_revenue)),
619           est_quantity = NVL(est_quantity,0) + DECODE
620                                              (p_merge_unmerge_mode,
621                                              'MERGE', l_est_quantity,
622                                              'UNMERGE', (-1 * l_est_quantity)),
623           est_projfunc_revenue = NVL(est_projfunc_revenue,0) + DECODE
624                                              (p_merge_unmerge_mode,
625                                              'MERGE', l_est_projfunc_revenue,
626                                              'UNMERGE', (-1 * l_est_projfunc_revenue))
627           WHERE
628                bv.project_id = p_project_id
629           AND bv.budget_version_id = p_target_version_id;
630           IF (SQL%ROWCOUNT = 0) THEN
631                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
632                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
633           END IF;
634      END IF;
635 
636      IF (p_update_agreement = 'Y') THEN
637           UPDATE PA_BUDGET_VERSIONS bv
638           SET
639           agreement_id = l_agreement_id
640           WHERE
641                bv.project_id = p_project_id
642           AND bv.budget_version_id = p_target_version_id;
643           IF (SQL%ROWCOUNT = 0) THEN
644                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
645                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
646           END IF;
647      END IF;
648   IF NVL(p_commit_flag,'N') = 'Y' THEN
649      COMMIT;
650   END IF;
651   EXCEPTION
652      WHEN OTHERS THEN
653          FND_MSG_PUB.add_exc_msg
654                   ( p_pkg_name       => 'PA_FP_CI_MERGE.' ||
655                    'fp_ci_update_est_amounts'
656                    ,p_procedure_name => PA_DEBUG.G_Err_Stack);
657     IF p_pa_debug_mode = 'Y' THEN
658          PA_DEBUG.g_err_stage := 'Unexpected error in FP_CI_UPDATE_EST_AMOUNTS';
659          PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
660     END IF;
661          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
662          --------dbms_output.put_line('FP_CI_UPDATE_EST_AMOUNTS - 6****');
663  IF p_pa_debug_mode = 'Y' THEN
664          PA_DEBUG.Reset_Curr_Function;
665 END IF;
666          RAISE;
667 
668   END FP_CI_UPDATE_EST_AMOUNTS;
669 -- end of fp_ci_update_est_amounts
670 
671 /****************************************************************
672   This API is called to update the financial impact for control
673   items. The main updation in this API is to update the status
674   code of the impact and the person who caused that impact
675   ***************************************************************/
676   --Added p_impact_type_code for bug 3550073.
677   --p_impact_type can be FINPLAN_COST, FINPLAN_REVENUE or
678   --FINPLAN_BOTH in which case both FINPLAN_COST and FINPLAN_REVENUE records will be updated
679   PROCEDURE FP_CI_UPDATE_IMPACT
680   (
681     p_ci_id                      IN  pa_ci_impacts.ci_id%TYPE,
682     p_status_code            IN  pa_ci_impacts.status_code%TYPE,
683     p_implementation_date     IN  pa_ci_impacts.implementation_date%TYPE,
684     p_implemented_by          IN  pa_ci_impacts.implemented_by%TYPE,
685     p_record_version_number   IN  pa_ci_impacts.record_version_number%TYPE,
686     p_impacted_task_id        IN  pa_ci_impacts.impacted_task_id%TYPE,
687     p_impact_type_code      IN  pa_ci_impacts.impact_type_code%TYPE,
688     p_commit_flag            IN  VARCHAR2 ,
689     p_init_msg_list          IN  VARCHAR2 ,
690     x_return_status      OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
691     x_msg_count          OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
692     x_msg_data           OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
693    )
694    IS
695   -- Local Variable Declaration
696 
697         l_last_updated_by     NUMBER := FND_GLOBAL.USER_ID;
698      l_last_update_date       DATE := SYSDATE;
699         l_last_update_login     NUMBER := FND_GLOBAL.LOGIN_ID;
700 
701         l_party_id       NUMBER := 0;
702         l_implemented_by NUMBER := 0;
703         l_impact_type_code    VARCHAR2(30);
704         l_debug_mode          VARCHAR2(30);
705 
706   BEGIN
707     IF p_pa_debug_mode = 'Y' THEN
708          pa_debug.init_err_stack('PAFPCIMB.FP_CI_UPDATE_IMPACT');
709     END IF;
710      IF NVL(p_init_msg_list,'N') = 'Y' THEN
711                FND_MSG_PUB.initialize;
712      END IF;
713      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
714      l_debug_mode := NVL(l_debug_mode, 'Y');
715    IF p_pa_debug_mode = 'Y' THEN
716      pa_debug.set_process('PLSQL','LOG',l_debug_mode);
717    END IF;
718      x_return_status := FND_API.G_RET_STS_SUCCESS;
719           x_msg_count := 0;
720           --------dbms_output.put_line('FP_CI_UPDATE_IMPACT - 1');
721           --Checking for a valid implementation date
722           IF (p_implementation_date > SYSDATE) THEN
723           PA_UTILS.Add_Message ( p_app_short_name => 'PA',
724                            p_msg_name => 'PA_FP_CI_INV_IMPACT_DATE'
725                          );
726           x_return_status := FND_API.G_RET_STS_ERROR;
727           RETURN;
728      END IF;
729 
730      --Checking if control item id is null
731      IF p_ci_id IS NULL THEN
732           PA_UTILS.Add_Message ( p_app_short_name => 'PA',
733                                    p_msg_name => 'PA_FP_CI_NULL_CI_ID'
734                                  );
735           x_return_status := FND_API.G_RET_STS_ERROR;
736           RETURN;
737      END IF;
738 
739     IF p_impact_type_code  IS NULL OR
740        p_impact_type_code NOT IN ('FINPLAN_COST', 'FINPLAN_REVENUE', 'FINPLAN_BOTH') THEN
741           PA_UTILS.Add_Message ( p_app_short_name => 'PA',
742                                    p_msg_name => 'PA_FP_INV_PARAM_PASSED',
743                                p_token1=>'PROCEDURENAME',
744                                p_value1=>'FP_CI_UPDATE_IMPACT'
745                                  );
746           x_return_status := FND_API.G_RET_STS_ERROR;
747           RETURN;
748      END IF;
749 
750 
751      --Setting impact type code to FINPLAN
752      l_impact_type_code := p_impact_type_code;
753 
754      IF p_implemented_by IS NOT NULL THEN
755           l_implemented_by := p_implemented_by;
756      ELSE
757           l_implemented_by := FND_GLOBAL.USER_ID;
758      END IF;
759 
760      BEGIN
761           SELECT hp.party_id
762           INTO
763                l_party_id
764           FROM      fnd_user fu,
765                hz_parties hp
766           WHERE
767              user_id = l_implemented_by
768              and employee_id IS NOT NULL
769              and hp.orig_system_reference = 'PER:' || fu.employee_id;
770           EXCEPTION
771           WHEN NO_DATA_FOUND THEN
772                SELECT hp.party_id
773                INTO
774                l_party_id
775                FROM fnd_user fu,
776              hz_parties hp
777           WHERE
778              user_id = l_implemented_by
779              and employee_id IS NULL
780              -- Bug 4931044: R12 ATG Mandate: Moving customer_id to
781              --    person_party_id in fnd_user
782              and hp.party_id =  fu.person_party_id;
783              --and hp.party_id =  fu.customer_id;
784         END;
785 
786      -- Update the PA_CI_IMPACTS table
787 
788      UPDATE PA_CI_IMPACTS
789      SET
790          STATUS_CODE = NVL(p_status_code,status_code),
791          IMPLEMENTATION_DATE = NVL(p_implementation_date,SYSDATE),
792          IMPLEMENTED_BY = l_party_id,
793          IMPACTED_TASK_ID = NVL(p_impacted_task_id,impacted_task_id),
794          LAST_UPDATE_DATE = l_last_update_date,
795          LAST_UPDATED_BY = l_last_updated_by,
796          LAST_UPDATE_LOGIN = l_last_update_login,
797          RECORD_VERSION_NUMBER = NVL(p_record_version_number, record_version_number +1)
798          WHERE ci_id = p_ci_id
799         AND   ((l_impact_type_code ='FINPLAN_BOTH' AND impact_type_code IN ('FINPLAN_COST', 'FINPLAN_REVENUE'))
800           OR   (impact_type_code = l_impact_type_code));
801      --------dbms_output.put_line('SQL%ROWCOUNT****' || SQL%ROWCOUNT);
802      IF (SQL%ROWCOUNT = 0) THEN
803            x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
804            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
805      END IF;
806   IF NVL(p_commit_flag,'N') = 'Y' THEN
807      COMMIT;
808   END IF;
809   EXCEPTION
810      WHEN OTHERS THEN
811          FND_MSG_PUB.add_exc_msg
812                   ( p_pkg_name       => 'PA_FP_CI_MERGE.' ||
813                    'fp_ci_update_impact'
814                    ,p_procedure_name => PA_DEBUG.G_Err_Stack);
815     IF p_pa_debug_mode = 'Y' THEN
816          PA_DEBUG.g_err_stage := 'Unexpected error in FP_CI_UPDATE_IMPACT';
817          PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
818     END IF;
819          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
820          --------dbms_output.put_line('FP_CI_UPDATE_IMPACT - 6****');
821  IF p_pa_debug_mode = 'Y' THEN
822          PA_DEBUG.Reset_Curr_Function;
823 END IF;
824          RAISE;
825 
826   END FP_CI_UPDATE_IMPACT;
827 -- end of fp_ci_update_impact
828 
829 
830 
831 PROCEDURE FP_CI_MERGE_CI_ITEMS
832 (
833   p_project_id           IN NUMBER,
834   p_s_fp_ci_id           IN pa_budget_versions.ci_id%TYPE,
835   p_t_fp_ci_id           IN pa_budget_versions.ci_id%TYPE,
836   p_merge_unmerge_mode        IN VARCHAR2 ,
837   p_commit_flag               IN VARCHAR2 ,
838   p_init_msg_list        IN VARCHAR2 ,
839   p_calling_context      IN VARCHAR2,
840   x_warning_flag         OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
841   x_msg_count                 OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
842   x_msg_data                  OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
843   x_return_status             OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
844  )
845   IS
846  --Defining PL/SQL local variables
847        l_s_fp_ci_id      pa_budget_versions.ci_id%TYPE;
848        l_t_fp_ci_id      pa_budget_versions.ci_id%TYPE;
849        l_budget_version_id    pa_budget_versions.budget_version_id%TYPE;
850        l_target_version_id    pa_budget_versions.budget_version_id%TYPE;
851        l_target_version_id_tmp  pa_budget_versions.budget_version_id%TYPE;
852        l_source_version_id    pa_budget_versions.budget_version_id%TYPE;
853        l_counter         NUMBER := 0;
854        l_task_id         NUMBER;
855        l_s_version_id         pa_budget_versions.budget_version_id%TYPE;
856        l_s_fin_plan_pref_code      pa_proj_fp_options.fin_plan_preference_code%TYPE;
857        l_s_multi_curr_flag    pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
858        l_s_time_phased_code   pa_proj_fp_options. all_time_phased_code%TYPE;
859        l_s_resource_list_id   pa_proj_fp_options.all_resource_list_id%TYPE;
860        l_s_fin_plan_level_code     pa_proj_fp_options.all_fin_plan_level_code%TYPE;
861        l_s_uncategorized_flag pa_resource_lists_all_bg.uncategorized_flag %TYPE;
862        l_s_group_res_type_id  pa_resource_lists_all_bg.group_resource_type_id%TYPE;
863        l_s_version_type  pa_budget_versions.version_type%TYPE;
864        l_s_ci_id         pa_budget_versions.ci_id%TYPE;
865        l_t_version_id         pa_budget_versions.budget_version_id%TYPE;
866        l_t_fin_plan_pref_code      pa_proj_fp_options.fin_plan_preference_code%TYPE;
867        l_t_time_phased_code   pa_proj_fp_options. all_time_phased_code%TYPE;
868        l_t_resource_list_id   pa_proj_fp_options.all_resource_list_id%TYPE;
869        l_t_multi_curr_flag    pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
870        l_t_fin_plan_level_code     pa_proj_fp_options.all_fin_plan_level_code%TYPE;
871        l_t_uncategorized_flag pa_resource_lists_all_bg.uncategorized_flag %TYPE;
872        l_t_group_res_type_id  pa_resource_lists_all_bg.group_resource_type_id%TYPE;
873        l_t_version_type  pa_budget_versions.version_type%TYPE;
874        l_t_ci_id         pa_budget_versions.ci_id%TYPE;
875        l_source_ver_type pa_budget_versions.version_type%TYPE;
876        l_target_ver_type pa_budget_versions.version_type%TYPE;
877        l_target_plan_type_p_code pa_proj_fp_options.fin_plan_preference_code%TYPE;
878 
879      --Defining Local PL/SQL variables for source version
880        l_source_id_tbl PA_PLSQL_DATATYPES.IdTabTyp;
881        l_s_fp_version_id_tbl PA_PLSQL_DATATYPES.IdTabTyp;
882        l_s_fin_plan_pref_code_tbl PA_PLSQL_DATATYPES.Char30TabTyp;
883        l_s_multi_curr_flag_tbl     PA_PLSQL_DATATYPES.Char1TabTyp;
884        l_s_fin_plan_level_code_tbl PA_PLSQL_DATATYPES.Char30TabTyp;
885        l_s_uncategorized_flag_tbl PA_PLSQL_DATATYPES.Char1TabTyp;
886        l_s_group_res_type_id_tbl   PA_PLSQL_DATATYPES.IdTabTyp;
887        l_s_version_type_tbl   PA_PLSQL_DATATYPES.Char30TabTyp;
888        l_s_time_phased_code_tbl    PA_PLSQL_DATATYPES.Char30TabTyp;
889        l_source_fp_version_id_tbl PA_PLSQL_DATATYPES.IdTabTyp;
890 
891    --Defining Local PL/SQL variables for target version
892        l_target_id_tbl PA_PLSQL_DATATYPES.IdTabTyp;
893        l_t_fp_version_id_tbl  PA_PLSQL_DATATYPES.IdTabTyp;
894        l_t_fin_plan_pref_code_tbl PA_PLSQL_DATATYPES.Char30TabTyp;
895        l_t_multi_curr_flag_tbl     PA_PLSQL_DATATYPES.Char1TabTyp;
896        l_t_fin_plan_level_code_tbl PA_PLSQL_DATATYPES.Char30TabTyp;
897        l_t_uncategorized_flag_tbl PA_PLSQL_DATATYPES.Char1TabTyp;
898        l_t_version_type_tbl   PA_PLSQL_DATATYPES.Char30TabTyp;
899        l_t_group_res_type_id_tbl   PA_PLSQL_DATATYPES.IdTabTyp;
900        l_t_time_phased_code_tbl    PA_PLSQL_DATATYPES.Char30TabTyp;
901        l_target_fp_version_id_tbl PA_PLSQL_DATATYPES.IdTabTyp;
902 
903    --Other Local Variables
904        l_merge_possible_code_tbl PA_PLSQL_DATATYPES.Char1TabTyp;
905        l_count_merged_versions     NUMBER := 0;
906        l_copy_version_flag    VARCHAR2(1);
907        l_copy_possible_flag   VARCHAR2(1);
908        l_debug_mode           VARCHAR2(30);
909        l_bulk_fetch_count     NUMBER := 0;
910        l_index           NUMBER := 1;
911        l_count           NUMBER := 0;
912        l_t_count_versions          NUMBER := 0;
913        l_count_projects       NUMBER := 0;
914        l_target_plan_types_cnt  NUMBER := 0;
915        l_merged_count         NUMBER := 0;
916        l_insert_flag          VARCHAR2(1) := 'N';
917        l_merge_possible_code    VARCHAR2(1);
918        l_s_version_id_count   NUMBER := 0;
919        l_update_agreement_flag     VARCHAR2(1) := 'N';
920 
921        l_ci_id_tbl                      SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
922        l_ci_cost_version_id_tbl         SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
923        l_ci_rev_version_id_tbl          SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
924        l_ci_all_version_id_tbl          SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
925        l_target_version_id_tbl          SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
926        l_impl_cost_flag_tbl             SYSTEM.pa_varchar2_1_tbl_type:=SYSTEM.pa_varchar2_1_tbl_type();
927        l_impl_rev_flag_tbl              SYSTEM.pa_varchar2_1_tbl_type:=SYSTEM.pa_varchar2_1_tbl_type();
928        l_translated_msgs_tbl            SYSTEM.pa_varchar2_2000_tbl_type:=SYSTEM.pa_varchar2_2000_tbl_type();
929        l_translated_err_msg_count       NUMBER;
930        l_translated_err_msg_level_tbl   SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
931 
932        l_impact_record_exists   VARCHAR2(1); -- Bug 3677924 Raja 02-Jul-04
933 
934        -- Bug 5845142
935        l_s_app_cost_flag                pa_budget_versions.approved_cost_plan_type_flag%TYPE;
936        l_s_app_rev_flag                 pa_budget_versions.approved_rev_plan_type_flag%TYPE;
937        l_t_app_cost_flag                pa_budget_versions.approved_cost_plan_type_flag%TYPE;
938        l_t_app_rev_flag                 pa_budget_versions.approved_rev_plan_type_flag%TYPE;
939 
940 BEGIN
941 
942 IF p_pa_debug_mode = 'Y' THEN
943 pa_debug.init_err_stack('PAFPINCB.FP_CI_MERGE_CI_ITEMS');
944 END IF;
945 IF NVL(p_init_msg_list,'N') = 'Y' THEN
946      FND_MSG_PUB.initialize;
947 END IF;
948 fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
949 l_debug_mode := NVL(l_debug_mode, 'Y');
950 IF p_pa_debug_mode = 'Y' THEN
951   pa_debug.set_process('PLSQL','LOG',l_debug_mode);
952 END IF;
953 x_return_status := FND_API.G_RET_STS_SUCCESS;
954 x_msg_count := 0;
955 
956 l_target_version_id := NULL;
957 l_update_agreement_flag  := 'N';
958 l_copy_version_flag := 'N';
959 x_warning_flag := 'N';
960 savepoint before_fp_ci_copy;
961 
962 --Getting the target ci_id from the parameter
963      l_s_fp_ci_id := p_s_fp_ci_id;
964      l_t_fp_ci_id := p_t_fp_ci_id;
965 
966      ------dbms_output.put_line('l_s_fp_ci_id : ' || l_s_fp_ci_id);
967      ------dbms_output.put_line('l_t_fp_ci_id : ' || l_t_fp_ci_id);
968 
969 --Check if any budget versions exist for the target
970 --control item id or not
971 SELECT COUNT(*) INTO l_t_count_versions
972 FROM pa_budget_versions bv
973 WHERE
974 bv.project_id = p_project_id
975 AND bv.ci_id = l_t_fp_ci_id
976 AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
977      OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
978 
979 --Get number of approved plan types
980 SELECT count(*)
981 INTO l_target_plan_types_cnt
982 FROM pa_proj_fp_options po
983 WHERE
984 project_id = p_project_id
985 and fin_plan_option_level_code = 'PLAN_TYPE'
986 and (NVL(po.approved_rev_plan_type_flag,'N') = 'Y'
987 OR NVL(po.approved_cost_plan_type_flag,'N') = 'Y');
988 
989 -- Get the number of source budget versions
990 SELECT count(*)
991 INTO l_s_version_id_count
992 FROM pa_budget_versions bv
993 WHERE
994 bv.project_id = p_project_id
995 AND bv.ci_id = l_s_fp_ci_id
996 AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
997 OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
998 
999 IF (l_s_version_id_count = 1) THEN
1000 
1001      --Get source version id
1002      SELECT bv.budget_version_id
1003      BULK COLLECT INTO l_source_id_tbl
1004      FROM pa_budget_versions bv
1005      WHERE
1006      bv.project_id = p_project_id
1007      AND bv.ci_id = l_s_fp_ci_id
1008      AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1009      OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1010 
1011      For i in l_source_id_tbl.FIRST.. l_source_id_tbl.LAST
1012      LOOP
1013           --Get source version id in local variable
1014           l_source_version_id := l_source_id_tbl(i);
1015      END LOOP;
1016 
1017      -- Get the source version type. Bug 5845142
1018      SELECT bv.version_type,
1019             NVL(bv.approved_cost_plan_type_flag,'N'),
1020             NVL(bv.approved_rev_plan_type_flag,'N')
1021      INTO l_source_ver_type,
1022           l_s_app_cost_flag,
1023           l_s_app_rev_flag
1024      FROM pa_budget_versions bv
1025      WHERE
1026      bv.project_id = p_project_id
1027      AND bv.budget_version_id = l_source_version_id;
1028 
1029      IF (l_t_count_versions = 1) THEN
1030           --Get target version id
1031           BEGIN
1032                SELECT bv.budget_version_id
1033                INTO l_target_version_id
1034                FROM pa_budget_versions bv
1035                WHERE
1036                bv.project_id = p_project_id
1037                AND bv.ci_id = l_t_fp_ci_id
1038                AND bv.version_type = l_source_ver_type
1039                AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1040                OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1041           EXCEPTION
1042                WHEN NO_DATA_FOUND THEN
1043                    l_t_count_versions := 0;
1044           END;
1045      END IF;
1046 
1047 
1048      IF (l_t_count_versions = 0) THEN
1049           l_copy_version_flag := 'Y';
1050           -- We must copy the target version from source version
1051 
1052           --Call the check API to see if the copy should
1053           --go through or not
1054 
1055           --Before that get details of source version
1056           Pa_Fp_Control_Items_Utils.FP_CI_GET_VERSION_DETAILS
1057           (
1058                p_project_id        => p_project_id,
1059                p_budget_version_id => l_source_version_id,
1060                x_fin_plan_pref_code     => l_s_fin_plan_pref_code,
1061                x_multi_curr_flag   => l_s_multi_curr_flag,
1062                x_fin_plan_level_code    => l_s_fin_plan_level_code,
1063                x_resource_list_id  => l_s_resource_list_id,
1064                x_time_phased_code  => l_s_time_phased_code,
1065                x_uncategorized_flag     => l_s_uncategorized_flag,
1066                x_group_res_type_id => l_s_group_res_type_id,
1067                x_version_type      => l_s_version_type,
1068                x_ci_id             => l_s_ci_id,
1069                x_return_status          => x_return_status,
1070                x_msg_count              => x_msg_count,
1071                x_msg_data               => x_msg_data
1072           )  ;
1073 
1074           IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1075                RETURN;
1076           END IF;
1077 
1078           -- Bug 3677924 Jul 06 2004 Raja
1079           -- Check if target change order has corresponding impact record
1080           -- Note: If the target change order type does not allow the impact, the record
1081           -- would not have been created. For ALL ci version type, there would be two
1082           -- records in the impacts table. If ALL ci version can not be copied
1083           -- then no impact records would be present.
1084           BEGIN
1085                SELECT 'Y'
1086                INTO   l_impact_record_exists
1087                FROM   DUAL
1088                WHERE  EXISTS
1089                       (SELECT 1
1090                        FROM   pa_ci_impacts
1091                        WHERE  ci_id = l_t_fp_ci_id
1092                        AND    (l_s_version_type IN ('REVENUE','ALL') AND impact_type_code = 'FINPLAN_REVENUE'
1093                                 OR l_s_version_type IN ('COST','ALL') AND impact_type_code = 'FINPLAN_COST'));
1094           EXCEPTION
1095                WHEN NO_DATA_FOUND THEN
1096                    l_impact_record_exists := 'N';
1097           END;
1098 
1099           -- Bug 3677924 If the impact record does not exist do not proceed
1100           IF l_impact_record_exists = 'N' THEN
1101              RETURN;
1102           END IF;
1103 
1104           -- Call Copy version API
1105           PA_FIN_PLAN_PUB.Copy_Version
1106           (p_project_id           => p_project_id,
1107            p_source_version_id    => l_source_version_id,
1108            p_copy_mode            => PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
1109            p_calling_module   => PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN,
1110            px_target_version_id   => l_target_version_id,
1111            x_return_status        => x_return_status,
1112            x_msg_count            => x_msg_count,
1113            x_msg_data             => x_msg_data);
1114 
1115            IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1116                ROLLBACK TO before_fp_ci_copy;
1117                RETURN;
1118            END IF;
1119 
1120            l_update_agreement_flag := 'Y';
1121            --Stamp the Control item ids for these budget versions
1122            UPDATE pa_budget_versions bv
1123            SET CI_ID = l_t_fp_ci_id
1124               ,VERSION_NUMBER = 1 -- bug 3677924
1125            WHERE
1126            p_project_id = bv.project_id
1127            AND bv.budget_version_id = l_target_version_id
1128            AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1129            OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1130 
1131            /* copy the supplier data to the target control item
1132             * This will copy only the new suppler cost data  as part of Enc
1133             */
1134 
1135            copy_supplier_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1136                                    p_ci_id_from           => l_s_fp_ci_id,
1137                                    x_return_status        => x_return_status,
1138                                    x_msg_count            => x_msg_count,
1139                                    x_msg_data             => x_msg_data );
1140 
1141           /* added for Enc 12.1.3 to copy direct cost data for target control item*/
1142              copy_direct_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1143                                    p_ci_id_from           => l_s_fp_ci_id,
1144                                    p_bv_id                => l_target_version_id,
1145                                    p_project_id           => p_project_id,
1146                                    x_return_status        => x_return_status,
1147                                    x_msg_count            => x_msg_count,
1148                                    x_msg_data             => x_msg_data );
1149 
1150             -- p_commit_flag :='Y';
1151            IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1152                ROLLBACK TO before_fp_ci_copy;
1153                RETURN;
1154            END IF;
1155      ELSE
1156           --Target version already exists
1157           IF (l_t_count_versions = 2) THEN
1158                --Get target version id for correct source version type
1159                BEGIN
1160                     SELECT bv.budget_version_id
1161                     INTO l_target_version_id
1162                     FROM pa_budget_versions bv
1163                     WHERE
1164                     bv.project_id = p_project_id
1165                     AND bv.ci_id = l_t_fp_ci_id
1166                     AND bv.version_type = l_source_ver_type
1167                     AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1168                     OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1169                EXCEPTION
1170                     WHEN NO_DATA_FOUND THEN
1171                         PA_UTILS.ADD_MESSAGE
1172                          ( p_app_short_name => 'PA',
1173                            p_msg_name       => 'PA_FP_CI_C_INV_VER_TYPE_MATCH');
1174                         ------dbms_output.put_line('FP_CI_CHECK_COPY_POSSIBLE - 2***');
1175                         x_return_status := FND_API.G_RET_STS_ERROR;
1176                END;
1177           END IF;
1178 
1179             l_impl_cost_flag_tbl.extend(1);
1180             l_impl_rev_flag_tbl.extend(1);
1181             l_impl_cost_flag_tbl(1):='N';
1182             l_impl_rev_flag_tbl(1):='N';
1183             l_ci_id_tbl.extend(1); -- Bug 3787567
1184             l_ci_id_tbl(1):=l_s_fp_ci_id;
1185             l_target_version_id_tbl.extend(1); -- Bug 3787567
1186             l_target_version_id_tbl(1):=l_target_version_id;
1187             IF l_source_ver_type ='COST' THEN
1188                 l_ci_cost_version_id_tbl.extend(1);
1189                 l_ci_cost_version_id_tbl(1):=l_source_version_id;
1190                 l_impl_cost_flag_tbl(1):='Y';
1191             ELSIF l_source_ver_type ='REVENUE' THEN
1192                 l_ci_rev_version_id_tbl.extend(1);
1193                 l_ci_rev_version_id_tbl(1):=l_source_version_id;
1194                 l_impl_rev_flag_tbl(1):='Y';
1195             ELSIF l_source_ver_type ='ALL' THEN
1196 
1197               -- Bug 5845142. Cost CI version can be of version type ALL.
1198               IF l_s_app_cost_flag ='Y' AND l_s_app_rev_flag = 'Y'  THEN
1199 
1200                 l_ci_all_version_id_tbl.extend(1);
1201                 l_ci_all_version_id_tbl(1):=l_source_version_id;
1202                 l_impl_cost_flag_tbl(1):='Y';
1203                 l_impl_rev_flag_tbl(1):='Y';
1204 
1205               ELSIF l_s_app_cost_flag ='Y' AND l_s_app_rev_flag = 'N'  THEN
1206 
1207                 l_ci_cost_version_id_tbl.extend(1);
1208                 l_ci_cost_version_id_tbl(1):=l_source_version_id;
1209                 l_impl_cost_flag_tbl(1):='Y';
1210 
1211               END IF;
1212 
1213             END IF;
1214 
1215             implement_change_document
1216             (  p_context                       => 'CI_MERGE'
1217                ,p_ci_id_tbl                     => l_ci_id_tbl
1218                ,p_ci_cost_version_id_tbl        => l_ci_cost_version_id_tbl
1219                ,p_ci_rev_version_id_tbl         => l_ci_rev_version_id_tbl
1220                ,p_ci_all_version_id_tbl         => l_ci_all_version_id_tbl
1221                ,p_budget_version_id_tbl         => l_target_version_id_tbl
1222                ,p_impl_cost_flag_tbl            => l_impl_cost_flag_tbl
1223                ,p_impl_rev_flag_tbl             => l_impl_rev_flag_tbl
1224                ,x_translated_msgs_tbl           => l_translated_msgs_tbl
1225                ,x_translated_err_msg_count      => l_translated_err_msg_count
1226                ,x_translated_err_msg_level_tbl  => l_translated_err_msg_level_tbl
1227                ,x_return_status                 => x_return_status
1228                ,x_msg_count                     => x_msg_count
1229                ,x_msg_data                      => x_msg_data);
1230             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1231                 ROLLBACK TO before_fp_ci_copy;
1232                 --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 11*****');
1233                 RETURN;
1234             END IF;
1235 
1236             l_update_agreement_flag     := 'N';
1237 
1238                --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 10');
1239             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1240                 ROLLBACK TO before_fp_ci_copy;
1241                 --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 11*****');
1242                 RETURN;
1243             END IF;
1244 
1245            copy_supplier_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1246                                    p_ci_id_from           => l_s_fp_ci_id,
1247                                    x_return_status        => x_return_status,
1248                                    x_msg_count            => x_msg_count,
1249                                    x_msg_data             => x_msg_data );
1250 
1251           /* added for Enc 12.1.3 to copy direct cost data for target control item*/
1252              copy_direct_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1253                                    p_ci_id_from           => l_s_fp_ci_id,
1254                                    p_bv_id                => l_target_version_id,
1255                                    p_project_id           => p_project_id,
1256                                    x_return_status        => x_return_status,
1257                                    x_msg_count            => x_msg_count,
1258                                    x_msg_data             => x_msg_data );
1259 
1260             -- p_commit_flag :='Y';
1261            IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1262                ROLLBACK TO before_fp_ci_copy;
1263                RETURN;
1264            END IF;
1265 
1266      END IF;
1267 
1268      --Call the update estimated amounts API to update the estimated amounts
1269       FP_CI_UPDATE_EST_AMOUNTS
1270        (
1271          p_project_id         => p_project_id,
1272          p_source_version_id       => l_source_version_id,
1273          p_target_version_id       => l_target_version_id,
1274          p_merge_unmerge_mode => p_merge_unmerge_mode ,
1275          p_commit_flag        => 'N' ,
1276          p_init_msg_list      => 'N',
1277          p_update_agreement        => l_update_agreement_flag,
1278          x_return_status      => x_return_status,
1279          x_msg_count          => x_msg_count,
1280          x_msg_data           => x_msg_data
1281        );
1282        IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1283           --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 150****');
1284           ROLLBACK TO before_fp_ci_copy;
1285           RETURN;
1286        END IF;
1287 ELSIF (l_s_version_id_count = 2) THEN
1288      --Get both source version ids
1289      SELECT bv.budget_version_id
1290      BULK COLLECT INTO l_source_fp_version_id_tbl
1291      FROM pa_budget_versions bv
1292      WHERE
1293      bv.project_id = p_project_id
1294      AND bv.ci_id = l_s_fp_ci_id
1295      AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1296      OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y')
1297      ORDER BY bv.version_type;
1298 
1299 
1300      IF (l_t_count_versions = 0) THEN
1301           For i in l_source_fp_version_id_tbl.FIRST.. l_source_fp_version_id_tbl.LAST
1302           LOOP
1303           BEGIN
1304                --Get source version id in local variable
1305                l_source_version_id := l_source_fp_version_id_tbl(i);
1306                l_target_version_id := NULL;
1307                -- We must copy the target version from source version
1308 
1309                --Before that get details of source version
1310                Pa_Fp_Control_Items_Utils.FP_CI_GET_VERSION_DETAILS
1311                (
1312                     p_project_id        => p_project_id,
1313                     p_budget_version_id => l_source_version_id,
1314                     x_fin_plan_pref_code     => l_s_fin_plan_pref_code,
1315                     x_multi_curr_flag   => l_s_multi_curr_flag,
1316                     x_fin_plan_level_code    => l_s_fin_plan_level_code,
1317                     x_resource_list_id  => l_s_resource_list_id,
1318                     x_time_phased_code  => l_s_time_phased_code,
1319                     x_uncategorized_flag     => l_s_uncategorized_flag,
1320                     x_group_res_type_id => l_s_group_res_type_id,
1321                     x_version_type      => l_s_version_type,
1322                     x_ci_id             => l_s_ci_id,
1323                     x_return_status          => x_return_status,
1324                     x_msg_count              => x_msg_count,
1325                     x_msg_data               => x_msg_data
1326                )  ;
1327 
1328                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1329                     RETURN;
1330                END IF;
1331 
1332                -- Bug 3677924 Jul 06 2004 Raja
1333                -- Check if target change order has corresponding impact record
1334                -- Note: If the target change order type does not allow the corresponding
1335                -- impact,the impact record would not have been created. The fact that there
1336                -- are two source versions version type would be either cost or rev only.
1337                -- Bug 5845142. Cost CI Impact can be of type ALL
1338                BEGIN
1339                     SELECT 'Y'
1340                     INTO   l_impact_record_exists
1341                     FROM   DUAL
1342                     WHERE  EXISTS
1343                            (SELECT 1
1344                             FROM   pa_ci_impacts
1345                             WHERE  ci_id = l_t_fp_ci_id
1346                             AND    (l_s_version_type IN ('REVENUE') AND impact_type_code = 'FINPLAN_REVENUE'
1347                                      OR l_s_version_type IN ('COST','ALL') AND impact_type_code = 'FINPLAN_COST'));
1348                EXCEPTION
1349                     WHEN NO_DATA_FOUND THEN
1350                         l_impact_record_exists := 'N';
1351                END;
1352 
1353                -- Bug 3677924 If the impact record does not exist skip processing
1354                IF l_impact_record_exists = 'Y' THEN
1355                    -- Call Copy version API
1356                    PA_FIN_PLAN_PUB.Copy_Version
1357                    (p_project_id           => p_project_id,
1358                     p_source_version_id    => l_source_version_id,
1359                     p_copy_mode            => PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
1360                     p_calling_module   => PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN,
1361                     px_target_version_id   => l_target_version_id,
1362                     x_return_status        => x_return_status,
1363                     x_msg_count            => x_msg_count,
1364                     x_msg_data             => x_msg_data);
1365 
1366                     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1367                         ROLLBACK TO before_fp_ci_copy;
1368                         RETURN;
1369                     END IF;
1370 
1371                     l_update_agreement_flag := 'Y';
1372 
1373                     --Stamp the Control item ids for this budget versions
1374                     UPDATE pa_budget_versions bv
1375                     SET CI_ID = l_t_fp_ci_id
1376                        ,version_number = 1  -- Bug 3677924 Jul 06 2004 Raja
1377                     WHERE
1378                     p_project_id = bv.project_id
1379                     AND bv.budget_version_id = l_target_version_id
1380                     AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1381                          OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1382 
1383                     --Call the update estimated amounts API to update the estimated amounts
1384                     FP_CI_UPDATE_EST_AMOUNTS
1385                      (
1386                        p_project_id         => p_project_id,
1387                        p_source_version_id       => l_source_version_id,
1388                        p_target_version_id       => l_target_version_id,
1389                        p_merge_unmerge_mode => p_merge_unmerge_mode ,
1390                        p_commit_flag        => 'N' ,
1391                        p_init_msg_list      => 'N',
1392                        p_update_agreement        => l_update_agreement_flag,
1393                        x_return_status      => x_return_status,
1394                        x_msg_count          => x_msg_count,
1395                        x_msg_data           => x_msg_data
1396                      );
1397 
1398 
1399 
1400            /* For Enc 12.1.3 start */
1401            SELECT bv.approved_cost_plan_type_flag
1402           INTO l_t_app_cost_flag
1403           FROM pa_budget_versions bv
1404           WHERE budget_version_id = l_target_version_id  ;
1405 
1406           if(l_t_app_cost_flag ='Y') then
1407           /*  call the below procedure only for cost budget */
1408 
1409            copy_supplier_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1410                                    p_ci_id_from           => l_s_fp_ci_id,
1411                                    x_return_status        => x_return_status,
1412                                    x_msg_count            => x_msg_count,
1413                                    x_msg_data             => x_msg_data );
1414 
1415           /* added for Enc 12.1.3 to copy direct cost data for target control item*/
1416              copy_direct_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1417                                    p_ci_id_from           => l_s_fp_ci_id,
1418                                    p_bv_id                => l_target_version_id,
1419                                    p_project_id           => p_project_id,
1420                                    x_return_status        => x_return_status,
1421                                    x_msg_count            => x_msg_count,
1422                                    x_msg_data             => x_msg_data );
1423           --p_commit_flag :='Y';
1424          end if;
1425          l_t_app_cost_flag := null ;
1426             /* For Enc 12.1.3 end */
1427                      IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1428                         --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 150****');
1429                         ROLLBACK TO before_fp_ci_copy;
1430                         RETURN;
1431                      END IF;
1432               END IF;  -- if impact record exists copy amounts
1433           EXCEPTION
1434                WHEN RAISE_COPY_ERROR THEN
1435                     x_warning_flag := 'Y';
1436                     x_return_status := FND_API.G_RET_STS_ERROR;
1437           END;
1438           END LOOP;
1439      ELSIF(l_t_count_versions = 2) THEN
1440 
1441           --Get target version id
1442         --Since ordered by version type, the first version should be cost and the second one should be revenue
1443           SELECT bv.budget_version_id
1444           BULK COLLECT INTO l_target_fp_version_id_tbl
1445           FROM pa_budget_versions bv
1446           WHERE
1447           bv.project_id = p_project_id
1448           AND bv.ci_id = l_t_fp_ci_id
1449           AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1450           OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y')
1451           ORDER BY bv.version_type;
1452 
1453 
1454         --Prepare the pl/sql tables for change document
1455         l_ci_id_tbl.extend(1);
1456         l_ci_cost_version_id_tbl.extend(1);
1457         l_ci_rev_version_id_tbl.extend(1);
1458         l_ci_id_tbl(1):=l_s_fp_ci_id;
1459         l_ci_cost_version_id_tbl(1):=l_source_fp_version_id_tbl(1);--Cost Version
1460         l_ci_rev_version_id_tbl(1):=l_source_fp_version_id_tbl(2);--Revenue Version
1461 
1462 
1463         --Prepare pl/sql tbls for target version
1464         l_target_version_id_tbl.extend(2);
1465         l_impl_cost_flag_tbl.extend(2);
1466         l_impl_rev_flag_tbl.extend(2);
1467         l_target_version_id_tbl(1):=l_target_fp_version_id_tbl(1);
1468         l_target_version_id_tbl(2):=l_target_fp_version_id_tbl(2);
1469         l_impl_cost_flag_tbl(1):='Y';
1470         l_impl_rev_flag_tbl(1):='N';
1471         l_impl_cost_flag_tbl(2):='N';
1472         l_impl_rev_flag_tbl(2):='Y';
1473 
1474         implement_change_document
1475         (      p_context                       => 'CI_MERGE'
1476            ,p_ci_id_tbl                     => l_ci_id_tbl
1477            ,p_ci_cost_version_id_tbl        => l_ci_cost_version_id_tbl
1478            ,p_ci_rev_version_id_tbl         => l_ci_rev_version_id_tbl
1479            ,p_ci_all_version_id_tbl         => l_ci_all_version_id_tbl
1480            ,p_budget_version_id_tbl         => l_target_version_id_tbl
1481            ,p_impl_cost_flag_tbl            => l_impl_cost_flag_tbl
1482            ,p_impl_rev_flag_tbl             => l_impl_rev_flag_tbl
1483            ,x_translated_msgs_tbl           => l_translated_msgs_tbl
1484            ,x_translated_err_msg_count      => l_translated_err_msg_count
1485            ,x_translated_err_msg_level_tbl  => l_translated_err_msg_level_tbl
1486            ,x_return_status                 => x_return_status
1487            ,x_msg_count                     => x_msg_count
1488            ,x_msg_data                      => x_msg_data);
1489         l_update_agreement_flag    := 'N';
1490         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1491             ROLLBACK TO before_fp_ci_copy;
1492             --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 11*****');
1493             RETURN;
1494         END IF;
1495 
1496           l_budget_version_id := l_target_fp_version_id_tbl(1);
1497 
1498           /*  call the below procedure only for cost budget */
1499            copy_supplier_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1500                                    p_ci_id_from           => l_s_fp_ci_id,
1501                                    x_return_status        => x_return_status,
1502                                    x_msg_count            => x_msg_count,
1503                                    x_msg_data             => x_msg_data );
1504 
1505           /* added for Enc 12.1.3 to copy direct cost data for target control item*/
1506              copy_direct_cost_data(p_ci_id_to             => l_t_fp_ci_id,
1507                                    p_ci_id_from           => l_s_fp_ci_id,
1508                                    p_bv_id                => l_budget_version_id,
1509                                    p_project_id           => p_project_id,
1510                                    x_return_status        => x_return_status,
1511                                    x_msg_count            => x_msg_count,
1512                                    x_msg_data             => x_msg_data );
1513           --p_commit_flag :='Y';
1514 
1515         --Bug 4132915. Passing the correct source and target version id.
1516         FOR i IN 1..2
1517         LOOP
1518                 FP_CI_UPDATE_EST_AMOUNTS
1519         (
1520           p_project_id            => p_project_id,
1521           p_source_version_id => l_source_fp_version_id_tbl(i),
1522           p_target_version_id => l_target_fp_version_id_tbl(i),
1523           p_merge_unmerge_mode     => p_merge_unmerge_mode ,
1524           p_commit_flag           => 'N' ,
1525           p_init_msg_list          => 'N',
1526           p_update_agreement  => l_update_agreement_flag,
1527           x_return_status          => x_return_status,
1528           x_msg_count              => x_msg_count,
1529           x_msg_data               => x_msg_data
1530         );
1531 
1532         END LOOP;
1533 
1534         IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1535           --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 150****');
1536           ROLLBACK TO before_fp_ci_copy;
1537           RETURN;
1538         END IF;
1539 
1540      ELSIF(l_t_count_versions = 1) THEN
1541 
1542           --Get target version id and version type
1543           -- Bug 5845142
1544           SELECT bv.budget_version_id, bv.version_type,
1545                  bv.approved_cost_plan_type_flag,bv.approved_rev_plan_type_flag
1546           INTO l_target_version_id, l_target_ver_type,
1547                l_t_app_cost_flag,l_t_app_rev_flag
1548           FROM pa_budget_versions bv
1549           WHERE
1550           bv.project_id = p_project_id
1551           AND bv.ci_id = l_t_fp_ci_id
1552           AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1553           OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1554 
1555           l_target_version_id_tmp := l_target_version_id;
1556 
1557         --Bug 5845142. In any scenario the code should never come into this IF. all the conditions are taken
1558         --care in the ELSE block below. This will happen because a CR's cost impact and CO's cost impact
1559         --will be of same version type and similarly CR's revenue impact and CO's revenue impact too will be
1560         --of same version type.
1561         IF l_target_ver_type='ALL' AND
1562            l_t_app_cost_flag ='Y' AND
1563            l_t_app_rev_flag ='Y' THEN
1564 
1565             --Prepare the pl/sql tables for change document
1566             l_ci_id_tbl.extend(1);
1567             l_ci_cost_version_id_tbl.extend(1);
1568             l_ci_rev_version_id_tbl.extend(1);
1569             l_ci_id_tbl(1):=l_s_fp_ci_id;
1570             l_ci_cost_version_id_tbl(1):=l_source_fp_version_id_tbl(1);--Cost Version
1571             l_ci_rev_version_id_tbl(1):=l_source_fp_version_id_tbl(2);--Revenue Version
1572 
1573 
1574             --Prepare pl/sql tbls for target version
1575             l_target_version_id_tbl.extend(1);
1576             l_impl_cost_flag_tbl.extend(1);
1577             l_impl_rev_flag_tbl.extend(1);
1578             l_target_version_id_tbl(1):=l_target_fp_version_id_tbl(1);
1579             l_impl_cost_flag_tbl(1):='Y';
1580             l_impl_rev_flag_tbl(1):='Y';
1581 
1582             implement_change_document
1583             (   p_context                       => 'CI_MERGE'
1584                ,p_ci_id_tbl                     => l_ci_id_tbl
1585                ,p_ci_cost_version_id_tbl        => l_ci_cost_version_id_tbl
1586                ,p_ci_rev_version_id_tbl         => l_ci_rev_version_id_tbl
1587                ,p_ci_all_version_id_tbl         => l_ci_all_version_id_tbl
1588                ,p_budget_version_id_tbl         => l_target_version_id_tbl
1589                ,p_impl_cost_flag_tbl            => l_impl_cost_flag_tbl
1590                ,p_impl_rev_flag_tbl             => l_impl_rev_flag_tbl
1591                ,x_translated_msgs_tbl           => l_translated_msgs_tbl
1592                ,x_translated_err_msg_count      => l_translated_err_msg_count
1593                ,x_translated_err_msg_level_tbl  => l_translated_err_msg_level_tbl
1594                ,x_return_status                 => x_return_status
1595                ,x_msg_count                     => x_msg_count
1596                ,x_msg_data                      => x_msg_data);
1597             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1598                 ROLLBACK TO before_fp_ci_copy;
1599                 --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 11*****');
1600                 RETURN;
1601             END IF;
1602 
1603             l_update_agreement_flag     := 'N';
1604 
1605             --Bug 4132915. Passing the correct source and target version id.
1606             FOR i IN 1..2
1607             LOOP
1608             FP_CI_UPDATE_EST_AMOUNTS
1609             (
1610               p_project_id             => p_project_id,
1611               p_source_version_id  => l_source_fp_version_id_tbl(i),
1612               p_target_version_id  => l_target_version_id_tbl(1),
1613               p_merge_unmerge_mode => p_merge_unmerge_mode ,
1614               p_commit_flag            => 'N' ,
1615               p_init_msg_list      => 'N',
1616               p_update_agreement   => l_update_agreement_flag,
1617               x_return_status      => x_return_status,
1618               x_msg_count          => x_msg_count,
1619               x_msg_data           => x_msg_data
1620             );
1621 
1622             END LOOP;
1623 
1624             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1625               --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 150****');
1626               ROLLBACK TO before_fp_ci_copy;
1627               RETURN;
1628             END IF;
1629 
1630         ELSE
1631 
1632             --Process for each source version id
1633             FOR i in l_source_fp_version_id_tbl.FIRST.. l_source_fp_version_id_tbl.LAST
1634             LOOP
1635                 --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 2');
1636                 l_source_version_id := l_source_fp_version_id_tbl(i);
1637 
1638                 -- Get the source version type
1639                 SELECT bv.version_type,bv.approved_cost_plan_type_flag, bv.approved_rev_plan_type_flag
1640                 INTO l_source_ver_type,l_s_app_cost_flag,l_s_app_rev_flag
1641                 FROM pa_budget_versions bv
1642                 WHERE bv.project_id = p_project_id
1643                 AND   bv.budget_version_id = l_source_version_id;
1644 
1645                 IF(l_source_ver_type <> l_target_ver_type) THEN
1646                 --The target should be copied from the source
1647                 BEGIN
1648                     --Since the current target is not for the present source
1649                     l_target_version_id := NULL;
1650                     --Before that get details of source version
1651                     Pa_Fp_Control_Items_Utils.FP_CI_GET_VERSION_DETAILS
1652                     (
1653                         p_project_id         => p_project_id,
1654                         p_budget_version_id  => l_source_version_id,
1655                         x_fin_plan_pref_code => l_s_fin_plan_pref_code,
1656                         x_multi_curr_flag    => l_s_multi_curr_flag,
1657                         x_fin_plan_level_code     => l_s_fin_plan_level_code,
1658                         x_resource_list_id   => l_s_resource_list_id,
1659                         x_time_phased_code   => l_s_time_phased_code,
1660                         x_uncategorized_flag => l_s_uncategorized_flag,
1661                         x_group_res_type_id  => l_s_group_res_type_id,
1662                         x_version_type       => l_s_version_type,
1663                         x_ci_id              => l_s_ci_id,
1664                         x_return_status      => x_return_status,
1665                         x_msg_count          => x_msg_count,
1666                         x_msg_data           => x_msg_data
1667                     )  ;
1668 
1669                     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1670                         RETURN;
1671                     END IF;
1672 
1673                     -- Bug 3677924 Jul 06 2004 Raja
1674                     -- Check if target change order has corresponding impact record
1675                     -- Note: If the target change order type does not allow the corresponding
1676                     -- impact,the impact record would not have been created. The fact that there
1677                     -- are two source versions version type would be either cost or rev only.
1678                     -- Bug 5845142
1679                     BEGIN
1680                          SELECT 'Y'
1681                          INTO   l_impact_record_exists
1682                          FROM   DUAL
1683                          WHERE  EXISTS
1684                                 (SELECT 1
1685                                  FROM   pa_ci_impacts
1686                                  WHERE  ci_id = l_t_fp_ci_id
1687                                  AND    (l_s_version_type IN ('REVENUE') AND impact_type_code = 'FINPLAN_REVENUE'
1688                                           OR l_s_version_type IN ('COST','ALL') AND impact_type_code = 'FINPLAN_COST'));
1689                     EXCEPTION
1690                          WHEN NO_DATA_FOUND THEN
1691                              l_impact_record_exists := 'N';
1692                     END;
1693 
1694                     -- Bug 3677924 If the impact record does not exist skip processing
1695                     IF l_impact_record_exists = 'Y' THEN
1696 
1697                         -- Call Copy version API
1698                         PA_FIN_PLAN_PUB.Copy_Version
1699                         (p_project_id           => p_project_id,
1700                          p_source_version_id    => l_source_version_id,
1701                          p_copy_mode            => PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
1702                          p_calling_module   => PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN,
1703                          px_target_version_id   => l_target_version_id,
1704                          x_return_status        => x_return_status,
1705                          x_msg_count            => x_msg_count,
1706                          x_msg_data             => x_msg_data);
1707 
1708                          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1709                             ROLLBACK TO before_fp_ci_copy;
1710                             RETURN;
1711                          END IF;
1712                          l_update_agreement_flag := 'Y';
1713                          --Stamp the Control item ids for these budget versions
1714                          UPDATE pa_budget_versions bv
1715                          SET CI_ID = l_t_fp_ci_id
1716                          WHERE
1717                          p_project_id = bv.project_id
1718                          AND bv.budget_version_id = l_target_version_id
1719                          AND (NVL(bv.approved_rev_plan_type_flag,'N') = 'Y'
1720                               OR NVL(bv.approved_cost_plan_type_flag,'N') = 'Y');
1721 
1722                          --Call the update estimated amounts API to update the estimated amounts
1723                          FP_CI_UPDATE_EST_AMOUNTS
1724                           (
1725                             p_project_id         => p_project_id,
1726                             p_source_version_id       => l_source_version_id,
1727                             p_target_version_id       => l_target_version_id,
1728                             p_merge_unmerge_mode => p_merge_unmerge_mode ,
1729                             p_commit_flag        => 'N' ,
1730                             p_init_msg_list      => 'N',
1731                             p_update_agreement        => l_update_agreement_flag,
1732                             x_return_status      => x_return_status,
1733                             x_msg_count          => x_msg_count,
1734                             x_msg_data           => x_msg_data
1735                           );
1736                           IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1737                             --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 150****');
1738                             ROLLBACK TO before_fp_ci_copy;
1739                             RETURN;
1740                           END IF;
1741 
1742                     END IF; -- copy only if impact record exists
1743 
1744                 EXCEPTION
1745                     WHEN RAISE_COPY_ERROR THEN
1746                         x_warning_flag := 'Y';
1747                         x_return_status := FND_API.G_RET_STS_ERROR;
1748                 END;
1749                 ELSE
1750                 --Merge the source into the target
1751                     l_source_id_tbl.DELETE;
1752                     --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 3');
1753                     l_source_id_tbl(1) := l_source_version_id;
1754                     l_target_version_id := l_target_version_id_tmp;
1755 
1756                     --Prepare the pl/sql tables for change document
1757                     l_ci_id_tbl.extend(1);
1758                     l_ci_id_tbl(1):=l_s_fp_ci_id;
1759                     l_impl_cost_flag_tbl.extend(1);
1760                     l_impl_rev_flag_tbl.extend(1);
1761                     l_impl_cost_flag_tbl(1):='N';
1762                     l_impl_rev_flag_tbl(1):='N';
1763                     --Bug 5845142
1764                     IF l_s_app_cost_flag='Y' THEN
1765                         l_ci_cost_version_id_tbl.extend(1);
1766                         l_ci_cost_version_id_tbl(1):=l_source_fp_version_id_tbl(i);--Cost Version
1767                         l_impl_cost_flag_tbl(1):='Y';
1768                     ELSIF l_s_app_rev_flag='Y' THEN
1769                         l_ci_rev_version_id_tbl.extend(1);
1770                         l_ci_rev_version_id_tbl(1):=l_source_fp_version_id_tbl(i);--Revenue Version
1771                         l_impl_rev_flag_tbl(1):='Y';
1772                     END IF;
1773 
1774 
1775                     --Prepare pl/sql tbls for target version
1776                     l_target_version_id_tbl.extend(1);
1777 
1778                     --l_target_version_id_tbl(1):=l_target_fp_version_id_tbl(1);   --Bug 4132915.
1779                     l_target_version_id_tbl(1) := l_target_version_id;
1780 
1781                     -- Bug 5845142. Moved this code above
1782                     /* l_impl_cost_flag_tbl(1):='Y';
1783                     l_impl_rev_flag_tbl(1):='Y'; */
1784 
1785                     implement_change_document
1786                     (   p_context                       => 'CI_MERGE'
1787                        ,p_ci_id_tbl                     => l_ci_id_tbl
1788                        ,p_ci_cost_version_id_tbl        => l_ci_cost_version_id_tbl
1789                        ,p_ci_rev_version_id_tbl         => l_ci_rev_version_id_tbl
1790                        ,p_ci_all_version_id_tbl         => l_ci_all_version_id_tbl
1791                        ,p_budget_version_id_tbl         => l_target_version_id_tbl
1792                        ,p_impl_cost_flag_tbl            => l_impl_cost_flag_tbl
1793                        ,p_impl_rev_flag_tbl             => l_impl_rev_flag_tbl
1794                        ,x_translated_msgs_tbl           => l_translated_msgs_tbl
1795                        ,x_translated_err_msg_count      => l_translated_err_msg_count
1796                        ,x_translated_err_msg_level_tbl  => l_translated_err_msg_level_tbl
1797                        ,x_return_status                 => x_return_status
1798                        ,x_msg_count                     => x_msg_count
1799                        ,x_msg_data                      => x_msg_data);
1800                     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1801                         ROLLBACK TO before_fp_ci_copy;
1802                         --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 11*****');
1803                         RETURN;
1804                     END IF;
1805 
1806                     l_update_agreement_flag  := 'N';
1807 
1808                     --Bug 4132915. Passing the correct source and target version id.
1809                     FP_CI_UPDATE_EST_AMOUNTS
1810                     (
1811                       p_project_id          => p_project_id,
1812                       p_source_version_id    => l_source_id_tbl(1),
1813                       p_target_version_id    => l_target_version_id_tbl(1),
1814                       p_merge_unmerge_mode   => p_merge_unmerge_mode ,
1815                       p_commit_flag              => 'N' ,
1816                       p_init_msg_list        => 'N',
1817                       p_update_agreement     => l_update_agreement_flag,
1818                       x_return_status        => x_return_status,
1819                       x_msg_count            => x_msg_count,
1820                       x_msg_data             => x_msg_data
1821                     );
1822 
1823                     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1824                       --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 150****');
1825                       ROLLBACK TO before_fp_ci_copy;
1826                       RETURN;
1827                     END IF;
1828 
1829                END IF;
1830           END LOOP;
1831         END IF;
1832      END IF;
1833 END IF;
1834 IF NVL(p_commit_flag,'N') = 'Y' THEN
1835      COMMIT;
1836 END IF;
1837 EXCEPTION
1838      WHEN OTHERS THEN
1839          ROLLBACK TO before_fp_ci_copy;
1840          FND_MSG_PUB.add_exc_msg
1841                   ( p_pkg_name       => 'PA_FP_CI_MERGE.' ||
1842                    'fp_ci_merge_ci_items'
1843                    ,p_procedure_name => PA_DEBUG.G_Err_Stack);
1844     IF p_pa_debug_mode = 'Y' THEN
1845          PA_DEBUG.g_err_stage := 'Unexpected error in FP_CI_MERGE_CI_ITEMS';
1846          PA_DEBUG.Log_Message(p_message => PA_DEBUG.g_err_stage);
1847     END IF;
1848          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1849          --------dbms_output.put_line('FP_CI_MERGE_CI_ITEMS - 14');
1850  IF p_pa_debug_mode = 'Y' THEN
1851          PA_DEBUG.Reset_Curr_Function;
1852 END IF;
1853          RAISE;
1854 END FP_CI_MERGE_CI_ITEMS;
1855 -- end of fp_ci_merge_ci_items
1856 
1857 
1858 
1859 /*==================================================================
1860    This api copies the merged ctrl items from source budget version
1861    to target budget version.
1862 
1863 --###
1864 
1865  r11.5 FP.M Developement ----------------------------------
1866 
1867  08-JAN-2004 jwhite        Bug 3362316
1868 
1869                            Extensively rewrote copy_merged_ctrl_items
1870                            - INSERT INTO pa_fp_merged_ctrl_items (
1871 
1872  ==================================================================*/
1873 
1874 --Bug 4247703. Added the parameter p_calling_context. The valid values are NULL or GENERATION
1875 PROCEDURE copy_merged_ctrl_items
1876    (  p_project_id            IN   pa_budget_versions.project_id%TYPE
1877      ,p_source_version_id     IN   pa_budget_versions.budget_version_id%TYPE
1878      ,p_target_version_id     IN   pa_budget_versions.budget_version_id%TYPE
1879      ,p_calling_context       IN   VARCHAR2
1880      ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1881      ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
1882      ,x_msg_data              OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1883 AS
1884 
1885 l_msg_count                     NUMBER := 0;
1886 l_data                          VARCHAR2(2000);
1887 l_msg_data                      VARCHAR2(2000);
1888 l_msg_index_out                 NUMBER;
1889 l_return_status                 VARCHAR2(2000);
1890 l_debug_mode                    VARCHAR2(30);
1891 
1892 BEGIN
1893 
1894       x_msg_count := 0;
1895       x_return_status := FND_API.G_RET_STS_SUCCESS;
1896     IF p_pa_debug_mode = 'Y' THEN
1897           pa_debug.set_err_stack('PA_FP_CI_MERGE.copy_merged_ctrl_items');
1898     END IF;
1899       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1900       l_debug_mode := NVL(l_debug_mode, 'Y');
1901     IF p_pa_debug_mode = 'Y' THEN
1902       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
1903       -- Check for business rules violations
1904 
1905       pa_debug.g_err_stage:= 'Validating input parameters';
1906       pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
1907     END IF;
1908       --Check if plan version id is null
1909 
1910       IF (p_project_id        IS NULL) OR
1911          (p_source_version_id IS NULL) OR
1912          (p_target_version_id IS NULL)
1913       THEN
1914              IF p_pa_debug_mode = 'Y' THEN
1915                 pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
1916                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
1917                 pa_debug.g_err_stage:= 'p_source_version_id = '|| p_source_version_id;
1918                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
1919                 pa_debug.g_err_stage:= 'p_target_version_id = '|| p_target_version_id;
1920                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
1921              END IF;
1922                 PA_UTILS.ADD_MESSAGE
1923                        (p_app_short_name => 'PA',
1924                         p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
1925              IF p_pa_debug_mode = 'Y' THEN
1926                 pa_debug.g_err_stage:= 'Invalid Arguments Passed';
1927                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
1928             END IF;
1929                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1930 
1931       END IF;
1932 
1933       IF NVL(p_calling_context,'GENERATION')<>'GENERATION' THEN
1934 
1935             IF p_pa_debug_mode = 'Y' THEN
1936                 pa_debug.g_err_stage:= 'p_calling_context = '|| p_calling_context;
1937                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
1938             END IF;
1939                 PA_UTILS.ADD_MESSAGE
1940                        (p_app_short_name => 'PA',
1941                         p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
1942                          p_token1        => 'PROCEDURENAME',
1943                          p_value1        => 'PAFPCIMB.copy_merged_ctrl_items',
1944                          p_token2        => 'STAGE',
1945                          p_value2        => 'Invalid p_calling_context '||p_calling_context);
1946 
1947                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1948 
1949       END IF;
1950 
1951       --Bug 4247703. When called in generation flow, the CI links in the source should be inserted into the
1952       --target only if they are not already there in the target. In other flows(copy version) the ci links in the
1953       --target will be deleted before this API is called and hence the check is not required.
1954 
1955       IF p_calling_context ='GENERATION' THEN
1956 
1957           --Bug 5845142. If approved budget (approved only for cost) is created with "Cost and Revenue together"
1958           --setup then it is not possible to include change orders. Change orders can only be implemented.
1959           IF Pa_Fp_Control_Items_Utils.check_valid_combo
1960              ( p_project_id         => p_project_id
1961               ,p_targ_app_cost_flag => 'N'
1962               ,p_targ_app_rev_flag  => 'N') = 'N' THEN
1963 
1964              IF P_PA_DEBUG_MODE = 'Y' THEN
1965                pa_debug.reset_err_stack;
1966              END IF;
1967              RETURN;
1968 
1969           END IF;
1970 
1971           MERGE INTO pa_fp_merged_ctrl_items target
1972           USING
1973              (SELECT
1974                       p_project_id                     project_id
1975                      ,p_target_version_id              plan_version_id
1976                      ,pmc.ci_id                        ci_id
1977                      ,pmc.ci_plan_version_id           ci_plan_version_id
1978                      ,1                                record_version_number
1979                      ,sysdate                          creation_date
1980                      ,fnd_global.user_id               created_by
1981                      ,fnd_global.login_id              last_update_login
1982                      ,fnd_global.user_id               last_updated_by
1983                      ,sysdate                          last_update_date
1984                      ,'COPIED'                         inclusion_method_code
1985                      ,pmc.included_by_person_id        included_by_person_id
1986                      ,pmc.version_type                 version_type
1987                      ,pmc.impl_proj_func_raw_cost      impl_proj_func_raw_cost
1988                      ,pmc.impl_proj_func_burdened_cost impl_proj_func_burdened_cost
1989                      ,pmc.impl_proj_func_revenue       impl_proj_func_revenue
1990                      ,pmc.impl_proj_raw_cost           impl_proj_raw_cost
1991                      ,pmc.impl_proj_burdened_cost      impl_proj_burdened_cost
1992                      ,pmc.impl_proj_revenue            impl_proj_revenue
1993                      ,pmc.impl_quantity                impl_quantity
1994                      ,pmc.impl_equipment_quantity      impl_equipment_quantity
1995                      ,pmc.impl_agr_revenue             impl_agr_revenue
1996               FROM  pa_fp_merged_ctrl_items pmc,
1997                     pa_budget_versions sourcever,
1998                     pa_budget_versions targetver    -- Bug 3720445
1999               WHERE plan_version_id = p_source_version_id
2000               AND   sourcever.budget_version_id=p_source_version_id
2001               AND   targetver.budget_version_id=p_target_version_id   -- Bug 3720445
2002               AND   pmc.version_type = Decode (targetver.version_type, 'ALL', pmc.version_type,
2003                                                                         targetver.version_type)
2004               AND   (sourcever.fin_plan_type_id=targetver.fin_plan_type_id         OR
2005                      EXISTS (SELECT 1
2006                              FROM   pa_pt_co_impl_statuses ptco,
2007                                     pa_control_items pci
2008                              WHERE  ptco.fin_plan_type_id=targetver.fin_plan_type_id
2009                              AND    pci.ci_id=pmc.ci_id
2010                              AND    ptco.ci_type_id=pci.ci_type_id
2011                              AND    ptco.version_type=pmc.version_type
2012                              AND    ptco.status_code=pci.status_code))) source
2013           ON (target.project_id=source.project_id AND
2014               target.plan_version_id=source.plan_version_id AND
2015               target.ci_id=source.ci_id AND
2016               target.ci_plan_version_id=source.ci_plan_version_id AND
2017               target.version_type=source.version_type)
2018           WHEN MATCHED THEN
2019              UPDATE  SET target.last_update_date = sysdate
2020           WHEN NOT MATCHED THEN
2021              INSERT(
2022                     target.project_id
2023                    ,target.plan_version_id
2024                    ,target.ci_id
2025                    ,target.ci_plan_version_id
2026                    ,target.record_version_number
2027                    ,target.creation_date
2028                    ,target.created_by
2029                    ,target.last_update_login
2030                    ,target.last_updated_by
2031                    ,target.last_update_date
2032                    ,target.inclusion_method_code
2033                    ,target.included_by_person_id
2034                    ,target.version_type
2035                    ,target.impl_proj_func_raw_cost
2036                    ,target.impl_proj_func_burdened_cost
2037                    ,target.impl_proj_func_revenue
2038                    ,target.impl_proj_raw_cost
2039                    ,target.impl_proj_burdened_cost
2040                    ,target.impl_proj_revenue
2041                    ,target.impl_quantity
2042                    ,target.impl_equipment_quantity
2043                    ,target.impl_agr_revenue
2044                    )
2045             VALUES(
2046                     source.project_id
2047                    ,source.plan_version_id
2048                    ,source.ci_id
2049                    ,source.ci_plan_version_id
2050                    ,source.record_version_number
2051                    ,source.creation_date
2052                    ,source.created_by
2053                    ,source.last_update_login
2054                    ,source.last_updated_by
2055                    ,source.last_update_date
2056                    ,source.inclusion_method_code
2057                    ,source.included_by_person_id
2058                    ,source.version_type
2059                    ,source.impl_proj_func_raw_cost
2060                    ,source.impl_proj_func_burdened_cost
2061                    ,source.impl_proj_func_revenue
2062                    ,source.impl_proj_raw_cost
2063                    ,source.impl_proj_burdened_cost
2064                    ,source.impl_proj_revenue
2065                    ,source.impl_quantity
2066                    ,source.impl_equipment_quantity
2067                    ,source.impl_agr_revenue);
2068 
2069       ELSE--Calling Context is not GENERATION
2070 
2071           -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
2072           -- The control items which are partially implemented should not be copied. Bug 3550073
2073           -- Bug 3720445, 29-JUL-2004: copy based on ci version type and target version type values
2074           -- Bug 3784823, 29-JUL-2004: It is incorrect not to copy links if ci is partially implemented.
2075           -- Bug 3882920: Links will be copied only if the target version's plan type allows inclusion of CIs. This
2076           -- Bug 4493425: Added another condition in the where clause of select to improve performance.
2077           --check is made only if source/target plan types are different. See bug for details.
2078           INSERT INTO pa_fp_merged_ctrl_items (
2079                    project_id
2080                   ,plan_version_id
2081                   ,ci_id
2082                   ,ci_plan_version_id
2083                   ,record_version_number
2084                   ,creation_date
2085                   ,created_by
2086                   ,last_update_login
2087                   ,last_updated_by
2088                   ,last_update_date
2089                   ,inclusion_method_code
2090                   ,included_by_person_id
2091                   ,version_type
2092                   ,impl_proj_func_raw_cost
2093                   ,impl_proj_func_burdened_cost
2094                   ,impl_proj_func_revenue
2095                   ,impl_proj_raw_cost
2096                   ,impl_proj_burdened_cost
2097                   ,impl_proj_revenue
2098                   ,impl_quantity
2099                   ,impl_equipment_quantity
2100                   ,impl_agr_revenue
2101                     )
2102           SELECT
2103                   p_project_id
2104                  ,p_target_version_id
2105                  ,pmc.ci_id
2106                  ,pmc.ci_plan_version_id
2107                  ,1
2108                  ,sysdate
2109                  ,fnd_global.user_id
2110                  ,fnd_global.login_id
2111                  ,fnd_global.user_id
2112                  ,sysdate
2113                  ,'COPIED'
2114                  ,pmc.included_by_person_id
2115                  ,pmc.version_type
2116                  ,pmc.impl_proj_func_raw_cost
2117                  ,pmc.impl_proj_func_burdened_cost
2118                  ,pmc.impl_proj_func_revenue
2119                  ,pmc.impl_proj_raw_cost
2120                  ,pmc.impl_proj_burdened_cost
2121                  ,pmc.impl_proj_revenue
2122                  ,pmc.impl_quantity
2123                  ,pmc.impl_equipment_quantity
2124                  ,pmc.impl_agr_revenue
2125           FROM  pa_fp_merged_ctrl_items pmc,
2126                 pa_budget_versions sourcever,
2127                 pa_budget_versions targetver    -- Bug 3720445
2128           WHERE plan_version_id = p_source_version_id
2129           AND   sourcever.budget_version_id=p_source_version_id
2130           AND   targetver.budget_version_id=p_target_version_id   -- Bug 3720445
2131           AND   pmc.project_id = p_project_id  -- Bug 4493425
2132           AND   pmc.version_type = Decode (targetver.version_type, 'ALL', pmc.version_type,
2133                                                                     targetver.version_type)
2134           AND   (sourcever.fin_plan_type_id=targetver.fin_plan_type_id         OR
2135                  EXISTS (SELECT 1
2136                          FROM   pa_pt_co_impl_statuses ptco,
2137                                 pa_control_items pci
2138                          WHERE  ptco.fin_plan_type_id=targetver.fin_plan_type_id
2139                          AND    pci.ci_id=pmc.ci_id
2140                          AND    ptco.ci_type_id=pci.ci_type_id
2141                          AND    ptco.version_type=pmc.version_type
2142                          AND    ptco.status_code=pci.status_code)); -- Bug 3720445
2143           -- Bug 3784823 AND   nvl(civer.rev_partially_impl_flag,'N')  <> 'Y';
2144 
2145           -- End: Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
2146 
2147       END IF;--IF p_calling_context ='GENERATION' THEN
2148 
2149     IF p_pa_debug_mode = 'Y' THEN
2150       pa_debug.g_err_stage:= 'Exiting copy_merged_ctrl_items';
2151       pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
2152       pa_debug.reset_err_stack;
2153     END IF;
2154 
2155   EXCEPTION
2156 
2157      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
2158 
2159            x_return_status := FND_API.G_RET_STS_ERROR;
2160            l_msg_count := FND_MSG_PUB.count_msg;
2161            IF l_msg_count = 1 THEN
2162                 PA_INTERFACE_UTILS_PUB.get_messages
2163                       (p_encoded        => FND_API.G_TRUE
2164                       ,p_msg_index      => 1
2165                       ,p_msg_count      => l_msg_count
2166                       ,p_msg_data       => l_msg_data
2167                       ,p_data           => l_data
2168                       ,p_msg_index_out  => l_msg_index_out);
2169                 x_msg_data := l_data;
2170                 x_msg_count := l_msg_count;
2171            ELSE
2172                 x_msg_count := l_msg_count;
2173            END IF;
2174     IF p_pa_debug_mode = 'Y' THEN
2175            pa_debug.reset_err_stack;
2176     END IF;
2177            RAISE;
2178 
2179    WHEN others THEN
2180 
2181           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2182           x_msg_count     := 1;
2183           x_msg_data      := SQLERRM;
2184           FND_MSG_PUB.add_exc_msg
2185                           ( p_pkg_name        => 'PA_FP_CI_MERGE'
2186                            ,p_procedure_name  => 'copy_merged_ctrl_items'
2187                            ,p_error_text      => sqlerrm);
2188      IF p_pa_debug_mode = 'Y' THEN
2189           pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
2190           pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2191           pa_debug.reset_err_stack;
2192     END IF;
2193           RAISE;
2194 
2195 END copy_merged_ctrl_items;
2196 
2197 /*==================================================================
2198    This api is a wrapper API which is called when the user clicks on
2199    Mark As Included button on the page. Included for bug 2681589.
2200  ==================================================================*/
2201  --Changed the API to accept the additional parameters. It now accepts ci version ids for ci id and the version type
2202  --of the target.Based on the version type of target either 1 or 2 records will be created in
2203  --pa_fp_merged_ctrl_items.
2204 
2205  -- bug 3978200  29-0ct-04  Donot throw an error if target version is 'ALL' version and
2206  -- change order has no cost/revenue impact. Create a record in pa_fp_merged_ctrl_items
2207  -- only if there is corresponding impact
2208 
2209 PROCEDURE FP_CI_MANUAL_MERGE
2210 (
2211      p_project_id                  IN  NUMBER,
2212      p_ci_id                       IN  pa_ci_impacts.ci_id%TYPE,
2213      p_ci_cost_version_id          IN  pa_budget_versions.budget_version_id%TYPE,
2214      p_ci_rev_version_id           IN  pa_budget_versions.budget_version_id%TYPE,
2215      p_ci_all_version_id           IN  pa_budget_versions.budget_version_id%TYPE,
2216      p_t_fp_version_id             IN  pa_budget_versions.budget_version_id%TYPE,
2217      p_targ_version_type           IN  pa_budget_versions.version_type%TYPE,
2218      x_return_status               OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
2219      x_msg_count                   OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
2220      x_msg_data                    OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2221 )
2222 AS
2223 
2224 l_msg_count                     NUMBER := 0;
2225 l_data                          VARCHAR2(2000);
2226 l_msg_data                      VARCHAR2(2000);
2227 l_msg_index_out                 NUMBER;
2228 
2229 l_update_impact_allowed         varchar2(1);
2230 
2231 l_debug_mode                     varchar2(1);
2232 l_s_ci_id                       pa_control_items.ci_id%TYPE;
2233 l_impl_version_type             pa_fp_merged_ctrl_items.version_type%TYPE;
2234 
2235     --This cursor is introduced for bug 3550073. In manual merge a link will be created which
2236     --indicates that the amounts are copied from the source to the target version. The user has to
2237     CURSOR c_impl_dtls_csr (c_ci_version_id pa_budget_versions.budget_version_id%TYPE)
2238     IS
2239     SELECT pbvs.burdened_cost pfc_burd_cost,
2240            pbvs.revenue pfc_revenue,
2241            pbvs.raw_cost pfc_raw_cost,
2242            pbvs.total_project_raw_cost pc_raw_cost,
2243            pbvs.total_project_burdened_cost pc_burd_cost,
2244            pbvs.total_project_revenue pc_revenue,
2245            DECODE(pbvs.version_type,'REVENUE',NULL,pbvs.labor_quantity) cost_ppl_qty,
2246            DECODE(pbvs.version_type,'REVENUE',NULL,pbvs.equipment_quantity) cost_equip_qty,
2247            DECODE(pbvs.version_type,'REVENUE',pbvs.labor_quantity,NULL) rev_ppl_qty,
2248            DECODE(pbvs.version_type,'REVENUE',pbvs.equipment_quantity,NULL) rev_equip_qty
2249     FROM   pa_budget_versions pbvs
2250     WHERE  pbvs.budget_version_id=c_ci_version_id;
2251 
2252     l_impl_dtls_rec   c_impl_dtls_csr%ROWTYPE;
2253 
2254     l_upd_cost_impact_allowed   VARCHAR2(1);
2255     l_upd_rev_impact_allowed    VARCHAR2(1);
2256     --Bug 4136386
2257     l_impact_type_implemented   pa_fp_merged_ctrl_items.version_type%TYPE;
2258     l_impl_version_type_tbl     SYSTEM.pa_varchar2_30_tbl_type := SYSTEM.pa_varchar2_30_tbl_type();
2259 
2260 BEGIN
2261 
2262       x_msg_count := 0;
2263       x_return_status := FND_API.G_RET_STS_SUCCESS;
2264       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
2265       IF l_debug_mode = 'Y' THEN
2266               pa_debug.set_err_stack('PA_FP_CI_MERGE.FP_CI_MANUAL_MERGE');
2267               pa_debug.set_process('PLSQL','LOG',l_debug_mode);
2268       END IF;
2269 
2270       -- Check for business rules violations
2271 
2272       IF l_debug_mode = 'Y' THEN
2273               pa_debug.g_err_stage:= 'Validating input parameters';
2274               pa_debug.write(l_module_name,pa_debug.g_err_stage,
2275                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
2276       END IF;
2277 
2278       IF (p_project_id IS NULL) OR
2279          (p_ci_id is NULL) OR
2280          (p_t_fp_version_id is NULL) OR
2281          (p_targ_version_type is NULL) THEN
2282 
2283               IF l_debug_mode = 'Y' THEN
2284                         pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
2285                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
2286                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2287                         pa_debug.g_err_stage:= 'p_ci_id = '|| p_ci_id;
2288                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
2289                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2290                         pa_debug.g_err_stage:= 'p_t_fp_version_id = '|| p_t_fp_version_id;
2291                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
2292                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2293                         pa_debug.g_err_stage:= 'p_targ_version_type = '|| p_targ_version_type;
2294                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
2295                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2296               END IF;
2297               PA_UTILS.ADD_MESSAGE
2298                       (p_app_short_name => 'PA',
2299                        p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
2300                        p_token1         => 'PROCEDURENAME',
2301                        p_value1         => 'PAFPCIMB.FP_CI_MANUAL_MERGE');
2302               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2303 
2304       END IF;
2305 
2306       --Bug 4136386.If either cost or revenue part of the change order impact has already got implemented then the
2307       --impact that  got implemented should not be manually merged. This block will find that impact and
2308       --initialize the variable l_impact_type_implemented accordingly
2309       SELECT version_type
2310       BULK COLLECT INTO l_impl_version_type_tbl
2311       FROM   pa_fp_merged_ctrl_items
2312       WHERE  project_id=p_project_id
2313       AND    plan_version_id=p_t_fp_version_id
2314       AND    ci_id=p_ci_id
2315       AND    ci_plan_version_id IN ( NVL(p_ci_cost_version_id,-99),NVL(p_ci_rev_version_id,-99),NVL(p_ci_all_version_id,-99))
2316       AND    version_type IN ('COST','REVENUE');
2317 
2318       IF l_impl_version_type_tbl.COUNT=0 THEN
2319 
2320           l_impact_type_implemented:='NONE';
2321 
2322       ELSIF l_impl_version_type_tbl.COUNT=1 THEN
2323 
2324           l_impact_type_implemented := l_impl_version_type_tbl(1);
2325 
2326       ELSE
2327 
2328           --This is the case where the impact is fully implemented. In this case the api should not have been called
2329           --at all. Hence throw error.
2330           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
2331                 p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
2332                 p_token1         => 'PROCEDURENAME',
2333                 p_value1         => 'PAFPCIMB.FP_CI_MANUAL_MERGE',
2334                 p_token2         => 'STAGE',
2335                 p_value2         => 'Manual merge called for a ci which is fully impl.['||p_ci_id||', '||p_t_fp_version_id||']');
2336 
2337           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2338 
2339       END IF;
2340 
2341       IF p_targ_version_type IN ('ALL','COST')
2342          AND nvl(p_ci_cost_version_id,p_ci_all_version_id) IS NOT NULL -- bug 3978200
2343          AND l_impact_type_implemented <> 'COST' --Bug 4136386
2344       THEN
2345           /* commented for bug 3978200
2346           IF nvl(p_ci_cost_version_id,p_ci_all_version_id) IS NULL THEN
2347               IF l_debug_mode = 'Y' THEN
2348                   pa_debug.g_err_stage:= 'p_ci_cost_version_id = '|| p_ci_cost_version_id;
2349                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2350 
2351                   pa_debug.g_err_stage:= 'p_ci_all_version_id = '|| p_ci_all_version_id;
2352                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2353 
2354               END IF;
2355               PA_UTILS.ADD_MESSAGE
2356                       (p_app_short_name => 'PA',
2357                        p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
2358                        p_token1         => 'PROCEDURENAME',
2359                        p_value1         => 'PAFPCIMB.FP_CI_MANUAL_MERGE');
2360               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2361           END IF;
2362           */
2363 
2364           /* Call FP_CI_LINK_CONTROL_ITEMS*/
2365           IF l_debug_mode = 'Y' THEN
2366              pa_debug.g_err_stage:= 'B F p_ci_cost_version_id = '|| p_ci_cost_version_id;
2367              pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2368 
2369              pa_debug.g_err_stage:= 'B F p_ci_all_version_id = '|| p_ci_all_version_id;
2370              pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2371           END IF;
2372 
2373           OPEN  c_impl_dtls_csr(NVL(p_ci_cost_version_id,p_ci_all_version_id));
2374           FETCH c_impl_dtls_csr INTO l_impl_dtls_rec ;
2375           CLOSE c_impl_dtls_csr;
2376 
2377           IF l_debug_mode = 'Y' THEN
2378              pa_debug.g_err_stage:= 'COST - AFTER FETCH of cursor c_impl_dtls_csr';
2379              pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2380           END IF;
2381 
2382          --Though all the amounts are passed while calling the FP_CI_LINK_CONTROL_ITEMS, the called API
2383          --will take care of nulling out the amounts depending on the version type. Nulling out of cost amts
2384          --for rev vesion types is not done here as it will be easier to put the logic in FP_CI_LINK_CONTROL_ITEMS
2385           FP_CI_LINK_CONTROL_ITEMS
2386          (
2387            p_project_id           => p_project_id
2388           ,p_s_fp_version_id  => NVL(p_ci_cost_version_id,p_ci_all_version_id)
2389           ,p_t_fp_version_id  => p_t_fp_version_id
2390           ,p_inclusion_method => 'MANUAL'
2391           ,p_included_by     => NULL
2392           ,p_version_type       => 'COST'
2393           ,p_ci_id              =>  p_ci_id
2394           ,p_cost_ppl_qty       => l_impl_dtls_rec.cost_ppl_qty
2395           ,p_rev_ppl_qty        => NULL
2396           ,p_cost_equip_qty     => l_impl_dtls_rec.cost_equip_qty
2397           ,p_rev_equip_qty      => NULL
2398           ,p_impl_pfc_raw_cost  => l_impl_dtls_rec.pfc_raw_cost
2399           ,p_impl_pfc_revenue   => NULL
2400           ,p_impl_pfc_burd_cost => l_impl_dtls_rec.pfc_burd_cost
2401           ,p_impl_pc_raw_cost   => l_impl_dtls_rec.pc_raw_cost
2402           ,p_impl_pc_revenue    => NULL
2403           ,p_impl_pc_burd_cost  => l_impl_dtls_rec.pc_burd_cost
2404           ,p_impl_agr_revenue   => NULL
2405           ,x_return_status         => x_return_status
2406           ,x_msg_count             => x_msg_count
2407           ,x_msg_data              => x_msg_data
2408         );
2409 
2410           IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
2411                 IF l_debug_mode = 'Y' THEN
2412                     pa_debug.g_err_stage:= 'Error calling FP_CI_LINK_CONTROL_ITEMS';
2413             pa_debug.write('FP_CI_MANUAL_MERGE: ' || l_module_name,pa_debug.g_err_stage,
2414                                                                 PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2415                 END IF;
2416                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2417           END IF;
2418 
2419       END IF;
2420 
2421       IF  p_targ_version_type IN ('ALL','REVENUE')
2422           AND nvl(p_ci_rev_version_id,p_ci_all_version_id) IS NOT NULL -- bug 3978200
2423           AND l_impact_type_implemented <> 'REVENUE' --Bug 4136386
2424       THEN
2425           /* commented for bug 3978200
2426           IF nvl(p_ci_rev_version_id,p_ci_all_version_id) IS NULL THEN
2427               IF l_debug_mode = 'Y' THEN
2428                   pa_debug.g_err_stage:= 'p_ci_rev_version_id = '|| p_ci_rev_version_id;
2429                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2430 
2431                   pa_debug.g_err_stage:= 'p_ci_all_version_id = '|| p_ci_all_version_id;
2432                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2433 
2434               END IF;
2435               PA_UTILS.ADD_MESSAGE
2436                       (p_app_short_name => 'PA',
2437                        p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
2438                        p_token1         => 'PROCEDURENAME',
2439                        p_value1         => 'PAFPCIMB.FP_CI_MANUAL_MERGE');
2440               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2441           END IF;
2442           */
2443 
2444           IF l_debug_mode = 'Y' THEN
2445               pa_debug.g_err_stage:= 'B F p_ci_rev_version_id = '|| p_ci_rev_version_id;
2446               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2447 
2448               pa_debug.g_err_stage:= 'B F p_ci_all_version_id = '|| p_ci_all_version_id;
2449               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2450           END IF;
2451 
2452           /* Call FP_CI_LINK_CONTROL_ITEMS*/
2453           OPEN  c_impl_dtls_csr(NVL(p_ci_rev_version_id,p_ci_all_version_id));
2454           FETCH c_impl_dtls_csr INTO l_impl_dtls_rec ;
2455           CLOSE c_impl_dtls_csr;
2456 
2457           IF l_debug_mode = 'Y' THEN
2458              pa_debug.g_err_stage:= 'REV - AFTER FETCH of cursor c_impl_dtls_csr';
2459              pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2460           END IF;
2461 
2462          --Though all the amounts are passed while calling the FP_CI_LINK_CONTROL_ITEMS, the called API
2463          --will take care of nulling out the amounts depending on the version type. Nulling out of cost amts
2464          --for rev vesion types is not done here as it will be easier to put the logic in FP_CI_LINK_CONTROL_ITEMS
2465           FP_CI_LINK_CONTROL_ITEMS
2466         (
2467            p_project_id           => p_project_id
2468           ,p_s_fp_version_id  => NVL(p_ci_rev_version_id,p_ci_all_version_id)
2469           ,p_t_fp_version_id  => p_t_fp_version_id
2470           ,p_inclusion_method => 'MANUAL'
2471           ,p_included_by     => NULL
2472           ,p_version_type       => 'REVENUE'
2473           ,p_ci_id              =>  p_ci_id
2474           ,p_cost_ppl_qty       => NULL
2475           ,p_rev_ppl_qty        => l_impl_dtls_rec.rev_ppl_qty
2476           ,p_cost_equip_qty     => NULL
2477           ,p_rev_equip_qty      => l_impl_dtls_rec.rev_equip_qty
2478           ,p_impl_pfc_raw_cost  => NULL
2479           ,p_impl_pfc_revenue   => l_impl_dtls_rec.pfc_revenue
2480           ,p_impl_pfc_burd_cost => NULL
2481           ,p_impl_pc_raw_cost   => NULL
2482           ,p_impl_pc_revenue    => l_impl_dtls_rec.pc_revenue
2483           ,p_impl_pc_burd_cost  => NULL
2484           ,p_impl_agr_revenue   => NULL
2485           ,x_return_status         => x_return_status
2486           ,x_msg_count             => x_msg_count
2487           ,x_msg_data              => x_msg_data
2488         );
2489 
2490           IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
2491                 IF l_debug_mode = 'Y' THEN
2492                     pa_debug.g_err_stage:= 'Error calling FP_CI_LINK_CONTROL_ITEMS';
2493             pa_debug.write('FP_CI_MANUAL_MERGE: ' || l_module_name,pa_debug.g_err_stage,
2494                                                                 PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2495                 END IF;
2496                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2497           END IF;
2498       END IF;
2499 
2500       /*
2501          call Pa_Fp_Control_Items_Utils.FP_CI_VALIDATE_UPDATE_IMPACT to determine whether we can
2502       update impact as implemented or not
2503       */
2504 
2505       Pa_Fp_Control_Items_Utils.FP_CI_VALIDATE_UPDATE_IMPACT
2506       (
2507         p_project_id               => p_project_id
2508        ,p_ci_id                    => p_ci_id
2509        ,p_source_version_id        => NULL
2510        ,p_target_version_id        => p_t_fp_version_id
2511        ,x_upd_cost_impact_allowed  => l_upd_cost_impact_allowed
2512        ,x_upd_rev_impact_allowed   => l_upd_rev_impact_allowed
2513        ,x_msg_data                 => x_msg_data
2514        ,x_msg_count                => x_msg_count
2515        ,x_return_status            => x_return_status
2516       );
2517 
2518       IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
2519             IF  l_debug_mode = 'Y' THEN
2520                 pa_debug.g_err_stage:= 'Error calling Pa_Fp_Control_Items_Utils.FP_CI_VALIDATE_UPDATE_IMPACT';
2521           pa_debug.write('FP_CI_MANUAL_MERGE: ' || l_module_name,pa_debug.g_err_stage,
2522                                                               PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2523             END IF;
2524             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2525       END IF;
2526 
2527       IF l_debug_mode = 'Y' THEN
2528             pa_debug.g_err_stage:= 'l_update_impact_allowed' || l_update_impact_allowed;
2529          pa_debug.write('FP_CI_MANUAL_MERGE: ' || l_module_name,pa_debug.g_err_stage,
2530                                                               PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
2531       END IF;
2532 
2533       /*
2534      call FP_CI_UPDATE_IMPACT if flag is Y.
2535      pass only ci id and the status code as we need to only them
2536      in this case.
2537       */
2538       IF l_upd_cost_impact_allowed = 'Y' THEN
2539 
2540           IF p_targ_version_type IN ('ALL','COST') THEN
2541 
2542               FP_CI_UPDATE_IMPACT
2543               (
2544                 p_ci_id                => p_ci_id
2545                 ,p_status_code          => 'CI_IMPACT_IMPLEMENTED'
2546                 ,p_impact_type_code => 'FINPLAN_COST'
2547                 ,p_commit_flag          => 'Y'
2548                 ,x_return_status   => x_return_status
2549                 ,x_msg_count       => x_msg_count
2550                 ,x_msg_data             => x_msg_data
2551                );
2552 
2553               IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
2554                 IF l_debug_mode = 'Y' THEN
2555                     pa_debug.g_err_stage:= 'Error calling FP_CI_UPDATE_IMPACT';
2556                     pa_debug.write('FP_CI_MANUAL_MERGE: ' || l_module_name,pa_debug.g_err_stage,
2557                                                 PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2558                 END IF;
2559                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2560               END IF;
2561           END IF;
2562 
2563      END IF;
2564 
2565      IF l_upd_rev_impact_allowed = 'Y' THEN
2566 
2567           IF p_targ_version_type IN ('ALL','COST') THEN
2568 
2569               FP_CI_UPDATE_IMPACT
2570               (
2571                 p_ci_id                => p_ci_id
2572                 ,p_status_code          => 'CI_IMPACT_IMPLEMENTED'
2573                 ,p_impact_type_code => 'FINPLAN_REVENUE'
2574                 ,p_commit_flag          => 'Y'
2575                 ,x_return_status   => x_return_status
2576                 ,x_msg_count       => x_msg_count
2577                 ,x_msg_data             => x_msg_data
2578                );
2579 
2580               IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
2581                 IF l_debug_mode = 'Y' THEN
2582                     pa_debug.g_err_stage:= 'Error calling FP_CI_UPDATE_IMPACT';
2583                     pa_debug.write('FP_CI_MANUAL_MERGE: ' || l_module_name,pa_debug.g_err_stage,
2584                                                 PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2585                 END IF;
2586                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2587               END IF;
2588           END IF;
2589       END IF; -- update_impact_allowed = 'Y'
2590 
2591       IF l_debug_mode = 'Y' THEN
2592               pa_debug.g_err_stage:= 'Exiting FP_CI_MANUAL_MERGE';
2593               pa_debug.write(l_module_name,pa_debug.g_err_stage,
2594                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
2595               pa_debug.reset_err_stack;
2596 
2597       END IF;
2598 
2599  EXCEPTION
2600 
2601      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
2602 
2603            x_return_status := FND_API.G_RET_STS_ERROR;
2604            l_msg_count := FND_MSG_PUB.count_msg;
2605 
2606            IF l_msg_count = 1 and x_msg_data IS NULL THEN
2607                 PA_INTERFACE_UTILS_PUB.get_messages
2608                       (p_encoded        => FND_API.G_TRUE
2609                       ,p_msg_index      => 1
2610                       ,p_msg_count      => l_msg_count
2611                       ,p_msg_data       => l_msg_data
2612                       ,p_data           => l_data
2613                       ,p_msg_index_out  => l_msg_index_out);
2614                 x_msg_data := l_data;
2615                 x_msg_count := l_msg_count;
2616            ELSE
2617                 x_msg_count := l_msg_count;
2618            END IF;
2619            IF l_debug_mode = 'Y' THEN
2620                    pa_debug.reset_err_stack;
2621            END IF;
2622            -- RAISE; /* bug 3978200 Directly called from middle tier donot raise */
2623 
2624    WHEN others THEN
2625 
2626           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2627           x_msg_count     := 1;
2628           x_msg_data      := SQLERRM;
2629 
2630           FND_MSG_PUB.add_exc_msg
2631                           ( p_pkg_name        => 'PA_FP_CI_MERGE'
2632                            ,p_procedure_name  => 'FP_CI_MANUAL_MERGE'
2633                            ,p_error_text      => x_msg_data);
2634 
2635           IF l_debug_mode = 'Y' THEN
2636               pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
2637               pa_debug.write(l_module_name,pa_debug.g_err_stage,
2638                                      PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
2639               pa_debug.reset_err_stack;
2640 
2641           END IF;
2642           RAISE;
2643 
2644 END FP_CI_MANUAL_MERGE;
2645 
2646 
2647 -- Start of functions to be used only in implement_ci_into_single_ver API
2648 
2649 FUNCTION get_task_id(p_planning_level IN pa_proj_fp_options.cost_fin_plan_level_code%TYPE,
2650                      p_task_id        IN pa_resource_assignments.task_id%TYPE)
2651 RETURN NUMBER IS
2652 l_temp  NUMBER;
2653 BEGIN
2654    IF P_PA_DEBUG_MODE='Y' THEN
2655        pa_debug.write('PAFPCIMB.get_task_id','p_task_id IS '||p_task_id,3);
2656        pa_debug.write('PAFPCIMB.get_task_id','p_planning_level IS '||p_task_id,3);
2657    END IF;
2658    if p_task_id=0 THEN
2659       return 0;
2660    END IF;
2661 
2662    FOR kk IN 1..l_src_targ_task_tbl.COUNT LOOP
2663 
2664         IF l_src_targ_task_tbl(kk).key=p_task_id THEN
2665 
2666             RETURN l_src_targ_task_tbl(kk).value;
2667 
2668         END IF;
2669 
2670    END LOOP;
2671 
2672    l_temp := l_src_targ_task_tbl.COUNT +1;
2673    l_src_targ_task_tbl(l_temp).key:=p_task_id;
2674    select decode(p_planning_level, 'P',0,'T',pt.top_task_id, pt.task_id)
2675    into l_src_targ_task_tbl(l_temp).value
2676    from pa_tasks pt
2677    where pt.task_id = p_task_id;
2678 
2679    IF P_PA_DEBUG_MODE='Y' THEN
2680         pa_debug.write('PAFPCIMB.get_task_id','l_src_targ_task_tbl(l_temp).value is '||l_src_targ_task_tbl(l_temp).value,3);
2681    END IF;
2682    return  l_src_targ_task_tbl(l_temp).value;
2683 END;
2684 
2685 FUNCTION get_mapped_ra_id(p_task_id                IN pa_resource_assignments.task_id%TYPE,
2686                           p_rlm_id                 IN pa_resource_assignments.resource_list_member_id%TYPE,
2687                           p_resource_assignment_id IN pa_resource_assignments.resource_assignment_id%TYPE,
2688                           p_fin_plan_level_code    IN pa_proj_fp_options.cost_fin_plan_level_code%TYPE
2689                           )
2690 RETURN NUMBER IS
2691 l_index     NUMBER;
2692 l_task_id  pa_tasks.task_id%TYPE;
2693 l_rlm_id   pa_resource_list_members.resource_list_member_id%TYPE;
2694 BEGIN
2695      IF p_resource_assignment_id IS NULL AND p_fin_plan_level_code IS NULL THEN
2696 
2697         l_task_id:=p_task_id;
2698         l_rlm_id:=p_rlm_id;
2699 
2700      ELSE
2701         SELECT get_task_id(p_fin_plan_level_code,task_id),
2702                resource_list_member_id
2703         INTO   l_task_id,
2704                l_rlm_id
2705         FROM   pa_resource_assignments
2706         WHERE  resource_assignment_id=p_resource_assignment_id;
2707      END IF;
2708 
2709      l_index := 1;
2710      LOOP
2711         EXIT WHEN (l_index > l_res_assmt_map_rec_tbl.COUNT) OR
2712                   ( l_res_assmt_map_rec_tbl(l_index).task_id=l_task_id AND
2713                     l_res_assmt_map_rec_tbl(l_index).resource_list_member_id=l_rlm_id) ;
2714 
2715         l_index:=l_index+1;
2716 
2717      END LOOP;
2718      IF (l_index<=l_res_assmt_map_rec_tbl.COUNT) THEN
2719 
2720          RETURN l_res_assmt_map_rec_tbl(l_index).resource_assignment_id;
2721 
2722      ELSE
2723 
2724          RETURN NULL;
2725 
2726      END IF;
2727 END;
2728 
2729 FUNCTION get_mapped_dml_code(p_task_id        IN pa_resource_assignments.task_id%TYPE,
2730                              p_rlm_id         IN pa_resource_assignments.resource_list_member_id%TYPE,
2731                              p_resource_assignment_id IN pa_resource_assignments.resource_assignment_id%TYPE,
2732                              p_fin_plan_level_code    IN pa_proj_fp_options.cost_fin_plan_level_code%TYPE
2733 
2734                              )
2735 RETURN VARCHAR2 IS
2736 l_index    NUMBER;
2737 l_task_id  pa_tasks.task_id%TYPE;
2738 l_rlm_id   pa_resource_list_members.resource_list_member_id%TYPE;
2739 
2740 BEGIN
2741      IF p_resource_assignment_id IS NULL AND p_fin_plan_level_code IS NULL THEN
2742 
2743         l_task_id:=p_task_id;
2744         l_rlm_id:=p_rlm_id;
2745 
2746      ELSE
2747         SELECT get_task_id(p_fin_plan_level_code,task_id),
2748                resource_list_member_id
2749         INTO   l_task_id,
2750                l_rlm_id
2751         FROM   pa_resource_assignments
2752         WHERE  resource_assignment_id=p_resource_assignment_id;
2753      END IF;
2754 
2755      l_index := 1;
2756      LOOP
2757         EXIT WHEN (l_index > l_res_assmt_map_rec_tbl.COUNT) OR
2758                   ( l_res_assmt_map_rec_tbl(l_index).task_id=l_task_id AND
2759                     l_res_assmt_map_rec_tbl(l_index).resource_list_member_id=l_rlm_id) ;
2760 
2761         l_index:=l_index+1;
2762      END LOOP;
2763      IF (l_index<=l_res_assmt_map_rec_tbl.COUNT) THEN
2764 
2765          RETURN l_res_assmt_map_rec_tbl(l_index).ra_dml_code;
2766 
2767      ELSE
2768 
2769          RETURN NULL;
2770 
2771      END IF;
2772 END;
2773 
2774 -- End of functions to be used only in implement_ci_into_single_ver API
2775 
2776 --   Implements the impact of the change order into the target budget version id passed
2777 
2778 -- Bug 3934574 Oct 14 2004  Added a new parameter p_calling_context that would be populated when
2779 -- called as part of budget/forecast generation
2780 
2781 PROCEDURE implement_ci_into_single_ver(p_context                    IN     VARCHAR2
2782                                       ,p_calling_context            IN     VARCHAR2                             DEFAULT NULL -- bug 3934574
2783                                       ,P_ci_id                      IN     Pa_control_items.ci_id%TYPE --  The Id of the chg doc that needs to be implemented
2784                                       ,P_ci_cost_version_id         IN     Pa_budget_versions.budget_version_id%TYPE  DEFAULT  NULL -- The cost budget version id corresponding to the p_ci_id passed. This will be derived internally if not passed
2785                                       ,P_ci_rev_version_id          IN     Pa_budget_versions.budget_version_id%TYPE  DEFAULT  NULL -- The rev budget version id corresponding to the p_ci_id passed. This will be derived internally if not passed
2786                                       ,P_ci_all_version_id          IN     Pa_budget_versions.budget_version_id%TYPE   DEFAULT  NULL -- The all budget_version_id corresponding to the p_ci_id passed. This will be derived internally if not passed
2787                                       ,P_budget_version_id          IN     Pa_budget_versions.budget_version_id%TYPE -- The Id of the  budget version into which the CO needs to be implemented
2788                                       ,p_fin_plan_type_id           IN     pa_fin_plan_types_b.fin_plan_type_id%TYPE
2789                                       ,p_fin_plan_type_name         IN     pa_fin_plan_types_tl.name%TYPE
2790                                       ,P_partial_impl_rev_amt       IN     NUMBER    DEFAULT  NULL -- The revenue amount that should be implemented into the target. This will be passed only in the case of partial implementation
2791                                       ,p_cost_impl_flag             IN     VARCHAR2 -- Can be Y or N. Indicates whether cost can be implemented or not.
2792                                       ,p_rev_impl_flag              IN     VARCHAR2 -- Can be Y or N. Indicates whether rev can be implemented or not.
2793                                       ,P_submit_version_flag        IN     VARCHAR2 -- Can be Y or N. Indicates whether the version can be submitted for baseline after implementing CO or not.
2794                                       ,P_agreement_id               IN     Pa_agreements_all.agreement_id%TYPE  DEFAULT  NULL -- The id of the agreement that is linked with the CO.
2795                                       ,P_update_agreement_amt_flag  IN     VARCHAR2  DEFAULT  NULL -- Indicates whether to  update the agreement amt or not. Null is considered as N
2796                                       ,P_funding_category           IN     VARCHAR2  DEFAULT  NULL -- The funding category for the agreement
2797                                       ,p_raTxn_rollup_api_call_flag IN     VARCHAR2 -- Indicates whether the pa_resource_asgn_curr maintenance api should be called
2798                                       ,x_return_status              OUT    NOCOPY VARCHAR2 -- Indicates the exit status of the API --File.Sql.39 bug 4440895
2799                                       ,x_msg_data                   OUT    NOCOPY VARCHAR2 -- Indicates the error occurred --File.Sql.39 bug 4440895
2800                                       ,X_msg_count                  OUT    NOCOPY NUMBER)  -- Indicates the number of error messages --File.Sql.39 bug 4440895
2801 IS
2802 
2803    -- Start of variables used for debugging purpose
2804 
2805      l_msg_count          NUMBER :=0;
2806      l_data               VARCHAR2(2000);
2807      l_msg_data           VARCHAR2(2000);
2808      l_msg_index_out      NUMBER;
2809      l_return_status      VARCHAR2(2000);
2810      l_debug_mode         VARCHAR2(1) :=P_PA_DEBUG_MODE;
2811      l_debug_level3       CONSTANT NUMBER := 3;
2812      l_debug_level5       CONSTANT NUMBER := 5;
2813      l_module_name        VARCHAR2(100) := 'PAFPCIMB.implement_ci_into_single_ver' ;
2814      l_token_name         VARCHAR2(30) :='PROCEDURENAME';
2815      l_msg_code           VARCHAR2(2000);
2816 
2817      -- End of variables used for debugging purpose
2818 
2819      l_Projfunc_Currency_Code    pa_projects_all.projfunc_currency_code%TYPE := NULL;
2820      l_Project_Currency_Code     pa_projects_all.project_currency_code%TYPE := NULL;
2821      l_Txn_Currency_Code         pa_projects_all.projfunc_currency_code%TYPE := NULL;
2822      l_baseline_funding_flag     pa_projects_all.baseline_funding_flag%TYPE;
2823      l_cost_impl_flag            VARCHAR2(1);
2824      l_rev_impl_flag             VARCHAR2(1);
2825      l_cost_impact_impl_flag     VARCHAR2(1);
2826      l_rev_impact_impl_flag      VARCHAR2(1);
2827      l_partially_impl_flag       VARCHAR2(1);
2828      l_agreement_num             pa_agreements_all.agreement_num%TYPE;
2829      l_approved_fin_pt_id        pa_fin_plan_types_b.fin_plan_type_id%TYPE;
2830      l_call_rep_lines_api        VARCHAR2(1):='N';
2831      l_id_before_bl_insertion    pa_budget_lines.budget_line_id%TYPE;
2832      l_id_after_bl_insertion     pa_budget_lines.budget_line_id%TYPE;
2833      l_dummy                     NUMBER;
2834 
2835 
2836 
2837 
2838 
2839      I   NUMBER;
2840 
2841      l_fp_version_ids_tbl SYSTEM.pa_num_tbl_type         := SYSTEM.pa_num_tbl_type();
2842 
2843      l_src_ver_id_tbl     SYSTEM.pa_num_tbl_type         := SYSTEM.pa_num_tbl_type();
2844      l_impl_type_tbl      SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2845      l_impl_qty_tbl       SYSTEM.PA_VARCHAR2_1_TBL_TYPE  := SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2846 
2847      l_impact_type_code                 pa_ci_impacts.impact_type_code%TYPE;
2848 
2849      l_record_version_number            pa_budget_versions.record_version_number%TYPE;
2850      l_partial_factor                   NUMBER := 1;
2851      l_impl_amt                         pa_fp_merged_ctrl_items.impl_proj_revenue%TYPE;
2852      l_total_amt                        pa_budget_versions.total_project_revenue%TYPE;
2853      l_total_amt_in_pfc                 pa_budget_lines.revenue%TYPE;
2854      l_total_amt_in_pc                  pa_budget_lines.project_revenue%TYPE;
2855 
2856      l_src_proj_fp_options_id           pa_proj_fp_options.proj_fp_options_id%TYPE;
2857      l_src_multi_curr_flag              pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
2858      l_src_fin_plan_type_id             pa_proj_fp_options.fin_plan_type_id%TYPE;
2859      l_src_time_phased_code             pa_proj_fp_options.cost_time_phased_code%TYPE;
2860      l_src_report_lbr_hrs_frm_code      pa_proj_fp_options.report_labor_hrs_from_code%TYPE;
2861 
2862 
2863      l_targ_proj_fp_options_id          pa_proj_fp_options.proj_fp_options_id%TYPE;
2864      l_targ_multi_curr_flag             pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
2865 
2866      l_targ_time_phased_code            pa_proj_fp_options.cost_time_phased_code%TYPE;
2867      L_REPORT_COST_USING                pa_proj_fp_options.report_labor_hrs_from_code%TYPE;
2868      l_targ_app_rev_flag                pa_proj_fp_options.report_labor_hrs_from_code%TYPE;
2869      l_copy_pfc_for_txn_amt_flag        VARCHAR2(1);
2870 
2871      l_project_id                       pa_proj_fp_options.project_id%TYPE;
2872 
2873      l_txn_curr_code_tbl                SYSTEM.pa_varchar2_15_tbl_type := SYSTEM.pa_varchar2_15_tbl_type();
2874      l_src_resource_list_id             pa_proj_fp_options.cost_resource_list_id%TYPE;
2875      l_targ_resource_list_id            pa_proj_fp_options.cost_resource_list_id%TYPE;
2876      l_rbs_version_id                   pa_proj_fp_options.rbs_version_id%TYPE;
2877      l_targ_plan_level_code             pa_proj_fp_options.cost_fin_plan_level_code%TYPE;
2878      l_src_plan_level_code              pa_proj_fp_options.cost_fin_plan_level_code%TYPE;
2879 
2880      l_txn_source_id_tbl                system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2881      l_res_list_member_id_tbl           system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2882      l_rbs_element_id_prm_tbl           system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2883      l_txn_accum_header_id_prm_tbl      system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2884 
2885  -- for Start bug 5291484
2886          l_txn_source_id_tbl_1             system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2887          ltxnaccumheader_id_prm_tbl_1      system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2888          l_res_list_member_id_tbl_1        system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2889          l_rbs_element_id_prm_tbl_1        system.PA_NUM_TBL_TYPE:= system.PA_NUM_TBL_TYPE();
2890 -- for End  bug 5291484
2891 
2892 
2893      -- Start of tables used to bulk collect and insert into pa_resource_assignments
2894      L_TARG_RLM_ID_TBL                  SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2895      L_TARG_RA_ID_TBL                   SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2896 	 L_TARG_CBS_ELEM_ID_TBL             SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();  --bug#16200605
2897      L_RA_DML_CODE_TBL                  SYSTEM.PA_VARCHAR2_15_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_15_TBL_TYPE();
2898      L_targ_task_id_tbl                 SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2899      l_src_ra_id_cnt_tbl                SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2900      l_planning_start_date_tbl          SYSTEM.pa_date_tbl_type := SYSTEM.pa_date_tbl_type();
2901      l_planning_end_date_tbl            SYSTEM.pa_date_tbl_type := SYSTEM.pa_date_tbl_type();
2902      l_targ_rbs_element_id_tbl          SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2903      l_targ_spread_curve_id_tbl         SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2904      l_targ_etc_method_code_tbl         SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2905      l_targ_fc_res_type_code_tbl        SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2906      l_targ_organization_id_tbl         SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2907      l_targ_job_id_tbl                  SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2908      l_targ_person_id_tbl               SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2909      l_targ_expenditure_type_tbl        SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2910      l_targ_expend_category_tbl         SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2911      l_targ_rev_category_code_tbl       SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2912      l_targ_event_type_tbl              SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2913      l_targ_supplier_id_tbl             SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2914      l_targ_project_role_id_tbl         SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2915      l_targ_resource_type_code_tbl      SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2916      l_targ_person_type_code_tbl        SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2917      l_targ_non_labor_resource_tbl      SYSTEM.PA_VARCHAR2_20_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_20_TBL_TYPE();
2918      l_targ_bom_resource_id_tbl         SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2919      l_targ_inventory_item_id_tbl       SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2920      l_targ_item_category_id_tbl        SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2921      l_targ_INCURED_BY_RES_FLAG_tbl     SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2922      l_targ_RATE_BASED_FLAG_tbl         SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2923       l_targ_RES_RATE_BASED_FLAG_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE(); --IPM Architecture Enhancement
2924      l_targ_RESOURCE_CLASS_FLAG_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2925      l_targ_NAMED_ROLE_tbl              SYSTEM.PA_VARCHAR2_80_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_80_TBL_TYPE();
2926      l_targ_txn_accum_header_id_tbl     SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2927      l_targ_unit_of_measure_tbl         SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2928      l_targ_RESOURCE_CLASS_CODE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2929      l_targ_assignment_description      SYSTEM.PA_VARCHAR2_240_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_240_TBL_TYPE();
2930      l_targ_mfc_cost_type_id_tbl        SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2931      l_targ_RATE_JOB_ID_tbl             SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2932      l_targ_RATE_EXPEND_TYPE_tbl        SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2933      l_targ_RATE_EXP_FC_CUR_COD_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2934      l_targ_RATE_EXPEND_ORG_ID_tbl      SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2935      l_targ_INCR_BY_RES_CLS_COD_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2936      l_targ_INCUR_BY_ROLE_ID_tbl        SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2937      l_targ_sp_fixed_date_tbl           SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE(); -- Bug 8350296
2938 
2939      -- End of tables used to bulk collect and insert into pa_resource_assignments
2940 
2941      --These pl/sql tbls will store the data corresponding to the details such as task_id, rate based flag
2942      --etc of RAs that have got updated. Bug 3678314.
2943      l_upd_ra_task_id_tbl               SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2944      l_upd_ra_rbs_elem_id_tbl           SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2945 	 l_upd_ra_cbs_elem_id_tbl           SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE(); --bug#16911079
2946      l_upd_ra_res_class_code_tbl        SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2947      l_upd_ra_rbf_tbl                   SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2948      l_upd_ra_res_asmt_id_tbl           SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2949 
2950 
2951      l_index                                    NUMBER;
2952      l_matching_index                           NUMBER;
2953      -- Start of local Variables for calling get_resource_defaults API
2954      l_da_resource_list_members_tab             SYSTEM.PA_NUM_TBL_TYPE:=SYSTEM.PA_NUM_TBL_TYPE();
2955      l_da_resource_class_flag_tab               SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=   SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2956      l_da_resource_class_code_tab               SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2957      l_da_resource_class_id_tab                 SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2958      l_da_res_type_code_tab                     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2959      l_da_person_id_tab                         SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2960      l_da_job_id_tab                            SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2961      l_da_person_type_code_tab                  SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2962      l_da_named_role_tab                        SYSTEM.PA_VARCHAR2_80_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_80_TBL_TYPE();
2963      l_da_bom_resource_id_tab                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2964      l_da_non_labor_resource_tab                SYSTEM.PA_VARCHAR2_20_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_20_TBL_TYPE();
2965      l_da_inventory_item_id_tab                 SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2966      l_da_item_category_id_tab                  SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2967      l_da_project_role_id_tab                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2968      l_da_organization_id_tab                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2969      l_da_fc_res_type_code_tab                  SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2970      l_da_expenditure_type_tab                  SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2971      l_da_expenditure_category_tab              SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2972      l_da_event_type_tab                        SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2973      l_da_revenue_category_code_tab             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2974      l_da_supplier_id_tab                       SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2975      l_da_spread_curve_id_tab                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2976      l_da_etc_method_code_tab                   SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2977      l_da_mfc_cost_type_id_tab                  SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2978      l_da_incurred_by_res_flag_tab              SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=   SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2979      l_da_incur_by_res_cls_code_tab             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2980      l_da_incur_by_role_id_tab                  SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2981      l_da_unit_of_measure_tab                   SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2982      l_da_org_id_tab                            SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2983      l_da_rate_based_flag_tab                   SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=   SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2984      l_da_rate_expenditure_type_tab             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2985      l_da_rate_func_curr_code_tab               SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2986      l_da_incur_by_res_type_tab                 SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2987 
2988   --for Start bug 5291484
2989         lresource_list_members_tab_1             SYSTEM.PA_NUM_TBL_TYPE:= SYSTEM.PA_NUM_TBL_TYPE();
2990         lresource_class_flag_tab_1               SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=   SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
2991         lresource_class_code_tab_1               SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2992         lresource_class_id_tab_1                 SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2993         lres_type_code_tab_1                     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2994         lperson_id_tab_1                         SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2995         ljob_id_tab_1                            SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2996         lperson_type_code_tab_1                  SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
2997         lnamed_role_tab_1                        SYSTEM.PA_VARCHAR2_80_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_80_TBL_TYPE();
2998         lbom_resource_id_tab_1                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
2999         lnon_labor_resource_tab_1                SYSTEM.PA_VARCHAR2_20_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_20_TBL_TYPE();
3000         linventory_item_id_tab_1                 SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3001         litem_category_id_tab_1                  SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3002         lproject_role_id_tab_1                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3003         lorganization_id_tab_1                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3004         lfc_res_type_code_tab_1                  SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3005         lexpenditure_type_tab_1                  SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3006         lexpenditure_category_tab_1              SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3007         l_da_event_type_tab_1                    SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3008         lrevenue_category_code_tab_1             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3009         l_da_supplier_id_tab_1                   SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3010         l_da_spread_curve_id_tab_1               SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3011         l_da_etc_method_code_tab_1               SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3012         l_da_mfc_cost_type_id_tab_1              SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3013         lincurred_by_res_flag_tab_1              SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=   SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
3014         lincur_by_res_cls_code_tab_1             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3015         l_da_incur_by_role_id_tab_1              SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3016         l_da_unit_of_measure_tab_1               SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3017         l_da_org_id_tab_1                        SYSTEM.PA_NUM_TBL_TYPE:=          SYSTEM.PA_NUM_TBL_TYPE();
3018         l_da_rate_based_flag_tab_1               SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=   SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
3019         lrate_expenditure_type_tab_1             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3020         l_da_rate_func_curr_code_tab_1           SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3021         l_da_incur_by_res_type_tab_1             SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3022         ltxnsrctyp_code_rbs_prm_tbl_1            SYSTEM.pa_varchar2_30_tbl_type:=  SYSTEM.pa_varchar2_30_tbl_type();
3023 
3024    --for End bug 5291484
3025 
3026 
3027 
3028      -- Endof local Variables for calling get_resource_defaults API
3029 
3030      l_project_structure_version_id       pa_proj_element_versions.parent_structure_version_id%TYPE;
3031 
3032      -- Start of variable to be used in Calculate API Call
3033      l_line_start_date_tbl         SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3034      l_line_end_date_tbl           SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3035 
3036      l_currency_code_tbl               SYSTEM.PA_VARCHAR2_15_TBL_TYPE:= SYSTEM.PA_VARCHAR2_15_TBL_TYPE();
3037      l_total_quantity_tbl              SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3038      l_total_raw_cost_tbl              SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3039      l_total_burdened_cost_tbl         SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3040      l_total_revenue_tbl               SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3041      l_cost_rate_tbl                   SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3042      l_burden_multiplier_tbl           SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3043      l_bill_rate_tbl                   SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3044      l_txn_src_typ_code_rbs_prm_tbl    SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3045      l_prm_bl_start_date_tbl           SYSTEM.PA_DATE_TBL_TYPE       :=SYSTEM.PA_DATE_TBL_TYPE();
3046      l_prm_bl_end_date_tbl             SYSTEM.PA_DATE_TBL_TYPE       :=SYSTEM.PA_DATE_TBL_TYPE();
3047      l_period_name_tbl                 SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3048      l_pc_raw_cost_tbl                 SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3049      l_pc_burd_cost_tbl                SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3050      l_pc_revenue_tbl                  SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3051      l_pfc_raw_cost_tbl                SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3052      l_pfc_burd_cost_tbl               SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3053      l_pfc_revenue_tbl                 SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3054      l_cost_rejection_code             SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3055      l_revenue_rejection_code          SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3056      l_burden_rejection_code           SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3057      l_other_rejection_code            SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3058      l_pc_cur_conv_rejection_code      SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3059      l_pfc_cur_conv_rejection_code     SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3060      l_pji_prm_task_id_tbl             SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3061      l_pji_prm_rbs_elem_id_tbl         SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3062 	 l_pji_prm_cbs_elem_id_tbl         SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE(); --bug#16911079
3063      l_pji_prm_res_cls_code_tbl        SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3064      l_pji_prm_rbf_tbl                 SYSTEM.pa_varchar2_1_tbl_type :=SYSTEM.pa_varchar2_1_tbl_type();
3065      l_upd_bl_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE        := SYSTEM.PA_NUM_TBL_TYPE();
3066      l_upd_period_name_tbl             SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3067      l_upd_currency_code_tbl           SYSTEM.pa_varchar2_15_tbl_type:=SYSTEM.pa_varchar2_15_tbl_type();
3068      l_upd_bl_start_date_tbl           SYSTEM.PA_DATE_TBL_TYPE       :=SYSTEM.PA_DATE_TBL_TYPE();
3069      l_upd_bl_end_date_tbl             SYSTEM.PA_DATE_TBL_TYPE       :=SYSTEM.PA_DATE_TBL_TYPE();
3070      l_upd_cost_rejection_code         SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3071      l_upd_revenue_rejection_code      SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3072      l_upd_burden_rejection_code       SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3073      l_upd_other_rejection_code        SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3074      l_upd_pc_cur_conv_rej_code        SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3075      l_upd_pfc_cur_conv_rej_code       SYSTEM.pa_varchar2_30_tbl_type:=SYSTEM.pa_varchar2_30_tbl_type();
3076 
3077      l_res_assignment_id_tbl           SYSTEM.pa_num_tbl_type        := SYSTEM.pa_num_tbl_type();
3078      l_delete_budget_lines_tbl         SYSTEM.pa_varchar2_1_tbl_type:=SYSTEM.pa_varchar2_1_tbl_type();
3079      L_SPREAD_AMOUNT_FLAGS_TBL         SYSTEM.pa_varchar2_1_tbl_type:=SYSTEM.pa_varchar2_1_tbl_type();
3080 
3081 
3082       -- End of variable to be used in Calculate API Call
3083 
3084 
3085 
3086      l_proj_raw_cost          pa_resource_assignments.TOTAL_PROJECT_RAW_COST%TYPE;
3087      l_proj_burdened_cost     pa_resource_assignments.TOTAL_PROJECT_BURDENED_COST%TYPE;
3088      l_proj_revenue           pa_resource_assignments.TOTAL_PROJECT_REVENUE%TYPE;
3089 
3090      l_projfunc_raw_cost      pa_resource_assignments.TOTAL_PLAN_RAW_COST%TYPE;
3091      l_projfunc_burdened_cost pa_resource_assignments.TOTAL_PLAN_BURDENED_COST%TYPE;
3092      l_projfunc_revenue       pa_resource_assignments.TOTAL_PLAN_REVENUE%TYPE;
3093      l_labor_quantity         pa_resource_assignments.TOTAL_PLAN_QUANTITY%TYPE;
3094      l_equip_quantity         pa_resource_assignments.TOTAL_PLAN_QUANTITY%TYPE;
3095 
3096 
3097 
3098 
3099      -- Start of tables prepared to insert/update into pa_budget_lines
3100      l_bl_RESOURCE_ASIGNMENT_ID_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3101      l_upd_ra_bl_dml_code_tbl          SYSTEM.PA_VARCHAR2_15_TBL_TYPE := SYSTEM.PA_VARCHAR2_15_TBL_TYPE();
3102      l_bl_START_DATE_tbl               SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3103      l_bl_END_DATE_tbl                 SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3104      l_bl_PERIOD_NAME_tbl              SYSTEM.PA_VARCHAR2_30_TBL_TYPE :=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3105      l_bl_QUANTITY_tbl                 SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3106      l_bl_RAW_COST_tbl                 SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3107      l_bl_BURDENED_COST_tbl            SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3108      l_bl_REVENUE_tbl                  SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3109      l_bl_CHANGE_REASON_CODE_tbl       SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3110      l_bl_DESCRIPTION_tbl              SYSTEM.PA_VARCHAR2_2000_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_2000_TBL_TYPE();
3111      l_bl_ATTRIBUTE_CATEGORY_tbl       SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3112      l_bl_ATTRIBUTE1_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3113      l_bl_ATTRIBUTE2_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3114      l_bl_ATTRIBUTE3_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3115      l_bl_ATTRIBUTE4_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3116      l_bl_ATTRIBUTE5_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3117      l_bl_ATTRIBUTE6_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3118      l_bl_ATTRIBUTE7_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3119      l_bl_ATTRIBUTE8_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3120      l_bl_ATTRIBUTE9_tbl               SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3121      l_bl_ATTRIBUTE10_tbl              SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3122      l_bl_ATTRIBUTE11_tbl              SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3123      l_bl_ATTRIBUTE12_tbl              SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3124      l_bl_ATTRIBUTE13_tbl              SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3125      l_bl_ATTRIBUTE14_tbl              SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3126      l_bl_ATTRIBUTE15_tbl              SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3127      l_bl_PM_PRODUCT_CODE_tbl          SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3128      l_bl_PM_BUDGET_LINE_REF_tbl       SYSTEM.PA_VARCHAR2_150_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_150_TBL_TYPE();
3129      l_bl_COST_REJECTION_CODE_tbl      SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3130      l_bl_REVENUE_REJ_CODE_tbl         SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3131      l_bl_BURDEN_REJECTION_CODE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3132      l_bl_OTHER_REJECTION_CODE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3133      l_bl_CODE_COMBINATION_ID_tbl      SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3134      l_bl_CCID_GEN_STATUS_CODE_tbl     SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
3135      l_bl_CCID_GEN_REJ_MESSAGE_tbl     SYSTEM.PA_VARCHAR2_2000_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_2000_TBL_TYPE();
3136      l_bl_REQUEST_ID_tbl               SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3137      l_bl_BORROWED_REVENUE_tbl         SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3138      l_bl_TP_REVENUE_IN_tbl            SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3139      l_bl_TP_REVENUE_OUT_tbl           SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3140      l_bl_REVENUE_ADJ_tbl              SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3141      l_bl_LENT_RESOURCE_COST_tbl       SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3142      l_bl_TP_COST_IN_tbl               SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3143      l_bl_TP_COST_OUT_tbl              SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3144      l_bl_COST_ADJ_tbl                 SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3145      l_bl_UNASSIGNED_TIME_COST_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3146      l_bl_UTILIZATION_PERCENT_tbl      SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3147      l_bl_UTILIZATION_HOURS_tbl        SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3148      l_bl_UTILIZATION_ADJ_tbl          SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3149      l_bl_CAPACITY_tbl                 SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3150      l_bl_HEAD_COUNT_tbl               SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3151      l_bl_HEAD_COUNT_ADJ_tbl           SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3152      l_bl_PROJFUNC_CUR_CODE_tbl        SYSTEM.PA_VARCHAR2_15_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_15_TBL_TYPE();
3153      l_bl_PROJFUNC_COST_RAT_TYP_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3154      l_bl_PJFN_COST_RAT_DAT_TYP_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3155      l_bl_PROJFUNC_COST_RAT_DAT_tbl     SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3156      l_bl_PROJFUNC_REV_RATE_TYP_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3157      l_bl_PJFN_REV_RAT_DAT_TYPE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3158      l_bl_PROJFUNC_REV_RAT_DATE_tbl     SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3159      l_bl_PROJECT_COST_RAT_TYPE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3160      l_bl_PROJ_COST_RAT_DAT_TYP_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3161      l_bl_PROJ_COST_RATE_DATE_tbl      SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3162      l_bl_PROJECT_RAW_COST_tbl         SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3163      l_bl_PROJECT_BURDENED_COST_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3164      l_bl_PROJECT_REV_RATE_TYPE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3165      l_bl_PRJ_REV_RAT_DATE_TYPE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3166      l_bl_PROJECT_REV_RATE_DATE         SYSTEM.PA_DATE_TBL_TYPE           := SYSTEM.PA_DATE_TBL_TYPE();
3167      l_bl_PROJECT_REVENUE_tbl          SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3168      l_bl_TXN_CURRENCY_CODE_tbl        SYSTEM.PA_VARCHAR2_15_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_15_TBL_TYPE();
3169      l_bl_TXN_RAW_COST_tbl             SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3170      l_bl_TXN_BURDENED_COST_tbl        SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3171      l_bl_TXN_REVENUE_tbl              SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3172      l_bl_BUCKETING_PERIOD_CODE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3173      l_bl_TXN_STD_COST_RATE_tbl        SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3174      l_bl_TXN_COST_RATE_OVERIDE_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3175      l_bl_COST_IND_CMPLD_SET_ID_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3176      l_bl_TXN_BURDEN_MULTIPLIER_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3177      l_bl_TXN_BRD_MLTIPLI_OVRID_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3178      l_bl_TXN_STD_BILL_RATE_tbl        SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3179      l_bl_TXN_BILL_RATE_OVERRID_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3180      l_bl_TXN_MARKUP_PERCENT_tbl       SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3181      l_bl_TXN_MRKUP_PER_OVERIDE_tbl     SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3182      l_bl_TXN_DISC_PERCENTAGE_tbl      SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3183      l_bl_TRANSFER_PRICE_RATE_tbl      SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3184      l_bl_BURDEN_COST_RATE_tbl         SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3185      l_bl_BURDEN_COST_RAT_OVRID_tbl    SYSTEM.PA_NUM_TBL_TYPE :=          SYSTEM.PA_NUM_TBL_TYPE();
3186      l_bl_PC_CUR_CONV_REJ_CODE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3187      l_bl_PFC_CUR_CONV_REJ_CODE_tbl     SYSTEM.PA_VARCHAR2_30_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
3188      l_bl_rbf_flag_tbl                  SYSTEM.PA_VARCHAR2_1_TBL_TYPE:=  SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
3189 
3190      -- End of tables prepared to insert/update into pa_budget_lines
3191 
3192      l_ci_cost_version_id     pa_budget_versions.budget_version_id%TYPE;
3193      l_ci_rev_version_id      pa_budget_versions.budget_version_id%TYPE;
3194      l_ci_all_version_id      pa_budget_versions.budget_version_id%TYPE;
3195 
3196      l_etc_start_date         pa_budget_versions.etc_start_date%TYPE;
3197      L_TARG_APP_COST_FLAG     pa_proj_fp_options.approved_cost_plan_type_flag%TYPE;
3198 
3199      L_SAME_MULTI_CURR_FLAG   VARCHAR2(1);
3200      l_amt_used_for_rate_calc NUMBER;
3201 
3202       CURSOR c_proj_level_amounts IS
3203           SELECT nvl(total_project_raw_cost,0)
3204                 ,nvl(total_project_burdened_cost,0)
3205                 ,nvl(total_project_revenue,0)
3206                 ,nvl(raw_cost,0)
3207                 ,nvl(burdened_cost,0)
3208                 ,nvl(revenue,0)
3209                 ,nvl(labor_quantity,0)
3210                 ,nvl(equipment_quantity,0)
3211            FROM  pa_budget_versions
3212           WHERE  budget_version_id = p_budget_version_id;
3213 
3214 
3215      -- Variables for calling FP_CI_LINK_CONTROL_ITEMS
3216      l_version_type                  pa_budget_versions.version_type%TYPE;
3217      l_cost_ppl_qty                  pa_fp_merged_ctrl_items.impl_quantity%TYPE;
3218      l_rev_ppl_qty                   pa_fp_merged_ctrl_items.impl_quantity%TYPE;
3219      l_cost_equip_qty                pa_fp_merged_ctrl_items.impl_equipment_quantity%TYPE;
3220      l_rev_equip_qty                 pa_fp_merged_ctrl_items.impl_equipment_quantity%TYPE;
3221      l_impl_pfc_raw_cost             pa_fp_merged_ctrl_items.impl_proj_func_raw_cost%TYPE;
3222      l_impl_pfc_revenue              pa_fp_merged_ctrl_items.impl_proj_func_revenue%TYPE;
3223      l_impl_pfc_burd_cost            pa_fp_merged_ctrl_items.impl_proj_func_burdened_cost%TYPE;
3224      l_impl_pc_raw_cost              pa_fp_merged_ctrl_items.impl_proj_raw_cost%TYPE;
3225      l_impl_pc_revenue               pa_fp_merged_ctrl_items.impl_proj_revenue%TYPE;
3226      l_impl_pc_burd_cost             pa_fp_merged_ctrl_items.impl_proj_burdened_cost%TYPE;
3227      l_target_version_type           pa_budget_versions.version_type%TYPE;
3228      l_baselined_fp_options_id       pa_proj_fp_options.proj_fp_options_id%TYPE;
3229      l_baselined_version_id          pa_budget_versions.budget_version_id%TYPE;
3230      l_orig_record_version_number    pa_budget_versions.record_version_number%TYPE;
3231      l_fc_version_created_flag       VARCHAR2(1);
3232      l_targ_lab_qty_before_merge     pa_budget_versions.labor_quantity%TYPE;
3233      l_targ_eqp_qty_before_merge     pa_budget_versions.equipment_quantity%TYPE;
3234      l_targ_pfc_rawc_before_merge    pa_budget_versions.raw_cost%TYPE;
3235      l_targ_pfc_burdc_before_merge   pa_budget_versions.burdened_cost%TYPE;
3236      l_targ_pfc_rev_before_merge     pa_budget_versions.revenue%TYPE;
3237      l_targ_pc_rawc_before_merge     pa_budget_versions.total_project_raw_cost%TYPE;
3238      l_targ_pc_burdc_before_merge    pa_budget_versions.total_project_burdened_cost%TYPE;
3239      l_targ_pc_rev_before_merge      pa_budget_versions.total_project_revenue%TYPE;
3240      l_targ_lab_qty_after_merge      pa_budget_versions.labor_quantity%TYPE;
3241      l_targ_eqp_qty_after_merge      pa_budget_versions.equipment_quantity%TYPE;
3242      l_targ_pfc_rawc_after_merge     pa_budget_versions.raw_cost%TYPE;
3243      l_targ_pfc_burdc_after_merge    pa_budget_versions.burdened_cost%TYPE;
3244      l_targ_pfc_rev_after_merge      pa_budget_versions.revenue%TYPE;
3245      l_targ_pc_rawc_after_merge      pa_budget_versions.total_project_raw_cost%TYPE;
3246      l_targ_pc_burdc_after_merge     pa_budget_versions.total_project_burdened_cost%TYPE;
3247      l_targ_pc_rev_after_merge       pa_budget_versions.total_project_revenue%TYPE;
3248      l_impl_earlier                  VARCHAR2(1);
3249      l_total_agr_revenue             pa_budget_lines.txn_revenue%TYPE;
3250 
3251      l_partial_impl_rev_amt          NUMBER;
3252      l_partial_rev_impl_flag         VARCHAR2(1);
3253 
3254      --Variable used for passing ci id to the change management baseline API.
3255      l_CI_ID_Tab                     PA_PLSQL_DATATYPES.IdTabTyp;
3256      l_temp                          NUMBER;
3257      X_Err_Code                      NUMBER;
3258      l_current_working_flag          pa_budget_versions.current_working_flag%TYPE;
3259 
3260      -- variables introduced for bug 3934574
3261      l_retain_manual_lines_flag     VARCHAR2(1);
3262 
3263    -- Start of variables declared for bug 4035856
3264      l_src_delta_amt_adj_task_id    pa_tasks.task_id%TYPE;
3265      l_targ_delta_amt_adj_rlm_id    pa_resource_list_members.resource_list_member_id%TYPE;
3266      l_src_delta_amt_adj_ra_id      pa_resource_assignments.resource_assignment_id%TYPE;
3267      l_src_delta_amt_adj_start_date pa_budget_lines.start_date%TYPE;
3268      l_pc_revenue_delta             pa_budget_lines.project_revenue%TYPE;
3269      l_pfc_revenue_delta            pa_budget_lines.revenue%TYPE;
3270      l_pc_rev_merged                pa_budget_lines.project_revenue%TYPE;
3271      l_pfc_rev_merged               pa_budget_lines.revenue%TYPE;
3272      l_pc_rev_for_merge             pa_budget_lines.project_revenue%TYPE;
3273      l_pfc_rev_for_merge            pa_budget_lines.revenue%TYPE;
3274      l_src_dummy1                   number;
3275      l_src_dummy2                   pa_resource_list_members.alias%TYPE;
3276      l_impl_proj_func_revenue       pa_fp_merged_ctrl_items.impl_proj_func_revenue%TYPE;-- bug 4035856
3277      l_impl_proj_revenue            pa_fp_merged_ctrl_items.impl_proj_revenue%TYPE;-- bug 4035856
3278      l_impl_quantity                pa_fp_merged_ctrl_items.impl_quantity%TYPE;-- bug 4035856
3279      l_agreement_id                 pa_agreements_all.agreement_id%TYPE;
3280      l_agreement_currency_code      pa_agreements_all.agreement_currency_code%TYPE;
3281      l_rounded_bl_id                pa_budget_lines.budget_line_id%TYPE;
3282      l_rounded_bl_rbf               pa_resource_assignments.rate_based_flag%TYPE;
3283      l_qty_adjusted_flag            VARCHAR2(1);
3284      --These variables will be used in calling create_ci_impact_fund_lines
3285      l_impl_pc_rev_amt              NUMBER;
3286      l_impl_pfc_rev_amt             NUMBER;
3287 
3288      --This variable will be used to call pa_resource_asgn_curr maintenance api
3289      l_fp_cols_rec                  PA_FP_GEN_AMOUNT_UTILS.FP_COLS;
3290 
3291      -- Cursor to identify resource assignment id against which the adjustment amount should be placed
3292      -- The idea is to choose the last ra in the edit plan page in the default view for ci version
3293      -- Added a condition to choose only the RA having the budget lines as the LAST RA.This cursor will be
3294      -- used in the case when both source and target have the same resource lists.(Please note that any changes
3295      -- in this cursor might have to be done in src_delta_amt_adj_ra_cur1 also)
3296      CURSOR src_delta_amt_adj_ra_cur
3297          (c_budget_version_id pa_budget_versions.budget_version_id%TYPE)IS
3298      SELECT pra.task_id, pra.resource_list_member_id, pra.resource_assignment_id,
3299             PA_PROJ_ELEMENTS_UTILS.GET_DISPLAY_SEQUENCE(pra.task_id) as dispSeq, rlm.alias
3300      FROM   pa_resource_assignments pra, pa_resource_list_members rlm
3301      WHERE  pra.budget_version_id = c_budget_version_id
3302      AND    rlm.resource_list_member_id = pra.resource_list_member_id
3303      AND    EXISTS (SELECT 1
3304                     FROM   pa_budget_lines pbl
3305                     WHERE  pbl.resource_assignment_id = pra.resource_assignment_id)
3306      ORDER BY dispSeq DESC , rlm.alias DESC;
3307 
3308      --This cursor is same as src_delta_amt_adj_ra_cur. This will be used when the source and target
3309      --have different resource lists.(Please note that any changes in this cursor might have to be
3310      --done in src_delta_amt_adj_ra_cur also)
3311      CURSOR src_delta_amt_adj_ra_cur1
3312          (c_budget_version_id pa_budget_versions.budget_version_id%TYPE)IS
3313      SELECT pra.task_id, tmp4.resource_list_member_id, pra.resource_assignment_id,
3314             PA_PROJ_ELEMENTS_UTILS.GET_DISPLAY_SEQUENCE(pra.task_id) as dispSeq, rlm.alias
3315      FROM   pa_resource_assignments pra, pa_resource_list_members rlm,pa_res_list_map_tmp4 tmp4
3316      WHERE  pra.budget_version_id = c_budget_version_id
3317      AND    pra.resource_assignment_id=tmp4.txn_source_id
3318      AND    rlm.resource_list_member_id = tmp4.resource_list_member_id
3319      AND    EXISTS (SELECT 1
3320                     FROM   pa_budget_lines pbl
3321                     WHERE  pbl.resource_assignment_id = pra.resource_assignment_id)
3322      ORDER BY dispSeq DESC , rlm.alias DESC;
3323 
3324      -- Cursor to identify against which period/date the adjustment amount should be placed
3325      -- Note that revenue only change orders can have amounts against agreement currency only.
3326      CURSOR src_delta_amt_adj_date_cur
3327          (c_resource_assignment_id pa_resource_assignments.resource_assignment_id%TYPE)IS
3328      SELECT max(start_date)
3329      FROM   pa_budget_lines
3330      WHERE  resource_assignment_id = c_resource_assignment_id;
3331 
3332 
3333    -- End of variables declared for bug 4035856
3334 
3335      --For Bug 3980129. These variables will hold the values that indicate whether cost/revenue implementation is OK or NOT
3336      --l_cost_impl_flag , l_rev_impl_flag will also have the values. But l_rev_impl_flag will be set to N when cost
3337      --is being impelemented and l_cost_impl_flag will be set to N when revenue is being implemented. If cost gets
3338      --implemented first then l_rev_impl_flag will be set to l_derv_rev_impl_flag when revenue gets implemented and
3339      --vice-versa.
3340      l_derv_cost_impl_flag          VARCHAR2(1);
3341      l_derv_rev_impl_flag           VARCHAR2(1);
3342 
3343      l_spread_curve_id              pa_spread_curves_b.spread_curve_id%TYPE; -- Bug 8350296
3344 
3345 BEGIN
3346      FND_MSG_PUB.initialize;
3347 
3348      IF p_pa_debug_mode = 'Y' THEN
3349      pa_debug.set_curr_function( p_function   => 'implement_ci_into_single_ver',
3350                                      p_debug_mode => P_PA_debug_mode );
3351      END IF;
3352      x_msg_count := 0;
3353      x_return_status := FND_API.G_RET_STS_SUCCESS;
3354 
3355      SAVEPOINT implement_ci_into_single_ver;
3356 
3357      --dbms_output.put_line('I1');
3358      IF P_PA_debug_mode = 'Y' THEN
3359            pa_debug.g_err_stage:= 'Entering implement_ci_into_single_ver';
3360            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3361      END IF;
3362 
3363      --dbms_output.put_line('I2');
3364      -- Derive the version ids for the change order if none of them is passed
3365      IF p_ci_cost_version_id IS NULL AND p_ci_rev_version_id IS NULL AND p_ci_all_version_id IS NULL THEN
3366          IF P_PA_debug_mode = 'Y' THEN
3367                pa_debug.g_err_stage:= 'Calling GET_CI_VERSIONS p_ci_id :'||p_ci_id;
3368                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3369          END IF;
3370          Pa_Fp_Control_Items_Utils.GET_CI_VERSIONS
3371                                           ( p_ci_id                   => p_ci_id
3372                                            ,X_cost_budget_version_id  => l_ci_cost_version_id
3373                                            ,X_rev_budget_version_id   => l_ci_rev_version_id
3374                                            ,X_all_budget_version_id   => l_ci_all_version_id
3375                                            ,x_return_status           => l_return_status
3376                                            ,x_msg_count               => l_msg_data
3377                                            ,x_msg_data                => l_msg_count);
3378 
3379           IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
3380                IF P_PA_debug_mode = 'Y' THEN
3381                     pa_debug.g_err_stage:= 'Error in GET_CI_VERSIONS';
3382                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3383                END IF;
3384                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3385           END IF;
3386      ELSE
3387           l_ci_cost_version_id     := p_ci_cost_version_id;
3388           l_ci_rev_version_id      := p_ci_rev_version_id;
3389           l_ci_all_version_id      := p_ci_all_version_id;
3390 
3391      END IF;
3392 
3393      -- Bug 8350296
3394      SELECT spread_curve_id INTO l_spread_curve_id
3395      FROM pa_spread_curves_b
3396      WHERE spread_curve_code = 'FIXED_DATE';
3397 
3398      --dbms_output.put_line('I3');
3399      BEGIN
3400           -- Select the details required from pa_budget_versions so that they can be used in the later part of the code
3401           SELECT  etc_start_date
3402                  ,project_id
3403                  ,version_type
3404                  ,PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(project_id )
3405                  ,nvl(labor_quantity,0)
3406                  ,nvl(equipment_quantity,0)
3407                  ,nvl(raw_cost,0)
3408                  ,nvl(burdened_cost,0)
3409                  ,nvl(revenue,0)
3410                  ,nvl(total_project_raw_cost,0)
3411                  ,nvl(total_project_burdened_cost,0)
3412                  ,nvl(total_project_revenue,0)
3413                  ,current_working_flag
3414           into    l_etc_start_date
3415                  ,l_project_id
3416                  ,l_target_version_type
3417                  ,l_project_structure_version_id
3418                  ,l_targ_lab_qty_before_merge
3419                  ,l_targ_eqp_qty_before_merge
3420                  ,l_targ_pfc_rawc_before_merge
3421                  ,l_targ_pfc_burdc_before_merge
3422                  ,l_targ_pfc_rev_before_merge
3423                  ,l_targ_pc_rawc_before_merge
3424                  ,l_targ_pc_burdc_before_merge
3425                  ,l_targ_pc_rev_before_merge
3426                  ,l_current_working_flag
3427           from   pa_budget_versions
3428           WHERE budget_version_id = p_budget_version_id;
3429 
3430          IF P_PA_debug_mode = 'Y' THEN
3431                pa_debug.g_err_stage:= 'Fetching l_etc_start_date :'||l_etc_start_date;
3432                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3433 
3434                pa_debug.g_err_stage:= 'Fetching l_project_id :'||l_project_id;
3435                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3436 
3437                pa_debug.g_err_stage:= 'Fetching l_target_version_type :'||l_target_version_type;
3438                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3439 
3440                pa_debug.g_err_stage:= 'Fetching l_project_structure_version_id :'||l_project_structure_version_id;
3441                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3442          END IF;
3443 
3444           -- Select the details required from pa_projects_all so that they can be used in the later part of the code
3445           SELECT project_currency_code
3446                 ,projfunc_currency_code
3447                 ,nvl(baseline_funding_flag,'N')
3448           INTO   l_project_currency_code
3449                 ,l_projfunc_currency_code
3450                 ,l_baseline_funding_flag
3451           FROM   pa_projects_all
3452           WHERE  project_id=l_project_id;
3453 
3454          IF P_PA_debug_mode = 'Y' THEN
3455                pa_debug.g_err_stage:= 'Fetching l_project_currency_code :'||l_project_currency_code;
3456                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3457 
3458                pa_debug.g_err_stage:= 'Fetching l_projfunc_currency_code :'||l_projfunc_currency_code;
3459                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3460 
3461                pa_debug.g_err_stage:= 'Fetching l_baseline_funding_flag :'||l_baseline_funding_flag;
3462                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3463          END IF;
3464 
3465           -- Select the details required from pa_proj_fp_options so that they can be used in the later part of the code
3466           SELECT proj_fp_options_id,
3467                  plan_in_multi_curr_flag,
3468                  nvl(cost_time_phased_code,nvl(revenue_time_phased_code,all_time_phased_code)),
3469                  margin_derived_from_code,
3470                  approved_cost_plan_type_flag,
3471                  approved_rev_plan_type_flag,
3472                  nvl(cost_resource_list_id,nvl(revenue_resource_list_id,all_resource_list_id)),
3473                  rbs_version_id,
3474                  nvl(cost_fin_plan_level_code,nvl(revenue_fin_plan_level_code,all_fin_plan_level_code)),
3475                  decode(fin_plan_preference_code, 'COST_ONLY',    gen_cost_ret_manual_line_flag,
3476                                   'REVENUE_ONLY', gen_rev_ret_manual_line_flag,
3477                                  'COST_AND_REV_SAME', gen_all_ret_manual_line_flag)
3478           INTO   l_targ_proj_fp_options_id,
3479                  l_targ_multi_curr_flag,
3480                  l_targ_time_phased_code,
3481                  L_REPORT_COST_USING,
3482                  l_targ_app_cost_flag,
3483                  l_targ_app_rev_flag,
3484                  l_targ_resource_list_id,
3485                  l_rbs_version_id,
3486                  l_targ_plan_level_code,
3487                  l_retain_manual_lines_flag -- bug 3934574
3488           FROM   pa_proj_fp_options
3489           WHERE  fin_plan_version_id = p_budget_version_id
3490           AND    fin_plan_type_id    = p_fin_plan_type_id;
3491 
3492           /*l_copy_pfc_for_txn_amt_flag will be set to Y if the amounts can be entered only in project functinal
3493            *Curreny in target. If the target is a change document then the amounts should always be copied in txn
3494            *currency and hence the flag will be set to N. If the target is not a change document and if it is a
3495            *approved revenue version then the amounts should always be copied in project functional currency and
3496            *hence the flag will be Y.
3497            */
3498           l_copy_pfc_for_txn_amt_flag:='N';
3499           IF p_context = 'CI_MERGE' THEN
3500                l_copy_pfc_for_txn_amt_flag := 'N';
3501           ELSIF l_targ_app_rev_flag='Y' THEN
3502                l_copy_pfc_for_txn_amt_flag :='Y';
3503           END IF;
3504 
3505          IF P_PA_debug_mode = 'Y' THEN
3506                pa_debug.g_err_stage:= 'Fetching l_targ_proj_fp_options_id :'||l_targ_proj_fp_options_id;
3507                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3508 
3509                pa_debug.g_err_stage:= 'Fetching l_targ_multi_curr_flag :'||l_targ_multi_curr_flag;
3510                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3511 
3512                pa_debug.g_err_stage:= 'Fetching l_targ_time_phased_code :'||l_targ_time_phased_code;
3513                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3514 
3515                pa_debug.g_err_stage:= 'Fetching L_REPORT_COST_USING :'||L_REPORT_COST_USING;
3516                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3517 
3518                pa_debug.g_err_stage:= 'Fetching l_targ_app_cost_flag :'||l_targ_app_cost_flag;
3519                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3520 
3521                pa_debug.g_err_stage:= 'Fetching l_targ_app_rev_flag :'||l_targ_app_rev_flag;
3522                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3523 
3524                pa_debug.g_err_stage:= 'Fetching l_copy_pfc_for_txn_amt_flag :'||l_copy_pfc_for_txn_amt_flag;
3525                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3526 
3527                pa_debug.g_err_stage:= 'Fetching l_targ_resource_list_id :'||l_targ_resource_list_id;
3528                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3529 
3530                pa_debug.g_err_stage:= 'Fetching l_rbs_version_id :'||l_rbs_version_id;
3531                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3532 
3533                pa_debug.g_err_stage:= 'Fetching l_targ_plan_level_code :'||l_targ_plan_level_code;
3534                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3535        END IF;
3536 
3537      EXCEPTION
3538           WHEN NO_DATA_FOUND THEN
3539               IF P_PA_debug_mode = 'Y' THEN
3540                     pa_debug.g_err_stage:='NO_DATA_FOUND while getting details required to be used in the later part of the code';
3541                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3542               END IF;
3543               RAISE NO_DATA_FOUND;
3544      END;
3545 
3546      --dbms_output.put_line('I4');
3547      --Derive cost/rev impl flags if they are passed as null
3548      IF p_cost_impl_flag IS NULL OR
3549         p_rev_impl_flag IS NULL THEN
3550 
3551          IF P_PA_debug_mode = 'Y' THEN
3552                pa_debug.g_err_stage:= 'Calling get_impl_details';
3553                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3554          END IF;
3555 
3556          pa_fp_control_items_utils.get_impl_details
3557          ( P_fin_plan_type_id       => p_fin_plan_type_id
3558           ,P_project_id             => l_project_id
3559           ,P_ci_id                  => p_ci_id
3560           ,P_ci_cost_version_id     => l_ci_cost_version_id
3561           ,P_ci_rev_version_id      => l_ci_rev_version_id
3562           ,P_ci_all_version_id      => l_ci_all_version_id
3563           ,p_targ_bv_id             => p_budget_version_id
3564           ,x_cost_impl_flag         => l_cost_impl_flag
3565           ,x_rev_impl_flag          => l_rev_impl_flag
3566           ,X_cost_impact_impl_flag  => l_cost_impact_impl_flag
3567           ,x_rev_impact_impl_flag   => l_rev_impact_impl_flag
3568           ,x_partially_impl_flag    => l_partially_impl_flag
3569           ,x_agreement_num          => l_agreement_num
3570           ,x_approved_fin_pt_id     => l_approved_fin_pt_id
3571           ,x_return_status          => l_return_status
3572           ,x_msg_data               => l_msg_data
3573           ,x_msg_count              => l_msg_count);
3574 
3575           IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
3576                IF P_PA_debug_mode = 'Y' THEN
3577                     pa_debug.g_err_stage:= 'pa_fp_control_items_utils.get_impl_details returned error';
3578                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3579                END IF;
3580                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3581           END IF;
3582 
3583      END IF;
3584 
3585     IF P_PA_debug_mode = 'Y' THEN
3586        pa_debug.g_err_stage:= '--BEFORE IMPL DETAILS-- p_ci_id --'||p_ci_id;
3587        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3588 
3589        pa_debug.g_err_stage:= '--BEFORE IMPL DETAILS-- l_cost_impl_flag --'||l_cost_impl_flag;
3590        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3591 
3592        pa_debug.g_err_stage:= '--BEFORE IMPL DETAILS-- l_rev_impl_flag --'||l_rev_impl_flag;
3593        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3594     END IF;
3595 
3596      IF p_cost_impl_flag IS NULL THEN
3597 
3598         --This check is required since get_impl_details can assign values other than Y / N to l_cost_impl_flag
3599         --Refer to the API for the possible values of l_cost_impl_flag
3600         IF l_cost_impl_flag <>'Y' THEN
3601 
3602             l_cost_impl_flag :='N';
3603 
3604         END IF;
3605 
3606     ELSE
3607 
3608         l_cost_impl_flag:=p_cost_impl_flag;
3609 
3610     END IF;
3611 
3612 
3613     -- We will consider the value 'R' as 'Y' for l_rev_impl_flag
3614     IF p_rev_impl_flag IS NULL THEN
3615 
3616         --This check is required since get_impl_details can assign values other than Y / N to l_rev_impl_flag
3617         --Refer to the API for the possible values of l_rev_impl_flag
3618         IF l_rev_impl_flag NOT IN ('Y','R') THEN
3619 
3620             l_rev_impl_flag :='N';
3621 
3622         ELSIF l_rev_impl_flag = 'R' THEN
3623 
3624             l_rev_impl_flag :='Y';
3625             l_partial_rev_impl_flag := 'R'; --caching this value so that we may use this to skip/execute chunks of code based on its value
3626 
3627         END IF;
3628 
3629     ELSE
3630 
3631         IF p_rev_impl_flag = 'R' THEN
3632             l_rev_impl_flag:='Y';
3633             l_partial_rev_impl_flag := 'R'; --caching this value so that we may use this to skip/execute chunks of code based on its value
3634         ELSE
3635             l_rev_impl_flag:=p_rev_impl_flag;
3636         END IF;
3637 
3638     END IF;
3639 
3640     --dbms_output.put_line('I5');
3641 
3642     IF P_PA_debug_mode = 'Y' THEN
3643        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- p_fin_plan_type_id --'||p_fin_plan_type_id;
3644        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3645 
3646        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_project_id --'||l_project_id;
3647        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3648 
3649        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- p_ci_id --'||p_ci_id;
3650        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3651 
3652        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_ci_cost_version_id --'||l_ci_cost_version_id;
3653        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3654 
3655        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_ci_rev_version_id --'||l_ci_rev_version_id;
3656        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3657 
3658        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_ci_all_version_id --'||l_ci_all_version_id;
3659        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3660 
3661        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_cost_impl_flag --'||l_cost_impl_flag;
3662        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3663 
3664        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_rev_impl_flag --'||l_rev_impl_flag;
3665        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3666 
3667        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_cost_impact_impl_flag --'||l_cost_impact_impl_flag;
3668        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3669 
3670        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_rev_impact_impl_flag --'||l_rev_impact_impl_flag;
3671        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3672 
3673        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_partially_impl_flag --'||l_partially_impl_flag;
3674        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3675 
3676        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_agreement_num --'||l_agreement_num;
3677        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3678 
3679        pa_debug.g_err_stage:= '--AFTER IMPL DETAILS-- l_approved_fin_pt_id --'||l_approved_fin_pt_id;
3680        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3681     END IF;
3682 
3683     --Bug 4136238.Moved it to later part of the code
3684     --l_partial_impl_rev_amt := p_partial_impl_rev_amt; -- Bug 3732446
3685 
3686     I:=1;
3687     IF l_cost_impl_flag = 'Y' THEN
3688          l_src_ver_id_tbl.extend();
3689          l_impl_type_tbl.extend();
3690          l_impl_qty_tbl.extend();
3691          l_src_ver_id_tbl(i) := nvl(l_ci_cost_version_id, l_ci_all_version_id);
3692          l_impl_type_tbl(i) := 'COST';
3693          l_impl_qty_tbl(i) := 'Y'; -- l_impl_qty_tbl(i) contains whether quantity needs to be implemented or not
3694          I:= I+1;
3695     END IF;
3696 
3697     --Bug 5845142. It could be that the COST ci impact is of type ALL. In this case even revenue impact is
3698     --not considered to be implemented.
3699     IF l_rev_impl_flag = 'Y' AND
3700      (NVL(l_ci_all_version_id,-1) <> -1 OR
3701       NVL(l_ci_rev_version_id,-1) <> -1) THEN
3702          IF I = 2 THEN
3703               IF l_src_ver_id_tbl(1) <> nvl(l_ci_all_version_id,-99) THEN -- Bug 3662136
3704                    l_src_ver_id_tbl.extend();
3705                    l_impl_type_tbl.extend();
3706                    l_impl_qty_tbl.extend();
3707                    l_src_ver_id_tbl(i) :=  l_ci_rev_version_id;
3708                    l_impl_type_tbl(i) := 'REVENUE';
3709                    --If the target verison type is ALL then quantity should be merged only with Cost and not with
3710                    --Revenue.Hence the impl qty should be N.
3711                    IF l_target_version_type ='ALL' THEN
3712                        l_impl_qty_tbl(i) := 'N';
3713                    ELSE
3714                        l_impl_qty_tbl(i) := 'Y';
3715                    END IF;
3716               ELSE
3717                    l_impl_type_tbl(1) := 'ALL';
3718               END IF;
3719          --Only Revenue impact is being implemented. The target should always be of type REVENUE in this case since
3720          --revenue impact can be implemented either into a ALL or REVENUE version and revenue impact alone can not
3721          --be implemented into an ALL version
3722          ELSE
3723               l_src_ver_id_tbl.extend();
3724               l_impl_type_tbl.extend();
3725               l_impl_qty_tbl.extend();
3726               l_src_ver_id_tbl(i) := nvl(l_ci_rev_version_id, l_ci_all_version_id);
3727               l_impl_type_tbl(i) := 'REVENUE';
3728               --Merge quantity while merging either an ALL CI or a REVENUE CI verison into a revenue target version
3729               l_impl_qty_tbl(i) := 'Y';
3730          END IF;
3731     END IF;
3732 
3733     --Bug 3980129. Store the derived values for cost/rev impl flags
3734     l_derv_cost_impl_flag := l_cost_impl_flag;
3735     l_derv_rev_impl_flag  := l_rev_impl_flag;
3736     --dbms_output.put_line('I6');
3737     -- For each source version id of the change order for which there is an impact
3738     FOR J IN l_src_ver_id_tbl.FIRST..l_src_ver_id_tbl.LAST LOOP
3739 
3740          --Initialize parital factor to 1. It will be derived later
3741          l_partial_factor :=1;
3742 
3743          --Bug 3980129.Derive the values for l_cost/rev_impl_flags
3744          --Bug 4136238. Initialized l_partial_impl_rev_amt only in revenue implementation.
3745          IF l_impl_type_tbl(J)= 'COST' THEN
3746 
3747               l_cost_impl_flag := l_derv_cost_impl_flag;
3748               --Bug 5845142. Add revenue amounts to the target version if the source version is of type ALL but
3749               --is approved only for REVENUE. Note that this does not mean that revenue impact will be
3750               --implemented into the target version.
3751               IF l_target_version_type='ALL' AND
3752                  l_targ_app_cost_flag ='Y' AND
3753                  l_targ_app_rev_flag='N' THEN
3754 
3755                    l_rev_impl_flag  := 'Y';
3756               ELSE
3757                    l_rev_impl_flag  := 'N';
3758               END IF;
3759               l_partial_impl_rev_amt := NULL;
3760 
3761          ELSIF l_impl_type_tbl(J)= 'REVENUE' THEN
3762 
3763               l_cost_impl_flag := 'N';
3764               l_rev_impl_flag  := l_derv_rev_impl_flag;
3765               l_partial_impl_rev_amt := p_partial_impl_rev_amt;
3766 
3767          ELSIF l_impl_type_tbl(J)= 'ALL' THEN
3768 
3769               l_cost_impl_flag := l_derv_cost_impl_flag;
3770               l_rev_impl_flag  := l_derv_rev_impl_flag;
3771               l_partial_impl_rev_amt := p_partial_impl_rev_amt;
3772 
3773          END IF;
3774          --Delete the lables which will be used for caching
3775          l_src_targ_task_tbl.delete;
3776          l_res_assmt_map_rec_tbl.delete;
3777 
3778          l_impl_earlier:='N';
3779 
3780          --One CO  can be implemented more than once into the target version only when it gets
3781          --implemented partially . IN other cases a case will never arise where this API is called for the
3782          --same CO and target version combination for more than once for implementing the same impact.
3783          --In case a CO is getting implemented partially and if a record already exists in pa_fp_mergedc_ctrl_items
3784          --then that existing record should be updated.
3785          --The difference between the amounts existing before merge and the resulting amounts after merge
3786          --will be updated in pa_fp_merged_ctrl_items
3787          IF  p_context='PARTIAL_REV' OR
3788          (p_context = 'INCLUDE' AND l_partial_rev_impl_flag = 'R' AND l_impl_type_tbl(j) IN ( 'REVENUE', 'ALL')) THEN
3789 
3790               BEGIN
3791 
3792                   SELECT impl_agr_revenue,
3793                          nvl(impl_proj_func_revenue,0), -- bug 4035856
3794                          nvl(impl_proj_revenue,0),      -- bug 4035856
3795                          nvl(impl_quantity,0)           -- bug 4035856
3796                   INTO   l_impl_amt,
3797                          l_impl_proj_func_revenue, -- bug 4035856
3798                          l_impl_proj_revenue,      -- bug 4035856
3799                          l_impl_quantity           -- bug 4035856
3800                   FROM   pa_fp_merged_ctrl_items
3801                   WHERE  project_id=l_project_id
3802                   AND    plan_version_id=p_budget_version_id
3803                   AND    ci_id=p_ci_id
3804                   AND    ci_plan_version_id=l_src_ver_id_tbl(j)
3805                   AND    version_type='REVENUE';
3806 
3807                   l_impl_earlier:='Y';
3808               EXCEPTION
3809               WHEN NO_DATA_FOUND THEN
3810                   l_impl_amt:=0;
3811                   l_impl_proj_func_revenue:=0;
3812                   l_impl_proj_revenue :=0;
3813                   l_impl_quantity:=0;
3814                   l_impl_earlier:='N';
3815               END;
3816 
3817 
3818               IF p_context = 'PARTIAL_REV' OR l_impl_earlier = 'Y' THEN
3819 
3820                   BEGIN
3821                        SELECT nvl(sum(pbl.txn_revenue),0) total_amt,
3822                               nvl(sum(pbl.revenue),0) total_amt_in_pfc,
3823                               nvl(sum(pbl.project_revenue),0) total_amt_in_pc
3824                        INTO   L_total_amt,
3825                               l_total_amt_in_pfc,
3826                               l_total_amt_in_pc
3827                        FROM   Pa_budget_lines pbl
3828                        WHERE  pbl.budget_Version_id= l_src_ver_id_tbl(j);
3829                      --IPM Arch Enhancement Bug 4865563
3830                         /*
3831                        and    pbl.cost_rejection_code IS NULL
3832                        and    pbl.revenue_rejection_code IS NULL
3833                        and    pbl.burden_rejection_code IS NULL
3834                        and    pbl.other_rejection_code IS NULL
3835                        and    pbl.pc_cur_conv_rejection_code IS NULL
3836                        and    pbl.pfc_cur_conv_rejection_code IS NULL; */
3837 
3838 
3839                   IF P_PA_debug_mode = 'Y' THEN
3840                     pa_debug.g_err_stage:='Fetching l_impl_amt'||l_impl_amt;
3841                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3842 
3843                     pa_debug.g_err_stage:='Fetching L_total_amt'||L_total_amt;
3844                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3845                   END IF;
3846 
3847                   EXCEPTION
3848                        WHEN OTHERS THEN
3849                             IF P_PA_debug_mode = 'Y' THEN
3850                                   pa_debug.g_err_stage:='Error while getting total and implemented rev ';
3851                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3852                             END IF;
3853                        RAISE;
3854                   END;
3855 
3856                   --For bug 3814932
3857                   IF l_total_amt = 0 THEN
3858 
3859                       l_partial_factor:=1;
3860 
3861                   ELSE
3862 
3863                       --If l_partial_factor is 0 only Resource assignments will be copied. Control will return
3864                       --from API without copying the budget lines.
3865 
3866                       IF p_context = 'INCLUDE' THEN
3867                            l_partial_impl_rev_amt := l_total_amt - l_impl_amt;
3868                       END IF;
3869                       l_partial_factor:=l_partial_impl_rev_amt/(l_total_amt);
3870 
3871                   END IF;
3872               END IF;
3873          END IF;
3874 
3875          --Bug 4136238. The variable l_partial_impl_rev_amt will always(not only in partial impl case) contain the
3876          --revenue amount in txn currency that will get implemented. The below block of code will take care of
3877          --populating that variable when the revenue gets implemented fully
3878          IF  l_impl_type_tbl(J) <>'COST' AND
3879              (p_context = 'IMPL_FIN_IMPACT' OR
3880               (p_context = 'INCLUDE' AND l_impl_earlier ='N')) THEN
3881 
3882              IF P_PA_debug_mode = 'Y' THEN
3883                 pa_debug.g_err_stage:='Deriving l_partial_impl_rev_amt in FULL Rev impl case ';
3884                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3885              END IF;
3886 
3887              SELECT NVL(sum(txn_revenue),0)
3888              INTO   l_partial_impl_rev_amt
3889              FROM   pa_budget_lines
3890              WHERE  budget_version_id=l_src_ver_id_tbl(j);
3891 
3892              IF P_PA_debug_mode = 'Y' THEN
3893                 pa_debug.g_err_stage:='l_partial_impl_rev_amt derived is '||l_partial_impl_rev_amt;
3894                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3895              END IF;
3896 
3897         END IF;
3898 
3899 
3900         IF P_PA_debug_mode = 'Y' THEN
3901            pa_debug.g_err_stage:='l_partial_factor IS '||l_partial_factor;
3902            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
3903         END IF;
3904 
3905 
3906          BEGIN
3907               SELECT proj_fp_options_id,
3908                      plan_in_multi_curr_flag,
3909                      fin_plan_type_id,
3910                      nvl(cost_time_phased_code,nvl(revenue_time_phased_code,all_time_phased_code)),
3911                      report_labor_hrs_from_code,
3912                      nvl(cost_resource_list_id,nvl(revenue_resource_list_id,all_resource_list_id)),
3913                      nvl(cost_fin_plan_level_code,nvl(revenue_fin_plan_level_code,all_fin_plan_level_code))
3914               INTO   l_src_proj_fp_options_id,
3915                      l_src_multi_curr_flag,
3916                      l_src_fin_plan_type_id,
3917                      l_src_time_phased_code,
3918                      l_src_report_lbr_hrs_frm_code,
3919                      l_src_resource_list_id,
3920                      l_src_plan_level_code
3921               FROM   pa_proj_fp_options
3922               WHERE  fin_plan_version_id = l_src_ver_id_tbl(j);
3923 
3924         IF P_PA_debug_mode = 'Y' THEN
3925            pa_debug.g_err_stage:='Fetching l_src_proj_fp_options_id'||l_src_proj_fp_options_id;
3926            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3927 
3928            pa_debug.g_err_stage:='Fetching l_src_multi_curr_flag'||l_src_multi_curr_flag;
3929            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3930 
3931            pa_debug.g_err_stage:='Fetching l_src_fin_plan_type_id'||l_src_fin_plan_type_id;
3932            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3933 
3934            pa_debug.g_err_stage:='Fetching l_src_time_phased_code'||l_src_time_phased_code;
3935            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3936 
3937            pa_debug.g_err_stage:='Fetching l_src_report_lbr_hrs_frm_code'||l_src_report_lbr_hrs_frm_code;
3938            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3939 
3940            pa_debug.g_err_stage:='Fetching l_src_resource_list_id'||l_src_resource_list_id;
3941            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3942 
3943            pa_debug.g_err_stage:='Fetching l_src_plan_level_code'||l_src_plan_level_code;
3944            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3945         END IF;
3946 
3947 
3948          EXCEPTION
3949               WHEN NO_DATA_FOUND THEN
3950                    IF P_PA_debug_mode = 'Y' THEN
3951                          pa_debug.g_err_stage:='NO_DATA_FOUND while getting src attributes ';
3952                          pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3953                    END IF;
3954               RAISE NO_DATA_FOUND;
3955          END;
3956 
3957          --dbms_output.put_line('I8');
3958          -- Add the currencies in the source which are not there already to PA_FP_TXN_CURRENCIES
3959          IF l_src_multi_curr_flag = 'Y' AND l_targ_multi_curr_flag = 'Y' AND nvl(l_targ_app_rev_flag, 'N') = 'N' THEN
3960 
3961                SELECT ptxn_s.txn_currency_code
3962                BULK COLLECT INTO l_txn_curr_code_tbl
3963                FROM    pa_fp_txn_currencies ptxn_s
3964                WHERE   ptxn_s.proj_fp_options_id=l_src_proj_fp_options_id
3965                AND     NOT EXISTS (SELECT 'X'
3966                                    FROM   pa_fp_txn_currencies ptxn_t
3967                                    WHERE  ptxn_t.proj_fp_options_id=l_targ_proj_fp_options_id
3968                                    AND    ptxn_t.txn_currency_code=ptxn_s.txn_currency_code);
3969 
3970 
3971                IF l_txn_curr_code_tbl.COUNT > 0 THEN
3972                    For i in l_txn_curr_code_tbl.FIRST..l_txn_curr_code_tbl.LAST LOOP
3973                         IF P_PA_debug_mode = 'Y' THEN
3974                            pa_debug.g_err_stage:='INSERTING l_targ_proj_fp_options_id'||l_targ_proj_fp_options_id;
3975                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3976 
3977                            pa_debug.g_err_stage:='INSERTING l_PROJECT_ID'||l_PROJECT_ID;
3978                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3979 
3980                            pa_debug.g_err_stage:='INSERTING p_budget_version_id'||p_budget_version_id;
3981                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3982 
3983                            pa_debug.g_err_stage:='INSERTING l_txn_curr_code_tbl'||l_txn_curr_code_tbl(i);
3984                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
3985                         END IF;
3986                    END LOOP;
3987                END IF;
3988 
3989                IF l_txn_curr_code_tbl.COUNT > 0 THEN
3990                     Forall jj in l_txn_curr_code_tbl.FIRST..l_txn_curr_code_tbl.LAST
3991                     INSERT INTO PA_FP_TXN_CURRENCIES (
3992                        fp_txn_currency_id
3993                        ,proj_fp_options_id
3994                        ,project_id
3995                        ,fin_plan_type_id
3996                        ,fin_plan_version_id
3997                        ,txn_currency_code
3998                        ,default_rev_curr_flag
3999                        ,default_cost_curr_flag
4000                        ,default_all_curr_flag
4001                        ,project_currency_flag
4002                        ,projfunc_currency_flag
4003                        ,last_update_date
4004                        ,last_updated_by
4005                        ,creation_date
4006                        ,created_by
4007                        ,last_update_login
4008                        ,project_cost_exchange_rate
4009                        ,project_rev_exchange_rate
4010                        ,projfunc_cost_exchange_Rate
4011                        ,projfunc_rev_exchange_Rate
4012                        )
4013                        VALUES
4014                        ( pa_fp_txn_currencies_s.NEXTVAL
4015                        , l_targ_proj_fp_options_id
4016                        , l_PROJECT_ID
4017                        , p_fin_plan_type_id
4018                        , p_budget_version_id
4019                        , l_txn_curr_code_tbl(jj)
4020                        , 'N'
4021                        , 'N'
4022                        , 'N'
4023                        , 'N'
4024                        , 'N'
4025                        , sysdate
4026                        , fnd_global.user_id
4027                        , sysdate
4028                        , fnd_global.user_id
4029                        , fnd_global.login_id
4030                        , NULL
4031                        , NULL
4032                        , NULL
4033                        , NULL);
4034 
4035                END IF; -- l_txn_curr_code_tbl.COUNT > 0
4036          END IF; -- l_src_multi_curr_flag = 'Y' AND l_targ_multi_curr_flag = 'Y' AND nvl(l_targ_app_rev_flag, 'N') = 'N'
4037 
4038          --dbms_output.put_line('I9');
4039          IF l_src_resource_list_id <> l_targ_resource_list_id THEN
4040 
4041               -- When the resource lists are different we need to call the mapping API.
4042               PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs(
4043               p_budget_version_id           => l_src_ver_id_tbl(j)
4044              ,p_resource_list_id            => l_targ_resource_list_id
4045              ,p_calling_process             => 'BUDGET_GENERATION'
4046              ,p_calling_context             => 'PLSQL'
4047              ,p_process_code                => 'RES_MAP'
4048              ,p_calling_mode                => 'BUDGET_VERSION'
4049              ,p_init_msg_list_flag          => 'N'
4050              ,p_commit_flag                 => 'N'
4051              ,x_txn_source_id_tab           => l_txn_source_id_tbl
4052              ,x_res_list_member_id_tab      => l_res_list_member_id_tbl
4053              ,x_rbs_element_id_tab          => l_rbs_element_id_prm_tbl
4054              ,x_txn_accum_header_id_tab     => l_txn_accum_header_id_prm_tbl
4055              ,x_return_status               => l_return_status
4056              ,x_msg_count                   => l_msg_count
4057              ,x_msg_data                    => l_msg_data);
4058 
4059              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
4060                   IF P_PA_debug_mode = 'Y' THEN
4061                        pa_debug.g_err_stage:= 'PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs returned error';
4062                        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
4063                   END IF;
4064                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4065              END IF;
4066 
4067              -- bug 3934574 In generation context, if retain manual edited lines is 'Y' filter
4068              -- all RAs with null transaction souce code
4069              SELECT rlmap.resource_list_member_id -- rlm id for the target
4070              ,DECODE(prat.resource_assignment_id,null, 'INSERT','UPDATE') --Indicates whether the records needs to be updated/inserted in the target
4071              ,get_task_id(l_targ_plan_level_code,rlmap.task_id),
4072               prat.resource_assignment_id,
4073 			  rlmap.cbs_element_id,  --bug#16200605
4074              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.txn_source_id),null),
4075              min(LEAST(nvl(prat.planning_start_date, rlmap.planning_start_date),rlmap.planning_start_date)),
4076              max(GREATEST(nvl(prat.planning_end_date, rlmap.planning_end_date),rlmap.planning_end_date)),
4077              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.txn_spread_curve_id),null),
4078              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.sp_fixed_date),null), -- Bug 8350296
4079              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.txn_etc_method_code),null),
4080              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.resource_type_code),null),
4081              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.fc_res_type_code),null),
4082              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.RESOURCE_CLASS_CODE),null),
4083              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.organization_id),null),
4084              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.job_id),null),
4085              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.person_id),null),
4086              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.expenditure_type),null),
4087              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.expenditure_category),null),
4088              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.revenue_category),null),
4089              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.event_type),null),
4090              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.vendor_id),null),
4091              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.project_role_id),null),
4092              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.person_type_code),null),
4093              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.non_labor_resource),null),
4094              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.bom_resource_id),null),
4095              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.inventory_item_id),null),
4096              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.item_category_id),null),
4097              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.mfc_cost_type_id),null),
4098              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.INCURRED_BY_RES_FLAG),null),
4099              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.RESOURCE_CLASS_FLAG),null),
4100              decode(COUNT(rlmap.txn_source_id),1,max(rlmap.NAMED_ROLE),null),
4101              NULL,
4102              NULL,
4103              NULL,
4104              NULL,
4105              NULL,
4106              --The below decodes will return the Rate Based Flag(rbf) and unit of measure(uom) for the target
4107              --resource assignment. These decodes will derive the rbf and uom based on following logic
4108              ------If multiple source planning transactions are being merged into a single target transaction
4109              ----------If the all the source planning transactions have same UOM AND RBF then
4110              --------------if the UOM and RBF are equal to those of target planning transaction then
4111              ------------------take UOM and RBF from the source
4112              --------------else
4113              ------------------take DOLLARS and N for UOM and RBF
4114              ----------else
4115              --------------take DOLLARS and N for UOM and RBF
4116              ------else if there is one to one mapping
4117              ----------If the UOM and RBF of the source and target plannig transactions are same then
4118              --------------take UOM and RBF from source
4119              ----------else
4120              --------------take DOLLARS and N for UOM and RBF
4121              ------else if there is no matching target transaction(In this case one should be inserted into target)
4122              ----------source's RBF and UOM will be compared with the RBF and UOM returned be get_resource_defaults.
4123              ----------This is done below
4124              decode(max(rlmap.rbf),
4125                     min(rlmap.rbf),decode(max(rlmap.uom),
4126                                           min(rlmap.uom),decode(prat.resource_assignment_id,
4127                                                                 null, max(rlmap.rbf),
4128                                                                 decode(max(rlmap.rbf),
4129                                                                        max(prat.rate_based_flag),decode(max(rlmap.uom),
4130                                                                                                         max(prat.unit_of_measure),max(rlmap.rbf),
4131                                                                                                         'N'),
4132                                                                       'N')),
4133                                          'N'),
4134                     'N'),
4135                /* bug 5073816: Changed the following */
4136                 max(rlmap.rrbf),  --IPM Arch Enhancement
4137              decode(max(rlmap.rbf),
4138                     min(rlmap.rbf),decode(max(rlmap.uom),
4139                                           min(rlmap.uom),decode(prat.resource_assignment_id,
4140                                                                 null, max(rlmap.uom),
4141                                                                 decode(max(rlmap.rbf),
4142                                                                        max(prat.rate_based_flag),decode(max(rlmap.uom),
4143                                                                                                         max(prat.unit_of_measure),max(rlmap.uom),
4144                                                                                                         'DOLLARS'),
4145                                                                       'DOLLARS')),
4146                                          'DOLLARS'),
4147                     'DOLLARS'),
4148              --Bug 3752352. If the resource lists are differnt then initialise the rbs element id and
4149              --txn accum header tbls .
4150              NULL,
4151              NULL
4152              BULK COLLECT INTO
4153                L_targ_rlm_id_tbl,
4154                L_ra_dml_code_tbl,
4155                L_targ_task_id_tbl,
4156                L_targ_ra_id_tbl,
4157 			   L_TARG_CBS_ELEM_ID_TBL,  --bug#16200605
4158                l_src_ra_id_cnt_tbl,
4159                l_planning_start_date_tbl,
4160                l_planning_end_date_tbl,
4161                l_targ_spread_curve_id_tbl,
4162                l_targ_sp_fixed_date_tbl, -- Bug 8350296
4163                l_targ_etc_method_code_tbl,
4164                l_targ_resource_type_code_tbl,
4165                l_targ_fc_res_type_code_tbl,
4166                l_targ_RESOURCE_CLASS_CODE_tbl,
4167                l_targ_organization_id_tbl,
4168                l_targ_job_id_tbl,
4169                l_targ_person_id_tbl,
4170                l_targ_expenditure_type_tbl,
4171                l_targ_expend_category_tbl,
4172                l_targ_rev_category_code_tbl,
4173                l_targ_event_type_tbl,
4174                l_targ_supplier_id_tbl,
4175                l_targ_project_role_id_tbl,
4176                l_targ_person_type_code_tbl,
4177                l_targ_non_labor_resource_tbl,
4178                l_targ_bom_resource_id_tbl,
4179                l_targ_inventory_item_id_tbl,
4180                l_targ_item_category_id_tbl,
4181                l_targ_mfc_cost_type_id_tbl,
4182                l_targ_INCURED_BY_RES_FLAG_tbl,
4183                l_targ_RESOURCE_CLASS_FLAG_tbl,
4184                l_targ_NAMED_ROLE_tbl ,
4185                l_targ_RATE_EXPEND_TYPE_tbl,
4186                l_targ_RATE_EXP_FC_CUR_COD_tbl,
4187                l_targ_RATE_EXPEND_ORG_ID_tbl,
4188                l_targ_INCR_BY_RES_CLS_COD_tbl,
4189                l_targ_INCUR_BY_ROLE_ID_tbl,
4190                l_targ_RATE_BASED_FLAG_tbl,
4191                l_targ_RES_RATE_BASED_FLAG_tbl, --IPM Arch Enhancement
4192                l_targ_unit_of_measure_tbl,
4193                l_targ_rbs_element_id_tbl,
4194                l_targ_txn_accum_header_id_tbl
4195 
4196              FROM    Pa_resource_assignments prat,
4197                      (SELECT
4198                        rlmap.txn_spread_curve_id,
4199                        rlmap.txn_etc_method_code,
4200                        rlmap.resource_type_code,
4201                        rlmap.fc_res_type_code,
4202                        rlmap.RESOURCE_CLASS_CODE,
4203                        rlmap.organization_id,
4204                        rlmap.job_id,
4205                        rlmap.person_id,
4206                        rlmap.expenditure_type,
4207                        rlmap.expenditure_category,
4208                        rlmap.revenue_category,
4209                        rlmap.event_type,
4210                        rlmap.vendor_id,
4211                        rlmap.project_role_id,
4212                        rlmap.person_type_code,
4213                        rlmap.non_labor_resource,
4214                        rlmap.bom_resource_id,
4215                        rlmap.inventory_item_id,
4216                        rlmap.item_category_id,
4217                        rlmap.mfc_cost_type_id,
4218                        rlmap.INCURRED_BY_RES_FLAG,
4219                        rlmap.TXN_RATE_BASED_FLAG,
4220                        rlmap.RESOURCE_CLASS_FLAG,
4221                        rlmap.NAMED_ROLE,
4222                        rlmap.txn_source_id,
4223                        rlmap.resource_list_member_id,
4224                        pra.planning_start_date,
4225                        pra.planning_end_date,
4226                        pra.task_id,
4227                        pra.rate_based_flag as rbf,
4228                        pra.resource_rate_based_flag as rrbf,    --IPM Arch Enhancement
4229                        pra.unit_of_measure as uom,
4230                        pra.sp_fixed_date AS sp_fixed_date, -- Bug 8350296
4231 					   pra.cbs_element_id AS cbs_element_id --bug#16200605
4232                       FROM
4233                        pa_resource_assignments pra,
4234                        pa_res_list_map_tmp4 rlmap
4235                       WHERE
4236                        pra.resource_assignment_id=rlmap.txn_source_id)rlmap
4237              WHERE   prat.budget_version_id(+)=p_budget_version_id
4238              AND     prat.resource_list_member_id(+)=rlmap.resource_list_member_id
4239              AND     prat.task_id(+)=get_task_id(l_targ_plan_level_code,rlmap.task_id)
4240              AND     prat.project_assignment_id(+)=-1
4241 /* Bug 7287101 - skkoppul - commented
4242              AND     (prat.resource_assignment_id is null  --> target ra doesnot exist
4243                       OR decode(p_calling_context,
4244                                  'BUDGET_GENERATION', decode(l_retain_manual_lines_flag, 'Y', prat.transaction_source_code, 'x'),
4245                                  'FORECAST_GENERATION', decode(l_retain_manual_lines_flag, 'Y', prat.transaction_source_code, 'x'),
4246                                    -99) is not null) -- bug 3934574 */
4247              GROUP BY get_task_id(l_targ_plan_level_code,rlmap.task_id) ,
4248                       rlmap.resource_list_member_id, prat.resource_assignment_id,
4249 					  rlmap.cbs_element_id;  ----bug#16200605
4250         ELSE
4251 
4252             IF P_PA_debug_mode = 'Y' THEN
4253                   pa_debug.g_err_stage:= 'About to select the RAs with same res list for INS/UPD';
4254                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4255             END IF;
4256 
4257              -- bug 3934574 In generation context, if retain manual edited lines is 'Y' filter
4258              -- all RAs with null transaction souce code
4259              SELECT pras.resource_list_member_id -- rlm id for the target
4260              ,DECODE(prat.resource_assignment_id,null, 'INSERT','UPDATE') --Indicates whether the records needs to be updated/inserted in the target
4261              , get_task_id(l_targ_plan_level_code,pras.task_id),
4262                 prat.resource_assignment_id,
4263 				pras.cbs_element_id,  --bug#16200605
4264              decode(COUNT(pras.resource_assignment_id),1,max(pras.resource_assignment_id),null),
4265              min(LEAST(nvl(prat.planning_start_date, pras.planning_start_date),pras.planning_start_date)),
4266              max(GREATEST(nvl(prat.planning_end_date, pras.planning_end_date),pras.planning_end_date)),
4267              decode(COUNT(pras.resource_assignment_id),1,max(pras.rbs_element_id),null),
4268              decode(COUNT(pras.resource_assignment_id),1,max(pras.spread_curve_id),null),
4269              decode(COUNT(pras.resource_assignment_id),1,max(pras.sp_fixed_date),null), -- Bug 8350296
4270              decode(COUNT(pras.resource_assignment_id),1,max(pras.etc_method_code),null),
4271              decode(COUNT(pras.resource_assignment_id),1,max(pras.fc_res_type_code),null),
4272              decode(COUNT(pras.resource_assignment_id),1,max(pras.organization_id),null),
4273              decode(COUNT(pras.resource_assignment_id),1,max(pras.job_id),null),
4274              decode(COUNT(pras.resource_assignment_id),1,max(pras.person_id),null),
4275              decode(COUNT(pras.resource_assignment_id),1,max(pras.expenditure_type),null),
4276              decode(COUNT(pras.resource_assignment_id),1,max(pras.expenditure_category),null),
4277              decode(COUNT(pras.resource_assignment_id),1,max(pras.revenue_category_code),null),
4278              decode(COUNT(pras.resource_assignment_id),1,max(pras.event_type),null),
4279              decode(COUNT(pras.resource_assignment_id),1,max(pras.supplier_id),null),
4280              decode(COUNT(pras.resource_assignment_id),1,max(pras.project_role_id),null),
4281              decode(COUNT(pras.resource_assignment_id),1,max(pras.res_type_code),null),
4282              decode(COUNT(pras.resource_assignment_id),1,max(pras.person_type_code),null),
4283              decode(COUNT(pras.resource_assignment_id),1,max(pras.non_labor_resource),null),
4284              decode(COUNT(pras.resource_assignment_id),1,max(pras.bom_resource_id),null),
4285              decode(COUNT(pras.resource_assignment_id),1,max(pras.inventory_item_id),null),
4286              decode(COUNT(pras.resource_assignment_id),1,max(pras.item_category_id),null),
4287              decode(COUNT(pras.resource_assignment_id),1,max(pras.INCURRED_BY_RES_FLAG),null),
4288              decode(COUNT(pras.resource_assignment_id),1,max(pras.RESOURCE_CLASS_FLAG),null),
4289              decode(COUNT(pras.resource_assignment_id),1,max(pras.NAMED_ROLE),null),
4290              decode(COUNT(pras.resource_assignment_id),1,max(pras.TXN_ACCUM_HEADER_ID),null),
4291              decode(COUNT(pras.resource_assignment_id),1,max(pras.RESOURCE_CLASS_CODE),null),
4292              decode(COUNT(pras.resource_assignment_id),1,max(pras.ASSIGNMENT_DESCRIPTION),null),
4293              decode(COUNT(pras.resource_assignment_id),1,max(pras.MFC_COST_TYPE_ID),null),
4294              decode(COUNT(pras.resource_assignment_id),1,max(pras.RATE_JOB_ID),null),
4295              decode(COUNT(pras.resource_assignment_id),1,max(pras.RATE_EXPENDITURE_TYPE),null),
4296              decode(COUNT(pras.resource_assignment_id),1,max(pras.RATE_EXP_FUNC_CURR_CODE),null),
4297              decode(COUNT(pras.resource_assignment_id),1,max(pras.RATE_EXPENDITURE_ORG_ID),null),
4298              decode(COUNT(pras.resource_assignment_id),1,max(pras.INCUR_BY_RES_CLASS_CODE),null),
4299              decode(COUNT(pras.resource_assignment_id),1,max(pras.INCUR_BY_ROLE_ID),null),
4300              --The below decodes will return the Rate Based Flag(rbf) and unit of measure(uom) for the target
4301              --resource assignment. These decodes will derive the rbf and uom based on following logic
4302              ------If multiple source planning transactions are being merged into a single target transaction
4303              ----------If the all the source planning transactions have same UOM AND RBF then
4304              --------------if the UOM and RBF are equal to those of target planning transaction then
4305              ------------------take UOM and RBF from the source
4306              --------------else
4307              ------------------take DOLLARS and N for UOM and RBF
4308              ----------else
4309              --------------take DOLLARS and N for UOM and RBF
4310              ------else if there is one to one mapping
4311              ----------If the UOM and RBF of the source and target plannig transactions are same then
4312              --------------take UOM and RBF from source
4313              ----------else
4314              --------------take DOLLARS and N for UOM and RBF
4315              ------else if there is no matching target transaction(In this case one should be inserted into target)
4316              ----------source's RBF and UOM will be compared with the RBF and UOM returned be get_resource_defaults.
4317              ----------This is done below
4318              decode(max(pras.rate_based_flag),
4319                     min(pras.rate_based_flag),decode(max(pras.unit_of_measure),
4320                                                      min(pras.unit_of_measure),decode(prat.resource_assignment_id,
4321                                                                                       null,  max(pras.rate_based_flag),
4322                                                                                       decode(max(pras.rate_based_flag),
4323                                                                                              max(prat.rate_based_flag),decode(max(pras.unit_of_measure),
4324                                                                                                                               max(prat.unit_of_measure),max(pras.rate_based_flag),
4325                                                                                                                               'N'),
4326                                                                                              'N')),
4327                                                     'N'),
4328                     'N'),
4329             /* bug 5073816: Changed the following */
4330                 max(pras.resource_rate_based_flag),    --IPM Arch Enhancement Bug 4865563
4331              decode(max(pras.rate_based_flag),
4332                     min(pras.rate_based_flag),decode(max(pras.unit_of_measure),
4333                                                      min(pras.unit_of_measure),decode(prat.resource_assignment_id,
4334                                                                                       null,  max(pras.unit_of_measure),
4335                                                                                       decode(max(pras.rate_based_flag),
4336                                                                                              max(prat.rate_based_flag),decode(max(pras.unit_of_measure),
4337                                                                                                                               max(prat.unit_of_measure),max(pras.unit_of_measure),
4338                                                                                                                               'DOLLARS'),
4339                                                                                              'DOLLARS')),
4340                                                      'DOLLARS'),
4341                    'DOLLARS')
4342              BULK COLLECT INTO
4343                L_targ_rlm_id_tbl,
4344                L_ra_dml_code_tbl,
4345                L_targ_task_id_tbl,
4346                L_targ_ra_id_tbl,
4347 			   L_TARG_CBS_ELEM_ID_TBL,  --bug#16200605
4348                l_src_ra_id_cnt_tbl,
4349                l_planning_start_date_tbl,
4350                l_planning_end_date_tbl,
4351                l_targ_rbs_element_id_tbl,
4352                l_targ_spread_curve_id_tbl,
4353                l_targ_sp_fixed_date_tbl, -- Bug 8350296
4354                l_targ_etc_method_code_tbl,
4355                l_targ_fc_res_type_code_tbl,
4356                l_targ_organization_id_tbl,
4357                l_targ_job_id_tbl,
4358                l_targ_person_id_tbl,
4359                l_targ_expenditure_type_tbl,
4360                l_targ_expend_category_tbl,
4361                l_targ_rev_category_code_tbl,
4362                l_targ_event_type_tbl,
4363                l_targ_supplier_id_tbl,
4364                l_targ_project_role_id_tbl,
4365                l_targ_resource_type_code_tbl,
4366                l_targ_person_type_code_tbl,
4367                l_targ_non_labor_resource_tbl,
4368                l_targ_bom_resource_id_tbl,
4369                l_targ_inventory_item_id_tbl,
4370                l_targ_item_category_id_tbl,
4371                l_targ_INCURED_BY_RES_FLAG_tbl,
4372                l_targ_RESOURCE_CLASS_FLAG_tbl,
4373                l_targ_NAMED_ROLE_tbl,
4374                l_targ_txn_accum_header_id_tbl,
4375                l_targ_RESOURCE_CLASS_CODE_tbl,
4376                l_targ_assignment_description,
4377                l_targ_mfc_cost_type_id_tbl,
4378                l_targ_RATE_JOB_ID_tbl,
4379                l_targ_RATE_EXPEND_TYPE_tbl,
4380                l_targ_RATE_EXP_FC_CUR_COD_tbl,
4381                l_targ_RATE_EXPEND_ORG_ID_tbl,
4382                l_targ_INCR_BY_RES_CLS_COD_tbl,
4383                l_targ_INCUR_BY_ROLE_ID_tbl,
4384                l_targ_RATE_BASED_FLAG_tbl,
4385                l_targ_RES_RATE_BASED_FLAG_tbl,    --IPM Arch Enhancement Bug 4865563
4386                l_targ_unit_of_measure_tbl
4387              FROM    pa_resource_assignments pras,
4388                      Pa_resource_assignments prat
4389              WHERE   pras.budget_version_id=l_src_ver_id_tbl(j)
4390              AND     prat.budget_version_id(+)=p_budget_version_id
4391              AND     prat.resource_list_member_id(+)=pras.resource_list_member_id
4392              AND     prat.task_id(+)=get_task_id(l_targ_plan_level_code,pras.task_id)
4393              AND     prat.project_assignment_id(+)=-1
4394 /* Bug 7287101 - skkoppul - commented
4395              AND     (prat.resource_assignment_id is null --> target ra doesnot exist
4396                       OR decode(p_calling_context,
4397                                  'BUDGET_GENERATION', decode(l_retain_manual_lines_flag, 'Y', prat.transaction_source_code, 'x'),
4398                                  'FORECAST_GENERATION', decode(l_retain_manual_lines_flag, 'Y', prat.transaction_source_code, 'x'),
4399                                    -99) is not null) -- bug 3934574 */
4400              GROUP BY get_task_id(l_targ_plan_level_code,pras.task_id) , pras.resource_list_member_id,
4401                        prat.resource_assignment_id,
4402 					   pras.cbs_element_id;    --bug#16200605
4403 
4404             IF P_PA_debug_mode = 'Y' THEN
4405                   pa_debug.g_err_stage:= ' selected the RAs with same res list for INS/UPD '||l_targ_ra_id_tbl.count;
4406                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4407             END IF;
4408 
4409         END IF;
4410 
4411         IF P_PA_debug_mode = 'Y' THEN
4412               pa_debug.g_err_stage:= ' Collected the Target Ras that should either be ins or upd. Count is '||l_targ_ra_id_tbl.count;
4413               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4414         END IF;
4415 
4416          --dbms_output.put_line('I10 '||l_targ_ra_id_tbl.COUNT);
4417 
4418         IF l_targ_ra_id_tbl.COUNT >0 THEN
4419 
4420             --The variable can be made Y now as there are some resource assignments that can be merged. Note that even if this variable is Y
4421             --the reporting lines API will not be called if calculate API is called in the flow
4422             l_call_rep_lines_api:='Y';
4423 
4424              --dbms_output.put_line('I11');
4425 
4426             /* Store the values for target task id, target rlm id and target resource assignment id in the form of a record.
4427                so that it will be easy later to identify the  resource assignment id corresponding to the given task id
4428                and resource list member id*/
4429             FOR ind IN L_targ_ra_id_tbl.FIRST..L_targ_ra_id_tbl.LAST LOOP
4430                   IF L_targ_ra_id_tbl(ind) IS NULL THEN
4431                        SELECT pa_resource_assignments_s.nextval
4432                        INTO L_targ_ra_id_tbl(ind)
4433                        FROM dual;
4434                   END IF;
4435                   l_res_assmt_map_rec_tbl(ind).task_id:=l_targ_task_id_tbl(ind);
4436                   l_res_assmt_map_rec_tbl(ind).resource_list_member_id:=l_targ_rlm_id_tbl(ind);
4437                   l_res_assmt_map_rec_tbl(ind).resource_assignment_id:=L_targ_ra_id_tbl(ind);
4438                   l_res_assmt_map_rec_tbl(ind).ra_dml_code:=L_ra_dml_code_tbl(ind);
4439             END LOOP;
4440 
4441             l_da_resource_list_members_tab:=SYSTEM.PA_NUM_TBL_TYPE();
4442             l_txn_src_typ_code_rbs_prm_tbl :=SYSTEM.pa_varchar2_30_tbl_type();
4443 
4444             --dbms_output.put_line('I12');
4445             IF l_src_resource_list_id = l_targ_resource_list_id THEN
4446                   --Calling resource default API
4447 
4448                    --dbms_output.put_line('I13');
4449                    FOR kk in 1 .. L_targ_rlm_id_tbl.count LOOP
4450 
4451                        --Find the distinct rlms among the resoruce assignments that have to be inserted .
4452                        --This is done to call get_resource_defaults only for distinct rlms. Bug 3859738
4453                        IF   L_ra_dml_code_tbl(kk) = 'INSERT'
4454                        AND l_src_ra_id_cnt_tbl(KK) IS NULL THEN
4455 
4456                             l_temp:= NULL;
4457                             FOR LL IN 1..l_da_resource_list_members_tab.COUNT LOOP
4458                                 IF l_da_resource_list_members_tab(LL)= L_targ_rlm_id_tbl(kk) THEN
4459                                     l_temp:=LL;
4460                                     EXIT;
4461                                 END IF;
4462                             END LOOP;
4463 
4464                             IF l_temp IS NULL THEN
4465                                 --Indicates that the resource list member is not already selected and hence it has to be
4466                                 --considered while calling get_resource_defaults.Bug 3859738
4467 
4468                                 l_da_resource_list_members_tab.extend;
4469                                 l_da_resource_list_members_tab(l_da_resource_list_members_tab.COUNT) := L_targ_rlm_id_tbl(kk);
4470                                 l_txn_src_typ_code_rbs_prm_tbl.extend;
4471                                 l_txn_src_typ_code_rbs_prm_tbl(l_txn_src_typ_code_rbs_prm_tbl.COUNT):='RES_ASSIGNMENT';
4472                             END IF;
4473 --for Start Bug 5291484
4474                           ELSIF L_ra_dml_code_tbl(kk) = 'INSERT'
4475                           AND l_src_ra_id_cnt_tbl(KK) IS NOT NULL THEN
4476 
4477                                l_temp := NULL; --Bug 5532905.
4478                                FOR LL IN 1..lresource_list_members_tab_1.COUNT LOOP
4479                                    IF lresource_list_members_tab_1(LL)= L_targ_rlm_id_tbl(kk) THEN
4480                                        l_temp:=LL;
4481                                        EXIT;
4482                                    END IF;
4483                                END LOOP;
4484 
4485                                   IF P_PA_debug_mode = 'Y' THEN
4486                                          pa_debug.g_err_stage:= 'before check l_temp ' || l_temp;
4487                                          pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4488                                   END IF;
4489 
4490                                IF l_temp IS NULL THEN
4491                                    lresource_list_members_tab_1.extend;
4492                                    lresource_list_members_tab_1(lresource_list_members_tab_1.COUNT) := L_targ_rlm_id_tbl(kk);
4493                                    ltxnsrctyp_code_rbs_prm_tbl_1.extend;
4494                                    ltxnsrctyp_code_rbs_prm_tbl_1(ltxnsrctyp_code_rbs_prm_tbl_1.COUNT) := 'RES_ASSIGNMENT';
4495                                    lperson_id_tab_1.extend;
4496                                    lperson_id_tab_1(lperson_id_tab_1.COUNT) := l_targ_person_id_tbl(kk);
4497                                    ljob_id_tab_1.extend;
4498                                    ljob_id_tab_1(ljob_id_tab_1.COUNT) := l_targ_job_id_tbl(kk);
4499                                    lorganization_id_tab_1.extend;
4500                                    lorganization_id_tab_1(lorganization_id_tab_1.COUNT) := l_targ_organization_id_tbl(kk);
4501                                    l_da_supplier_id_tab_1.extend;
4502                                    l_da_supplier_id_tab_1(l_da_supplier_id_tab_1.COUNT) := l_targ_supplier_id_tbl(kk);
4503                                    lexpenditure_type_tab_1.extend;
4504                                    lexpenditure_type_tab_1(lexpenditure_type_tab_1.COUNT) := l_targ_expenditure_type_tbl(kk);
4505                                    l_da_event_type_tab_1.extend;
4506                                    l_da_event_type_tab_1(l_da_event_type_tab_1.COUNT) := l_targ_event_type_tbl(kk);
4507                                    lnon_labor_resource_tab_1.extend;
4508                                    lnon_labor_resource_tab_1(lnon_labor_resource_tab_1.COUNT) := l_targ_non_labor_resource_tbl(kk);
4509                                    lexpenditure_category_tab_1.extend;
4510                                    lexpenditure_category_tab_1(lexpenditure_category_tab_1.COUNT) := l_targ_expend_category_tbl(kk);
4511                                    lrevenue_category_code_tab_1.extend;
4512                                    lrevenue_category_code_tab_1(lrevenue_category_code_tab_1.COUNT) := l_targ_rev_category_code_tbl(kk);
4513                                    lproject_role_id_tab_1.extend;
4514                                    lproject_role_id_tab_1(lproject_role_id_tab_1.COUNT) := l_targ_project_role_id_tbl(kk);
4515                                    lresource_class_code_tab_1.extend;
4516                                    lresource_class_code_tab_1(lresource_class_code_tab_1.COUNT) := l_targ_RESOURCE_CLASS_CODE_tbl(kk);
4517                                    l_da_mfc_cost_type_id_tab_1.extend;
4518                                    l_da_mfc_cost_type_id_tab_1(l_da_mfc_cost_type_id_tab_1.COUNT) := l_targ_mfc_cost_type_id_tbl(kk);
4519                                    lresource_class_flag_tab_1.extend;
4520                                    lresource_class_flag_tab_1(lresource_class_flag_tab_1.COUNT) := l_targ_resource_class_flag_tbl(kk);
4521                                    lfc_res_type_code_tab_1.extend;
4522                                    lfc_res_type_code_tab_1(lfc_res_type_code_tab_1.COUNT) := l_targ_fc_res_type_code_tbl(kk);
4523                                    linventory_item_id_tab_1.extend;
4524                                    linventory_item_id_tab_1(linventory_item_id_tab_1.COUNT) := l_targ_inventory_item_id_tbl(kk);
4525                                    litem_category_id_tab_1.extend;
4526                                    litem_category_id_tab_1(litem_category_id_tab_1.COUNT) := l_targ_item_category_id_tbl(kk);
4527                                    lperson_type_code_tab_1.extend;
4528                                    lperson_type_code_tab_1(lperson_type_code_tab_1.COUNT) :=  l_targ_person_type_code_tbl(kk);
4529                                    lbom_resource_id_tab_1.extend;
4530                                    lbom_resource_id_tab_1(lbom_resource_id_tab_1.COUNT) := l_targ_bom_resource_id_tbl(kk);
4531                                    lnamed_role_tab_1.extend;
4532                                    lnamed_role_tab_1(lnamed_role_tab_1.COUNT) := l_targ_NAMED_ROLE_tbl(kk);
4533                                    lincurred_by_res_flag_tab_1.extend;
4534                                    lincurred_by_res_flag_tab_1(lincurred_by_res_flag_tab_1.COUNT) := l_targ_incured_by_res_flag_tbl(kk);
4535                                    l_da_rate_based_flag_tab_1.extend;
4536                                    l_da_rate_based_flag_tab_1(l_da_rate_based_flag_tab_1.COUNT) := l_targ_rate_based_flag_tbl(kk);
4537                                END IF;
4538    --for End Bug 5291484
4539 
4540                        END IF;
4541                    END LOOP;
4542 
4543                    IF P_PA_debug_mode = 'Y' THEN
4544                           pa_debug.g_err_stage:= 'Calling res defaults with l_da_resource_list_members_tab '||l_da_resource_list_members_tab.count;
4545                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4546                    END IF;
4547 
4548 
4549                    IF l_da_resource_list_members_tab.COUNT > 0 THEN
4550                         PA_PLANNING_RESOURCE_UTILS.get_resource_defaults (
4551                         P_resource_list_members      => l_da_resource_list_members_tab,
4552                         P_project_id                 => l_project_id,
4553                         X_resource_class_flag        => l_da_resource_class_flag_tab,
4554                         X_resource_class_code        => l_da_resource_class_code_tab,
4555                         X_resource_class_id          => l_da_resource_class_id_tab,
4556                         X_res_type_code              => l_da_res_type_code_tab,
4557                         X_incur_by_res_type          => l_da_incur_by_res_type_tab,
4558                         X_person_id                  => l_da_person_id_tab,
4559                         X_job_id                     => l_da_job_id_tab,
4560                         X_person_type_code           => l_da_person_type_code_tab,
4561                         X_named_role                 => l_da_named_role_tab,
4562                         X_bom_resource_id            => l_da_bom_resource_id_tab,
4563                         X_non_labor_resource         => l_da_non_labor_resource_tab,
4564                         X_inventory_item_id          => l_da_inventory_item_id_tab,
4565                         X_item_category_id           => l_da_item_category_id_tab,
4566                         X_project_role_id            => l_da_project_role_id_tab,
4567                         X_organization_id            => l_da_organization_id_tab,
4568                         X_fc_res_type_code           => l_da_fc_res_type_code_tab,
4569                         X_expenditure_type           => l_da_expenditure_type_tab,
4570                         X_expenditure_category       => l_da_expenditure_category_tab,
4571                         X_event_type                 => l_da_event_type_tab,
4572                         X_revenue_category_code      => l_da_revenue_category_code_tab,
4573                         X_supplier_id                => l_da_supplier_id_tab,
4574                         X_spread_curve_id            => l_da_spread_curve_id_tab,
4575                         X_etc_method_code            => l_da_etc_method_code_tab,
4576                         X_mfc_cost_type_id           => l_da_mfc_cost_type_id_tab,
4577                         X_incurred_by_res_flag       => l_da_incurred_by_res_flag_tab,
4578                         X_incur_by_res_class_code    => l_da_incur_by_res_cls_code_tab,
4579                         X_incur_by_role_id           => l_da_incur_by_role_id_tab,
4580                         X_unit_of_measure            => l_da_unit_of_measure_tab,
4581                         X_org_id                     => l_da_org_id_tab,
4582                         X_rate_based_flag            => l_da_rate_based_flag_tab,
4583                         X_rate_expenditure_type      => l_da_rate_expenditure_type_tab,
4584                         X_rate_func_curr_code        => l_da_rate_func_curr_code_tab,
4585                         X_msg_data                   => l_MSG_DATA,
4586                         X_msg_count                  => l_MSG_COUNT,
4587                         X_return_status              => l_RETURN_STATUS);
4588 
4589 
4590                         IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
4591                               IF P_PA_debug_mode = 'Y' THEN
4592                                    pa_debug.g_err_stage:= 'Error in get_resource_defaults';
4593                                    pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
4594                               END IF;
4595                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4596                         END IF;
4597 
4598                         --The above API (resource defaults) returns resource attributes for the distinct resource list members passed.
4599                         --The below loop will copy the resource attributes from the distinct resource list members into all the
4600                         --resource assignments that have to be copied into the target. Bug 3678314.
4601                         IF P_PA_debug_mode = 'Y' THEN
4602                               pa_debug.g_err_stage:= 'Same RL About to copy the resource attributes for the distinct rlms into the tbls that will be used copying RAs into target';
4603                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4604                         END IF;
4605 
4606                         FOR kk in 1 .. l_targ_ra_id_tbl.count LOOP
4607 
4608                             --The attributes returned by the get_resource_defaults should be considered only if
4609                             --there is no one to one mapping between source and target (i.e l_src_ra_id_cnt_tbl(KK) is null)
4610                             --and the resource assignment has to be inserted into target(i.e  L_ra_dml_code_tbl(kk) = 'INSERT')
4611                             --In other cases the attributes in the source will be copied into target.
4612                             --Bug 3678314
4613                             IF   L_ra_dml_code_tbl(kk) = 'INSERT'
4614                             AND l_src_ra_id_cnt_tbl(KK) IS NULL THEN
4615 
4616                                 l_temp:=1;
4617                                 FOR LL IN 1..l_da_resource_list_members_tab.COUNT LOOP
4618 
4619                                     IF l_da_resource_list_members_tab(LL)=l_targ_rlm_id_tbl(kk) THEN
4620                                         l_temp:=LL;
4621                                         EXIT;
4622                                     END IF;
4623 
4624                                 END LOOP;
4625 
4626                                 --Raise an error if the resource list member in l_da_resource_list_members_tab does not
4627                                 --exist in l_targ_rlm_id_tbl (This should never happen). Bug 3678314.
4628                                 IF l_da_resource_list_members_tab(l_temp)<>l_targ_rlm_id_tbl(kk) THEN
4629 
4630                                     IF P_PA_debug_mode = 'Y' THEN
4631                                           pa_debug.g_err_stage:= 'Match for l_targ_rlm_id_tbl('||kk||') '||l_targ_rlm_id_tbl(kk) ||' not found in l_da_resource_list_members_tab';
4632                                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
4633 
4634                                     END IF;
4635                                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4636 
4637                                 END IF;
4638 
4639                                 l_targ_RESOURCE_CLASS_FLAG_tbl(kk)   := l_da_resource_class_flag_tab(l_temp)       ;
4640                                 l_targ_RESOURCE_CLASS_CODE_tbl(kk)   := l_da_resource_class_code_tab(l_temp)       ;
4641                                 -- If incurred_by_res_flag is 'Y', incur_by_res_type should be used
4642                                 IF nvl(l_da_incurred_by_res_flag_tab(l_temp),'N') = 'Y'  THEN
4643                                    l_targ_resource_type_code_tbl(kk) := l_da_incur_by_res_type_tab(l_temp)         ;
4644                                 ELSE
4645                                    l_targ_resource_type_code_tbl(kk) := l_da_res_type_code_tab(l_temp)             ;
4646                                 END IF;
4647                                 l_targ_person_id_tbl(kk)             := l_da_person_id_tab(l_temp)                 ;
4648                                 l_targ_job_id_tbl(kk)                := l_da_job_id_tab(l_temp)                    ;
4649                                 l_targ_person_type_code_tbl(kk)      := l_da_person_type_code_tab(l_temp)          ;
4650                                 l_targ_NAMED_ROLE_tbl(kk)            := l_da_named_role_tab(l_temp)                ;
4651                                 l_targ_bom_resource_id_tbl(kk)       := l_da_bom_resource_id_tab(l_temp)           ;
4652                                 l_targ_non_labor_resource_tbl(kk)    := l_da_non_labor_resource_tab(l_temp)        ;
4653                                 l_targ_inventory_item_id_tbl(kk)     := l_da_inventory_item_id_tab(l_temp)         ;
4654                                 l_targ_item_category_id_tbl(kk)      := l_da_item_category_id_tab(l_temp)          ;
4655                                 l_targ_project_role_id_tbl(kk)       := l_da_project_role_id_tab(l_temp)           ;
4656                                 l_targ_organization_id_tbl(kk)       := l_da_organization_id_tab(l_temp)           ;
4657                                 l_targ_fc_res_type_code_tbl(kk)      := l_da_fc_res_type_code_tab(l_temp)          ;
4658                                 l_targ_expenditure_type_tbl(kk)      := l_da_expenditure_type_tab(l_temp)          ;
4659                                 l_targ_expend_category_tbl(kk)       := l_da_expenditure_category_tab(l_temp)      ;
4660                                 l_targ_event_type_tbl(kk)            := l_da_event_type_tab(l_temp)                ;
4661                                 l_targ_rev_category_code_tbl(kk)     := l_da_revenue_category_code_tab(l_temp)     ;
4662                                 l_targ_supplier_id_tbl(kk)           := l_da_supplier_id_tab(l_temp)               ;
4663                                 l_targ_spread_curve_id_tbl(kk)       := l_da_spread_curve_id_tab(l_temp)           ;
4664                                 l_targ_etc_method_code_tbl(kk)       := l_da_etc_method_code_tab(l_temp)           ;
4665                                 l_targ_mfc_cost_type_id_tbl(kk)      := l_da_mfc_cost_type_id_tab(l_temp)          ;
4666                                 l_targ_INCURED_BY_RES_FLAG_tbl(kk)   := l_da_incurred_by_res_flag_tab(l_temp)      ;
4667                                 l_targ_INCR_BY_RES_CLS_COD_tbl(kk)   := l_da_incur_by_res_cls_code_tab(l_temp)     ;
4668                                 l_targ_INCUR_BY_ROLE_ID_tbl(kk)      := l_da_incur_by_role_id_tab(l_temp)          ;
4669                                 l_targ_RATE_EXPEND_TYPE_tbl(kk)      := l_da_rate_expenditure_type_tab(l_temp)     ;
4670                                 l_targ_RATE_EXP_FC_CUR_COD_tbl(kk)   := l_da_rate_func_curr_code_tab(l_temp)       ;
4671                                 l_targ_RATE_EXPEND_ORG_ID_tbl(kk)    := l_da_org_id_tab(l_temp);
4672                                 l_targ_res_rate_based_flag_tbl(kk)   := l_da_rate_based_flag_tab(l_temp);  --IPM Architecture Enhancement. This flag will be the same through out as derived from the resource list
4673 
4674                                 --For the resoruce assignments that should be inserted, the RBF flag and UOM derived from the source
4675                                 --planning transactions should be equal to the RBF and UOM returned by the get_resource_defaults API.
4676                                 --If they are not equal then RBF should be inserted as N in the target planning transaction and UOM
4677                                 --should be inserted as DOLLARS(i.e. Currency). Bug 3621847
4678                                 IF P_PA_debug_mode = 'Y' THEN
4679                                       pa_debug.g_err_stage:= 'About to derive the rbs and UOM for the target txn that should be inserted';
4680                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4681                                 END IF;
4682 
4683                                 IF P_PA_debug_mode = 'Y' THEN
4684                                       pa_debug.g_err_stage:= 'l_targ_rate_based_flag_tbl('||kk||') '||l_targ_rate_based_flag_tbl(kk);
4685                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4686 
4687                                       pa_debug.g_err_stage:= 'l_targ_unit_of_measure_tbl('||kk||') '||l_targ_unit_of_measure_tbl(kk);
4688                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4689 
4690                                 END IF;
4691 
4692                                 --If there is one to one mapping between source and target then UOM and RBF will be copied directly
4693                                 --from source. Otherwise, the UOM and RBF derived from source should be compared with the ones returned
4694                                 --by get_resource_defaults . If the source's UOM/RBF and default UOM/RBF are same then they will be
4695                                 --copied to target . Otherwise DOLLARS/N will be copied for UOM/RBF
4696 
4697                                 IF P_PA_debug_mode = 'Y' THEN
4698                                       pa_debug.g_err_stage:= 'l_targ_rate_based_flag_tbl('||kk||') '||l_targ_rate_based_flag_tbl(kk);
4699                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4700 
4701                                       pa_debug.g_err_stage:= 'l_targ_unit_of_measure_tbl('||kk||') '||l_targ_unit_of_measure_tbl(kk);
4702                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4703 
4704                                 END IF;
4705 
4706                                 IF l_targ_rate_based_flag_tbl(kk)=l_da_rate_based_flag_tab(l_temp) AND
4707                                    l_targ_unit_of_measure_tbl(kk)=l_da_unit_of_measure_tab(l_temp) THEN
4708 
4709                                     NULL;--Do Nothing in this case
4710 
4711                                 ELSE
4712 
4713                                     l_targ_rate_based_flag_tbl(kk):='N';
4714                                     l_targ_unit_of_measure_tbl(kk):='DOLLARS';
4715 
4716                                 END IF;
4717                                 IF P_PA_debug_mode = 'Y' THEN
4718                                       pa_debug.g_err_stage:= 'Al_targ_rate_based_flag_tbl('||kk||') '||l_targ_rate_based_flag_tbl(kk);
4719                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4720 
4721                                       pa_debug.g_err_stage:= 'Al_targ_unit_of_measure_tbl('||kk||') '||l_targ_unit_of_measure_tbl(kk);
4722                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4723 
4724                                 END IF;
4725 
4726                             END IF; --IF   L_ra_dml_code_tbl(kk) = 'INSERT'
4727                                     --AND l_src_ra_id_cnt_tbl(KK) IS NULL THEN
4728 
4729                        END LOOP;
4730 
4731                        --dbms_output.put_line('I14');
4732                        IF P_PA_debug_mode = 'Y' THEN
4733                               pa_debug.g_err_stage:= 'Done with the loop for preparing the pl/sql tbls for res attrs ';
4734                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4735                        END IF;
4736 
4737                        l_rbs_element_id_prm_tbl.EXTEND(l_da_resource_list_members_tab.COUNT);
4738                        l_txn_accum_header_id_prm_tbl.EXTEND(l_da_resource_list_members_tab.COUNT);
4739 
4740                        IF l_rbs_version_id IS NOT NULL THEN
4741                            PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs
4742                             (p_budget_version_id              => l_src_ver_id_tbl(j)
4743                             ,p_rbs_version_id                 => l_rbs_version_id
4744                             ,p_calling_process                => 'RBS_REFRESH'
4745                             ,p_calling_context                => 'PLSQL'
4746                             ,p_process_code                   => 'RBS_MAP'
4747                             ,p_calling_mode                   => 'PLSQL_TABLE'
4748                             ,p_init_msg_list_flag             => 'N'
4749                             ,p_commit_flag                    => 'N'
4750                             ,p_TXN_SOURCE_ID_tab           => l_da_resource_list_members_tab
4751                             ,p_TXN_SOURCE_TYPE_CODE_tab    => l_txn_src_typ_code_rbs_prm_tbl
4752                             ,p_PERSON_ID_tab               => l_da_person_id_tab
4753                             ,p_JOB_ID_tab                  => l_da_job_id_tab
4754                             ,p_ORGANIZATION_ID_tab         => l_da_organization_id_tab
4755                             ,p_VENDOR_ID_tab               => l_da_supplier_id_tab
4756                             ,p_EXPENDITURE_TYPE_tab        => l_da_expenditure_type_tab
4757                             ,p_EVENT_TYPE_tab              => l_da_event_type_tab
4758                             ,p_NON_LABOR_RESOURCE_tab      => l_da_non_labor_resource_tab
4759                             ,p_EXPENDITURE_CATEGORY_tab    => l_da_expenditure_category_tab
4760                             ,p_REVENUE_CATEGORY_CODE_tab   => l_da_revenue_category_code_tab
4761                     --        ,p_NLR_ORGANIZATION_ID_tab     =>
4762                     --        ,p_EVENT_CLASSIFICATION_tab    =>
4763                     --        ,p_SYS_LINK_FUNCTION_tab       =>
4764                             ,p_PROJECT_ROLE_ID_tab         => l_da_project_role_id_tab
4765                             ,p_RESOURCE_CLASS_CODE_tab     => l_da_resource_class_code_tab
4766                             ,p_MFC_COST_TYPE_ID_tab        => l_da_mfc_cost_type_id_tab
4767                             ,p_RESOURCE_CLASS_FLAG_tab     => l_da_resource_class_flag_tab
4768                             ,p_FC_RES_TYPE_CODE_tab        => l_da_fc_res_type_code_tab
4769                             ,p_INVENTORY_ITEM_ID_tab       => l_da_inventory_item_id_tab
4770                             ,p_ITEM_CATEGORY_ID_tab        => l_da_item_category_id_tab
4771                             ,p_PERSON_TYPE_CODE_tab        => l_da_person_type_code_tab
4772                             ,p_BOM_RESOURCE_ID_tab         => l_da_bom_resource_id_tab
4773                             ,p_NAMED_ROLE_tab              => l_da_named_role_tab
4774                             ,p_INCURRED_BY_RES_FLAG_tab    => l_da_incurred_by_res_flag_tab
4775                             ,p_RATE_BASED_FLAG_tab         => l_da_rate_based_flag_tab
4776                     --        ,p_TXN_TASK_ID_tab             =>
4777                     --        ,p_TXN_WBS_ELEMENT_VER_ID_tab  =>
4778                     --        ,p_TXN_RBS_ELEMENT_ID_tab      =>
4779                     --        ,p_TXN_PLAN_START_DATE_tab     =>
4780                     --        ,p_TXN_PLAN_END_DATE_tab       =>
4781                               ,x_txn_source_id_tab           => l_txn_source_id_tbl
4782                               ,x_res_list_member_id_tab      => l_res_list_member_id_tbl
4783                               ,x_rbs_element_id_tab          => l_rbs_element_id_prm_tbl
4784                               ,x_txn_accum_header_id_tab     => l_txn_accum_header_id_prm_tbl
4785                               ,x_return_status               => l_return_status
4786                               ,x_msg_count                   => l_msg_count
4787                               ,x_msg_data                    => l_msg_data);
4788 
4789                            IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
4790                                   IF P_PA_debug_mode = 'Y' THEN
4791                                        pa_debug.g_err_stage:= 'Error in get_resource_defaults';
4792                                        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
4793                                   END IF;
4794                                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4795                            END IF;
4796 
4797                            IF P_PA_debug_mode = 'Y' THEN
4798                                   pa_debug.g_err_stage:= 'Returned from PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs for getting rbs elem id for new RAs';
4799                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4800 
4801                                   pa_debug.g_err_stage:= 'l_rbs_element_id_prm_tbl.COUNT '||l_rbs_element_id_prm_tbl.COUNT ||' l_txn_accum_header_id_prm_tbl.COUNT '||l_txn_accum_header_id_prm_tbl.COUNT;
4802                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4803 
4804                            END IF;
4805 
4806                            -- The rbs element id and txn accum header id are returned for distinct resource list members. These attributes
4807                            -- should be copied for all the target resoruce assignments also. Bug 3678314.
4808                            FOR kk in 1 .. l_targ_ra_id_tbl.count LOOP
4809 
4810                                 IF   L_ra_dml_code_tbl(kk) = 'INSERT'
4811                                 AND  l_src_ra_id_cnt_tbl(KK) IS NULL THEN
4812 
4813                                     l_temp:=1;
4814                                     FOR LL IN 1..l_da_resource_list_members_tab.COUNT LOOP
4815 
4816                                         IF l_da_resource_list_members_tab(LL)=l_targ_rlm_id_tbl(kk) THEN
4817                                             l_temp:=LL;
4818                                             EXIT;
4819                                         END IF;
4820 
4821                                     END LOOP;
4822 
4823                                     --Raise an error if the resource list member in l_da_resource_list_members_tab does not
4824                                     --exist in l_targ_rlm_id_tbl (This should never happen).Bug 3678314.
4825                                     IF l_da_resource_list_members_tab(l_temp)<>l_targ_rlm_id_tbl(kk) THEN
4826 
4827                                         IF P_PA_debug_mode = 'Y' THEN
4828                                               pa_debug.g_err_stage:= 'Match for l_targ_rlm_id_tbl('||kk||') '||l_targ_rlm_id_tbl(kk) ||' not found in l_da_resource_list_members_tab';
4829                                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
4830 
4831                                         END IF;
4832                                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4833 
4834                                     END IF;
4835 
4836                                     l_targ_txn_accum_header_id_tbl(kk) := l_txn_accum_header_id_prm_tbl(l_temp);
4837                                     l_targ_rbs_element_id_tbl(kk)      := l_rbs_element_id_prm_tbl(l_temp);
4838 
4839                                 END IF;--IF   L_ra_dml_code_tbl(kk) = 'INSERT'
4840                                        --AND  l_src_ra_id_cnt_tbl(KK) IS NULL THEN
4841                            END LOOP;
4842 
4843                            IF P_PA_debug_mode = 'Y' THEN
4844                                   pa_debug.g_err_stage:= 'Done with preparing tbls of indexed txn accum header and rbs elem ids';
4845                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4846                            END IF;
4847 
4848                        END IF; --IF l_rbs_version_id IS NOT NULL THEN
4849 
4850  -- for Start Bug 5291484
4851 
4852                       ELSIF lresource_list_members_tab_1.COUNT > 0 THEN
4853 
4854                           l_rbs_element_id_prm_tbl_1.EXTEND(lresource_list_members_tab_1.COUNT);
4855                           ltxnaccumheader_id_prm_tbl_1.EXTEND(lresource_list_members_tab_1.COUNT);
4856 
4857                           IF l_rbs_version_id IS NOT NULL THEN
4858                               PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs
4859                                (p_budget_version_id              => l_src_ver_id_tbl(j)
4860                                ,p_rbs_version_id                 => l_rbs_version_id
4861                                ,p_calling_process                => 'RBS_REFRESH'
4862                                ,p_calling_context                => 'PLSQL'
4863                                ,p_process_code                   => 'RBS_MAP'
4864                                ,p_calling_mode                   => 'PLSQL_TABLE'
4865                                ,p_init_msg_list_flag             => 'N'
4866                                ,p_commit_flag                    => 'N'
4867                                ,p_TXN_SOURCE_ID_tab           => lresource_list_members_tab_1
4868                                ,p_TXN_SOURCE_TYPE_CODE_tab    => ltxnsrctyp_code_rbs_prm_tbl_1
4869                                ,p_PERSON_ID_tab               => lperson_id_tab_1
4870                                ,p_JOB_ID_tab                  => ljob_id_tab_1
4871                                ,p_ORGANIZATION_ID_tab         => lorganization_id_tab_1
4872                                ,p_VENDOR_ID_tab               => l_da_supplier_id_tab_1
4873                                ,p_EXPENDITURE_TYPE_tab        => lexpenditure_type_tab_1
4874                                ,p_EVENT_TYPE_tab              => l_da_event_type_tab_1
4875                                ,p_NON_LABOR_RESOURCE_tab      => lnon_labor_resource_tab_1
4876                                ,p_EXPENDITURE_CATEGORY_tab    => lexpenditure_category_tab_1
4877                                ,p_REVENUE_CATEGORY_CODE_tab   => lrevenue_category_code_tab_1
4878                                ,p_PROJECT_ROLE_ID_tab         => lproject_role_id_tab_1
4879                                ,p_RESOURCE_CLASS_CODE_tab     => lresource_class_code_tab_1
4880                                ,p_MFC_COST_TYPE_ID_tab        => l_da_mfc_cost_type_id_tab_1
4881                                ,p_RESOURCE_CLASS_FLAG_tab     => lresource_class_flag_tab_1
4882                                ,p_FC_RES_TYPE_CODE_tab        => lfc_res_type_code_tab_1
4883                                ,p_INVENTORY_ITEM_ID_tab       => linventory_item_id_tab_1
4884                                ,p_ITEM_CATEGORY_ID_tab        => litem_category_id_tab_1
4885                                ,p_PERSON_TYPE_CODE_tab        => lperson_type_code_tab_1
4886                                ,p_BOM_RESOURCE_ID_tab         => lbom_resource_id_tab_1
4887                                ,p_NAMED_ROLE_tab              => lnamed_role_tab_1
4888                                ,p_INCURRED_BY_RES_FLAG_tab    => lincurred_by_res_flag_tab_1
4889                                ,p_RATE_BASED_FLAG_tab         => l_da_rate_based_flag_tab_1
4890                                  ,x_txn_source_id_tab           => l_txn_source_id_tbl_1
4891                                  ,x_res_list_member_id_tab      => l_res_list_member_id_tbl_1
4892                                  ,x_rbs_element_id_tab          => l_rbs_element_id_prm_tbl_1
4893                                  ,x_txn_accum_header_id_tab     => ltxnaccumheader_id_prm_tbl_1
4894                                  ,x_return_status               => l_return_status
4895                                  ,x_msg_count                   => l_msg_count
4896                                  ,x_msg_data                    => l_msg_data);
4897 
4898                               IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
4899                                      IF P_PA_debug_mode = 'Y' THEN
4900                                           pa_debug.g_err_stage:= 'Error in Map_Rlmi_Rbs';
4901                                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
4902                                      END IF;
4903                                      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4904                               END IF;
4905 
4906                               FOR kk in 1 .. l_targ_ra_id_tbl.count LOOP
4907 
4908                                    IF   L_ra_dml_code_tbl(kk) = 'INSERT'
4909                                    AND  l_src_ra_id_cnt_tbl(KK) IS NOT NULL THEN
4910                                        l_temp:=1;
4911                                        FOR LL IN 1..lresource_list_members_tab_1.COUNT LOOP
4912 
4913                                            IF lresource_list_members_tab_1(LL)=l_targ_rlm_id_tbl(kk) THEN
4914                                                l_temp:=LL;
4915                                                EXIT;
4916                                            END IF;
4917 
4918                                        END LOOP;
4919 
4920                                        --Raise an error if the resource list member in l_da_resource_list_members_tab does not
4921                                        --exist in l_targ_rlm_id_tbl (This should never happen).Bug 3678314.
4922                                        IF lresource_list_members_tab_1(l_temp)<>l_targ_rlm_id_tbl(kk) THEN
4923                                            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4924                                        END IF;
4925                                        l_targ_txn_accum_header_id_tbl(kk) := ltxnaccumheader_id_prm_tbl_1(l_temp);
4926                                        l_targ_rbs_element_id_tbl(kk)      := l_rbs_element_id_prm_tbl_1(l_temp);
4927 
4928                                    END IF;
4929                               END LOOP;
4930                           END IF;
4931 
4932    -- for End Bug 5291484
4933 
4934 
4935                    END IF;--IF l_da_resource_list_members_tab.COUNT > 0
4936 
4937 
4938             ELSE --Resource lists are different
4939 
4940                --dbms_output.put_line('I15');
4941 
4942                IF P_PA_debug_mode = 'Y' THEN
4943                       pa_debug.g_err_stage:= 'About to prepare input table for get resource defaults API';
4944                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
4945                END IF;
4946                l_da_resource_list_members_tab:=SYSTEM.PA_NUM_TBL_TYPE();
4947                l_txn_src_typ_code_rbs_prm_tbl :=SYSTEM.pa_varchar2_30_tbl_type();
4948 
4949                FOR kk in 1 .. L_targ_rlm_id_tbl.count LOOP
4950 
4951                    --Find the distinct rlms among the resoruce assignments that have to be inserted .
4952                    --This is done to call get_resource_defaults only for distinct rlms. Bug 3859738
4953                    IF  L_ra_dml_code_tbl(kk) = 'INSERT' THEN
4954 
4955                         l_temp:= NULL;
4956                         FOR LL IN 1..l_da_resource_list_members_tab.COUNT LOOP
4957                             IF l_da_resource_list_members_tab(LL)= L_targ_rlm_id_tbl(kk) THEN
4958                                 l_temp:=LL;
4959                                 EXIT;
4960                             END IF;
4961                         END LOOP;
4962 
4963                         IF l_temp IS NULL THEN
4964                             --Indicates that the resource list member is not already selected and hence it has to be
4965                             --considered while calling get_resource_defaults.Bug 3859738
4966 
4967                             l_da_resource_list_members_tab.extend;
4968                             l_da_resource_list_members_tab(l_da_resource_list_members_tab.COUNT) := L_targ_rlm_id_tbl(kk);
4969                             l_txn_src_typ_code_rbs_prm_tbl.EXTEND;
4970                             l_txn_src_typ_code_rbs_prm_tbl(l_txn_src_typ_code_rbs_prm_tbl.COUNT):='RES_ASSIGNMENT';
4971 
4972                         END IF;
4973                    END IF;
4974                END LOOP;
4975 
4976                IF l_da_resource_list_members_tab.COUNT > 0 THEN
4977                     PA_PLANNING_RESOURCE_UTILS.get_resource_defaults (
4978                     P_resource_list_members      => l_da_resource_list_members_tab,
4979                     P_project_id                 => l_project_id,
4980                     X_resource_class_flag        => l_da_resource_class_flag_tab,
4981                     X_resource_class_code        => l_da_resource_class_code_tab,
4982                     X_resource_class_id          => l_da_resource_class_id_tab,
4983                     X_res_type_code              => l_da_res_type_code_tab,
4984                     X_incur_by_res_type          => l_da_incur_by_res_type_tab,
4985                     X_person_id                  => l_da_person_id_tab,
4986                     X_job_id                     => l_da_job_id_tab,
4987                     X_person_type_code           => l_da_person_type_code_tab,
4988                     X_named_role                 => l_da_named_role_tab,
4989                     X_bom_resource_id            => l_da_bom_resource_id_tab,
4990                     X_non_labor_resource         => l_da_non_labor_resource_tab,
4991                     X_inventory_item_id          => l_da_inventory_item_id_tab,
4992                     X_item_category_id           => l_da_item_category_id_tab,
4993                     X_project_role_id            => l_da_project_role_id_tab,
4994                     X_organization_id            => l_da_organization_id_tab,
4995                     X_fc_res_type_code           => l_da_fc_res_type_code_tab,
4996                     X_expenditure_type           => l_da_expenditure_type_tab,
4997                     X_expenditure_category       => l_da_expenditure_category_tab,
4998                     X_event_type                 => l_da_event_type_tab,
4999                     X_revenue_category_code      => l_da_revenue_category_code_tab,
5000                     X_supplier_id                => l_da_supplier_id_tab,
5001                     X_spread_curve_id            => l_da_spread_curve_id_tab,
5002                     X_etc_method_code            => l_da_etc_method_code_tab,
5003                     X_mfc_cost_type_id           => l_da_mfc_cost_type_id_tab,
5004                     X_incurred_by_res_flag       => l_da_incurred_by_res_flag_tab,
5005                     X_incur_by_res_class_code    => l_da_incur_by_res_cls_code_tab,
5006                     X_incur_by_role_id           => l_da_incur_by_role_id_tab,
5007                     X_unit_of_measure            => l_da_unit_of_measure_tab,
5008                     X_org_id                     => l_da_org_id_tab,
5009                     X_rate_based_flag            => l_da_rate_based_flag_tab,
5010                     X_rate_expenditure_type      => l_da_rate_expenditure_type_tab,
5011                     X_rate_func_curr_code        => l_da_rate_func_curr_code_tab,
5012                     X_msg_data                   => l_MSG_DATA,
5013                     X_msg_count                  => l_MSG_COUNT,
5014                     X_return_status              => l_RETURN_STATUS);
5015 
5016 
5017                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
5018                           IF P_PA_debug_mode = 'Y' THEN
5019                                pa_debug.g_err_stage:= 'Error in get_resource_defaults';
5020                                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5021                           END IF;
5022                           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5023                     END IF;
5024 
5025 
5026                     --The above API (resource defaults) returns resource attributes for the distinct resource list members passed.
5027                     --The below loop will copy the resource attributes from the distinct resource list members into all the
5028                     --resource assignments that have to be copied into the target. Bug 3678314.
5029                     IF P_PA_debug_mode = 'Y' THEN
5030                           pa_debug.g_err_stage:= 'Diff RL About to copy the resource attributes for the distinct rlms into the tbls that will be used copying RAs into target';
5031                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5032                     END IF;
5033 
5034                     FOR kk in 1 .. l_targ_ra_id_tbl.count LOOP
5035 
5036                         --The attributes returned by the get_resource_defaults should be considered only if
5037                         --the resource assignment has to be inserted into target(i.e  L_ra_dml_code_tbl(kk) = 'INSERT')
5038                         --If there is one-one mapping then the attributes in the source can not be used since
5039                         --the resource lists are different.
5040                         --If the resource assignment is already available then the attributes of the resource assignment
5041                         --will not change because of merge.
5042                         --Bug 3678314
5043                         IF l_ra_dml_code_tbl(kk)='INSERT'  THEN
5044                             l_temp:=1;
5045                             FOR LL IN 1..l_da_resource_list_members_tab.COUNT LOOP
5046 
5047                                 IF l_da_resource_list_members_tab(LL)=l_targ_rlm_id_tbl(kk) THEN
5048                                     l_temp:=LL;
5049                                     EXIT;
5050                                 END IF;
5051 
5052                             END LOOP;
5053 
5054                             --Raise an error if the resource list member in l_da_resource_list_members_tab does not
5055                             --exist in l_targ_rlm_id_tbl (This should never happen). Bug 3678314.
5056                             IF l_da_resource_list_members_tab(l_temp)<>l_targ_rlm_id_tbl(kk) THEN
5057 
5058                                 IF P_PA_debug_mode = 'Y' THEN
5059                                       pa_debug.g_err_stage:= 'Match for l_targ_rlm_id_tbl('||kk||') '||l_targ_rlm_id_tbl(kk) ||' not found in l_da_resource_list_members_tab';
5060                                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5061 
5062                                 END IF;
5063                                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5064 
5065                             END IF;
5066 
5067                             l_targ_RESOURCE_CLASS_FLAG_tbl(kk)   := l_da_resource_class_flag_tab(l_temp)       ;
5068                             l_targ_RESOURCE_CLASS_CODE_tbl(kk)   := l_da_resource_class_code_tab(l_temp)       ;
5069                             -- If incurred_by_res_flag is 'Y', incur_by_res_type should be used
5070                             IF nvl(l_da_incurred_by_res_flag_tab(l_temp),'N') = 'Y'  THEN
5071                                l_targ_resource_type_code_tbl(kk) := l_da_incur_by_res_type_tab(l_temp)         ;
5072                             ELSE
5073                                l_targ_resource_type_code_tbl(kk) := l_da_res_type_code_tab(l_temp)             ;
5074                             END IF;
5075                             l_targ_person_id_tbl(kk)             := l_da_person_id_tab(l_temp)                 ;
5076                             l_targ_job_id_tbl(kk)                := l_da_job_id_tab(l_temp)                    ;
5077                             l_targ_person_type_code_tbl(kk)      := l_da_person_type_code_tab(l_temp)          ;
5078                             l_targ_NAMED_ROLE_tbl(kk)            := l_da_named_role_tab(l_temp)                ;
5079                             l_targ_bom_resource_id_tbl(kk)       := l_da_bom_resource_id_tab(l_temp)           ;
5080                             l_targ_non_labor_resource_tbl(kk)    := l_da_non_labor_resource_tab(l_temp)        ;
5081                             l_targ_inventory_item_id_tbl(kk)     := l_da_inventory_item_id_tab(l_temp)         ;
5082                             l_targ_item_category_id_tbl(kk)      := l_da_item_category_id_tab(l_temp)          ;
5083                             l_targ_project_role_id_tbl(kk)       := l_da_project_role_id_tab(l_temp)           ;
5084                             l_targ_organization_id_tbl(kk)       := l_da_organization_id_tab(l_temp)           ;
5085                             l_targ_fc_res_type_code_tbl(kk)      := l_da_fc_res_type_code_tab(l_temp)          ;
5086                             l_targ_expenditure_type_tbl(kk)      := l_da_expenditure_type_tab(l_temp)          ;
5087                             l_targ_expend_category_tbl(kk)       := l_da_expenditure_category_tab(l_temp)      ;
5088                             l_targ_event_type_tbl(kk)            := l_da_event_type_tab(l_temp)                ;
5089                             l_targ_rev_category_code_tbl(kk)     := l_da_revenue_category_code_tab(l_temp)     ;
5090                             l_targ_supplier_id_tbl(kk)           := l_da_supplier_id_tab(l_temp)               ;
5091                             l_targ_spread_curve_id_tbl(kk)       := l_da_spread_curve_id_tab(l_temp)           ;
5092                             l_targ_etc_method_code_tbl(kk)       := l_da_etc_method_code_tab(l_temp)           ;
5093                             l_targ_mfc_cost_type_id_tbl(kk)      := l_da_mfc_cost_type_id_tab(l_temp)          ;
5094                             l_targ_INCURED_BY_RES_FLAG_tbl(kk)   := l_da_incurred_by_res_flag_tab(l_temp)      ;
5095 
5096                             l_targ_INCR_BY_RES_CLS_COD_tbl(kk)   := l_da_incur_by_res_cls_code_tab(l_temp)     ;
5097                             l_targ_INCUR_BY_ROLE_ID_tbl(kk)      := l_da_incur_by_role_id_tab(l_temp)          ;
5098                             l_targ_RATE_EXPEND_TYPE_tbl(kk)      := l_da_rate_expenditure_type_tab(l_temp)     ;
5099                             l_targ_RATE_EXP_FC_CUR_COD_tbl(kk)   := l_da_rate_func_curr_code_tab(l_temp)       ;
5100                             l_targ_RATE_EXPEND_ORG_ID_tbl(kk)    := l_da_org_id_tab(l_temp);
5101                             l_targ_RES_RATE_BASED_FLAG_tbl(kk)   := l_targ_rate_based_flag_tbl(l_temp)         ; --IPM
5102 
5103                             --For the resoruce assignments that should be inserted, the RBF flag and UOM derived from the source
5104                             --planning transactions should be equal to the RBF and UOM returned by the get_resource_defaults API.
5105                             --If they are not equal then RBF should be inserted as N in the target planning transaction and UOM
5106                             --should be inserted as DOLLARS(i.e. Currency). Bug 3678314
5107                             IF P_PA_debug_mode = 'Y' THEN
5108                                   pa_debug.g_err_stage:= 'About to derive the rbs and UOM for the target txn that should be inserted';
5109                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5110                             END IF;
5111 
5112                             IF P_PA_debug_mode = 'Y' THEN
5113                                   pa_debug.g_err_stage:= 'l_targ_rate_based_flag_tbl('||kk||') '||l_targ_rate_based_flag_tbl(kk);
5114                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5115 
5116                                   pa_debug.g_err_stage:= 'l_targ_unit_of_measure_tbl('||kk||') '||l_targ_unit_of_measure_tbl(kk);
5117                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5118 
5119                             END IF;
5120 
5121 
5122                             --If the RBF/UOM derived through resource list mapping are not same as RBF/UOM returned by get_resource_defaults
5123                             --then RBF/UOM should be changed to DOLLARS/N. Bug 3678314
5124                             IF P_PA_debug_mode = 'Y' THEN
5125                                   pa_debug.g_err_stage:= 'l_targ_rate_based_flag_tbl('||kk||') '||l_targ_rate_based_flag_tbl(kk);
5126                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5127 
5128                                   pa_debug.g_err_stage:= 'l_targ_unit_of_measure_tbl('||kk||') '||l_targ_unit_of_measure_tbl(kk);
5129                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5130 
5131                             END IF;
5132 
5133                             IF l_targ_rate_based_flag_tbl(kk)=l_da_rate_based_flag_tab(l_temp) AND
5134                                l_targ_unit_of_measure_tbl(kk)=l_da_unit_of_measure_tab(l_temp) THEN
5135 
5136                                 NULL;--Do Nothing in this case
5137 
5138                             ELSE
5139 
5140                                 l_targ_rate_based_flag_tbl(kk):='N';
5141                                 l_targ_unit_of_measure_tbl(kk):='DOLLARS';
5142 
5143                             END IF;
5144                             IF P_PA_debug_mode = 'Y' THEN
5145                                   pa_debug.g_err_stage:= 'Al_targ_rate_based_flag_tbl('||kk||') '||l_targ_rate_based_flag_tbl(kk);
5146                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5147 
5148                                   pa_debug.g_err_stage:= 'Al_targ_unit_of_measure_tbl('||kk||') '||l_targ_unit_of_measure_tbl(kk);
5149                                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5150 
5151                             END IF;
5152 
5153                         END IF;-- IF l_ra_dml_code_tbl(kk)='INSERT'  THEN
5154 
5155                     END LOOP;
5156 
5157                     l_rbs_element_id_prm_tbl:=SYSTEM.pa_num_tbl_type();
5158                     l_txn_accum_header_id_prm_tbl:=SYSTEM.pa_num_tbl_type();
5159                     l_rbs_element_id_prm_tbl.EXTEND(l_da_resource_list_members_tab.COUNT);
5160                     l_txn_accum_header_id_prm_tbl.EXTEND(l_da_resource_list_members_tab.COUNT);
5161                     IF l_rbs_version_id IS NOT NULL THEN
5162 
5163                         IF P_PA_debug_mode = 'Y' THEN
5164                               pa_debug.g_err_stage:= 'About to call PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs when RLS are different for RBS REF';
5165                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5166                         END IF;
5167 
5168 
5169                         PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs
5170                                  (p_budget_version_id              => l_src_ver_id_tbl(j)
5171                                  ,p_rbs_version_id                 => l_rbs_version_id
5172                                  ,p_calling_process                => 'RBS_REFRESH'
5173                                  ,p_calling_context                => 'PLSQL'
5174                                  ,p_process_code                   => 'RBS_MAP'
5175                                  ,p_calling_mode                   => 'PLSQL_TABLE'
5176                                  ,p_init_msg_list_flag             => 'N'
5177                                  ,p_commit_flag                    => 'N'
5178                                  ,p_TXN_SOURCE_ID_tab           => l_da_resource_list_members_tab
5179                                  ,p_TXN_SOURCE_TYPE_CODE_tab    => l_txn_src_typ_code_rbs_prm_tbl
5180                                  ,p_PERSON_ID_tab               => l_da_person_id_tab
5181                                  ,p_JOB_ID_tab                  => l_da_job_id_tab
5182                                  ,p_ORGANIZATION_ID_tab         => l_da_organization_id_tab
5183                                  ,p_VENDOR_ID_tab               => l_da_supplier_id_tab
5184                                  ,p_EXPENDITURE_TYPE_tab        => l_da_expenditure_type_tab
5185                                  ,p_EVENT_TYPE_tab              => l_da_event_type_tab
5186                                  ,p_NON_LABOR_RESOURCE_tab      => l_da_non_labor_resource_tab
5187                                  ,p_EXPENDITURE_CATEGORY_tab    => l_da_expenditure_category_tab
5188                                  ,p_REVENUE_CATEGORY_CODE_tab   => l_da_revenue_category_code_tab
5189                          --        ,p_NLR_ORGANIZATION_ID_tab     =>
5190                          --        ,p_EVENT_CLASSIFICATION_tab    =>
5191                          --        ,p_SYS_LINK_FUNCTION_tab       =>
5192                                  ,p_PROJECT_ROLE_ID_tab         => l_da_project_role_id_tab
5193                                  ,p_RESOURCE_CLASS_CODE_tab     => l_da_resource_class_code_tab
5194                                  ,p_MFC_COST_TYPE_ID_tab        => l_da_mfc_cost_type_id_tab
5195                                  ,p_RESOURCE_CLASS_FLAG_tab     => l_da_resource_class_flag_tab
5196                                  ,p_FC_RES_TYPE_CODE_tab        => l_da_fc_res_type_code_tab
5197                                  ,p_INVENTORY_ITEM_ID_tab       => l_da_inventory_item_id_tab
5198                                  ,p_ITEM_CATEGORY_ID_tab        => l_da_item_category_id_tab
5199                                  ,p_PERSON_TYPE_CODE_tab        => l_da_person_type_code_tab
5200                                  ,p_BOM_RESOURCE_ID_tab         => l_da_bom_resource_id_tab
5201                                  ,p_NAMED_ROLE_tab              => l_da_named_role_tab
5202                                  ,p_INCURRED_BY_RES_FLAG_tab    => l_da_incurred_by_res_flag_tab
5203                                  ,p_RATE_BASED_FLAG_tab         => l_da_rate_based_flag_tab
5204                          --        ,p_TXN_TASK_ID_tab             =>
5205                          --        ,p_TXN_WBS_ELEMENT_VER_ID_tab  =>
5206                          --        ,p_TXN_RBS_ELEMENT_ID_tab      =>
5207                          --        ,p_TXN_PLAN_START_DATE_tab     =>
5208                          --        ,p_TXN_PLAN_END_DATE_tab       =>
5209                                    ,x_txn_source_id_tab           => l_txn_source_id_tbl
5210                                    ,x_res_list_member_id_tab      => l_res_list_member_id_tbl
5211                                    ,x_rbs_element_id_tab          => l_rbs_element_id_prm_tbl
5212                                    ,x_txn_accum_header_id_tab     => l_txn_accum_header_id_prm_tbl
5213                                    ,x_return_status               => l_return_status
5214                                    ,x_msg_count                   => l_msg_count
5215                                    ,x_msg_data                    => l_msg_data);
5216 
5217                          IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
5218                              IF P_PA_debug_mode = 'Y' THEN
5219                                    pa_debug.g_err_stage:= 'Error in get_resource_defaults ';
5220                                    pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5221                              END IF;
5222                              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5223                          END IF;
5224 
5225                          IF P_PA_debug_mode = 'Y' THEN
5226                               pa_debug.g_err_stage:= 'Preparing TXN Accum Header Id and RBS Elem Id tbls';
5227                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5228 
5229                               pa_debug.g_err_stage:= 'l_txn_accum_header_id_prm_tbl.count is '||l_txn_accum_header_id_prm_tbl.count ||' l_rbs_element_id_prm_tbl.count is '||l_rbs_element_id_prm_tbl.count;
5230                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5231 
5232                          END IF;
5233 
5234 
5235                          -- The rbs element id and txn accum header id are returned for distinct resource list members. These attributes
5236                          -- should be copied for all the target resoruce assignments also. Bug 3678314.
5237                          FOR kk in 1 .. l_targ_ra_id_tbl.count LOOP
5238 
5239                               IF l_ra_dml_code_tbl(kk)='INSERT'  THEN
5240 
5241                                   l_temp:=1;
5242                                   FOR LL IN 1..l_da_resource_list_members_tab.COUNT LOOP
5243 
5244                                       IF l_da_resource_list_members_tab(LL)=l_targ_rlm_id_tbl(kk) THEN
5245                                           l_temp:=LL;
5246                                           EXIT;
5247                                       END IF;
5248 
5249                                   END LOOP;
5250 
5251                                   --Raise an error if the resource list member in l_da_resource_list_members_tab does not
5252                                   --exist in l_targ_rlm_id_tbl (This should never happen).Bug 3678314.
5253                                   IF l_da_resource_list_members_tab(l_temp)<>l_targ_rlm_id_tbl(kk) THEN
5254 
5255                                       IF P_PA_debug_mode = 'Y' THEN
5256                                             pa_debug.g_err_stage:= 'Match for l_targ_rlm_id_tbl('||kk||') '||l_targ_rlm_id_tbl(kk) ||' not found in l_da_resource_list_members_tab';
5257                                             pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5258 
5259                                       END IF;
5260                                       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5261 
5262                                   END IF;
5263 
5264                                   l_targ_txn_accum_header_id_tbl(kk) := l_txn_accum_header_id_prm_tbl(l_temp);
5265                                   l_targ_rbs_element_id_tbl(kk) := l_rbs_element_id_prm_tbl(l_temp);
5266 
5267                               END IF;-- IF l_ra_dml_code_tbl(kk)='INSERT'  THEN
5268 
5269                          END LOOP;
5270 
5271                     END IF;--IF l_rbs_version_id IS NOT NULL THEN
5272 
5273                END IF; -- IF l_da_resource_list_members_tab.COUNT > 0
5274 
5275             END IF;  --IF l_src_resource_list_id = l_targ_resource_list_id
5276 
5277             --dbms_output.put_line('I16');
5278 
5279             --dbms_output.put_line('I17');
5280 
5281 
5282             IF P_PA_debug_mode = 'Y' THEN
5283                   pa_debug.g_err_stage:= 'About to bulk insert into PRA';
5284                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5285 
5286                IF l_targ_rlm_id_tbl.COUNT>1 THEN
5287                   pa_debug.g_err_stage:= 'l_targ_rlm_id_tbl(1) is '||l_targ_rlm_id_tbl(1)||' l_targ_rlm_id_tbl(2) is '||l_targ_rlm_id_tbl(2);
5288                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5289 
5290                   pa_debug.g_err_stage:= 'L_targ_ra_id_tbl(1) is '||L_targ_ra_id_tbl(1)||' L_targ_ra_id_tbl(2) is '||L_targ_ra_id_tbl(2);
5291                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5292 
5293                   pa_debug.g_err_stage:= 'L_targ_task_id_tbl(1) is '||L_targ_task_id_tbl(1)||' L_targ_task_id_tbl(2) is '||L_targ_task_id_tbl(2);
5294                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5295 
5296                   pa_debug.g_err_stage:= 'l_src_ra_id_cnt_tbl(1) is '||l_src_ra_id_cnt_tbl(1)||' l_src_ra_id_cnt_tbl(2) is '||l_src_ra_id_cnt_tbl(2);
5297                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5298 
5299                  pa_debug.g_err_stage:= 'l_src_resource_list_id is '||l_src_resource_list_id||' l_targ_resource_list_id is '||l_targ_resource_list_id;
5300                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5301 
5302                  pa_debug.g_err_stage:= 'L_ra_dml_code_tbl(1) is '||L_ra_dml_code_tbl(1)||' L_ra_dml_code_tbl(2) is '||L_ra_dml_code_tbl(2);
5303                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5304 
5305                  pa_debug.g_err_stage:= 'l_targ_txn_accum_header_id_tbl(1) is '||l_targ_txn_accum_header_id_tbl(1)||' l_targ_txn_accum_header_id_tbl(2) is '||l_targ_txn_accum_header_id_tbl(2);
5306                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5307 
5308                  pa_debug.g_err_stage:= 'l_targ_rbs_element_id_tbl(1) is '||l_targ_rbs_element_id_tbl(1)||' l_targ_rbs_element_id_tbl(2) is '||l_targ_rbs_element_id_tbl(2);
5309                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5310 
5311 
5312                END IF;
5313 
5314             END IF;
5315 
5316             --dbms_output.put_line('I18');
5317             -- Bug 3934574 The following business rules are incorporated in the insert
5318             -- 1) If p_calling_context is null, insert transaction source code as null
5319             -- 2) If p_calling_context is generation insert transaction source code as 'CHANGE DOCUMENTS'
5320 
5321             FORALL kk in L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
5322             INSERT INTO PA_RESOURCE_ASSIGNMENTS (
5323                     RESOURCE_ASSIGNMENT_ID,BUDGET_VERSION_ID,PROJECT_ID,TASK_ID,RESOURCE_LIST_MEMBER_ID,CBS_ELEMENT_ID,LAST_UPDATE_DATE,LAST_UPDATED_BY,CREATION_DATE,CREATED_BY
5324                     ,LAST_UPDATE_LOGIN,UNIT_OF_MEASURE,TRACK_AS_LABOR_FLAG,STANDARD_BILL_RATE,AVERAGE_BILL_RATE,AVERAGE_COST_RATE
5325                     ,PROJECT_ASSIGNMENT_ID,PLAN_ERROR_CODE,TOTAL_PLAN_REVENUE,TOTAL_PLAN_RAW_COST,TOTAL_PLAN_BURDENED_COST,TOTAL_PLAN_QUANTITY
5326                     ,AVERAGE_DISCOUNT_PERCENTAGE,TOTAL_BORROWED_REVENUE,TOTAL_TP_REVENUE_IN,TOTAL_TP_REVENUE_OUT,TOTAL_REVENUE_ADJ
5327                     ,TOTAL_LENT_RESOURCE_COST,TOTAL_TP_COST_IN,TOTAL_TP_COST_OUT,TOTAL_COST_ADJ,TOTAL_UNASSIGNED_TIME_COST
5328                     ,TOTAL_UTILIZATION_PERCENT,TOTAL_UTILIZATION_HOURS,TOTAL_UTILIZATION_ADJ,TOTAL_CAPACITY,TOTAL_HEAD_COUNT
5329                     ,TOTAL_HEAD_COUNT_ADJ,RESOURCE_ASSIGNMENT_TYPE,TOTAL_PROJECT_RAW_COST,TOTAL_PROJECT_BURDENED_COST,TOTAL_PROJECT_REVENUE
5330                     ,PARENT_ASSIGNMENT_ID,WBS_ELEMENT_VERSION_ID,RBS_ELEMENT_ID,PLANNING_START_DATE,PLANNING_END_DATE
5331                     ,SPREAD_CURVE_ID,ETC_METHOD_CODE,RES_TYPE_CODE,ATTRIBUTE_CATEGORY,ATTRIBUTE1,ATTRIBUTE2,ATTRIBUTE3,ATTRIBUTE4,ATTRIBUTE5
5332                     ,ATTRIBUTE6,ATTRIBUTE7,ATTRIBUTE8,ATTRIBUTE9,ATTRIBUTE10,ATTRIBUTE11,ATTRIBUTE12,ATTRIBUTE13,ATTRIBUTE14,ATTRIBUTE15
5333                     ,ATTRIBUTE16,ATTRIBUTE17,ATTRIBUTE18,ATTRIBUTE19,ATTRIBUTE20,ATTRIBUTE21,ATTRIBUTE22,ATTRIBUTE23,ATTRIBUTE24,ATTRIBUTE25
5334                     ,ATTRIBUTE26,ATTRIBUTE27,ATTRIBUTE28,ATTRIBUTE29,ATTRIBUTE30,FC_RES_TYPE_CODE,RESOURCE_CLASS_CODE,ORGANIZATION_ID,JOB_ID
5335                     ,PERSON_ID,EXPENDITURE_TYPE,EXPENDITURE_CATEGORY,REVENUE_CATEGORY_CODE,EVENT_TYPE,SUPPLIER_ID,NON_LABOR_RESOURCE
5336                     ,BOM_RESOURCE_ID,INVENTORY_ITEM_ID,ITEM_CATEGORY_ID,RECORD_VERSION_NUMBER,BILLABLE_PERCENT
5337                     ,TRANSACTION_SOURCE_CODE,MFC_COST_TYPE_ID,PROCURE_RESOURCE_FLAG,ASSIGNMENT_DESCRIPTION
5338                     ,INCURRED_BY_RES_FLAG,RATE_JOB_ID,RATE_EXPENDITURE_TYPE,TA_DISPLAY_FLAG
5339                     ,SP_FIXED_DATE,PERSON_TYPE_CODE,RATE_BASED_FLAG,RESOURCE_RATE_BASED_FLAG            --IPM Arch Enhancement
5340                     ,USE_TASK_SCHEDULE_FLAG,RATE_EXP_FUNC_CURR_CODE
5341                     ,RATE_EXPENDITURE_ORG_ID,INCUR_BY_RES_CLASS_CODE,INCUR_BY_ROLE_ID
5342                     ,PROJECT_ROLE_ID,RESOURCE_CLASS_FLAG,NAMED_ROLE,TXN_ACCUM_HEADER_ID)
5343                  SELECT  L_targ_ra_id_tbl(kk)               -- RESOURCE_ASSIGNMENT_ID
5344                         ,p_budget_version_id                -- BUDGET_VERSION_ID
5345                         ,l_project_id                       -- PROJECT_ID
5346                         ,L_targ_task_id_tbl(kk)             -- TASK_ID
5347                         ,L_targ_rlm_id_tbl(kk)              -- RESOURCE_LIST_MEMBER_ID
5348                         ,L_TARG_CBS_ELEM_ID_TBL(kk)				-- CBS_ELEMENT_ID  --bug#16200605
5349 						,sysdate                            -- LAST_UPDATE_DATE
5350                         ,fnd_global.user_id                 -- LAST_UPDATED_BY
5351                         ,sysdate                            -- CREATION_DATE
5352                         ,fnd_global.user_id                 -- CREATED_BY
5353                         ,fnd_global.login_id                -- LAST_UPDATE_LOGIN
5354                         ,l_targ_unit_of_measure_tbl(kk)     -- UNIT_OF_MEASURE
5355                         ,NULL                               -- TRACK_AS_LABOR_FLAG
5356                         ,NULL                               -- STANDARD_BILL_RATE
5357                         ,NULL                               -- AVERAGE_BILL_RATE
5358                         ,NULL                               -- AVERAGE_COST_RATE
5359                         ,-1                                 -- PROJECT_ASSIGNMENT_ID
5360                         ,NULL                               -- PLAN_ERROR_CODE
5361                         ,NULL                               -- TOTAL_PLAN_REVENUE
5362                         ,NULL                               -- TOTAL_PLAN_RAW_COST
5363                         ,NULL                               -- TOTAL_PLAN_BURDENED_COST
5364                         ,NULL                               -- TOTAL_PLAN_QUANTITY
5365                         ,NULL                               -- AVERAGE_DISCOUNT_PERCENTAGE
5366                         ,NULL                               -- TOTAL_BORROWED_REVENUE
5367                         ,NULL                               -- TOTAL_TP_REVENUE_IN
5368                         ,NULL                               -- TOTAL_TP_REVENUE_OUT
5369                         ,NULL                               -- TOTAL_REVENUE_ADJ
5370                         ,NULL                               -- TOTAL_LENT_RESOURCE_COST
5371                         ,NULL                               -- TOTAL_TP_COST_IN
5372                         ,NULL                               -- TOTAL_TP_COST_OUT
5373                         ,NULL                               -- TOTAL_COST_ADJ
5374                         ,NULL                               -- TOTAL_UNASSIGNED_TIME_COST
5375                         ,NULL                               -- TOTAL_UTILIZATION_PERCENT
5376                         ,NULL                               -- TOTAL_UTILIZATION_HOURS
5377                         ,NULL                               -- TOTAL_UTILIZATION_ADJ
5378                         ,NULL                               -- TOTAL_CAPACITY
5379                         ,NULL                               -- TOTAL_HEAD_COUNT
5380                         ,NULL                               -- TOTAL_HEAD_COUNT_ADJ
5381                         ,'USER_ENTERED'                     -- RESOURCE_ASSIGNMENT_TYPE
5382                         ,NULL                               -- TOTAL_PROJECT_RAW_COST
5383                         ,NULL                               -- TOTAL_PROJECT_BURDENED_COST
5384                         ,NULL                               -- TOTAL_PROJECT_REVENUE
5385                         ,NULL                               -- PARENT_ASSIGNMENT_ID
5386                         ,NULL                               -- WBS_ELEMENT_VERSION_ID
5387                         ,l_targ_rbs_element_id_tbl(kk)      -- RBS_ELEMENT_ID
5388                         ,l_planning_start_date_tbl(kk)      -- PLANNING_START_DATE
5389                         ,l_planning_end_date_tbl(kk)        -- PLANNING_END_DATE
5390                         ,l_targ_spread_curve_id_tbl(kk)     -- SPREAD_CURVE_ID
5391                         ,l_targ_etc_method_code_tbl(kk)     -- ETC_METHOD_CODE
5392                         ,l_targ_resource_type_code_tbl(kk)  -- RES_TYPE_CODE
5393                         ,NULL                               -- ATTRIBUTE_CATEGORY
5394                         ,NULL                               -- ATTRIBUTE1
5395                         ,NULL                               -- ATTRIBUTE2
5396                         ,NULL                               -- ATTRIBUTE3
5397                         ,NULL                               -- ATTRIBUTE4
5398                         ,NULL                               -- ATTRIBUTE5
5399                         ,NULL                               -- ATTRIBUTE6
5400                         ,NULL                               -- ATTRIBUTE7
5401                         ,NULL                               -- ATTRIBUTE8
5402                         ,NULL                               -- ATTRIBUTE9
5403                         ,NULL                               -- ATTRIBUTE10
5404                         ,NULL                               -- ATTRIBUTE11
5405                         ,NULL                               -- ATTRIBUTE12
5406                         ,NULL                               -- ATTRIBUTE13
5407                         ,NULL                               -- ATTRIBUTE14
5408                         ,NULL                               -- ATTRIBUTE15
5409                         ,NULL                               -- ATTRIBUTE16
5410                         ,NULL                               -- ATTRIBUTE17
5411                         ,NULL                               -- ATTRIBUTE18
5412                         ,NULL                               -- ATTRIBUTE19
5413                         ,NULL                               -- ATTRIBUTE20
5414                         ,NULL                               -- ATTRIBUTE21
5415                         ,NULL                               -- ATTRIBUTE22
5416                         ,NULL                               -- ATTRIBUTE23
5417                         ,NULL                               -- ATTRIBUTE24
5418                         ,NULL                               -- ATTRIBUTE25
5419                         ,NULL                               -- ATTRIBUTE26
5420                         ,NULL                               -- ATTRIBUTE27
5421                         ,NULL                               -- ATTRIBUTE28
5422                         ,NULL                               -- ATTRIBUTE29
5423                         ,NULL                               -- ATTRIBUTE30
5424                         ,l_targ_fc_res_type_code_tbl(kk)    -- FC_RES_TYPE_CODE
5425                         ,l_targ_resource_class_code_tbl(kk) -- RESOURCE_CLASS_CODE
5426                         ,l_targ_organization_id_tbl(kk)     -- ORGANIZATION_ID
5427                         ,l_targ_job_id_tbl(kk)              -- JOB_ID
5428                         ,l_targ_person_id_tbl(kk)           -- PERSON_ID
5429                         ,l_targ_expenditure_type_tbl(kk)    -- EXPENDITURE_TYPE
5430                         ,l_targ_expend_category_tbl(kk)     -- EXPENDITURE_CATEGORY
5431                         ,l_targ_rev_category_code_tbl(kk)   -- REVENUE_CATEGORY_CODE
5432                         ,l_targ_event_type_tbl(kk)          -- EVENT_TYPE
5433                         ,l_targ_supplier_id_tbl(kk)         -- SUPPLIER_ID
5434                         ,l_targ_non_labor_resource_tbl(kk)  -- NON_LABOR_RESOURCE
5435                         ,l_targ_bom_resource_id_tbl(kk)     -- BOM_RESOURCE_ID
5436                         ,l_targ_inventory_item_id_tbl(kk)   -- INVENTORY_ITEM_ID
5437                         ,l_targ_item_category_id_tbl(kk)    -- ITEM_CATEGORY_ID
5438                         ,1                                  -- RECORD_VERSION_NUMBER
5439                         ,NULL                               -- BILLABLE_PERCENT
5440                         , Decode(p_calling_context, null, null, -- BUG 3934574
5441                                   'BUDGET_GENERATION', 'CHANGE_DOCUMENTS',
5442                                   'FORECAST_GENERATION','CHANGE_DOCUMENTS') -- TRANSACTION_SOURCE_CODE
5443                         ,l_targ_mfc_cost_type_id_tbl(kk)    -- MFC_COST_TYPE_ID
5444                         ,NULL                               -- PROCURE_RESOURCE_FLAG
5445                         ,NULL                               -- ASSIGNMENT_DESCRIPTION
5446                         ,l_targ_incured_by_res_flag_tbl(kk) -- INCURRED_BY_RES_FLAG
5447                         ,NULL                               -- RATE_JOB_ID
5448                         ,l_targ_RATE_EXPEND_TYPE_tbl(kk)    -- RATE_EXPENDITURE_TYPE
5449                         ,NULL                               -- TA_DISPLAY_FLAG
5450                         ,DECODE(l_targ_spread_curve_id_tbl(kk),
5451                                   l_spread_curve_id, l_targ_sp_fixed_date_tbl(kk),
5452                                   NULL)                     -- SP_FIXED_DATE -- Bug 8350296
5453                         ,l_targ_person_type_code_tbl(kk)    -- PERSON_TYPE_CODE
5454                         ,l_targ_RATE_BASED_FLAG_tbl(kk)     -- RATE_BASED_FLAG
5455                         ,l_targ_RES_RATE_BASED_FLAG_tbl(kk) -- RESOURCE_RATE_BASED_FLAG IPM Arch Enhancement
5456                         ,NULL                               -- USE_TASK_SCHEDULE_FLAG
5457                         ,l_targ_RATE_EXP_FC_CUR_COD_tbl(kk) -- RATE_EXP_FUNC_CURR_CODE
5458                         ,l_targ_RATE_EXPEND_ORG_ID_tbl(kk)  -- RATE_EXPENDITURE_ORG_ID
5459                         ,l_targ_INCR_BY_RES_CLS_COD_tbl(kk) -- INCUR_BY_RES_CLASS_CODE
5460                         ,l_targ_INCUR_BY_ROLE_ID_tbl(kk)    -- INCUR_BY_ROLE_ID
5461                         ,l_targ_project_role_id_tbl(kk)     -- PROJECT_ROLE_ID
5462                         ,l_targ_RESOURCE_CLASS_FLAG_tbl(kk) -- RESOURCE_CLASS_FLAG
5463                         ,l_targ_NAMED_ROLE_tbl(kk)          -- NAMED_ROLE
5464                         ,l_targ_txn_accum_header_id_tbl(kk) -- TXN ACCUM HEADER ID
5465                  FROM    dual
5466                  WHERE  L_ra_dml_code_tbl(kk)='INSERT';
5467 
5468 
5469             IF P_PA_debug_mode = 'Y' THEN
5470                   pa_debug.g_err_stage:= 'Done with bulk insert into PRA';
5471                   pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5472             END IF;
5473 
5474             --dbms_output.put_line('I19');
5475 
5476             --The pl/sql tbls prepared thru this DML will be used later while calling the PJI reporting API.
5477             --It is assumed that all the resource assignments that are updated are available in the below
5478             --pl/sql tbls. Hence changing the WHERE clause of this DML will have an impact on the way these
5479             --tbls are used . Bug 3678314
5480 
5481             -- Bug 3934574 The following business rules are incorporated in the update
5482             -- 1) If p_calling_context is null, null out transaction source code
5483             -- 2) If p_calling_context is generation
5484             --       a) if retain manually edited lines is Y do not update ras with transaction source code as null
5485             --       b) for ras that can be updated do not override transaction source code if there are already amounts
5486             --          if no amounts stamp transaction source code as 'CHANGE DOCUMENTS'
5487             -- Please note that the select against pa_budget_lines is unnecessary when p_calling_context is null
5488             -- Howeever, in oracle 8i select can not be used inside a decode. So, two sqls are used for better performance
5489             -- Bug 4171006: Updating UOM and rate_based_flag as well with the values already derived.
5490             IF p_calling_context IN ('BUDGET_GENERATION','FORECAST_GENERATION') THEN
5491                 FORALL kk IN L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
5492                 UPDATE pa_resource_assignments pra
5493                 SET    PLANNING_START_DATE         = l_planning_start_date_tbl(kk),
5494                        PLANNING_END_DATE           = l_planning_end_date_tbl(kk),
5495                        UNIT_OF_MEASURE             = l_targ_unit_of_measure_tbl(kk), -- bug 4171006
5496                        TRANSACTION_SOURCE_CODE     =
5497                           (SELECT DECODE(COUNT(*),0,'CHANGE_DOCUMENTS',TRANSACTION_SOURCE_CODE)
5498                            FROM pa_budget_lines pbl
5499                            WHERE  pbl.resource_assignment_id = pra.resource_assignment_id),
5500                        RATE_BASED_FLAG             = l_targ_RATE_BASED_FLAG_tbl(kk), -- bug 4171006
5501                        LAST_UPDATE_DATE            = sysdate,
5502                        LAST_UPDATED_BY             = fnd_global.user_id,
5503                        LAST_UPDATE_LOGIN           = fnd_global.login_id,
5504                        RECORD_VERSION_NUMBER       = NVL( RECORD_VERSION_NUMBER, 0 )  + 1
5505                 WHERE  l_ra_dml_code_tbl (kk)= 'UPDATE' -- Bug 3662136
5506                 AND    resource_assignment_id=l_targ_ra_id_tbl(kk)
5507                 RETURNING
5508                 task_id,
5509                 rbs_element_id,
5510 				cbs_element_id, --bug#16911079
5511                 resource_class_code,
5512                 rate_based_flag,
5513                 resource_assignment_id
5514                 BULK COLLECT INTO
5515                 l_upd_ra_task_id_tbl,
5516                 l_upd_ra_rbs_elem_id_tbl,
5517 				l_upd_ra_cbs_elem_id_tbl, --bug#16911079
5518                 l_upd_ra_res_class_code_tbl,
5519                 l_upd_ra_rbf_tbl,
5520                 l_upd_ra_res_asmt_id_tbl;
5521             ELSE
5522                 FORALL kk IN L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
5523                 UPDATE pa_resource_assignments pra
5524                 SET    PLANNING_START_DATE         = l_planning_start_date_tbl(kk),
5525                        PLANNING_END_DATE           = l_planning_end_date_tbl(kk),
5526                        UNIT_OF_MEASURE             = l_targ_unit_of_measure_tbl(kk), -- bug 4171006
5527                        TRANSACTION_SOURCE_CODE     = null,
5528                        RATE_BASED_FLAG             = l_targ_RATE_BASED_FLAG_tbl(kk), -- bug 4171006
5529                        LAST_UPDATE_DATE            = sysdate,
5530                        LAST_UPDATED_BY             = fnd_global.user_id,
5531                        LAST_UPDATE_LOGIN           = fnd_global.login_id,
5532                        RECORD_VERSION_NUMBER       = NVL( RECORD_VERSION_NUMBER, 0 )  + 1
5533                 WHERE  l_ra_dml_code_tbl (kk)= 'UPDATE' -- Bug 3662136
5534                 AND    resource_assignment_id=l_targ_ra_id_tbl(kk)
5535                 RETURNING
5536                 task_id,
5537                 rbs_element_id,
5538 				cbs_element_id,--bug#16911079
5539                 resource_class_code,
5540                 rate_based_flag,
5541                 resource_assignment_id
5542                 BULK COLLECT INTO
5543                 l_upd_ra_task_id_tbl,
5544                 l_upd_ra_rbs_elem_id_tbl,
5545 				l_upd_ra_cbs_elem_id_tbl,--bug#16911079
5546                 l_upd_ra_res_class_code_tbl,
5547                 l_upd_ra_rbf_tbl,
5548                 l_upd_ra_res_asmt_id_tbl;
5549             END IF;
5550             -- For bug 3814932
5551             --At this point in code, l_partial_factor can be 0 only if the user has chosen to implement 0 amount
5552             --into target. Please note that if the total amount in the change order itself is 0 parital factor
5553             --will be 1. It will be 0 only if the user did not chose to transfer amounts from source to target.
5554             --Hence budget lines need not be copied.
5555 
5556             IF l_partial_factor<>0 THEN
5557 
5558                 IF P_PA_debug_mode = 'Y' THEN
5559                     pa_debug.g_err_stage:= 'Done with bulk update of PRA';
5560                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5561                  END IF;
5562 
5563 
5564                 l_same_multi_curr_flag := 'N';
5565                 IF  l_src_multi_curr_flag  = l_targ_multi_curr_flag THEN
5566                   l_same_multi_curr_flag := 'Y';
5567                 END IF;
5568 
5569                 IF P_PA_debug_mode = 'Y' THEN
5570                     pa_debug.g_err_stage:= 'Done with deriving elem ver ids.';
5571                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5572                 END IF;
5573 
5574                 --dbms_output.put_line('I20');
5575 
5576 
5577                 --dbms_output.put_line('I22');
5578                 IF l_src_time_phased_code = 'N' AND (l_targ_time_phased_code = 'P' OR l_targ_time_phased_code = 'G') THEN
5579 
5580                    IF l_src_resource_list_id = l_targ_resource_list_id THEN
5581 
5582                       IF P_PA_debug_mode = 'Y' THEN
5583                           pa_debug.g_err_stage:= 'About to fire select for deriving params to calc API. Same Rls';
5584                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5585                       END IF;
5586 
5587 
5588                       SELECT get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id),pbls.resource_list_member_id),
5589                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,
5590                          DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
5591                       'N', --delete
5592                       'Y', --spread
5593                       decode(l_cost_impl_flag,'Y',pbls.quantity,decode(l_rev_impl_flag,'Y',
5594                                                               decode(l_impl_qty_tbl(j),'Y', nvl(pbls.quantity,0) * l_partial_factor,0),0)) + nvl(pblt.quantity,0), --total
5595                       Decode(l_cost_impl_flag ,'Y',DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.raw_cost,nvl(pbls.txn_raw_cost,0)),
5596                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0),
5597                                                    nvl(pbls.project_raw_cost,nvl(pbls.txn_raw_cost,0)))),0) +
5598                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pblt.raw_cost,nvl(pblt.txn_raw_cost,0)),
5599                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pblt.txn_raw_cost,0),
5600                                                    nvl(pblt.project_raw_cost,nvl(pblt.txn_raw_cost,0)))) , --total
5601                       Decode(l_cost_impl_flag ,'Y',DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.burdened_cost,nvl(pbls.txn_burdened_cost,0)),
5602                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
5603                                                    nvl(pbls.project_burdened_cost,nvl(pbls.txn_burdened_cost,0)))),0) +
5604                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pblt.burdened_cost,nvl(pblt.txn_burdened_cost,0)),
5605                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pblt.txn_burdened_cost,0),
5606                                                    nvl(pblt.project_burdened_cost,nvl(pblt.txn_burdened_cost,0)))), --total
5607                       Decode(l_rev_impl_flag ,'Y',DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,nvl(pbls.txn_revenue,0)),
5608                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
5609                                                         nvl(pbls.project_revenue,nvl(pbls.txn_revenue,0)))),0)*l_partial_factor  +
5610                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pblt.revenue,nvl(pblt.txn_revenue,0)),
5611                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pblt.txn_revenue,0),
5612                                                         nvl(pblt.project_revenue,nvl(pblt.txn_revenue,0)))), --total
5613                       NULL,
5614                       NULL,
5615                       NULL
5616                       BULK COLLECT INTO
5617                       l_res_assignment_id_tbl,
5618                       l_currency_code_tbl,
5619                       l_delete_budget_lines_tbl,
5620                       l_spread_amount_flags_tbl,
5621                       l_total_quantity_tbl,
5622                       l_total_raw_cost_tbl,
5623                       l_total_burdened_cost_tbl,
5624                       l_total_revenue_tbl,
5625                       l_bl_TXN_COST_RATE_OVERIDE_tbl,
5626                       l_bl_BURDEN_COST_RAT_OVRID_tbl,
5627                       l_bl_TXN_BILL_RATE_OVERRID_tbl
5628                       from  (SELECT pra.resource_assignment_id resource_assignment_id,
5629                                     pra.task_id task_id,
5630                                     pra.resource_list_member_id resource_list_member_id,
5631                                     sum(quantity) quantity,
5632                                     sum(pbl.txn_raw_cost) txn_raw_cost,
5633                                     sum(pbl.txn_burdened_cost) txn_burdened_cost,
5634                                     sum(pbl.txn_revenue) txn_revenue,
5635                                     sum(pbl.project_raw_cost) project_raw_cost,
5636                                     sum(pbl.project_burdened_cost) project_burdened_cost,
5637                                     sum(pbl.project_revenue) project_revenue,
5638                                     sum(pbl.raw_cost) raw_cost,
5639                                     sum(pbl.burdened_cost) burdened_cost,
5640                                     sum(pbl.revenue) revenue,
5641                                     DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,
5642                                            DECODE(l_same_multi_curr_flag,'Y', pbl.txn_currency_code,l_project_currency_code)) txn_currency_code
5643                              FROM   pa_budget_lines pbl,
5644                                     pa_resource_assignments pra
5645                              WHERE  pbl.resource_assignment_id = pra.resource_assignment_id
5646                              AND    pra.budget_version_id=l_src_ver_id_tbl(j)
5647                              GROUP BY pra.resource_assignment_id, pra.task_id, pra.resource_list_member_id,
5648                                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,
5649                                              DECODE(l_same_multi_curr_flag,'Y', pbl.txn_currency_code,l_project_currency_code))  ) pbls,
5650                             (SELECT pbl.resource_assignment_id,
5651                                     sum(quantity) quantity,
5652                                     sum(pbl.txn_raw_cost) txn_raw_cost,
5653                                     sum(pbl.txn_burdened_cost) txn_burdened_cost,
5654                                     sum(pbl.txn_revenue) txn_revenue,
5655                                     sum(pbl.project_raw_cost) project_raw_cost,
5656                                     sum(pbl.project_burdened_cost) project_burdened_cost,
5657                                     sum(pbl.project_revenue) project_revenue,
5658                                     sum(pbl.raw_cost) raw_cost,
5659                                     sum(pbl.burdened_cost) burdened_cost,
5660                                     sum(pbl.revenue) revenue,
5661                                     pbl.txn_currency_code
5662                              FROM   pa_budget_lines pbl
5663                              WHERE  pbl.budget_Version_id = p_budget_version_id
5664                              GROUP BY pbl.resource_assignment_id, pbl.txn_currency_code)pblt
5665                       where get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id),pbls.resource_list_member_id)=pblt.resource_assignment_id(+)
5666                       and   pblt.txn_Currency_code(+)= pbls.txn_currency_code;
5667                       IF P_PA_debug_mode = 'Y' THEN
5668                           pa_debug.g_err_stage:= 'Done with select for deriving params to calc API. Same Rls';
5669                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5670                       END IF;
5671 
5672 
5673                    ELSE
5674 
5675                       IF P_PA_debug_mode = 'Y' THEN
5676                           pa_debug.g_err_stage:= 'About to bulk collect BLs with diff RLs for calling calc API';
5677                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5678                       END IF;
5679 
5680                       select get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id),pbls.resource_list_member_id),
5681                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,
5682                          DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
5683                       'N', --delete
5684                       'Y', --spread
5685                       decode(l_cost_impl_flag,'Y',pbls.quantity,decode(l_rev_impl_flag,'Y',
5686                                                               decode(l_impl_qty_tbl(j),'Y', nvl(pbls.quantity,0) * l_partial_factor,0),0)) +
5687                                                                      nvl(pblt.quantity,0), --total
5688                       Decode(l_cost_impl_flag ,'Y',DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.raw_cost,nvl(pbls.txn_raw_cost,0)),
5689                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0),
5690                                                    nvl(pbls.project_raw_cost,nvl(pbls.txn_raw_cost,0)))),0) +
5691                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pblt.raw_cost,nvl(pblt.txn_raw_cost,0)),
5692                                                 DECODE(l_same_multi_curr_flag, 'Y', nvl(pblt.txn_raw_cost,0),
5693                                                        nvl(pblt.project_raw_cost,nvl(pblt.txn_raw_cost,0)))), --total
5694                       Decode(l_cost_impl_flag ,'Y',DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.burdened_cost,nvl(pbls.txn_burdened_cost,0)),
5695                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
5696                                                    nvl(pbls.project_burdened_cost,nvl(pbls.txn_burdened_cost,0)))),0) +
5697                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pblt.burdened_cost,nvl(pblt.txn_burdened_cost,0)),
5698                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pblt.txn_burdened_cost,0),
5699                                                    nvl(pblt.project_burdened_cost,nvl(pblt.txn_burdened_cost,0)))), --total
5700                       Decode(l_rev_impl_flag ,'Y',DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,nvl(pbls.txn_revenue,0)),
5701                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
5702                                                         nvl(pbls.project_revenue,nvl(pbls.txn_revenue,0)))),0)*l_partial_factor +
5703                       DECODE(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pblt.revenue,nvl(pblt.txn_revenue,0)),
5704                                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pblt.txn_revenue,0),
5705                                                         nvl(pblt.project_revenue,nvl(pblt.txn_revenue,0)))),  --total
5706                       NULL,
5707                       NULL,
5708                       NULL
5709                       BULK COLLECT INTO
5710                       l_res_assignment_id_tbl,
5711                       l_currency_code_tbl,
5712                       l_delete_budget_lines_tbl,
5713                       l_spread_amount_flags_tbl,
5714                       l_total_quantity_tbl,
5715                       l_total_raw_cost_tbl,
5716                       l_total_burdened_cost_tbl,
5717                       l_total_revenue_tbl,
5718                       l_bl_TXN_COST_RATE_OVERIDE_tbl,
5719                       l_bl_BURDEN_COST_RAT_OVRID_tbl,
5720                       l_bl_TXN_BILL_RATE_OVERRID_tbl
5721                       from (SELECT pra.task_id task_id,
5722                                    tmp.resource_list_member_id resource_list_member_id,
5723                                    pra.resource_assignment_id resource_assignment_id,
5724                                    sum(quantity) quantity,
5725                                    sum(pbl.txn_raw_cost) txn_raw_cost,
5726                                    sum(pbl.txn_burdened_cost) txn_burdened_cost,
5727                                    sum(pbl.txn_revenue) txn_revenue,
5728                                    sum(pbl.project_raw_cost) project_raw_cost,
5729                                    sum(pbl.project_burdened_cost) project_burdened_cost,
5730                                    sum(pbl.project_revenue) project_revenue,
5731                                    sum(pbl.raw_cost) raw_cost,
5732                                    sum(pbl.burdened_cost) burdened_cost,
5733                                    sum(pbl.revenue) revenue,
5734                                    DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,
5735                                           DECODE(l_same_multi_curr_flag,'Y', pbl.txn_currency_code,l_project_currency_code)) txn_currency_code
5736                             FROM   pa_resource_assignments pra
5737                                   ,pa_res_list_map_tmp4 tmp
5738                                   ,pa_budget_lines pbl
5739                             WHERE  pra.resource_assignment_id=tmp.txn_source_id
5740                             AND    pra.budget_version_id=l_src_ver_id_tbl(j)
5741                             AND    pbl.resource_assignment_id=pra.resource_assignment_id
5742                             GROUP BY pra.resource_assignment_id, pra.task_id, tmp.resource_list_member_id,
5743                                      DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,
5744                                           DECODE(l_same_multi_curr_flag,'Y', pbl.txn_currency_code,l_project_currency_code)) ) pbls,
5745                             (SELECT pbl.resource_assignment_id resource_assignment_id,
5746                                     sum(quantity) quantity,
5747                                     sum(pbl.txn_raw_cost) txn_raw_cost,
5748                                     sum(pbl.txn_burdened_cost) txn_burdened_cost,
5749                                     sum(pbl.txn_revenue) txn_revenue,
5750                                     sum(pbl.project_raw_cost) project_raw_cost,
5751                                     sum(pbl.project_burdened_cost) project_burdened_cost,
5752                                     sum(pbl.project_revenue) project_revenue,
5753                                     sum(pbl.raw_cost) raw_cost,
5754                                     sum(pbl.burdened_cost) burdened_cost,
5755                                     sum(pbl.revenue) revenue,
5756                                     pbl.txn_currency_code txn_currency_code
5757                              FROM   pa_budget_lines pbl
5758                              WHERE  pbl.budget_Version_id = p_budget_version_id
5759                              GROUP BY pbl.resource_assignment_id, pbl.txn_currency_code)pblt
5760                       where get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id),pbls.resource_list_member_id)=pblt.resource_assignment_id(+)
5761                       and   pblt.txn_currency_code(+)= pbls.txn_currency_code;
5762 
5763                       IF P_PA_debug_mode = 'Y' THEN
5764                           pa_debug.g_err_stage:= 'Done with bulk collect BLs with diff RLs for calling calc API';
5765                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5766                       END IF;
5767 
5768                    END IF;
5769 
5770                    --dbms_output.put_line('I23');
5771 
5772                    IF l_res_assignment_id_tbl.COUNT>0 THEN
5773 
5774                      --The below loop will calculate the override rates. These override rates are required because
5775                      --the amounts in the source should be added to the corresponding amounts in the target and there
5776                      --should not be re-derivation of amounts in the target based on the changed quantity in the target.
5777                      --For example, Consider the implementaion of a COST impact into a cost and revenue together
5778                      --version. In this case the quantity in the target will change but this in turn should not change
5779                      --the revenue amount in the target as only cost amounts should be impacted in the target.
5780 
5781                      FOR kk IN  l_res_assignment_id_tbl.FIRST..l_res_assignment_id_tbl.LAST LOOP
5782 
5783                         IF P_PA_debug_mode = 'Y' THEN
5784                               pa_debug.g_err_stage:= 'Before finding the RBF flag for  l_res_assignment_id_tbl('||kk||')'||l_res_assignment_id_tbl(kk);
5785                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5786                         END IF;
5787 
5788 
5789                         FOR ww IN l_targ_ra_id_tbl.FIRST..l_targ_ra_id_tbl.LAST LOOP
5790 
5791                             IF P_PA_debug_mode = 'Y' THEN
5792                                 pa_debug.g_err_stage:= 'l_targ_ra_id_tbl('||ww||')'||l_targ_ra_id_tbl(ww);
5793                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5794                             END IF;
5795 
5796                             IF l_targ_ra_id_tbl(ww)=l_res_assignment_id_tbl(kk) THEN
5797 
5798                               IF P_PA_debug_mode = 'Y' THEN
5799                                     pa_debug.g_err_stage:= 'Exiting with ww '||ww;
5800                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5801                               END IF;
5802 
5803                               l_matching_index := ww;
5804 
5805                               EXIT;
5806 
5807                             END IF;
5808 
5809                         END LOOP;
5810 
5811                         IF P_PA_debug_mode = 'Y' THEN
5812 
5813                               pa_debug.g_err_stage:= 'l_matching_index is '||l_matching_index;
5814                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5815 
5816                         END IF;
5817 
5818 
5819                         IF  l_targ_RATE_BASED_FLAG_tbl(l_matching_index)='Y' THEN
5820 
5821                             l_amt_used_for_rate_calc := l_total_quantity_tbl(kk);
5822 
5823                         ELSE
5824 
5825                             l_amt_used_for_rate_calc := l_total_raw_cost_tbl(kk);
5826 
5827                         END IF;
5828 
5829                         IF l_amt_used_for_rate_calc=0 THEN
5830 
5831                             l_bl_TXN_COST_RATE_OVERIDE_tbl(kk):=0;
5832                             l_bl_BURDEN_COST_RAT_OVRID_tbl(kk):=0;
5833                             l_bl_TXN_BILL_RATE_OVERRID_tbl(kk):=0;
5834 
5835                         ELSE
5836 
5837                             l_bl_TXN_COST_RATE_OVERIDE_tbl(kk):=l_total_raw_cost_tbl(kk)/l_amt_used_for_rate_calc;
5838                             l_bl_BURDEN_COST_RAT_OVRID_tbl(kk):=l_total_burdened_cost_tbl(kk)/l_amt_used_for_rate_calc;
5839                             l_bl_TXN_BILL_RATE_OVERRID_tbl(kk):=l_total_revenue_tbl(kk)/l_amt_used_for_rate_calc;
5840 
5841                         END IF;
5842 
5843 
5844                      END LOOP;
5845 
5846                      IF P_PA_debug_mode = 'Y' THEN
5847                           pa_debug.g_err_stage:= 'Calling Calc';
5848                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5849 
5850                           FOR kk IN l_res_assignment_id_tbl.FIRST..l_res_assignment_id_tbl.LAST LOOP
5851 
5852                               pa_debug.g_err_stage:= 'l_res_assignment_id_tbl('||KK||') IS'||l_res_assignment_id_tbl(kk);
5853                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5854 
5855                               pa_debug.g_err_stage:= 'l_delete_budget_lines_tbl('||KK||') IS'||l_delete_budget_lines_tbl(kk);
5856                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5857 
5858                               pa_debug.g_err_stage:= 'l_spread_amount_flags_tbl('||KK||') IS'||l_spread_amount_flags_tbl(kk);
5859                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5860 
5861                               pa_debug.g_err_stage:= 'l_currency_code_tbl('||KK||') IS'||l_currency_code_tbl(kk);
5862                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5863 
5864                               pa_debug.g_err_stage:= 'l_total_quantity_tbl('||KK||') IS'||l_total_quantity_tbl(kk);
5865                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5866 
5867                               pa_debug.g_err_stage:= 'l_total_raw_cost_tbl('||KK||') IS'||l_total_raw_cost_tbl(kk);
5868                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5869 
5870                               pa_debug.g_err_stage:= 'l_total_burdened_cost_tbl('||KK||') IS'||l_total_burdened_cost_tbl(kk);
5871                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5872 
5873                               pa_debug.g_err_stage:= 'l_total_revenue_tbl('||KK||') IS'||l_total_revenue_tbl(kk);
5874                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5875 
5876                               pa_debug.g_err_stage:= 'l_bl_TXN_COST_RATE_OVERIDE_tbl('||KK||') IS'||l_bl_TXN_COST_RATE_OVERIDE_tbl(kk);
5877                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5878 
5879                               pa_debug.g_err_stage:= 'l_bl_BURDEN_COST_RAT_OVRID_tbl('||KK||') IS'||l_bl_BURDEN_COST_RAT_OVRID_tbl(kk);
5880                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5881 
5882                               pa_debug.g_err_stage:= 'l_bl_TXN_BILL_RATE_OVERRID_tbl('||KK||') IS'||l_bl_TXN_BILL_RATE_OVERRID_tbl(kk);
5883                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5884 
5885                           END LOOP;
5886 
5887                      END IF;
5888 
5889                      PA_FP_CALC_PLAN_PKG.calculate(
5890                       p_project_id                 =>   l_project_id
5891                      ,p_budget_version_id          =>   p_budget_version_id
5892                      ,p_source_context             =>   PA_FP_CONSTANTS_PKG.G_CALC_API_RESOURCE_CONTEXT
5893                      ,p_resource_assignment_tab    =>   l_res_assignment_id_tbl
5894                      ,p_delete_budget_lines_tab    =>   l_delete_budget_lines_tbl
5895                      ,p_spread_amts_flag_tab       =>   l_spread_amount_flags_tbl
5896                      ,p_txn_currency_code_tab      =>   l_currency_code_tbl
5897                      ,p_total_qty_tab              =>   l_total_quantity_tbl
5898                      ,p_total_raw_cost_tab         =>   l_total_raw_cost_tbl -- dervie
5899                      ,p_total_burdened_cost_tab    =>   l_total_burdened_cost_tbl -- dervie
5900                      ,p_total_revenue_tab          =>   l_total_revenue_tbl -- derive
5901                      ,p_rw_cost_rate_override_tab  =>   l_bl_TXN_COST_RATE_OVERIDE_tbl
5902                      ,p_b_cost_rate_override_tab   =>   l_bl_BURDEN_COST_RAT_OVRID_tbl
5903                      ,p_bill_rate_override_tab     =>   l_bl_TXN_BILL_RATE_OVERRID_tbl
5904                      ,p_raTxn_rollup_api_call_flag =>   p_raTxn_rollup_api_call_flag --Indicates whether the pa_resource_asgn_curr maintenance api should be called
5905                      ,x_return_status              =>   l_return_status
5906                      ,x_msg_count                  =>   l_msg_count
5907                      ,x_msg_data                   =>   l_msg_data);
5908 
5909                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
5910                          IF P_PA_debug_mode = 'Y' THEN
5911                               pa_debug.g_err_stage:= 'Error in calculate';
5912                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
5913                          END IF;
5914                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5915                     END IF;
5916 
5917                     --dbms_output.put_line('I24');
5918 
5919                    END IF;--IF l_res_assignment_id_tbl.COUNT>0 THEN
5920 
5921                 END IF;--IF l_src_time_phased_code = 'N' AND (l_targ_time_phased_code = 'P' OR l_targ_time_phased_code = 'G') THEN
5922 
5923                 --Get the budget line sequence before inserting data into budget lines. After inserting the budget lines
5924                 --the sequence is again compared to see the no. of budget lines that have got inserted. Since pa_budget_lines_s.currval
5925                 --is used at a later part of code this SELECT should not be removed
5926                 SELECT pa_budget_lines_s.nextval
5927                 INTO   l_id_before_bl_insertion
5928                 FROM   DUAL;
5929 
5930                 --dbms_output.put_line('I25');
5931                 IF l_src_time_phased_code  = l_targ_time_phased_code OR l_targ_time_phased_code = 'N' THEN
5932                    IF l_targ_time_phased_code = 'N' THEN
5933                      IF l_src_resource_list_id = l_targ_resource_list_id THEN
5934 
5935                         IF P_PA_debug_mode = 'Y' THEN
5936                               pa_debug.g_err_stage:= 'About to bulk insert Budget lines with same RLs';
5937                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
5938                         END IF;
5939 
5940                         --dbms_output.put_line('I26');
5941                         FORALL kk in L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
5942                             INSERT INTO PA_BUDGET_LINES(
5943                                     RESOURCE_ASSIGNMENT_ID,
5944                                     START_DATE,
5945                                     LAST_UPDATE_DATE,
5946                                     LAST_UPDATED_BY,
5947                                     CREATION_DATE,
5948                                     CREATED_BY,
5949                                     LAST_UPDATE_LOGIN,
5950                                     END_DATE,
5951                                     PERIOD_NAME,
5952                                     QUANTITY,
5953                                     RAW_COST,
5954                                     BURDENED_COST,
5955                                     REVENUE,
5956                                     CHANGE_REASON_CODE,
5957                                     DESCRIPTION,
5958                                     ATTRIBUTE_CATEGORY,
5959                                     ATTRIBUTE1,
5960                                     ATTRIBUTE2,
5961                                     ATTRIBUTE3,
5962                                     ATTRIBUTE4,
5963                                     ATTRIBUTE5,
5964                                     ATTRIBUTE6,
5965                                     ATTRIBUTE7,
5966                                     ATTRIBUTE8,
5967                                     ATTRIBUTE9,
5968                                     ATTRIBUTE10,
5969                                     ATTRIBUTE11,
5970                                     ATTRIBUTE12,
5971                                     ATTRIBUTE13,
5972                                     ATTRIBUTE14,
5973                                     ATTRIBUTE15,
5974                                     RAW_COST_SOURCE,
5975                                     BURDENED_COST_SOURCE,
5976                                     QUANTITY_SOURCE,
5977                                     REVENUE_SOURCE,
5978                                     PM_PRODUCT_CODE,
5979                                     PM_BUDGET_LINE_REFERENCE,
5980                                     COST_REJECTION_CODE,
5981                                     REVENUE_REJECTION_CODE,
5982                                     BURDEN_REJECTION_CODE,
5983                                     OTHER_REJECTION_CODE,
5984                                     CODE_COMBINATION_ID,
5985                                     CCID_GEN_STATUS_CODE,
5986                                     CCID_GEN_REJ_MESSAGE,
5987                                     REQUEST_ID,
5988                                     BORROWED_REVENUE,
5989                                     TP_REVENUE_IN,
5990                                     TP_REVENUE_OUT,
5991                                     REVENUE_ADJ,
5992                                     LENT_RESOURCE_COST,
5993                                     TP_COST_IN,
5994                                     TP_COST_OUT,
5995                                     COST_ADJ,
5996                                     UNASSIGNED_TIME_COST,
5997                                     UTILIZATION_PERCENT,
5998                                     UTILIZATION_HOURS,
5999                                     UTILIZATION_ADJ,
6000                                     CAPACITY,
6001                                     HEAD_COUNT,
6002                                     HEAD_COUNT_ADJ,
6003                                     PROJFUNC_CURRENCY_CODE,
6004                                     PROJFUNC_COST_RATE_TYPE,
6005                                     PROJFUNC_COST_EXCHANGE_RATE,
6006                                     PROJFUNC_COST_RATE_DATE_TYPE,
6007                                     PROJFUNC_COST_RATE_DATE,
6008                                     PROJFUNC_REV_RATE_TYPE,
6009                                     PROJFUNC_REV_EXCHANGE_RATE,
6010                                     PROJFUNC_REV_RATE_DATE_TYPE,
6011                                     PROJFUNC_REV_RATE_DATE,
6012                                     PROJECT_CURRENCY_CODE,
6013                                     PROJECT_COST_RATE_TYPE,
6014                                     PROJECT_COST_EXCHANGE_RATE,
6015                                     PROJECT_COST_RATE_DATE_TYPE,
6016                                     PROJECT_COST_RATE_DATE,
6017                                     PROJECT_RAW_COST,
6018                                     PROJECT_BURDENED_COST,
6019                                     PROJECT_REV_RATE_TYPE,
6020                                     PROJECT_REV_EXCHANGE_RATE,
6021                                     PROJECT_REV_RATE_DATE_TYPE,
6022                                     PROJECT_REV_RATE_DATE,
6023                                     PROJECT_REVENUE,
6024                                     TXN_CURRENCY_CODE,
6025                                     TXN_RAW_COST,
6026                                     TXN_BURDENED_COST,
6027                                     TXN_REVENUE,
6028                                     BUCKETING_PERIOD_CODE,
6029                                     BUDGET_LINE_ID,
6030                                     BUDGET_VERSION_ID,
6031                                     TXN_STANDARD_COST_RATE,
6032                                     TXN_COST_RATE_OVERRIDE,
6033                                     COST_IND_COMPILED_SET_ID,
6034                           --            TXN_BURDEN_MULTIPLIER,
6035                           --            TXN_BURDEN_MULTIPLIER_OVERRIDE,
6036                                     TXN_STANDARD_BILL_RATE,
6037                                     TXN_BILL_RATE_OVERRIDE,
6038                                     TXN_MARKUP_PERCENT,
6039                                     TXN_MARKUP_PERCENT_OVERRIDE,
6040                                     TXN_DISCOUNT_PERCENTAGE,
6041                                     TRANSFER_PRICE_RATE,
6042                                     BURDEN_COST_RATE,
6043                                     BURDEN_COST_RATE_OVERRIDE,
6044                                     PC_CUR_CONV_REJECTION_CODE,
6045                                     PFC_CUR_CONV_REJECTION_CODE
6046                                     )
6047                               SELECT  pbl.resource_assignment_id,
6048                                     l_planning_start_date_tbl(kk) start_date,
6049                                     pbl.last_update_date,
6050                                     pbl.last_updated_by,
6051                                     pbl.creation_date,
6052                                     pbl.created_by,
6053                                     pbl.last_update_login,
6054                                     l_planning_end_date_tbl(kk) end_date,
6055                                     pbl.period_name,
6056                                     DECODE(l_targ_rate_based_flag_tbl(kk),
6057                                            'N',DECODE(l_target_version_type,
6058                                                       'REVENUE',pbl.txn_revenue
6059                                                                ,pbl.txn_raw_cost),
6060                                            pbl.quantity),
6061                                     pbl.raw_cost,
6062                                     pbl.burdened_cost,
6063                                     pbl.revenue,
6064                                     pbl.change_reason_code,
6065                                     pbl.description,
6066                                     pbl.attribute_category,
6067                                     pbl.attribute1,
6068                                     pbl.attribute2,
6069                                     pbl.attribute3,
6070                                     pbl.attribute4,
6071                                     pbl.attribute5,
6072                                     pbl.attribute6,
6073                                     pbl.attribute7,
6074                                     pbl.attribute8,
6075                                     pbl.attribute9,
6076                                     pbl.attribute10,
6077                                     pbl.attribute11,
6078                                     pbl.attribute12,
6079                                     pbl.attribute13,
6080                                     pbl.attribute14,
6081                                     pbl.attribute15,
6082                                     pbl.raw_cost_source,
6083                                     pbl.burdened_cost_source,
6084                                     pbl.quantity_source,
6085                                     pbl.revenue_source,
6086                                     pbl.pm_product_code,
6087                                     pbl.pm_budget_line_reference,
6088                                     pbl.cost_rejection_code,
6089                                     pbl.revenue_rejection_code,
6090                                     pbl.burden_rejection_code,
6091                                     pbl.other_rejection_code,
6092                                     pbl.code_combination_id,
6093                                     pbl.ccid_gen_status_code,
6094                                     pbl.ccid_gen_rej_message,
6095                                     pbl.request_id,
6096                                     pbl.borrowed_revenue,
6097                                     pbl.tp_revenue_in,
6098                                     pbl.tp_revenue_out,
6099                                     pbl.revenue_adj,
6100                                     pbl.lent_resource_cost,
6101                                     pbl.tp_cost_in,
6102                                     pbl.tp_cost_out,
6103                                     pbl.cost_adj,
6104                                     pbl.unassigned_time_cost,
6105                                     pbl.utilization_percent,
6106                                     pbl.utilization_hours,
6107                                     pbl.utilization_adj,
6108                                     pbl.capacity,
6109                                     pbl.head_count,
6110                                     pbl.head_count_adj,
6111                                     pbl.projfunc_currency_code,
6112                                     pbl.projfunc_cost_rate_type,
6113                                     DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y',
6114                                         Decode(decode(l_report_cost_using, 'R', nvl(pbl.txn_raw_cost,0),
6115                                                                   'B', nvl(pbl.txn_burdened_cost,0)),0,0,
6116                                             (decode(l_report_cost_using,'R',nvl(pbl.raw_cost,0),
6117                                                                   'B',nvl(pbl.burdened_cost,0)) /decode(l_report_cost_using,'R', pbl.txn_raw_cost,
6118                                                                                                             'B', pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
6119                                     pbl.projfunc_cost_rate_date_type,
6120                                     pbl.projfunc_cost_rate_date,
6121                                     pbl.projfunc_rev_rate_type,
6122                                     Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y',  Decode(nvl(pbl.txn_revenue,0),0,0,nvl(pbl.revenue,0) / pbl.txn_revenue),Null),Null), --Bug 3839273
6123                                     pbl.projfunc_rev_rate_date_type,
6124                                     pbl.projfunc_rev_rate_date,
6125                                     pbl.project_currency_code,
6126                                     pbl.project_cost_rate_type,
6127                                     DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
6128                                                                   'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.project_raw_cost,0),
6129                                                                          'B',nvl(pbl.project_burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
6130                                                                          'B',pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
6131                                     pbl.project_cost_rate_date_type,
6132                                     pbl.project_cost_rate_date,
6133                                     pbl.project_raw_cost,
6134                                     pbl.project_burdened_cost,
6135                                     pbl.project_rev_rate_type,
6136                                     Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', Decode(nvl(pbl.txn_revenue,0),0,0,nvl(pbl.project_revenue,0) /pbl.txn_revenue),Null),Null), --Bug 3839273
6137                                     pbl.project_rev_rate_date_type,
6138                                     pbl.project_rev_rate_date,
6139                                     pbl.project_revenue,
6140                                     pbl.txn_currency_code,
6141                                     pbl.txn_raw_cost,
6142                                     pbl.txn_burdened_cost,
6143                                     pbl.txn_revenue,
6144                                     pbl.bucketing_period_code,
6145                                     pa_budget_lines_s.nextval,
6146                                     pbl.budget_version_id,
6147                                     pbl.txn_standard_cost_rate,
6148                                     DECODE(l_target_version_type,
6149                                          'REVENUE',pbl.txn_cost_rate_override,
6150                                           DECODE(l_targ_rate_based_flag_tbl(kk),
6151                                                 'N',1,
6152                                                 pbl.txn_cost_rate_override)),
6153                                     pbl.cost_ind_compiled_set_id,
6154                           --          pbl.  txn_burden_multiplier,
6155                           --          pbl.  txn_burden_multiplier_override,
6156                                     pbl.txn_standard_bill_rate,
6157                                     DECODE(l_target_version_type,
6158                                            'REVENUE',DECODE(l_targ_rate_based_flag_tbl(kk),
6159                                                             'N',1,
6160                                                             pbl.txn_bill_rate_override)
6161                                            ,pbl.txn_bill_rate_override),
6162                                     pbl.txn_markup_percent,
6163                                     pbl.txn_markup_percent_override,
6164                                     pbl.txn_discount_percentage,
6165                                     pbl.transfer_price_rate,
6166                                     pbl.burden_cost_rate,
6167                                     DECODE(l_target_version_type,
6168                                          'REVENUE',pbl.burden_cost_rate_override,
6169                                           DECODE(l_targ_rate_based_flag_tbl(kk),
6170                                                 'Y',pbl.burden_cost_rate_override,
6171                                                  DECODE(nvl(pbl.txn_raw_cost,0),
6172                                                         0,null,
6173                                                         pbl.txn_burdened_cost/pbl.txn_raw_cost))),
6174                                     pbl.pc_cur_conv_rejection_code,
6175                                     pbl.pfc_cur_conv_rejection_code
6176                               FROM
6177                                  --The entire SELECT is moved to the sub query in FROM clause as nextval would not work with group by
6178                                 (SELECT get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pras.task_id), pras.resource_list_member_id) resource_assignment_id,
6179                                    sysdate last_update_date,
6180                                    fnd_global.user_id last_updated_by,
6181                                    sysdate creation_date,
6182                                    fnd_global.user_id created_by,
6183                                    fnd_global.login_id last_update_login,
6184                                    NULL period_name,
6185                                    decode(l_cost_impl_flag,
6186                                              'Y',sum(pbls.quantity),
6187                                              decode(l_rev_impl_flag,
6188                                                       'Y',decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),
6189                                                       null)) quantity,
6190                                    sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)) raw_cost,
6191                                    sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)) burdened_cost,
6192                                    sum(Decode(l_rev_impl_flag ,'Y',  pbls.revenue,null))*l_partial_factor revenue,
6193                                    decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null) change_reason_code,
6194                                    decode(count(pbls.budget_line_id),1,max(pbls.description),null) description,
6195                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null) attribute_category,
6196                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null) attribute1,
6197                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null) attribute2,
6198                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null) attribute3,
6199                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null) attribute4,
6200                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null) attribute5,
6201                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null) attribute6,
6202                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null) attribute7,
6203                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null) attribute8,
6204                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null) attribute9,
6205                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null) attribute10,
6206                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null) attribute11,
6207                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null) attribute12,
6208                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null) attribute13,
6209                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null) attribute14,
6210                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null) attribute15,
6211                                    'I' raw_cost_source,
6212                                    'I' burdened_cost_source,
6213                                    'I' quantity_source,
6214                                    'I' revenue_source,
6215                                    decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null) pm_product_code,
6216                                    decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null) pm_budget_line_reference,
6217                                    decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null) cost_rejection_code,
6218                                    decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null) revenue_rejection_code,
6219                                    decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null) burden_rejection_code,
6220                                    decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null)other_rejection_code,
6221                                    decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null) code_combination_id,
6222                                    decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null) ccid_gen_status_code,
6223                                    decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null) ccid_gen_rej_message,
6224                                    decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null) request_id,
6225                                    decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null) borrowed_revenue,
6226                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null) tp_revenue_in,
6227                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null) tp_revenue_out,
6228                                    decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null) revenue_adj,
6229                                    decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null) lent_resource_cost,
6230                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null) tp_cost_in,
6231                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null) tp_cost_out,
6232                                    decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null) cost_adj,
6233                                    decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null) unassigned_time_cost,
6234                                    decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null) utilization_percent,
6235                                    decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null) utilization_hours,
6236                                    decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null) utilization_adj,
6237                                    decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null) capacity,
6238                                    decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null) head_count,
6239                                    decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null) head_count_adj,
6240                                    l_projfunc_currency_code projfunc_currency_code,
6241                                    DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_cost_rate_type,
6242                                    null projfunc_cost_exchange_rate, --Bug 3839273
6243                                    null projfunc_cost_rate_date_type,
6244                                    null projfunc_cost_rate_date,
6245                                    Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_rev_rate_type,
6246                                    null projfunc_rev_exchange_rate, --Bug 3839273
6247                                    null projfunc_rev_rate_date_type,
6248                                    null projfunc_rev_rate_date,
6249                                    l_project_currency_code project_currency_code ,
6250                                    DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null) project_cost_rate_type,
6251                                    null project_cost_exchange_rate, --Bug 3839273
6252                                    null project_cost_rate_date_type,
6253                                    null project_cost_rate_date,
6254                                    sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost,null)) project_raw_cost,
6255                                    sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost,null)) project_burdened_cost,
6256                                    Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null) project_rev_rate_type,
6257                                    null project_rev_exchange_rate, --Bug 3839273
6258                                    null project_rev_rate_date_type,
6259                                    null project_rev_rate_date,
6260                                    sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor project_revenue,
6261                                    DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)) txn_currency_code,
6262 
6263                                    --Bug 4224757. Code changes for bug#4224757 starts here
6264                                    SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
6265                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0)))))
6266                                    txn_raw_cost,
6267                                    SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
6268                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
6269                                    nvl(pbls.project_burdened_cost,0))))) txn_burdened_cost,
6270                                    SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
6271                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
6272                                    nvl(pbls.project_revenue,0)))))*l_partial_factor txn_revenue,
6273                                    --Bug 4224757.. Code changes for bug#4224757 ends here
6274 
6275                                    null bucketing_period_code,
6276                                    p_budget_version_id  budget_version_id,
6277                                    decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null) txn_standard_cost_rate,
6278 
6279                                    --Bug 4224757. Code changes for bug#4224757 starts here
6280                                    decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
6281                                    decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
6282                                    nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
6283                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6284                                                                         decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))     txn_cost_rate_override,
6285                                    --Bug 4224757. Code changes for bug#4224757 ends here
6286 
6287 
6288 
6289 
6290                                    decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null) cost_ind_compiled_set_id,
6291                                --      decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
6292                                    decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null) txn_standard_bill_rate ,
6293 
6294                                    --Bug 4224757. Code changes for bug#4224757 starts here
6295                                    decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
6296                                           decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
6297                                           'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
6298                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6299                                                                         decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))txn_bill_rate_override,
6300                                    --Bug 4224757. Code changes for bug#4224757 ends here
6301 
6302                                    decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null)   txn_markup_percent,
6303                                    decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null) txn_markup_percent_override,
6304                                    decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null)   txn_discount_percentage,
6305                                    decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null) transfer_price_rate,
6306                                    decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null) burden_cost_rate,
6307 
6308                                    --Bug 4224757. Code changes for bug#4224757 starts here
6309                                    decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
6310                                    decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag,
6311                                    'Y', nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
6312                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6313                                                                         decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) burden_cost_rate_override,
6314                                    --Bug 4224757. Code changes for bug#4224757 ends here
6315 
6316 
6317 
6318                                    decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null) pc_cur_conv_rejection_code,
6319                                    decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null) pfc_cur_conv_rejection_code
6320                               from   pa_budget_lines pbls,
6321                                    pa_resource_assignments pras
6322                               where  l_ra_dml_code_tbl(kk)='INSERT'
6323                               and    pras.resource_assignment_id = pbls.resource_assignment_id
6324                               and    pras.budget_version_id = l_src_ver_id_tbl(j)
6325                               and    PA_FP_CI_MERGE.get_mapped_ra_id(PA_FP_CI_MERGE.get_task_id(l_targ_plan_level_code,pras.task_id), pras.resource_list_member_id)= L_targ_ra_id_tbl(kk)
6326                                --IPM Arch Enhancement Bug 4865563
6327                               /*and    pbls.cost_rejection_code IS NULL
6328                               and    pbls.revenue_rejection_code IS NULL
6329                               and    pbls.burden_rejection_code IS NULL
6330                               and    pbls.other_rejection_code IS NULL
6331                               and    pbls.pc_cur_conv_rejection_code IS NULL
6332                               and    pbls.pfc_cur_conv_rejection_code IS NULL*/
6333                               GROUP BY get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pras.task_id),pras.resource_list_member_id) ,
6334                                      DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code)))pbl;
6335 
6336                         IF P_PA_debug_mode = 'Y' THEN
6337                               pa_debug.g_err_stage:= 'After bulk-inserting resource assignments';
6338                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
6339                         END IF;
6340                         --dbms_output.put_line('I27');
6341 
6342                    ELSE-- Resource lists are different
6343 
6344                       IF P_PA_debug_mode = 'Y' THEN
6345                           pa_debug.g_err_stage:= 'About to bulk insert Budget lines with diff RLs and with targ TP as None';
6346                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
6347                       END IF;
6348 
6349                       --dbms_output.put_line('I28');
6350                       FORALL kk in L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
6351                       INSERT INTO PA_BUDGET_LINES(RESOURCE_ASSIGNMENT_ID,
6352                                START_DATE,
6353                                LAST_UPDATE_DATE,
6354                                LAST_UPDATED_BY,
6355                                CREATION_DATE,
6356                                CREATED_BY,
6357                                LAST_UPDATE_LOGIN,
6358                                END_DATE,
6359                                PERIOD_NAME,
6360                                QUANTITY,
6361                                RAW_COST,
6362                                BURDENED_COST,
6363                                REVENUE,
6364                                CHANGE_REASON_CODE,
6365                                DESCRIPTION,
6366                                ATTRIBUTE_CATEGORY,
6367                                ATTRIBUTE1,
6368                                ATTRIBUTE2,
6369                                ATTRIBUTE3,
6370                                ATTRIBUTE4,
6371                                ATTRIBUTE5,
6372                                ATTRIBUTE6,
6373                                ATTRIBUTE7,
6374                                ATTRIBUTE8,
6375                                ATTRIBUTE9,
6376                                ATTRIBUTE10,
6377                                ATTRIBUTE11,
6378                                ATTRIBUTE12,
6379                                ATTRIBUTE13,
6380                                ATTRIBUTE14,
6381                                ATTRIBUTE15,
6382                                RAW_COST_SOURCE,
6383                                BURDENED_COST_SOURCE,
6384                                QUANTITY_SOURCE,
6385                                REVENUE_SOURCE,
6386                                PM_PRODUCT_CODE,
6387                                PM_BUDGET_LINE_REFERENCE,
6388                                COST_REJECTION_CODE,
6389                                REVENUE_REJECTION_CODE,
6390                                BURDEN_REJECTION_CODE,
6391                                OTHER_REJECTION_CODE,
6392                                CODE_COMBINATION_ID,
6393                                CCID_GEN_STATUS_CODE,
6394                                CCID_GEN_REJ_MESSAGE,
6395                                REQUEST_ID,
6396                                BORROWED_REVENUE,
6397                                TP_REVENUE_IN,
6398                                TP_REVENUE_OUT,
6399                                REVENUE_ADJ,
6400                                LENT_RESOURCE_COST,
6401                                TP_COST_IN,
6402                                TP_COST_OUT,
6403                                COST_ADJ,
6404                                UNASSIGNED_TIME_COST,
6405                                UTILIZATION_PERCENT,
6406                                UTILIZATION_HOURS,
6407                                UTILIZATION_ADJ,
6408                                CAPACITY,
6409                                HEAD_COUNT,
6410                                HEAD_COUNT_ADJ,
6411                                PROJFUNC_CURRENCY_CODE,
6412                                PROJFUNC_COST_RATE_TYPE,
6413                                PROJFUNC_COST_EXCHANGE_RATE,
6414                                PROJFUNC_COST_RATE_DATE_TYPE,
6415                                PROJFUNC_COST_RATE_DATE,
6416                                PROJFUNC_REV_RATE_TYPE,
6417                                PROJFUNC_REV_EXCHANGE_RATE,
6418                                PROJFUNC_REV_RATE_DATE_TYPE,
6419                                PROJFUNC_REV_RATE_DATE,
6420                                PROJECT_CURRENCY_CODE,
6421                                PROJECT_COST_RATE_TYPE,
6422                                PROJECT_COST_EXCHANGE_RATE,
6423                                PROJECT_COST_RATE_DATE_TYPE,
6424                                PROJECT_COST_RATE_DATE,
6425                                PROJECT_RAW_COST,
6426                                PROJECT_BURDENED_COST,
6427                                PROJECT_REV_RATE_TYPE,
6428                                PROJECT_REV_EXCHANGE_RATE,
6429                                PROJECT_REV_RATE_DATE_TYPE,
6430                                PROJECT_REV_RATE_DATE,
6431                                PROJECT_REVENUE,
6432                                TXN_CURRENCY_CODE,
6433                                TXN_RAW_COST,
6434                                TXN_BURDENED_COST,
6435                                TXN_REVENUE,
6436                                BUCKETING_PERIOD_CODE,
6437                                BUDGET_LINE_ID,
6438                                BUDGET_VERSION_ID,
6439                                TXN_STANDARD_COST_RATE,
6440                                TXN_COST_RATE_OVERRIDE,
6441                                COST_IND_COMPILED_SET_ID,
6442                         --           TXN_BURDEN_MULTIPLIER,
6443                         --           TXN_BURDEN_MULTIPLIER_OVERRIDE,
6444                                TXN_STANDARD_BILL_RATE,
6445                                TXN_BILL_RATE_OVERRIDE,
6446                                TXN_MARKUP_PERCENT,
6447                                TXN_MARKUP_PERCENT_OVERRIDE,
6448                                TXN_DISCOUNT_PERCENTAGE,
6449                                TRANSFER_PRICE_RATE,
6450                                BURDEN_COST_RATE,
6451                                BURDEN_COST_RATE_OVERRIDE,
6452                                PC_CUR_CONV_REJECTION_CODE,
6453                                PFC_CUR_CONV_REJECTION_CODE
6454                                )
6455                         SELECT  pbl.resource_assignment_id,
6456                               l_planning_start_date_tbl(kk) start_date,
6457                               pbl.last_update_date,
6458                               pbl.last_updated_by,
6459                               pbl.creation_date,
6460                               pbl.created_by,
6461                               pbl.last_update_login,
6462                               l_planning_end_date_tbl(kk) end_date,
6463                               pbl.period_name,
6464                               DECODE(l_targ_rate_based_flag_tbl(kk),
6465                                      'N',DECODE(l_target_version_type,
6466                                                 'REVENUE',pbl.txn_revenue
6467                                                          ,pbl.txn_raw_cost),
6468                                       pbl.quantity),
6469                               pbl.raw_cost,
6470                               pbl.burdened_cost,
6471                               pbl.revenue,
6472                               pbl.change_reason_code,
6473                               pbl.description,
6474                               pbl.attribute_category,
6475                               pbl.attribute1,
6476                               pbl.attribute2,
6477                               pbl.attribute3,
6478                               pbl.attribute4,
6479                               pbl.attribute5,
6480                               pbl.attribute6,
6481                               pbl.attribute7,
6482                               pbl.attribute8,
6483                               pbl.attribute9,
6484                               pbl.attribute10,
6485                               pbl.attribute11,
6486                               pbl.attribute12,
6487                               pbl.attribute13,
6488                               pbl.attribute14,
6489                               pbl.attribute15,
6490                               pbl.raw_cost_source,
6491                               pbl.burdened_cost_source,
6492                               pbl.quantity_source,
6493                               pbl.revenue_source,
6494                               pbl.pm_product_code,
6495                               pbl.pm_budget_line_reference,
6496                               pbl.cost_rejection_code,
6497                               pbl.revenue_rejection_code,
6498                               pbl.burden_rejection_code,
6499                               pbl.other_rejection_code,
6500                               pbl.code_combination_id,
6501                               pbl.ccid_gen_status_code,
6502                               pbl.ccid_gen_rej_message,
6503                               pbl.request_id,
6504                               pbl.borrowed_revenue,
6505                               pbl.tp_revenue_in,
6506                               pbl.tp_revenue_out,
6507                               pbl.revenue_adj,
6508                               pbl.lent_resource_cost,
6509                               pbl.tp_cost_in,
6510                               pbl.tp_cost_out,
6511                               pbl.cost_adj,
6512                               pbl.unassigned_time_cost,
6513                               pbl.utilization_percent,
6514                               pbl.utilization_hours,
6515                               pbl.utilization_adj,
6516                               pbl.capacity,
6517                               pbl.head_count,
6518                               pbl.head_count_adj,
6519                               pbl.projfunc_currency_code,
6520                               pbl.projfunc_cost_rate_type,
6521                               DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
6522                                               'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.raw_cost,0),
6523                                               'B',nvl(pbl.burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
6524                                               'B', pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
6525                               pbl.projfunc_cost_rate_date_type,
6526                               pbl.projfunc_cost_rate_date,
6527                               pbl.projfunc_rev_rate_type,
6528                               Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y',  Decode(nvl(pbl.txn_revenue,0),0,0,nvl(pbl.revenue,0) /pbl.txn_revenue),Null),Null), --Bug 3839273
6529                               pbl.projfunc_rev_rate_date_type,
6530                               pbl.projfunc_rev_rate_date,
6531                               pbl.project_currency_code,
6532                               pbl.project_cost_rate_type,
6533                               DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
6534                                                'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.project_raw_cost,0),
6535                                                'B',nvl(pbl.project_burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
6536                                                'B',pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
6537                               pbl.project_cost_rate_date_type,
6538                               pbl.project_cost_rate_date,
6539                               pbl.project_raw_cost,
6540                               pbl.project_burdened_cost,
6541                               pbl.project_rev_rate_type,
6542                               Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', Decode(nvl(pbl.txn_revenue,0),0,0,(nvl(pbl.project_revenue,0) /pbl.txn_revenue)),Null),Null), --Bug 3839273
6543                               pbl.project_rev_rate_date_type,
6544                               pbl.project_rev_rate_date,
6545                               pbl.project_revenue,
6546                               pbl.txn_currency_code,
6547                               pbl.txn_raw_cost,
6548                               pbl.txn_burdened_cost,
6549                               pbl.txn_revenue,
6550                               pbl.bucketing_period_code,
6551                               pa_budget_lines_s.nextval,
6552                               pbl.budget_version_id,
6553                               pbl.txn_standard_cost_rate,
6554                               DECODE(l_target_version_type,
6555                                    'REVENUE',pbl.txn_cost_rate_override,
6556                                     DECODE(l_targ_rate_based_flag_tbl(kk),
6557                                            'N',1,
6558                                            pbl.txn_cost_rate_override)),
6559                               pbl.cost_ind_compiled_set_id,
6560                     --          pbl.  txn_burden_multiplier,
6561                     --          pbl.  txn_burden_multiplier_override,
6562                               pbl.txn_standard_bill_rate,
6563                               DECODE(l_target_version_type,
6564                                      'REVENUE',DECODE(l_targ_rate_based_flag_tbl(kk),
6565                                                       'N',1,
6566                                                       pbl.txn_bill_rate_override),
6567                                      pbl.txn_bill_rate_override),
6568                               pbl.txn_markup_percent,
6569                               pbl.txn_markup_percent_override,
6570                               pbl.txn_discount_percentage,
6571                               pbl.transfer_price_rate,
6572                               pbl.burden_cost_rate,
6573                               DECODE(l_target_version_type,
6574                                    'REVENUE',pbl.burden_cost_rate_override,
6575                                     DECODE(l_targ_rate_based_flag_tbl(kk),
6576                                           'Y',pbl.burden_cost_rate_override,
6577                                           DECODE(nvl(pbl.txn_raw_cost,0),
6578                                                  0,null,
6579                                                  pbl.txn_burdened_cost/pbl.txn_raw_cost))),
6580                               pbl.pc_cur_conv_rejection_code,
6581                               pbl.pfc_cur_conv_rejection_code
6582                         FROM
6583                                  --The entier SELECT is moved to the sub query in FROM clause as nextval would not work with group by
6584                          (SELECT pa_fp_ci_merge.get_mapped_ra_id(pa_fp_ci_merge.get_task_id(l_targ_plan_level_code,pras.task_id), rlmap.resource_list_member_id) resource_assignment_id,
6585                               sysdate    last_update_date,
6586                               fnd_global.user_id  last_updated_by,
6587                               sysdate  creation_date,
6588                               fnd_global.user_id  created_by,
6589                               fnd_global.login_id last_update_login,
6590                               NULL period_name,
6591                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6592                                                                    decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)) quantity,
6593                               sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)) raw_cost,
6594                               sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)) burdened_cost,
6595                               sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue,null))*l_partial_factor revenue,
6596                               decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null) change_reason_code,
6597                               decode(count(pbls.budget_line_id),1,max(pbls.description),null) description,
6598                               decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null) attribute_category,
6599                               decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null)   attribute1 ,
6600                               decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null)   attribute2 ,
6601                               decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null)   attribute3 ,
6602                               decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null)   attribute4 ,
6603                               decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null)   attribute5 ,
6604                               decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null)   attribute6 ,
6605                               decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null)   attribute7 ,
6606                               decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null)   attribute8 ,
6607                               decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null)   attribute9 ,
6608                               decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null)attribute10 ,
6609                               decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null)attribute11 ,
6610                               decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null)attribute12 ,
6611                               decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null)attribute13 ,
6612                               decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null)attribute14 ,
6613                               decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null)attribute15 ,
6614                               'I' raw_cost_source     ,
6615                               'I' burdened_cost_source,
6616                               'I' quantity_source     ,
6617                               'I' revenue_source      ,
6618                               decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null) pm_product_code,
6619                               decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null) pm_budget_line_reference,
6620                               decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null) cost_rejection_code,
6621                               decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null) revenue_rejection_code,
6622                               decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null) burden_rejection_code,
6623                               decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null) other_rejection_code,
6624                               decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null) code_combination_id,
6625                               decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null) ccid_gen_status_code,
6626                               decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null) ccid_gen_rej_message,
6627                               decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null) request_id,
6628                               decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null) borrowed_revenue,
6629                               decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null) tp_revenue_in,
6630                               decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null) tp_revenue_out,
6631                               decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null)revenue_adj,
6632                               decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null) lent_resource_cost,
6633                               decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null) tp_cost_in,
6634                               decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null) tp_cost_out,
6635                               decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null) cost_adj,
6636                               decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null) unassigned_time_cost,
6637                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null) utilization_percent,
6638                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null) utilization_hours,
6639                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null) utilization_adj,
6640                               decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null) capacity,
6641                               decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null) head_count,
6642                               decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null) head_count_adj,
6643                               l_projfunc_currency_code projfunc_currency_code,
6644                               DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_cost_rate_type,
6645                               null projfunc_cost_exchange_rate, --Bug 3839273
6646                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE_TYPE),null) projfunc_cost_rate_date_type,
6647                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE),null) projfunc_cost_rate_date,
6648                               Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_rev_rate_type,
6649                               null projfunc_rev_exchange_rate, --Bug 3839273
6650                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE_TYPE),null) projfunc_rev_rate_date_type,
6651                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE),null) projfunc_rev_rate_date,
6652                               l_project_currency_code project_currency_code,
6653                               DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null) project_cost_rate_type,
6654                               null project_cost_exchange_rate, --Bug 3839273
6655                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE_TYPE),null) project_cost_rate_date_type,
6656                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE),null) project_cost_rate_date,
6657                               sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost,null)) project_raw_cost,
6658                               sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost,null)) project_burdened_cost,
6659                               Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null) project_rev_rate_type,
6660                               null project_rev_exchange_rate, --Bug 3839273
6661                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE_TYPE),null) project_rev_rate_date_type,
6662                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE),null) project_rev_rate_date,
6663                               sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor project_revenue,
6664                               DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)) txn_currency_code,
6665 
6666                               --Bug 4224757.. Code changes for bug#4224757 starts here
6667                               SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
6668                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0)))))
6669                               txn_raw_cost,
6670                               SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
6671                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
6672                               nvl(pbls.project_burdened_cost,0))))) txn_burdened_cost,
6673                               SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
6674                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
6675                               nvl(pbls.project_revenue,0)))))*l_partial_factor txn_revenue,
6676                               --Bug 4224757.. Code changes for bug#4224757 ends here
6677 
6678                               decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null) bucketing_period_code,
6679                               p_budget_version_id budget_version_id,
6680                               decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null) txn_standard_cost_rate,
6681 
6682                               --Bug 4224757. Code changes for bug#4224757 starts here
6683                               decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
6684                               decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
6685                                nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
6686                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6687                                                                   decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))     txn_cost_rate_override,
6688                               --Bug 4224757. Code changes for bug#4224757 ends here
6689 
6690 
6691 
6692                               decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null) cost_ind_compiled_set_id,
6693                         --        decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
6694 
6695                               decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null) txn_standard_bill_rate,
6696 
6697                               --Bug 4224757. Code changes for bug#4224757 starts here
6698                               decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
6699                                    decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
6700                                      'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
6701                                      decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6702                                                                    decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) txn_bill_rate_override,
6703                               --Bug 4224757. Code changes for bug#4224757 ends here
6704 
6705                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null) txn_markup_percent,
6706                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null) txn_markup_percent_override,
6707                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null) txn_discount_percentage,
6708                               decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null) transfer_price_rate,
6709                               decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null) burden_cost_rate,
6710 
6711                               --Bug 4224757. Code changes for bug#4224757 starts here
6712                               decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
6713                               decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
6714                                 nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
6715                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6716                                                                decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) burden_cost_rate_override,
6717                               --Bug 4224757. Code changes for bug#4224757 ends here
6718 
6719 
6720 
6721                               decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null) pc_cur_conv_rejection_code,
6722                               decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null) pfc_cur_conv_rejection_code
6723                          from   pa_budget_lines pbls,
6724                               pa_resource_assignments pras,
6725                               pa_res_list_map_tmp4  rlmap
6726                          where  l_ra_dml_code_tbl(kk)='INSERT'
6727                          and    pras.resource_assignment_id = pbls.resource_assignment_id
6728                          and    pras.budget_version_id = l_src_ver_id_tbl(j)
6729                          AND    pras.resource_assignment_id=rlmap.txn_source_id
6730                          and    pa_fp_ci_merge.get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pras.task_id), rlmap.resource_list_member_id)=l_targ_ra_id_tbl(kk)
6731                             --IPM Arch Enhancement Bug 4865563
6732                        /*and    pbls.cost_rejection_code IS NULL
6733                          and    pbls.revenue_rejection_code IS NULL
6734                          and    pbls.burden_rejection_code IS NULL
6735                          and    pbls.other_rejection_code IS NULL
6736                          and    pbls.pc_cur_conv_rejection_code IS NULL
6737                          and    pbls.pfc_cur_conv_rejection_code IS NULL*/
6738                          GROUP BY pa_fp_ci_merge.get_mapped_ra_id(pa_fp_ci_merge.get_task_id(l_targ_plan_level_code,pras.task_id),rlmap.resource_list_member_id) ,
6739                                 DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code)))pbl;
6740 
6741                         IF P_PA_debug_mode = 'Y' THEN
6742                               pa_debug.g_err_stage:= 'Done with bulk insert Budget lines with diff RLs and with targ TP as None';
6743                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
6744                         END IF;
6745                         --dbms_output.put_line('I29');
6746                    END IF;
6747                 ELSE -- Time phased code is not N and src time phasing = target time phasing
6748 
6749                    IF P_PA_debug_mode = 'Y' THEN
6750                         pa_debug.g_err_stage:= 'SRC tp =targ TP. same RLS. About to bulk insert BLs';
6751                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
6752                    END IF;
6753 
6754                    --dbms_output.put_line('I30');
6755                    IF l_src_resource_list_id = l_targ_resource_list_id THEN
6756                       FORALL kk in L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
6757                          INSERT INTO PA_BUDGET_LINES(
6758                                     RESOURCE_ASSIGNMENT_ID,
6759                                     START_DATE,
6760                                     LAST_UPDATE_DATE,
6761                                     LAST_UPDATED_BY,
6762                                     CREATION_DATE,
6763                                     CREATED_BY,
6764                                     LAST_UPDATE_LOGIN,
6765                                     END_DATE,
6766                                     PERIOD_NAME,
6767                                     QUANTITY,
6768                                     RAW_COST,
6769                                     BURDENED_COST,
6770                                     REVENUE,
6771                                     CHANGE_REASON_CODE,
6772                                     DESCRIPTION,
6773                                     ATTRIBUTE_CATEGORY,
6774                                     ATTRIBUTE1,
6775                                     ATTRIBUTE2,
6776                                     ATTRIBUTE3,
6777                                     ATTRIBUTE4,
6778                                     ATTRIBUTE5,
6779                                     ATTRIBUTE6,
6780                                     ATTRIBUTE7,
6781                                     ATTRIBUTE8,
6782                                     ATTRIBUTE9,
6783                                     ATTRIBUTE10,
6784                                     ATTRIBUTE11,
6785                                     ATTRIBUTE12,
6786                                     ATTRIBUTE13,
6787                                     ATTRIBUTE14,
6788                                     ATTRIBUTE15,
6789                                     RAW_COST_SOURCE,
6790                                     BURDENED_COST_SOURCE,
6791                                     QUANTITY_SOURCE,
6792                                     REVENUE_SOURCE,
6793                                     PM_PRODUCT_CODE,
6794                                     PM_BUDGET_LINE_REFERENCE,
6795                                     COST_REJECTION_CODE,
6796                                     REVENUE_REJECTION_CODE,
6797                                     BURDEN_REJECTION_CODE,
6798                                     OTHER_REJECTION_CODE,
6799                                     CODE_COMBINATION_ID,
6800                                     CCID_GEN_STATUS_CODE,
6801                                     CCID_GEN_REJ_MESSAGE,
6802                                     REQUEST_ID,
6803                                     BORROWED_REVENUE,
6804                                     TP_REVENUE_IN,
6805                                     TP_REVENUE_OUT,
6806                                     REVENUE_ADJ,
6807                                     LENT_RESOURCE_COST,
6808                                     TP_COST_IN,
6809                                     TP_COST_OUT,
6810                                     COST_ADJ,
6811                                     UNASSIGNED_TIME_COST,
6812                                     UTILIZATION_PERCENT,
6813                                     UTILIZATION_HOURS,
6814                                     UTILIZATION_ADJ,
6815                                     CAPACITY,
6816                                     HEAD_COUNT,
6817                                     HEAD_COUNT_ADJ,
6818                                     PROJFUNC_CURRENCY_CODE,
6819                                     PROJFUNC_COST_RATE_TYPE,
6820                                     PROJFUNC_COST_EXCHANGE_RATE,
6821                                     PROJFUNC_COST_RATE_DATE_TYPE,
6822                                     PROJFUNC_COST_RATE_DATE,
6823                                     PROJFUNC_REV_RATE_TYPE,
6824                                     PROJFUNC_REV_EXCHANGE_RATE,
6825                                     PROJFUNC_REV_RATE_DATE_TYPE,
6826                                     PROJFUNC_REV_RATE_DATE,
6827                                     PROJECT_CURRENCY_CODE,
6828                                     PROJECT_COST_RATE_TYPE,
6829                                     PROJECT_COST_EXCHANGE_RATE,
6830                                     PROJECT_COST_RATE_DATE_TYPE,
6831                                     PROJECT_COST_RATE_DATE,
6832                                     PROJECT_RAW_COST,
6833                                     PROJECT_BURDENED_COST,
6834                                     PROJECT_REV_RATE_TYPE,
6835                                     PROJECT_REV_EXCHANGE_RATE,
6836                                     PROJECT_REV_RATE_DATE_TYPE,
6837                                     PROJECT_REV_RATE_DATE,
6838                                     PROJECT_REVENUE,
6839                                     TXN_CURRENCY_CODE,
6840                                     TXN_RAW_COST,
6841                                     TXN_BURDENED_COST,
6842                                     TXN_REVENUE,
6843                                     BUCKETING_PERIOD_CODE,
6844                                     BUDGET_LINE_ID,
6845                                     BUDGET_VERSION_ID,
6846                                     TXN_STANDARD_COST_RATE,
6847                                     TXN_COST_RATE_OVERRIDE,
6848                                     COST_IND_COMPILED_SET_ID,
6849                              --         TXN_BURDEN_MULTIPLIER,
6850                              --         TXN_BURDEN_MULTIPLIER_OVERRIDE,
6851                                     TXN_STANDARD_BILL_RATE,
6852                                     TXN_BILL_RATE_OVERRIDE,
6853                                     TXN_MARKUP_PERCENT,
6854                                     TXN_MARKUP_PERCENT_OVERRIDE,
6855                                     TXN_DISCOUNT_PERCENTAGE,
6856                                     TRANSFER_PRICE_RATE,
6857                                     BURDEN_COST_RATE,
6858                                     BURDEN_COST_RATE_OVERRIDE,
6859                                     PC_CUR_CONV_REJECTION_CODE,
6860                                     PFC_CUR_CONV_REJECTION_CODE
6861                                     )
6862                          SELECT       pbl.resource_assignment_id,
6863                                     pbl.start_date,
6864                                     pbl.last_update_date,
6865                                     pbl.last_updated_by,
6866                                     pbl.creation_date,
6867                                     pbl.created_by,
6868                                     pbl.last_update_login,
6869                                     pbl.end_date,
6870                                     pbl.period_name,
6871                                     DECODE(l_targ_rate_based_flag_tbl(kk),
6872                                            'N',DECODE(l_target_version_type,
6873                                                       'REVENUE',pbl.txn_revenue
6874                                                                ,pbl.txn_raw_cost),
6875                                            pbl.quantity),
6876                                     pbl.raw_cost,
6877                                     pbl.burdened_cost,
6878                                     pbl.revenue,
6879                                     pbl.change_reason_code,
6880                                     pbl.description,
6881                                     pbl.attribute_category,
6882                                     pbl.attribute1,
6883                                     pbl.attribute2,
6884                                     pbl.attribute3,
6885                                     pbl.attribute4,
6886                                     pbl.attribute5,
6887                                     pbl.attribute6,
6888                                     pbl.attribute7,
6889                                     pbl.attribute8,
6890                                     pbl.attribute9,
6891                                     pbl.attribute10,
6892                                     pbl.attribute11,
6893                                     pbl.attribute12,
6894                                     pbl.attribute13,
6895                                     pbl.attribute14,
6896                                     pbl.attribute15,
6897                                     pbl.raw_cost_source,
6898                                     pbl.burdened_cost_source,
6899                                     pbl.quantity_source,
6900                                     pbl.revenue_source,
6901                                     pbl.pm_product_code,
6902                                     pbl.pm_budget_line_reference,
6903                                     pbl.cost_rejection_code,
6904                                     pbl.revenue_rejection_code,
6905                                     pbl.burden_rejection_code,
6906                                     pbl.other_rejection_code,
6907                                     pbl.code_combination_id,
6908                                     pbl.ccid_gen_status_code,
6909                                     pbl.ccid_gen_rej_message,
6910                                     pbl.request_id,
6911                                     pbl.borrowed_revenue,
6912                                     pbl.tp_revenue_in,
6913                                     pbl.tp_revenue_out,
6914                                     pbl.revenue_adj,
6915                                     pbl.lent_resource_cost,
6916                                     pbl.tp_cost_in,
6917                                     pbl.tp_cost_out,
6918                                     pbl.cost_adj,
6919                                     pbl.unassigned_time_cost,
6920                                     pbl.utilization_percent,
6921                                     pbl.utilization_hours,
6922                                     pbl.utilization_adj,
6923                                     pbl.capacity,
6924                                     pbl.head_count,
6925                                     pbl.head_count_adj,
6926                                     pbl.projfunc_currency_code,
6927                                     pbl.projfunc_cost_rate_type,
6928                                     DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
6929                                                      'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.raw_cost,0),
6930                                                      'B',nvl(pbl.burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
6931                                                      'B', pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
6932                                     pbl.projfunc_cost_rate_date_type,
6933                                     pbl.projfunc_cost_rate_date,
6934                                     pbl.projfunc_rev_rate_type,
6935                                     Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y',  Decode(nvl(pbl.txn_revenue,0),0,0,(nvl(pbl.revenue,0) /pbl.txn_revenue)),Null),Null), --Bug 3839273
6936                                     pbl.projfunc_rev_rate_date_type,
6937                                     pbl.projfunc_rev_rate_date,
6938                                     pbl.project_currency_code,
6939                                     pbl.project_cost_rate_type,
6940                                     DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
6941                                                                    'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.project_raw_cost,0),
6942                                                                    'B',nvl(pbl.project_burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
6943                                                                    'B',pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
6944                                     pbl.project_cost_rate_date_type,
6945                                     pbl.project_cost_rate_date,
6946                                     pbl.project_raw_cost,
6947                                     pbl.project_burdened_cost,
6948                                     pbl.project_rev_rate_type,
6949                                     Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', Decode(nvl(pbl.txn_revenue,0),0,0,(nvl(pbl.project_revenue,0) /pbl.txn_revenue)),Null),Null), --Bug 3839273
6950                                     pbl.project_rev_rate_date_type,
6951                                     pbl.project_rev_rate_date,
6952                                     pbl.project_revenue,
6953                                     pbl.txn_currency_code,
6954                                     pbl.txn_raw_cost,
6955                                     pbl.txn_burdened_cost,
6956                                     pbl.txn_revenue,
6957                                     pbl.bucketing_period_code,
6958                                     pa_budget_lines_s.nextval,
6959                                     pbl.budget_version_id,
6960                                     pbl.txn_standard_cost_rate,
6961                                     DECODE(l_target_version_type,
6962                                          'REVENUE',pbl.txn_cost_rate_override,
6963                                           DECODE(l_targ_rate_based_flag_tbl(kk),
6964                                                  'N',1,
6965                                                  pbl.txn_cost_rate_override)),
6966                                     pbl.cost_ind_compiled_set_id,
6967                              --       pbl.  txn_burden_multiplier,
6968                              --       pbl.  txn_burden_multiplier_override,
6969                                     pbl.txn_standard_bill_rate,
6970                                     DECODE(l_target_version_type,
6971                                            'REVENUE',DECODE(l_targ_rate_based_flag_tbl(kk),
6972                                                             'N',1,
6973                                                             pbl.txn_bill_rate_override),
6974                                            pbl.txn_bill_rate_override),
6975                                     pbl.txn_markup_percent,
6976                                     pbl.txn_markup_percent_override,
6977                                     pbl.txn_discount_percentage,
6978                                     pbl.transfer_price_rate,
6979                                     pbl.burden_cost_rate,
6980                                     DECODE(l_target_version_type,
6981                                          'REVENUE',pbl.burden_cost_rate_override,
6982                                           DECODE(l_targ_rate_based_flag_tbl(kk),
6983                                                  'Y',pbl.burden_cost_rate_override,
6984                                                   DECODE(nvl(pbl.txn_raw_cost,0),
6985                                                          0,null,
6986                                                          pbl.txn_burdened_cost/pbl.txn_raw_cost))),
6987                                     pbl.pc_cur_conv_rejection_code,
6988                                     pbl.pfc_cur_conv_rejection_code
6989                          FROM(SELECT get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pras.task_id), pras.resource_list_member_id) resource_assignment_id,
6990                                    pbls.start_date start_date,
6991                                    sysdate last_update_date,
6992                                    fnd_global.user_id last_updated_by,
6993                                    sysdate creation_date,
6994                                    fnd_global.user_id created_by,
6995                                    fnd_global.login_id last_update_login,
6996                                    pbls.end_date end_date,
6997                                    pbls.period_name period_name,
6998                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
6999                                                                       decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)) quantity,
7000                                    sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)) raw_cost,
7001                                    sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)) burdened_cost,
7002                                    sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue, null))*l_partial_factor revenue,
7003                                    decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null) change_reason_code,
7004                                    decode(count(pbls.budget_line_id),1,max(pbls.description),null) description,
7005                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null) attribute_category,
7006                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null) attribute1,
7007                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null) attribute2,
7008                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null) attribute3,
7009                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null) attribute4,
7010                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null) attribute5,
7011                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null) attribute6,
7012                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null) attribute7,
7013                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null) attribute8,
7014                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null) attribute9,
7015                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null) attribute10,
7016                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null) attribute11,
7017                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null) attribute12,
7018                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null) attribute13,
7019                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null) attribute14,
7020                                    decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null) attribute15,
7021                                    'I' raw_cost_source     ,
7022                                    'I' burdened_cost_source,
7023                                    'I' quantity_source     ,
7024                                    'I' revenue_source      ,
7025                                    decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null) pm_product_code,
7026                                    decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null) pm_budget_line_reference,
7027                                    decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null) cost_rejection_code,
7028                                    decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null) revenue_rejection_code,
7029                                    decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null) burden_rejection_code,
7030                                    decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null) other_rejection_code,
7031                                    decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null) code_combination_id,
7032                                    decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null) ccid_gen_status_code,
7033                                    decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null) ccid_gen_rej_message,
7034                                    decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null) request_id,
7035                                    decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null) borrowed_revenue,
7036                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null) tp_revenue_in,
7037                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null) tp_revenue_out,
7038                                    decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null) revenue_adj,
7039                                    decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null) lent_resource_cost,
7040                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null) tp_cost_in,
7041                                    decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null)  tp_cost_out,
7042                                    decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null) cost_adj,
7043                                    decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null) unassigned_time_cost,
7044                                    decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null) utilization_percent,
7045                                    decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null) utilization_hours,
7046                                    decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null) utilization_adj,
7047                                    decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null) capacity,
7048                                    decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null) head_count,
7049                                    decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null) head_count_adj,
7050                                    l_projfunc_currency_code projfunc_currency_code,
7051                                    DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_cost_rate_type,
7052                                    null projfunc_cost_exchange_rate, --Bug 3839273
7053                                    null projfunc_cost_rate_date_type,
7054                                    null projfunc_cost_rate_date,
7055                                    Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_rev_rate_type,
7056                                    null projfunc_rev_exchange_rate, --Bug 3839273
7057                                    null projfunc_rev_rate_date_type,
7058                                    null projfunc_rev_rate_date,
7059                                    l_project_currency_code project_currency_code,
7060                                    DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null) project_cost_rate_type,
7061                                    null project_cost_exchange_rate, --Bug 3839273
7062                                    null project_cost_rate_date_type,
7063                                    null project_cost_rate_date,
7064                                    sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost,null)) project_raw_cost,
7065                                    sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost,null)) project_burdened_cost,
7066                                    Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null) project_rev_rate_type,
7067                                    null project_rev_exchange_rate, --Bug 3839273
7068                                    null project_rev_rate_date_type,
7069                                    null project_rev_rate_date,
7070                                    sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor project_revenue,
7071                                    DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)) txn_currency_code,
7072 
7073 
7074                                    --Bug 4224757.. Code changes for bug#4224757 starts here
7075                                    SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
7076                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0)))))
7077                                    txn_raw_cost,
7078                                    SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
7079                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
7080                                    nvl(pbls.project_burdened_cost,0))))) txn_burdened_cost,
7081                                    SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
7082                                    DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
7083                                    nvl(pbls.project_revenue,0)))))*l_partial_factor txn_revenue,
7084                                    --Bug 4224757.. Code changes for bug#4224757 ends here
7085 
7086                                    decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null) bucketing_period_code,
7087                                    p_budget_version_id budget_version_id,
7088                                    decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null) txn_standard_cost_rate,
7089 
7090                                    --Bug 4224757. Code changes for bug#4224757 starts here
7091                                    decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7092                                    decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7093                                   nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
7094                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7095                                                                         decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))     txn_cost_rate_override,
7096                                    --Bug 4224757. Code changes for bug#4224757 ends here
7097 
7098 
7099 
7100                                    decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null) cost_ind_compiled_set_id,
7101                             --         decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
7102 
7103                                    decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null) txn_standard_bill_rate,
7104 
7105                                    --Bug 4224757. Code changes for bug#4224757 starts here
7106                                    decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
7107                                           decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
7108                                           'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
7109                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7110                                                                         decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) txn_bill_rate_override,
7111                                    --Bug 4224757. Code changes for bug#4224757 ends here
7112 
7113                                    decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null) txn_markup_percent,
7114                                    decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null) txn_markup_percent_override,
7115                                    decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null) txn_discount_percentage,
7116                                    decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null) transfer_price_rate,
7117                                    decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null) burden_cost_rate,
7118 
7119                                    --Bug 4224757. Code changes for bug#4224757 starts here
7120                                    decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7121                                    decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag,
7122                                   'Y', nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
7123                                    decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7124                                                                         decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) burden_cost_rate_override,
7125                                    --Bug 4224757. Code changes for bug#4224757 ends here
7126 
7127 
7128                                    decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null) pc_cur_conv_rejection_code,
7129                                    decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null) pfc_cur_conv_rejection_code
7130                               from   pa_budget_lines pbls,
7131                                    pa_resource_assignments pras
7132                               where  l_ra_dml_code_tbl(kk)='INSERT'
7133                               and    pras.budget_version_id = l_src_ver_id_tbl(j)
7134                               and    pras.resource_assignment_id = pbls.resource_assignment_id
7135                               and    get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pras.task_id), pras.resource_list_member_id)=L_targ_ra_id_tbl(kk)
7136                                  --IPM Arch Enhancement Bug 4865563
7137                             /*and    pbls.cost_rejection_code IS NULL
7138                               and    pbls.revenue_rejection_code IS NULL
7139                               and    pbls.burden_rejection_code IS NULL
7140                               and    pbls.other_rejection_code IS NULL
7141                               and    pbls.pc_cur_conv_rejection_code IS NULL
7142                               and    pbls.pfc_cur_conv_rejection_code IS NULL*/
7143                               and    pbls.start_date >= nvl(l_etc_start_date,pbls.start_date)
7144                               GROUP BY get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pras.task_id),pras.resource_list_member_id) ,
7145                                      DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code))
7146                                      ,pbls.start_date,pbls.end_date,pbls.period_name)pbl;
7147 
7148                                IF P_PA_debug_mode = 'Y' THEN
7149                                     pa_debug.g_err_stage:= 'SRC tp =targ TP. same RLS.Done with bulk insert BLs';
7150                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7151                                END IF;
7152                                --dbms_output.put_line('I31');
7153 
7154                    ELSE
7155                       IF P_PA_debug_mode = 'Y' THEN
7156                           pa_debug.g_err_stage:= 'About to bulk insert Budget lines with different RLs and  TP not N and src Tp= targ TP';
7157                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7158 
7159                       END IF;
7160 
7161                       --dbms_output.put_line('I32');
7162                       FORALL kk in L_targ_ra_id_tbl.FIRST ..L_targ_ra_id_tbl.LAST
7163                       INSERT INTO PA_BUDGET_LINES(
7164                                RESOURCE_ASSIGNMENT_ID,
7165                                START_DATE,
7166                                LAST_UPDATE_DATE,
7167                                LAST_UPDATED_BY,
7168                                CREATION_DATE,
7169                                CREATED_BY,
7170                                LAST_UPDATE_LOGIN,
7171                                END_DATE,
7172                                PERIOD_NAME,
7173                                QUANTITY,
7174                                RAW_COST,
7175                                BURDENED_COST,
7176                                REVENUE,
7177                                CHANGE_REASON_CODE,
7178                                DESCRIPTION,
7179                                ATTRIBUTE_CATEGORY,
7180                                ATTRIBUTE1,
7181                                ATTRIBUTE2,
7182                                ATTRIBUTE3,
7183                                ATTRIBUTE4,
7184                                ATTRIBUTE5,
7185                                ATTRIBUTE6,
7186                                ATTRIBUTE7,
7187                                ATTRIBUTE8,
7188                                ATTRIBUTE9,
7189                                ATTRIBUTE10,
7190                                ATTRIBUTE11,
7191                                ATTRIBUTE12,
7192                                ATTRIBUTE13,
7193                                ATTRIBUTE14,
7194                                ATTRIBUTE15,
7195                                RAW_COST_SOURCE,
7196                                BURDENED_COST_SOURCE,
7197                                QUANTITY_SOURCE,
7198                                REVENUE_SOURCE,
7199                                PM_PRODUCT_CODE,
7200                                PM_BUDGET_LINE_REFERENCE,
7201                                COST_REJECTION_CODE,
7202                                REVENUE_REJECTION_CODE,
7203                                BURDEN_REJECTION_CODE,
7204                                OTHER_REJECTION_CODE,
7205                                CODE_COMBINATION_ID,
7206                                CCID_GEN_STATUS_CODE,
7207                                CCID_GEN_REJ_MESSAGE,
7208                                REQUEST_ID,
7209                                BORROWED_REVENUE,
7210                                TP_REVENUE_IN,
7211                                TP_REVENUE_OUT,
7212                                REVENUE_ADJ,
7213                                LENT_RESOURCE_COST,
7214                                TP_COST_IN,
7215                                TP_COST_OUT,
7216                                COST_ADJ,
7217                                UNASSIGNED_TIME_COST,
7218                                UTILIZATION_PERCENT,
7219                                UTILIZATION_HOURS,
7220                                UTILIZATION_ADJ,
7221                                CAPACITY,
7222                                HEAD_COUNT,
7223                                HEAD_COUNT_ADJ,
7224                                PROJFUNC_CURRENCY_CODE,
7225                                PROJFUNC_COST_RATE_TYPE,
7226                                PROJFUNC_COST_EXCHANGE_RATE,
7227                                PROJFUNC_COST_RATE_DATE_TYPE,
7228                                PROJFUNC_COST_RATE_DATE,
7229                                PROJFUNC_REV_RATE_TYPE,
7230                                PROJFUNC_REV_EXCHANGE_RATE,
7231                                PROJFUNC_REV_RATE_DATE_TYPE,
7232                                PROJFUNC_REV_RATE_DATE,
7233                                PROJECT_CURRENCY_CODE,
7234                                PROJECT_COST_RATE_TYPE,
7235                                PROJECT_COST_EXCHANGE_RATE,
7236                                PROJECT_COST_RATE_DATE_TYPE,
7237                                PROJECT_COST_RATE_DATE,
7238                                PROJECT_RAW_COST,
7239                                PROJECT_BURDENED_COST,
7240                                PROJECT_REV_RATE_TYPE,
7241                                PROJECT_REV_EXCHANGE_RATE,
7242                                PROJECT_REV_RATE_DATE_TYPE,
7243                                PROJECT_REV_RATE_DATE,
7244                                PROJECT_REVENUE,
7245                                TXN_CURRENCY_CODE,
7246                                TXN_RAW_COST,
7247                                TXN_BURDENED_COST,
7248                                TXN_REVENUE,
7249                                BUCKETING_PERIOD_CODE,
7250                                BUDGET_LINE_ID,
7251                                BUDGET_VERSION_ID,
7252                                TXN_STANDARD_COST_RATE,
7253                                TXN_COST_RATE_OVERRIDE,
7254                                COST_IND_COMPILED_SET_ID,
7255                          --        TXN_BURDEN_MULTIPLIER,
7256                          --        TXN_BURDEN_MULTIPLIER_OVERRIDE,
7257                                TXN_STANDARD_BILL_RATE,
7258                                TXN_BILL_RATE_OVERRIDE,
7259                                TXN_MARKUP_PERCENT,
7260                                TXN_MARKUP_PERCENT_OVERRIDE,
7261                                TXN_DISCOUNT_PERCENTAGE,
7262                                TRANSFER_PRICE_RATE,
7263                                BURDEN_COST_RATE,
7264                                BURDEN_COST_RATE_OVERRIDE,
7265                                PC_CUR_CONV_REJECTION_CODE,
7266                                PFC_CUR_CONV_REJECTION_CODE
7267                                )
7268                          SELECT    pbl.resource_assignment_id,
7269                                pbl.start_date,
7270                                pbl.last_update_date,
7271                                pbl.last_updated_by,
7272                                pbl.creation_date,
7273                                pbl.created_by,
7274                                pbl.last_update_login,
7275                                pbl.end_date,
7276                                pbl.period_name,
7277                                DECODE(l_targ_rate_based_flag_tbl(kk),
7278                                       'N',DECODE(l_target_version_type,
7279                                                  'REVENUE',pbl.txn_revenue
7280                                                           ,pbl.txn_raw_cost),
7281                                        pbl.quantity),
7282                                pbl.raw_cost,
7283                                pbl.burdened_cost,
7284                                pbl.revenue,
7285                                pbl.change_reason_code,
7286                                pbl.description,
7287                                pbl.attribute_category,
7288                                pbl.attribute1,
7289                                pbl.attribute2,
7290                                pbl.attribute3,
7291                                pbl.attribute4,
7292                                pbl.attribute5,
7293                                pbl.attribute6,
7294                                pbl.attribute7,
7295                                pbl.attribute8,
7296                                pbl.attribute9,
7297                                pbl.attribute10,
7298                                pbl.attribute11,
7299                                pbl.attribute12,
7300                                pbl.attribute13,
7301                                pbl.attribute14,
7302                                pbl.attribute15,
7303                                pbl.raw_cost_source,
7304                                pbl.burdened_cost_source,
7305                                pbl.quantity_source,
7306                                pbl.revenue_source,
7307                                pbl.pm_product_code,
7308                                pbl.pm_budget_line_reference,
7309                                pbl.cost_rejection_code,
7310                                pbl.revenue_rejection_code,
7311                                pbl.burden_rejection_code,
7312                                pbl.other_rejection_code,
7313                                pbl.code_combination_id,
7314                                pbl.ccid_gen_status_code,
7315                                pbl.ccid_gen_rej_message,
7316                                pbl.request_id,
7317                                pbl.borrowed_revenue,
7318                                pbl.tp_revenue_in,
7319                                pbl.tp_revenue_out,
7320                                pbl.revenue_adj,
7321                                pbl.lent_resource_cost,
7322                                pbl.tp_cost_in,
7323                                pbl.tp_cost_out,
7324                                pbl.cost_adj,
7325                                pbl.unassigned_time_cost,
7326                                pbl.utilization_percent,
7327                                pbl.utilization_hours,
7328                                pbl.utilization_adj,
7329                                pbl.capacity,
7330                                pbl.head_count,
7331                                pbl.head_count_adj,
7332                                pbl.projfunc_currency_code,
7333                                pbl.projfunc_cost_rate_type,
7334                                DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
7335                                                  'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.raw_cost,0),
7336                                                  'B',nvl(pbl.burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
7337                                                  'B', pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
7338                                pbl.projfunc_cost_rate_date_type,
7339                                pbl.projfunc_cost_rate_date,
7340                                pbl.projfunc_rev_rate_type,
7341                                Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y',  Decode(nvl(pbl.txn_revenue,0),0,0,(nvl(pbl.revenue,0) /pbl.txn_revenue)),Null),Null), --Bug 3839273
7342                                pbl.projfunc_rev_rate_date_type,
7343                                pbl.projfunc_rev_rate_date,
7344                                pbl.project_currency_code,
7345                                pbl.project_cost_rate_type,
7346                                DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(pbl.txn_raw_cost,0),
7347                                                  'B', nvl(pbl.txn_burdened_cost,0)),0,0,(decode(l_report_cost_using,'R',nvl(pbl.project_raw_cost,0),
7348                                                  'B',nvl(pbl.project_burdened_cost,0)) / decode(l_report_cost_using,'R',pbl.txn_raw_cost,
7349                                                  'B',pbl.txn_burdened_cost))),Null),Null), --Bug 3839273
7350                                pbl.project_cost_rate_date_type,
7351                                pbl.project_cost_rate_date,
7352                                pbl.project_raw_cost,
7353                                pbl.project_burdened_cost,
7354                                pbl.project_rev_rate_type,
7355                                Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', Decode(nvl(pbl.txn_revenue,0),0,0,(nvl(pbl.project_revenue,0) /pbl.txn_revenue)),Null),Null), --Bug 3839273
7356                                pbl.project_rev_rate_date_type,
7357                                pbl.project_rev_rate_date,
7358                                pbl.project_revenue,
7359                                pbl.txn_currency_code,
7360                                pbl.txn_raw_cost,
7361                                pbl.txn_burdened_cost,
7362                                pbl.txn_revenue,
7363                                pbl.bucketing_period_code,
7364                                pa_budget_lines_s.nextval,
7365                                pbl.budget_version_id,
7366                                pbl.txn_standard_cost_rate,
7367                                DECODE(l_target_version_type,
7368                                       'REVENUE',pbl.txn_cost_rate_override,
7369                                       DECODE(l_targ_rate_based_flag_tbl(kk),
7370                                              'N',1,
7371                                              pbl.txn_cost_rate_override)),
7372                                pbl.cost_ind_compiled_set_id,
7373                          --      pbl.  txn_burden_multiplier,
7374                          --      pbl.  txn_burden_multiplier_override,
7375                                pbl.txn_standard_bill_rate,
7376                                DECODE(l_target_version_type,
7377                                       'REVENUE',DECODE(l_targ_rate_based_flag_tbl(kk),
7378                                                        'N',1,
7379                                                        pbl.txn_bill_rate_override),
7380                                       pbl.txn_bill_rate_override),
7381                                pbl.txn_markup_percent,
7382                                pbl.txn_markup_percent_override,
7383                                pbl.txn_discount_percentage,
7384                                pbl.transfer_price_rate,
7385                                pbl.burden_cost_rate,
7386                                DECODE(l_target_version_type,
7387                                       'REVENUE',pbl.burden_cost_rate_override,
7388                                        DECODE(l_targ_rate_based_flag_tbl(kk),
7389                                               'Y',pbl.burden_cost_rate_override,
7390                                               DECODE(nvl(pbl.txn_raw_cost,0),
7391                                                      0,null,
7392                                                      pbl.txn_burdened_cost/pbl.txn_raw_cost))),
7393                                pbl.pc_cur_conv_rejection_code,
7394                                pbl.pfc_cur_conv_rejection_code
7395                          FROM(SELECT get_mapped_ra_id(get_task_id(l_targ_plan_level_code,rlmap.task_id), rlmap.resource_list_member_id) resource_assignment_id,
7396                               pbls.start_date start_date,
7397                               sysdate last_update_date,
7398                               fnd_global.user_id last_updated_by,
7399                               sysdate creation_date,
7400                               fnd_global.user_id created_by,
7401                               fnd_global.login_id last_update_login,
7402                               pbls.end_date end_date,
7403                               pbls.period_name period_name,
7404                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7405                                                                    decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)) quantity,
7406                               sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)) raw_cost,
7407                               sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)) burdened_cost,
7408                               sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue, null))*l_partial_factor revenue,
7409                               decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null) change_reason_code,
7410                               decode(count(pbls.budget_line_id),1,max(pbls.description),null) description,
7411                               decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null) attribute_category,
7412                               decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null) attribute1,
7413                               decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null) attribute2,
7414                               decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null) attribute3,
7415                               decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null) attribute4,
7416                               decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null) attribute5,
7417                               decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null) attribute6,
7418                               decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null) attribute7,
7419                               decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null) attribute8,
7420                               decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null) attribute9,
7421                               decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null) attribute10,
7422                               decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null) attribute11,
7423                               decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null) attribute12,
7424                               decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null) attribute13,
7425                               decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null) attribute14,
7426                               decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null) attribute15,
7427                               'I' raw_cost_source     ,
7428                               'I' burdened_cost_source,
7429                               'I' quantity_source     ,
7430                               'I' revenue_source      ,
7431                               decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null) pm_product_code,
7432                               decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null) pm_budget_line_reference,
7433                               decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null) cost_rejection_code,
7434                               decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null) revenue_rejection_code,
7435                               decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null) burden_rejection_code,
7436                               decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null) other_rejection_code,
7437                               decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null) code_combination_id,
7438                               decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null) ccid_gen_status_code,
7439                               decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null) ccid_gen_rej_message,
7440                               decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null) request_id,
7441                               decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null) borrowed_revenue,
7442                               decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null) tp_revenue_in,
7443                               decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null) tp_revenue_out,
7444                               decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null) revenue_adj,
7445                               decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null) lent_resource_cost,
7446                               decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null) tp_cost_in,
7447                               decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null) tp_cost_out,
7448                               decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null) cost_adj,
7449                               decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null) unassigned_time_cost,
7450                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null) utilization_percent,
7451                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null) utilization_hours,
7452                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null) utilization_adj,
7453                               decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null) capacity,
7454                               decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null) head_count,
7455                               decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null) head_count_adj,
7456                               l_projfunc_currency_code projfunc_currency_code,
7457                               DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_cost_rate_type,
7458                               null projfunc_cost_exchange_rate, --Bug 3839273
7459                               null projfunc_cost_rate_date_type,
7460                               null projfunc_cost_rate_date,
7461                               Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null) projfunc_rev_rate_type,
7462                               null projfunc_rev_exchange_rate, --Bug 3839273
7463                               null projfunc_rev_rate_date_type,
7464                               null projfunc_rev_rate_date,
7465                               l_project_currency_code project_currency_code,
7466                               DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null) project_cost_rate_type,
7467                               null project_cost_exchange_rate, --Bug 3839273
7468                               null project_cost_rate_date_type,
7469                               null project_cost_rate_date,
7470                               sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost, null)) project_raw_cost,
7471                               sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost, null)) project_burdened_cost,
7472                               Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null) project_rev_rate_type,
7473                               null project_rev_exchange_rate, --Bug 3839273
7474                               null project_rev_rate_date_type ,
7475                               null project_rev_rate_date ,
7476                               sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor project_revenue,
7477                               DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)) txn_currency_code,
7478 
7479 
7480                               --Bug 4224757.. Code changes for bug#4224757 starts here
7481                               SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
7482                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0)))))
7483                               txn_raw_cost,
7484                               SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
7485                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
7486                               nvl(pbls.project_burdened_cost,0))))) txn_burdened_cost,
7487                               SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
7488                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
7489                               nvl(pbls.project_revenue,0)))))*l_partial_factor txn_revenue,
7490                               --Bug 4224757.. Code changes for bug#4224757 ends here
7491 
7492                               decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null) bucketing_period_code,
7493                               p_budget_version_id budget_version_id,
7494                               decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null) txn_standard_cost_rate,
7495 
7496                               --Bug 4224757. Code changes for bug#4224757 starts here
7497                               decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7498                               decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7499                                nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
7500                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7501                                                               decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))     txn_cost_rate_override,
7502                               --Bug 4224757. Code changes for bug#4224757 ends here
7503 
7504                               decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null) cost_ind_compiled_set_id,
7505                       --            decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
7506 
7507                               decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null) txn_standard_bill_rate,
7508 
7509                               --Bug 4224757. Code changes for bug#4224757 starts here
7510                               decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
7511                                   decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
7512                                     'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
7513                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7514                                                                    decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) txn_bill_rate_override,
7515                               --Bug 4224757. Code changes for bug#4224757 ends here
7516 
7517                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null) txn_markup_percent,
7518                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null) txn_markup_percent_override,
7519                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null) txn_discount_percentage,
7520                               decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null) transfer_price_rate,
7521                               decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null) burden_cost_rate,
7522 
7523                               --Bug 4224757. Code changes for bug#4224757 starts here
7524                               decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7525                               decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7526                                 nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
7527                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7528                                                                 decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) burden_cost_rate_override,
7529                               --Bug 4224757. Code changes for bug#4224757 ends here
7530 
7531                               decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null) pc_cur_conv_rejection_code,
7532                               decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null)  pfc_cur_conv_rejection_code
7533                          from   pa_budget_lines pbls,
7534                               (SELECT pra.task_id task_id,
7535                                     tmp4.resource_list_member_id resource_list_member_id,
7536                                     tmp4.txn_source_id resource_assignment_id
7537                                FROM   pa_resource_assignments pra,
7538                                     pa_res_list_map_tmp4 tmp4
7539                                WHERE  tmp4.txn_source_id=pra.resource_assignment_id) rlmap
7540                          where  l_ra_dml_code_tbl(kk)='INSERT'
7541                          and    rlmap.resource_assignment_id = pbls.resource_assignment_id
7542                          and    get_mapped_ra_id(get_task_id(l_targ_plan_level_code,rlmap.task_id), rlmap.resource_list_member_id)=L_targ_ra_id_tbl(kk)
7543                          --IPM Arch Enhancement Bug 4865563
7544                        /*and    pbls.cost_rejection_code IS NULL
7545                          and    pbls.revenue_rejection_code IS NULL
7546                          and    pbls.burden_rejection_code IS NULL
7547                          and    pbls.other_rejection_code IS NULL
7548                          and    pbls.pc_cur_conv_rejection_code IS NULL
7549                          and    pbls.pfc_cur_conv_rejection_code IS NULL*/
7550                          and    pbls.start_date >= nvl(l_etc_start_date, pbls.start_date)
7551                          GROUP BY get_mapped_ra_id(get_task_id(l_targ_plan_level_code,rlmap.task_id),rlmap.resource_list_member_id) ,
7552                                 DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code))
7553                                 ,pbls.start_date,pbls.end_date,pbls.period_name)pbl;
7554 
7555                       IF P_PA_debug_mode = 'Y' THEN
7556                           pa_debug.g_err_stage:= 'Done with bulk insert Budget lines with different RLs and  TP not N and src Tp= targ TP'||SQL%ROWCOUNT;
7557                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7558                       END IF;
7559                       --dbms_output.put_line('I33');
7560                     END IF;
7561                    END IF;
7562                    --Update
7563 
7564                    IF l_targ_time_phased_code = 'N' THEN
7565                     IF l_src_resource_list_id = l_targ_resource_list_id THEN
7566                              IF P_PA_debug_mode = 'Y' THEN
7567                                 pa_debug.g_err_stage:= 'About to bulk insert resource assignments';
7568                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7569                              END IF;
7570 
7571                              --dbms_output.put_line('I34');
7572                              SELECT get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code),
7573                              decode(pblt.resource_assignment_id,null, 'INSERT',
7574                                                    decode(pblt.txn_currency_code, null,'INSERT','UPDATE')),
7575                              decode(pblt.resource_assignment_id,null, prat.planning_start_date,
7576                                                    decode(pblt.txn_currency_code, null,prat.planning_start_date,pblt.start_date)),
7577                              decode(pblt.resource_assignment_id,null, prat.planning_end_date,
7578                                                    decode(pblt.txn_currency_code, null,prat.planning_end_date,pblt.end_date)),
7579                              NULL,
7580                              decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7581                                                                 decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)),
7582                              sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)),
7583                              sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)),
7584                              sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue, null))*l_partial_factor,
7585                              decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null),
7586                              decode(count(pbls.budget_line_id),1,max(pbls.description),null),
7587                              decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null),
7588                              decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null),
7589                              decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null),
7590                              decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null),
7591                              decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null),
7592                              decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null),
7593                              decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null),
7594                              decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null),
7595                              decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null),
7596 
7597                              decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null),
7598                              decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null),
7599                              decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null),
7600                              decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null),
7601                              decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null),
7602                              decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null),
7603                              decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null),
7604                              decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null),
7605                              decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null),
7606                              decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null),
7607                              decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null),
7608                              decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null),
7609                              decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null),
7610                              decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null),
7611                              decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null),
7612                              decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null),
7613                              decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null),
7614                              decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null),
7615                              decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null),
7616                              decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null),
7617                              decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null),
7618                              decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null),
7619                              decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null),
7620                              decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null),
7621                              decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null),
7622                              decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null),
7623                              decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null),
7624                              decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null),
7625                              decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null),
7626                              decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null),
7627                              decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null),
7628                              decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null),
7629                              l_projfunc_currency_code,
7630                              DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
7631                              decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE_TYPE),null),
7632                              decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE),null),
7633                              Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
7634                              decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE_TYPE),null),
7635                              decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE),null),
7636                              DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null),
7637                              decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE_TYPE),null),
7638                              decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE),null),
7639                              sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost,null)),
7640                              sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost, null)),
7641                              Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null),
7642                              decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE_TYPE),null),
7643                              decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE),null),
7644                              sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor,
7645                              DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
7646 
7647 
7648                              --Bug 4224757.. Code changes for bug#4224757 starts here
7649                              SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
7650                              DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))))) ,
7651                              SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
7652                              DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
7653                              nvl(pbls.project_burdened_cost,0))))) ,
7654                              SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
7655                              DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
7656                              nvl(pbls.project_revenue,0)))))*l_partial_factor ,
7657                              --Bug 4224757.. Code changes for bug#4224757 ends here
7658 
7659                              decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null),
7660                              decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null),
7661 
7662                              --Bug 4224757. Code changes for bug#4224757 starts here
7663                              decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7664                              decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7665                               nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
7666                              decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7667                                                              decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))   ,
7668                              --Bug 4224757. Code changes for bug#4224757 ends here
7669 
7670                              decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null),
7671                         --     decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
7672 
7673                              decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null),
7674 
7675                              --Bug 4224757. Code changes for bug#4224757 starts here
7676                              decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
7677                                   decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
7678                                     'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
7679                              decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7680                                                                   decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))),
7681                              --Bug 4224757. Code changes for bug#4224757 ends here
7682 
7683                              decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null),
7684                              decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null),
7685                              decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null),
7686                              decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null),
7687                              decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null),
7688 
7689 
7690                              --Bug 4224757. Code changes for bug#4224757 starts here
7691                              decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7692                              decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7693                              nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
7694                              decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7695                                                            decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) ,
7696                              --Bug 4224757. Code changes for bug#4224757 ends here
7697 
7698                              decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null),
7699                              decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null)
7700                         BULK COLLECT INTO
7701                          l_bl_RESOURCE_ASIGNMENT_ID_tbl,
7702                          l_upd_ra_bl_dml_code_tbl,
7703                          l_bl_START_DATE_tbl,
7704                          l_bl_END_DATE_tbl,
7705                          l_bl_PERIOD_NAME_tbl,
7706                          l_bl_QUANTITY_tbl,
7707                          l_bl_RAW_COST_tbl,
7708                          l_bl_BURDENED_COST_tbl,
7709                          l_bl_REVENUE_tbl,
7710                          l_bl_CHANGE_REASON_CODE_tbl,
7711                          l_bl_DESCRIPTION_tbl,
7712                          l_bl_ATTRIBUTE_CATEGORY_tbl,
7713                          l_bl_ATTRIBUTE1_tbl,
7714                          l_bl_ATTRIBUTE2_tbl,
7715                          l_bl_ATTRIBUTE3_tbl,
7716                          l_bl_ATTRIBUTE4_tbl,
7717                          l_bl_ATTRIBUTE5_tbl,
7718                          l_bl_ATTRIBUTE6_tbl,
7719                          l_bl_ATTRIBUTE7_tbl,
7720                          l_bl_ATTRIBUTE8_tbl,
7721                          l_bl_ATTRIBUTE9_tbl,
7722                          l_bl_ATTRIBUTE10_tbl,
7723                          l_bl_ATTRIBUTE11_tbl,
7724                          l_bl_ATTRIBUTE12_tbl,
7725                          l_bl_ATTRIBUTE13_tbl,
7726                          l_bl_ATTRIBUTE14_tbl,
7727                          l_bl_ATTRIBUTE15_tbl,
7728                          l_bl_PM_PRODUCT_CODE_tbl,
7729                          l_bl_PM_BUDGET_LINE_REF_tbl,
7730                          l_bl_COST_REJECTION_CODE_tbl,
7731                          l_bl_REVENUE_REJ_CODE_tbl,
7732                          l_bl_BURDEN_REJECTION_CODE_tbl,
7733                          l_bl_OTHER_REJECTION_CODE_tbl,
7734                          l_bl_CODE_COMBINATION_ID_tbl,
7735                          l_bl_CCID_GEN_STATUS_CODE_tbl,
7736                          l_bl_CCID_GEN_REJ_MESSAGE_tbl,
7737                          l_bl_REQUEST_ID_tbl,
7738                          l_bl_BORROWED_REVENUE_tbl,
7739                          l_bl_TP_REVENUE_IN_tbl,
7740                          l_bl_TP_REVENUE_OUT_tbl,
7741                          l_bl_REVENUE_ADJ_tbl,
7742                          l_bl_LENT_RESOURCE_COST_tbl,
7743                          l_bl_TP_COST_IN_tbl,
7744                          l_bl_TP_COST_OUT_tbl,
7745                          l_bl_COST_ADJ_tbl,
7746                          l_bl_UNASSIGNED_TIME_COST_tbl,
7747                          l_bl_UTILIZATION_PERCENT_tbl,
7748                          l_bl_UTILIZATION_HOURS_tbl,
7749                          l_bl_UTILIZATION_ADJ_tbl,
7750                          l_bl_CAPACITY_tbl,
7751                          l_bl_HEAD_COUNT_tbl,
7752                          l_bl_HEAD_COUNT_ADJ_tbl,
7753                          l_bl_PROJFUNC_CUR_CODE_tbl,
7754                          l_bl_PROJFUNC_COST_RAT_TYP_tbl,
7755                          l_bl_PJFN_COST_RAT_DAT_TYP_tbl,
7756                          l_bl_PROJFUNC_COST_RAT_DAT_tbl,
7757                          l_bl_PROJFUNC_REV_RATE_TYP_tbl,
7758                          l_bl_PJFN_REV_RAT_DAT_TYPE_tbl,
7759                          l_bl_PROJFUNC_REV_RAT_DATE_tbl,
7760                          l_bl_PROJECT_COST_RAT_TYPE_tbl,
7761                          l_bl_PROJ_COST_RAT_DAT_TYP_tbl,
7762                          l_bl_PROJ_COST_RATE_DATE_tbl,
7763                          l_bl_PROJECT_RAW_COST_tbl,
7764                          l_bl_PROJECT_BURDENED_COST_tbl,
7765                          l_bl_PROJECT_REV_RATE_TYPE_tbl,
7766                          l_bl_PRJ_REV_RAT_DATE_TYPE_tbl,
7767                          l_bl_PROJECT_REV_RATE_DATE,
7768                          l_bl_PROJECT_REVENUE_tbl,
7769                          l_bl_TXN_CURRENCY_CODE_tbl,
7770                          l_bl_TXN_RAW_COST_tbl,
7771                          l_bl_TXN_BURDENED_COST_tbl,
7772                          l_bl_TXN_REVENUE_tbl,
7773                          l_bl_BUCKETING_PERIOD_CODE_tbl,
7774                          l_bl_TXN_STD_COST_RATE_tbl,
7775                          l_bl_TXN_COST_RATE_OVERIDE_tbl,
7776                          l_bl_COST_IND_CMPLD_SET_ID_tbl,
7777                    --        l_bl_TXN_BURDEN_MULTIPLIER_tbl,
7778                    --        l_bl_TXN_BRD_MLTIPLI_OVRID_tbl,
7779                          l_bl_TXN_STD_BILL_RATE_tbl,
7780                          l_bl_TXN_BILL_RATE_OVERRID_tbl,
7781                          l_bl_TXN_MARKUP_PERCENT_tbl,
7782                          l_bl_TXN_MRKUP_PER_OVERIDE_tbl,
7783                          l_bl_TXN_DISC_PERCENTAGE_tbl,
7784                          l_bl_TRANSFER_PRICE_RATE_tbl,
7785                          l_bl_BURDEN_COST_RATE_tbl,
7786                          l_bl_BURDEN_COST_RAT_OVRID_tbl,
7787                          l_bl_PC_CUR_CONV_REJ_CODE_tbl,
7788                          l_bl_PFC_CUR_CONV_REJ_CODE_tbl
7789                         from   pa_budget_lines pbls,
7790                                pa_budget_lines pblt,
7791                                pa_resource_assignments prat
7792                         where  get_mapped_dml_code(null,null,pbls.resource_assignment_id,l_targ_plan_level_code)='UPDATE'
7793                         and    pbls.budget_version_id = l_src_ver_id_tbl(j)
7794                         and    pblt.budget_version_id(+) = p_budget_version_id
7795                         and    pblt.resource_assignment_id(+)=get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code)
7796                         AND    pblt.txn_currency_code(+)=DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,
7797                                                       DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code))
7798                         and    prat.resource_assignment_id = get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code)
7799                         --IPM Arch Enhancement Bug 4865563
7800                       /*and    pbls.cost_rejection_code IS NULL
7801                         and    pbls.revenue_rejection_code IS NULL
7802                         and    pbls.burden_rejection_code IS NULL
7803                         and    pbls.other_rejection_code IS NULL
7804                         and    pbls.pc_cur_conv_rejection_code IS NULL
7805                         and    pbls.pfc_cur_conv_rejection_code IS NULL*/
7806                         GROUP BY get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code),
7807                         DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
7808                         pblt.resource_assignment_id,
7809                         pblt.txn_currency_code,
7810                         pblt.start_date,
7811                         pblt.end_date,
7812                         prat.planning_start_date,
7813                         prat.planning_end_date;
7814 
7815                         --dbms_output.put_line('I35');
7816 
7817                   ELSE--Resource lists are different , target time phasing is N and the target resource assignments already exist in prat
7818                               IF P_PA_debug_mode = 'Y' THEN
7819                                     pa_debug.g_err_stage:= 'About to bulk select for ins/upd the budget lins with targ NTP and Diff RLS';
7820                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7821                               END IF;
7822                               --dbms_output.put_line('I36');
7823 
7824                               SELECT get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id),
7825                               decode(pblt.resource_assignment_id,null, 'INSERT',
7826                                                          decode(pblt.txn_currency_code, null,'INSERT','UPDATE')),
7827                               decode(pblt.resource_assignment_id,null, prat.planning_start_date,
7828                                                          decode(pblt.txn_currency_code, null,prat.planning_start_date,pblt.start_date)),
7829                               decode(pblt.resource_assignment_id,null, prat.planning_end_date,
7830                                                          decode(pblt.txn_currency_code, null,prat.planning_end_date,pblt.end_date)),
7831                               NULL,
7832                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7833                                                                    decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)),
7834                               sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)),
7835                               sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)),
7836                               sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue, null))*l_partial_factor,
7837                               decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null),
7838                               decode(count(pbls.budget_line_id),1,max(pbls.description),null),
7839                               decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null),
7840                               decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null),
7841                               decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null),
7842                               decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null),
7843                               decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null),
7844                               decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null),
7845                               decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null),
7846                               decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null),
7847                               decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null),
7848                               decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null),
7849                               decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null),
7850                               decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null),
7851                               decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null),
7852                               decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null),
7853                               decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null),
7854                               decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null),
7855                               decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null),
7856                               decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null),
7857                               decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null),
7858                               decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null),
7859                               decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null),
7860                               decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null),
7861                               decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null),
7862                               decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null),
7863                               decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null),
7864                               decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null),
7865                               decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null),
7866                               decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null),
7867                               decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null),
7868                               decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null),
7869                               decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null),
7870                               decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null),
7871                               decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null),
7872                               decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null),
7873                               decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null),
7874                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null),
7875                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null),
7876                               decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null),
7877                               decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null),
7878                               decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null),
7879                               decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null),
7880                               l_projfunc_currency_code,
7881                               DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
7882                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE_TYPE),null),
7883                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE),null),
7884                               Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
7885                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE_TYPE),null),
7886                               decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE),null),
7887                               DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null),
7888                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE_TYPE),null),
7889                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE),null),
7890                               sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost, null)),
7891                               sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost,null)),
7892                               Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null),
7893                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE_TYPE),null),
7894                               decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE),null),
7895                               sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor,
7896                               DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
7897 
7898 
7899                               --Bug 4224757.. Code changes for bug#4224757 starts here
7900                               SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
7901                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))))),
7902                               SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
7903                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
7904                               nvl(pbls.project_burdened_cost,0))))) ,
7905                               SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
7906                               DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
7907                               nvl(pbls.project_revenue,0)))))*l_partial_factor ,
7908                               --Bug 4224757.. Code changes for bug#4224757 ends here
7909 
7910                               decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null),
7911                               decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null),
7912 
7913                               --Bug 4224757. Code changes for bug#4224757 starts here
7914                               decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7915                               decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7916                                nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
7917                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7918                                                                 decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))   ,
7919                               --Bug 4224757. Code changes for bug#4224757 ends here
7920 
7921                               decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null),
7922                            --     decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
7923 
7924                               decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null),
7925 
7926                               --Bug 4224757. Code changes for bug#4224757 starts here
7927                               decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
7928                                  decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
7929                                    'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
7930                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7931                                                                    decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))),
7932                                  --Bug 4224757. Code changes for bug#4224757 ends here
7933 
7934                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null),
7935                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null),
7936                               decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null),
7937                               decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null),
7938                               decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null),
7939 
7940                               --Bug 4224757. Code changes for bug#4224757 starts here
7941                               decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
7942                               decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
7943                                nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
7944                               decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
7945                                                          decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) ,
7946                               --Bug 4224757. Code changes for bug#4224757 ends here
7947 
7948                               decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null),
7949                               decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null)
7950                          BULK COLLECT INTO
7951                                l_bl_RESOURCE_ASIGNMENT_ID_tbl,
7952                                l_upd_ra_bl_dml_code_tbl,
7953                                l_bl_START_DATE_tbl,
7954                                l_bl_END_DATE_tbl,
7955                                l_bl_PERIOD_NAME_tbl,
7956                                l_bl_QUANTITY_tbl,
7957                                l_bl_RAW_COST_tbl,
7958                                l_bl_BURDENED_COST_tbl,
7959                                l_bl_REVENUE_tbl,
7960                                l_bl_CHANGE_REASON_CODE_tbl,
7961                                l_bl_DESCRIPTION_tbl,
7962                                l_bl_ATTRIBUTE_CATEGORY_tbl,
7963                                l_bl_ATTRIBUTE1_tbl,
7964                                l_bl_ATTRIBUTE2_tbl,
7965                                l_bl_ATTRIBUTE3_tbl,
7966                                l_bl_ATTRIBUTE4_tbl,
7967                                l_bl_ATTRIBUTE5_tbl,
7968                                l_bl_ATTRIBUTE6_tbl,
7969                                l_bl_ATTRIBUTE7_tbl,
7970                                l_bl_ATTRIBUTE8_tbl,
7971                                l_bl_ATTRIBUTE9_tbl,
7972                                l_bl_ATTRIBUTE10_tbl,
7973                                l_bl_ATTRIBUTE11_tbl,
7974                                l_bl_ATTRIBUTE12_tbl,
7975                                l_bl_ATTRIBUTE13_tbl,
7976                                l_bl_ATTRIBUTE14_tbl,
7977                                l_bl_ATTRIBUTE15_tbl,
7978                                l_bl_PM_PRODUCT_CODE_tbl,
7979                                l_bl_PM_BUDGET_LINE_REF_tbl,
7980                                l_bl_COST_REJECTION_CODE_tbl,
7981                                l_bl_REVENUE_REJ_CODE_tbl,
7982                                l_bl_BURDEN_REJECTION_CODE_tbl,
7983                                l_bl_OTHER_REJECTION_CODE_tbl,
7984                                l_bl_CODE_COMBINATION_ID_tbl,
7985                                l_bl_CCID_GEN_STATUS_CODE_tbl,
7986                                l_bl_CCID_GEN_REJ_MESSAGE_tbl,
7987                                l_bl_REQUEST_ID_tbl,
7988                                l_bl_BORROWED_REVENUE_tbl,
7989                                l_bl_TP_REVENUE_IN_tbl,
7990                                l_bl_TP_REVENUE_OUT_tbl,
7991                                l_bl_REVENUE_ADJ_tbl,
7992                                l_bl_LENT_RESOURCE_COST_tbl,
7993                                l_bl_TP_COST_IN_tbl,
7994                                l_bl_TP_COST_OUT_tbl,
7995                                l_bl_COST_ADJ_tbl,
7996                                l_bl_UNASSIGNED_TIME_COST_tbl,
7997                                l_bl_UTILIZATION_PERCENT_tbl,
7998                                l_bl_UTILIZATION_HOURS_tbl,
7999                                l_bl_UTILIZATION_ADJ_tbl,
8000                                l_bl_CAPACITY_tbl,
8001                                l_bl_HEAD_COUNT_tbl,
8002                                l_bl_HEAD_COUNT_ADJ_tbl,
8003                                l_bl_PROJFUNC_CUR_CODE_tbl,
8004                                l_bl_PROJFUNC_COST_RAT_TYP_tbl,
8005                                l_bl_PJFN_COST_RAT_DAT_TYP_tbl,
8006                                l_bl_PROJFUNC_COST_RAT_DAT_tbl,
8007                                l_bl_PROJFUNC_REV_RATE_TYP_tbl,
8008                                l_bl_PJFN_REV_RAT_DAT_TYPE_tbl,
8009                                l_bl_PROJFUNC_REV_RAT_DATE_tbl,
8010                                l_bl_PROJECT_COST_RAT_TYPE_tbl,
8011                                l_bl_PROJ_COST_RAT_DAT_TYP_tbl,
8012                                l_bl_PROJ_COST_RATE_DATE_tbl,
8013                                l_bl_PROJECT_RAW_COST_tbl,
8014                                l_bl_PROJECT_BURDENED_COST_tbl,
8015                                l_bl_PROJECT_REV_RATE_TYPE_tbl,
8016                                l_bl_PRJ_REV_RAT_DATE_TYPE_tbl,
8017                                l_bl_PROJECT_REV_RATE_DATE,
8018                                l_bl_PROJECT_REVENUE_tbl,
8019                                l_bl_TXN_CURRENCY_CODE_tbl,
8020                                l_bl_TXN_RAW_COST_tbl,
8021                                l_bl_TXN_BURDENED_COST_tbl,
8022                                l_bl_TXN_REVENUE_tbl,
8023                                l_bl_BUCKETING_PERIOD_CODE_tbl,
8024                                l_bl_TXN_STD_COST_RATE_tbl,
8025                                l_bl_TXN_COST_RATE_OVERIDE_tbl,
8026                                l_bl_COST_IND_CMPLD_SET_ID_tbl,
8027                            --      l_bl_TXN_BURDEN_MULTIPLIER_tbl,
8028                            --      l_bl_TXN_BRD_MLTIPLI_OVRID_tbl,
8029                                l_bl_TXN_STD_BILL_RATE_tbl,
8030                                l_bl_TXN_BILL_RATE_OVERRID_tbl,
8031                                l_bl_TXN_MARKUP_PERCENT_tbl,
8032                                l_bl_TXN_MRKUP_PER_OVERIDE_tbl,
8033                                l_bl_TXN_DISC_PERCENTAGE_tbl,
8034                                l_bl_TRANSFER_PRICE_RATE_tbl,
8035                                l_bl_BURDEN_COST_RATE_tbl,
8036                                l_bl_BURDEN_COST_RAT_OVRID_tbl,
8037                                l_bl_PC_CUR_CONV_REJ_CODE_tbl,
8038                                l_bl_PFC_CUR_CONV_REJ_CODE_tbl
8039                          from   pa_budget_lines pblt,
8040                                 pa_resource_assignments prat,
8041                            (SELECT  pbls.resource_assignment_id
8042                                  ,pbls.start_date
8043                                  ,pbls.last_update_date
8044                                  ,pbls.last_updated_by
8045                                  ,pbls.creation_date
8046                                  ,pbls.created_by
8047                                  ,pbls.last_update_login
8048                                  ,pbls.end_date
8049                                  ,pbls.period_name
8050                                  ,pbls.quantity
8051                                  ,pbls.raw_cost
8052                                  ,pbls.burdened_cost
8053                                  ,pbls.revenue
8054                                  ,pbls.change_reason_code
8055                                  ,pbls.description
8056                                  ,pbls.attribute_category
8057                                  ,pbls.attribute1
8058                                  ,pbls.attribute2
8059                                  ,pbls.attribute3
8060                                  ,pbls.attribute4
8061                                  ,pbls.attribute5
8062                                  ,pbls.attribute6
8063                                  ,pbls.attribute7
8064                                  ,pbls.attribute8
8065                                  ,pbls.attribute9
8066                                  ,pbls.attribute10
8067                                  ,pbls.attribute11
8068                                  ,pbls.attribute12
8069                                  ,pbls.attribute13
8070                                  ,pbls.attribute14
8071                                  ,pbls.attribute15
8072                                  ,pbls.raw_cost_source
8073                                  ,pbls.burdened_cost_source
8074                                  ,pbls.quantity_source
8075                                  ,pbls.revenue_source
8076                                  ,pbls.pm_product_code
8077                                  ,pbls.pm_budget_line_reference
8078                                  ,pbls.cost_rejection_code
8079                                  ,pbls.revenue_rejection_code
8080                                  ,pbls.burden_rejection_code
8081                                  ,pbls.other_rejection_code
8082                                  ,pbls.code_combination_id
8083                                  ,pbls.ccid_gen_status_code
8084                                  ,pbls.ccid_gen_rej_message
8085                                  ,pbls.request_id
8086                                  ,pbls.borrowed_revenue
8087                                  ,pbls.tp_revenue_in
8088                                  ,pbls.tp_revenue_out
8089                                  ,pbls.revenue_adj
8090                                  ,pbls.lent_resource_cost
8091                                  ,pbls.tp_cost_in
8092                                  ,pbls.tp_cost_out
8093                                  ,pbls.cost_adj
8094                                  ,pbls.unassigned_time_cost
8095                                  ,pbls.utilization_percent
8096                                  ,pbls.utilization_hours
8097                                  ,pbls.utilization_adj
8098                                  ,pbls.capacity
8099                                  ,pbls.head_count
8100                                  ,pbls.head_count_adj
8101                                  ,pbls.projfunc_currency_code
8102                                  ,pbls.projfunc_cost_rate_type
8103                                  ,pbls.projfunc_cost_exchange_rate
8104                                  ,pbls.projfunc_cost_rate_date_type
8105                                  ,pbls.projfunc_cost_rate_date
8106                                  ,pbls.projfunc_rev_rate_type
8107                                  ,pbls.projfunc_rev_exchange_rate
8108                                  ,pbls.projfunc_rev_rate_date_type
8109                                  ,pbls.projfunc_rev_rate_date
8110                                  ,pbls.project_currency_code
8111                                  ,pbls.project_cost_rate_type
8112                                  ,pbls.project_cost_exchange_rate
8113                                  ,pbls.project_cost_rate_date_type
8114                                  ,pbls.project_cost_rate_date
8115                                  ,pbls.project_raw_cost
8116                                  ,pbls.project_burdened_cost
8117                                  ,pbls.project_rev_rate_type
8118                                  ,pbls.project_rev_exchange_rate
8119                                  ,pbls.project_rev_rate_date_type
8120                                  ,pbls.project_rev_rate_date
8121                                  ,pbls.project_revenue
8122                                  ,pbls.txn_currency_code
8123                                  ,pbls.txn_raw_cost
8124                                  ,pbls.txn_burdened_cost
8125                                  ,pbls.txn_revenue
8126                                  ,pbls.bucketing_period_code
8127                                  ,pbls.budget_line_id
8128                                  ,pbls.budget_version_id
8129                                  ,pbls.txn_standard_cost_rate
8130                                  ,pbls.txn_cost_rate_override
8131                                  ,pbls.cost_ind_compiled_set_id
8132                                  ,pbls.txn_standard_bill_rate
8133                                  ,pbls.txn_bill_rate_override
8134                                  ,pbls.txn_markup_percent
8135                                  ,pbls.txn_markup_percent_override
8136                                  ,pbls.txn_discount_percentage
8137                                  ,pbls.transfer_price_rate
8138                                  ,pbls.burden_cost_rate
8139                                  ,pbls.burden_cost_rate_override
8140                                  ,pbls.pc_cur_conv_rejection_code
8141                                  ,pbls.pfc_cur_conv_rejection_code
8142                                  ,pras.resource_assignment_id
8143                                  ,pras.task_id
8144                                  ,tmp4.resource_list_member_id
8145                              FROM   pa_resource_assignments pras,
8146                                   pa_res_list_map_tmp4 tmp4,
8147                                   pa_budget_lines pbls
8148                              WHERE  tmp4.txn_source_id=pras.resource_assignment_id
8149                              AND    pbls.resource_assignment_id=pras.resource_assignment_id) pbls
8150                          where  get_mapped_dml_code(get_task_id(l_targ_plan_level_code,pbls.task_id),pbls.resource_list_member_id)='UPDATE'
8151                          and    pblt.budget_version_id(+) = p_budget_version_id
8152                          and    pblt.resource_assignment_id(+)=get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id)
8153                          AND    pblt.txn_currency_code(+)=DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,
8154                                                        DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code))
8155                          and    prat.resource_assignment_id=get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id)
8156                          --IPM Arch Enhancement Bug 4865563
8157                        /*and    pbls.cost_rejection_code IS NULL
8158                          and    pbls.revenue_rejection_code IS NULL
8159                          and    pbls.burden_rejection_code IS NULL
8160                          and    pbls.other_rejection_code IS NULL
8161                          and    pbls.pc_cur_conv_rejection_code IS NULL
8162                          and    pbls.pfc_cur_conv_rejection_code IS NULL*/
8163                          GROUP BY get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id), DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code),
8164                                 pblt.resource_assignment_id,pblt.txn_currency_code,pblt.start_date,prat.planning_start_date, prat.planning_end_date,pblt.end_date ;
8165 
8166                          IF P_PA_debug_mode = 'Y' THEN
8167                               pa_debug.g_err_stage:= 'Done with bulk select for ins/upd the budget lins with targ NTP and Diff RLS';
8168                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8169                          END IF;
8170                          --dbms_output.put_line('I37');
8171 
8172                   END IF;
8173 
8174 
8175                 ELSE -- Time phased code is not N and src time phasing = target time phasing
8176                   IF l_src_resource_list_id = l_targ_resource_list_id THEN
8177 
8178                      IF P_PA_debug_mode = 'Y' THEN
8179                          pa_debug.g_err_stage:='About to select bls for PA/GL TP and same resource list';
8180                          pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
8181                      END IF;
8182 
8183                      --dbms_output.put_line('I38');
8184 
8185                      SELECT get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code),
8186                          decode(pblt.resource_assignment_id,null, 'INSERT',
8187                                                    decode(pblt.txn_currency_code, null,'INSERT',
8188                                                       decode(pblt.start_date,null,'INSERT','UPDATE'))),
8189                          pbls.start_date,
8190                          pbls.end_date,
8191                          pbls.period_name,
8192                          decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8193                                                             decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)),
8194                          sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost,null)),
8195                          sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost, null)),
8196                          sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue, null))*l_partial_factor,
8197                          decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null),
8198                          decode(count(pbls.budget_line_id),1,max(pbls.description),null),
8199                          decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null),
8200                          decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null),
8201                          decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null),
8202                          decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null),
8203                          decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null),
8204                          decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null),
8205                          decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null),
8206                          decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null),
8207                          decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null),
8208                          decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null),
8209                          decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null),
8210                          decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null),
8211                          decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null),
8212                          decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null),
8213                          decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null),
8214                          decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null),
8215                          decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null),
8216                          decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null),
8217                          decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null),
8218                          decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null),
8219                          decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null),
8220                          decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null),
8221                          decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null),
8222                          decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null),
8223                          decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null),
8224                          decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null),
8225                          decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null),
8226                          decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null),
8227                          decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null),
8228                          decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null),
8229                          decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null),
8230                          decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null),
8231                          decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null),
8232                          decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null),
8233                          decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null),
8234                          decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null),
8235                          decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null),
8236                          decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null),
8237                          decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null),
8238                          decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null),
8239                          decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null),
8240                          l_projfunc_currency_code,
8241                          DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
8242                          decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE_TYPE),null),
8243                          decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE),null),
8244                          Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
8245                          decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE_TYPE),null),
8246                          decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE),null),
8247                          DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null),
8248                          decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE_TYPE),null),
8249                          decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE),null),
8250                          sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost,null)),
8251                          sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost, null)),
8252                          Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null),
8253                          decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE_TYPE),null),
8254                          decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE),null),
8255                          sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor,
8256                          DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
8257                          --Bug 4247568. Code changes for bug 4247568  starts here. If src multi curr flag and targ multi curr flag are  -- diff then reutrn project raw cost, project burdened cost  and project  revenue.
8258                          SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))))),
8259                          SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
8260                          DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))))),
8261                          SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
8262                          DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor,
8263                          --Bug 4247568. Code changes for bug 4247568  ends here.
8264                          decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null),
8265                          decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null),
8266 
8267                          --Bug 4224757. Code changes for bug#4224757 starts here
8268                          decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
8269                          decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
8270                         nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
8271                          decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8272                                                          decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))   ,
8273                          --Bug 4224757. Code changes for bug#4224757 ends here
8274 
8275                          decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null),
8276                     --       decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
8277 
8278                          decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null),
8279 
8280                          --Bug 4224757. Code changes for bug#4224757 starts here
8281                          decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
8282                                decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
8283                                    'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
8284                          decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8285                                                               decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))),
8286                          --Bug 4224757. Code changes for bug#4224757 ends here
8287 
8288 
8289                          decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null),
8290                          decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null),
8291                          decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null),
8292                          decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null),
8293                          decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null),
8294 
8295                          --Bug 4224757. Code changes for bug#4224757 starts here
8296                          decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
8297                          decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
8298                           nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
8299                          decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8300                                                       decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))),
8301                          --Bug 4224757. Code changes for bug#4224757 ends here
8302 
8303                          decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null),
8304                          decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null)
8305                         BULK COLLECT INTO
8306                          l_bl_RESOURCE_ASIGNMENT_ID_tbl,
8307                          l_upd_ra_bl_dml_code_tbl,
8308                          l_bl_START_DATE_tbl,
8309                          l_bl_END_DATE_tbl,
8310                          l_bl_PERIOD_NAME_tbl,
8311                          l_bl_QUANTITY_tbl,
8312                          l_bl_RAW_COST_tbl,
8313                          l_bl_BURDENED_COST_tbl,
8314                          l_bl_REVENUE_tbl,
8315                          l_bl_CHANGE_REASON_CODE_tbl,
8316                          l_bl_DESCRIPTION_tbl,
8317                          l_bl_ATTRIBUTE_CATEGORY_tbl,
8318                          l_bl_ATTRIBUTE1_tbl,
8319                          l_bl_ATTRIBUTE2_tbl,
8320                          l_bl_ATTRIBUTE3_tbl,
8321                          l_bl_ATTRIBUTE4_tbl,
8322                          l_bl_ATTRIBUTE5_tbl,
8323                          l_bl_ATTRIBUTE6_tbl,
8324                          l_bl_ATTRIBUTE7_tbl,
8325                          l_bl_ATTRIBUTE8_tbl,
8326                          l_bl_ATTRIBUTE9_tbl,
8327                          l_bl_ATTRIBUTE10_tbl,
8328                          l_bl_ATTRIBUTE11_tbl,
8329                          l_bl_ATTRIBUTE12_tbl,
8330                          l_bl_ATTRIBUTE13_tbl,
8331                          l_bl_ATTRIBUTE14_tbl,
8332                          l_bl_ATTRIBUTE15_tbl,
8333                          l_bl_PM_PRODUCT_CODE_tbl,
8334                          l_bl_PM_BUDGET_LINE_REF_tbl,
8335                          l_bl_COST_REJECTION_CODE_tbl,
8336                          l_bl_REVENUE_REJ_CODE_tbl,
8337                          l_bl_BURDEN_REJECTION_CODE_tbl,
8338                          l_bl_OTHER_REJECTION_CODE_tbl,
8339                          l_bl_CODE_COMBINATION_ID_tbl,
8340                          l_bl_CCID_GEN_STATUS_CODE_tbl,
8341                          l_bl_CCID_GEN_REJ_MESSAGE_tbl,
8342                          l_bl_REQUEST_ID_tbl,
8343                          l_bl_BORROWED_REVENUE_tbl,
8344                          l_bl_TP_REVENUE_IN_tbl,
8345                          l_bl_TP_REVENUE_OUT_tbl,
8346                          l_bl_REVENUE_ADJ_tbl,
8347                          l_bl_LENT_RESOURCE_COST_tbl,
8348                          l_bl_TP_COST_IN_tbl,
8349                          l_bl_TP_COST_OUT_tbl,
8350                          l_bl_COST_ADJ_tbl,
8351                          l_bl_UNASSIGNED_TIME_COST_tbl,
8352                          l_bl_UTILIZATION_PERCENT_tbl,
8353                          l_bl_UTILIZATION_HOURS_tbl,
8354                          l_bl_UTILIZATION_ADJ_tbl,
8355                          l_bl_CAPACITY_tbl,
8356                          l_bl_HEAD_COUNT_tbl,
8357                          l_bl_HEAD_COUNT_ADJ_tbl,
8358                          l_bl_PROJFUNC_CUR_CODE_tbl,
8359                          l_bl_PROJFUNC_COST_RAT_TYP_tbl,
8360                          l_bl_PJFN_COST_RAT_DAT_TYP_tbl,
8361                          l_bl_PROJFUNC_COST_RAT_DAT_tbl,
8362                          l_bl_PROJFUNC_REV_RATE_TYP_tbl,
8363                          l_bl_PJFN_REV_RAT_DAT_TYPE_tbl,
8364                          l_bl_PROJFUNC_REV_RAT_DATE_tbl,
8365                          l_bl_PROJECT_COST_RAT_TYPE_tbl,
8366                          l_bl_PROJ_COST_RAT_DAT_TYP_tbl,
8367                          l_bl_PROJ_COST_RATE_DATE_tbl,
8368                          l_bl_PROJECT_RAW_COST_tbl,
8369                          l_bl_PROJECT_BURDENED_COST_tbl,
8370                          l_bl_PROJECT_REV_RATE_TYPE_tbl,
8371                          l_bl_PRJ_REV_RAT_DATE_TYPE_tbl,
8372                          l_bl_PROJECT_REV_RATE_DATE,
8373                          l_bl_PROJECT_REVENUE_tbl,
8374                          l_bl_TXN_CURRENCY_CODE_tbl,
8375                          l_bl_TXN_RAW_COST_tbl,
8376                          l_bl_TXN_BURDENED_COST_tbl,
8377                          l_bl_TXN_REVENUE_tbl,
8378                          l_bl_BUCKETING_PERIOD_CODE_tbl,
8379                          l_bl_TXN_STD_COST_RATE_tbl,
8380                          l_bl_TXN_COST_RATE_OVERIDE_tbl,
8381                          l_bl_COST_IND_CMPLD_SET_ID_tbl,
8382                       --     l_bl_TXN_BURDEN_MULTIPLIER_tbl,
8383                       --     l_bl_TXN_BRD_MLTIPLI_OVRID_tbl,
8384                          l_bl_TXN_STD_BILL_RATE_tbl,
8385                          l_bl_TXN_BILL_RATE_OVERRID_tbl,
8386                          l_bl_TXN_MARKUP_PERCENT_tbl,
8387                          l_bl_TXN_MRKUP_PER_OVERIDE_tbl,
8388                          l_bl_TXN_DISC_PERCENTAGE_tbl,
8389                          l_bl_TRANSFER_PRICE_RATE_tbl,
8390                          l_bl_BURDEN_COST_RATE_tbl,
8391                          l_bl_BURDEN_COST_RAT_OVRID_tbl,
8392                          l_bl_PC_CUR_CONV_REJ_CODE_tbl,
8393                          l_bl_PFC_CUR_CONV_REJ_CODE_tbl
8394                         from   pa_budget_lines pbls,
8395                              pa_budget_lines pblt
8396                         where  get_mapped_dml_code(null,null,pbls.resource_assignment_id,l_targ_plan_level_code)='UPDATE'
8397                         and    pbls.budget_version_id = l_src_ver_id_tbl(j)
8398                         and    pblt.budget_version_id(+) = p_budget_version_id
8399                         and    pblt.resource_assignment_id(+)=get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code)
8400                         AND    pblt.txn_currency_code(+)=DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,
8401                                                       DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code))
8402                         AND    pblt.start_date(+)=pbls.start_date
8403                         --IPM Arch Enhancement Bug 4865563
8404                       /*and    pbls.cost_rejection_code IS NULL
8405                         and    pbls.revenue_rejection_code IS NULL
8406                         and    pbls.burden_rejection_code IS NULL
8407                         and    pbls.other_rejection_code IS NULL
8408                         and    pbls.pc_cur_conv_rejection_code IS NULL
8409                         and    pbls.pfc_cur_conv_rejection_code IS NULL*/
8410                         and    pbls.start_date >= nvl(l_etc_start_date,pbls.start_date)
8411                         GROUP BY get_mapped_ra_id(null,null,pbls.resource_assignment_id,l_targ_plan_level_code), DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code)
8412                               ,pbls.start_date, pbls.period_name,pbls.end_date,pblt.resource_assignment_id,
8413                               pblt.start_Date,pblt.txn_currency_code;
8414 
8415                        IF P_PA_debug_mode = 'Y' THEN
8416                          pa_debug.g_err_stage:='selected bls for PA/GL TP and same resource list';
8417                          pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
8418                        END IF;
8419                        --dbms_output.put_line('I39');
8420 
8421                   ELSE--Time phased code is not None and Resource lists are different
8422 
8423                       --dbms_output.put_line('I40');
8424                       SELECT get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id),
8425                            decode(pblt.resource_assignment_id,null, 'INSERT',
8426                                                       decode(pblt.txn_currency_code, null,'INSERT',
8427                                                            decode(pblt.start_date,null,'INSERT','UPDATE'))),
8428                            pbls.start_date,
8429                            pbls.end_date,
8430                            pbls.period_name,
8431                            decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8432                                                               decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)),
8433                            sum(Decode(l_cost_impl_flag ,'Y', pbls.raw_cost, null)),
8434                            sum(Decode(l_cost_impl_flag ,'Y', pbls.burdened_cost,null)),
8435                            sum(Decode(l_rev_impl_flag ,'Y', pbls.revenue,null))*l_partial_factor,
8436                            decode(count(pbls.budget_line_id),1,max(pbls.change_reason_code),null),
8437                            decode(count(pbls.budget_line_id),1,max(pbls.description),null),
8438                            decode(count(pbls.budget_line_id),1,max(pbls.attribute_category),null),
8439                            decode(count(pbls.budget_line_id),1,max(pbls.attribute1),null),
8440                            decode(count(pbls.budget_line_id),1,max(pbls.attribute2),null),
8441                            decode(count(pbls.budget_line_id),1,max(pbls.attribute3),null),
8442                            decode(count(pbls.budget_line_id),1,max(pbls.attribute4),null),
8443                            decode(count(pbls.budget_line_id),1,max(pbls.attribute5),null),
8444                            decode(count(pbls.budget_line_id),1,max(pbls.attribute6),null),
8445                            decode(count(pbls.budget_line_id),1,max(pbls.attribute7),null),
8446                            decode(count(pbls.budget_line_id),1,max(pbls.attribute8),null),
8447                            decode(count(pbls.budget_line_id),1,max(pbls.attribute9),null),
8448                            decode(count(pbls.budget_line_id),1,max(pbls.attribute10),null),
8449                            decode(count(pbls.budget_line_id),1,max(pbls.attribute11),null),
8450                            decode(count(pbls.budget_line_id),1,max(pbls.attribute12),null),
8451                            decode(count(pbls.budget_line_id),1,max(pbls.attribute13),null),
8452                            decode(count(pbls.budget_line_id),1,max(pbls.attribute14),null),
8453                            decode(count(pbls.budget_line_id),1,max(pbls.attribute15),null),
8454                            decode(count(pbls.budget_line_id),1,max(pbls.pm_product_code),null),
8455                            decode(count(pbls.budget_line_id),1,max(pbls.pm_budget_line_reference),null),
8456                            decode(count(pbls.budget_line_id),1,max(pbls.cost_rejection_code),null),
8457                            decode(count(pbls.budget_line_id),1,max(pbls.revenue_rejection_code),null),
8458                            decode(count(pbls.budget_line_id),1,max(pbls.burden_rejection_code),null),
8459                            decode(count(pbls.budget_line_id),1,max(pbls.other_rejection_code),null),
8460                            decode(count(pbls.budget_line_id),1,max(pbls.code_combination_id),null),
8461                            decode(count(pbls.budget_line_id),1,max(pbls.ccid_gen_status_code),null),
8462                            decode(count(pbls.budget_line_id),1,max(pbls.CCID_GEN_REJ_MESSAGE),null),
8463                            decode(count(pbls.budget_line_id),1,max(pbls.REQUEST_ID),null),
8464                            decode(count(pbls.budget_line_id),1,max(pbls.BORROWED_REVENUE),null),
8465                            decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_IN),null),
8466                            decode(count(pbls.budget_line_id),1,max(pbls.TP_REVENUE_OUT),null),
8467                            decode(count(pbls.budget_line_id),1,max(pbls.REVENUE_ADJ),null),
8468                            decode(count(pbls.budget_line_id),1,max(pbls.LENT_RESOURCE_COST),null),
8469                            decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_IN),null),
8470                            decode(count(pbls.budget_line_id),1,max(pbls.TP_COST_OUT),null),
8471                            decode(count(pbls.budget_line_id),1,max(pbls.COST_ADJ),null),
8472                            decode(count(pbls.budget_line_id),1,max(pbls.UNASSIGNED_TIME_COST),null),
8473                            decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_PERCENT),null),
8474                            decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_HOURS),null),
8475                            decode(count(pbls.budget_line_id),1,max(pbls.UTILIZATION_ADJ),null),
8476                            decode(count(pbls.budget_line_id),1,max(pbls.CAPACITY),null),
8477                            decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT),null),
8478                            decode(count(pbls.budget_line_id),1,max(pbls.HEAD_COUNT_ADJ),null),
8479                            l_projfunc_currency_code,
8480                            DECODE(l_cost_impl_flag,'Y', DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
8481                            decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE_TYPE),null),
8482                            decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_COST_RATE_DATE),null),
8483                            Decode(l_rev_impl_flag,'Y',DECODE(l_targ_multi_curr_flag,'Y', 'User', null),Null),
8484                            decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE_TYPE),null),
8485                            decode(count(pbls.budget_line_id),1,max(pbls.PROJFUNC_REV_RATE_DATE),null),
8486                            DECODE(l_cost_impl_flag,'Y',DECODE(l_targ_multi_curr_flag, 'Y','User', null),Null),
8487                            decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE_TYPE),null),
8488                            decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_COST_RATE_DATE),null),
8489                            sum(Decode(l_cost_impl_flag ,'Y',pbls.project_raw_cost,null)),
8490                            sum(Decode(l_cost_impl_flag ,'Y',pbls.project_burdened_cost,null)),
8491                            Decode(l_rev_impl_flag, 'Y', DECODE(l_targ_multi_curr_flag, 'Y', 'User', null),Null),
8492                            decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE_TYPE),null),
8493                            decode(count(pbls.budget_line_id),1,max(pbls.PROJECT_REV_RATE_DATE),null),
8494                            sum(Decode(l_rev_impl_flag , 'Y', pbls.project_revenue,null))*l_partial_factor,
8495                            DECODE(l_copy_pfc_for_txn_amt_flag,'Y',l_projfunc_currency_code,DECODE(l_same_multi_curr_flag,'Y', pbls.txn_currency_code,l_project_currency_code)),
8496 
8497 
8498                            --Bug 4224757.. Code changes for bug#4224757 starts here
8499                            SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0),
8500                            DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))))),
8501                            SUM(decode(l_cost_impl_flag,'Y', decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0),
8502                            DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_burdened_cost,0),
8503                            nvl(pbls.project_burdened_cost,0))))) ,
8504                            SUM(decode(l_rev_impl_flag,'Y',decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.revenue,0),
8505                            DECODE(l_same_multi_curr_flag, 'Y', nvl(pbls.txn_revenue,0),
8506                            nvl(pbls.project_revenue,0)))))*l_partial_factor ,
8507                            --Bug 4224757.. Code changes for bug#4224757 ends here
8508 
8509 
8510                            decode(count(pbls.budget_line_id),1,max(pbls.BUCKETING_PERIOD_CODE),null),
8511                            decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_cost_rate),null),
8512 
8513                            --Bug 4224757. Code changes for bug#4224757 starts here
8514                            decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
8515                            decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.raw_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
8516                             nvl(pbls.txn_raw_cost,0), nvl(pbls.project_raw_cost,0))) ))/
8517                            decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8518                                                        decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null)))  ,
8519                            --Bug 4224757. Code changes for bug#4224757 ends here
8520 
8521                            decode(count(pbls.budget_line_id),1,max(pbls.cost_ind_compiled_set_id),null),
8522                       --      decode(count(pbls.budget_line_id),1,max(pbls.txn_burden_multiplier),null),
8523 
8524                            decode(count(pbls.budget_line_id),1,max(pbls.txn_standard_bill_rate),null),
8525 
8526                            --Bug 4224757. Code changes for bug#4224757 starts here
8527                            decode(nvl(sum(pbls.quantity),0),0,0,sum(Decode(l_rev_impl_flag ,'Y',
8528                                 decode(l_copy_pfc_for_txn_amt_flag,'Y', nvl(pbls.revenue,0), DECODE(l_same_multi_curr_flag,
8529                                    'Y', nvl(pbls.txn_revenue,0), nvl(pbls.project_revenue,0)))))*l_partial_factor/
8530                            decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8531                                                                 decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))),
8532                            --Bug 4224757. Code changes for bug#4224757 ends here
8533 
8534                            decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT),null),
8535                            decode(count(pbls.budget_line_id),1,max(pbls.TXN_MARKUP_PERCENT_OVERRIDE),null),
8536                            decode(count(pbls.budget_line_id),1,max(pbls.TXN_DISCOUNT_PERCENTAGE),null),
8537                            decode(count(pbls.budget_line_id),1,max(pbls.TRANSFER_PRICE_RATE),null),
8538                            decode(count(pbls.budget_line_id),1,max(pbls.BURDEN_COST_RATE),null),
8539 
8540                            --Bug 4224757. Code changes for bug#4224757 starts here
8541                            decode(nvl(sum(pbls.quantity),0),0,0,SUM(decode(l_cost_impl_flag,'Y',
8542                            decode(l_copy_pfc_for_txn_amt_flag,'Y',nvl(pbls.burdened_cost,0), DECODE(l_same_multi_curr_flag, 'Y',
8543                            nvl(pbls.txn_burdened_cost,0), nvl(pbls.project_burdened_cost,0))) ))/
8544                            decode(l_cost_impl_flag,'Y',sum(pbls.quantity),decode(l_rev_impl_flag,'Y',
8545                                                              decode(l_impl_qty_tbl(j),'Y', sum(pbls.quantity) * l_partial_factor,null),null))) ,
8546                            --Bug 4224757. Code changes for bug#4224757 ends here
8547 
8548                            decode(count(pbls.budget_line_id),1,max(pbls.PC_CUR_CONV_REJECTION_CODE),null),
8549                            decode(count(pbls.budget_line_id),1,max(pbls.PFC_CUR_CONV_REJECTION_CODE),null)
8550                       BULK COLLECT INTO
8551                             l_bl_RESOURCE_ASIGNMENT_ID_tbl,
8552                             l_upd_ra_bl_dml_code_tbl,
8553                             l_bl_START_DATE_tbl,
8554                             l_bl_END_DATE_tbl,
8555                             l_bl_PERIOD_NAME_tbl,
8556                             l_bl_QUANTITY_tbl,
8557                             l_bl_RAW_COST_tbl,
8558                             l_bl_BURDENED_COST_tbl,
8559                             l_bl_REVENUE_tbl,
8560                             l_bl_CHANGE_REASON_CODE_tbl,
8561                             l_bl_DESCRIPTION_tbl,
8562                             l_bl_ATTRIBUTE_CATEGORY_tbl,
8563                             l_bl_ATTRIBUTE1_tbl,
8564                             l_bl_ATTRIBUTE2_tbl,
8565                             l_bl_ATTRIBUTE3_tbl,
8566                             l_bl_ATTRIBUTE4_tbl,
8567                             l_bl_ATTRIBUTE5_tbl,
8568                             l_bl_ATTRIBUTE6_tbl,
8569                             l_bl_ATTRIBUTE7_tbl,
8570                             l_bl_ATTRIBUTE8_tbl,
8571                             l_bl_ATTRIBUTE9_tbl,
8572                             l_bl_ATTRIBUTE10_tbl,
8573                             l_bl_ATTRIBUTE11_tbl,
8574                             l_bl_ATTRIBUTE12_tbl,
8575                             l_bl_ATTRIBUTE13_tbl,
8576                             l_bl_ATTRIBUTE14_tbl,
8577                             l_bl_ATTRIBUTE15_tbl,
8578                             l_bl_PM_PRODUCT_CODE_tbl,
8579                             l_bl_PM_BUDGET_LINE_REF_tbl,
8580                             l_bl_COST_REJECTION_CODE_tbl,
8581                             l_bl_REVENUE_REJ_CODE_tbl,
8582                             l_bl_BURDEN_REJECTION_CODE_tbl,
8583                             l_bl_OTHER_REJECTION_CODE_tbl,
8584                             l_bl_CODE_COMBINATION_ID_tbl,
8585                             l_bl_CCID_GEN_STATUS_CODE_tbl,
8586                             l_bl_CCID_GEN_REJ_MESSAGE_tbl,
8587                             l_bl_REQUEST_ID_tbl,
8588                             l_bl_BORROWED_REVENUE_tbl,
8589                             l_bl_TP_REVENUE_IN_tbl,
8590                             l_bl_TP_REVENUE_OUT_tbl,
8591                             l_bl_REVENUE_ADJ_tbl,
8592                             l_bl_LENT_RESOURCE_COST_tbl,
8593                             l_bl_TP_COST_IN_tbl,
8594                             l_bl_TP_COST_OUT_tbl,
8595                             l_bl_COST_ADJ_tbl,
8596                             l_bl_UNASSIGNED_TIME_COST_tbl,
8597                             l_bl_UTILIZATION_PERCENT_tbl,
8598                             l_bl_UTILIZATION_HOURS_tbl,
8599                             l_bl_UTILIZATION_ADJ_tbl,
8600                             l_bl_CAPACITY_tbl,
8601                             l_bl_HEAD_COUNT_tbl,
8602                             l_bl_HEAD_COUNT_ADJ_tbl,
8603                             l_bl_PROJFUNC_CUR_CODE_tbl,
8604                             l_bl_PROJFUNC_COST_RAT_TYP_tbl,
8605                             l_bl_PJFN_COST_RAT_DAT_TYP_tbl,
8606                             l_bl_PROJFUNC_COST_RAT_DAT_tbl,
8607                             l_bl_PROJFUNC_REV_RATE_TYP_tbl,
8608                             l_bl_PJFN_REV_RAT_DAT_TYPE_tbl,
8609                             l_bl_PROJFUNC_REV_RAT_DATE_tbl,
8610                             l_bl_PROJECT_COST_RAT_TYPE_tbl,
8611                             l_bl_PROJ_COST_RAT_DAT_TYP_tbl,
8612                             l_bl_PROJ_COST_RATE_DATE_tbl,
8613                             l_bl_PROJECT_RAW_COST_tbl,
8614                             l_bl_PROJECT_BURDENED_COST_tbl,
8615                             l_bl_PROJECT_REV_RATE_TYPE_tbl,
8616                             l_bl_PRJ_REV_RAT_DATE_TYPE_tbl,
8617                             l_bl_PROJECT_REV_RATE_DATE,
8618                             l_bl_PROJECT_REVENUE_tbl,
8619                             l_bl_TXN_CURRENCY_CODE_tbl,
8620                             l_bl_TXN_RAW_COST_tbl,
8621                             l_bl_TXN_BURDENED_COST_tbl,
8622                             l_bl_TXN_REVENUE_tbl,
8623                             l_bl_BUCKETING_PERIOD_CODE_tbl,
8624                             l_bl_TXN_STD_COST_RATE_tbl,
8625                             l_bl_TXN_COST_RATE_OVERIDE_tbl,
8626                             l_bl_COST_IND_CMPLD_SET_ID_tbl,
8627                          --     l_bl_TXN_BURDEN_MULTIPLIER_tbl,
8628                          --     l_bl_TXN_BRD_MLTIPLI_OVRID_tbl,
8629                             l_bl_TXN_STD_BILL_RATE_tbl,
8630                             l_bl_TXN_BILL_RATE_OVERRID_tbl,
8631                             l_bl_TXN_MARKUP_PERCENT_tbl,
8632                             l_bl_TXN_MRKUP_PER_OVERIDE_tbl,
8633                             l_bl_TXN_DISC_PERCENTAGE_tbl,
8634                             l_bl_TRANSFER_PRICE_RATE_tbl,
8635                             l_bl_BURDEN_COST_RATE_tbl,
8636                             l_bl_BURDEN_COST_RAT_OVRID_tbl,
8637                             l_bl_PC_CUR_CONV_REJ_CODE_tbl,
8638                             l_bl_PFC_CUR_CONV_REJ_CODE_tbl
8639                       from   pa_budget_lines pblt,
8640                            (SELECT  pbls.resource_assignment_id
8641                                  ,pbls.start_date
8642                                  ,pbls.last_update_date
8643                                  ,pbls.last_updated_by
8644                                  ,pbls.creation_date
8645                                  ,pbls.created_by
8646                                  ,pbls.last_update_login
8647                                  ,pbls.end_date
8648                                  ,pbls.period_name
8649                                  ,pbls.quantity
8650                                  ,pbls.raw_cost
8651                                  ,pbls.burdened_cost
8652                                  ,pbls.revenue
8653                                  ,pbls.change_reason_code
8654                                  ,pbls.description
8655                                  ,pbls.attribute_category
8656                                  ,pbls.attribute1
8657                                  ,pbls.attribute2
8658                                  ,pbls.attribute3
8659                                  ,pbls.attribute4
8660                                  ,pbls.attribute5
8661                                  ,pbls.attribute6
8662                                  ,pbls.attribute7
8663                                  ,pbls.attribute8
8664                                  ,pbls.attribute9
8665                                  ,pbls.attribute10
8666                                  ,pbls.attribute11
8667                                  ,pbls.attribute12
8668                                  ,pbls.attribute13
8669                                  ,pbls.attribute14
8670                                  ,pbls.attribute15
8671                                  ,pbls.raw_cost_source
8672                                  ,pbls.burdened_cost_source
8673                                  ,pbls.quantity_source
8674                                  ,pbls.revenue_source
8675                                  ,pbls.pm_product_code
8676                                  ,pbls.pm_budget_line_reference
8677                                  ,pbls.cost_rejection_code
8678                                  ,pbls.revenue_rejection_code
8679                                  ,pbls.burden_rejection_code
8680                                  ,pbls.other_rejection_code
8681                                  ,pbls.code_combination_id
8682                                  ,pbls.ccid_gen_status_code
8683                                  ,pbls.ccid_gen_rej_message
8684                                  ,pbls.request_id
8685                                  ,pbls.borrowed_revenue
8686                                  ,pbls.tp_revenue_in
8687                                  ,pbls.tp_revenue_out
8688                                  ,pbls.revenue_adj
8689                                  ,pbls.lent_resource_cost
8690                                  ,pbls.tp_cost_in
8691                                  ,pbls.tp_cost_out
8692                                  ,pbls.cost_adj
8693                                  ,pbls.unassigned_time_cost
8694                                  ,pbls.utilization_percent
8695                                  ,pbls.utilization_hours
8696                                  ,pbls.utilization_adj
8697                                  ,pbls.capacity
8698                                  ,pbls.head_count
8699                                  ,pbls.head_count_adj
8700                                  ,pbls.projfunc_currency_code
8701                                  ,pbls.projfunc_cost_rate_type
8702                                  ,pbls.projfunc_cost_exchange_rate
8703                                  ,pbls.projfunc_cost_rate_date_type
8704                                  ,pbls.projfunc_cost_rate_date
8705                                  ,pbls.projfunc_rev_rate_type
8706                                  ,pbls.projfunc_rev_exchange_rate
8707                                  ,pbls.projfunc_rev_rate_date_type
8708                                  ,pbls.projfunc_rev_rate_date
8709                                  ,pbls.project_currency_code
8710                                  ,pbls.project_cost_rate_type
8711                                  ,pbls.project_cost_exchange_rate
8712                                  ,pbls.project_cost_rate_date_type
8713                                  ,pbls.project_cost_rate_date
8714                                  ,pbls.project_raw_cost
8715                                  ,pbls.project_burdened_cost
8716                                  ,pbls.project_rev_rate_type
8717                                  ,pbls.project_rev_exchange_rate
8718                                  ,pbls.project_rev_rate_date_type
8719                                  ,pbls.project_rev_rate_date
8720                                  ,pbls.project_revenue
8721                                  ,pbls.txn_currency_code
8722                                  ,pbls.txn_raw_cost
8723                                  ,pbls.txn_burdened_cost
8724                                  ,pbls.txn_revenue
8725                                  ,pbls.bucketing_period_code
8726                                  ,pbls.budget_line_id
8727                                  ,pbls.budget_version_id
8728                                  ,pbls.txn_standard_cost_rate
8729                                  ,pbls.txn_cost_rate_override
8730                                  ,pbls.cost_ind_compiled_set_id
8731                                  ,pbls.txn_standard_bill_rate
8732                                  ,pbls.txn_bill_rate_override
8733                                  ,pbls.txn_markup_percent
8734                                  ,pbls.txn_markup_percent_override
8735                                  ,pbls.txn_discount_percentage
8736                                  ,pbls.transfer_price_rate
8737                                  ,pbls.burden_cost_rate
8738                                  ,pbls.burden_cost_rate_override
8739                                  ,pbls.pc_cur_conv_rejection_code
8740                                  ,pbls.pfc_cur_conv_rejection_code
8741                                  ,pras.resource_assignment_id
8742                                  ,pras.task_id
8743                                  ,tmp4.resource_list_member_id
8744                              FROM   pa_resource_assignments pras,
8745                                   pa_res_list_map_tmp4 tmp4,
8746                                   pa_budget_lines pbls
8747                              WHERE  tmp4.txn_source_id=pras.resource_assignment_id
8748                              AND    pbls.resource_assignment_id=pras.resource_assignment_id) pbls
8749                       where  get_mapped_dml_code(get_task_id(l_targ_plan_level_code,pbls.task_id),pbls.resource_list_member_id)='UPDATE'
8750                       and    pblt.budget_version_id(+) = p_budget_version_id
8751                       and    pblt.resource_assignment_id(+)=get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id)
8752                       AND    pblt.txn_currency_code(+)=DECODE(l_copy_pfc_for_txn_amt_flag,'Y', l_projfunc_currency_code,
8753                                                     DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code))
8754                       AND    pblt.start_date(+)=pbls.start_date
8755                       --IPM Arch Enhancement Bug 4865563
8756                     /*and    pbls.cost_rejection_code IS NULL
8757                       and    pbls.revenue_rejection_code IS NULL
8758                       and    pbls.burden_rejection_code IS NULL
8759                       and    pbls.other_rejection_code IS NULL
8760                       and    pbls.pc_cur_conv_rejection_code IS NULL
8761                       and    pbls.pfc_cur_conv_rejection_code IS NULL*/
8762                       and    pbls.start_date >= nvl(l_etc_start_date,pbls.start_date)
8763                       GROUP BY get_mapped_ra_id(get_task_id(l_targ_plan_level_code,pbls.task_id), pbls.resource_list_member_id), DECODE(l_same_multi_curr_flag, 'Y', pbls.txn_currency_code,l_project_currency_code)
8764                       ,pbls.start_date,pbls.end_date,pbls.period_name,pblt.resource_assignment_id,pblt.txn_currency_code,pblt.start_date;
8765 
8766                       --dbms_output.put_line('I41');
8767 
8768                   END IF;
8769                   END IF;
8770                 END IF;
8771 
8772 
8773                 --Prepare a pl/sql table equal in length to l_upd_ra_bl_dml_code_tbl which contains the rate based flag
8774                 --for the resource assignment to which the budget line corresponds. This will help in populating
8775                 --correct values in quantity and rate columns of budget lines.Bug 3621847
8776                 IF l_upd_ra_bl_dml_code_tbl.COUNT>0 THEN
8777 
8778                       l_bl_rbf_flag_tbl :=  SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
8779                       l_bl_rbf_flag_tbl.EXTEND(l_bl_RESOURCE_ASIGNMENT_ID_tbl.COUNT);
8780                       --dbms_output.put_line('I42');
8781                       FOR KK IN l_bl_RESOURCE_ASIGNMENT_ID_tbl.FIRST..l_bl_RESOURCE_ASIGNMENT_ID_tbl.LAST LOOP
8782 
8783                         FOR jj IN l_targ_ra_id_tbl.FIRST..l_targ_ra_id_tbl.LAST LOOP
8784 
8785                             IF l_bl_RESOURCE_ASIGNMENT_ID_tbl(kk)=l_targ_ra_id_tbl(jj) THEN
8786 
8787                               l_bl_rbf_flag_tbl(kk):=l_targ_rate_based_flag_tbl(jj);
8788 
8789                               EXIT;
8790 
8791                             END IF;
8792 
8793                         END LOOP;
8794 
8795                       END LOOP;
8796 
8797                       --Round the amounts prepared in the pl/sql tbls in case of partial implementation
8798 
8799                       IF l_partial_factor <> 1 THEN
8800 
8801                           IF l_debug_mode = 'Y' THEN
8802                                pa_debug.g_err_stage:='Fetching the agreement details';
8803                                pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
8804                           END IF;
8805                           -- Select agreement currency code
8806                           SELECT agr.agreement_id,
8807                                  agr.agreement_currency_code
8808                           INTO   l_agreement_id,
8809                                  l_agreement_currency_code
8810                           FROM   pa_budget_versions cibv,
8811                                  pa_agreements_all  agr
8812                           WHERE  cibv.budget_version_id = l_src_ver_id_tbl(j)
8813                           AND    cibv.agreement_id = agr.agreement_id;
8814 
8815                           IF l_debug_mode = 'Y' THEN
8816                                pa_debug.g_err_stage:='Calling pa_fp_multi_currency_pkg.round_amounts';
8817                                pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level3);
8818                           END IF;
8819 
8820                           pa_fp_multi_currency_pkg.round_amounts
8821                             ( px_quantity_tbl               => l_bl_QUANTITY_tbl
8822                              ,p_agr_currency_code           => l_agreement_currency_code
8823                              ,px_txn_raw_cost_tbl           => l_bl_TXN_RAW_COST_tbl
8824                              ,px_txn_burdened_cost_tbl      => l_bl_TXN_BURDENED_COST_tbl
8825                              ,px_txn_revenue_tbl            => l_bl_TXN_REVENUE_tbl
8826                              ,p_project_currency_code       => l_Project_Currency_Code
8827                              ,px_project_raw_cost_tbl       => l_bl_PROJECT_RAW_COST_tbl
8828                              ,px_project_burdened_cost_tbl  => l_bl_PROJECT_BURDENED_COST_tbl
8829                              ,px_project_revenue_tbl        => l_bl_PROJECT_REVENUE_tbl
8830                              ,p_projfunc_currency_code      => l_Projfunc_Currency_Code
8831                              ,px_projfunc_raw_cost_tbl      => l_bl_RAW_COST_tbl
8832                              ,px_projfunc_burdened_cost_tbl => l_bl_BURDENED_COST_tbl
8833                              ,px_projfunc_revenue_tbl       => l_bl_REVENUE_tbl
8834                              ,x_return_status               => x_return_status
8835                              ,x_msg_count                   => x_msg_count
8836                              ,x_msg_data                    => x_msg_data    );
8837 
8838                           IF x_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
8839 
8840                               IF P_PA_debug_mode = 'Y' THEN
8841                                    pa_debug.g_err_stage:= 'pa_fp_multi_currency_pkg.round_amounts returned error';
8842                                    pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
8843                               END IF;
8844                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8845 
8846                           END IF;
8847 
8848                        END IF; --IF l_partial_factor <> 1
8849 
8850                       --dbms_output.put_line('I43');
8851                       FORALL kk in 1..l_upd_ra_bl_dml_code_tbl.count
8852                         INSERT INTO PA_BUDGET_LINES(RESOURCE_ASSIGNMENT_ID,
8853                                    START_DATE,
8854                                    LAST_UPDATE_DATE,
8855                                    LAST_UPDATED_BY,
8856                                    CREATION_DATE,
8857                                    CREATED_BY,
8858                                    LAST_UPDATE_LOGIN,
8859                                    END_DATE,
8860                                    PERIOD_NAME,
8861                                    QUANTITY,
8862                                    RAW_COST,
8863                                    BURDENED_COST,
8864                                    REVENUE,
8865                                    CHANGE_REASON_CODE,
8866                                    DESCRIPTION,
8867                                    ATTRIBUTE_CATEGORY,
8868                                    ATTRIBUTE1,
8869                                    ATTRIBUTE2,
8870                                    ATTRIBUTE3,
8871                                    ATTRIBUTE4,
8872                                    ATTRIBUTE5,
8873                                    ATTRIBUTE6,
8874                                    ATTRIBUTE7,
8875                                    ATTRIBUTE8,
8876                                    ATTRIBUTE9,
8877                                    ATTRIBUTE10,
8878                                    ATTRIBUTE11,
8879                                    ATTRIBUTE12,
8880                                    ATTRIBUTE13,
8881                                    ATTRIBUTE14,
8882                                    ATTRIBUTE15,
8883                                    RAW_COST_SOURCE,
8884                                    BURDENED_COST_SOURCE,
8885                                    QUANTITY_SOURCE,
8886                                    REVENUE_SOURCE,
8887                                    PM_PRODUCT_CODE,
8888                                    PM_BUDGET_LINE_REFERENCE,
8889                                    COST_REJECTION_CODE,
8890                                    REVENUE_REJECTION_CODE,
8891                                    BURDEN_REJECTION_CODE,
8892                                    OTHER_REJECTION_CODE,
8893                                    CODE_COMBINATION_ID,
8894                                    CCID_GEN_STATUS_CODE,
8895                                    CCID_GEN_REJ_MESSAGE,
8896                                    REQUEST_ID,
8897                                    BORROWED_REVENUE,
8898                                    TP_REVENUE_IN,
8899                                    TP_REVENUE_OUT,
8900                                    REVENUE_ADJ,
8901                                    LENT_RESOURCE_COST,
8902                                    TP_COST_IN,
8903                                    TP_COST_OUT,
8904                                    COST_ADJ,
8905                                    UNASSIGNED_TIME_COST,
8906                                    UTILIZATION_PERCENT,
8907                                    UTILIZATION_HOURS,
8908                                    UTILIZATION_ADJ,
8909                                    CAPACITY,
8910                                    HEAD_COUNT,
8911                                    HEAD_COUNT_ADJ,
8912                                    PROJFUNC_CURRENCY_CODE,
8913                                    PROJFUNC_COST_RATE_TYPE,
8914                                    PROJFUNC_COST_EXCHANGE_RATE,
8915                                    PROJFUNC_COST_RATE_DATE_TYPE,
8916                                    PROJFUNC_COST_RATE_DATE,
8917                                    PROJFUNC_REV_RATE_TYPE,
8918                                    PROJFUNC_REV_EXCHANGE_RATE,
8919                                    PROJFUNC_REV_RATE_DATE_TYPE,
8920                                    PROJFUNC_REV_RATE_DATE,
8921                                    PROJECT_CURRENCY_CODE,
8922                                    PROJECT_COST_RATE_TYPE,
8923                                    PROJECT_COST_EXCHANGE_RATE,
8924                                    PROJECT_COST_RATE_DATE_TYPE,
8925                                    PROJECT_COST_RATE_DATE,
8926                                    PROJECT_RAW_COST,
8927                                    PROJECT_BURDENED_COST,
8928                                    PROJECT_REV_RATE_TYPE,
8929                                    PROJECT_REV_EXCHANGE_RATE,
8930                                    PROJECT_REV_RATE_DATE_TYPE,
8931                                    PROJECT_REV_RATE_DATE,
8932                                    PROJECT_REVENUE,
8933                                    TXN_CURRENCY_CODE,
8934                                    TXN_RAW_COST,
8935                                    TXN_BURDENED_COST,
8936                                    TXN_REVENUE,
8937                                    BUCKETING_PERIOD_CODE,
8938                                    BUDGET_LINE_ID,
8939                                    BUDGET_VERSION_ID,
8940                                    TXN_STANDARD_COST_RATE,
8941                                    TXN_COST_RATE_OVERRIDE,
8942                                    COST_IND_COMPILED_SET_ID,
8943                               --     TXN_BURDEN_MULTIPLIER,
8944                               --     TXN_BURDEN_MULTIPLIER_OVERRIDE,
8945                                    TXN_STANDARD_BILL_RATE,
8946                                    TXN_BILL_RATE_OVERRIDE,
8947                                    TXN_MARKUP_PERCENT,
8948                                    TXN_MARKUP_PERCENT_OVERRIDE,
8949                                    TXN_DISCOUNT_PERCENTAGE,
8950                                    TRANSFER_PRICE_RATE,
8951                                    BURDEN_COST_RATE,
8952                                    BURDEN_COST_RATE_OVERRIDE,
8953                                    PC_CUR_CONV_REJECTION_CODE,
8954                                    PFC_CUR_CONV_REJECTION_CODE
8955                                    )
8956                              SELECT l_bl_RESOURCE_ASIGNMENT_ID_tbl(kk),
8957                                   l_bl_START_DATE_tbl(kk),
8958                                   sysdate,
8959                                   fnd_global.user_id,
8960                                   sysdate,
8961                                   fnd_global.user_id,
8962                                   fnd_global.login_id,
8963                                   l_bl_END_DATE_tbl(kk),
8964                                   l_bl_PERIOD_NAME_tbl(kk),
8965                                   Decode(l_bl_rbf_flag_tbl(kk),
8966                                          'N',Decode(l_target_version_type,
8967                                                     'REVENUE',l_bl_TXN_REVENUE_tbl(kk),
8968                                                               l_bl_TXN_RAW_COST_tbl(kk)),
8969                                           l_bl_QUANTITY_tbl(kk)),
8970                                   l_bl_RAW_COST_tbl(kk),
8971                                   l_bl_BURDENED_COST_tbl(kk),
8972                                   l_bl_REVENUE_tbl(kk),
8973                                   l_bl_CHANGE_REASON_CODE_tbl(kk),
8974                                   l_bl_DESCRIPTION_tbl(kk),
8975                                   l_bl_ATTRIBUTE_CATEGORY_tbl(kk),
8976                                   l_bl_ATTRIBUTE1_tbl(kk),
8977                                   l_bl_ATTRIBUTE2_tbl(kk),
8978                                   l_bl_ATTRIBUTE3_tbl(kk),
8979                                   l_bl_ATTRIBUTE4_tbl(kk),
8980                                   l_bl_ATTRIBUTE5_tbl(kk),
8981                                   l_bl_ATTRIBUTE6_tbl(kk),
8982                                   l_bl_ATTRIBUTE7_tbl(kk),
8983                                   l_bl_ATTRIBUTE8_tbl(kk),
8984                                   l_bl_ATTRIBUTE9_tbl(kk),
8985                                   l_bl_ATTRIBUTE10_tbl(kk),
8986                                   l_bl_ATTRIBUTE11_tbl(kk),
8987                                   l_bl_ATTRIBUTE12_tbl(kk),
8988                                   l_bl_ATTRIBUTE13_tbl(kk),
8989                                   l_bl_ATTRIBUTE14_tbl(kk),
8990                                   l_bl_ATTRIBUTE15_tbl(kk),
8991                                   'I',
8992                                   'I',
8993                                   'I',
8994                                   'I',
8995                                   l_bl_PM_PRODUCT_CODE_tbl(kk),
8996                                   l_bl_PM_BUDGET_LINE_REF_tbl(kk),
8997                                   l_bl_COST_REJECTION_CODE_tbl(kk),
8998                                   l_bl_REVENUE_REJ_CODE_tbl(kk),
8999                                   l_bl_BURDEN_REJECTION_CODE_tbl(kk),
9000                                   l_bl_OTHER_REJECTION_CODE_tbl(kk),
9001                                   l_bl_CODE_COMBINATION_ID_tbl(kk),
9002                                   l_bl_CCID_GEN_STATUS_CODE_tbl(kk),
9003                                   l_bl_CCID_GEN_REJ_MESSAGE_tbl(kk),
9004                                   l_bl_REQUEST_ID_tbl(kk),
9005                                   l_bl_BORROWED_REVENUE_tbl(kk),
9006                                   l_bl_TP_REVENUE_IN_tbl(kk),
9007                                   l_bl_TP_REVENUE_OUT_tbl(kk),
9008                                   l_bl_REVENUE_ADJ_tbl(kk),
9009                                   l_bl_LENT_RESOURCE_COST_tbl(kk),
9010                                   l_bl_TP_COST_IN_tbl(kk),
9011                                   l_bl_TP_COST_OUT_tbl(kk),
9012                                   l_bl_COST_ADJ_tbl(kk),
9013                                   l_bl_UNASSIGNED_TIME_COST_tbl(kk),
9014                                   l_bl_UTILIZATION_PERCENT_tbl(kk),
9015                                   l_bl_UTILIZATION_HOURS_tbl(kk),
9016                                   l_bl_UTILIZATION_ADJ_tbl(kk),
9017                                   l_bl_CAPACITY_tbl(kk),
9018                                   l_bl_HEAD_COUNT_tbl(kk),
9019                                   l_bl_HEAD_COUNT_ADJ_tbl(kk),
9020                                   l_bl_PROJFUNC_CUR_CODE_tbl(kk),
9021                                   l_bl_PROJFUNC_COST_RAT_TYP_tbl(kk),
9022                                   DECODE(l_bl_PROJFUNC_COST_RAT_TYP_tbl(kk),'User', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9023                                           'B', nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0)),0,0,(decode(l_report_cost_using,'R',nvl(l_bl_RAW_COST_tbl(kk),0),
9024                                           'B',nvl(l_bl_BURDENED_COST_tbl(kk),0)) / (decode(l_report_cost_using,'R',nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9025                                           'B', nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0))))),Null),Null), -- Bug 3839273
9026                                   l_bl_PJFN_COST_RAT_DAT_TYP_tbl(kk),
9027                                   l_bl_PROJFUNC_COST_RAT_DAT_tbl(kk),
9028                                   l_bl_PROJFUNC_REV_RATE_TYP_tbl(kk),
9029                                   Decode(l_bl_PROJFUNC_REV_RATE_TYP_tbl(kk),'User',DECODE(l_targ_multi_curr_flag,'Y', Decode(nvl(l_bl_TXN_REVENUE_tbl(kk),0),0,0,nvl(l_bl_REVENUE_tbl(kk),0) /
9030                                          nvl(l_bl_TXN_REVENUE_tbl(kk),0)),Null),Null), -- Bug 3839273
9031                                   l_bl_PJFN_REV_RAT_DAT_TYPE_tbl(kk),
9032                                   l_bl_PROJFUNC_REV_RAT_DATE_tbl(kk),
9033                                   l_project_currency_code,
9034                                   l_bl_PROJECT_COST_RAT_TYPE_tbl(kk),
9035                                   DECODE(l_bl_PROJECT_COST_RAT_TYPE_tbl(kk),'User', DECODE(l_targ_multi_curr_flag,'Y', Decode(decode(l_report_cost_using, 'R',nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9036                                         'B', nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0)),0,0,(decode(l_report_cost_using,'R',nvl(l_bl_PROJECT_RAW_COST_tbl(kk),0),
9037                                         'B',nvl(l_bl_PROJECT_BURDENED_COST_tbl(kk),0)) / (decode(l_report_cost_using,'R',nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9038                                         'B',nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0))))),Null),Null), -- Bug 3839273
9039                                   l_bl_PROJ_COST_RAT_DAT_TYP_tbl(kk),
9040                                   l_bl_PROJ_COST_RATE_DATE_tbl(kk),
9041                                   l_bl_PROJECT_RAW_COST_tbl(kk),
9042                                   l_bl_PROJECT_BURDENED_COST_tbl(kk),
9043                                   l_bl_PROJECT_REV_RATE_TYPE_tbl(kk),
9044                                   Decode(l_bl_PROJECT_REV_RATE_TYPE_tbl(kk),'User',DECODE(l_targ_multi_curr_flag,'Y', Decode(nvl(l_bl_TXN_REVENUE_tbl(kk),0),0,0,nvl(l_bl_PROJECT_REVENUE_tbl(kk),0) /
9045                                          nvl(l_bl_TXN_REVENUE_tbl(kk),0)),Null),Null), -- Bug 3839273
9046                                   l_bl_PRJ_REV_RAT_DATE_TYPE_tbl(kk),
9047                                   l_bl_PROJECT_REV_RATE_DATE(kk),
9048                                   l_bl_PROJECT_REVENUE_tbl(kk),
9049                                   l_bl_TXN_CURRENCY_CODE_tbl(kk),
9050                                   l_bl_TXN_RAW_COST_tbl(kk),
9051                                   l_bl_TXN_BURDENED_COST_tbl(kk),
9052                                   l_bl_TXN_REVENUE_tbl(kk),
9053                                   l_bl_BUCKETING_PERIOD_CODE_tbl(kk),
9054                                   pa_budget_lines_s.nextval,
9055                                   p_budget_version_id,
9056                                   l_bl_TXN_STD_COST_RATE_tbl(kk),
9057                                   DECODE(l_target_version_type,
9058                                        'REVENUE',l_bl_TXN_COST_RATE_OVERIDE_tbl(kk),
9059                                         DECODE(l_bl_rbf_flag_tbl(kk),
9060                                                'N',1,
9061                                                l_bl_TXN_COST_RATE_OVERIDE_tbl(kk))),
9062                                   l_bl_COST_IND_CMPLD_SET_ID_tbl(kk),
9063                               --      l_bl_TXN_BURDEN_MULTIPLIER_tbl(kk),
9064                               --      l_bl_TXN_BRD_MLTIPLI_OVRID_tbl(kk),
9065                                   l_bl_TXN_STD_BILL_RATE_tbl(kk),
9066                                   DECODE(l_target_version_type,
9067                                          'REVENUE',DECODE(l_bl_rbf_flag_tbl(kk),
9068                                                           'N',1,
9069                                                            l_bl_TXN_BILL_RATE_OVERRID_tbl(kk)),
9070                                          l_bl_TXN_BILL_RATE_OVERRID_tbl(kk)),
9071                                   l_bl_TXN_MARKUP_PERCENT_tbl(kk),
9072                                   l_bl_TXN_MRKUP_PER_OVERIDE_tbl(kk),
9073                                   l_bl_TXN_DISC_PERCENTAGE_tbl(kk),
9074                                   l_bl_TRANSFER_PRICE_RATE_tbl(kk),
9075                                   l_bl_BURDEN_COST_RATE_tbl(kk),
9076                                   DECODE(l_target_version_type,
9077                                        'REVENUE',l_bl_BURDEN_COST_RAT_OVRID_tbl(kk),
9078                                         DECODE(l_bl_rbf_flag_tbl(kk),
9079                                              'Y',l_bl_BURDEN_COST_RAT_OVRID_tbl(kk),
9080                                              DECODE(nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9081                                                     0,null,
9082                                                     l_bl_TXN_BURDENED_COST_tbl(kk)/l_bl_TXN_RAW_COST_tbl(kk)))),
9083                                   l_bl_PC_CUR_CONV_REJ_CODE_tbl(kk),
9084                                   l_bl_PFC_CUR_CONV_REJ_CODE_tbl(kk)
9085                              from   dual
9086                              where  l_upd_ra_bl_dml_code_tbl(kk)='INSERT';
9087 
9088                    --dbms_output.put_line('I43');
9089 
9090                    --Fix for bug 3734888. Null handled the pl/sql tbls while updating.
9091                    FORALL kk in 1..l_upd_ra_bl_dml_code_tbl.count
9092                       UPDATE PA_BUDGET_LINES
9093                       SET    LAST_UPDATE_DATE=sysdate,
9094                            LAST_UPDATED_BY=fnd_global.user_id,
9095                            LAST_UPDATE_LOGIN=fnd_global.login_id,
9096                            QUANTITY= DECODE(l_bl_rbf_flag_tbl(kk),
9097                                             'N',DECODE(l_target_version_type,
9098                                                        'REVENUE',nvl(TXN_REVENUE,0)+ nvl(l_bl_TXN_REVENUE_tbl(kk),0)
9099                                                                 ,nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0)),
9100                                             nvl(QUANTITY,0)+ nvl(l_bl_QUANTITY_tbl(kk),0)),
9101                            RAW_COST= nvl(RAW_COST,0) + nvl(l_bl_RAW_COST_tbl(kk),0),
9102                            BURDENED_COST= nvl(BURDENED_COST,0) + nvl(l_bl_BURDENED_COST_tbl(kk),0),
9103                            REVENUE= nvl(REVENUE,0) + nvl(l_bl_REVENUE_tbl(kk),0),
9104                            PROJFUNC_COST_RATE_TYPE= nvl(l_bl_PROJFUNC_COST_RAT_TYP_tbl(kk),PROJFUNC_COST_RATE_TYPE),
9105                            PROJFUNC_COST_EXCHANGE_RATE= DECODE(nvl(l_bl_PROJFUNC_COST_RAT_TYP_tbl(kk),PROJFUNC_COST_RATE_TYPE),'User', Decode(decode(l_report_cost_using, 'R',nvl(nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0),0),
9106                                                        'B',nvl(nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0),0) ),0,0,
9107                                                        (decode(l_report_cost_using,'R',nvl(nvl(RAW_COST,0) + nvl(l_bl_RAW_COST_tbl(kk),0),0),
9108                                                                            'B',nvl(nvl(BURDENED_COST,0) + nvl(l_bl_BURDENED_COST_tbl(kk),0),0)) / decode(l_report_cost_using,'R', nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9109                                                                                                     'B', nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0)))),PROJFUNC_COST_EXCHANGE_RATE),
9110                            PROJFUNC_REV_RATE_TYPE= nvl(l_bl_PROJFUNC_REV_RATE_TYP_tbl(kk),PROJFUNC_REV_RATE_TYPE),
9111                            PROJFUNC_REV_EXCHANGE_RATE= DECODE(nvl(l_bl_PROJFUNC_REV_RATE_TYP_tbl(kk),PROJFUNC_REV_RATE_TYPE),'User',
9112                                                       Decode(nvl(nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0),0),0,0,
9113                                                 (nvl(nvl(REVENUE,0) + nvl(l_bl_REVENUE_tbl(kk),0),0) /(nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0)))),PROJFUNC_REV_EXCHANGE_RATE),
9114                            PROJECT_COST_RATE_TYPE= nvl(l_bl_PROJECT_COST_RAT_TYPE_tbl(kk),PROJECT_COST_RATE_TYPE),
9115                            PROJECT_COST_EXCHANGE_RATE= DECODE(nvl(l_bl_PROJECT_COST_RAT_TYPE_tbl(kk),PROJECT_COST_RATE_TYPE),'User', Decode(decode(l_report_cost_using, 'R',nvl(nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0),0),
9116                                                        'B',nvl(nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0),0) ),0,0,
9117                                                        (decode(l_report_cost_using,'R',nvl(nvl(PROJECT_RAW_COST,0) + nvl(l_bl_PROJECT_RAW_COST_tbl(kk),0),0),
9118                                                                            'B',nvl(nvl(PROJECT_BURDENED_COST,0) + nvl(l_bl_PROJECT_BURDENED_COST_tbl(kk),0),0)) / decode(l_report_cost_using,
9119                                                                                                     'R', nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9120                                                                                                     'B', nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0)))),PROJECT_COST_EXCHANGE_RATE),
9121                            PROJECT_RAW_COST = nvl(PROJECT_RAW_COST,0) + nvl(l_bl_PROJECT_RAW_COST_tbl(kk),0),
9122                            PROJECT_BURDENED_COST = nvl(PROJECT_BURDENED_COST,0) + nvl(l_bl_PROJECT_BURDENED_COST_tbl(kk),0),
9123                            PROJECT_REV_RATE_TYPE = nvl(l_bl_PROJECT_REV_RATE_TYPE_tbl(kk),PROJECT_REV_RATE_TYPE),
9124                            PROJECT_REV_EXCHANGE_RATE =  DECODE(nvl(l_bl_PROJECT_REV_RATE_TYPE_tbl(kk),PROJECT_REV_RATE_TYPE),'User',
9125                                                       Decode(nvl(nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0),0),0,0,
9126                                                       (nvl(nvl(PROJECT_REVENUE,0) + nvl(l_bl_PROJECT_REVENUE_tbl(kk),0),0) /(nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0)))),PROJECT_REV_EXCHANGE_RATE),
9127                            PROJECT_REVENUE =  nvl(PROJECT_REVENUE,0) + nvl(l_bl_PROJECT_REVENUE_tbl(kk),0),
9128                            TXN_RAW_COST =  nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0),
9129                            TXN_BURDENED_COST= nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0),
9130                            TXN_REVENUE =  nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0),
9131                            TXN_COST_RATE_OVERRIDE = DECODE(l_target_Version_type,
9132                                                    'REVENUE', TXN_COST_RATE_OVERRIDE,
9133                                                    DECODE(l_bl_rbf_flag_tbl(kk),
9134                                                          'N',1,
9135                                                          decode((nvl(QUANTITY,0) + nvl(l_bl_QUANTITY_tbl(kk),0)),0,0,((nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0))/
9136                                                               (nvl(QUANTITY,0) + nvl(l_bl_QUANTITY_tbl(kk),0)))))),
9137                            BURDEN_COST_RATE_OVERRIDE = DECODE( l_target_Version_type,
9138                                                        'REVENUE',BURDEN_COST_RATE_OVERRIDE,
9139                                                        DECODE(l_bl_rbf_flag_tbl(kk),
9140                                                             'N',decode((nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0)),0,0,((nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0))/
9141                                                                    (nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0)))),
9142                                                              decode((nvl(QUANTITY,0) + nvl(l_bl_QUANTITY_tbl(kk),0)),0,0,((nvl(TXN_BURDENED_COST,0) + nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0))/
9143                                                             (nvl(QUANTITY,0) + nvl(l_bl_QUANTITY_tbl(kk),0)))))),
9144                            TXN_BILL_RATE_OVERRIDE =  DECODE(l_bl_rbf_flag_tbl(kk),
9145                                                             'N',DECODE(l_target_version_type,
9146                                                                        'REVENUE',1,
9147                                                                         decode((nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0)),0,0,((nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0))/
9148                                                                                    (nvl(TXN_RAW_COST,0) + nvl(l_bl_TXN_RAW_COST_tbl(kk),0))))),
9149                                                             decode((nvl(QUANTITY,0) + nvl(l_bl_QUANTITY_tbl(kk),0)),0,0,((nvl(TXN_REVENUE,0) + nvl(l_bl_TXN_REVENUE_tbl(kk),0))/
9150                                                                    (nvl(QUANTITY,0) + nvl(l_bl_QUANTITY_tbl(kk),0)))))
9151                        WHERE l_upd_ra_bl_dml_code_tbl(kk) = 'UPDATE'
9152                        AND   resource_assignment_id       = l_bl_RESOURCE_ASIGNMENT_ID_tbl(kk)
9153                        AND   start_date                   = l_bl_START_DATE_tbl(kk)
9154                        AND   txn_currency_code            = l_bl_TXN_CURRENCY_CODE_tbl(kk)
9155                        RETURNING
9156                        period_name,
9157                        txn_currency_code,
9158                        start_date,
9159                        end_date,
9160                        cost_rejection_code,
9161                        revenue_rejection_code,
9162                        burden_rejection_code,
9163                        other_rejection_code,
9164                        pc_cur_conv_rejection_code,
9165                        pfc_cur_conv_rejection_code,
9166                        budget_line_id
9167                        BULK COLLECT INTO
9168                        l_upd_period_name_tbl,
9169                        l_upd_currency_code_tbl,
9170                        l_upd_bl_start_date_tbl,
9171                        l_upd_bl_end_date_tbl,
9172                        l_upd_cost_rejection_code,
9173                        l_upd_revenue_rejection_code,
9174                        l_upd_burden_rejection_code,
9175                        l_upd_other_rejection_code,
9176                        l_upd_pc_cur_conv_rej_code,
9177                        l_upd_pfc_cur_conv_rej_code,
9178                        l_upd_bl_id_tbl;
9179 
9180 
9181                    --dbms_output.put_line('I44');
9182 
9183                 END IF;--   IF l_upd_ra_bl_dml_code_tbl.COUNT>0 THEN
9184 
9185                 -- Bug 4035856 Call rounding api if partial implementation has happened
9186                 IF  l_partial_factor <> 1 THEN
9187                     -- Call rounding api
9188                     PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts
9189                            (  p_budget_version_id     => p_budget_version_id
9190                              ,p_calling_context       => 'CHANGE_ORDER_MERGE'
9191                              ,p_bls_inserted_after_id => l_id_before_bl_insertion
9192                              ,x_return_status         => l_return_status
9193                              ,x_msg_count             => l_msg_count
9194                              ,x_msg_data              => l_msg_data);
9195 
9196                      IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9197                          IF P_PA_debug_mode = 'Y' THEN
9198                               pa_debug.g_err_stage:= 'Error in PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts';
9199                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9200                          END IF;
9201                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9202                      END IF;
9203                      IF l_debug_mode = 'Y' THEN
9204                          pa_debug.g_err_stage:='Start of residual amount logic';
9205                          pa_debug.write('Round_Budget_Line_Amounts: ' || l_module_name,pa_debug.g_err_stage,3);
9206                      END IF;
9207 
9208                      Begin
9209 
9210                          -- Compute the total revenue sum of target budget version in PC,PFC (TXN=PFC)
9211                          SELECT nvl(sum(nvl(revenue,0)),0), nvl(sum(nvl(project_revenue,0)),0)
9212                          INTO   l_targ_pfc_rev_after_merge, l_targ_pc_rev_after_merge
9213                          FROM   pa_budget_lines
9214                          WHERE  budget_version_id = p_budget_version_id;
9215 
9216                          -- Initialise residual amount variables to zero
9217                          l_pc_revenue_delta := 0;
9218                          l_pfc_revenue_delta := 0;
9219 
9220                          -- Compute the total implemented amount using already implemented amount and currently
9221                          -- implemented amount
9222                          l_pc_rev_merged := l_impl_proj_revenue -- already impl amt from pa_fp_merged_ctrl_items
9223                                             +  (l_targ_pc_rev_after_merge - l_targ_pc_rev_before_merge);
9224                          l_pfc_rev_merged := l_impl_proj_func_revenue -- already impl amt from pa_fp_merged_ctrl_items
9225                                             +  (l_targ_pfc_rev_after_merge - l_targ_pfc_rev_before_merge);
9226 
9227                          -- If all the remaining agreement amount is being implemented, make sure that
9228                          -- implemeted PC, PFC amounts in pa_fp_merged_ctrl_items tally with ci version totals
9229                          IF (nvl(l_impl_amt,0) + nvl(l_partial_impl_rev_amt,0) = nvl(l_total_amt,0)) THEN
9230 
9231                             l_pfc_revenue_delta := l_total_amt_in_pfc - l_pfc_rev_merged;
9232                             l_pc_revenue_delta := l_total_amt_in_pc - l_pc_rev_merged;
9233 
9234                          ELSE
9235 
9236                             --Find out the PC, PFC amount that should have got merged. This will be calculated as follows:
9237                             --In the CI version, let us say, TotTxnRev is the total revenue in txn currency, TotPfcRev is the
9238                             --total revenue in PFC, ParTxnRev is the amount that the user has entered for implementation and
9239                             --ParPfcRev is the amount in PFC corresponding to ParTxnRev. If TotPfcRev corresponds to TotTxnRev
9240                             --in PFC then ParPfcRev is nothging but ((ParTxnRev + ImplTxnRev) *TotPfcRev)/TotTxnRev. Here ImplTxnRev is
9241                             --the revenue amount in txn currency that has already been implemented
9242                             --Similary Partial amount in Project currency can also be calculated.
9243                             IF l_total_amt_in_pfc = 0 THEN
9244 
9245                                 l_pfc_rev_for_merge := 0;
9246 
9247                             ELSE
9248 
9249                                 --(l_partial_impl_rev_amt + nvl(l_impl_amt,0) would constitute the total amount in
9250                                 --txn currency of the source version that has got implemented.
9251                                 l_pfc_rev_for_merge := ( (l_partial_impl_rev_amt + nvl(l_impl_amt,0)) * l_total_amt_in_pfc )/l_total_amt;
9252                                 l_pfc_rev_for_merge :=
9253                                    Pa_currency.round_trans_currency_amt1(l_pfc_rev_for_merge,
9254                                                                          l_projfunc_currency_code);
9255 
9256                             END IF;
9257 
9258                             IF l_total_amt_in_pc = 0 THEN
9259 
9260                                 l_pc_rev_for_merge := 0;
9261 
9262                             ELSE
9263 
9264                                 l_pc_rev_for_merge := ((l_partial_impl_rev_amt + nvl(l_impl_amt,0)) * l_total_amt_in_pc )/l_total_amt;
9265                                 l_pc_rev_for_merge :=
9266                                    Pa_currency.round_trans_currency_amt1(l_pc_rev_for_merge,
9267                                                                          l_project_currency_code);
9268 
9269                             END IF;
9270 
9271                             l_pfc_revenue_delta := l_pfc_rev_for_merge - l_pfc_rev_merged;
9272                             l_pc_revenue_delta := l_pc_rev_for_merge - l_pc_rev_merged;
9273 
9274                          END IF;
9275                          IF l_debug_mode = 'Y' THEN
9276                              pa_debug.g_err_stage:='l_pfc_revenue_delta = '||l_pfc_revenue_delta
9277                                                    || 'l_pc_revenue_delta = ' || l_pc_revenue_delta;
9278                              pa_debug.write('Round_Budget_Line_Amounts: ' || l_module_name,pa_debug.g_err_stage,3);
9279                          END IF;
9280 
9281                          IF (l_pfc_revenue_delta <> 0 OR l_pc_revenue_delta <> 0) THEN
9282 
9283                              -- Find source resource assignment id, budget line start_date into which
9284                              -- the residual amount should be added
9285                              IF l_src_resource_list_id = l_targ_resource_list_id THEN
9286 
9287                                  OPEN   src_delta_amt_adj_ra_cur(l_src_ver_id_tbl(j));
9288                                  FETCH  src_delta_amt_adj_ra_cur INTO
9289                                          l_src_delta_amt_adj_task_id,
9290                                          l_targ_delta_amt_adj_rlm_id,
9291                                          l_src_delta_amt_adj_ra_id,
9292                                          l_src_dummy1,
9293                                          l_src_dummy2;
9294                                  CLOSE  src_delta_amt_adj_ra_cur;
9295 
9296                              ELSE
9297 
9298                                  OPEN   src_delta_amt_adj_ra_cur1(l_src_ver_id_tbl(j));
9299                                  FETCH  src_delta_amt_adj_ra_cur1 INTO
9300                                          l_src_delta_amt_adj_task_id,
9301                                          l_targ_delta_amt_adj_rlm_id,
9302                                          l_src_delta_amt_adj_ra_id,
9303                                          l_src_dummy1,
9304                                          l_src_dummy2;
9305                                  CLOSE  src_delta_amt_adj_ra_cur1;
9306 
9307                              END IF;
9308 
9309                              OPEN  src_delta_amt_adj_date_cur(l_src_delta_amt_adj_ra_id);
9310                              FETCH src_delta_amt_adj_date_cur
9311                              INTO  l_src_delta_amt_adj_start_date;
9312                              CLOSE src_delta_amt_adj_date_cur;
9313 
9314                              IF l_debug_mode = 'Y' THEN
9315                                  pa_debug.g_err_stage:= 'target ra id = '||get_mapped_ra_id(get_task_id(l_targ_plan_level_code,
9316                                                                  l_src_delta_amt_adj_task_id), l_targ_delta_amt_adj_rlm_id);
9317                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9318                                  pa_debug.g_err_stage:= 'l_src_delta_amt_adj_start_date = '||l_src_delta_amt_adj_start_date;
9319                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9320                                  pa_debug.g_err_stage:= 'l_targ_plan_level_code = '||l_targ_plan_level_code
9321                                           || 'l_src_delta_amt_adj_task_id = '||l_src_delta_amt_adj_task_id ;
9322                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9323                              END IF;
9324 
9325                              -- Using the source res assigment and target assignment mapping update
9326                              -- the target version budget line. Note for AR Versions there can be amounts
9327                              -- against txn currency only
9328                              UPDATE pa_budget_lines
9329                              SET    revenue = nvl(revenue,0) + nvl(l_pfc_revenue_delta,0),
9330                                     txn_revenue = nvl(revenue,0) + nvl(l_pfc_revenue_delta,0), -- TXN and PFC should be same for AR versions
9331                                     project_revenue = nvl(project_revenue,0) + nvl(l_pc_revenue_delta,0)
9332                              WHERE  resource_assignment_id =
9333                                       get_mapped_ra_id(get_task_id(l_targ_plan_level_code,l_src_delta_amt_adj_task_id),
9334                                                        l_targ_delta_amt_adj_rlm_id)
9335                              AND    l_src_delta_amt_adj_start_date  BETWEEN start_date AND end_date
9336                              AND    budget_version_id = p_budget_version_id
9337                              AND    rownum < 2 -- not really necessary
9338                              RETURNING
9339                              budget_line_id
9340                              INTO
9341                              l_rounded_bl_id;
9342 
9343                              IF SQL%ROWCOUNT = 0 THEN
9344                                  -- If no row is updated, target must be None time phased version.
9345                                  -- So there would be only one line
9346                                  UPDATE pa_budget_lines
9347                                  SET    revenue = nvl(revenue,0) + nvl(l_pfc_revenue_delta,0),
9348                                         txn_revenue = nvl(revenue,0) + nvl(l_pfc_revenue_delta,0), -- TXN and PFC should be same for AR versions
9349                                         project_revenue = nvl(project_revenue,0) + nvl(l_pc_revenue_delta,0)
9350                                  WHERE  resource_assignment_id =
9351                                           get_mapped_ra_id(get_task_id(l_targ_plan_level_code,l_src_delta_amt_adj_task_id),
9352                                                            l_targ_delta_amt_adj_rlm_id)
9353                                  AND    budget_version_id = p_budget_version_id
9354                                  AND    rownum < 2
9355                                  RETURNING
9356                                  budget_line_id
9357                                  INTO
9358                                  l_rounded_bl_id;
9359 
9360                              END IF;
9361 
9362                              --For non rate-based transaction, quantity should always be same as revenue. In the above update
9363                              --revenue is modified. If the revenue is adjusted for non rate-based transaction then the change
9364                              --should be made in quantity also. If the budget line is among those that are updated then in the
9365                              --below FOR Loop will that budget line would also get updated. If the budget line is among those
9366                              --budget lines that are inserted then it will be changed after the FOR loop. This variable
9367                              --l_qty_adjusted_flag will be used to track this
9368                              l_qty_adjusted_flag:='N';
9369 
9370                              --Since the amount is changed for a budget line in above update, the change has to be
9371                              --propogated to the corresponding entry of pl/sql tbls prepared above
9372                              FOR kk IN 1..l_upd_bl_id_tbl.COUNT LOOP
9373 
9374                                  IF l_upd_bl_id_tbl(kk)=l_rounded_bl_id THEN
9375 
9376                                      l_bl_REVENUE_tbl(kk) := nvl(l_bl_REVENUE_tbl(kk),0) + nvl(l_pfc_revenue_delta,0);
9377                                      l_bl_PROJECT_REVENUE_tbl(kk) := nvl(l_bl_PROJECT_REVENUE_tbl(kk),0) + nvl(l_pc_revenue_delta,0);
9378                                      l_bl_TXN_REVENUE_tbl(kk) := nvl(l_bl_TXN_REVENUE_tbl(kk),0) + nvl(l_pfc_revenue_delta,0); -- TXN and PFC should be same for AR versions
9379                                      --For non rate based transactions quantity should be same as revenue
9380                                      IF l_bl_rbf_flag_tbl(kk) = 'N' THEN
9381 
9382                                          l_bl_QUANTITY_tbl(kk) := nvl(l_bl_TXN_REVENUE_tbl(kk),0) + nvl(l_pfc_revenue_delta,0);
9383 
9384                                          UPDATE pa_budget_lines
9385                                          SET    quantity=txn_revenue
9386                                          WHERE  budget_line_id=l_rounded_bl_id;
9387 
9388                                          l_qty_adjusted_flag:='Y';
9389 
9390                                      END IF;
9391 
9392                                      EXIT;
9393 
9394                                  END IF;
9395 
9396                              END LOOP;
9397 
9398                              IF l_qty_adjusted_flag = 'N' THEN
9399 
9400                                 SELECT pra.rate_based_flag
9401                                 INTO   l_rounded_bl_rbf
9402                                 FROM   pa_resource_assignments pra,
9403                                        pa_budget_lines pbl
9404                                 WHERE  pra.resource_assignment_id = pbl.resource_assignment_id
9405                                 AND    pbl.budget_line_id = l_rounded_bl_id;
9406 
9407                                 IF l_rounded_bl_rbf ='N' THEN
9408 
9409                                     UPDATE pa_budget_lines
9410                                     SET    quantity=txn_revenue
9411                                     WHERE  budget_line_id=l_rounded_bl_id;
9412 
9413                                 END IF;
9414 
9415                              END IF;
9416 
9417 
9418                          END IF;
9419                      Exception
9420                          WHEN OTHERS THEN
9421                              IF P_PA_DEBUG_MODE = 'Y' THEN
9422                               pa_debug.g_err_stage:='Error in residual amount adjust logic'||SQLERRM;
9423                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9424                              END IF;
9425                              RAISE;
9426                      End;
9427 
9428                      IF l_debug_mode = 'Y' THEN
9429                         pa_debug.g_err_stage:='End of residual amount logic';
9430                         pa_debug.write('Round_Budget_Line_Amounts: ' || l_module_name,pa_debug.g_err_stage,3);
9431                      END IF;
9432 
9433                 END IF;
9434 
9435                 --dbms_output.put_line('I45');
9436                 -- Needs to execute only when calculate API has not been called
9437                 IF NOT (l_src_time_phased_code = 'N' AND (l_targ_time_phased_code = 'P' OR l_targ_time_phased_code = 'G')) THEN
9438 
9439                      /* Bug 5726773:
9440  	                 Start of coding done for Support of negative quantity/amounts enhancement.
9441  	                 Call to the api CheckZeroQtyNegETC has been commented out below to allow
9442  	                 creation of -ve quantity/amounts in the target version due to the change order
9443  	                 merge. Commented out the delete and forall statements below which populate
9444  	                 pa_fp_spread_calc_tmp with the resource assignment ids and budget version ids
9445  	                 which will be used by the CheckZeroQtyNegETC api.
9446  	             */
9447 		     --Check if the budget lines have -Ve ETC because of the change order merge. This need not
9448                      --not be done when calculate API is called since calculate API internally calls this API
9449                      --Bug 4395494
9450 		     /*
9451                      DELETE FROM pa_fp_spread_calc_tmp;
9452 
9453                      FORALL kk IN 1..l_targ_ra_id_tbl.COUNT
9454                         INSERT INTO pa_fp_spread_calc_tmp
9455                         (budget_version_id,
9456                          resource_assignment_id)
9457                         VALUES
9458                         (p_budget_version_id,
9459                          l_targ_ra_id_tbl(kk));
9460 
9461                      PA_FP_CALC_PLAN_PKG.CheckZeroQTyNegETC
9462                      (p_budget_version_id     => p_budget_version_id
9463                      ,p_initialize            => 'Y'
9464                      ,x_return_status         => l_return_status
9465                      ,x_msg_count             => l_msg_count
9466                      ,x_msg_data              => l_msg_data);
9467 
9468                      IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9469                          IF P_PA_debug_mode = 'Y' THEN
9470                               pa_debug.g_err_stage:= 'Error CALLING PAFPCALB.CheckZeroQTyNegETC';
9471                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9472                          END IF;
9473                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9474                      END IF;
9475 		     */
9476  	             /* Bug 5726773: End of coding done for Support of negative quantity/amounts enhancement. */
9477 
9478                      ----Prepare the pl/sql tbls to call the API in planning transaction utils that calls PJI API
9479                      ----plan_update
9480 
9481                      SELECT pa_budget_lines_s.currval
9482                      INTO   l_dummy
9483                      FROM   dual;
9484 
9485                      IF l_dummy=l_id_before_bl_insertion THEN
9486 
9487                         l_id_after_bl_insertion := l_id_before_bl_insertion;
9488 
9489                      ELSE
9490 
9491                          SELECT pa_budget_lines_s.nextval
9492                          INTO   l_id_after_bl_insertion
9493                          FROM   dual;
9494 
9495                      END IF;
9496 
9497                      IF  l_id_before_bl_insertion <> l_id_after_bl_insertion THEN
9498 
9499                          IF P_PA_DEBUG_MODE = 'Y' THEN
9500                           pa_debug.g_err_stage:='Preparing input tbls for calculate API';
9501                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9502                          END IF;
9503 
9504                          SELECT pra.resource_assignment_id,
9505                               'N',
9506                               'Y',
9507                               pbl.txn_currency_code,
9508                               pbl.quantity,
9509                               pbl.txn_raw_cost,
9510                               pbl.txn_burdened_cost,
9511                               pbl.txn_revenue,
9512                               pbl.start_date,
9513                               pbl.end_date,
9514                               pbl.period_name,
9515                               pbl.project_raw_cost,
9516                               pbl.project_burdened_cost,
9517                               pbl.project_revenue,
9518                               pbl.raw_cost,
9519                               pbl.burdened_cost,
9520                               pbl.revenue,
9521                               pbl.cost_rejection_code,
9522                               pbl.revenue_rejection_code,
9523                               pbl.burden_rejection_code,
9524                               pbl.other_rejection_code,
9525                               pbl.pc_cur_conv_rejection_code,
9526                               pbl.pfc_cur_conv_rejection_code,
9527                               pra.task_id,
9528                               pra.rbs_element_id,
9529 							  pra.cbs_element_id, --bug#16911079
9530                               pra.resource_class_code,
9531                               pra.rate_based_flag
9532                         BULK COLLECT INTO
9533                               l_res_assignment_id_tbl,
9534                               l_delete_budget_lines_tbl,
9535                               l_spread_amount_flags_tbl,
9536                               l_currency_code_tbl,
9537                               l_total_quantity_tbl,
9538                               l_total_raw_cost_tbl,
9539                               l_total_burdened_cost_tbl,
9540                               l_total_revenue_tbl,
9541                               l_prm_bl_start_date_tbl,
9542                               l_prm_bl_end_date_tbl,
9543                               l_period_name_tbl,
9544                               l_pc_raw_cost_tbl,
9545                               l_pc_burd_cost_tbl,
9546                               l_pc_revenue_tbl,
9547                               l_pfc_raw_cost_tbl,
9548                               l_pfc_burd_cost_tbl,
9549                               l_pfc_revenue_tbl,
9550                               l_cost_rejection_code,
9551                               l_revenue_rejection_code,
9552                               l_burden_rejection_code,
9553                               l_other_rejection_code,
9554                               l_pc_cur_conv_rejection_code,
9555                               l_pfc_cur_conv_rejection_code,
9556                               l_pji_prm_task_id_tbl,
9557                               l_pji_prm_rbs_elem_id_tbl,
9558 							  l_pji_prm_cbs_elem_id_tbl, --bug#16911079
9559                               l_pji_prm_res_cls_code_tbl,
9560                               l_pji_prm_rbf_tbl
9561                         FROM    pa_resource_assignments pra,
9562                               pa_budget_lines pbl
9563                         WHERE   pra.resource_assignment_id = pbl.resource_assignment_id
9564                         AND     (pbl.budget_line_id BETWEEN l_id_before_bl_insertion AND l_id_after_bl_insertion)
9565                         AND     pra.budget_Version_id=p_budget_version_id;
9566 
9567                         IF P_PA_DEBUG_MODE = 'Y' THEN
9568                           pa_debug.g_err_stage:='l_res_assignment_id_tbl.COUNT IS '||l_res_assignment_id_tbl.COUNT;
9569                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9570 
9571                           pa_debug.g_err_stage:='l_rev_impl_flag  '||l_rev_impl_flag;
9572                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9573 
9574                           pa_debug.g_err_stage:=' l_impl_qty_tbl('||j ||  ') is '|| l_impl_qty_tbl(j);
9575                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9576 
9577                         END IF;
9578 
9579                     END IF;--IF  l_id_before_bl_insertion <> l_id_after_bl_insertion THEN
9580 
9581                     --Budget lines might have got updated in addition to getting inserted because of merge
9582                     --Those budget lines should also be considered while calling PJI API Plan_Update
9583                     --Note that l_bl_RESOURCE_ASIGNMENT_ID_tbl will contain BLs corresponding to BLs that got inserted
9584                     --as well as updated. Here we should consider only those BLs that have got updated. BLs that
9585                     --are inserted are considered in the earlier block
9586 
9587                     /* Bug 5335211: Removing the check for null rbs_version_id, to populate the variables always
9588                      * so that they contain proper values when calling the PJI rollup API, irrespective of whether
9589                      * a RBS is present in the target version or not.
9590                     IF l_rbs_version_id IS NOT NULL THEN */
9591                     IF l_bl_RESOURCE_ASIGNMENT_ID_tbl.COUNT > 0 THEN
9592 
9593                       IF P_PA_DEBUG_MODE = 'Y' THEN
9594                           pa_debug.g_err_stage:='Preparing tbls for for the lines that got update';
9595                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9596                       END IF;
9597 
9598 
9599                       --This index will be used for l_updXXX tbls containinng data for the BLs that have got
9600                       --updated. As told above l_bl_RESOURCE_ASIGNMENT_ID_tbl contains data for BLs  that have
9601                       --inserted as well as updated. Here we need to cosider only those rows that have got updated
9602                       --Hence differenct index is required
9603                       l_index:=0;
9604                       FOR kk in l_bl_RESOURCE_ASIGNMENT_ID_tbl.FIRST..l_bl_RESOURCE_ASIGNMENT_ID_tbl.LAST LOOP
9605 
9606                           IF l_upd_ra_bl_dml_code_tbl(kk) = 'UPDATE' THEN
9607 
9608                             l_index:=l_index+1;
9609 
9610                             l_res_assignment_id_tbl.extend;
9611                             l_period_name_tbl.extend;
9612                             l_currency_code_tbl.extend;
9613                             l_total_quantity_tbl.extend;
9614                             l_total_raw_cost_tbl.extend;
9615                             l_total_burdened_cost_tbl.extend;
9616                             l_total_revenue_tbl.extend;
9617                             l_prm_bl_start_date_tbl.extend;
9618                             l_prm_bl_end_date_tbl.extend;
9619                             l_pc_raw_cost_tbl.extend;
9620                             l_pc_burd_cost_tbl.extend;
9621                             l_pc_revenue_tbl.extend;
9622                             l_pfc_raw_cost_tbl.extend;
9623                             l_pfc_burd_cost_tbl.extend;
9624                             l_pfc_revenue_tbl.extend;
9625                             l_cost_rejection_code.extend;
9626                             l_revenue_rejection_code.extend;
9627                             l_burden_rejection_code.extend;
9628                             l_other_rejection_code.extend;
9629                             l_pc_cur_conv_rejection_code.extend;
9630                             l_pfc_cur_conv_rejection_code.extend;
9631 
9632                             IF P_PA_DEBUG_MODE = 'Y' THEN
9633                                 pa_debug.g_err_stage:='Done with tbl extending';
9634                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9635                             END IF;
9636 
9637 
9638                             --Here l_res_assignment_id_tbl, l_delete_budget_lines_tbl are already populated above with the
9639                             --budget line details that have got inserted. Also all the will have the same length.
9640                             --Hence using l_res_assignment_id_tbl.count as index
9641 
9642                             IF P_PA_DEBUG_MODE = 'Y' THEN
9643                                 pa_debug.g_err_stage:=' B l_res_assignment_id_tbl.count is '||l_res_assignment_id_tbl.count;
9644                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9645                             END IF;
9646 
9647                             l_res_assignment_id_tbl(l_res_assignment_id_tbl.count)      := l_bl_RESOURCE_ASIGNMENT_ID_tbl(kk);
9648 
9649                             IF P_PA_DEBUG_MODE = 'Y' THEN
9650 
9651                                 pa_debug.g_err_stage:=' A l_res_assignment_id_tbl.count is '||l_res_assignment_id_tbl.count;
9652                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9653 
9654                                 pa_debug.g_err_stage:='Done with l_res_assignment_id_tbl';
9655                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9656 
9657                                 pa_debug.g_err_stage:='l_period_name_tbl.count is '||l_period_name_tbl.count;
9658                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9659 
9660                                 pa_debug.g_err_stage:='l_upd_period_name_tbl('||kk||') is '||l_upd_period_name_tbl(l_index);
9661                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9662 
9663 
9664                             END IF;
9665 
9666                             l_period_name_tbl(l_period_name_tbl.count)            :=l_upd_period_name_tbl(l_index);
9667 
9668                             IF P_PA_DEBUG_MODE = 'Y' THEN
9669                                 pa_debug.g_err_stage:='Done with l_period_name_tbl';
9670                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9671                             END IF;
9672 
9673                             l_currency_code_tbl(l_res_assignment_id_tbl.count)          := l_upd_currency_code_tbl(l_index);
9674 
9675                             IF P_PA_DEBUG_MODE = 'Y' THEN
9676                                 pa_debug.g_err_stage:='Done with l_currency_code_tbl';
9677                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9678                             END IF;
9679 
9680                             l_total_quantity_tbl(l_res_assignment_id_tbl.count)         := nvl(l_bl_QUANTITY_tbl(kk),0);
9681                             l_total_raw_cost_tbl(l_res_assignment_id_tbl.count)         := nvl(l_bl_TXN_RAW_COST_tbl(kk),0);
9682                             l_total_burdened_cost_tbl(l_res_assignment_id_tbl.count)    := nvl(l_bl_TXN_BURDENED_COST_tbl(kk),0);
9683                             l_total_revenue_tbl(l_res_assignment_id_tbl.count)          := nvl(l_bl_TXN_REVENUE_tbl(kk),0);
9684                             l_prm_bl_start_date_tbl(l_res_assignment_id_tbl.count)      := l_upd_bl_start_date_tbl(l_index);
9685                             l_prm_bl_end_date_tbl(l_res_assignment_id_tbl.count)        := l_upd_bl_end_date_tbl(l_index);
9686                             l_cost_rejection_code(l_res_assignment_id_tbl.count)        := l_upd_cost_rejection_code(l_index);
9687                             l_revenue_rejection_code(l_res_assignment_id_tbl.count)     := l_upd_revenue_rejection_code(l_index);
9688                             l_burden_rejection_code(l_res_assignment_id_tbl.count)      := l_upd_burden_rejection_code(l_index);
9689                             l_other_rejection_code(l_res_assignment_id_tbl.count)       := l_upd_other_rejection_code(l_index);
9690                             l_pc_cur_conv_rejection_code(l_res_assignment_id_tbl.count) := l_upd_pc_cur_conv_rej_code(l_index);
9691                             l_pfc_cur_conv_rejection_code(l_res_assignment_id_tbl.count):= l_upd_pfc_cur_conv_rej_code(l_index);
9692                             l_pc_raw_cost_tbl(l_res_assignment_id_tbl.count)            := nvl(l_bl_PROJECT_RAW_COST_tbl(kk),0);
9693                             l_pc_burd_cost_tbl(l_res_assignment_id_tbl.count)           := nvl(l_bl_PROJECT_BURDENED_COST_tbl(kk),0);
9694                             l_pc_revenue_tbl(l_res_assignment_id_tbl.count)             := nvl(l_bl_PROJECT_REVENUE_tbl(kk),0);
9695                             l_pfc_raw_cost_tbl(l_res_assignment_id_tbl.count)           := nvl(l_bl_RAW_COST_tbl(kk),0);
9696                             l_pfc_burd_cost_tbl(l_res_assignment_id_tbl.count)          := nvl(l_bl_BURDENED_COST_tbl(kk),0);
9697                             l_pfc_revenue_tbl(l_res_assignment_id_tbl.count)            := nvl(l_bl_REVENUE_tbl(kk),0);
9698 
9699                             IF P_PA_DEBUG_MODE = 'Y' THEN
9700                                 pa_debug.g_err_stage:='Done with bl tbl copy. Proceeding to get RA attributes';
9701                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9702                             END IF;
9703 
9704                             l_temp:=NULL;
9705                             --Loop thru the resource assignments that were updated earlier to get the details
9706                             --such as task id, resource class code etc.Bug 3678314
9707                             FOR LL IN 1..l_upd_ra_res_asmt_id_tbl.COUNT LOOP
9708                                 IF l_bl_RESOURCE_ASIGNMENT_ID_tbl(kk)=l_upd_ra_res_asmt_id_tbl(LL) THEN
9709 
9710                                     l_temp:=LL;
9711                                     EXIT;
9712 
9713                                 END IF;
9714 
9715                             END LOOP;
9716 
9717                             --The below condition should never happen since if a budget line gets updated then
9718                             --the corresponding RA should also get updated and hence it should be part of l_upd_ra_res_asmt_id_tbl
9719                             --Bug 3678314
9720                             IF l_temp IS NULL THEN
9721 
9722                                 IF P_PA_DEBUG_MODE = 'Y' THEN
9723                                     pa_debug.g_err_stage:='RA in l_bl_RESOURCE_ASIGNMENT_ID_tbl '||l_bl_RESOURCE_ASIGNMENT_ID_tbl(kk) ||' doesnt exist in l_upd_ra_res_asmt_id_tbl ';
9724                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9725                                 END IF;
9726 
9727                                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9728 
9729                             END IF;
9730 
9731                             l_pji_prm_task_id_tbl.extend;
9732                             l_pji_prm_rbs_elem_id_tbl.extend;
9733 							l_pji_prm_cbs_elem_id_tbl.extend; --bug#16911079
9734                             l_pji_prm_res_cls_code_tbl.extend;
9735                             l_pji_prm_rbf_tbl.extend;
9736                             l_pji_prm_task_id_tbl(l_res_assignment_id_tbl.count):=l_upd_ra_task_id_tbl(l_temp);
9737                             l_pji_prm_rbs_elem_id_tbl(l_res_assignment_id_tbl.count):=l_upd_ra_rbs_elem_id_tbl(l_temp);
9738 							l_pji_prm_cbs_elem_id_tbl(l_res_assignment_id_tbl.count):=l_upd_ra_cbs_elem_id_tbl(l_temp); --bug#16911079
9739 
9740                             l_pji_prm_res_cls_code_tbl(l_res_assignment_id_tbl.count):=l_upd_ra_res_class_code_tbl(l_temp);
9741                             l_pji_prm_rbf_tbl(l_res_assignment_id_tbl.count):=l_upd_ra_rbf_tbl(l_temp);
9742 
9743                             IF P_PA_DEBUG_MODE = 'Y' THEN
9744                                 pa_debug.g_err_stage:='Done with ra tbl copy';
9745                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9746                             END IF;
9747 
9748                           END IF;
9749 
9750                       END LOOP;-- FOR kk in l_bl_RESOURCE_ASIGNMENT_ID_tbl.FIRST..l_bl_RESOURCE_ASIGNMENT_ID_tbl..LAST LOOP
9751 
9752                     END IF;--IF l_bl_RESOURCE_ASIGNMENT_ID_tbl.COUNT>O THEN
9753 
9754                     /* END IF;--IF l_rbs_version_id IS NOT NULL THEN : Bug 5335211 */
9755 
9756                      IF P_PA_DEBUG_MODE = 'Y' THEN
9757                         pa_debug.g_err_stage:='Calling rollup api ........ ';
9758                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9759                      END IF;
9760 
9761                      PA_FP_ROLLUP_PKG.ROLLUP_BUDGET_VERSION
9762                      (      p_budget_version_id     => p_budget_version_id
9763                          ,p_entire_version        => 'Y'
9764                          ,x_return_status         => l_return_status
9765                          ,x_msg_count             => l_msg_count
9766                          ,x_msg_data              => l_msg_data);
9767 
9768                      IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9769                          IF P_PA_debug_mode = 'Y' THEN
9770                               pa_debug.g_err_stage:= 'Error in ROLLUP_BUDGET_VERSION';
9771                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9772                          END IF;
9773                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9774                      END IF;
9775 
9776                      --dbms_output.put_line('I46');
9777 
9778                      -- Bug Fix: 4569365. Removed MRC code.
9779                      /*
9780                      IF P_PA_DEBUG_MODE = 'Y' THEN
9781                         pa_debug.g_err_stage:='Calling mrc api ........ ';
9782                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
9783                      END IF;
9784 
9785                      IF PA_MRC_FINPLAN.g_mrc_enabled_for_budgets IS NULL THEN
9786                           PA_MRC_FINPLAN.check_mrc_install
9787                                   (x_return_status      => l_return_status,
9788                                    x_msg_count          => l_msg_count,
9789                                    x_msg_data           => l_msg_data);
9790                      END IF;
9791 
9792                      IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9793                          IF P_PA_debug_mode = 'Y' THEN
9794                               pa_debug.g_err_stage:= 'Error in g_mrc_enabled_for_budgets';
9795                               pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9796                          END IF;
9797                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9798                      END IF;
9799 
9800                      --dbms_output.put_line('I47');
9801 
9802                      IF PA_MRC_FINPLAN.g_mrc_enabled_for_budgets AND
9803                           PA_MRC_FINPLAN.g_finplan_mrc_option_code = 'A' THEN
9804 
9805                              PA_MRC_FINPLAN.g_calling_module := NULL;
9806 
9807                              PA_MRC_FINPLAN.maintain_all_mc_budget_lines
9808                                   (p_fin_plan_version_id => p_budget_version_id,
9809                                    p_entire_version      => 'Y',
9810                                    x_return_status       => x_return_status,
9811                                    x_msg_count           => x_msg_count,
9812                                    x_msg_data            => x_msg_data);
9813 
9814 
9815                              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9816                                  IF P_PA_debug_mode = 'Y' THEN
9817                                     pa_debug.g_err_stage:= 'Error in maintain_all_mc_budget_lines';
9818                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9819                                  END IF;
9820                                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9821                             END IF;
9822 
9823 
9824                      END IF;
9825                      */
9826 
9827               --IPM Architecture Enhancement: Start Bug 4865563
9828                 -- The PA_RESOURCE_ASGN_CURR maintenance api updates the pa_budget_lines manually.
9829                 -- populate_display_quantity populates the display_quantity appropriately
9830 
9831                 --This api will take care of inserting display_quantity appropriately.Not necessary to insert every time in the budget lines
9832                 PA_BUDGET_LINES_UTILS.populate_display_qty
9833                     (p_budget_version_id          => p_budget_version_id,
9834                      p_context                    => 'FINANCIAL',
9835                      --p_use_temp_table_flag  => 'N',
9836                      --p_resource_assignment_id_tab  => l_res_assignment_id_tbl,
9837                      p_set_disp_qty_null_for_nrbf => 'Y',
9838                      x_return_status              => l_return_status);
9839 
9840                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9841                         IF P_PA_debug_mode = 'Y' THEN
9842                            pa_debug.g_err_stage:= 'Error in PA_BUDGET_LINES_UTILS.populate_display_qty';
9843                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9844                         END IF;
9845                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9846                     END IF;
9847 
9848 
9849                 PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DTLS
9850                    (P_BUDGET_VERSION_ID              => p_budget_version_id,
9851                     X_FP_COLS_REC                    => l_fp_cols_rec,
9852                     X_RETURN_STATUS                  => l_return_status,
9853                     X_MSG_COUNT                      => l_msg_count,
9854                     X_MSG_DATA                       => l_msg_data);
9855 
9856                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9857                         IF P_PA_debug_mode = 'Y' THEN
9858                            pa_debug.g_err_stage:= 'Error in PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DETAILS';
9859                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9860                         END IF;
9861                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9862                     END IF;
9863 
9864 
9865                 pa_res_asg_currency_pub.maintain_data
9866                    (p_fp_cols_rec           => l_fp_cols_rec,
9867                     p_calling_module        => 'CHANGE_MGT',
9868                     p_rollup_flag           => 'Y',
9869                     p_version_level_flag    => 'Y',
9870                     p_called_mode           => 'SELF_SERVICE',
9871                     x_return_status         => l_return_status,
9872                     x_msg_data              => l_msg_data,
9873                     x_msg_count             => l_msg_count);
9874 
9875                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9876                         IF P_PA_debug_mode = 'Y' THEN
9877                            pa_debug.g_err_stage:= 'Error in PA_RES_ASG_CURRENCY_PUB.MAINTAIN_DATA';
9878                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9879                         END IF;
9880                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9881                     END IF;
9882 
9883                 /* bug 5073816: At this point of time, the new entity would have records
9884                  * from the resource assignments in the target versions which have amounts.
9885                  * it is possible that some of the RAs created in the target version which
9886                  * do not have any budget lines would not be inserted into pa_resource_asgn_curr
9887                  * by the maintenance API. So to insert those left over RAs, we are calling
9888                  * the following.
9889                  */
9890                 pa_fin_plan_pub.create_default_plan_txn_rec
9891                  (p_budget_version_id   => p_budget_version_id,
9892                   p_calling_module      => 'CHANGE_MGT',
9893                   x_return_status       => l_return_status,
9894                   x_msg_count           => l_msg_count,
9895                   x_msg_data            => l_msg_data);
9896 
9897                   IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
9898                       IF P_PA_debug_mode = 'Y' THEN
9899                          pa_debug.g_err_stage:= 'Error in pa_fin_plan_pub.create_default_plan_txn_rec';
9900                          pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
9901                       END IF;
9902                       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9903                   END IF;
9904 
9905                 --IPM Architecture Enhancement: End Bug 4865563
9906 
9907 
9908                      --Call the PJI Plan Updte API only if the calculate API is not called earlier
9909                      IF (NOT (l_src_time_phased_code = 'N' AND (l_targ_time_phased_code = 'P' OR l_targ_time_phased_code = 'G'))) AND
9910                          l_call_rep_lines_api ='Y' THEN
9911 
9912                          -- Bug 5335211: Removing the check for null rbs_version_id
9913                          -- IF l_rbs_version_id IS NOT NULL AND -- end bug 5335211
9914                          IF l_res_assignment_id_tbl.COUNT>0 THEN
9915 
9916                             IF P_PA_DEBUG_MODE = 'Y' THEN
9917                                pa_debug.g_err_stage := 'Calling  pa_planning_transaction_utils.call_update_rep_lines_api';
9918                                pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
9919                             END IF;
9920                             pa_planning_transaction_utils.call_update_rep_lines_api
9921                             (
9922                                p_source                     => 'PL-SQL'
9923                               ,p_budget_version_id          => p_budget_version_id
9924                               ,p_resource_assignment_id_tbl => l_res_assignment_id_tbl
9925 							  ,p_cbs_element_id_tbl			=> l_pji_prm_cbs_elem_id_tbl --bug#16911079
9926                               ,p_period_name_tbl            => l_period_name_tbl
9927                               ,p_start_date_tbl             => l_prm_bl_start_date_tbl
9928                               ,p_end_date_tbl               => l_prm_bl_end_date_tbl
9929                               ,p_txn_currency_code_tbl      => l_currency_code_tbl
9930                               ,p_txn_raw_cost_tbl           => l_total_raw_cost_tbl
9931                               ,p_txn_burdened_cost_tbl      => l_total_burdened_cost_tbl
9932                               ,p_txn_revenue_tbl            => l_total_revenue_tbl
9933                               ,p_project_raw_cost_tbl       => l_pc_raw_cost_tbl
9934                               ,p_project_burdened_cost_tbl  => l_pc_burd_cost_tbl
9935                               ,p_project_revenue_tbl        => l_pc_revenue_tbl
9936                               ,p_raw_cost_tbl               => l_pfc_raw_cost_tbl
9937                               ,p_burdened_cost_tbl          => l_pfc_burd_cost_tbl
9938                               ,p_revenue_tbl                => l_pfc_revenue_tbl
9939                               ,p_cost_rejection_code_tbl    => l_cost_rejection_code
9940                               ,p_revenue_rejection_code_tbl => l_revenue_rejection_code
9941                               ,p_burden_rejection_code_tbl  => l_burden_rejection_code
9942                               ,p_other_rejection_code       => l_other_rejection_code
9943                               ,p_pc_cur_conv_rej_code_tbl   => l_pc_cur_conv_rejection_code
9944                               ,p_pfc_cur_conv_rej_code_tbl  => l_pfc_cur_conv_rejection_code
9945                               ,p_quantity_tbl               => l_total_quantity_tbl
9946                               ,p_rbs_element_id_tbl         => l_pji_prm_rbs_elem_id_tbl
9947                               ,p_task_id_tbl                => l_pji_prm_task_id_tbl
9948                               ,p_res_class_code_tbl         => l_pji_prm_res_cls_code_tbl
9949                               ,p_rate_based_flag_tbl        => l_pji_prm_rbf_tbl
9950                               ,x_return_status              => x_return_status
9951                               ,x_msg_count                  => x_msg_count
9952                               ,x_msg_data                   => x_msg_data);
9953 
9954                               IF x_return_Status <> FND_API.G_RET_STS_SUCCESS  THEN
9955                                 IF P_PA_DEBUG_MODE = 'Y' THEN
9956                                    pa_debug.g_err_stage := 'pa_planning_transaction_utils.call_update_rep_lines_api errored .... ' || x_msg_data;
9957                                    pa_debug.write( l_module_name,pa_debug.g_err_stage,5);
9958                                 END IF;
9959                                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9960                               END IF;
9961 
9962                          END IF;--IF l_rbs_version_id IS NOT NULL AND
9963 
9964                      END IF;--IF (NOT (l_src_time_phased_code = 'N' AND (l_targ_time_phased_code = 'P' OR l_targ_time_phased_code = 'G'))) AND
9965                      --dbms_output.put_line('I48');
9966                   END IF;
9967 
9968                  --dbms_output.put_line('I49');
9969                  IF  P_submit_version_flag = 'Y' THEN
9970                       IF l_targ_app_rev_flag = 'Y'    AND
9971                          l_impl_type_tbl(j) <> 'COST' AND
9972                          l_rev_impl_flag ='Y' THEN
9973 
9974                         --In this code to put the residual amount into the last budget line would have got executed and
9975                         --hence l_targ_pc/pfc_rev_after_merge and l_pc/pfc_revenue_delta would have got populated above
9976                         IF l_partial_factor <> 1 THEN
9977 
9978                             l_impl_pc_rev_amt  := l_targ_pc_rev_after_merge - l_targ_pc_rev_before_merge + l_pc_revenue_delta;
9979                             l_impl_pfc_rev_amt := l_targ_pfc_rev_after_merge - l_targ_pfc_rev_before_merge + l_pfc_revenue_delta;
9980 
9981                         ELSE
9982                             --Derive l_pc/pfc_revenue_delta. In this case  we have to go to pa_budget_lines since the amounts
9983                             --would not have got rolled up
9984                             SELECT sum(pbl.project_revenue) - l_targ_pc_rev_before_merge
9985                                   ,sum(pbl.revenue) - l_targ_pfc_rev_before_merge
9986                             INTO   l_impl_pc_rev_amt
9987                                   ,l_impl_pfc_rev_amt
9988                             FROM   pa_budget_lines pbl
9989                             WHERE  budget_version_id=p_budget_version_id;
9990 
9991                         END IF;
9992 
9993                         pa_fp_ci_implement_pkg.create_ci_impact_fund_lines
9994                         (p_project_id             => l_project_id,
9995                          p_ci_id                  => p_ci_id,
9996                          p_update_agr_amount_flag => P_update_agreement_amt_flag,
9997                          p_funding_category       => p_funding_category,
9998                          p_partial_factor         => l_partial_factor,
9999                          p_impl_txn_rev_amt       => l_partial_impl_rev_amt,
10000                          p_impl_pc_rev_amt        => l_impl_pc_rev_amt,
10001                          p_impl_pfc_rev_amt       => l_impl_pfc_rev_amt,
10002                          x_msg_data               => l_msg_data,
10003                          x_msg_count              => l_msg_count,
10004                          x_return_status          => l_return_status);
10005 
10006                          IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
10007                               IF P_PA_debug_mode = 'Y' THEN
10008                                  pa_debug.g_err_stage:= 'Error in create_ci_impact_fund_lines';
10009                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10010                               END IF;
10011                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10012                          END IF;
10013 
10014                       END IF;
10015 
10016                       --If the version is approved revenue version and if the project is enabled for auto
10017                       --baseline then the version should be baselined
10018                       --Submit the version if the project is not enabled for baseline or if the version is not
10019                       --an approved revenue version.
10020                       --please NOTE that the its not required to check whether the target version is the current
10021                       --working version or not as
10022                       ----P_submit_version_flag can be Y only when this API is called IMplement Financial Impact
10023                       ----page and in that page impact will always be implemented into the current working version.
10024                       /*IF l_baseline_funding_flag ='Y' AND
10025                          l_targ_app_rev_flag = 'Y'    AND
10026                          l_impl_type_tbl(j) <> 'COST' AND
10027                          l_rev_impl_flag ='Y' THEN
10028 
10029                          IF P_PA_debug_mode = 'Y' THEN
10030                                pa_debug.g_err_stage:= 'Calling the change management baseline API';
10031                                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10032                          END IF;
10033 
10034                          l_CI_ID_Tab.delete;
10035                          l_CI_ID_Tab(1):=p_ci_id;
10036 
10037                          pa_baseline_funding_pkg.change_management_baseline
10038                          (P_Project_ID   => l_project_id,
10039                           P_CI_ID_Tab    => l_CI_ID_Tab,
10040                           X_Err_Code     => X_Err_Code,
10041                           X_Status       => l_return_status);
10042 
10043                          IF X_Err_Code <>  0 THEN
10044                               IF P_PA_debug_mode = 'Y' THEN
10045                                  pa_debug.g_err_stage:= 'Error in change_management_baseline';
10046                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10047                               END IF;
10048                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10049                          END IF;
10050 
10051                       ELSE*/-- Commented as part of bug 3877815, would remove completely, once review is done.
10052 
10053                          SELECT record_version_number
10054                          INTO   l_record_version_number
10055                          FROM   pa_budget_versions
10056                          WHERE  budget_version_id=p_budget_version_id;
10057 
10058                          pa_fin_plan_pub.Submit_Current_Working
10059                             (p_project_id                   =>    l_project_id,
10060                              p_budget_version_id            =>    p_budget_version_id,
10061                              p_record_version_number        =>    l_record_version_number,
10062                              x_return_status                =>    l_return_status,
10063                              x_msg_count                    =>    l_msg_count,
10064                              x_msg_data                     =>    l_msg_data);
10065 
10066                          IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
10067                              IF P_PA_debug_mode = 'Y' THEN
10068                                 pa_debug.g_err_stage:= 'Error in Submit_Current_Working';
10069                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10070                              END IF;
10071                              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10072                          END IF;
10073 
10074                       /*END IF;*/
10075 
10076                    END IF;--IF  P_submit_version_flag = 'Y' THEN
10077 
10078             END IF;--IF l_partial_factor<>0
10079 
10080             END IF;--If l_targ_ra_id_tbl.COUNT>0
10081            --dbms_output.put_line('I50');
10082            -- Preparing input parameters for FP_CI_LINK_CONTROL_ITEMS
10083 
10084            /* Opening a cursor to get the project levee amounts. */
10085 
10086             OPEN c_proj_level_amounts;
10087 
10088 
10089 
10090               IF P_PA_DEBUG_MODE = 'Y' THEN
10091                  pa_debug.g_err_stage := 'fetching project level amounts';
10092                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10093               END IF;
10094 
10095 
10096 
10097               FETCH c_proj_level_amounts INTO
10098                     l_targ_pc_rawc_after_merge
10099                    ,l_targ_pc_burdc_after_merge
10100                    ,l_targ_pc_rev_after_merge
10101                    ,l_targ_pfc_rawc_after_merge
10102                    ,l_targ_pfc_burdc_after_merge
10103                    ,l_targ_pfc_rev_after_merge
10104                    ,l_targ_lab_qty_after_merge
10105                    ,l_targ_eqp_qty_after_merge;
10106 
10107            CLOSE c_proj_level_amounts;
10108 
10109 
10110            l_cost_ppl_qty         := null;
10111            l_rev_ppl_qty          := null;
10112            l_cost_equip_qty       := null;
10113            l_rev_equip_qty        := null;
10114            l_impl_pfc_raw_cost    := null;
10115            l_impl_pfc_revenue     := null;
10116            l_impl_pfc_burd_cost   := null;
10117            l_impl_pc_raw_cost     := null;
10118            l_impl_pc_revenue      := null;
10119            l_impl_pc_burd_cost    := null;
10120 
10121            IF l_cost_impl_flag = 'Y' THEN
10122                 l_cost_ppl_qty := l_targ_lab_qty_after_merge-l_targ_lab_qty_before_merge;
10123                 l_cost_equip_qty := l_targ_eqp_qty_after_merge-l_targ_eqp_qty_before_merge;
10124                 l_impl_pfc_raw_cost := l_targ_pfc_rawc_after_merge-l_targ_pfc_rawc_before_merge;
10125                 l_impl_pfc_burd_cost := l_targ_pfc_burdc_after_merge-l_targ_pfc_burdc_before_merge;
10126                 l_impl_pc_raw_cost := l_targ_pc_rawc_after_merge-l_targ_pc_rawc_before_merge;
10127                 l_impl_pc_burd_cost := l_targ_pc_burdc_after_merge-l_targ_pc_burdc_before_merge;
10128            END IF;
10129 
10130            IF l_rev_impl_flag = 'Y' THEN
10131                  IF l_impl_qty_tbl(j) = 'Y' AND l_impl_type_tbl(j) <> 'ALL' THEN -- Bug 3947169
10132                       l_rev_ppl_qty := l_targ_lab_qty_after_merge-l_targ_lab_qty_before_merge;
10133                       l_rev_equip_qty := l_targ_eqp_qty_after_merge-l_targ_eqp_qty_before_merge;
10134                  END IF;
10135 
10136                 l_impl_pfc_revenue := l_targ_pfc_rev_after_merge - l_targ_pfc_rev_before_merge;
10137                 l_impl_pc_revenue  := l_targ_pc_rev_after_merge - l_targ_pc_rev_before_merge;
10138            END IF;
10139 
10140            IF l_impl_type_tbl(j) = 'ALL' THEN
10141                 l_version_type := 'BOTH';
10142            ELSE
10143                 l_version_type := l_impl_type_tbl(j);
10144            END IF;
10145 
10146            --dbms_output.put_line('I51');
10147 
10148 
10149             IF P_PA_debug_mode = 'Y' THEN
10150                  pa_debug.g_err_stage:= 'l_cost_ppl_qty '||l_cost_ppl_qty;
10151                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10152 
10153                  pa_debug.g_err_stage:= 'l_rev_ppl_qty '||l_rev_ppl_qty;
10154                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10155 
10156                  pa_debug.g_err_stage:= 'l_cost_equip_qty '||l_cost_equip_qty;
10157                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10158 
10159                  pa_debug.g_err_stage:= 'l_rev_equip_qty '||l_rev_equip_qty;
10160                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10161 
10162                  pa_debug.g_err_stage:= 'l_impl_pfc_raw_cost '||l_impl_pfc_raw_cost;
10163                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10164 
10165                  pa_debug.g_err_stage:= 'l_impl_pfc_revenue '||l_impl_pfc_revenue;
10166                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10167 
10168                  pa_debug.g_err_stage:= 'l_impl_pfc_burd_cost '||l_impl_pfc_burd_cost;
10169                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10170 
10171                  pa_debug.g_err_stage:= 'l_impl_pc_raw_cost '||l_impl_pc_raw_cost;
10172                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10173 
10174                  pa_debug.g_err_stage:= 'l_impl_pc_revenue '||l_impl_pc_revenue;
10175                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10176 
10177                  pa_debug.g_err_stage:= 'l_impl_pc_burd_cost '||l_impl_pc_burd_cost;
10178                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10179 
10180                  pa_debug.g_err_stage:= 'l_partial_impl_rev_amt '||l_partial_impl_rev_amt;
10181                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10182 
10183             END IF;
10184 
10185             IF l_impl_earlier='N' THEN
10186 
10187                 pa_fp_ci_merge.FP_CI_LINK_CONTROL_ITEMS(
10188                             p_project_id           => l_project_id
10189                            ,p_s_fp_version_id      => l_src_ver_id_tbl(j)
10190                            ,p_t_fp_version_id      => p_budget_version_id
10191                            ,p_inclusion_method     => 'AUTOMATIC'
10192                            ,p_included_by          => NULL
10193                            ,p_version_type         => l_version_type
10194                            ,p_ci_id                => p_ci_id
10195                            ,p_cost_ppl_qty         => l_cost_ppl_qty
10196                            ,p_rev_ppl_qty          => l_rev_ppl_qty
10197                            ,p_cost_equip_qty       => l_cost_equip_qty
10198                            ,p_rev_equip_qty        => l_rev_equip_qty
10199                            ,p_impl_pfc_raw_cost    => l_impl_pfc_raw_cost
10200                            ,p_impl_pfc_revenue     => l_impl_pfc_revenue
10201                            ,p_impl_pfc_burd_cost   => l_impl_pfc_burd_cost
10202                            ,p_impl_pc_raw_cost     => l_impl_pc_raw_cost
10203                            ,p_impl_pc_revenue      => l_impl_pc_revenue
10204                            ,p_impl_pc_burd_cost    => l_impl_pc_burd_cost
10205                            ,p_impl_agr_revenue     => l_partial_impl_rev_amt
10206                            ,x_return_status        => l_return_status
10207                            ,x_msg_count            => l_msg_count
10208                            ,x_msg_data             => l_msg_data
10209                           )  ;
10210 
10211                 IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
10212                     IF P_PA_debug_mode = 'Y' THEN
10213                          pa_debug.g_err_stage:= 'Error in FP_CI_LINK_CONTROL_ITEMS';
10214                          pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10215                     END IF;
10216                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10217                 END IF;
10218 
10219             ELSE--IF l_impl_earlier='Y'
10220 
10221                IF l_version_type IN ('REVENUE', 'BOTH') THEN
10222 
10223                     UPDATE pa_fp_merged_ctrl_items
10224                     SET    impl_proj_func_revenue       =nvl(impl_proj_func_revenue,0)+nvl(l_impl_pfc_revenue,0)
10225                           ,impl_proj_revenue            =nvl(impl_proj_revenue,0)+nvl(l_impl_pc_revenue,0)
10226                           ,impl_quantity                =nvl(impl_quantity,0)+nvl(l_rev_ppl_qty,0)
10227                           ,impl_equipment_quantity      =nvl(impl_equipment_quantity,0)+nvl(l_rev_equip_qty,0)
10228                           ,impl_agr_revenue             =nvl(impl_agr_revenue,0) + nvl(l_partial_impl_rev_amt,0)
10229                           ,record_version_number        =record_version_number+1
10230                           ,last_update_date             =sysdate
10231                           ,last_update_login            =fnd_global.login_id
10232                           ,last_updated_by              =fnd_global.user_id
10233                     WHERE  project_id=l_project_id
10234                     AND    plan_version_id=p_budget_version_id
10235                     AND    ci_id=p_ci_id
10236                     AND    ci_plan_version_id=l_src_ver_id_tbl(j)
10237                     AND    version_type='REVENUE';
10238 
10239                 END IF;
10240 
10241             END IF;
10242 
10243             --rev_partially_impl_flag of pa_budget_Versions should be updated in case of partial implementation
10244             --It should be Y if the CO is implemented partially. It should be N if the CO has got fully implemented
10245             -- If the CO has earlier been partially implemented(l_impl_earlier = 'Y'), inclusion will make it fully implemented.
10246             -- So, rev_partially_impl_flag should be set to 'N'
10247             IF  p_context='PARTIAL_REV' OR l_impl_earlier = 'Y' THEN
10248 
10249                 IF nvl(l_impl_amt,0) + l_partial_impl_rev_amt = l_total_amt THEN
10250 
10251                     UPDATE pa_budget_versions
10252                     SET    rev_partially_impl_flag ='N'
10253                           ,record_version_number   =record_version_number+1
10254                           ,last_update_date        =sysdate
10255                           ,last_update_login       =fnd_global.login_id
10256                           ,last_updated_by         =fnd_global.user_id
10257                     WHERE  budget_version_id = l_src_ver_id_tbl(j);
10258 
10259                 ELSE
10260 
10261                     --Update pa_budget_versions only if rev_partially_impl_flag is not already Y
10262                     UPDATE pa_budget_versions
10263                     SET    rev_partially_impl_flag ='Y'
10264                           ,record_version_number   =record_version_number+1
10265                           ,last_update_date        =sysdate
10266                           ,last_update_login       =fnd_global.login_id
10267                           ,last_updated_by         =fnd_global.user_id
10268                     WHERE  budget_version_id = l_src_ver_id_tbl(j)
10269                     AND    nvl(rev_partially_impl_flag,'N')='N';
10270 
10271                 END IF;
10272 
10273             END IF;
10274 
10275            --dbms_output.put_line('I52');
10276             IF (l_targ_app_cost_flag = 'Y' OR l_targ_app_rev_flag = 'Y') AND l_current_working_flag = 'Y' THEN
10277                  l_impact_type_code := 'FINPLAN_' || l_version_type;
10278                  pa_fp_ci_merge.FP_CI_UPDATE_IMPACT
10279                           (p_ci_id                 => p_ci_id
10280                           ,p_status_code           => 'CI_IMPACT_IMPLEMENTED'
10281                           ,p_implemented_by        => FND_GLOBAL.USER_ID
10282                           ,p_impact_type_code      => l_impact_type_code
10283                           ,p_commit_flag           => 'N'
10284                           ,p_init_msg_list         => 'N'
10285                           ,p_record_version_number => null
10286                           ,x_return_status         => l_return_status
10287                           ,x_msg_count             => l_msg_count
10288                           ,x_msg_data              => l_msg_data
10289                           );
10290 
10291                   IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
10292                      IF P_PA_debug_mode = 'Y' THEN
10293                           pa_debug.g_err_stage:= 'Error in FP_CI_UPDATE_IMPACT';
10294                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10295                      END IF;
10296                      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10297                   END IF;
10298 
10299 
10300             END IF;
10301            --dbms_output.put_line('I53');
10302        END LOOP;
10303 
10304        --dbms_output.put_line('I54');
10305                   --updating reporting lines. Call is necessary only if calculate API has not been called
10306 
10307      IF P_PA_debug_mode = 'Y' THEN
10308           pa_debug.g_err_stage:= 'Exiting implement_ci_into_single_ver';
10309           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
10310      --dbms_output.put_line('I58');
10311      pa_debug.reset_curr_function;
10312    END IF;
10313 EXCEPTION
10314       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10315           l_msg_count := FND_MSG_PUB.count_msg;
10316 
10317           IF l_msg_count = 1 and x_msg_data IS NULL THEN
10318                PA_INTERFACE_UTILS_PUB.get_messages
10319                      (p_encoded        => FND_API.G_TRUE
10320                       ,p_msg_index      => 1
10321                       ,p_msg_count      => l_msg_count
10322                       ,p_msg_data       => l_msg_data
10323                       ,p_data           => l_data
10324                       ,p_msg_index_out  => l_msg_index_out);
10325                x_msg_data := l_data;
10326                x_msg_count := l_msg_count;
10327           ELSE
10328               x_msg_count := l_msg_count;
10329           END IF;
10330           x_return_status := FND_API.G_RET_STS_ERROR;
10331 
10332           ROLLBACK TO implement_ci_into_single_ver;
10333          IF p_pa_debug_mode = 'Y' THEN
10334           pa_debug.reset_curr_function;
10335     END IF;
10336           RETURN;
10337 
10338      WHEN Others THEN
10339           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10340           x_msg_count     := 1;
10341           x_msg_data      := SQLERRM;
10342 
10343           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FP_CI_MERGE'
10344                                   ,p_procedure_name  => 'implement_ci_into_single_ver');
10345           IF P_PA_DEBUG_MODE = 'Y' THEN
10346                pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
10347                pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
10348           END IF;
10349           ROLLBACK TO implement_ci_into_single_ver;
10350      IF p_pa_debug_mode = 'Y' THEN
10351           pa_debug.reset_curr_function;
10352     END IF;
10353           RAISE;
10354 
10355 
10356 END implement_ci_into_single_ver;
10357 
10358 --This is a private API called from the implement change document API. This API will perform all the validationa
10359 --required for deciding whether a CI document can be merged into a target version or not. Bug 3550073
10360 PROCEDURE validate_ci_merge_input_data(
10361  p_context                      IN       VARCHAR2
10362 ,p_ci_id_tbl                    IN       SYSTEM.pa_num_tbl_type
10363 ,p_ci_cost_version_id_tbl       IN       SYSTEM.pa_num_tbl_type
10364 ,p_ci_rev_version_id_tbl        IN       SYSTEM.pa_num_tbl_type
10365 ,p_ci_all_version_id_tbl        IN       SYSTEM.pa_num_tbl_type
10366 ,p_budget_version_id_tbl        IN       SYSTEM.pa_num_tbl_type
10367 ,p_fin_plan_type_id_tbl         IN       SYSTEM.pa_num_tbl_type
10368 ,p_fin_plan_type_name_tbl       IN       SYSTEM.pa_varchar2_150_tbl_type
10369 ,p_impl_cost_flag_tbl           IN       SYSTEM.pa_varchar2_1_tbl_type
10370 ,p_impl_rev_flag_tbl            IN       SYSTEM.pa_varchar2_1_tbl_type
10371 ,p_submit_version_flag_tbl      IN       SYSTEM.pa_varchar2_1_tbl_type
10372 ,px_partial_impl_rev_amt        IN  OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10373 ,p_agreement_id                 IN       pa_agreements_all.agreement_id%TYPE
10374 ,p_update_agreement_amt_flag    IN       VARCHAR2
10375 ,p_funding_category             IN       VARCHAR2
10376 ,x_ci_id_tbl                    OUT      NOCOPY SYSTEM.pa_num_tbl_type --File.Sql.39 bug 4440895
10377 ,x_ci_cost_version_id_tbl       OUT      NOCOPY SYSTEM.pa_num_tbl_type --File.Sql.39 bug 4440895
10378 ,x_ci_rev_version_id_tbl        OUT      NOCOPY SYSTEM.pa_num_tbl_type --File.Sql.39 bug 4440895
10379 ,x_ci_all_version_id_tbl        OUT      NOCOPY SYSTEM.pa_num_tbl_type --File.Sql.39 bug 4440895
10380 ,x_budget_version_id_tbl        OUT      NOCOPY SYSTEM.pa_num_tbl_type --File.Sql.39 bug 4440895
10381 ,x_fin_plan_type_id_tbl         OUT      NOCOPY SYSTEM.pa_num_tbl_type --File.Sql.39 bug 4440895
10382 ,x_fin_plan_type_name_tbl       OUT      NOCOPY SYSTEM.pa_varchar2_150_tbl_type --File.Sql.39 bug 4440895
10383 ,x_submit_version_flag_tbl      OUT      NOCOPY SYSTEM.pa_varchar2_1_tbl_type --File.Sql.39 bug 4440895
10384 ,x_ci_number                    OUT      NOCOPY SYSTEM.pa_varchar2_100_tbl_type --File.Sql.39 bug 4440895
10385 ,x_budget_ci_map_rec_tbl        OUT      NOCOPY budget_ci_map_rec_tbl_type --File.Sql.39 bug 4440895
10386 ,x_agreement_id                 OUT      NOCOPY pa_agreements_all.agreement_id%TYPE --File.Sql.39 bug 4440895
10387 ,x_funding_category             OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10388 ,x_return_status                OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10389 ,x_msg_count                    OUT      NOCOPY NUMBER --File.Sql.39 bug 4440895
10390 ,x_msg_data                     OUT      NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
10391 IS
10392     --Start of variables used for debugging
10393     l_msg_count                 NUMBER :=0;
10394     l_data                      VARCHAR2(2000);
10395     l_msg_data                  VARCHAR2(2000);
10396     l_error_msg_code            VARCHAR2(30);
10397     l_msg_index_out             NUMBER;
10398     l_return_status             VARCHAR2(2000);
10399     l_debug_mode                VARCHAR2(30);
10400     l_module_name               VARCHAR2(100):='PAFPCIMB.validate_ci_merge_input_data';
10401     --End of variables used for debugging
10402     i                           NUMBER;
10403     l_ci_cost_version_id_tbl    pa_budget_versions.budget_version_id%TYPE;
10404     l_ci_rev_version_id_tbl     pa_budget_versions.budget_version_id%TYPE;
10405     l_ci_all_version_id_tbl     pa_budget_versions.budget_version_id%TYPE;
10406     l_out_index                 NUMBER;
10407     l_error_occurred_flag       VARCHAR2(1);
10408     l_budget_status_code        pa_budget_versions.budget_status_code%TYPE;
10409     l_ci_cost_version_id        pa_budget_versions.budget_version_id%TYPE;
10410     l_ci_rev_version_id         pa_budget_versions.budget_version_id%TYPE;
10411     l_ci_all_version_id         pa_budget_versions.budget_version_id%TYPE;
10412     l_project_id                pa_projects_all.project_id%TYPE;
10413     l_source_version_id_tbl     SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
10414     l_calling_mode_for_chk_api  VARCHAR2(30);
10415     l_merge_possible_code_tbl   SYSTEM.pa_varchar2_1_tbl_type :=SYSTEM.pa_varchar2_1_tbl_type();
10416     l_copy_ci_ver_flag          VARCHAR2(1);
10417     l_record_version_number     pa_budget_versions.record_Version_number%TYPE;
10418     l_cost_ci_ver_index         NUMBER;
10419     l_rev_ci_ver_index          NUMBER;
10420     l_all_ci_ver_index          NUMBER;
10421     l_index                     NUMBER:=0;
10422     l_implementable_impact      VARCHAR2(10);
10423     l_dummy                     VARCHAR2(1);
10424     l_ci_number                 pa_control_items.ci_number%TYPE;
10425 
10426     l_cost_impl_flag            VARCHAR2(1);
10427     l_rev_impl_flag             VARCHAR2(1);
10428     l_cost_impact_impl_flag     VARCHAR2(1);
10429     l_rev_impact_impl_flag      VARCHAR2(1);
10430     l_partially_impl_flag       VARCHAR2(1);
10431     l_agreement_num             pa_agreements_all.agreement_num%TYPE;
10432     l_approved_fin_pt_id        pa_fin_plan_types_b.fin_plan_type_id%TYPE;
10433     l_impl_cost_flag_tbl        SYSTEM.pa_varchar2_1_tbl_type;
10434     l_impl_rev_flag_tbl         SYSTEM.pa_varchar2_1_tbl_type;
10435     l_fin_plan_type_id          pa_fin_plan_types_b.fin_plan_type_id%TYPE;
10436     l_fin_plan_type_name        pa_fin_plan_types_tl.name%TYPE;
10437     l_total_amount              NUMBER;
10438     l_implemented_amount        NUMBER;
10439     l_remaining_amount          NUMBER;
10440     l_agr_curr_code             pa_agreements_all.agreement_currency_code%TYPE;
10441 
10442     CURSOR c_chk_rej_codes (ci_ci_ver_id1 pa_budget_versions.budget_version_id%TYPE,
10443                             ci_ci_ver_id2 pa_budget_versions.budget_version_id%TYPE)
10444     IS
10445     SELECT 'Y'
10446     FROM    DUAL
10447     WHERE   EXISTS (SELECT 1
10448                     FROM   pa_budget_lines pbl
10449                     WHERE  pbl.budget_version_id IN (ci_ci_ver_id1, ci_ci_ver_id2)
10450                     AND(       pbl.cost_rejection_code         IS NOT NULL
10451                         OR     pbl.revenue_rejection_code      IS NOT NULL
10452                         OR     pbl.burden_rejection_code       IS NOT NULL
10453                         OR     pbl.other_rejection_code        IS NOT NULL
10454                         OR     pbl.pc_cur_conv_rejection_code  IS NOT NULL
10455                         OR     pbl.pfc_cur_conv_rejection_code IS NOT NULL));
10456 
10457 --These two variables will be used for comparing the no. of error messages in the error message stack when the
10458 --API called and when the API is done with the processing. If the no of messages in the two pl/sql tbls are
10459 --different then the error status will be returned as E from the API. This will be done only when the p_context='CI_MERGE'
10460 l_init_msg_count                NUMBER;
10461 l_msg_count_at_end               NUMBER;
10462 
10463 l_version_type              pa_budget_versions.version_type%TYPE;
10464 l_version_type_tbl          SYSTEM.pa_varchar2_30_tbl_type :=SYSTEM.pa_varchar2_30_tbl_type();
10465 l_fin_plan_type_name_tbl    SYSTEM.pa_varchar2_150_tbl_type :=SYSTEM.pa_varchar2_150_tbl_type();
10466 -- Bug 5845142
10467 l_app_cost_plan_type_flag_tbl  SYSTEM.pa_varchar2_1_tbl_type :=SYSTEM.pa_varchar2_1_tbl_type();
10468 l_app_rev_plan_type_flag_tbl   SYSTEM.pa_varchar2_1_tbl_type :=SYSTEM.pa_varchar2_1_tbl_type();
10469 l_app_cost_plan_type_flag      VARCHAR2(1);
10470 l_app_rev_plan_type_flag       VARCHAR2(1);
10471 
10472 l_part_impl_err             VARCHAR2(1);
10473 l_ci_name                   VARCHAR2(300);
10474 
10475 l_ci_type_class_code        pa_ci_types_b.ci_type_class_code%TYPE;
10476 
10477 -- Bug 3986129: Added the following
10478 l_targ_ver_plan_prc_code    pa_budget_versions.plan_processing_code%TYPE;
10479 --CBS 12.2 enhancement changes Start here
10480 l_ci_cost_res_cls_flag      VARCHAR2(1) := 'Y';
10481 l_ci_rev_res_cls_flag       VARCHAR2(1) := 'Y';
10482 l_ci_all_res_cls_flag       VARCHAR2(1) := 'Y';
10483 l_targ_resource_cls_flag    VARCHAR2(1) := 'Y';
10484 --CBS 12.2 enhancement changes End here
10485 BEGIN
10486     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10487     l_debug_mode := NVL(l_debug_mode, 'Y');
10488     x_msg_count := 0;
10489     x_return_status := FND_API.G_RET_STS_SUCCESS;
10490     IF p_context='CI_MERGE' THEN
10491          l_init_msg_count:= FND_MSG_PUB.count_msg;
10492     END IF;
10493     IF p_pa_debug_mode = 'Y' THEN
10494     PA_DEBUG.Set_Curr_Function( p_function   => 'PAFPCIMB.validate_ci_merge_input_data',
10495                                 p_debug_mode => l_debug_mode );
10496     END IF;
10497     IF l_debug_mode = 'Y' THEN
10498 
10499         pa_debug.g_err_stage:= 'Validating the input parameters';
10500         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10501 
10502     END IF;
10503     --dbms_output.put_line('1');
10504 
10505 
10506     --p_context should be  valid
10507     IF p_context  NOT IN ('IMPL_FIN_IMPACT' , 'INCLUDE', 'PARTIAL_REV', 'CI_MERGE') THEN
10508 
10509         IF l_debug_mode = 'Y' THEN
10510 
10511             pa_debug.g_err_stage:= 'p_context passed is  '|| p_context;
10512             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10513         END IF;
10514         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10515                      p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10516                      p_token1         => 'PROCEDURENAME',
10517                      p_value1         => l_module_name);
10518         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10519 
10520 
10521     END IF;
10522 
10523 
10524     --The no. of elements in ci_id tbl and the ci version id tbls should always be same
10525     IF ((p_ci_cost_version_id_tbl.COUNT <> 0 AND
10526         (p_ci_id_tbl.COUNT <> p_ci_cost_version_id_tbl.COUNT))OR
10527         (p_ci_rev_version_id_tbl.COUNT  <> 0 AND
10528         (p_ci_id_tbl.COUNT <> p_ci_rev_version_id_tbl.COUNT)) OR
10529         (p_ci_all_version_id_tbl.COUNT  <> 0 AND
10530         (p_ci_id_tbl.COUNT <> p_ci_all_version_id_tbl.COUNT ))) THEN
10531 
10532         IF l_debug_mode = 'Y' THEN
10533 
10534             pa_debug.g_err_stage:= 'p_ci_id_tbl.COUNT '|| p_ci_id_tbl.COUNT;
10535             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10536 
10537             pa_debug.g_err_stage:= 'p_ci_cost_version_id_tbl.COUNT '|| p_ci_cost_version_id_tbl.COUNT;
10538             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10539 
10540             pa_debug.g_err_stage:= 'p_ci_rev_version_id_tbl.COUNT '|| p_ci_rev_version_id_tbl.COUNT;
10541             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10542 
10543             pa_debug.g_err_stage:= 'p_ci_all_version_id_tbl.COUNT '|| p_ci_all_version_id_tbl.COUNT;
10544             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10545 
10546         END IF;
10547 
10548         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10549                              p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10550                              p_token1         => 'PROCEDURENAME',
10551                              p_value1         => l_module_name);
10552         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10553 
10554     END IF;
10555         --dbms_output.put_line('2');
10556 
10557     --The no of elements in the p_fin_plan_type_id_tbl, p_fin_plan_type_name_tbl p_impl_cost_flag_tbl
10558     --p_impl_rev_flag_tbl,p_submit_version_flag_tbl
10559     IF (p_fin_plan_type_id_tbl.COUNT   <> 0 AND p_fin_plan_type_id_tbl.COUNT<>p_budget_version_id_tbl.COUNT) OR
10560        (p_fin_plan_type_name_tbl.COUNT <> 0 AND p_fin_plan_type_name_tbl.COUNT<>p_budget_version_id_tbl.COUNT) OR
10561        (p_impl_cost_flag_tbl.COUNT     <> 0 AND p_impl_cost_flag_tbl.COUNT     <> p_budget_version_id_tbl.COUNT) OR
10562        (p_impl_rev_flag_tbl.COUNT      <> 0 AND p_impl_rev_flag_tbl.COUNT      <> p_budget_version_id_tbl.COUNT) OR
10563        (p_submit_version_flag_tbl.COUNT<> 0 AND p_submit_version_flag_tbl.COUNT<> p_budget_version_id_tbl.COUNT) THEN
10564 
10565         IF l_debug_mode = 'Y' THEN
10566 
10567             pa_debug.g_err_stage:= 'p_fin_plan_type_id_tbl.COUNT '|| p_fin_plan_type_id_tbl.COUNT;
10568             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10569 
10570             pa_debug.g_err_stage:= 'p_fin_plan_type_name_tbl.COUNT '|| p_fin_plan_type_name_tbl.COUNT;
10571             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10572 
10573             pa_debug.g_err_stage:= 'p_impl_cost_flag_tbl.COUNT '|| p_impl_cost_flag_tbl.COUNT;
10574             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10575 
10576             pa_debug.g_err_stage:= 'p_impl_rev_flag_tbl.COUNT '|| p_impl_rev_flag_tbl.COUNT;
10577             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10578 
10579             pa_debug.g_err_stage:= 'p_submit_version_flag_tbl.COUNT '|| p_submit_version_flag_tbl.COUNT;
10580             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10581 
10582             pa_debug.g_err_stage:= 'p_budget_version_id_tbl.COUNT '|| p_budget_version_id_tbl.COUNT;
10583             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10584 
10585         END IF;
10586 
10587         PA_UTILS.ADD_MESSAGE
10588                     (p_app_short_name => 'PA',
10589                      p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10590                      p_token1         => 'PROCEDURENAME',
10591                      p_value1         => l_module_name);
10592         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10593 
10594     END IF;
10595 
10596         --dbms_output.put_line('3');
10597     --In case of partial implementation the ci id tbl and target budget version id tbls should have
10598     --only one record
10599     IF p_context='PARTIAL_REV' THEN
10600         IF (p_ci_id_tbl.COUNT <>1 OR
10601             p_budget_version_id_tbl.COUNT <>1) THEN
10602 
10603             IF l_debug_mode = 'Y' THEN
10604 
10605                 pa_debug.g_err_stage:= 'p_ci_id_tbl.COUNT in partial implementation context '|| p_ci_id_tbl.COUNT;
10606                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10607 
10608                 pa_debug.g_err_stage:= 'p_budget_version_id_tbl.COUNT in partial implementation context '|| p_budget_version_id_tbl.COUNT;
10609                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10610 
10611             END IF;
10612 
10613             PA_UTILS.ADD_MESSAGE
10614                         (p_app_short_name => 'PA',
10615                          p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10616                          p_token1         => 'PROCEDURENAME',
10617                          p_value1         => l_module_name);
10618             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10619 
10620         END IF;
10621 
10622         IF (p_impl_cost_flag_tbl(1) ='Y' OR
10623             p_impl_rev_flag_tbl(1) = 'N') THEN
10624 
10625             IF l_debug_mode = 'Y' THEN
10626 
10627                 pa_debug.g_err_stage:= 'p_cost_impl_flag passed in partial implementation case is '|| p_impl_cost_flag_tbl(1);
10628                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10629 
10630                 pa_debug.g_err_stage:= 'p_rev_impl_flag passed in partial implementation case is '|| p_impl_rev_flag_tbl(1);
10631                 pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
10632 
10633             END IF;
10634 
10635             PA_UTILS.ADD_MESSAGE
10636                         (p_app_short_name => 'PA',
10637                          p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10638                          p_token1         => 'PROCEDURENAME',
10639                          p_value1         => l_module_name);
10640             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10641 
10642         END IF;
10643 
10644     END IF;
10645     --Initialise all the out put pl/sql tables
10646     x_ci_id_tbl                :=SYSTEM.pa_num_tbl_type();
10647     x_ci_cost_version_id_tbl   :=SYSTEM.pa_num_tbl_type();
10648     x_ci_rev_version_id_tbl    :=SYSTEM.pa_num_tbl_type();
10649     x_ci_all_version_id_tbl    :=SYSTEM.pa_num_tbl_type();
10650     x_budget_version_id_tbl    :=SYSTEM.pa_num_tbl_type();
10651     x_ci_number                :=SYSTEM.pa_varchar2_100_tbl_type();
10652     l_impl_cost_flag_tbl       :=SYSTEM.pa_varchar2_1_tbl_type();
10653     l_impl_rev_flag_tbl        :=SYSTEM.pa_varchar2_1_tbl_type();
10654     x_submit_version_flag_tbl  :=SYSTEM.pa_varchar2_1_tbl_type();
10655     x_fin_plan_type_id_tbl     :=SYSTEM.pa_num_tbl_type();
10656     x_fin_plan_type_name_tbl   :=SYSTEM.pa_varchar2_150_tbl_type();
10657     IF p_funding_category IS NULL THEN
10658         x_funding_category := 'ADDITIONAL';
10659     -- Bug 3749322- adding the else clause to pass the value of
10660     -- p_funding_category as it is passed
10661     ELSE
10662         x_funding_category := p_funding_category;
10663     END IF;
10664     x_agreement_id             :=p_agreement_id;
10665 
10666     --Derive the calling context that should be passed to the check merge possible API
10667     IF p_context='IMPL_FIN_IMPACT' OR
10668        p_context='PARTIAL_REV' THEN
10669 
10670         l_calling_mode_for_chk_api:='IMPLEMENT';
10671 
10672     ELSIF p_context='INCLUDE' THEN
10673 
10674         l_calling_mode_for_chk_api:='INCLUDE';
10675 
10676     ELSIF p_context='CI_MERGE' THEN
10677         l_calling_mode_for_chk_api:= 'INCLUDE_CR_TO_CO';
10678 
10679     END IF;
10680 
10681     IF l_debug_mode = 'Y' THEN
10682 
10683         pa_debug.g_err_stage:= 'Calling mode for the chk API derived is '||l_calling_mode_for_chk_api;
10684         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10685 
10686         pa_debug.g_err_stage:= 'Validating the the CIs passed';
10687         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10688 
10689 
10690     END IF;
10691 
10692     IF p_ci_id_tbl.COUNT=0 OR
10693        p_budget_version_id_tbl.COUNT=0 THEN
10694 
10695         IF l_debug_mode = 'Y' THEN
10696 
10697             pa_debug.g_err_stage:= 'CI Ids/BV Ids are not passed for merge. Returning';
10698             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10699          pa_debug.reset_curr_function;
10700     END IF;
10701        RETURN;
10702 
10703     END IF;
10704     --dbms_output.put_line('4');
10705 
10706     FOR i IN p_ci_id_tbl.FIRST..p_ci_id_tbl.LAST LOOP
10707 
10708         IF p_ci_id_tbl(i) IS NULL THEN
10709 
10710             IF l_debug_mode = 'Y' THEN
10711 
10712                 pa_debug.g_err_stage:= 'p_ci_id_tbl('||i||') IS '|| p_ci_id_tbl(i);
10713                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10714 
10715             END IF;
10716             PA_UTILS.ADD_MESSAGE
10717             (p_app_short_name => 'PA',
10718              p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
10719              p_token1         => 'PROCEDURENAME',
10720              p_value1         => l_module_name);
10721             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10722 
10723         END IF;
10724 
10725         l_copy_ci_ver_flag:='Y';
10726 
10727         l_ci_cost_version_id:=NULL;
10728         l_ci_rev_version_id:=NULL;
10729         l_ci_all_version_id:=NULL;
10730         IF ((NOT p_ci_cost_version_id_tbl.EXISTS(i)) OR p_ci_cost_version_id_tbl(i) IS NULL) AND
10731            ((NOT p_ci_rev_version_id_tbl.EXISTS(i)) OR p_ci_rev_version_id_tbl(i) IS NULL) AND
10732            ((NOT p_ci_all_version_id_tbl.EXISTS(i)) OR p_ci_all_version_id_tbl(i) IS NULL)THEN
10733 
10734             IF l_debug_mode = 'Y' THEN
10735                 pa_debug.g_err_stage:= 'Calling Pa_Fp_Control_Items_Utils.get_ci_versions';
10736                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10737             END IF;
10738 
10739            Pa_Fp_Control_Items_Utils.get_ci_versions
10740            ( p_ci_id                    => p_ci_id_tbl(i)
10741             ,X_cost_budget_version_id   => l_ci_cost_version_id
10742             ,X_rev_budget_version_id    => l_ci_rev_version_id
10743             ,X_all_budget_version_id    => l_ci_all_version_id
10744             ,x_return_status            => x_return_status
10745             ,x_msg_count                => x_msg_count
10746             ,x_msg_data                 => x_msg_data);
10747 
10748             --ci id will be skipped. Processing will continue with other cis
10749             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10750                 l_copy_ci_ver_flag:='N';
10751             END IF;
10752         ELSE
10753 
10754             IF p_ci_cost_version_id_tbl.EXISTS(i) THEN
10755                 l_ci_cost_version_id:= p_ci_cost_version_id_tbl(i);
10756             END IF;
10757 
10758             IF p_ci_rev_version_id_tbl.EXISTS(i) THEN
10759                 l_ci_rev_version_id:=p_ci_rev_version_id_tbl(i);
10760             END IF;
10761 
10762             IF p_ci_all_version_id_tbl.EXISTS(i) THEN
10763                 l_ci_all_version_id:=p_ci_all_version_id_tbl(i);
10764             END IF;
10765 
10766 
10767         END IF;
10768 
10769         --Check for the existence of rejection lines in the change order versions. If the rejection codes exist
10770         --then the change order is not eligible for merge.Derive ci number as it has to be passed as token to
10771         --error messages
10772         SELECT pci.ci_number,
10773                pct.ci_type_class_code
10774         INTO   l_ci_number,
10775                l_ci_type_class_code
10776         FROM   pa_control_items pci,
10777                pa_ci_types_b pct
10778         WHERE  pci.ci_id = p_ci_id_tbl(i)
10779         AND    pci.ci_type_id=pct.ci_type_id;
10780 
10781         IF l_copy_ci_ver_flag='Y' THEN
10782 
10783             IF l_debug_mode = 'Y' THEN
10784                 pa_debug.g_err_stage:= 'Checking for the existence of budget lines with rejection codes in ci version';
10785                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10786             END IF;
10787 
10788             OPEN c_chk_rej_codes(NVL(l_ci_cost_version_id,NVL(l_ci_all_version_id,l_ci_rev_version_id)),
10789                                  NVL(l_ci_rev_version_id,NVL(l_ci_all_version_id,l_ci_cost_version_id)));
10790             FETCH c_chk_rej_codes INTO l_dummy;
10791             IF c_chk_rej_codes%FOUND THEN
10792 
10793                 IF l_debug_mode = 'Y' THEN
10794                     pa_debug.g_err_stage:= 'budget lines with rejection codes EXIST in ci version';
10795                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10796                 END IF;
10797 
10798                 IF l_ci_type_class_code='CHANGE_ORDER' THEN
10799                      PA_UTILS.ADD_MESSAGE
10800                      (p_app_short_name => 'PA',
10801                       p_msg_name       => 'PA_FP_IMPL_CO_REJ_CODES_EXST',
10802                       p_token1         => 'CI_NUMBER',
10803                       p_value1         => l_ci_number);
10804                 ELSE
10805 
10806                      PA_UTILS.ADD_MESSAGE
10807                      (p_app_short_name => 'PA',
10808                       p_msg_name       => 'PA_FP_IMPL_CR_REJ_CODES_EXST',
10809                       p_token1         => 'CI_NUMBER',
10810                       p_value1         => l_ci_number);
10811 
10812                 END IF;
10813 
10814                 l_copy_ci_ver_flag:='N';
10815 
10816             END IF;
10817 
10818             CLOSE c_chk_rej_codes;
10819 
10820         END IF;
10821 
10822         -- For bug 3814932
10823         IF  l_copy_ci_ver_flag='Y' THEN
10824 
10825            IF p_context = 'PARTIAL_REV' Then
10826 
10827               IF l_debug_mode = 'Y' THEN
10828                  pa_debug.g_err_stage:= 'In the context of PARTIAL_REV';
10829                  pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10830               END IF;
10831 
10832               If px_partial_impl_rev_amt is null Then
10833 
10834                  PA_UTILS.ADD_MESSAGE
10835                              (p_app_short_name => 'PA',
10836                               p_msg_name       => 'PA_CI_PAR_REV_IMPL_AMT_NULL');
10837 
10838                  IF l_debug_mode = 'Y' THEN
10839                     pa_debug.g_err_stage:= 'px_partial_impl_rev_amt is null.';
10840                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10841                  END IF;
10842 
10843                  l_copy_ci_ver_flag:='N';
10844 
10845               End If;
10846 
10847               IF l_copy_ci_ver_flag <> 'N' THEN
10848 
10849                   -- Get the project id
10850 
10851                   SELECT project_id
10852                   INTO   l_project_id
10853                   FROM   pa_budget_versions
10854                   WHERE  budget_version_id=p_budget_version_id_tbl(i);
10855 
10856 
10857                   IF l_debug_mode = 'Y' THEN
10858                      pa_debug.g_err_stage:= 'Project id is:' ||l_project_id;
10859                      pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10860                   END IF;
10861 
10862                   --To get the total amount.
10863                   SELECT (sum(nvl(txn_revenue,0)) )
10864                   INTO l_total_amount
10865                   FROM  pa_budget_lines
10866                   WHERE budget_version_id = NVL(l_ci_all_version_id,l_ci_rev_version_id);
10867 
10868                   IF l_debug_mode = 'Y' THEN
10869                      pa_debug.g_err_stage:= 'Total Planned Revenue amount is:' ||l_total_amount;
10870                      pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10871                   END IF;
10872 
10873                   --To get implemented amount.
10874                   l_implemented_amount := Pa_Fp_Control_Items_Utils.get_impl_agr_revenue(
10875                                                                            p_project_id => l_project_id,
10876                                                                            p_ci_id      => p_ci_id_tbl(i) );
10877 
10878                   IF l_debug_mode = 'Y' THEN
10879                      pa_debug.g_err_stage:= 'Implemented Amount is:' ||l_implemented_amount;
10880                      pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10881                   END IF;
10882 
10883                   If l_total_amount = 0 Then
10884 
10885                       If(px_partial_impl_rev_amt<>0) Then
10886                            --Error
10887                          PA_UTILS.ADD_MESSAGE
10888                              (p_app_short_name => 'PA',
10889                               p_msg_name       => 'PA_CI_PAR_REV_AMT_NOT_ZERO');
10890 
10891                          IF l_debug_mode = 'Y' THEN
10892                             pa_debug.g_err_stage:= 'Partial implementation revenue cannot be anything other than 0';
10893                             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10894                          END IF;
10895 
10896                          l_copy_ci_ver_flag:='N';
10897 
10898                       End If;--If(px_partial_impl_rev_amt<>0) Then
10899 
10900                   End If; --End of l_total_amount = 0
10901 
10902               End If;--IF l_copy_ci_ver_flag <> 'N' THEN
10903 
10904 
10905               If l_copy_ci_ver_flag <> 'N' Then
10906 
10907                  If abs(l_implemented_amount)>abs(l_total_amount) Then
10908 
10909                     IF l_debug_mode = 'Y' THEN
10910                        pa_debug.g_err_stage:= 'Implemented amount is greater than total amount.';
10911                        pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10912                     END IF;
10913 
10914                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
10915 
10916                  End If;
10917 
10918               End If;
10919 
10920               If l_copy_ci_ver_flag <> 'N' Then
10921 
10922                  If l_total_amount >0 Then
10923 
10924                     If px_partial_impl_rev_amt <0 Then
10925                          --Error;
10926                        PA_UTILS.ADD_MESSAGE
10927                              (p_app_short_name => 'PA',
10928                               p_msg_name       => 'PA_CI_PAR_REV_AMT_NOT_POS');
10929 
10930                        IF l_debug_mode = 'Y' THEN
10931                           pa_debug.g_err_stage:= 'Partial implementation revenue amount is negetive';
10932                           pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10933                        END IF;
10934 
10935                        l_copy_ci_ver_flag:='N';
10936 
10937                     End If;--End of px_partial_impl_rev_amt <0
10938 
10939                     If l_copy_ci_ver_flag<>'N' Then
10940 
10941                        l_remaining_amount:=l_total_amount-l_implemented_amount;
10942 
10943                        IF l_debug_mode = 'Y' THEN
10944                           pa_debug.g_err_stage:= 'Remaining amount to be implemented is:' || l_remaining_amount;
10945                           pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10946                        END IF;
10947 
10948                        If px_partial_impl_rev_amt>l_remaining_amount Then
10949                           --Error;
10950                           PA_UTILS.ADD_MESSAGE
10951                              (p_app_short_name => 'PA',
10952                               p_msg_name       => 'PA_CI_PAR_REV_IMPL_AMT_GREATER');
10953 
10954                           IF l_debug_mode = 'Y' THEN
10955                              pa_debug.g_err_stage:= 'Partial impl rev is > Reamaining amount to be implemented';
10956                              pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10957                           END IF;
10958 
10959                           l_copy_ci_ver_flag:='N';
10960 
10961                        End If; --End of px_partial_impl_rev_amt>l_remaining_amount
10962 
10963                     End If; --End of l_copy_ci_ver_flag<>'N'
10964 
10965                  End If;   --End of l_total_amount>0
10966 
10967               End If;
10968 
10969               If l_copy_ci_ver_flag<>'N' Then
10970 
10971                  If l_total_amount<0 Then
10972 
10973                     If px_partial_impl_rev_amt>0 Then
10974                        --Error;
10975                        PA_UTILS.ADD_MESSAGE
10976                              (p_app_short_name => 'PA',
10977                               p_msg_name       => 'PA_CI_PAR_REV_AMT_NOT_NEG');
10978 
10979                        IF l_debug_mode = 'Y' THEN
10980                           pa_debug.g_err_stage:= 'Partial implementation revenue amount is positive';
10981                           pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10982                        END IF;
10983 
10984                        l_copy_ci_ver_flag:='N';
10985 
10986                     End If; --End of px_partial_impl_rev_amt>0
10987 
10988                     If l_copy_ci_ver_flag<>'N' Then
10989 
10990                        l_remaining_amount:=l_total_amount-l_implemented_amount;
10991 
10992                        IF l_debug_mode = 'Y' THEN
10993                           pa_debug.g_err_stage:= 'Remaining amount to be implemented is:' || l_remaining_amount;
10994                           pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
10995                        END IF;
10996 
10997                        If abs(px_partial_impl_rev_amt)>abs(l_remaining_amount) Then
10998                           --Error;
10999                           PA_UTILS.ADD_MESSAGE
11000                              (p_app_short_name => 'PA',
11001                               p_msg_name       => 'PA_CI_PAR_REV_IMPL_AMT_GREATER');
11002 
11003                           IF l_debug_mode = 'Y' THEN
11004                              pa_debug.g_err_stage:= 'Partial impl rev is > Reamaining amount to be implemented';
11005                              pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11006                           END IF;
11007 
11008                           l_copy_ci_ver_flag:='N';
11009 
11010                        End If;-- End of abs(px_partial_impl_rev_amt)>abs(l_remaining_amount)
11011 
11012                     End If; --End of l_copy_ci_ver_flag<>'N'
11013 
11014                  End If;  --l_total_amount<0
11015 
11016               End If;
11017 
11018            End If;  --end of p_context 'PARTIAL_REV'
11019 
11020         End If; --End of bug 3814932
11021 
11022 
11023         IF l_copy_ci_ver_flag='Y' THEN
11024 
11025             IF l_debug_mode = 'Y' THEN
11026                 pa_debug.g_err_stage:= 'l_copy_ci_ver_flag is Y Copying';
11027                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11028             END IF;
11029 
11030             x_ci_id_tbl.EXTEND(1);
11031             x_ci_cost_version_id_tbl.EXTEND(1);
11032             x_ci_rev_version_id_tbl.EXTEND(1);
11033             x_ci_all_version_id_tbl.EXTEND(1);
11034             x_ci_number.EXTEND(1);
11035             x_ci_id_tbl(x_ci_id_tbl.COUNT):=p_ci_id_tbl(i);
11036             x_ci_cost_version_id_tbl(x_ci_cost_version_id_tbl.COUNT):=l_ci_cost_version_id;
11037             x_ci_rev_version_id_tbl(x_ci_rev_version_id_tbl.COUNT):=l_ci_rev_version_id;
11038             x_ci_all_version_id_tbl(x_ci_all_version_id_tbl.COUNT):=l_ci_all_version_id;
11039 
11040             --Derive the ci number in case the API is called from the include change orders page
11041             IF (p_context = 'INCLUDE') THEN
11042 
11043                 x_ci_number(x_ci_number.COUNT):=l_ci_number;
11044 
11045             ELSIF (p_context = 'IMPL_FIN_IMPACT' OR
11046                    p_context = 'PARTIAL_REV') THEN
11047 
11048                 --In this context only one ci id will be passed always and hence we can fetch the agreement id
11049                 --into the scalar variable
11050                 IF x_agreement_id IS NULL  AND
11051                    nvl(l_ci_rev_version_id, l_ci_all_version_id) IS NOT NULL THEN
11052 
11053                     SELECT agreement_id
11054                     INTO   x_agreement_id
11055                     FROM   pa_budget_Versions
11056                     WHERE  budget_Version_id=nvl(l_ci_rev_version_id, l_ci_all_version_id);
11057 
11058                 END IF;
11059 
11060             END IF;
11061 
11062         END IF;--IF l_copy_ci_ver_flag='Y' THEN
11063 
11064     END LOOP;
11065 
11066     --dbms_output.put_line('5');
11067 
11068     IF l_debug_mode = 'Y' THEN
11069         pa_debug.g_err_stage:= 'Proceeding with the budget version loopn with count '||p_budget_version_id_tbl.count;
11070         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11071     END IF;
11072 
11073     --dbms_output.put_line('6');
11074 
11075     --Validate the passed target budget version ids. The budget version ids should not be in a submitted status and
11076     --and the version should not be already locked by some other user.
11077     FOR i IN p_budget_version_id_tbl.FIRST..p_budget_version_id_tbl.LAST LOOP
11078 
11079         IF l_debug_mode = 'Y' THEN
11080             pa_debug.g_err_stage:= 'p_budget_version_id_tbl ('||i||') is'||p_budget_version_id_tbl(i);
11081             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11082         END IF;
11083 
11084         --Derive the fin plan type name and id if they are not passed
11085         IF ((NOT p_fin_plan_type_id_tbl.EXISTS(i)) OR  p_fin_plan_type_id_tbl(i)  IS NULL) OR
11086            ((NOT p_fin_plan_type_name_tbl.EXISTS(i)) OR  p_fin_plan_type_name_tbl(i)  IS NULL) THEN
11087 
11088             SELECT fin.name,
11089                    fin.fin_plan_type_id
11090             INTO   l_fin_plan_type_name,
11091                    l_fin_plan_type_id
11092             FROM   pa_fin_plan_types_vl fin,
11093                    pa_budget_versions pbv
11094             WHERE  fin.fin_plan_type_id=pbv.fin_plan_type_id
11095             AND    pbv.budget_version_id=p_budget_version_id_tbl(i);
11096         ELSE
11097             l_fin_plan_type_name  := p_fin_plan_type_name_tbl(i);
11098             l_fin_plan_type_id    := p_fin_plan_type_id_tbl(i);
11099         END IF;
11100 
11101         l_error_occurred_flag:='N';
11102 
11103         --dbms_output.put_line('6.1 '||p_budget_version_id_tbl(i));
11104         IF NVL(p_budget_version_id_tbl(i),-1)=-1 THEN
11105             --The current working version does not exist
11106             IF l_debug_mode = 'Y' THEN
11107                 pa_debug.g_err_stage:= 'CWV doest not exist.adding msg to stack';
11108                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11109             END IF;
11110 
11111             PA_UTILS.ADD_MESSAGE
11112                         (p_app_short_name => 'PA',
11113                          p_msg_name       => 'PA_FP_CI_MRG_NO_CW_VER',
11114                          p_token1         => 'PLAN_TYPE',
11115                          p_value1         => l_fin_plan_type_name);
11116 
11117             IF l_debug_mode = 'Y' THEN
11118                 pa_debug.g_err_stage:= 'CWV doest not exist.added message to stack '|| l_fin_plan_type_name;
11119                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11120             END IF;
11121 
11122             l_error_occurred_flag:='Y';
11123         END IF;
11124 
11125         IF l_error_occurred_flag='N' THEN
11126 
11127             IF l_debug_mode = 'Y' THEN
11128                 pa_debug.g_err_stage:= 'Checking for S status';
11129                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11130             END IF;
11131 
11132 
11133             SELECT budget_status_code,
11134                    record_version_number,
11135                    project_id,
11136                    version_type,
11137                    plan_processing_code,
11138                    NVL(approved_cost_plan_type_flag,'N'),
11139                    NVL(approved_rev_plan_type_flag,'N')
11140             INTO   l_budget_status_code,
11141                    l_record_version_number,
11142                    l_project_id,
11143                    l_version_type,
11144                    l_targ_ver_plan_prc_code, -- for Bug 3986129
11145                    -- Bug 5845142
11146                    l_app_cost_plan_type_flag,
11147                    l_app_rev_plan_type_flag
11148             FROM   pa_budget_versions pbv
11149             WHERE  pbv.budget_version_id=p_budget_version_id_tbl(i);
11150 
11151             -- Bug 5845142. When the approved cost budget is setup to have "Cost and Revenue Together"
11152             -- plan it is not possible to include change orders. Change Orders can only be implemented.
11153             IF Pa_Fp_Control_Items_Utils.check_valid_combo
11154               ( p_project_id         => l_project_id
11155                ,p_targ_app_cost_flag => l_app_cost_plan_type_flag
11156                ,p_targ_app_rev_flag  => l_app_rev_plan_type_flag) = 'N' THEN
11157 
11158                   PA_UTILS.ADD_MESSAGE
11159                   (p_app_short_name => 'PA',
11160                    p_msg_name       => 'PA_FP_CANT_INCL_CO_UANPP_AMT');
11161 
11162 
11163                   l_error_occurred_flag:='Y';
11164 
11165             END IF;
11166 
11167             IF l_budget_status_code ='S' THEN
11168 
11169                 IF p_context='IMPL_FIN_IMPACT' OR
11170                    p_context='PARTIAL_REV' THEN
11171 
11172                     PA_UTILS.ADD_MESSAGE
11173                                 (p_app_short_name => 'PA',
11174                                  p_msg_name       => 'PA_FP_MERGE_SUBMIT',
11175                                  p_token1         => 'PLAN_TYPE',
11176                                  p_value1         =>  l_fin_plan_type_name);
11177 
11178                 ELSIF p_context ='INCLUDE' THEN
11179 
11180                     PA_UTILS.ADD_MESSAGE
11181                                 (p_app_short_name => 'PA',
11182                                  p_msg_name       => 'PA_FP_MERGE_ALL_SUBMIT');
11183 
11184 
11185                 END IF;
11186                 l_error_occurred_flag:='Y';
11187 
11188             END IF;
11189 
11190             -- Bug 3986129: Added the following check. If the target plan version is locked for concurrent
11191             -- processing or the concurrent processing for the version has failed, then merge should not be allowed.
11192             IF l_targ_ver_plan_prc_code = 'XLUP' THEN
11193                   IF l_debug_mode = 'Y' THEN
11194                       pa_debug.g_err_stage:= 'Version is locked for conc processing';
11195                       pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11196                   END IF;
11197 
11198                   PA_UTILS.ADD_MESSAGE
11199                        (p_app_short_name => 'PA',
11200                         p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
11201 
11202                   l_error_occurred_flag := 'Y';
11203 
11204             ELSIF l_targ_ver_plan_prc_code = 'XLUE' THEN
11205                   IF l_debug_mode = 'Y' THEN
11206                       pa_debug.g_err_stage:= 'Conc process for version has failed';
11207                       pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11208                   END IF;
11209 
11210                   PA_UTILS.ADD_MESSAGE
11211                        (p_app_short_name => 'PA',
11212                         p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
11213 
11214                   l_error_occurred_flag := 'Y';
11215             END IF; -- Bug 3986129: end.
11216 
11217         END IF;
11218 
11219         IF l_error_occurred_flag='N' THEN
11220              -- Partial Implementation is not allowed into a target 'ALL' version
11221              IF p_context='PARTIAL_REV' AND l_version_type = 'ALL' THEN
11222                 IF l_debug_mode = 'Y' THEN
11223                          pa_debug.g_err_stage:= 'Partial Implementation is not allowed into a target ALL version';
11224                          pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11225                 END IF;
11226 
11227                 PA_UTILS.ADD_MESSAGE
11228                 (p_app_short_name => 'PA',
11229                  p_msg_name       => 'PA_FP_NO_PART_INTO_TRG_ALL_PT',
11230                  p_token1         => 'PLAN_TYPE',
11231                  p_value1         =>  l_fin_plan_type_name);
11232 
11233                  l_error_occurred_flag := 'Y';
11234               END IF;
11235         END IF;
11236 
11237         IF l_error_occurred_flag='N' THEN
11238 
11239             IF l_debug_mode = 'Y' THEN
11240                 pa_debug.g_err_stage:= 'calling  lock unlock versions';
11241                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11242             END IF;
11243 
11244             --Acquire a lock on the target budget version.
11245             pa_fin_plan_pvt.lock_unlock_version
11246             ( p_budget_version_id     => p_budget_version_id_tbl(i)
11247              ,p_record_version_number => l_record_version_number
11248              ,p_action                => 'L'
11249              ,p_user_id               => fnd_global.user_id
11250              ,p_person_id             => NULL
11251              ,x_return_status         => x_return_status
11252              ,x_msg_count             => x_msg_count
11253              ,x_msg_data              => x_msg_data );
11254 
11255             --plan type id will be skipped. Processing will continue with other plan types
11256             IF x_return_status <> 'S' THEN
11257                 l_error_occurred_flag:='Y';
11258             END IF;
11259 
11260         END IF;
11261 
11262         IF l_debug_mode = 'Y' THEN
11263             pa_debug.g_err_stage:= 'After lock unlock verson msg pub count'||FND_MSG_PUB.count_msg;
11264             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11265 
11266             pa_debug.g_err_stage:= 'After lock unlock verson x_msg_data '||x_msg_data;
11267             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11268 
11269         END IF;
11270 
11271         --Check if the settings of the source and target version allow merge or not
11272         IF l_error_occurred_flag='N' THEN
11273 
11274             IF l_debug_mode = 'Y' THEN
11275                 pa_debug.g_err_stage:= 'Valid BV. About to include in o/p';
11276                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11277             END IF;
11278 
11279             x_budget_version_id_tbl.extend(1);
11280             l_impl_cost_flag_tbl.extend(1);
11281             l_impl_rev_flag_tbl.extend(1);
11282             x_submit_version_flag_tbl.extend(1);
11283             x_fin_plan_type_id_tbl.extend(1);
11284             x_fin_plan_type_name_tbl.extend(1);
11285             x_budget_version_id_tbl(x_budget_version_id_tbl.COUNT)     := p_budget_version_id_tbl(i);
11286             l_version_type_tbl.extend(1);
11287             l_version_type_tbl(l_version_type_tbl.COUNT) := l_version_type;
11288             l_fin_plan_type_name_tbl.extend(1);
11289             l_fin_plan_type_name_tbl(l_fin_plan_type_name_tbl.COUNT) := l_fin_plan_type_name;
11290 
11291             -- Bug 5845142
11292             l_app_cost_plan_type_flag_tbl.extend(1);
11293             l_app_cost_plan_type_flag_tbl(l_app_cost_plan_type_flag_tbl.COUNT) := l_app_cost_plan_type_flag;
11294             l_app_rev_plan_type_flag_tbl.extend(1);
11295             l_app_rev_plan_type_flag_tbl(l_app_rev_plan_type_flag_tbl.COUNT) := l_app_rev_plan_type_flag;
11296 
11297             IF l_debug_mode = 'Y' THEN
11298 
11299                 pa_debug.g_err_stage:= 'About to assign vars';
11300                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11301 
11302             END IF;
11303 
11304             IF p_impl_cost_flag_tbl.EXISTS(i) THEN
11305                 l_impl_cost_flag_tbl(l_impl_cost_flag_tbl.COUNT)           := p_impl_cost_flag_tbl(i);
11306             END IF;
11307 
11308             IF p_impl_rev_flag_tbl.EXISTS(i) THEN
11309                 l_impl_rev_flag_tbl(l_impl_rev_flag_tbl.COUNT)             := p_impl_rev_flag_tbl(i);
11310             END IF;
11311 
11312             IF p_submit_version_flag_tbl.EXISTS(i) THEN
11313                 x_submit_version_flag_tbl(x_submit_version_flag_tbl.COUNT) := p_submit_version_flag_tbl(i);
11314             END IF;
11315 
11316             x_fin_plan_type_name_tbl(x_fin_plan_type_name_tbl.COUNT):=l_fin_plan_type_name;
11317             x_fin_plan_type_id_tbl(x_fin_plan_type_id_tbl.COUNT):=l_fin_plan_type_id;
11318 
11319         END IF;
11320 
11321     END LOOP; --The loop for the budget versions
11322 
11323     --dbms_output.put_line('7');
11324 
11325     IF l_debug_mode = 'Y' THEN
11326 
11327         pa_debug.g_err_stage:= 'About to check for possibility of merge';
11328         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11329 
11330     END IF;
11331 
11332 
11333     --Check if the merge of ci into the target versions is possible or not
11334     IF x_ci_id_tbl.COUNT>0 AND  x_budget_version_id_tbl.COUNT>0 THEN
11335 
11336         FOR i IN x_ci_id_tbl.FIRST..x_ci_id_tbl.LAST LOOP
11337             --CBS 12.2 enhancement changes start here
11338 	    l_ci_cost_res_cls_flag := PA_FP_GEN_AMOUNT_UTILS.get_src_resource_class_flag(x_ci_cost_version_id_tbl(i));
11339 	    l_ci_rev_res_cls_flag := PA_FP_GEN_AMOUNT_UTILS.get_src_resource_class_flag(x_ci_rev_version_id_tbl(i));
11340 	    l_ci_all_res_cls_flag := PA_FP_GEN_AMOUNT_UTILS.get_src_resource_class_flag(x_ci_all_version_id_tbl(i));
11341 	    --CBS 12.2 enhancement changes end here
11342             IF l_debug_mode = 'Y' THEN
11343 
11344                 pa_debug.g_err_stage:= 'x_ci_cost_version_id_tbl('||i||') '||x_ci_cost_version_id_tbl(i);
11345                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11346 
11347                 pa_debug.g_err_stage:= 'x_ci_rev_version_id_tbl('||i||') '||x_ci_rev_version_id_tbl(i);
11348                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11349 
11350                 pa_debug.g_err_stage:= 'x_ci_all_version_id_tbl('||i||') '||x_ci_all_version_id_tbl(i);
11351                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11352 
11353             END IF;
11354 
11355             FOR j IN x_budget_version_id_tbl.FIRST..x_budget_version_id_tbl.LAST LOOP
11356 
11357                 --Prepare pl/sql tbl so that it can be passed to check_mrg_possible API to check whether
11358                 --the merge is possible between the source and target versions
11359                 --The order is important here. The order will COST REVENUE and ALL.
11360                 --The index in the source version id tbl for COST is 1, REVENUE will be
11361                 l_source_version_id_tbl :=SYSTEM.pa_num_tbl_type();
11362                 --CBS 12.2 enhancement changes start here
11363 		l_targ_resource_cls_flag := PA_FP_GEN_AMOUNT_UTILS.get_src_resource_class_flag(x_budget_version_id_tbl(j));
11364 		if ((x_ci_cost_version_id_tbl(i) IS NOT NULL AND l_ci_cost_res_cls_flag <> l_targ_resource_cls_flag)
11365 		    OR (x_ci_rev_version_id_tbl(i) IS NOT NULL AND l_ci_rev_res_cls_flag <> l_targ_resource_cls_flag)
11366 		    OR (x_ci_all_version_id_tbl(i) IS NOT NULL AND l_ci_all_res_cls_flag <> l_targ_resource_cls_flag))
11367 		then
11368 			PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PA',
11369 				p_msg_name       => 'PA_PS_FP_SOU_TAR_PRL_NOT_EQUAL');
11370 			RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11371 		end if;
11372 		--CBS 12.2 enhancement changes end here
11373                 IF  l_impl_cost_flag_tbl(j) IS NULL AND
11374                     l_impl_rev_flag_tbl(j) IS NULL THEN
11375 
11376                     IF l_debug_mode = 'Y' THEN
11377                         pa_debug.g_err_stage:= 'calling  get_impl_details';
11378                         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11379                     END IF;
11380 
11381                     pa_fp_control_items_utils.get_impl_details
11382                     ( P_fin_plan_type_id      => x_fin_plan_type_id_tbl(j)
11383                     ,P_project_id             => l_project_id
11384                     ,P_ci_id                  => x_ci_id_tbl(i)
11385                     ,P_ci_cost_version_id     => x_ci_cost_version_id_tbl(i)
11386                     ,P_ci_rev_version_id      => x_ci_rev_version_id_tbl(i)
11387                     ,P_ci_all_version_id      => x_ci_all_version_id_tbl(i)
11388                     ,p_targ_bv_id             => x_budget_version_id_tbl(j)
11389                     ,x_cost_impl_flag         => l_cost_impl_flag
11390                     ,x_rev_impl_flag          => l_rev_impl_flag
11391                     ,X_cost_impact_impl_flag  => l_cost_impact_impl_flag
11392                     ,x_rev_impact_impl_flag   => l_rev_impact_impl_flag
11393                     ,x_partially_impl_flag    => l_partially_impl_flag
11394                     ,x_agreement_num          => l_agreement_num
11395                     ,x_approved_fin_pt_id     => l_approved_fin_pt_id
11396                     ,x_return_status          => l_return_status
11397                     ,x_msg_data               => l_msg_data
11398                     ,x_msg_count              => l_msg_count);
11399 
11400                     --Return status check can be done in this case as the above should never return
11401                     --a return status of E.
11402                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11403                        IF P_PA_debug_mode = 'Y' THEN
11404                             pa_debug.g_err_stage:= 'pa_fp_control_items_utils.get_impl_details returned error';
11405                             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
11406                        END IF;
11407                        RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11408                     END IF;
11409                 ELSE
11410 
11411                     l_cost_impl_flag:=nvl(l_impl_cost_flag_tbl(j),'N');
11412                     l_rev_impl_flag:=nvl(l_impl_rev_flag_tbl(j),'N');
11413 
11414                 END IF;
11415 
11416                 IF l_debug_mode = 'Y' THEN
11417 
11418                     pa_debug.g_err_stage:= 'l_cost_impl_flag '||l_cost_impl_flag;
11419                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11420 
11421                     pa_debug.g_err_stage:= 'l_rev_impl_flag '||l_rev_impl_flag;
11422                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11423 
11424                 END IF;
11425 
11426                 l_part_impl_err := 'N';
11427 
11428                 --Bug 4351878. If the target version type is ALL then only cost and revenue together change orders
11429                 --can be merged into those versions.
11430                 IF l_version_type_tbl(j) = 'ALL' AND
11431                     (x_ci_all_version_id_tbl(i) IS NULL  OR
11432                      NVL(l_cost_impl_flag,'N') <> 'Y'    OR
11433                      (NVL(l_rev_impl_flag,'N') <> 'Y' AND
11434                       NVL(l_rev_impl_flag,'N') <> 'R')) THEN
11435 
11436                    --Bug 5845142. If the cost ci version is of TYPE ALL then the l_rev_impl_flag will be N.
11437                    --But in this case actually an ALL CI version is being implemented into ALL Cost Budget
11438                    --Version which is allowed. Eventhough l_rev_impl_flag is N implement_ci_into_single_ver
11439                    --internally takes care of setting it to Y and adding the revenue amounts too.
11440                    IF l_version_type_tbl(j) = 'ALL' AND
11441                       l_app_cost_plan_type_flag_tbl(j)='Y' AND
11442                       l_app_rev_plan_type_flag_tbl(j)='N' AND
11443                       x_ci_cost_version_id_tbl(i) IS NOT NULL THEN
11444 
11445                         NULL;
11446 
11447                    ELSE
11448 
11449                      IF l_debug_mode = 'Y' THEN
11450                               pa_debug.g_err_stage:= 'Implementation of cost or revenue alone is not allowed into a target ALL version';
11451                               pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11452                      END IF;
11453                      IF p_context = 'IMPL_FIN_IMPACT' THEN
11454 
11455                           PA_UTILS.ADD_MESSAGE
11456                           (p_app_short_name => 'PA',
11457                            p_msg_name       => 'PA_FP_ALL_CO_REQ_FOR_ALL_PT',
11458                            p_token1         => 'PLAN_TYPE',
11459                            p_value1         =>  l_fin_plan_type_name_tbl(j));
11460 
11461                      ELSIF p_context = 'INCLUDE' THEN
11462 
11463                           PA_UTILS.ADD_MESSAGE
11464                           (p_app_short_name => 'PA',
11465                            p_msg_name       => 'PA_FP_ALL_CO_REQ_FOR_VER');
11466 
11467                           /*SELECT cit.name INTO l_ci_name
11468                           FROM  pa_control_items pci, pa_ci_types_tl cit
11469                           WHERE pci.ci_id= x_ci_id_tbl(i)
11470                           AND  pci.ci_type_id=cit.ci_type_id
11471                           AND cit.language=userenv('LANG');
11472 
11473                           l_ci_name := l_ci_name ||' ('|| x_ci_number(i) || ')';
11474 
11475                           IF l_cost_impl_flag <> 'Y' THEN
11476 
11477                               PA_UTILS.ADD_MESSAGE
11478                               (p_app_short_name => 'PA',
11479                                p_msg_name       => 'PA_FP_NO_REV_INTO_TRG_ALL_CO',
11480                                p_token1         => 'CHG_ORDER',
11481                                p_value1         =>  l_ci_name);
11482 
11483                           ELSE
11484 
11485                               PA_UTILS.ADD_MESSAGE
11486                               (p_app_short_name => 'PA',
11487                                p_msg_name       => 'PA_FP_NO_COST_INTO_TRG_ALL_CO',
11488                                p_token1         => 'CHG_ORDER',
11489                                p_value1         =>  l_ci_name);
11490 
11491                           END IF;*/
11492 
11493                      END IF;
11494 
11495                      l_part_impl_err := 'Y';
11496 
11497                 END IF;
11498 
11499               END IF;
11500 
11501                 IF l_part_impl_err = 'N' THEN
11502                      IF NVL(x_ci_cost_version_id_tbl(i),-1)<>-1  AND l_cost_impl_flag = 'Y' THEN
11503 
11504                          l_source_version_id_tbl.EXTEND(1);
11505                          l_cost_ci_ver_index:=l_source_version_id_tbl.COUNT;
11506                          l_source_version_id_tbl(l_source_version_id_tbl.COUNT):=x_ci_cost_version_id_tbl(i);
11507 
11508                      END IF;
11509 
11510                      IF NVL(x_ci_rev_version_id_tbl(i),-1)<>-1   AND l_rev_impl_flag IN ('Y','R') THEN -- Bug 3732446 : Need to consider l_rev_impl_flag = 'R' also
11511 
11512                          l_source_version_id_tbl.EXTEND(1);
11513                          l_rev_ci_ver_index:=l_source_version_id_tbl.COUNT;
11514                          l_source_version_id_tbl(l_source_version_id_tbl.COUNT):=x_ci_rev_version_id_tbl(i);
11515 
11516                      END IF;
11517 
11518                      IF NVL(x_ci_all_version_id_tbl(i),-1)<>-1  AND
11519                         (l_cost_impl_flag = 'Y' OR l_rev_impl_flag IN ('Y','R') )THEN -- Bug 3732446 : Need to consider l_rev_impl_flag = 'R' also
11520 
11521                          l_source_version_id_tbl.EXTEND(1);
11522                          l_all_ci_ver_index:=l_source_version_id_tbl.COUNT;
11523                          l_source_version_id_tbl(l_source_version_id_tbl.COUNT):=x_ci_all_version_id_tbl(i);
11524 
11525                      END IF;
11526 
11527                      IF l_debug_mode = 'Y' THEN
11528 
11529                          pa_debug.g_err_stage:= 'Calling fp_ci_check_merge_possible';
11530                          pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11531 
11532                      END IF;
11533 
11534                      IF l_source_version_id_tbl.COUNT>0 THEN
11535 
11536                          IF l_debug_mode = 'Y' THEN
11537                              pa_debug.g_err_stage:= 'calling  fp_ci_check_merge_possible';
11538                              pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11539                          END IF;
11540 
11541                          pa_fp_control_items_utils.fp_ci_check_merge_possible(
11542                                       p_project_id               => l_project_id
11543                                      ,p_source_fp_version_id_tbl => l_source_version_id_tbl
11544                                      ,p_target_fp_version_id     => x_budget_version_id_tbl(j)
11545                                      ,p_calling_mode             => l_calling_mode_for_chk_api
11546                                      ,x_merge_possible_code_tbl  => l_merge_possible_code_tbl
11547                                      ,x_return_status            => x_return_status
11548                                      ,x_msg_count                => x_msg_count
11549                                      ,x_msg_data                 => x_msg_data );
11550                      END IF;
11551 
11552                      -- Return status check is not/should not be done since it would be E if merge is not possble.!
11553 
11554                      --Populate the record type tbl x_budget_ci_map_rec_tbl with the impact of the ci_id that can be
11555                      --implemented into the target budget version id
11556                      l_implementable_impact := 'NONE';
11557                      IF NVL(x_ci_cost_version_id_tbl(i),-1)<>-1  AND l_cost_impl_flag = 'Y' THEN
11558 
11559                          IF l_merge_possible_code_tbl(l_cost_ci_ver_index)='Y' THEN
11560                              l_implementable_impact:='COST';
11561                          END IF;
11562 
11563                          --Bug 5845142. This code should never get executed.
11564                          IF NVL(x_ci_rev_version_id_tbl(i),-1)=-1 AND l_rev_impl_flag ='Y' THEN
11565                              l_implementable_impact:='BOTH';
11566                          END IF;
11567 
11568                      END IF;
11569 
11570                      IF NVL(x_ci_rev_version_id_tbl(i),-1)<>-1   AND l_rev_impl_flag IN ('Y','R') THEN -- Bug 3732446 : Need to consider l_rev_impl_flag = 'R' also
11571 
11572                          IF l_merge_possible_code_tbl(l_rev_ci_ver_index)='Y' THEN
11573 
11574                              IF l_implementable_impact='COST' THEN
11575                                  --The COST impact is implementable. Since REVENUE is also implementable now
11576                                  --the implementation code will be BOTH now
11577                                  l_implementable_impact:='BOTH';
11578                              ELSE
11579                                  l_implementable_impact:='REVENUE';
11580                              END IF;
11581 
11582                          END IF;
11583 
11584                      END IF;
11585 
11586                      IF NVL(x_ci_all_version_id_tbl(i),-1)<>-1  AND
11587                         (l_cost_impl_flag = 'Y' OR l_rev_impl_flag IN ('Y','R') )THEN -- Bug 3732446 : Need to consider l_rev_impl_flag = 'R' also
11588 
11589                          IF l_merge_possible_code_tbl(l_all_ci_ver_index)='Y' THEN
11590                              l_implementable_impact:='ALL';
11591                          END IF;
11592 
11593                      END IF;
11594                 END IF;
11595 
11596                 l_index:= l_index+1;
11597                 -- Prevent the implementation of only cost or revenue into an all version
11598                 IF l_part_impl_err = 'Y' THEN
11599                     l_implementable_impact := 'NONE';
11600                 END IF;
11601 
11602                 --Record this impact in the output plsql tbl used for mapping the budget version id and ci id
11603                 IF l_debug_mode = 'Y' THEN
11604 
11605                     pa_debug.g_err_stage:= 'About to assign to the rec type';
11606                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11607 
11608                 END IF;
11609 
11610                 x_budget_ci_map_rec_tbl(l_index).budget_version_id:=x_budget_version_id_tbl(j);
11611                 x_budget_ci_map_rec_tbl(l_index).ci_id:=x_ci_id_tbl(i);
11612                 x_budget_ci_map_rec_tbl(l_index).impact_type_code:=l_implementable_impact;
11613                 x_budget_ci_map_rec_tbl(l_index).impl_cost_flag:=l_cost_impl_flag;
11614                 x_budget_ci_map_rec_tbl(l_index).impl_rev_flag:=l_rev_impl_flag;
11615 
11616             END LOOP;--Budget version loop
11617 
11618         END LOOP;--ci Loop
11619 
11620     END IF;--IF x_ci_id_tbl.COUNT>0 AND  x_budget_version_id_tbl.COUNT>0 THEN
11621 
11622     --dbms_output.put_line('8');
11623     IF l_debug_mode = 'Y' THEN
11624 
11625         pa_debug.g_err_stage:= 'Exiting validate_ci_merge_input_data';
11626         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11627 
11628     END IF;
11629 
11630     --Round the Partial revenue amount based on the agreement currency.
11631     IF p_context='PARTIAL_REV' AND px_partial_impl_rev_amt <> 0 THEN
11632 
11633         SELECT agreement_currency_code
11634         INTO   l_agr_curr_code
11635         FROM   pa_agreements_all
11636         WHERE  agreement_id=x_agreement_id;
11637 
11638         px_partial_impl_rev_amt :=Pa_currency.round_trans_currency_amt1(px_partial_impl_rev_amt,
11639                                                                         l_agr_curr_code);
11640 
11641     END IF;
11642 
11643     -- For Bug 3855500
11644     IF p_context='CI_MERGE' THEN
11645          l_msg_count_at_end := fnd_msg_pub.count_msg;
11646          IF l_init_msg_count <> l_msg_count_at_end THEN
11647 
11648             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11649 
11650         ELSE
11651 
11652             x_return_status:='S';
11653 
11654         END IF;
11655     ELSE
11656 
11657          --This is required as the x_return_status can be reset by the APIs being called from this API. The return status
11658          --will be E when the Invalid_Args_Exception is thrown and in this case the processing will be stopped
11659          x_return_status := 'S' ;
11660     END IF;
11661 
11662  IF p_pa_debug_mode = 'Y' THEN
11663     pa_debug.reset_curr_function;
11664  END IF;
11665     --dbms_output.put_line('9');
11666 
11667 EXCEPTION
11668 
11669     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
11670         l_msg_count := FND_MSG_PUB.count_msg;
11671         IF l_msg_count = 1 THEN
11672             PA_INTERFACE_UTILS_PUB.get_messages
11673                  (p_encoded        => FND_API.G_TRUE
11674                   ,p_msg_index      => 1
11675                   ,p_msg_count      => l_msg_count
11676                   ,p_msg_data       => l_msg_data
11677                   ,p_data           => l_data
11678                   ,p_msg_index_out  => l_msg_index_out);
11679             x_msg_data := l_data;
11680             x_msg_count := l_msg_count;
11681 
11682         ELSE
11683             x_msg_count := l_msg_count;
11684         END IF;
11685         x_return_status := FND_API.G_RET_STS_ERROR;
11686  IF p_pa_debug_mode = 'Y' THEN
11687         pa_debug.reset_curr_function;
11688 END IF;
11689     WHEN OTHERS THEN
11690 
11691         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11692         x_msg_count     := 1;
11693         x_msg_data      := SQLERRM;
11694         FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FP_CI_MERGE'
11695                           ,p_procedure_name  => 'validate_ci_merge_input_data');
11696 
11697         IF l_debug_mode = 'Y' THEN
11698             pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
11699             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
11700            pa_debug.reset_curr_function;
11701     END IF;
11702      RAISE;
11703 
11704 END validate_ci_merge_input_data;
11705 
11706 
11707 /*--------------------------------------------------------------------------------------------------------
11708  * Bug 3877815: New procedure introduced, to be called for merge of CIs for auto baseline enabled projects
11709  * This api does the followings:
11710  *   i. Copies the CI links of the current working version from the pa_fp_merged_ctrl_items(if there is any
11711  *      record present for that version) to temporary nested pl/sql tables.
11712  *  ii. Call is made to pa_fp_ci_implement_pkg.create_ci_impact_fund_lines and pa_baseline_funding_pkg.change_management_baseline.
11713  * iii. Insert 2 records into pa_fp_merged_ctrl_items for newly created current working version and the
11714  *      baselied version with all the attributes of the record stored in the nested tables except the
11715  *      inclusion_method_code, which would be 'COPIED' for the current working version and 'AUTOMATIC' for the baselined version.
11716  *  iv. Call is made to pa_fp_ci_merge.FP_CI_UPDATE_IMPACT.
11717  *--------------------------------------------------------------------------------------------------------*/
11718 
11719 PROCEDURE impl_ci_into_autobaseline_proj( p_ci_id                     IN     Pa_control_items.ci_id%TYPE --  The Id of the chg doc that needs to be implemented
11720                                          ,p_ci_rev_version_id         IN     Pa_budget_versions.budget_version_id%TYPE  DEFAULT  NULL -- The rev budget version id corresponding to the p_ci_id passed. This will be derived internally if not passed
11721                                          ,p_budget_version_id         IN     Pa_budget_versions.budget_version_id%TYPE -- The Id of the  budget version into which the CO needs to be implemented
11722                                          ,p_fin_plan_type_id          IN     pa_fin_plan_types_b.fin_plan_type_id%TYPE
11723                                          ,p_partial_impl_rev_amt      IN     NUMBER    DEFAULT  NULL -- The revenue amount that should be implemented into the target. This will be passed only in the case of partial implementation
11724                                          ,p_agreement_id              IN     Pa_agreements_all.agreement_id%TYPE  DEFAULT  NULL -- The id of the agreement that is linked with the CO.
11725                                          ,p_update_agreement_amt_flag IN     VARCHAR2  DEFAULT  NULL -- Indicates whether to  update the agreement amt or not. Null is considered as N
11726                                          ,p_funding_category          IN     VARCHAR2  DEFAULT  NULL -- The funding category for the agreement
11727                                          ,x_return_status             OUT    NOCOPY VARCHAR2 -- Indicates the exit status of the API --File.Sql.39 bug 4440895
11728                                          ,x_msg_data                  OUT    NOCOPY VARCHAR2 -- Indicates the error occurred --File.Sql.39 bug 4440895
11729                                          ,x_msg_count                 OUT    NOCOPY NUMBER)  -- Indicates the number of error messages --File.Sql.39 bug 4440895
11730 IS
11731       --Start of variables used for debugging
11732       l_msg_count                        NUMBER :=0;
11733       l_data                             VARCHAR2(2000);
11734       l_msg_data                         VARCHAR2(2000);
11735       l_error_msg_code                   VARCHAR2(30);
11736       l_msg_index_out                    NUMBER;
11737       l_return_status                    VARCHAR2(2000);
11738       l_debug_mode                       VARCHAR2(30);
11739       l_module_name                      VARCHAR2(100):='PAFPCIMB.impl_ci_into_autobaseline_proj';
11740       l_debug_level5                     NUMBER := 5;
11741       --End of variables used for debugging
11742 
11743       l_project_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11744       l_plan_version_id_tbl              SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11745       l_ci_id_tbl                        SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11746       l_ci_plan_ver_id_tbl               SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11747       l_record_ver_number_tbl            SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11748       l_creation_date_tbl                SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
11749       l_created_by_tbl                   SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11750       l_last_update_login_tbl            SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11751       l_last_updated_by_tbl              SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11752       l_last_update_date_tbl             SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
11753       l_incl_method_code_tbl             SYSTEM.PA_VARCHAR2_30_TBL_TYPE :=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
11754       l_incl_by_person_id_tbl            SYSTEM.PA_NUM_TBL_TYPE  :=SYSTEM.PA_NUM_TBL_TYPE();
11755       l_version_type_tbl                 SYSTEM.PA_VARCHAR2_30_TBL_TYPE :=  SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
11756       l_impl_proj_func_raw_cost_tbl      SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11757       l_impl_proj_func_burd_cost_tbl     SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11758       l_impl_proj_func_revenue_tbl       SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11759       l_impl_proj_raw_cost_tbl           SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11760       l_impl_proj_burd_cost_tbl          SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11761       l_impl_proj_revenue_tbl            SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11762       l_impl_quantity_tbl                SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11763       l_impl_equipment_quant_tbl         SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11764       l_impl_agr_revenue_tbl             SYSTEM.PA_NUM_TBL_TYPE  := SYSTEM.PA_NUM_TBL_TYPE();
11765 
11766       l_project_id                       pa_projects_all.project_id%TYPE;
11767 
11768       --Variable used for passing ci id to the change management baseline API.
11769       l_CI_ID_Tab                        PA_PLSQL_DATATYPES.IdTabTyp;
11770       X_Err_Code                         NUMBER;
11771 
11772       l_new_cw_version_id                pa_budget_versions.budget_version_id%TYPE;
11773       l_baseline_version_id              pa_budget_versions.budget_version_id%TYPE;
11774       l_fp_options_id                    pa_proj_fp_options.proj_fp_options_id%TYPE;
11775       -- Variable to be used for calling FP_CI_LINK_CONTROL_ITEMS
11776       l_rev_ppl_quantity                 NUMBER;
11777       l_rev_equip_quantity               NUMBER;
11778       l_impl_pfc_revenue                 NUMBER;
11779       l_impl_pc_revenue                  NUMBER;
11780 
11781       -- variables to hold the amounts before merge and after merge
11782       l_rev_ppl_quantity_bf_mg           NUMBER;
11783       l_rev_equip_quantity_bf_mg         NUMBER;
11784       l_impl_pfc_revenue_bf_mg           NUMBER;
11785       l_impl_pc_revenue_bf_mg            NUMBER;
11786 
11787       l_rev_ppl_quantity_af_mg           NUMBER;
11788       l_rev_equip_quantity_af_mg         NUMBER;
11789       l_impl_pfc_revenue_af_mg           NUMBER;
11790       l_impl_pc_revenue_af_mg            NUMBER;
11791 
11792       -- Variables used for partial revenue implementation
11793       l_partial_factor                   NUMBER;
11794       l_total_amount                     NUMBER;
11795       l_total_amount_in_pfc              NUMBER;
11796       l_total_amount_in_pc               NUMBER;
11797       l_impl_pc_rev_amt                  NUMBER;
11798       l_impl_pfc_rev_amt                 NUMBER;
11799       l_implemented_amt                  NUMBER := 0;
11800       l_implemented_pc_amt               NUMBER;
11801       l_implemented_pfc_amt              NUMBER;
11802       l_project_currency_code            pa_projects_all.project_currency_code%TYPE;
11803       l_projfunc_currency_code           pa_projects_all.project_currency_code%TYPE;
11804       l_ci_already_impl_flag             VARCHAR2(1) := 'N';
11805 
11806       l_final_rev_par_impl_flag          pa_budget_versions.rev_partially_impl_flag%TYPE;
11807       --Bug 4136238
11808       l_partial_impl_rev_amt             pa_budget_lines.txn_revenue%TYPE;
11809 
11810 BEGIN
11811       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
11812       l_debug_mode := NVL(l_debug_mode, 'Y');
11813       x_msg_count := 0;
11814       x_return_status := FND_API.G_RET_STS_SUCCESS;
11815 
11816      IF p_pa_debug_mode = 'Y' THEN
11817       PA_DEBUG.Set_Curr_Function( p_function   => l_module_name,
11818                                   p_debug_mode => l_debug_mode );
11819 
11820      END IF;
11821       FND_MSG_PUB.initialize;
11822 
11823       IF l_debug_mode = 'Y' THEN
11824             pa_debug.g_err_stage:= 'Entering impl_ci_into_autobaseline_proj';
11825             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11826             pa_debug.g_err_stage:= 'Copying data from pa_fp_merged_ctrl_items';
11827             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11828       END IF;
11829 
11830       SELECT
11831       project_id,
11832       plan_version_id,
11833       ci_id,
11834       ci_plan_version_id,
11835       record_version_number,
11836       creation_date,
11837       created_by,
11838       last_update_login,
11839       last_updated_by,
11840       last_update_date,
11841       inclusion_method_code,
11842       included_by_person_id,
11843       version_type,
11844       impl_proj_func_raw_cost,
11845       impl_proj_func_burdened_cost,
11846       impl_proj_func_revenue,
11847       impl_proj_raw_cost,
11848       impl_proj_burdened_cost,
11849       impl_proj_revenue,
11850       impl_quantity,
11851       impl_equipment_quantity,
11852       impl_agr_revenue
11853       BULK COLLECT INTO
11854       l_project_id_tbl,
11855       l_plan_version_id_tbl,
11856       l_ci_id_tbl,
11857       l_ci_plan_ver_id_tbl,
11858       l_record_ver_number_tbl,
11859       l_creation_date_tbl,
11860       l_created_by_tbl,
11861       l_last_update_login_tbl,
11862       l_last_updated_by_tbl,
11863       l_last_update_date_tbl,
11864       l_incl_method_code_tbl,
11865       l_incl_by_person_id_tbl,
11866       l_version_type_tbl,
11867       l_impl_proj_func_raw_cost_tbl,
11868       l_impl_proj_func_burd_cost_tbl,
11869       l_impl_proj_func_revenue_tbl,
11870       l_impl_proj_raw_cost_tbl,
11871       l_impl_proj_burd_cost_tbl,
11872       l_impl_proj_revenue_tbl,
11873       l_impl_quantity_tbl,
11874       l_impl_equipment_quant_tbl,
11875       l_impl_agr_revenue_tbl
11876       FROM   pa_fp_merged_ctrl_items
11877       WHERE  plan_version_id = p_budget_version_id;
11878 
11879       IF l_debug_mode = 'Y' THEN
11880             pa_debug.g_err_stage:= 'Copy from pa_fp_merged_ctrl_items done';
11881             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11882             pa_debug.g_err_stage:= 'No. of records copied: ' || l_plan_version_id_tbl.COUNT;
11883             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11884       END IF;
11885 
11886       IF l_debug_mode = 'Y' THEN
11887             pa_debug.g_err_stage:= 'Checking if the CI has been implemented before';
11888             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11889       END IF;
11890 
11891       --Bug 4153570.
11892       l_implemented_pc_amt :=0;
11893       l_implemented_pfc_amt := 0;
11894 
11895       IF l_plan_version_id_tbl.COUNT > 0 THEN
11896              FOR i IN l_plan_version_id_tbl.FIRST .. l_plan_version_id_tbl.LAST
11897              LOOP
11898                   IF l_ci_id_tbl(i) = p_ci_id AND
11899                      l_version_type_tbl(i) = 'REVENUE' THEN
11900                           l_ci_already_impl_flag := 'Y';
11901                           l_implemented_amt := l_impl_agr_revenue_tbl(i);
11902                           l_implemented_pc_amt := l_impl_proj_revenue_tbl(i);
11903                           l_implemented_pfc_amt := l_impl_proj_func_revenue_tbl(i);
11904                           EXIT;
11905                   END IF;
11906              END LOOP;
11907       END IF;
11908       l_implemented_amt := nvl(l_implemented_amt,0);
11909 
11910       IF l_ci_already_impl_flag = 'Y'THEN
11911             IF l_debug_mode = 'Y' THEN
11912                  pa_debug.g_err_stage:= 'The CI has been implemented before';
11913                  pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11914             END IF;
11915       ELSE
11916             IF l_debug_mode = 'Y' THEN
11917                  pa_debug.g_err_stage:= 'The CI has NOT been implemented before';
11918                  pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11919             END IF;
11920       END IF;
11921 
11922       IF l_debug_mode = 'Y' THEN
11923             pa_debug.g_err_stage:= 'Getting the amounts of budget version before merge';
11924             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11925       END IF;
11926 
11927 
11928       SELECT Nvl(pbv.labor_quantity, 0),
11929              Nvl(pbv.equipment_quantity, 0),
11930              Nvl(pbv.revenue, 0),
11931              Nvl(pbv.total_project_revenue, 0),
11932              pbv.project_id,
11933              p.project_currency_code,
11934              p.projfunc_currency_code
11935       INTO   l_rev_ppl_quantity_bf_mg,
11936              l_rev_equip_quantity_bf_mg,
11937              l_impl_pfc_revenue_bf_mg,
11938              l_impl_pc_revenue_bf_mg,
11939              l_project_id,
11940              l_project_currency_code,
11941              l_projfunc_currency_code
11942       FROM   pa_budget_versions pbv,
11943              pa_projects_all p
11944       WHERE  pbv.project_id = p.project_id
11945       AND    pbv.budget_version_id = p_budget_version_id;
11946 
11947       IF l_debug_mode = 'Y' THEN
11948             pa_debug.g_err_stage:= 'Deriving l_partial_factor';
11949             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
11950       END IF;
11951 
11952       --Bug 4136238. Find out the total amounts for CI in pc/pfc/txn amounts. This will be used in deriving the amounts
11953       --that will be finally implemented.
11954       SELECT nvl(sum(txn_revenue),0) total_amt
11955             ,nvl(sum(revenue),0)  total_amt_in_pfc
11956             ,nvl(sum(project_revenue),0) total_amt_in_pc
11957       INTO   l_total_amount,
11958              l_total_amount_in_pfc,
11959              l_total_amount_in_pc
11960       FROM   pa_budget_lines
11961       WHERE  budget_version_id = p_ci_rev_version_id;
11962 
11963       --Bug 4136238. p_partial_impl_rev_amt will be NULL or 0 only if the full impact is being implemented. In this
11964       --funding lines should be created for the whole amount in the CI version.
11965       IF p_partial_impl_rev_amt IS NULL OR
11966          p_partial_impl_rev_amt = 0 THEN
11967             l_partial_factor := 1;
11968             l_partial_impl_rev_amt := l_total_amount;
11969             l_impl_pc_rev_amt := l_total_amount_in_pc;
11970             l_impl_pfc_rev_amt := l_total_amount_in_pfc;
11971 
11972       ELSE
11973             --This means that the total revenue amount for implementation is also 0 . It could be that
11974             --BLs exist with +ve and -ve amounts with the total sum being 0.
11975             IF l_total_amount = 0 THEN
11976                  l_partial_factor := 1;
11977                  l_impl_pc_rev_amt := l_total_amount_in_pc;
11978                  l_impl_pfc_rev_amt := l_total_amount_in_pfc;
11979             ELSE
11980                  l_partial_factor := p_partial_impl_rev_amt/(l_total_amount);
11981 
11982                  --In case of last implementation i.e. all the amount that is left is being implemented, the pc/pfc amounts
11983                  --should be the <total amount> -<amount already implemented>
11984                  IF Nvl(l_implemented_amt, 0) + p_partial_impl_rev_amt = l_total_amount THEN
11985 
11986                      l_impl_pc_rev_amt := l_total_amount_in_pc - l_implemented_pc_amt;
11987                      l_impl_pfc_rev_amt := l_total_amount_in_pfc - l_implemented_pfc_amt;
11988 
11989                  ELSE
11990 
11991                      l_impl_pc_rev_amt := Pa_currency.round_trans_currency_amt1(l_total_amount_in_pc * l_partial_factor,
11992                                                                                 l_project_currency_code);
11993                      l_impl_pfc_rev_amt :=Pa_currency.round_trans_currency_amt1(l_total_amount_in_pfc * l_partial_factor,
11994                                                                                 l_projfunc_currency_code);
11995                  END IF;
11996             END IF;
11997             --Bug 4136238
11998             l_partial_impl_rev_amt := p_partial_impl_rev_amt;
11999       END IF;
12000 
12001       IF l_debug_mode = 'Y' THEN
12002             pa_debug.g_err_stage:= 'l_partial_factor derivation done and is: ' || l_partial_factor;
12003             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12004       END IF;
12005 
12006 
12007       IF l_debug_mode = 'Y' THEN
12008             pa_debug.g_err_stage:= 'Got the amounts of budget version before merge';
12009             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12010             pa_debug.g_err_stage:= 'Calling pa_fp_ci_implement_pkg.create_ci_impact_fund_lines';
12011             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12012       END IF;
12013 
12014       pa_fp_ci_implement_pkg.create_ci_impact_fund_lines
12015                         (p_project_id             => l_project_id,
12016                          p_ci_id                  => p_ci_id,
12017                          p_update_agr_amount_flag => P_update_agreement_amt_flag,
12018                          p_funding_category       => p_funding_category,
12019                          p_partial_factor         => l_partial_factor,
12020                          p_impl_txn_rev_amt       => l_partial_impl_rev_amt,
12021                          p_impl_pc_rev_amt        => l_impl_pc_rev_amt,
12022                          p_impl_pfc_rev_amt       => l_impl_pfc_rev_amt,
12023                          x_msg_data               => l_msg_data,
12024                          x_msg_count              => l_msg_count,
12025                          x_return_status          => l_return_status);
12026 
12027                          IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
12028                               IF P_PA_debug_mode = 'Y' THEN
12029                                  pa_debug.g_err_stage:= 'Error in create_ci_impact_fund_lines';
12030                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
12031                               END IF;
12032                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12033                          END IF;
12034 
12035       IF l_debug_mode = 'Y' THEN
12036             pa_debug.g_err_stage:= 'Call to pa_fp_ci_implement_pkg.create_ci_impact_fund_lines done';
12037             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12038       END IF;
12039 
12040       IF l_debug_mode = 'Y' THEN
12041             pa_debug.g_err_stage:= 'Calling pa_baseline_funding_pkg.change_management_baseline';
12042             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12043       END IF;
12044 
12045       l_CI_ID_Tab.delete;
12046       l_CI_ID_Tab(1):=p_ci_id;
12047 
12048       pa_baseline_funding_pkg.change_management_baseline
12049                          (P_Project_ID   => l_project_id,
12050                           P_CI_ID_Tab    => l_CI_ID_Tab,
12051                           X_Err_Code     => X_Err_Code,
12052                           X_Status       => l_return_status);
12053 
12054                          IF X_Err_Code <>  0 THEN
12055                               IF P_PA_debug_mode = 'Y' THEN
12056                                     pa_debug.g_err_stage:= 'Error in change_management_baseline';
12057                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
12058                               END IF;
12059                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12060                          END IF;
12061 
12062       IF l_debug_mode = 'Y' THEN
12063             pa_debug.g_err_stage:= 'Call to pa_baseline_funding_pkg.change_management_baseline done';
12064             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12065       END IF;
12066 
12067       IF l_debug_mode = 'Y' THEN
12068             pa_debug.g_err_stage:= 'Deriving new budget version ids';
12069             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12070       END IF;
12071 
12072       /* Calling the following apis with hard coded version_type as this api would be
12073        * only called for merge of revenue impacts into the revenue current working/baselined
12074        * versions of an autobaselined enabled project
12075        */
12076       pa_fin_plan_utils.Get_Curr_Working_Version_Info
12077                            (p_project_id          => l_project_id
12078                            ,p_fin_plan_type_id    => p_fin_plan_type_id
12079                            ,p_version_type        => 'REVENUE'
12080                            ,x_fp_options_id       => l_fp_options_id
12081                            ,x_fin_plan_version_id => l_new_cw_version_id
12082                            ,x_return_status       => l_return_status
12083                            ,x_msg_count           => l_msg_count
12084                            ,x_msg_data            => l_msg_data);
12085 
12086                            IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
12087                               IF P_PA_debug_mode = 'Y' THEN
12088                                  pa_debug.g_err_stage:= 'Error in Get_Curr_Working_Version_Info';
12089                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
12090                               END IF;
12091                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12092                          END IF;
12093 
12094       IF l_debug_mode = 'Y' THEN
12095             pa_debug.g_err_stage:= 'New current working version id' || l_new_cw_version_id ;
12096             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12097       END IF;
12098 
12099       pa_fin_plan_utils.Get_Baselined_Version_Info
12100                            (p_project_id          => l_project_id
12101                            ,p_fin_plan_type_id    => p_fin_plan_type_id
12102                            ,p_version_type        => 'REVENUE'
12103                            ,x_fp_options_id       => l_fp_options_id
12104                            ,x_fin_plan_version_id => l_baseline_version_id
12105                            ,x_return_status       => l_return_status
12106                            ,x_msg_count           => l_msg_count
12107                            ,x_msg_data            => l_msg_data);
12108 
12109                            IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
12110                               IF P_PA_debug_mode = 'Y' THEN
12111                                  pa_debug.g_err_stage:= 'Error in Get_Baselined_Version_Info';
12112                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
12113                               END IF;
12114                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12115                            END IF;
12116 
12117       IF l_debug_mode = 'Y' THEN
12118             pa_debug.g_err_stage:= 'New baseline version id' || l_baseline_version_id ;
12119             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12120       END IF;
12121 
12122       IF l_plan_version_id_tbl.COUNT > 0 THEN
12123             IF l_debug_mode = 'Y' THEN
12124                   pa_debug.g_err_stage:= 'Inserting into pa_fp_merged_ctrl_items with old data';
12125                   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12126             END IF;
12127             -- For the current working version
12128             FORALL i IN l_plan_version_id_tbl.FIRST .. l_plan_version_id_tbl.LAST
12129                   INSERT  INTO pa_fp_merged_ctrl_items
12130                           (project_id,
12131                           plan_version_id,
12132                           ci_id,
12133                           ci_plan_version_id,
12134                           record_version_number,
12135                           creation_date,
12136                           created_by,
12137                           last_update_login,
12138                           last_updated_by,
12139                           last_update_date,
12140                           inclusion_method_code,
12141                           included_by_person_id,
12142                           version_type,
12143                           impl_proj_func_raw_cost,
12144                           impl_proj_func_burdened_cost,
12145                           impl_proj_func_revenue,
12146                           impl_proj_raw_cost,
12147                           impl_proj_burdened_cost,
12148                           impl_proj_revenue,
12149                           impl_quantity,
12150                           impl_equipment_quantity,
12151                           impl_agr_revenue)
12152                   VALUES (l_project_id_tbl(i),
12153                           l_new_cw_version_id, -- The new current working version id
12154                           l_ci_id_tbl(i),
12155                           l_ci_plan_ver_id_tbl(i),
12156                           1, -- Bug 3877815: Review comment
12157                           l_creation_date_tbl(i),
12158                           l_created_by_tbl(i),
12159                           FND_GLOBAL.login_id,
12160                           FND_GLOBAL.user_id,
12161                           SYSDATE,
12162                           l_incl_method_code_tbl(i),
12163                           l_incl_by_person_id_tbl(i),
12164                           l_version_type_tbl(i),
12165                           l_impl_proj_func_raw_cost_tbl(i),
12166                           l_impl_proj_func_burd_cost_tbl(i),
12167                           l_impl_proj_func_revenue_tbl(i),
12168                           l_impl_proj_raw_cost_tbl(i),
12169                           l_impl_proj_burd_cost_tbl(i),
12170                           l_impl_proj_revenue_tbl(i),
12171                           l_impl_quantity_tbl(i),
12172                           l_impl_equipment_quant_tbl(i),
12173                           l_impl_agr_revenue_tbl(i));
12174 
12175             -- For the baseline version
12176 
12177             FORALL i IN l_plan_version_id_tbl.FIRST .. l_plan_version_id_tbl.LAST
12178                   INSERT  INTO pa_fp_merged_ctrl_items
12179                           (project_id,
12180                           plan_version_id,
12181                           ci_id,
12182                           ci_plan_version_id,
12183                           record_version_number,
12184                           creation_date,
12185                           created_by,
12186                           last_update_login,
12187                           last_updated_by,
12188                           last_update_date,
12189                           inclusion_method_code,
12190                           included_by_person_id,
12191                           version_type,
12192                           impl_proj_func_raw_cost,
12193                           impl_proj_func_burdened_cost,
12194                           impl_proj_func_revenue,
12195                           impl_proj_raw_cost,
12196                           impl_proj_burdened_cost,
12197                           impl_proj_revenue,
12198                           impl_quantity,
12199                           impl_equipment_quantity,
12200                           impl_agr_revenue)
12201                   VALUES (l_project_id_tbl(i),
12202                           l_baseline_version_id, -- The baseline version id
12203                           l_ci_id_tbl(i),
12204                           l_ci_plan_ver_id_tbl(i),
12205                           1,
12206                           SYSDATE,
12207                           FND_GLOBAL.user_id,
12208                           FND_GLOBAL.login_id,
12209                           FND_GLOBAL.user_id,
12210                           SYSDATE,
12211                           'AUTOMATIC', -- Bug 3877815: Review comment
12212                           l_incl_by_person_id_tbl(i),
12213                           l_version_type_tbl(i),
12214                           l_impl_proj_func_raw_cost_tbl(i),
12215                           l_impl_proj_func_burd_cost_tbl(i),
12216                           l_impl_proj_func_revenue_tbl(i),
12217                           l_impl_proj_raw_cost_tbl(i),
12218                           l_impl_proj_burd_cost_tbl(i),
12219                           l_impl_proj_revenue_tbl(i),
12220                           l_impl_quantity_tbl(i),
12221                           l_impl_equipment_quant_tbl(i),
12222                           l_impl_agr_revenue_tbl(i));
12223       ELSE
12224             IF l_debug_mode = 'Y' THEN
12225                   pa_debug.g_err_stage:= 'No Data stored in tmp tables';
12226                   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12227             END IF;
12228       END IF;
12229 
12230       IF l_debug_mode = 'Y' THEN
12231             pa_debug.g_err_stage:= 'Getting the amounts after merge for the budget version';
12232             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12233       END IF;
12234 
12235       SELECT Nvl(labor_quantity, 0),
12236              Nvl(equipment_quantity, 0),
12237              Nvl(revenue, 0),
12238              Nvl(total_project_revenue, 0),
12239              Nvl(rev_partially_impl_flag, 'N')
12240       INTO   l_rev_ppl_quantity_af_mg,
12241              l_rev_equip_quantity_af_mg,
12242              l_impl_pfc_revenue_af_mg,
12243              l_impl_pc_revenue_af_mg,
12244              l_final_rev_par_impl_flag
12245       FROM   pa_budget_versions
12246       WHERE  project_id = l_project_id
12247       AND    budget_version_id = l_new_cw_version_id;
12248 
12249       IF l_debug_mode = 'Y' THEN
12250             pa_debug.g_err_stage:= 'Values obtained after merge for the budget version';
12251             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12252             pa_debug.g_err_stage:= 'Getting the diff of amounts before and after merge for the budget version';
12253             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12254       END IF;
12255 
12256       l_rev_ppl_quantity   := l_rev_ppl_quantity_af_mg - l_rev_ppl_quantity_bf_mg;
12257       l_rev_equip_quantity := l_rev_equip_quantity_af_mg - l_rev_equip_quantity_bf_mg;
12258       l_impl_pfc_revenue   := l_impl_pfc_revenue_af_mg - l_impl_pfc_revenue_bf_mg;
12259       l_impl_pc_revenue    := l_impl_pc_revenue_af_mg - l_impl_pc_revenue_bf_mg;
12260 
12261       IF l_ci_already_impl_flag = 'Y' THEN
12262             IF l_debug_mode = 'Y' THEN
12263                   pa_debug.g_err_stage:= 'Updating record if there is a record for the CI';
12264                   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12265             END IF;
12266 
12267             -- For Current working version
12268             UPDATE pa_fp_merged_ctrl_items
12269             SET    impl_proj_func_revenue  = (Nvl(impl_proj_func_revenue,0) + l_impl_pfc_revenue),
12270                    impl_proj_revenue       = (Nvl(impl_proj_revenue,0) + l_impl_pc_revenue),
12271                    impl_quantity           = (Nvl(impl_quantity,0) + l_rev_ppl_quantity),
12272                    impl_equipment_quantity = (Nvl(impl_equipment_quantity,0) + l_rev_equip_quantity),
12273                    impl_agr_revenue        = (Nvl(l_implemented_amt,0) + Nvl(l_partial_impl_rev_amt,0)),
12274                    record_version_number   = (Nvl(record_version_number, 0) + 1),
12275                    last_update_login       = FND_GLOBAL.login_id,
12276                    last_updated_by         = FND_GLOBAL.user_id,
12277                    last_update_date        = SYSDATE
12278             WHERE  project_id = l_project_id
12279             AND    ci_id = p_ci_id
12280             AND    plan_version_id = l_new_cw_version_id
12281             AND    version_type = 'REVENUE';
12282 
12283              -- For baselined version
12284             UPDATE pa_fp_merged_ctrl_items
12285             SET    impl_proj_func_revenue  = (Nvl(impl_proj_func_revenue,0) + l_impl_pfc_revenue),
12286                    impl_proj_revenue       = (Nvl(impl_proj_revenue,0) + l_impl_pc_revenue),
12287                    impl_quantity           = (Nvl(impl_quantity,0) + l_rev_ppl_quantity),
12288                    impl_equipment_quantity = (Nvl(impl_equipment_quantity,0) + l_rev_equip_quantity),
12289                    impl_agr_revenue        = (Nvl(l_implemented_amt,0) + Nvl(l_partial_impl_rev_amt,0)),
12290                    record_version_number   = (Nvl(record_version_number, 0) + 1),
12291                    last_update_login       = FND_GLOBAL.login_id,
12292                    last_updated_by         = FND_GLOBAL.user_id,
12293                    last_update_date        = SYSDATE
12294             WHERE  project_id = l_project_id
12295             AND    ci_id = p_ci_id
12296             AND    plan_version_id = l_baseline_version_id
12297             AND    version_type = 'REVENUE';
12298 
12299             IF l_debug_mode = 'Y' THEN
12300                   pa_debug.g_err_stage:= 'Updation of record is done for the CI';
12301                   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12302             END IF;
12303 
12304       ELSE -- There is no record present in pa_fp_merged_ctrl_items for the ci_id
12305             IF l_debug_mode = 'Y' THEN
12306                   pa_debug.g_err_stage:= 'Calling FP_CI_LINK_CONTROL_ITEMS for the CI which has been merged';
12307                   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12308             END IF;
12309             -- For the current working version
12310             FP_CI_LINK_CONTROL_ITEMS
12311               (
12312                 p_project_id         => l_project_id
12313                ,p_s_fp_version_id    => p_ci_rev_version_id
12314                ,p_t_fp_version_id    => l_new_cw_version_id
12315                ,p_inclusion_method   => 'COPIED'
12316                ,p_included_by        => NULL
12317                ,p_version_type       => 'REVENUE'
12318                ,p_ci_id              => p_ci_id
12319                ,p_cost_ppl_qty       => NULL
12320                ,p_rev_ppl_qty        => l_rev_ppl_quantity
12321                ,p_cost_equip_qty     => NULL
12322                ,p_rev_equip_qty      => l_rev_equip_quantity
12323                ,p_impl_pfc_raw_cost  => NULL
12324                ,p_impl_pfc_revenue   => l_impl_pfc_revenue
12325                ,p_impl_pfc_burd_cost => NULL
12326                ,p_impl_pc_raw_cost   => NULL
12327                ,p_impl_pc_revenue    => l_impl_pc_revenue
12328                ,p_impl_pc_burd_cost  => NULL
12329                ,p_impl_agr_revenue   => Nvl(l_partial_impl_rev_amt,0) --Bug 3877815: Review comment
12330                ,x_return_status      => l_return_status
12331                ,x_msg_count          => l_msg_count
12332                ,x_msg_data           => l_msg_data);
12333 
12334               IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
12335                     IF l_debug_mode = 'Y' THEN
12336                           pa_debug.g_err_stage:= 'Error calling FP_CI_LINK_CONTROL_ITEMS';
12337                           pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
12338                     END IF;
12339                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12340               END IF;
12341 
12342               -- For the baseline version
12343             FP_CI_LINK_CONTROL_ITEMS
12344               (
12345                 p_project_id         => l_project_id
12346                ,p_s_fp_version_id    => p_ci_rev_version_id
12347                ,p_t_fp_version_id    => l_baseline_version_id
12348                ,p_inclusion_method   => 'AUTOMATIC'
12349                ,p_included_by        => NULL
12350                ,p_version_type       => 'REVENUE'
12351                ,p_ci_id              => p_ci_id
12352                ,p_cost_ppl_qty       => NULL
12353                ,p_rev_ppl_qty        => l_rev_ppl_quantity
12354                ,p_cost_equip_qty     => NULL
12355                ,p_rev_equip_qty      => l_rev_equip_quantity
12356                ,p_impl_pfc_raw_cost  => NULL
12357                ,p_impl_pfc_revenue   => l_impl_pfc_revenue
12358                ,p_impl_pfc_burd_cost => NULL
12359                ,p_impl_pc_raw_cost   => NULL
12360                ,p_impl_pc_revenue    => l_impl_pc_revenue
12361                ,p_impl_pc_burd_cost  => NULL
12362                ,p_impl_agr_revenue   => Nvl(l_partial_impl_rev_amt,0) --Bug 3877815: Review comment
12363                ,x_return_status      => l_return_status
12364                ,x_msg_count          => l_msg_count
12365                ,x_msg_data           => l_msg_data);
12366 
12367                IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
12368                      IF l_debug_mode = 'Y' THEN
12369                            pa_debug.g_err_stage:= 'Error calling FP_CI_LINK_CONTROL_ITEMS';
12370                            pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
12371                      END IF;
12372                      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12373                END IF;
12374 
12375                IF l_debug_mode = 'Y' THEN
12376                      pa_debug.g_err_stage:= 'Call to FP_CI_LINK_CONTROL_ITEMS DONE';
12377                      pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12378                END IF;
12379       END IF;
12380 
12381       IF l_debug_mode = 'Y' THEN
12382             pa_debug.g_err_stage:= 'Calling pa_fp_ci_merge.FP_CI_UPDATE_IMPACT';
12383             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12384       END IF;
12385 
12386       pa_fp_ci_merge.FP_CI_UPDATE_IMPACT
12387                         (p_ci_id                 => p_ci_id
12388                         ,p_status_code           => 'CI_IMPACT_IMPLEMENTED'
12389                         ,p_implemented_by        => FND_GLOBAL.USER_ID
12390                         ,p_impact_type_code      => 'FINPLAN_REVENUE'
12391                         ,p_commit_flag           => 'N'
12392                         ,p_init_msg_list         => 'N'
12393                         ,p_record_version_number => null
12394                         ,x_return_status         => l_return_status
12395                         ,x_msg_count             => l_msg_count
12396                         ,x_msg_data              => l_msg_data);
12397 
12398                         IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
12399                               IF P_PA_debug_mode = 'Y' THEN
12400                                  pa_debug.g_err_stage:= 'Error in FP_CI_UPDATE_IMPACT';
12401                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
12402                               END IF;
12403                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12404                         END IF;
12405       IF l_debug_mode = 'Y' THEN
12406             pa_debug.g_err_stage:= 'Call to pa_fp_ci_merge.FP_CI_UPDATE_IMPACT done';
12407             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12408       END IF;
12409       -- Bug 3877815: Review comment
12410 
12411       IF l_debug_mode = 'Y' THEN
12412             pa_debug.g_err_stage:= '-------l_implemented_amt is: ' || l_implemented_amt;
12413             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12414             pa_debug.g_err_stage:= '-------l_partial_impl_rev_amt is: ' || l_partial_impl_rev_amt;
12415             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12416             pa_debug.g_err_stage:= '-------l_total_amount is: ' || l_total_amount;
12417             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12418       END IF;
12419       IF (Nvl(l_implemented_amt,0) + Nvl(l_partial_impl_rev_amt,0)) = l_total_amount THEN
12420       --setting rev impl flag to N.
12421            IF l_debug_mode = 'Y' THEN
12422                  pa_debug.g_err_stage:='Setting rev_impl_flag to N after autobaseline call';
12423                  pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12424            END IF;
12425            UPDATE pa_budget_versions
12426            SET    rev_partially_impl_flag ='N'
12427                  ,record_version_number   = record_version_number+1
12428                  ,last_update_date        = sysdate
12429                  ,last_update_login       = fnd_global.login_id
12430                  ,last_updated_by         = fnd_global.user_id
12431            WHERE  budget_version_id       = p_ci_rev_version_id;
12432 
12433            IF l_debug_mode = 'Y' THEN
12434                 pa_debug.g_err_stage:='rev_impl_flag set to N ';
12435                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12436            END IF;
12437       ELSE
12438            IF l_final_rev_par_impl_flag <> 'Y' THEN
12439                IF l_debug_mode = 'Y' THEN
12440                      pa_debug.g_err_stage:='Setting rev_impl_flag to Y after autobaseline call';
12441                      pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12442                END IF;
12443                UPDATE pa_budget_versions
12444                SET    rev_partially_impl_flag ='Y'
12445                      ,record_version_number   = record_version_number+1
12446                      ,last_update_date        = sysdate
12447                      ,last_update_login       = fnd_global.login_id
12448                      ,last_updated_by         = fnd_global.user_id
12449                WHERE  budget_version_id       = p_ci_rev_version_id;
12450 
12451                IF l_debug_mode = 'Y' THEN
12452                     pa_debug.g_err_stage:='rev_impl_flag set to Y ';
12453                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12454                END IF;
12455            END IF;
12456       END IF;
12457 
12458       IF l_debug_mode = 'Y' THEN
12459             pa_debug.g_err_stage:= 'Exiting impl_ci_into_autobaseline_proj';
12460             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12461       pa_debug.reset_curr_function;
12462     END IF;
12463 EXCEPTION
12464       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
12465             l_msg_count := FND_MSG_PUB.count_msg;
12466             IF l_msg_count = 1 THEN
12467                  PA_INTERFACE_UTILS_PUB.get_messages
12468                    (p_encoded        => FND_API.G_TRUE
12469                    ,p_msg_index      => 1
12470                    ,p_msg_count      => l_msg_count
12471                    ,p_msg_data       => l_msg_data
12472                    ,p_data           => l_data
12473                    ,p_msg_index_out  => l_msg_index_out);
12474                  x_msg_data := l_data;
12475                  x_msg_count := l_msg_count;
12476             ELSE
12477                  x_msg_count := l_msg_count;
12478             END IF;
12479 
12480             x_return_status := FND_API.G_RET_STS_ERROR;
12481      IF p_pa_debug_mode = 'Y' THEN
12482             pa_debug.reset_curr_function;
12483        END IF;
12484       WHEN OTHERS THEN
12485             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12486             x_msg_count     := 1;
12487             x_msg_data      := SQLERRM;
12488             FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FP_CI_MERGE'
12489                                     ,p_procedure_name  => 'impl_ci_into_autobaseline_proj');
12490 
12491             IF l_debug_mode = 'Y' THEN
12492                  pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
12493                  pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
12494             pa_debug.reset_curr_function;
12495     END IF;
12496             RAISE;
12497 END impl_ci_into_autobaseline_proj;
12498 
12499 ----------------------------------------------------------------------------------------------------------
12500 --1.p_context can be PARTIAL_REV( When called from implement partial revenue page )
12501 ----IMPL_FIN_IMPACT(when called from implement financial impact page)
12502 ----INCLUDE(when called from the include change documents page)
12503 ----CI_MERGE(When called for mergind a CI into a CO/CR)
12504 --2.p_ci_id_tbl, p_ci_cost_version_id_tbl, p_ci_rev_version_id_tbl and p_ci_all_version_id_tbl if passed should
12505 ----contain same number of records. p_ci_id_tbl is mandatory. The version ids for the ci_id will be derived if not
12506 ----passed. Either all the version ids for the CI should be passed or none of them should be passed
12507 --3.p_fin_plan_type_id_tbl, p_fin_plan_type_name_tbl, p_impl_cost_flag_tbl, p_impl_rev_flag_tbl,
12508 ----p_submit_version_flag_tbl should contain same number of records.
12509 ----p_fin_plan_type_id_tbl,p_fin_plan_type_name_tbl contains the fin plan type id and name for the
12510 ----corresponding element in p_budget_version_id_tbl
12511 ----p_impl_cost_flag_tbl, p_impl_rev_flag_tbl can have values of Y or N. They indicate whether the cost/revenue
12512 ----impact can be implemented into the corresponding element in p_budget_version_id_tbl
12513 ----p_submit_version_flag_tbl can contain Y or N, if passed. It indicates whether the target budget version id
12514 ----should be baselined after implementation or not
12515 --4.p_agreement_id, p_update_agreement_amt_flag, p_funding_category are related to the agreement chosen
12516 --5.p_add_msg_to_stack lets the API know whether the error messages should be added to the fnd_msg_pub or not. If
12517 ----Y the messages will be added. They will not be added otherwise
12518 --6.x_translated_msgs_tbl contains the translated error messages. x_translated_err_msg_count indicates the no. of
12519 ----elements in x_translated_err_msg_count. x_translated_err_msg_level indicates whether the level of the message is
12520 ----EXCEPTION, WARNING OR INFORMATION. They will be populated only if p_add_msg_to_stack is N
12521 --7.p_commit_flag can be Y or N. This is defaulted to N. If passed as Y then the commit will be executed after
12522 ----every implementation/inclusion i.e. after one ci has got implemented into the target budget version.
12523 
12524 --The processing goes like this
12525 ----Each ci_id will be implemented in every version id in p_budget_version_id_tbl. If p_impl_cost_flag_tbl is Y cost
12526 ----will be implemented. If p_impl_rev_flag_tbl is Y revenue will be implemented.
12527 
12528 -- Bug 3934574 Oct 14 2004  Added a new parameter p_calling_context that would be populated when
12529 -- called as part of budget/forecast generation
12530 
12531 PROCEDURE implement_change_document
12532 ( p_context                         IN     VARCHAR2
12533  ,p_calling_context                 IN     VARCHAR2                           --DEFAULT NULL --bug 3934574
12534  ,p_commit_flag                     IN     VARCHAR2
12535  ,p_ci_id_tbl                       IN     SYSTEM.pa_num_tbl_type
12536  ,p_ci_cost_version_id_tbl          IN     SYSTEM.pa_num_tbl_type
12537  ,p_ci_rev_version_id_tbl           IN     SYSTEM.pa_num_tbl_type
12538  ,p_ci_all_version_id_tbl           IN     SYSTEM.pa_num_tbl_type
12539  ,p_fin_plan_type_id_tbl            IN     SYSTEM.pa_num_tbl_type
12540  ,p_fin_plan_type_name_tbl          IN     SYSTEM.pa_varchar2_150_tbl_type
12541  ,p_budget_version_id_tbl           IN     SYSTEM.pa_num_tbl_type
12542  ,p_impl_cost_flag_tbl              IN     SYSTEM.pa_varchar2_1_tbl_type
12543  ,p_impl_rev_flag_tbl               IN     SYSTEM.pa_varchar2_1_tbl_type
12544  ,p_submit_version_flag_tbl         IN     SYSTEM.pa_varchar2_1_tbl_type
12545  ,p_partial_impl_rev_amt            IN     NUMBER
12546  ,p_agreement_id                    IN     pa_agreements_all.agreement_id%TYPE
12547  ,p_update_agreement_amt_flag       IN     VARCHAR2
12548  ,p_funding_category                IN     VARCHAR2
12549  ,p_raTxn_rollup_api_call_flag      IN     VARCHAR2   --IPM Arch Enhancement Bug 4865563
12550  ,p_add_msg_to_stack                IN     VARCHAR2
12551  ,x_translated_msgs_tbl             OUT    NOCOPY SYSTEM.pa_varchar2_2000_tbl_type --File.Sql.39 bug 4440895
12552  ,x_translated_err_msg_count        OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
12553  ,x_translated_err_msg_level_tbl    OUT    NOCOPY SYSTEM.pa_varchar2_30_tbl_type --File.Sql.39 bug 4440895
12554  ,x_return_status                   OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
12555  ,x_msg_count                       OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
12556  ,x_msg_data                        OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
12557 IS
12558     --Start of variables used for debugging
12559     l_msg_count                     NUMBER :=0;
12560     l_data                          VARCHAR2(2000);
12561     l_msg_data                      VARCHAR2(2000);
12562     l_error_msg_code                VARCHAR2(30);
12563     l_msg_index_out                 NUMBER;
12564     l_return_status                 VARCHAR2(2000);
12565     l_debug_mode                    VARCHAR2(30);
12566     l_module_name                   VARCHAR2(100):='PAFPCIMB.implement_change_document';
12567     --End of variables used for debugging
12568 
12569     --Variables used for getting the translated error messages
12570     l_msg_counter                   NUMBER;
12571     l_encoded_msg                   VARCHAR2(4000);
12572     l_decoded_msg                   VARCHAR2(4000);
12573     l_translated_msg                VARCHAR2(4000);
12574 
12575     i                               NUMBER;
12576     l_ci_id_tbl                     SYSTEM.pa_num_tbl_type          :=SYSTEM.pa_num_tbl_type();
12577     l_ci_cost_version_id_tbl        SYSTEM.pa_num_tbl_type          :=SYSTEM.pa_num_tbl_type();
12578     l_ci_rev_version_id_tbl         SYSTEM.pa_num_tbl_type          :=SYSTEM.pa_num_tbl_type();
12579     l_ci_all_version_id_tbl         SYSTEM.pa_num_tbl_type          :=SYSTEM.pa_num_tbl_type();
12580     l_budget_version_id_tbl         SYSTEM.pa_num_tbl_type          :=SYSTEM.pa_num_tbl_type();
12581     l_impl_cost_flag_tbl            SYSTEM.pa_varchar2_1_tbl_type   :=SYSTEM.pa_varchar2_1_tbl_type();
12582     l_impl_rev_flag_tbl             SYSTEM.pa_varchar2_1_tbl_type   :=SYSTEM.pa_varchar2_1_tbl_type();
12583     l_submit_version_flag_tbl       SYSTEM.pa_varchar2_1_tbl_type   :=SYSTEM.pa_varchar2_1_tbl_type();
12584     l_fin_plan_type_id_tbl          SYSTEM.pa_num_tbl_type          :=SYSTEM.pa_num_tbl_type();
12585     l_fin_plan_type_name_tbl        SYSTEM.pa_varchar2_150_tbl_type :=SYSTEM.pa_varchar2_150_tbl_type();
12586     l_succ_impl_plan_types          SYSTEM.pa_varchar2_150_tbl_type :=SYSTEM.pa_varchar2_150_tbl_type();
12587     l_succ_impl_cos                 SYSTEM.pa_varchar2_150_tbl_type :=SYSTEM.pa_varchar2_150_tbl_type();
12588     l_ci_number                     SYSTEM.pa_varchar2_100_tbl_type :=SYSTEM.pa_varchar2_100_tbl_type();
12589     l_plan_type_collection          VARCHAR2(2000);
12590     l_ci_number_collection          VARCHAR2(2000);
12591     l_partial_impl_succeeded        VARCHAR2(1);
12592     l_ci_bv_merge_possible_map_tbl  PA_PLSQL_DATATYPES.Char30TabTyp;
12593     l_index                         NUMBER;
12594     l_cost_ci_version_id            pa_budget_Versions.budget_version_id%TYPE;
12595     l_rev_ci_version_id             pa_budget_Versions.budget_version_id%TYPE;
12596     l_all_ci_version_id             pa_budget_Versions.budget_version_id%TYPE;
12597     l_budget_ci_map_rec_tbl         budget_ci_map_rec_tbl_type;
12598     l_impl_impact_type_code         VARCHAR2(10);
12599     l_agreement_id                  pa_agreements_all.agreement_id%TYPE;
12600     l_funding_category              VARCHAR2(30);
12601     l_partial_impl_rev_amt          pa_budget_lines.txn_revenue%TYPE;
12602 
12603     --These two variables will be used for comparing the no. of error messages in the error message stack when the
12604     --API called and when the API is done with the processing. If the no of messages in the two pl/sql tbls are
12605     --different then the error status will be returned as E from the API
12606     l_init_msg_count                NUMBER;
12607     l_msg_count_at_end              NUMBER;
12608 
12609     -- Bug 3877815: Additional local variables declared
12610     l_baseline_api_called           VARCHAR2(1);
12611     l_targ_app_cost_flag            VARCHAR2(1);
12612     l_targ_app_rev_flag             VARCHAR2(1);
12613     l_baseline_funding_flag         VARCHAR2(1);
12614 
12615     l_context_mode                  VARCHAR2(1) := null;
12616 
12617 
12618 BEGIN
12619     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
12620     l_debug_mode := NVL(l_debug_mode, 'Y');
12621     x_msg_count := 0;
12622     x_return_status := FND_API.G_RET_STS_SUCCESS;
12623     l_init_msg_count:= FND_MSG_PUB.count_msg;
12624    IF p_pa_debug_mode = 'Y' THEN
12625     PA_DEBUG.Set_Curr_Function( p_function   => l_module_name,
12626                                 p_debug_mode => l_debug_mode );
12627    END IF;
12628     -- For bug 3866629
12629     FND_MSG_PUB.initialize;
12630 
12631       -- Bug#13960733 start.
12632  	              l_context_mode := mo_global.get_access_mode;
12633  	              if l_context_mode = 'M' then
12634  	                  PA_MOAC_UTILS.SET_POLICY_CONTEXT('S',fnd_global.org_id);
12635  	              end if;
12636  	                -- Bug#13960733 end.
12637 
12638     --dbms_output.put_line('M1');
12639 
12640     --If p_commit_flag is N then all the changes done thru this API should be rolled back whenever an error occurs.
12641     --Otherwise only those changes that happened in the merge which failed will be rolled back and all other
12642     --successful merges will be committed.
12643     IF p_commit_flag ='N' THEN
12644         SAVEPOINT implement_change_document;
12645     END IF;
12646 
12647     IF l_debug_mode = 'Y' THEN
12648 
12649         pa_debug.g_err_stage:= 'Validating the input parameters';
12650         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12651 
12652     END IF;
12653 
12654     IF p_ci_id_tbl.COUNT =0 OR
12655        p_budget_version_id_tbl.COUNT = 0 THEN
12656 
12657         IF l_debug_mode = 'Y' THEN
12658 
12659             pa_debug.g_err_stage:= 'p_ci_id_tbl.COUNT is '||p_ci_id_tbl.COUNT;
12660             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12661 
12662             pa_debug.g_err_stage:= 'p_budget_version_id_tbl.COUNT is '||p_budget_version_id_tbl.COUNT;
12663             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12664 
12665             pa_debug.g_err_stage:= 'Returning-->';
12666             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12667         pa_debug.reset_curr_function;
12668 END IF;
12669         RETURN;
12670 
12671     END IF;
12672 
12673     IF l_debug_mode = 'Y' THEN
12674 
12675         pa_debug.g_err_stage:= 'Calling validate_ci_merge_input_data';
12676         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12677 
12678     END IF;
12679 
12680     --dbms_output.put_line('M2');
12681      l_partial_impl_rev_amt := p_partial_impl_rev_amt;
12682      validate_ci_merge_input_data(
12683      p_context                       => p_context
12684     ,p_ci_id_tbl                     => p_ci_id_tbl
12685     ,p_ci_cost_version_id_tbl        => p_ci_cost_version_id_tbl
12686     ,p_ci_rev_version_id_tbl         => p_ci_rev_version_id_tbl
12687     ,p_ci_all_version_id_tbl         => p_ci_all_version_id_tbl
12688     ,p_budget_version_id_tbl         => p_budget_version_id_tbl
12689     ,p_fin_plan_type_id_tbl          => p_fin_plan_type_id_tbl
12690     ,p_fin_plan_type_name_tbl        => p_fin_plan_type_name_tbl
12691     ,p_impl_cost_flag_tbl            => p_impl_cost_flag_tbl
12692     ,p_impl_rev_flag_tbl             => p_impl_rev_flag_tbl
12693     ,p_submit_version_flag_tbl       => p_submit_version_flag_tbl
12694     ,px_partial_impl_rev_amt         => l_partial_impl_rev_amt
12695     ,p_agreement_id                  => p_agreement_id
12696     ,p_update_agreement_amt_flag     => p_update_agreement_amt_flag
12697     ,p_funding_category              => p_funding_category
12698     ,x_ci_id_tbl                     => l_ci_id_tbl
12699     ,x_ci_cost_version_id_tbl        => l_ci_cost_version_id_tbl
12700     ,x_ci_rev_version_id_tbl         => l_ci_rev_version_id_tbl
12701     ,x_ci_all_version_id_tbl         => l_ci_all_version_id_tbl
12702     ,x_ci_number                     => l_ci_number
12703     ,x_budget_version_id_tbl         => l_budget_version_id_tbl
12704     ,x_fin_plan_type_id_tbl          => l_fin_plan_type_id_tbl
12705     ,x_fin_plan_type_name_tbl        => l_fin_plan_type_name_tbl
12706     ,x_submit_version_flag_tbl       => l_submit_version_flag_tbl
12707     ,x_budget_ci_map_rec_tbl         => l_budget_ci_map_rec_tbl
12708     ,x_agreement_id                  => l_agreement_id
12709     ,x_funding_category              => l_funding_category
12710     ,x_return_status                 => x_return_status
12711     ,x_msg_count                     => x_msg_count
12712     ,x_msg_data                      => x_msg_data );
12713 
12714     IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12715         IF l_debug_mode = 'Y' THEN
12716             pa_debug.g_err_stage:='Called API validate_ci_merge_input_data returned error';
12717             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
12718         END IF;
12719         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12720 
12721     END IF;
12722 
12723     --dbms_output.put_line('M3');
12724     IF l_debug_mode = 'Y' THEN
12725         pa_debug.g_err_stage:='l_agreement_id derived is '||l_agreement_id||' l_funding_category is '||l_funding_category;
12726         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12727 
12728         pa_debug.g_err_stage:='Looping for Calling the merge into single version API';
12729         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12730     END IF;
12731 
12732 
12733     x_translated_msgs_tbl := SYSTEM.pa_varchar2_2000_tbl_type();
12734     x_translated_err_msg_level_tbl := SYSTEM.pa_varchar2_30_tbl_type();
12735 
12736     IF p_add_msg_to_stack='N' THEN
12737 
12738         --Add the messages from the error stack to the output translated error messages table. These error messages
12739         --will be displayed on the OA pages
12740         l_msg_count := fnd_msg_pub.count_msg;
12741 
12742         IF l_debug_mode = 'Y' THEN
12743             pa_debug.g_err_stage:='Inside Loop for l_msg_count : '||l_msg_count;
12744             pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12745         END IF;
12746 
12747         FOR l_msg_counter IN REVERSE 1..l_msg_count LOOP
12748 
12749             IF l_debug_mode = 'Y' THEN
12750                 pa_debug.g_err_stage:='Inside l_msg_counter IN REVERSE l_msg_counter : '||l_msg_counter;
12751                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12752             END IF;
12753             PA_UTILS.Get_Encoded_Msg(p_index    => l_msg_counter,
12754                                      p_msg_out  => l_encoded_msg);
12755             fnd_message.set_encoded(l_encoded_msg);
12756             l_decoded_msg := fnd_message.get;
12757             l_translated_msg :=  nvl(l_decoded_msg, l_encoded_msg);
12758 
12759             x_translated_msgs_tbl.EXTEND(1);
12760             x_translated_err_msg_level_tbl.EXTEND(1);
12761             x_translated_msgs_tbl(x_translated_msgs_tbl.COUNT):=l_translated_msg;
12762             x_translated_err_msg_level_tbl(x_translated_err_msg_level_tbl.COUNT):='ERROR';
12763 
12764         END LOOP;
12765 
12766         --Initialise the msg pub so that these errors will not get added again to the translated err msg table
12767         FND_MSG_PUB.initialize;
12768 
12769     END IF;
12770 
12771     IF l_debug_mode = 'Y' THEN
12772         pa_debug.g_err_stage:='Looping for Calling the merge into single version API';
12773         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12774     END IF;
12775     --Initialise the pl/sql tbls.
12776     l_succ_impl_plan_types := SYSTEM.pa_varchar2_150_tbl_type();
12777     l_succ_impl_cos        := SYSTEM.pa_varchar2_150_tbl_type();
12778 
12779 
12780     --dbms_output.put_line('M4');
12781     --Call the Merge API
12782     IF l_ci_id_tbl.COUNT>0 AND l_budget_version_id_tbl.COUNT>0 THEN
12783 
12784         FOR i IN l_ci_id_tbl.FIRST..l_ci_id_tbl.LAST LOOP
12785 
12786             FOR j IN l_budget_version_id_tbl.FIRST..l_budget_version_id_tbl.LAST LOOP
12787 
12788                 l_index:= 1;
12789 
12790                 --Loop thru the l_budget_ci_map_rec_tbl to find out the record containing the ci id and  the
12791                 --budget version id combination for which the implementation is being considered
12792                 LOOP
12793                     EXIT WHEN (l_budget_ci_map_rec_tbl(l_index).budget_version_id=l_budget_version_id_tbl(j) AND
12794                                l_budget_ci_map_rec_tbl(l_index).ci_id=l_ci_id_tbl(i));
12795                     l_index:=l_index+1;
12796                 END LOOP;
12797                 l_impl_impact_type_code:=l_budget_ci_map_rec_tbl(l_index).impact_type_code;
12798 
12799                 IF l_debug_mode = 'Y' THEN
12800                     pa_debug.g_err_stage:='l_impl_impact_type_code IS '||l_impl_impact_type_code;
12801                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12802                 END IF;
12803 
12804 
12805                 IF l_impl_impact_type_code <> 'NONE' THEN
12806 
12807                     l_cost_ci_version_id:=NULL;
12808                     l_rev_ci_version_id:=NULL;
12809                     l_all_ci_version_id:=NULL;
12810 
12811                     -- Bug 5845142.
12812                     IF l_impl_impact_type_code IN ('COST', 'BOTH') AND
12813                        NVL(l_ci_cost_version_id_tbl(i),-1)<>-1 THEN
12814 
12815                         l_cost_ci_version_id:=l_ci_cost_version_id_tbl(i);
12816 
12817                     END IF;
12818 
12819                     -- Bug 5845142.
12820                     IF l_impl_impact_type_code IN ('REVENUE', 'BOTH') AND
12821                        NVL(l_ci_rev_version_id_tbl(i),-1)<>-1 THEN
12822 
12823                         l_rev_ci_version_id:=l_ci_rev_version_id_tbl(i);
12824 
12825                     END IF;
12826 
12827                     IF l_impl_impact_type_code = 'ALL' THEN
12828 
12829                         l_all_ci_version_id:=l_ci_all_version_id_tbl(i);
12830 
12831                     END IF;
12832 
12833                     /* Bug 3731975: impl_cost_flag and impl_rev_flag returned by get_impl_details is relevant to a plan type
12834                        context. But this API is called in the context of a single version. For example, it could be that for the
12835                        plan type impl_cost_flag and impl_rev_flag are 'Y' (COST_AND_REV_SEP) ,
12836                        but for the version (either COST or REVENUE) one of them doesnt make sense. */
12837 
12838                     IF l_impl_impact_type_code = 'COST' THEN
12839 
12840                         l_budget_ci_map_rec_tbl(l_index).impl_rev_flag:='N';
12841 
12842                     END IF;
12843 
12844                     IF l_impl_impact_type_code = 'REVENUE' THEN
12845 
12846                         l_budget_ci_map_rec_tbl(l_index).impl_cost_flag:= 'N';
12847 
12848                     END IF;
12849 
12850             -- End of code for Bug 3731975
12851 
12852                     /* Bug 3877815: Checking if the project is autobaselined enabled, if yes
12853                      * a separate api would be called to take care of merge for autobaselined projects
12854                      */
12855                     IF l_debug_mode = 'Y' THEN
12856                           pa_debug.g_err_stage:='Bug Fixing started-------';
12857                           pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12858                     END IF;
12859                     BEGIN
12860                          SELECT Nvl(bv.approved_cost_plan_type_flag, 'N'),
12861                                 Nvl(bv.approved_rev_plan_type_flag, 'N'),
12862                                 Nvl(pj.baseline_funding_flag, 'N')
12863                          INTO   l_targ_app_cost_flag,
12864                                 l_targ_app_rev_flag,
12865                                 l_baseline_funding_flag
12866                          FROM   pa_projects_all pj,
12867                                 pa_budget_versions bv
12868                          WHERE  bv.budget_version_id = l_budget_version_id_tbl(j)
12869                          AND    bv.project_id = pj.project_id;
12870                     EXCEPTION
12871                          WHEN OTHERS THEN
12872                               IF P_PA_debug_mode = 'Y' THEN
12873                                    pa_debug.g_err_stage:='Error while getting baseline_funding_flag';
12874                                    pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
12875                               END IF;
12876                               RAISE;
12877                     END;
12878 
12879                     l_baseline_api_called := 'N';
12880 
12881                     --If the version is approved revenue version and if the project is enabled for auto
12882                     --baseline then the version should be baselined
12883 
12884                     IF l_baseline_funding_flag ='Y' AND
12885                        l_targ_app_rev_flag = 'Y'    AND
12886                        l_budget_ci_map_rec_tbl(l_index).impl_rev_flag = 'Y' AND -- Bug 3877815: Review comment
12887                        p_context IN ('IMPL_FIN_IMPACT','PARTIAL_REV') THEN
12888 
12889                             l_baseline_api_called := 'Y';
12890                             IF l_debug_mode = 'Y' THEN
12891                                  pa_debug.g_err_stage:='Calling impl_ci_into_autobaseline_proj for autobaselined projects';
12892                                  pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12893                             END IF;
12894                             impl_ci_into_autobaseline_proj( p_ci_id                     => l_ci_id_tbl(i)
12895                                                            ,p_ci_rev_version_id         => l_rev_ci_version_id
12896                                                            ,p_budget_version_id         => l_budget_version_id_tbl(j)
12897                                                            ,p_fin_plan_type_id          => l_fin_plan_type_id_tbl(j)
12898                                                            ,p_partial_impl_rev_amt      => l_partial_impl_rev_amt
12899                                                            ,p_agreement_id              => l_agreement_id
12900                                                            ,p_update_agreement_amt_flag => p_update_agreement_amt_flag
12901                                                            ,p_funding_category          => l_funding_category
12902                                                            ,x_return_status             => x_return_status
12903                                                            ,x_msg_data                  => x_msg_data
12904                                                            ,x_msg_count                 => x_msg_count);
12905 
12906                             IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12907                                  IF l_debug_mode = 'Y' THEN
12908                                        pa_debug.g_err_stage:='--Call to impl_ci_into_autobaseline_proj returned with ERROR';
12909                                        pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
12910                                  END IF;
12911                                  IF p_context ='PARTIAL_REV' THEN
12912                                        l_partial_impl_succeeded:='N';
12913                                   END IF;
12914                                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12915                             ELSE
12916                                   --Record that this implementation is successful
12917                                   IF p_context='IMPL_FIN_IMPACT' THEN
12918                                         l_succ_impl_plan_types.EXTEND(1);
12919                                         l_succ_impl_plan_types(l_succ_impl_plan_types.COUNT):=l_fin_plan_type_name_tbl(j);
12920                                   ELSIF p_context='PARTIAL_REV' THEN
12921                                         l_partial_impl_succeeded:='Y';
12922                                   END IF;
12923                             END IF;
12924                     END IF;
12925 
12926                     IF l_baseline_api_called ='N' OR
12927                        l_budget_ci_map_rec_tbl(l_index).impl_cost_flag = 'Y' THEN -- Bug 3877815: Review comment
12928                          --dbms_output.put_line('M5');
12929                          IF l_debug_mode = 'Y' THEN
12930                                pa_debug.g_err_stage:='Calling implement_ci_into_single_ver';
12931                                pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12932                          END IF;
12933                          implement_ci_into_single_ver
12934                               (p_context                      => p_context
12935                               ,p_calling_context              => p_calling_context -- bug 3934574
12936                               ,p_ci_id                        => l_ci_id_tbl(i)
12937                               ,p_ci_cost_version_id           => l_cost_ci_version_id
12938                               ,p_ci_rev_version_id            => l_rev_ci_version_id
12939                               ,p_ci_all_version_id            => l_all_ci_version_id
12940                               ,p_budget_version_id            => l_budget_version_id_tbl(j)
12941                               ,p_fin_plan_type_id             => l_fin_plan_type_id_tbl(j)
12942                               ,p_fin_plan_type_name           => l_fin_plan_type_name_tbl(j)
12943                               ,p_partial_impl_rev_amt         => l_partial_impl_rev_amt
12944                               ,p_cost_impl_flag               => l_budget_ci_map_rec_tbl(l_index).impl_cost_flag
12945                               ,p_rev_impl_flag                => l_budget_ci_map_rec_tbl(l_index).impl_rev_flag
12946                               ,p_submit_version_flag          => l_submit_version_flag_tbl(j)
12947                               ,p_agreement_id                 => l_agreement_id
12948                               ,p_update_agreement_amt_flag    => p_update_agreement_amt_flag
12949                               ,p_funding_category             => l_funding_category
12950                               ,p_raTxn_rollup_api_call_flag   => p_raTxn_rollup_api_call_flag  --IPM Arch Enhancement Bug 4865563
12951                               ,x_return_status                => x_return_status
12952                               ,x_msg_data                     => x_msg_data
12953                               ,x_msg_count                    => x_msg_count);
12954 
12955                          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
12956                               IF l_debug_mode = 'Y' THEN
12957                                     pa_debug.g_err_stage:='Called API PAFPCIMB.implement_ci_into_single_ver error';
12958                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
12959                               END IF;
12960 
12961                               IF p_context ='PARTIAL_REV' THEN
12962                                    l_partial_impl_succeeded:='N';
12963                               END IF;
12964 
12965                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12966 
12967                          ELSE
12968                               --Record that this implementation is successful
12969                               IF p_context='IMPL_FIN_IMPACT' THEN
12970                                    l_succ_impl_plan_types.EXTEND(1);
12971                                    l_succ_impl_plan_types(l_succ_impl_plan_types.COUNT):=l_fin_plan_type_name_tbl(j);
12972                               ELSIF p_context='INCLUDE' THEN
12973                                    l_succ_impl_cos.EXTEND(1);
12974                                    l_succ_impl_cos(l_succ_impl_cos.COUNT):=l_ci_number(i);
12975                               ELSIF p_context='PARTIAL_REV' THEN
12976                                    l_partial_impl_succeeded:='Y';
12977                               END IF;
12978                          END IF;
12979                     END IF; -- Merge Done
12980 
12981                     IF p_commit_flag ='Y' THEN
12982                          IF l_debug_mode = 'Y' THEN
12983                                pa_debug.g_err_stage:='About to commit data';
12984                                pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
12985                          END IF;
12986 
12987                          COMMIT;
12988                     END IF;
12989 
12990                     --dbms_output.put_line('M6');
12991                     IF l_debug_mode = 'Y' THEN
12992                         pa_debug.g_err_stage:='l_impl_impact_type_code IS '||l_impl_impact_type_code;
12993                         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
12994                     END IF;
12995 
12996 
12997                     IF p_add_msg_to_stack='N' THEN
12998 
12999                         --Add the messages from the error stack to the output translated error messages table. These error messages
13000                         --will be displayed on the OA pages
13001                         l_msg_count := fnd_msg_pub.count_msg;
13002                         FOR l_msg_counter IN REVERSE 1..l_msg_count LOOP
13003 
13004                             PA_UTILS.Get_Encoded_Msg(p_index    => l_msg_counter,
13005                                                      p_msg_out  => l_encoded_msg);
13006                             fnd_message.set_encoded(l_encoded_msg);
13007                             l_decoded_msg := fnd_message.get;
13008                             l_translated_msg :=  nvl(l_decoded_msg, l_encoded_msg);
13009 
13010                             x_translated_msgs_tbl.EXTEND(1);
13011                             x_translated_err_msg_level_tbl.EXTEND(1);
13012                             x_translated_msgs_tbl(x_translated_msgs_tbl.COUNT):=l_translated_msg;
13013                             x_translated_err_msg_level_tbl(x_translated_err_msg_level_tbl.COUNT):='ERROR';
13014 
13015                         END LOOP;
13016 
13017                         --Initialise the msg pub so that these errors will not get added again to the translated err msg table
13018                         FND_MSG_PUB.initialize;
13019 
13020                     END IF;
13021 
13022                 END IF;--IF l_ci_bv_merge_possible_map_tbl(l_index) <> 'NONE' THEN
13023 
13024             END LOOP;--Budget version loop
13025 
13026         END LOOP;--Ci Loop
13027 
13028     END IF;--IF l_ci_id_tbl.COUNT>0 AND l_budget_version_id_tbl.COUNT>0 THEN
13029 
13030     IF l_debug_mode = 'Y' THEN
13031         pa_debug.g_err_stage:='About to add success msg '||l_succ_impl_plan_types.COUNT;
13032         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13033     END IF;
13034 
13035     IF p_context='IMPL_FIN_IMPACT' THEN
13036 
13037         IF l_succ_impl_plan_types.COUNT>0 THEN
13038 
13039             FOR i IN l_succ_impl_plan_types.FIRST..l_succ_impl_plan_types.LAST LOOP
13040 
13041                 IF l_debug_mode = 'Y' THEN
13042                     pa_debug.g_err_stage:='l_plan_type_collection '||l_plan_type_collection;
13043                     pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13044                 END IF;
13045 
13046 
13047                 IF l_plan_type_collection IS NOT NULL THEN
13048 
13049                     l_plan_type_collection:=l_plan_type_collection||', ';
13050 
13051                 END IF;
13052 
13053                 l_plan_type_collection := l_plan_type_collection || l_succ_impl_plan_types(i);
13054             END LOOP;
13055 
13056             IF l_debug_mode = 'Y' THEN
13057                 pa_debug.g_err_stage:='l_plan_type_collection a '||l_plan_type_collection;
13058                 pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13059             END IF;
13060 
13061             IF l_plan_type_collection IS NOT NULL THEN
13062                 l_plan_type_collection:= '[' || l_plan_type_collection || ']';
13063 
13064 				--iff only new supplier region is enabled
13065                  PA_UTILS.ADD_MESSAGE
13066                  (p_app_short_name => 'PA',
13067                   p_msg_name       => 'PA_COST_OR_REV_IMPACT_MISSING');
13068 
13069                 PA_UTILS.ADD_MESSAGE
13070                 (p_app_short_name => 'PA',
13071                  --p_msg_name       => 'PA_FP_IMPL_SUCC_PLAN_TYPE',--For bug 3829002
13072                  p_msg_name       => 'PA_FP_IMPL_OPT_SUCC_IMPL',
13073                  p_token1         => 'SUCC_PTS',
13074                  p_value1         => l_plan_type_collection );
13075            END IF;
13076 
13077         END IF;--IF l_succ_impl_plan_types.COUNT>0 THEN
13078 
13079 
13080     ELSIF p_context='INCLUDE' THEN
13081 
13082         IF l_succ_impl_cos.COUNT>0 THEN
13083             FOR i IN l_succ_impl_cos.FIRST..l_succ_impl_cos.LAST LOOP
13084 
13085                 IF l_ci_number_collection IS NOT NULL THEN
13086 
13087                     l_ci_number_collection:=l_ci_number_collection||', ';
13088 
13089                 END IF;
13090 
13091                 l_ci_number_collection := l_ci_number_collection || l_succ_impl_cos(i);
13092 
13093             END LOOP;
13094 
13095             IF l_ci_number_collection IS NOT NULL THEN
13096                 l_ci_number_collection:= '[' || l_ci_number_collection || ']';
13097 
13098 
13099                 PA_UTILS.ADD_MESSAGE
13100                 (p_app_short_name => 'PA',
13101                  p_msg_name       => 'PA_FP_INCL_SUCC_CHG_DOC',
13102                  p_token1         => 'SUCC_CIS',
13103                  p_value1         => l_ci_number_collection );
13104            END IF;
13105 
13106         END IF;--IF l_succ_impl_cos.COUNT>0 THEN
13107 
13108     ELSIF l_partial_impl_succeeded='Y' THEN
13109 
13110         --dbms_output.put_line('M7');
13111         PA_UTILS.ADD_MESSAGE
13112         (p_app_short_name => 'PA',
13113          p_msg_name       => 'PA_FP_PARTIAL_IMPL_SUCC');
13114     END IF;
13115 
13116     IF l_debug_mode = 'Y' THEN
13117         pa_debug.g_err_stage:='Populating the error msgs at the end';
13118         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13119     END IF;
13120 
13121 
13122     IF p_add_msg_to_stack='N' THEN
13123 
13124         --Add the messages from the error stack to the output translated error messages table. These error messages
13125         --will be displayed on the OA pages
13126         l_msg_count := fnd_msg_pub.count_msg;
13127         FOR l_msg_counter IN REVERSE 1..l_msg_count LOOP
13128 
13129             PA_UTILS.Get_Encoded_Msg(p_index    => l_msg_counter,
13130                                      p_msg_out  => l_encoded_msg);
13131             fnd_message.set_encoded(l_encoded_msg);
13132             l_decoded_msg := fnd_message.get;
13133             l_translated_msg :=  nvl(l_decoded_msg, l_encoded_msg);
13134 
13135             x_translated_msgs_tbl.EXTEND(1);
13136             x_translated_err_msg_level_tbl.EXTEND(1);
13137             x_translated_msgs_tbl(x_translated_msgs_tbl.COUNT):=l_translated_msg;
13138             x_translated_err_msg_level_tbl(x_translated_err_msg_level_tbl.COUNT):='INFORMATION';
13139 
13140         END LOOP;
13141         --Initialise the msg pub so that these errors will not get added again to the translated err msg table
13142         FND_MSG_PUB.initialize;
13143 
13144     END IF;
13145 
13146     --dbms_output.put_line('M8');
13147     IF l_debug_mode = 'Y' THEN
13148         pa_debug.g_err_stage:='Setting the return status';
13149         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13150     END IF;
13151 
13152     --Set the return status depending upon the parameter p_add_msg_to_stack
13153     IF p_add_msg_to_stack='N' THEN
13154 
13155         --Set the return status always to S as the calling API should look into the transalted error messages tbl
13156         --for the errors
13157         --Also Initialize x_translated_err_msg_count and clear the out variables
13158         x_translated_err_msg_count:=x_translated_msgs_tbl.count;
13159         x_return_status:='S';
13160         x_msg_data:=null;
13161         x_msg_count:=0;
13162 
13163 
13164 
13165     ELSE
13166         l_msg_count_at_end := fnd_msg_pub.count_msg;
13167 
13168 /* -- Commenting out this code for Setting the x_return_status as this would be taken
13169    -- care in the Calling Module. (This Else Part is primarily when this API is called
13170    -- from Generation Flow -- Bug 3749556
13171         IF l_init_msg_count <> l_msg_count_at_end THEN
13172 
13173             x_return_status :='E';
13174 
13175         ELSE
13176 
13177             x_return_status:='S';
13178 
13179         END IF;
13180 */
13181     END IF;
13182 
13183 
13184     IF l_debug_mode = 'Y' THEN
13185 
13186         pa_debug.g_err_stage:= 'Exiting implement_change_document count :' || fnd_msg_pub.count_msg;
13187         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13188         pa_debug.reset_curr_function;
13189     END IF;
13190 EXCEPTION
13191     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
13192         l_msg_count := FND_MSG_PUB.count_msg;
13193         IF l_msg_count = 1 THEN
13194             PA_INTERFACE_UTILS_PUB.get_messages
13195                  (p_encoded        => FND_API.G_TRUE
13196                   ,p_msg_index      => 1
13197                   ,p_msg_count      => l_msg_count
13198                   ,p_msg_data       => l_msg_data
13199                   ,p_data           => l_data
13200                   ,p_msg_index_out  => l_msg_index_out);
13201             x_msg_data := l_data;
13202             x_msg_count := l_msg_count;
13203 
13204         ELSE
13205             x_msg_count := l_msg_count;
13206         END IF;
13207 
13208         IF p_commit_flag ='N' THEN
13209             ROLLBACK TO implement_change_document;
13210         END IF;
13211 
13212         x_return_status := FND_API.G_RET_STS_ERROR;
13213  IF p_pa_debug_mode = 'Y' THEN
13214         pa_debug.reset_curr_function;
13215   END IF;
13216     WHEN OTHERS THEN
13217 
13218         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13219         x_msg_count     := 1;
13220         x_msg_data      := SQLERRM;
13221         FND_MSG_PUB.add_exc_msg( p_pkg_name  => 'PA_FP_CI_MERGE'
13222                           ,p_procedure_name  => 'implement_change_document');
13223 
13224         IF l_debug_mode = 'Y' THEN
13225             pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
13226             pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
13227         END IF;
13228 
13229         IF p_commit_flag ='N' THEN
13230             ROLLBACK TO implement_change_document;
13231         END IF;
13232 
13233     IF p_pa_debug_mode = 'Y' THEN
13234         pa_debug.reset_curr_function;
13235     END IF;
13236         RAISE;
13237 
13238 END implement_change_document;
13239 
13240 --Added for Enc to copy only new supplier cost region data
13241 procedure copy_supplier_cost_data(
13242          p_ci_id_to               IN     NUMBER
13243         ,p_ci_id_from             IN      NUMBER
13244         ,x_return_status        OUT    NOCOPY VARCHAR2
13245         ,x_msg_count            OUT    NOCOPY NUMBER
13246 		,x_msg_data             OUT    NOCOPY VARCHAR2
13247 ) IS
13248  x_error_msg_code       varchar2(100) := NULL;
13249  x_supp_rowid    varchar2(50) := null;
13250  x_supp_ci_transaction_id number := null;
13251  supp_cost_flag  VARCHAR2(1);
13252  l_debug_mode    VARCHAR2(30);
13253  l_ci_type_id number := null;
13254  l_ci_id_to number := null;
13255  l_ci_id_from number := null;
13256  G_EXCEPTION_ERROR		EXCEPTION;
13257 G_EXCEPTION_UNEXPECTED_ERROR	EXCEPTION;
13258 
13259 l_count  number;
13260 l_rec_index number;
13261 
13262 TYPE supp_rec is record(
13263 CI_TRANSACTION_ID            PA_CI_SUPPLIER_DETAILS.CI_TRANSACTION_ID%type,
13264 CI_ID                        PA_CI_SUPPLIER_DETAILS.CI_ID%type,
13265 CI_TYPE_ID                   PA_CI_SUPPLIER_DETAILS.CI_TYPE_ID%type,
13266 CI_IMPACT_ID                 PA_CI_SUPPLIER_DETAILS.CI_IMPACT_ID%TYPE,
13267 VENDOR_ID                    PA_CI_SUPPLIER_DETAILS.VENDOR_ID%TYPE,
13268 PO_HEADER_ID                 PA_CI_SUPPLIER_DETAILS.PO_HEADER_ID%TYPE,
13269 PO_LINE_ID                   PA_CI_SUPPLIER_DETAILS.PO_LINE_ID%TYPE,
13270 CHANGE_AMOUNT                PA_CI_SUPPLIER_DETAILS.CHANGE_AMOUNT%TYPE,
13271 CURRENCY_CODE                PA_CI_SUPPLIER_DETAILS.CURRENCY_CODE%TYPE,
13272 ADJUSTED_CI_TRANSACTION_ID   PA_CI_SUPPLIER_DETAILS.ADJUSTED_CI_TRANSACTION_ID%TYPE,
13273 CHANGE_DESCRIPTION           PA_CI_SUPPLIER_DETAILS.CHANGE_DESCRIPTION%TYPE,
13274 CHANGE_TYPE                  PA_CI_SUPPLIER_DETAILS.CHANGE_TYPE%TYPE,
13275 CREATED_BY                   PA_CI_SUPPLIER_DETAILS.CREATED_BY%TYPE,
13276 CREATION_DATE                PA_CI_SUPPLIER_DETAILS.CREATION_DATE%TYPE,
13277 LAST_UPDATED_BY              PA_CI_SUPPLIER_DETAILS.LAST_UPDATED_BY%TYPE,
13278 LAST_UPDATE_DATE             PA_CI_SUPPLIER_DETAILS.LAST_UPDATE_DATE%TYPE,
13279 LAST_UPDATE_LOGIN            PA_CI_SUPPLIER_DETAILS.LAST_UPDATE_LOGIN%TYPE,
13280 CHANGE_APPROVER              PA_CI_SUPPLIER_DETAILS.CHANGE_APPROVER%TYPE,
13281 AUDIT_HISTORY_NUMBER         PA_CI_SUPPLIER_DETAILS.AUDIT_HISTORY_NUMBER%TYPE,
13282 CURRENT_AUDIT_FLAG           PA_CI_SUPPLIER_DETAILS.CURRENT_AUDIT_FLAG%TYPE,
13283 ORIGINAL_SUPP_TRANS_ID       PA_CI_SUPPLIER_DETAILS.ORIGINAL_SUPP_TRANS_ID%TYPE,
13284 SOURCE_SUPP_TRANS_ID         PA_CI_SUPPLIER_DETAILS.SOURCE_SUPP_TRANS_ID%TYPE,
13285 FROM_CHANGE_DATE             PA_CI_SUPPLIER_DETAILS.FROM_CHANGE_DATE%TYPE,
13286 TO_CHANGE_DATE               PA_CI_SUPPLIER_DETAILS.TO_CHANGE_DATE%TYPE,
13287 RAW_COST                     PA_CI_SUPPLIER_DETAILS.RAW_COST%TYPE,
13288 BURDENED_COST                PA_CI_SUPPLIER_DETAILS.BURDENED_COST%TYPE,
13289 REVENUE_RATE                 PA_CI_SUPPLIER_DETAILS.REVENUE_RATE%TYPE,
13290 REVENUE_OVERRIDE_RATE        PA_CI_SUPPLIER_DETAILS.REVENUE_OVERRIDE_RATE%TYPE,
13291 REVENUE                      PA_CI_SUPPLIER_DETAILS.REVENUE%TYPE,
13292 TOTAL_REVENUE                PA_CI_SUPPLIER_DETAILS.TOTAL_REVENUE%TYPE,
13293 SUP_QUOTE_REF_NO             PA_CI_SUPPLIER_DETAILS.SUP_QUOTE_REF_NO%TYPE,
13294 TASK_ID                      PA_CI_SUPPLIER_DETAILS.TASK_ID%TYPE,
13295 RESOURCE_LIST_MEMBER_ID      PA_CI_SUPPLIER_DETAILS.RESOURCE_LIST_MEMBER_ID%TYPE,
13296 EXPENDITURE_TYPE_ID          PA_CI_SUPPLIER_DETAILS.EXPENDITURE_TYPE_ID%TYPE,
13297 ESTIMATED_COST               PA_CI_SUPPLIER_DETAILS.ESTIMATED_COST%TYPE,
13298 QUOTED_COST                  PA_CI_SUPPLIER_DETAILS.QUOTED_COST%TYPE,
13299 NEGOTIATED_COST              PA_CI_SUPPLIER_DETAILS.NEGOTIATED_COST%TYPE,
13300 FINAL_COST                   PA_CI_SUPPLIER_DETAILS.FINAL_COST%TYPE,
13301 MARKUP_COST                  PA_CI_SUPPLIER_DETAILS.MARKUP_COST%TYPE,
13302 STATUS                       PA_CI_SUPPLIER_DETAILS.STATUS%TYPE,
13303 EXPENDITURE_ORG_ID           PA_CI_SUPPLIER_DETAILS.EXPENDITURE_ORG_ID%TYPE,
13304 CHANGE_REASON_CODE           PA_CI_SUPPLIER_DETAILS.CHANGE_REASON_CODE%TYPE,
13305 QUOTE_NEGOTIATION_REFERENCE  PA_CI_SUPPLIER_DETAILS.QUOTE_NEGOTIATION_REFERENCE%TYPE,
13306 NEED_BY_DATE                 PA_CI_SUPPLIER_DETAILS.NEED_BY_DATE%TYPE,
13307 EXPENDITURE_TYPE             PA_CI_SUPPLIER_DETAILS.EXPENDITURE_TYPE%TYPE,
13308 RESOURCE_ASSIGNMENT_ID       PA_CI_SUPPLIER_DETAILS.RESOURCE_ASSIGNMENT_ID%TYPE);
13309 
13310 TYPE supp_rec_tbl is table of supp_rec index by binary_integer;
13311 
13312 supp_from_tbl supp_rec_tbl;
13313 supp_to_tbl   supp_rec_tbl;
13314 l_found       varchar2(1);
13315 l_return_status varchar2(1);
13316 l_error_msg_code varchar2(30);
13317 l_budget_version_id number;
13318 
13319 CURSOR get_supp_dtls(l_ci_id_f number)
13320 	is
13321 	   select * from  PA_CI_SUPPLIER_DETAILS
13322 	   WHERE ci_id = l_ci_id_f;
13323 
13324 
13325 CURSOR check_supp_cost_reg_flag (ci_id_t number,ci_id_f number)
13326 	is
13327 	   select 1 from pa_ci_types_b ci_id_to,
13328 	    pa_ci_types_b ci_id_from
13329 	    where ci_id_to.ci_type_id in(
13330 		select ci_type_id
13331 		from  pa_control_items where ci_id=ci_id_t)
13332 		and ci_id_from.ci_type_id in (
13333 		select ci_type_id
13334 		from  pa_control_items where ci_id=ci_id_f )
13335 		and ci_id_to.supp_cost_reg_flag='Y'
13336 		and ci_id_from.supp_cost_reg_flag='Y' ;
13337 
13338 begin
13339 
13340 		IF p_pa_debug_mode = 'Y' THEN
13341 		pa_debug.init_err_stack('PAFPCIMB.copy_supplier_cost_data');
13342 		END IF;
13343 		fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
13344 		l_debug_mode := NVL(l_debug_mode, 'Y');
13345 		IF p_pa_debug_mode = 'Y' THEN
13346 		  pa_debug.set_process('PLSQL','LOG',l_debug_mode);
13347 		END IF;
13348 		x_return_status := FND_API.G_RET_STS_SUCCESS;
13349 		x_msg_count := 0;
13350         l_ci_id_to :=p_ci_id_to;
13351         l_ci_id_from :=p_ci_id_from;
13352 
13353         savepoint copy_supp_det;
13354 
13355 
13356         open check_supp_cost_reg_flag(l_ci_id_to,l_ci_id_from);
13357 	    fetch check_supp_cost_reg_flag into supp_cost_flag;
13358         close check_supp_cost_reg_flag;
13359 
13360 
13361         select ci_type_id
13362         into l_ci_type_id
13363         from pa_control_items
13364         where ci_id=p_ci_id_to ;
13365 
13366 -- Check if new suppler cost region exists in both the control items
13367 
13368   if  supp_cost_flag is null then
13369      x_return_status :=FND_API.G_RET_STS_SUCCESS;
13370      x_msg_count :=0;
13371 	 x_msg_data  :='There are no Supplier records to copy';
13372 
13373   else
13374    -- verify for each record from the source whether it exists in the target
13375    -- if it exists, do an update based on the criteria. else do an insert
13376      supp_from_tbl.delete;
13377      supp_to_tbl.delete;
13378 
13379      select budget_version_id
13380        into l_budget_version_id
13381        from pa_budget_versions
13382       where ci_id = l_ci_id_to
13383         and version_type in ('COST', 'ALL');
13384 
13385      open get_supp_dtls(l_ci_id_from);
13386      fetch get_supp_dtls bulk collect into supp_from_tbl;
13387      close get_supp_dtls;
13388 
13389      open get_supp_dtls(p_ci_id_to);
13390      fetch get_supp_dtls bulk collect into supp_to_tbl;
13391      close get_supp_dtls;
13392 
13393      -- rules for determining existence of record in target:
13394      -- 1. for change type 'Create New', if task,exp type, resource, currency,
13395      -- supplier, exp org, need by date are the same, then update, else insert
13396      -- 2. for change type 'Update Existing', if task,exp type, resource, currency,
13397      -- supplier, po number, po line number are the same, then update, else insert
13398      if supp_from_tbl.count > 0 then
13399        for i in supp_from_tbl.first..supp_from_tbl.last loop
13400          l_found := 'N';
13401          l_rec_index := 0;
13402          if supp_to_tbl.count > 0 then
13403          if supp_from_tbl(i).change_type = 'CREATE' then
13404            for j in supp_to_tbl.first..supp_to_tbl.last loop
13405              if supp_from_tbl(i).task_id =  supp_to_tbl(j).task_id and
13406                 nvl(supp_from_tbl(i).expenditure_type, 'X') = nvl(supp_to_tbl(j).expenditure_type, 'X') and
13407                 supp_from_tbl(i).resource_list_member_id = supp_to_tbl(j).resource_list_member_id and
13408                 supp_from_tbl(i).currency_code = supp_to_tbl(j).currency_code and
13409                 supp_from_tbl(i).vendor_id = supp_to_tbl(j).vendor_id and
13410                 nvl(supp_from_tbl(i).expenditure_org_id, -1) = nvl(supp_to_tbl(j).expenditure_org_id, -1) and
13411                 nvl(supp_from_tbl(i).need_by_date, sysdate) = nvl(supp_to_tbl(j).need_by_date, sysdate) then
13412                   l_found := 'Y';
13413                   l_rec_index := j;
13414                   supp_to_tbl(j).change_amount := nvl(supp_to_tbl(j).change_amount, 0) + nvl(supp_from_tbl(i).change_amount, 0);
13415 				  -- bug 9840053: compute the burdened cost..start
13416 				   if supp_to_tbl(j).burdened_cost is not null then
13417 				      supp_to_tbl(j).burdened_cost := supp_to_tbl(j).burdened_cost +
13418 				        (supp_to_tbl(j).burdened_cost/supp_to_tbl(j).raw_cost) * supp_from_tbl(i).raw_cost;
13419 				   else
13420 				      supp_to_tbl(j).burdened_cost := supp_from_tbl(i).burdened_cost;
13421 				   end if;
13422 				  -- bug 9840053: compute the burdened cost..end
13423 
13424                   supp_to_tbl(j).raw_cost := nvl(supp_to_tbl(j).raw_cost, 0) + nvl(supp_from_tbl(i).raw_cost, 0);
13425                   supp_to_tbl(j).from_change_date := least(supp_to_tbl(j).from_change_date, supp_from_tbl(i).from_change_date);
13426                   supp_to_tbl(j).to_change_date := greatest(supp_to_tbl(j).to_change_date, supp_from_tbl(i).to_change_date);
13427                   supp_to_tbl(j).Estimated_Cost := nvl(supp_to_tbl(j).Estimated_Cost, 0) + nvl(supp_from_tbl(i).Estimated_Cost, 0);
13428                   supp_to_tbl(j).Quoted_Cost := nvl(supp_to_tbl(j).Quoted_Cost, 0) + nvl(supp_from_tbl(i).Quoted_Cost, 0);
13429                   supp_to_tbl(j).Negotiated_Cost := nvl(supp_to_tbl(j).Negotiated_Cost, 0) + nvl(supp_from_tbl(i).Negotiated_Cost, 0);
13430                   exit;
13431              end if;
13432            end loop;
13433          elsif supp_from_tbl(i).change_type = 'UPDATE' then
13434            for j in supp_to_tbl.first..supp_to_tbl.last loop
13435              if supp_from_tbl(i).task_id =  supp_to_tbl(j).task_id and
13436                 nvl(supp_from_tbl(i).expenditure_type, 'X') = nvl(supp_to_tbl(j).expenditure_type, 'X') and
13437                 supp_from_tbl(i).resource_list_member_id = supp_to_tbl(j).resource_list_member_id and
13438                 supp_from_tbl(i).currency_code = supp_to_tbl(j).currency_code and
13439                 supp_from_tbl(i).vendor_id = supp_to_tbl(j).vendor_id and
13440                 nvl(supp_from_tbl(i).po_header_id, -1) = nvl(supp_to_tbl(j).po_header_id, -1) and
13441                 nvl(supp_from_tbl(i).po_line_id, -1) = nvl(supp_to_tbl(j).po_line_id, -1) then
13442                   l_found := 'Y';
13443                   l_rec_index := j;
13444                   supp_to_tbl(j).change_amount := nvl(supp_to_tbl(j).change_amount, 0) + nvl(supp_from_tbl(i).change_amount, 0);
13445 				  -- bug 9840053: compute the burdened cost..start
13446 				   if supp_to_tbl(j).burdened_cost is not null then
13447 				      supp_to_tbl(j).burdened_cost := supp_to_tbl(j).burdened_cost +
13448 				        (supp_to_tbl(j).burdened_cost/supp_to_tbl(j).raw_cost) * supp_from_tbl(i).raw_cost;
13449 				   else
13450 				      supp_to_tbl(j).burdened_cost := supp_from_tbl(i).burdened_cost;
13451 				   end if;
13452 				  -- bug 9840053: compute the burdened cost..end
13453 
13454                   supp_to_tbl(j).raw_cost := nvl(supp_to_tbl(j).raw_cost, 0) + nvl(supp_from_tbl(i).raw_cost, 0);
13455                   supp_to_tbl(j).from_change_date := least(supp_to_tbl(j).from_change_date, supp_from_tbl(i).from_change_date);
13456                   supp_to_tbl(j).to_change_date := greatest(supp_to_tbl(j).to_change_date, supp_from_tbl(i).to_change_date);
13457                   supp_to_tbl(j).Estimated_Cost := nvl(supp_to_tbl(j).Estimated_Cost, 0) + nvl(supp_from_tbl(i).Estimated_Cost, 0);
13458                   supp_to_tbl(j).Quoted_Cost := nvl(supp_to_tbl(j).Quoted_Cost, 0) + nvl(supp_from_tbl(i).Quoted_Cost, 0);
13459                   supp_to_tbl(j).Negotiated_Cost := nvl(supp_to_tbl(j).Negotiated_Cost, 0) + nvl(supp_from_tbl(i).Negotiated_Cost, 0);
13460                   exit;
13461              end if;
13462            end loop;
13463          end if;
13464          end if; --> supp_to_tbl.count > 0
13465 
13466          if l_found = 'N' then
13467                x_supp_ci_transaction_id :=null;
13468                PA_CI_SUPPLIER_PKG.insert_row (
13469                         x_rowid                   => x_supp_rowid
13470                         ,x_ci_transaction_id      => x_supp_ci_transaction_id
13471                         ,p_CI_TYPE_ID             => l_ci_type_id
13472                         ,p_CI_ID           	      => l_ci_id_to
13473                         ,p_CI_IMPACT_ID           => supp_from_tbl(i).ci_impact_id
13474                         ,p_VENDOR_ID              => supp_from_tbl(i).vendor_id
13475                         ,p_PO_HEADER_ID           => supp_from_tbl(i).po_header_id
13476                         ,p_PO_LINE_ID             => supp_from_tbl(i).po_line_id
13477                         ,p_ADJUSTED_TRANSACTION_ID => supp_from_tbl(i).ADJUSTED_CI_TRANSACTION_ID
13478                         ,p_CURRENCY_CODE           => supp_from_tbl(i).CURRENCY_CODE
13479                         ,p_CHANGE_AMOUNT           => supp_from_tbl(i).CHANGE_AMOUNT
13480                         ,p_CHANGE_TYPE             => supp_from_tbl(i).CHANGE_TYPE
13481                         ,p_CHANGE_DESCRIPTION      => supp_from_tbl(i).CHANGE_DESCRIPTION
13482                         ,p_CREATED_BY              => FND_GLOBAL.login_id
13483                         ,p_CREATION_DATE           => trunc(sysdate)
13484                         ,p_LAST_UPDATED_BY         => FND_GLOBAL.login_id
13485                         ,p_LAST_UPDATE_DATE        => trunc(sysdate)
13486                         ,p_LAST_UPDATE_LOGIN       => FND_GLOBAL.login_id
13487                         ,p_Task_Id                 => supp_from_tbl(i).Task_Id
13488                         ,p_Resource_List_Mem_Id    => supp_from_tbl(i).Resource_List_Member_Id
13489                         ,p_From_Date               => supp_from_tbl(i).FROM_CHANGE_DATE
13490                         ,p_To_Date                 => supp_from_tbl(i).TO_CHANGE_DATE
13491                         ,p_Estimated_Cost          => supp_from_tbl(i).Estimated_Cost
13492                         ,p_Quoted_Cost             => supp_from_tbl(i).Quoted_Cost
13493                         ,p_Negotiated_Cost         => supp_from_tbl(i).Negotiated_Cost
13494                         ,p_Burdened_cost           => supp_from_tbl(i).Burdened_cost
13495                         ,p_revenue_override_rate   => supp_from_tbl(i).revenue_override_rate
13496                         ,p_audit_history_number    =>  1
13497                         ,p_current_audit_flag      =>  'Y'
13498                         ,p_Original_supp_trans_id  =>  0
13499                         ,p_Source_supp_trans_id    =>  0
13500                         ,p_Sup_ref_no              =>  supp_from_tbl(i).sup_quote_ref_no
13501                         ,p_version_type            =>  supp_from_tbl(i).status
13502                         ,p_ci_status               => 'COST'
13503                         /* Changes for 12.1.3 start */
13504                         ,p_expenditure_type        => supp_from_tbl(i).expenditure_type
13505                         ,p_expenditure_org_id      => supp_from_tbl(i).expenditure_org_id
13506                         ,p_change_reason_code      => supp_from_tbl(i).change_reason_code
13507                         ,p_quote_negotiation_reference  => supp_from_tbl(i).quote_negotiation_reference
13508                         ,p_need_by_date            => supp_from_tbl(i).need_by_date
13509                         /* Changes for 12.1.3 end */
13510                         ,x_return_status           => x_return_status
13511                         ,x_error_msg_code          => x_msg_data  );
13512 
13513                     IF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
13514                         RAISE G_EXCEPTION_UNEXPECTED_ERROR;
13515                     ELSIF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
13516                         RAISE G_EXCEPTION_ERROR;
13517                     END IF;
13518 
13519                     update pa_ci_supplier_details a
13520                        set resource_assignment_id =
13521                            (select prc.resource_assignment_id
13522                               from pa_resource_asgn_curr prc, pa_resource_assignments pra
13523                              where prc.resource_assignment_id = pra.resource_assignment_id
13524                                and pra.budget_version_id = l_budget_version_id
13525                                and pra.task_id = a.task_id
13526                                and pra.resource_list_member_id = a.resource_list_member_id
13527                                and prc.txn_currency_code = a.currency_code)
13528                      where a.ci_id = l_ci_id_to
13529 					   and a.ci_transaction_id = x_supp_ci_transaction_id;
13530 
13531          else -- l_found = 'Y'
13532                PA_CI_SUPPLIER_PKG.update_row (
13533                   p_rowid                    => x_supp_rowid
13534                   ,p_ci_transaction_id       => supp_to_tbl(l_rec_index).ci_transaction_id
13535                   ,p_CI_TYPE_ID              => supp_to_tbl(l_rec_index).ci_type_id
13536                   ,p_CI_ID           	     => supp_to_tbl(l_rec_index).ci_id
13537                   ,p_CI_IMPACT_ID            => supp_to_tbl(l_rec_index).ci_impact_id
13538                   ,p_VENDOR_ID               => supp_to_tbl(l_rec_index).vendor_id
13539                   ,p_PO_HEADER_ID            => supp_to_tbl(l_rec_index).po_header_id
13540                   ,p_PO_LINE_ID              => supp_to_tbl(l_rec_index).po_line_id
13541                   ,p_ADJUSTED_TRANSACTION_ID => supp_to_tbl(l_rec_index).adjusted_ci_transaction_id
13542                   ,p_CURRENCY_CODE           => supp_to_tbl(l_rec_index).currency_code
13543                   ,p_CHANGE_AMOUNT           => supp_to_tbl(l_rec_index).change_amount
13544                   ,p_CHANGE_TYPE             => supp_to_tbl(l_rec_index).change_type
13545                   ,p_CHANGE_DESCRIPTION      => supp_to_tbl(l_rec_index).change_description
13546                   ,p_LAST_UPDATED_BY         => FND_GLOBAL.login_id
13547                   ,p_LAST_UPDATE_DATE        => trunc(sysdate)
13548                   ,p_LAST_UPDATE_LOGIN       => FND_GLOBAL.login_id
13549                   ,p_Task_Id                 => supp_to_tbl(l_rec_index).task_id
13550                   ,p_Resource_List_Mem_Id    => supp_to_tbl(l_rec_index).resource_list_member_id
13551                   ,p_From_Date               => supp_to_tbl(l_rec_index).from_change_date
13552                   ,p_To_Date                 => supp_to_tbl(l_rec_index).to_change_date
13553                   ,p_Estimated_Cost          => supp_to_tbl(l_rec_index).estimated_cost
13554                   ,p_Quoted_Cost             => supp_to_tbl(l_rec_index).quoted_cost
13555                   ,p_Negotiated_Cost         => supp_to_tbl(l_rec_index).negotiated_cost
13556                   ,p_Burdened_cost           => supp_to_tbl(l_rec_index).burdened_cost
13557                   ,p_Revenue                 => supp_to_tbl(l_rec_index).revenue
13558                   ,p_revenue_override_rate   => supp_to_tbl(l_rec_index).revenue_override_rate
13559                   ,p_audit_history_number    => supp_to_tbl(l_rec_index).audit_history_number
13560                   ,p_current_audit_flag      => supp_to_tbl(l_rec_index).current_audit_flag
13561                   ,p_Original_supp_trans_id  => supp_to_tbl(l_rec_index).original_supp_trans_id
13562                   ,p_Source_supp_trans_id    => supp_to_tbl(l_rec_index).source_supp_trans_id
13563                   ,p_Sup_ref_no              => supp_to_tbl(l_rec_index).sup_quote_ref_no
13564                   ,p_version_type            => supp_to_tbl(l_rec_index).status
13565                   ,p_expenditure_type        => supp_to_tbl(l_rec_index).expenditure_type
13566                   ,p_expenditure_org_id      => supp_to_tbl(l_rec_index).expenditure_org_id
13567                   ,p_change_reason_code      => supp_to_tbl(l_rec_index).change_reason_code
13568                   ,p_quote_negotiation_reference => supp_to_tbl(l_rec_index).quote_negotiation_reference
13569                   ,p_need_by_date            => supp_to_tbl(l_rec_index).need_by_date
13570                   ,x_return_status           => l_return_status
13571                   ,x_error_msg_code          => l_error_msg_code );
13572 
13573                  IF (l_return_status = PA_API.G_RET_STS_UNEXP_ERROR) THEN
13574                    RAISE PA_API.G_EXCEPTION_UNEXPECTED_ERROR;
13575                  ELSIF (l_return_status = PA_API.G_RET_STS_ERROR) THEN
13576                    RAISE PA_API.G_EXCEPTION_ERROR;
13577                  END IF;
13578 
13579          end if;  -- l_found
13580 
13581 		 /* -- bug: 9840053 .. moved the update to after insert.
13582          select budget_version_id
13583            into l_budget_version_id
13584            from pa_budget_versions
13585           where ci_id = l_ci_id_to
13586             and version_type in ('COST', 'ALL');
13587 
13588             update pa_ci_supplier_details a
13589                set (resource_assignment_id, from_change_date, to_change_date, burdened_cost) =
13590                    (select prc.resource_assignment_id,
13591                            nvl(a.from_change_date, pra.planning_start_date),
13592                            nvl(a.to_change_date, pra.planning_end_date),
13593                            a.raw_cost * prc.txn_average_burden_cost_rate
13594                       from pa_resource_asgn_curr prc, pa_resource_assignments pra
13595                      where prc.resource_assignment_id = pra.resource_assignment_id
13596                        and pra.budget_version_id = l_budget_version_id
13597                        and pra.task_id = a.task_id
13598                        and pra.resource_list_member_id = a.resource_list_member_id
13599                        and prc.txn_currency_code = a.currency_code)
13600              where a.ci_id = l_ci_id_to;
13601           */
13602 
13603        end loop;
13604      end if; -- supp_from_tbl.count > 0 then
13605    -- end of bug 9674883 enh
13606   end if;
13607 EXCEPTION
13608   WHEN OTHERS THEN
13609     ROLLBACK TO copy_supp_det;
13610     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13611     fnd_msg_pub.add_exc_msg(p_pkg_name       => 'PA_FP_CI_MERGE',
13612                             p_procedure_name => 'copy_supplier_cost_data',
13613                             p_error_text     => SUBSTRB(SQLERRM,1,240));
13614 
13615     fnd_msg_pub.count_and_get(p_count => x_msg_count,
13616                               p_data  => x_msg_data);
13617     RAISE;
13618 end  copy_supplier_cost_data;
13619 /* Added for 12.1.3 for Enc to copy the direct cost data */
13620 
13621 
13622 procedure copy_direct_cost_data(
13623          p_ci_id_to               IN      NUMBER
13624         ,p_ci_id_from             IN      NUMBER
13625         ,p_bv_id                  IN      pa_budget_versions.budget_version_id%TYPE
13626         ,p_project_id              IN      NUMBER
13627         ,x_return_status          OUT     NOCOPY VARCHAR2
13628         ,x_msg_count              OUT     NOCOPY NUMBER
13629 		,x_msg_data               OUT     NOCOPY VARCHAR2
13630 ) IS
13631     l_project_id       NUMBER ;
13632     l_bv_id           pa_budget_versions.budget_version_id%TYPE ;
13633     l_task_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE ;
13634     l_expenditure_type_tbl          SYSTEM.PA_VARCHAR2_30_TBL_TYPE ;
13635     l_rlmi_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE;
13636     l_unit_of_measure_tbl           SYSTEM.PA_VARCHAR2_30_TBL_TYPE;
13637     l_currency_code_tbl             SYSTEM.PA_VARCHAR2_30_TBL_TYPE;
13638     l_planning_resource_rate_tbl    SYSTEM.PA_NUM_TBL_TYPE ;
13639     l_quantity_tbl                  SYSTEM.PA_NUM_TBL_TYPE ;
13640     l_raw_cost_tbl                  SYSTEM.PA_NUM_TBL_TYPE ;
13641     l_burdened_cost_tbl             SYSTEM.PA_NUM_TBL_TYPE ;
13642     l_raw_cost_rate_tbl             SYSTEM.PA_NUM_TBL_TYPE ;
13643     l_burden_cost_rate_tbl          SYSTEM.PA_NUM_TBL_TYPE ;
13644     l_resource_assignment_id_tbl    SYSTEM.PA_NUM_TBL_TYPE ;
13645     l_effective_from_tbl            SYSTEM.PA_DATE_TBL_TYPE;
13646     l_effective_to_tbl              SYSTEM.PA_DATE_TBL_TYPE;
13647     l_change_reason_code            SYSTEM.PA_VARCHAR2_30_TBL_TYPE ;
13648     l_change_description            SYSTEM.PA_VARCHAR2_2000_TBL_TYPE;
13649     -- target ci variables
13650     l_dc_line_id_to_tbl                SYSTEM.PA_NUM_TBL_TYPE ;
13651     l_task_id_to_tbl                   SYSTEM.PA_NUM_TBL_TYPE ;
13652     l_expenditure_type_to_tbl          SYSTEM.PA_VARCHAR2_30_TBL_TYPE ;
13653     l_rlmi_id_to_tbl                   SYSTEM.PA_NUM_TBL_TYPE;
13654     l_unit_of_measure_to_tbl           SYSTEM.PA_VARCHAR2_30_TBL_TYPE;
13655     l_currency_code_to_tbl             SYSTEM.PA_VARCHAR2_30_TBL_TYPE;
13656     l_resource_rate_to_tbl             SYSTEM.PA_NUM_TBL_TYPE ;
13657     l_quantity_to_tbl                  SYSTEM.PA_NUM_TBL_TYPE ;
13658     l_raw_cost_to_tbl                  SYSTEM.PA_NUM_TBL_TYPE ;
13659     l_burdened_cost_to_tbl             SYSTEM.PA_NUM_TBL_TYPE ;
13660     l_raw_cost_rate_to_tbl             SYSTEM.PA_NUM_TBL_TYPE ;
13661     l_burden_cost_rate_to_tbl          SYSTEM.PA_NUM_TBL_TYPE ;
13662     l_resource_asgn_id_to_tbl          SYSTEM.PA_NUM_TBL_TYPE ;
13663     l_effective_from_to_tbl            SYSTEM.PA_DATE_TBL_TYPE;
13664     l_effective_to_to_tbl              SYSTEM.PA_DATE_TBL_TYPE;
13665     -- for insert
13666     i_task_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13667     i_expenditure_type_tbl          SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13668     i_rlmi_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13669     i_unit_of_measure_tbl           SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13670     i_currency_code_tbl             SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13671     i_quantity_tbl                  SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13672     i_raw_cost_tbl                  SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13673     i_planning_resource_rate_tbl    SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13674     i_burdened_cost_tbl             SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13675     i_burden_cost_rate_tbl          SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13676     i_effective_from_tbl            SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
13677     i_effective_to_tbl              SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
13678     -- for update
13679     u_dc_line_id_tbl                SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13680     u_task_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13681     u_expenditure_type_tbl          SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13682     u_rlmi_id_tbl                   SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13683     u_unit_of_measure_tbl           SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13684     u_currency_code_tbl             SYSTEM.PA_VARCHAR2_30_TBL_TYPE := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13685     u_planning_resource_rate_tbl    SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13686     u_quantity_tbl                  SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13687     u_raw_cost_tbl                  SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13688     u_burdened_cost_tbl             SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13689     u_raw_cost_rate_tbl             SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13690     u_burden_cost_rate_tbl          SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13691     u_resource_assignment_id_tbl    SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13692     u_effective_from_tbl            SYSTEM.PA_DATE_TBL_TYPE:= SYSTEM.PA_DATE_TBL_TYPE();
13693     u_effective_to_tbl              SYSTEM.PA_DATE_TBL_TYPE := SYSTEM.PA_DATE_TBL_TYPE();
13694     u_change_reason_code            SYSTEM.PA_VARCHAR2_30_TBL_TYPE  := SYSTEM.PA_VARCHAR2_30_TBL_TYPE();
13695     u_change_description            SYSTEM.PA_VARCHAR2_2000_TBL_TYPE  := SYSTEM.PA_VARCHAR2_2000_TBL_TYPE();
13696     --
13697     l_api_version	     number := 1;
13698     l_api_name          CONSTANT VARCHAR2(60) := 'PA_FP_CI_MERGE.copy_direct_cost_data';
13699     l_return_status     VARCHAR2(1) := PA_API.G_RET_STS_SUCCESS;
13700     l_msg_count	     number;
13701     l_msg_data          varchar2(2000);
13702     l_dir_cost_flag  VARCHAR2(1);
13703  	l_debug_mode    VARCHAR2(30);
13704  	subtype PaCiDirCostTblType is pa_ci_dir_cost_pvt.PaCiDirectCostDetailsTblType;
13705  	l_dc_line_id_tbl    SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13706     i_PaCiDirCostDetTbl PaCiDirCostTblType;
13707     u_PaCiDirCostDetTbl PaCiDirCostTblType;
13708     x_PaCiDirCostDetTbl PaCiDirCostTblType;
13709 
13710 	l_found       SYSTEM.PA_VARCHAR2_1_TBL_TYPE := SYSTEM.PA_VARCHAR2_1_TBL_TYPE();
13711 	l_rec_index   SYSTEM.PA_NUM_TBL_TYPE := SYSTEM.PA_NUM_TBL_TYPE();
13712 
13713     ins_index     number;
13714     upd_index     number;
13715     seq_value     number;
13716 
13717  	l_ci_type_id number := null;
13718  	l_ci_id_to number := null;
13719  	l_ci_id_from number := null;
13720     l_count      number := null;
13721  	G_EXCEPTION_ERROR		EXCEPTION;
13722 	G_EXCEPTION_UNEXPECTED_ERROR	EXCEPTION;
13723 
13724 CURSOR check_dir_cost_reg_flag (ci_id_t number,ci_id_f number)
13725 	is
13726 	   select 1 from pa_ci_types_b ci_id_to,
13727 	    pa_ci_types_b ci_id_from
13728 	    where ci_id_to.ci_type_id in(
13729 		select ci_type_id
13730 		from  pa_control_items where ci_id=ci_id_t)
13731 		and ci_id_from.ci_type_id in (
13732 		select ci_type_id
13733 		from  pa_control_items where ci_id=ci_id_f )
13734 		and ci_id_to.dir_cost_reg_flag='Y'
13735 		and ci_id_from.dir_cost_reg_flag='Y' ;
13736 
13737 begin
13738        savepoint copy_dir_cost_det;
13739        l_project_id := p_project_id;
13740        l_ci_id_to    :=p_ci_id_to ;
13741        l_ci_id_from  :=p_ci_id_from ;
13742         open check_dir_cost_reg_flag(l_ci_id_to,l_ci_id_from);
13743 	    fetch check_dir_cost_reg_flag into l_dir_cost_flag;
13744         close check_dir_cost_reg_flag;
13745 
13746 -- Check if new direct cost region exists in both the control items
13747 
13748   if  l_dir_cost_flag is null then
13749      x_return_status :=FND_API.G_RET_STS_SUCCESS;
13750      x_msg_count :=0;
13751 	 x_msg_data  :='Cannot copy direct cost records';
13752   else
13753      select task_id
13754            ,expenditure_type
13755            ,resource_list_member_id
13756            ,unit_of_measure
13757            ,currency_code
13758            ,quantity
13759            ,raw_cost
13760 		   ,planning_resource_rate
13761 		   ,burdened_cost
13762 		   ,burden_cost_rate
13763            ,change_reason_code
13764            ,change_description
13765            ,effective_from
13766            ,effective_to
13767        BULK COLLECT into
13768        	    l_task_id_tbl
13769 		  	,l_expenditure_type_tbl
13770 			,l_rlmi_id_tbl
13771 			,l_unit_of_measure_tbl
13772 			,l_currency_code_tbl
13773 			,l_quantity_tbl
13774 			,l_raw_cost_tbl
13775 			,l_planning_resource_rate_tbl
13776 			,l_burdened_cost_tbl
13777 			,l_burden_cost_rate_tbl
13778 			,l_change_reason_code
13779 			,l_change_description
13780             ,l_effective_from_tbl
13781             ,l_effective_to_tbl
13782 	    From pa_ci_direct_cost_details
13783 	   where ci_id = p_ci_id_from ;
13784 
13785     select dc_line_id
13786           ,task_id
13787           ,expenditure_type
13788           ,resource_list_member_id
13789           ,unit_of_measure
13790           ,currency_code
13791           ,quantity
13792           ,raw_cost
13793 		  ,planning_resource_rate
13794 		  ,burdened_cost
13795 		  ,burden_cost_rate
13796           ,effective_from
13797           ,effective_to
13798           ,resource_assignment_id
13799       bulk collect into
13800            l_dc_line_id_to_tbl
13801           ,l_task_id_to_tbl
13802 		  ,l_expenditure_type_to_tbl
13803 		  ,l_rlmi_id_to_tbl
13804           ,l_unit_of_measure_to_tbl
13805 		  ,l_currency_code_to_tbl
13806 		  ,l_quantity_to_tbl
13807           ,l_raw_cost_to_tbl
13808           ,l_resource_rate_to_tbl
13809 		  ,l_burdened_cost_to_tbl
13810           ,l_burden_cost_rate_to_tbl
13811           ,l_effective_from_to_tbl
13812           ,l_effective_to_to_tbl
13813           ,l_resource_asgn_id_to_tbl
13814       From pa_ci_direct_cost_details
13815 	 where ci_id = l_ci_id_to ;
13816 
13817     ins_index := 0;
13818     upd_index := 0;
13819 
13820   if l_task_id_tbl.count > 0 then
13821         l_found.extend(l_task_id_tbl.count);
13822     if l_task_id_to_tbl.count > 0 then
13823       for i in l_task_id_tbl.first..l_task_id_tbl.last loop
13824         l_rec_index.extend(1);
13825         l_rec_index(i) := 0;
13826         for j in l_task_id_to_tbl.first..l_task_id_to_tbl.last loop
13827           if l_task_id_tbl(i) = l_task_id_to_tbl(j) and
13828              l_expenditure_type_tbl(i) = l_expenditure_type_to_tbl(j) and
13829              l_rlmi_id_tbl(i) = l_rlmi_id_to_tbl(j) and
13830              l_currency_code_tbl(i) = l_currency_code_to_tbl(j) then
13831                l_found(i) := 'Y';
13832                l_rec_index(i) := j;
13833 
13834              if l_quantity_tbl(i) is not null then
13835                l_quantity_tbl(i) := l_quantity_to_tbl(j) + l_quantity_tbl(i);
13836              end if;
13837 
13838              if l_raw_cost_tbl(i) is not null then
13839                l_raw_cost_tbl(i) := l_raw_cost_to_tbl(j) + l_raw_cost_tbl(i);
13840              end if;
13841 
13842 			 -- for the rates, if the target has values they take precedence.
13843 			 -- these rates will be used for computing the costs.
13844 			 -- this avoids reading the resource assignment rates, where an override may be present.
13845 			 -- for bug 9840053
13846 
13847 			 if l_resource_rate_to_tbl(j) is not null then
13848 			   l_planning_resource_rate_tbl(i) := l_resource_rate_to_tbl(j);
13849 			   l_burden_cost_rate_tbl(i) := nvl(l_burden_cost_rate_to_tbl(j), l_burden_cost_rate_tbl(i));
13850              end if;
13851 
13852              l_effective_from_tbl(i) := least(l_effective_from_to_tbl(j), l_effective_from_tbl(i));
13853              l_effective_to_tbl(i) := greatest(l_effective_to_to_tbl(j), l_effective_to_tbl(i));
13854              exit;
13855           end if;
13856         end loop;
13857       end loop;
13858     end if;
13859 
13860     for i in l_task_id_tbl.first..l_task_id_tbl.last loop
13861       if nvl(l_found(i), 'N') = 'Y' then
13862          upd_index := upd_index + 1;
13863 
13864          u_PaCiDirCostDetTbl(upd_index).dc_line_id              := l_dc_line_id_to_tbl(l_rec_index(i));
13865          u_PaCiDirCostDetTbl(upd_index).ci_id                   := l_ci_id_to;
13866          u_PaCiDirCostDetTbl(upd_index).project_id              := l_project_id;
13867          u_PaCiDirCostDetTbl(upd_index).task_id                 := l_task_id_tbl(i);
13868          u_PaCiDirCostDetTbl(upd_index).expenditure_type        := l_expenditure_type_tbl(i);
13869          u_PaCiDirCostDetTbl(upd_index).resource_list_member_id := l_rlmi_id_tbl(i);
13870          u_PaCiDirCostDetTbl(upd_index).unit_of_measure         := l_unit_of_measure_tbl(i);
13871          u_PaCiDirCostDetTbl(upd_index).currency_code           := l_currency_code_tbl(i);
13872          u_PaCiDirCostDetTbl(upd_index).quantity                := l_quantity_tbl(i);
13873          u_PaCiDirCostDetTbl(upd_index).planning_resource_rate  := l_planning_resource_rate_tbl(i);
13874 	     u_PaCiDirCostDetTbl(upd_index).raw_cost                := l_raw_cost_tbl(i);
13875          u_PaCiDirCostDetTbl(upd_index).burdened_cost           := FND_API.G_MISS_NUM;
13876          u_PaCiDirCostDetTbl(upd_index).raw_cost_rate           := FND_API.G_MISS_NUM;
13877          u_PaCiDirCostDetTbl(upd_index).burden_cost_rate        := l_burden_cost_rate_tbl(i);
13878          u_PaCiDirCostDetTbl(upd_index).resource_assignment_id  := FND_API.G_MISS_NUM;
13879          u_PaCiDirCostDetTbl(upd_index).effective_from          := l_effective_from_tbl(i);
13880          u_PaCiDirCostDetTbl(upd_index).effective_to            := l_effective_to_tbl(i);
13881          u_PaCiDirCostDetTbl(upd_index).change_reason_code      := FND_API.G_MISS_CHAR;
13882          u_PaCiDirCostDetTbl(upd_index).change_description      := FND_API.G_MISS_CHAR;
13883       else
13884          ins_index := ins_index + 1;
13885 	       select pa_ci_dir_cost_details_s.nextval
13886 	         into seq_value
13887 	         from dual;
13888 
13889          i_PaCiDirCostDetTbl(ins_index).dc_line_id              := seq_value;
13890          i_PaCiDirCostDetTbl(ins_index).ci_id                   := l_ci_id_to;
13891          i_PaCiDirCostDetTbl(ins_index).project_id              := l_project_id;
13892          i_PaCiDirCostDetTbl(ins_index).task_id                 := l_task_id_tbl(i);
13893          i_PaCiDirCostDetTbl(ins_index).expenditure_type        := l_expenditure_type_tbl(i);
13894          i_PaCiDirCostDetTbl(ins_index).resource_list_member_id := l_rlmi_id_tbl(i);
13895          i_PaCiDirCostDetTbl(ins_index).unit_of_measure         := l_unit_of_measure_tbl(i);
13896          i_PaCiDirCostDetTbl(ins_index).currency_code           := l_currency_code_tbl(i);
13897 
13898          if l_quantity_tbl.exists(i) then
13899             i_PaCiDirCostDetTbl(ins_index).quantity             := l_quantity_tbl(i);
13900          else
13901             i_PaCiDirCostDetTbl(ins_index).quantity             := NULL;
13902          end if;
13903 
13904 	     if l_planning_resource_rate_tbl.exists(i) then
13905            i_PaCiDirCostDetTbl(ins_index).planning_resource_rate := l_planning_resource_rate_tbl(i);
13906 	     else
13907            i_PaCiDirCostDetTbl(ins_index).planning_resource_rate := NULL;
13908 	     end if;
13909 
13910          if l_raw_cost_tbl.exists(i) then
13911            i_PaCiDirCostDetTbl(ins_index).raw_cost              := l_raw_cost_tbl(i);
13912          else
13913            i_PaCiDirCostDetTbl(ins_index).raw_cost              := null;
13914          end if;
13915 
13916          i_PaCiDirCostDetTbl(ins_index).burdened_cost            := l_burdened_cost_tbl(i);
13917          i_PaCiDirCostDetTbl(ins_index).raw_cost_rate            := NULL;
13918          i_PaCiDirCostDetTbl(ins_index).burden_cost_rate         := l_burden_cost_rate_tbl(i);
13919 
13920          i_PaCiDirCostDetTbl(ins_index).resource_assignment_id   := NULL;
13921 
13922          i_PaCiDirCostDetTbl(ins_index).effective_from           := NULL;
13923          i_PaCiDirCostDetTbl(ins_index).effective_to             := NULL;
13924 
13925          if l_change_reason_code.exists(i) then
13926             i_PaCiDirCostDetTbl(ins_index).change_reason_code    := l_change_reason_code(i);
13927          else
13928             i_PaCiDirCostDetTbl(ins_index).change_reason_code    := NULL;
13929          end if;
13930 
13931          if l_change_description.exists(i)  then
13932             i_PaCiDirCostDetTbl(ins_index).change_description    := l_change_description(i);
13933          else
13934             i_PaCiDirCostDetTbl(ins_index).change_description    := NULL;
13935          end if;
13936 
13937          i_PaCiDirCostDetTbl(ins_index).creation_date            := sysdate;
13938          i_PaCiDirCostDetTbl(ins_index).created_by               := FND_GLOBAL.USER_ID;
13939          i_PaCiDirCostDetTbl(ins_index).last_update_date         := sysdate;
13940          i_PaCiDirCostDetTbl(ins_index).last_update_by           := FND_GLOBAL.USER_ID;
13941          i_PaCiDirCostDetTbl(ins_index).last_update_login        := FND_GLOBAL.LOGIN_ID;
13942         end if;
13943        end loop;
13944 
13945       if ins_index > 0 then
13946         pa_ci_dir_cost_pvt.insert_row(
13947           p_api_version                  => l_api_version,
13948           p_init_msg_list                => FND_API.G_FALSE,
13949           x_return_status                => l_return_status,
13950           x_msg_count                    => l_msg_count,
13951           x_msg_data                     => l_msg_data,
13952           PPaCiDirectCostDetailsTbl      => i_PaCiDirCostDetTbl,
13953           XPaCiDirectCostDetailsTbl      => x_PaCiDirCostDetTbl);
13954 
13955         IF (l_return_status = PA_API.G_RET_STS_UNEXP_ERROR) THEN
13956           RAISE PA_API.G_EXCEPTION_UNEXPECTED_ERROR;
13957         ELSIF (l_return_status = PA_API.G_RET_STS_ERROR) THEN
13958           RAISE PA_API.G_EXCEPTION_ERROR;
13959         END IF;
13960 
13961       end if;
13962 
13963       if upd_index > 0 then
13964          pa_ci_dir_cost_pvt.update_row(
13965       	       p_api_version                  => l_api_version,
13966      	       p_init_msg_list                => FND_API.G_FALSE,
13967     	       x_return_status                => l_return_status,
13968     	       x_msg_count                    => l_msg_count,
13969     	       x_msg_data                     => l_msg_data,
13970     	       PPaCiDirectCostDetailsTbl      => u_PaCiDirCostDetTbl,
13971     	       XPaCiDirectCostDetailsTbl      => x_PaCiDirCostDetTbl);
13972 
13973           IF (l_return_status = PA_API.G_RET_STS_UNEXP_ERROR) THEN
13974             RAISE PA_API.G_EXCEPTION_UNEXPECTED_ERROR;
13975           ELSIF (l_return_status = PA_API.G_RET_STS_ERROR) THEN
13976             RAISE PA_API.G_EXCEPTION_ERROR;
13977           END IF;
13978       end if;
13979 
13980 	   -- bug 9840053: modified the update stmt.
13981        update pa_ci_direct_cost_details pcdc
13982           set (resource_assignment_id, effective_from, effective_to,
13983                planning_resource_rate, burden_cost_rate,
13984 			         raw_cost, burdened_cost) =
13985                   (select prac.resource_assignment_id,
13986                           nvl(pcdc.effective_from, pra.planning_start_date),
13987                           nvl(pcdc.effective_to, pra.planning_end_date),
13988                           nvl(pcdc.planning_resource_rate, prac.txn_average_raw_cost_rate),
13989                           nvl(pcdc.burden_cost_rate, prac.txn_average_burden_cost_rate),
13990                           decode(pcdc.quantity, null, pcdc.raw_cost,
13991                                     pcdc.quantity * nvl(pcdc.planning_resource_rate, prac.txn_average_raw_cost_rate)),
13992                           decode(pcdc.quantity, null,
13993                                     pcdc.raw_cost * nvl(pcdc.burden_cost_rate, prac.txn_average_burden_cost_rate),
13994                                     pcdc.quantity * nvl(pcdc.burden_cost_rate, prac.txn_average_burden_cost_rate))
13995                      from pa_resource_assignments pra, pa_resource_asgn_curr prac
13996                     where pra.budget_version_id = p_bv_id
13997                       and pra.task_id = pcdc.task_id
13998                       and pra.resource_list_member_id = pcdc.resource_list_member_id
13999 					  and prac.txn_currency_code = pcdc.currency_code
14000                       and prac.resource_assignment_id = pra.resource_assignment_id
14001                       and ((prac.total_quantity is not null and
14002                            nvl(pa_planning_resource_utils.get_rate_based_flag(pcdc.resource_list_member_id), 'N') = 'Y')
14003                            OR
14004                            (prac.total_txn_raw_cost is not null and
14005                            nvl(pa_planning_resource_utils.get_rate_based_flag(pcdc.resource_list_member_id), 'N') = 'N')))
14006         where ci_id =l_ci_id_to
14007           and ((pcdc.quantity is not null and
14008                 nvl(pa_planning_resource_utils.get_rate_based_flag(pcdc.resource_list_member_id), 'N') = 'Y')
14009                 OR
14010                 (pcdc.raw_cost is not null and
14011                 nvl(pa_planning_resource_utils.get_rate_based_flag(pcdc.resource_list_member_id), 'N') = 'N'));
14012 
14013        update pa_ci_direct_cost_details pcdc
14014           set (resource_assignment_id, effective_from, effective_to) =
14015                   (select prac.resource_assignment_id,
14016 				          nvl(pcdc.effective_from, pra.planning_start_date),
14017 						  nvl(pcdc.effective_to, pra.planning_end_date)
14018                      from pa_resource_assignments pra, pa_resource_asgn_curr prac
14019                     where pra.budget_version_id = p_bv_id
14020                       and pra.task_id = pcdc.task_id
14021                       and pra.resource_list_member_id = pcdc.resource_list_member_id
14022 					  and prac.txn_currency_code = pcdc.currency_code
14023                       and prac.resource_assignment_id = pra.resource_assignment_id
14024                       and (prac.total_quantity is null and
14025                            nvl(pa_planning_resource_utils.get_rate_based_flag(pcdc.resource_list_member_id), 'N') = 'Y'))
14026         where ci_id =l_ci_id_to
14027           and (pcdc.quantity is null and
14028                 nvl(pa_planning_resource_utils.get_rate_based_flag(pcdc.resource_list_member_id), 'N') = 'Y');
14029 
14030    end if; --> l_task_id_tbl.count > 0
14031  end if;
14032 
14033 EXCEPTION
14034   WHEN OTHERS THEN
14035     ROLLBACK TO copy_dir_cost_det;
14036     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
14037     fnd_msg_pub.add_exc_msg(p_pkg_name       => 'PA_FP_CI_MERGE',
14038                             p_procedure_name => 'copy_direct_cost_data',
14039                             p_error_text     => SUBSTRB(SQLERRM,1,240));
14040     fnd_msg_pub.count_and_get(p_count => x_msg_count,
14041                               p_data  => x_msg_data);
14042     RAISE;
14043 end copy_direct_cost_data;
14044 --end of PACKAGE pa_fp_ci_merge
14045 END PA_FP_CI_MERGE;