DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_FP_ORG_FCST_GEN_PUB

Source


1 PACKAGE BODY pa_fp_org_fcst_gen_pub as
2 /* $Header: PAFPORGB.pls 120.4.12020000.2 2013/03/28 12:22:55 bpottipa ship $ */
3 /**
4 -- Start of Comments
5 -- Package name     : PA_FP_ORG_FCST_GEN_PUB
6 -- Purpose          :
7 -- History          :
8 -- 27-SEP-2002          sdebroy       FPB2: Changes made due to addition of
9 --                                    budget_line_id into pa_budget_lines
10 --                                    table .
11 -- 26-NOV-02            ssarma        FPB4: Added txn_currency_code to
12 --                                    all inserts into budget_lines.
13 --                                    The value will be that of PFC.
14 --                                    This is done b'coz txn_currency_code
15 --                                    is a new not null column from
16 --                                    patchset K.
17 -- 10-JAN-03            ssarma        FPB7: Fix for 2744924. Look for seeded
18 --                                    row for amount sets.
19 -- 12-FEB-03            vejayara      Bug 2796261-Source_Txn_currency_code in
20 --                                    pa_fin_plan_lines_tmp was not populated.
21 --                                    Now it is populated.
22 -- 02-JUN-03            msoundra replaced the call to
23 --                               API pa_fp_org_fcst_utils.get_utilization_details
24 --              with pa_pji_util_pkg.get_utilization_dtls to get the numbers from
25 --              PJI data model if PJI is installed.
26 -- 21-AUG-03            dbora         Bug 3106741 Performance related changes
27 -- 25-AUG-08            bifernan      Bug 7309811 Use byte equivalent versions of
28 --                                    substr and length functions
29 -- 01-Feb-2013 bpottipa  Bug 16200605 - CBS enhancement
30 -- NOTE             :
31 -- End of Comments
32 REM | 03-OCT-2005   Riyengar    MRC Elimination Changes:
33 **/
34 
35 P_PA_DEBUG_MODE        varchar2(1)  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
36 g_module_name          VARCHAR2(100):= 'pa.plsql.pa_fp_org_fcst_gen_pub';
37 g_plsql_max_array_size NUMBER       := 200;
38 
39 FUNCTION budget_version_in_error
40   ( p_budget_version_id IN pa_budget_versions.budget_version_id%TYPE
41   ) RETURN NUMBER IS
42 
43 BEGIN
44 
45     UPDATE pa_budget_versions
46        SET plan_processing_code = 'E'
47      WHERE budget_version_id = p_budget_version_id;
48 
49     COMMIT;
50 
51     RETURN(1);
52 
53 EXCEPTION
54    WHEN OTHERS THEN
55           FND_MSG_PUB.add_exc_msg(
56               p_pkg_name => 'PA_FP_ORG_FCST_GEN_PUB.budget_version_in_error'
57              ,p_procedure_name => PA_DEBUG.G_Err_Stack);
58 
59               IF P_PA_DEBUG_MODE = 'Y' THEN
60                  pa_debug.write_file('budget_version_in_error: ' || SQLERRM);
61               END IF;
62               pa_debug.reset_err_stack;
63               RETURN(-1);
64               RAISE;
65 END budget_version_in_error;
66 
67 FUNCTION get_amttype_id
68   ( p_amt_typ_code     IN pa_amount_types_b.amount_type_code%TYPE
69                               := NULL
70   ) RETURN NUMBER IS
71     l_amount_type_id pa_amount_types_b.amount_type_id%TYPE;
72     l_amt_code pa_fp_org_fcst_gen_pub.char240_data_type_table;
73     l_amt_id   pa_fp_org_fcst_gen_pub.number_data_type_table;
74 
75     l_debug_mode VARCHAR2(30);
76 
77     CURSOR get_amt_det IS
78     SELECT atb.amount_type_id
79           ,atb.amount_type_code
80       FROM pa_amount_types_b atb
81      WHERE atb.amount_type_class = 'R';
82 
83     l_stage number := 0;
84 
85 BEGIN
86      IF P_PA_DEBUG_MODE = 'Y' THEN
87         pa_debug.init_err_stack('PA_FP_ORG_FCST_GEN_PUB.get_amttype_id');
88      END IF;
89 
90      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
91      l_debug_mode := NVL(l_debug_mode, 'Y');
92 
93      IF P_PA_DEBUG_MODE = 'Y' THEN
94         pa_debug.set_process('get_amttype_id: ' || 'PLSQL','LOG',l_debug_mode);
95      END IF;
96 
97        l_amount_type_id := -99;
98 
99        IF l_amt_code.last IS NULL THEN
100           OPEN get_amt_det;
101           LOOP
102               FETCH get_amt_det into l_amt_id(nvl(l_amt_id.last+1,1))
103                                     ,l_amt_code(nvl(l_amt_code.last+1,1));
104               EXIT WHEN get_amt_det%NOTFOUND;
105           END LOOP;
106        END IF;
107 
108        IF l_amt_code.last IS NOT NULL THEN
109           FOR i in l_amt_id.first..l_amt_id.last LOOP
110               IF l_amt_code(i) = p_amt_typ_code THEN
111                  l_amount_type_id := l_amt_id(i);
112               END IF;
113           END LOOP;
114        END IF;
115        IF l_amount_type_id = -99 THEN
116                  pa_debug.g_err_stage := 'p_amt_typ_code         ['||p_amt_typ_code          ||']';
117                  IF P_PA_DEBUG_MODE = 'Y' THEN
118                     pa_debug.write_file('get_amttype_id: ' || pa_debug.g_err_stage);
119                  END IF;
120        END IF;
121        pa_debug.reset_err_stack;
122        RETURN(l_amount_type_id);
123 
124 EXCEPTION
125      WHEN OTHERS THEN
126           FND_MSG_PUB.add_exc_msg(
127               p_pkg_name => 'PA_FP_ORG_FCST_GEN_PUB.get_amttype_id'
128              ,p_procedure_name => PA_DEBUG.G_Err_Stack);
129 
130               IF P_PA_DEBUG_MODE = 'Y' THEN
131                  pa_debug.write_file('get_amttype_id: ' || SQLERRM);
132               END IF;
133               pa_debug.reset_err_stack;
134               RAISE;
135 END get_amttype_id;
136 
137 PROCEDURE gen_org_fcst
138 ( errbuff                   OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
139  ,retcode                   OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
140  ,p_selection_criteria       IN VARCHAR2
141                                 := NULL
142  ,p_is_org                   IN VARCHAR2
143                                 := NULL
144  ,p_organization_id          IN hr_organization_units.organization_id%TYPE
145                                 := NULL
146  ,p_is_start_org             IN VARCHAR2
147                                 := NULL
148  ,p_starting_organization_id IN hr_organization_units.organization_id%TYPE
149                                 := NULL
150  ,p_budget_version_id        IN pa_budget_versions.budget_version_id%TYPE
151                                 := NULL)
152  IS
153 
154  /* Local Variables */
155 
156  /* Error handling local variables */
157  l_msg_count number := 0;
158  l_data VARCHAR2(2000);
159  l_msg_data VARCHAR2(2000);
160  l_err_code VARCHAR2(2000);
161  l_msg_index_out NUMBER(20);
162  l_return_status VARCHAR2(2000);
163  l_budget_version_in_error NUMBER(5) := 0;
164 
165  l_row_id ROWID;
166 
167  l_err_stage varchar2(240);
168  l_err_stack varchar2(240);
169  l_debug_mode varchar2(30) := 'Y';
170  l_bg_org varchar2(25);
171 
172  l_records_affected number(20);
173  l_fe_ctr number(20);
174  l_fe_new_seq number(20);
175  l_fl_ctr number(20);
176  l_fl_new_seq number(20);
177  l_string_length number(20);
178  l_stage number := 100;
179  l_budget_ctr number;
180  l_active_organization number;
181  l_request_id number;
182 
183  l_fcst_start_date date;
184  l_fcst_end_date date;
185  l_ppp_start_date date;
186  l_ppp_end_date date;
187 
188  l_project_action_allowed varchar2(1);
189 
190  l_proj_fp_options_id pa_proj_fp_options.proj_fp_options_id%TYPE;
191  l_time_phased_code pa_proj_fp_options.all_time_phased_code%TYPE;
192  l_fin_plan_amount_set_id pa_fin_plan_amount_sets.fin_plan_amount_set_id%TYPE;
193  l_budget_version_id pa_budget_versions.budget_version_id%TYPE;
194  l_period_profile_id pa_budget_versions.period_profile_id%TYPE;
195  l_resource_list_id pa_budget_versions.resource_list_id%TYPE;
196  l_bv_rec_ver_num pa_budget_versions.record_version_number%TYPE;
197  l_bv_version_number pa_budget_versions.version_number%TYPE;
198  l_bv_version_name pa_budget_versions.version_name%TYPE;
199  l_pfi_txn_project_id pa_budget_versions.project_id%TYPE;
200  l_fin_plan_type_id pa_budget_versions.fin_plan_type_id%TYPE;
201  l_current_working_flag pa_budget_versions.current_working_flag%TYPE;
202  l_org_fcst_period_type pa_forecasting_options_all.org_fcst_period_type%TYPE;
203  l_org_proj_template_id pa_forecasting_options_all.org_fcst_project_template_id%TYPE;
204  l_number_of_periods pa_forecasting_options_all.number_of_periods%TYPE;
205  l_weighted_or_full_code pa_forecasting_options_all.weighted_or_full_code%TYPE;
206  l_org_id pa_implementations_all.org_id%TYPE;
207  l_pfi_project_org_id pa_implementations_all.org_id%TYPE;
208  l_pfi_exp_org_id pa_implementations_all.org_id%TYPE;
209  l_pa_period_type pa_implementations_all.pa_period_type%TYPE;
210  l_period_set_name pa_implementations_all.period_set_name%TYPE;
211  l_org_structure_version_id pa_implementations_all.org_structure_version_id%TYPE;
212  l_task_organization_id hr_organization_units.organization_id%TYPE;
213  l_organization_id hr_organization_units.organization_id%TYPE;
214  l_business_group_id hr_organization_units.business_group_id%TYPE;
215  l_org_name hr_organization_units.name%TYPE;
216  l_org_location_id hr_organization_units.location_id%TYPE;
217  l_pfi_organization_id hr_organization_units.organization_id%TYPE;
218  l_pfi_project_organization_id hr_organization_units.organization_id%TYPE;
219  l_pfi_exp_organization_id hr_organization_units.organization_id%TYPE;
220  l_project_id pa_projects_all.project_id%TYPE;
221  l_project_name pa_projects_all.name%TYPE;
222  l_project_number pa_projects_all.segment1%TYPE;
223  l_new_project_number pa_projects_all.segment1%TYPE;
224  l_org_projfunc_currency_code pa_projects_all.projfunc_currency_code%TYPE;
225  l_own_task_id pa_tasks.task_id%TYPE;
226  l_prob_percent pa_probability_members.probability_percentage%TYPE;
227  l_period_profile_type pa_proj_period_profiles.period_profile_type%TYPE;
228  l_pfi_assignment_id pa_forecast_items.assignment_id%TYPE;
229  l_pfi_resource_id pa_forecast_items.resource_id%TYPE;
230  l_own_resource_assignment_id pa_resource_assignments.resource_assignment_id%TYPE;
231  l_proj_resource_assignment_id pa_resource_assignments.resource_assignment_id%TYPE;
232  l_resource_list_member_id pa_resource_assignments.resource_list_member_id%TYPE;
233  l_prv_forecast_element_id pa_org_fcst_elements.forecast_element_id%TYPE;
234  l_utl_hours pa_budget_lines.utilization_hours%TYPE;
235  l_utl_capacity pa_budget_lines.capacity%TYPE;
236  l_set_of_books_id gl_sets_of_books.set_of_books_id%TYPE;
237  l_act_period_type gl_periods.period_type%TYPE;
238 
239 
240  CURSOR fl_lines_task IS
241   SELECT
242          fl.budget_version_id                /* FPB2: budget_version_id */
243         ,l_own_resource_assignment_id
244         ,fl.period_name
245         ,fl.start_date
246         ,fl.end_date
247         ,nvl(sum(fl.quantity),0)
248         ,nvl(sum(fl.raw_cost),0)
249         ,nvl(sum(fl.burdened_cost),0)
250         ,nvl(sum(fl.revenue),0)
251         ,nvl(sum(fl.borrowed_revenue),0)
252         ,nvl(sum(fl.tp_revenue_in),0)
253         ,nvl(sum(fl.tp_revenue_out),0)
254         ,nvl(sum(fl.lent_resource_cost),0)
255         ,nvl(sum(fl.tp_cost_in),0)
256         ,nvl(sum(fl.tp_cost_out),0)
257         ,nvl(sum(fl.unassigned_time_cost),0)
258    FROM pa_org_forecast_lines fl
259   WHERE fl.budget_version_id = l_budget_version_id
260     AND fl.project_id        = l_project_id
261     AND fl.task_id           = l_own_task_id
262   GROUP BY fl.period_name
263           ,fl.start_date
264           ,fl.end_date
265           ,fl.budget_version_id; /* FPB2 */
266 
267 /* Bug 3106741 for performance improvement budget_version_id join has been added */
268 
269  CURSOR bl_lines_project IS
270   SELECT
271          bl.budget_version_id                /* FPB2: budget_version_id */
272         ,l_proj_resource_assignment_id
273         ,bl.period_name
274         ,bl.start_date
275         ,bl.end_date
276         ,nvl(sum(bl.quantity),0)
277         ,nvl(sum(bl.raw_cost),0)
278         ,nvl(sum(bl.burdened_cost),0)
279         ,nvl(sum(bl.revenue),0)
280         ,nvl(sum(bl.borrowed_revenue),0)
281         ,nvl(sum(bl.tp_revenue_in),0)
282         ,nvl(sum(bl.tp_revenue_out),0)
283         ,nvl(sum(bl.lent_resource_cost),0)
284         ,nvl(sum(bl.tp_cost_in),0)
285         ,nvl(sum(bl.tp_cost_out),0)
286         ,nvl(sum(bl.unassigned_time_cost),0)
287         ,nvl(sum(bl.utilization_percent),0)
288         ,nvl(sum(bl.utilization_hours),0)
289         ,nvl(sum(bl.capacity),0)
290         ,nvl(sum(bl.head_count),0)
291    FROM pa_budget_lines bl
292         ,pa_resource_assignments ra
293   WHERE bl.resource_assignment_id = ra.resource_assignment_id
294     AND bl.budget_version_id = ra.budget_version_id
295     AND ra.budget_version_id = l_budget_version_id
296     AND ra.task_id <> 0
297     AND bl.budget_version_id = l_budget_version_id   /* bug 3106741 */
298   GROUP BY bl.period_name
299           ,bl.start_date
300           ,bl.end_date
301           ,bl.budget_version_id; /* FPB2 */
302 
303    /* Only one can be passed at any given time -- that is if p_organization_id
304       is passed then p_starting_organization_id will be null and vice-a-versa
305       One of them is mandatory from the form, that is both cannot be passed as
306       null. p_budget_version_id will only be passed from the OA pages */
307 
308     CURSOR org_hierarchy is
309     SELECT child_organization_id
310       FROM pa_org_hierarchy_denorm
311      WHERE pa_org_use_type          = 'REPORTING'
312        and parent_organization_id   = p_starting_organization_id
313        and org_id                   = l_org_id
314        and org_hierarchy_version_id = l_org_structure_version_id
315        order by
316               parent_level  DESC
317              ,child_level   DESC
318              ,child_organization_id;
319 
320    /*
321     SELECT se.organization_id_child org_id
322       FROM per_org_structure_elements se
323      WHERE se.org_structure_version_id = l_org_structure_version_id
324    CONNECT BY PRIOR se.organization_id_child = se.organization_id_parent
325        AND se.org_structure_version_id = l_org_structure_version_id
326      START WITH se.organization_id_parent = p_starting_organization_id
327        AND se.org_structure_version_id = l_org_structure_version_id
328      UNION
329    SELECT p_starting_organization_id FROM DUAL;
330    */
331 
332     CURSOR specific_org_only is
333     SELECT hou.organization_id
334           ,houtl.name
335           ,hou.business_group_id
336           ,hou.location_id
337       FROM hr_all_organization_units hou,
338            hr_all_organization_units_tl houtl
339      WHERE hou.organization_id = p_organization_id
340        AND houtl.organization_id = hou.organization_id
341        AND houtl.language = USERENV('LANG');
342 
343     CURSOR sub_orgs IS
344     SELECT pa.child_organization_id
345       FROM pa_org_hierarchy_denorm pa
346      WHERE pa.pa_org_use_type = 'REPORTING'
347        AND   pa.parent_level-pa.child_level < 1
348        AND   nvl(pa.org_id,-99)              = l_org_id
349        AND   pa.org_hierarchy_version_id     = l_org_structure_version_id
350        AND   pa.parent_organization_id       = l_organization_id
351      ORDER BY parent_level  desc,
352               parent_organization_id,
353               child_level desc,
354               child_organization_id;
355 
356      CURSOR sub_tasks IS
357      SELECT carrying_out_organization_id,
358             task_id
359        FROM pa_tasks
360       WHERE project_id = l_project_id;
361 
362           /* Transfer Price Logic as part of Select statement:
363 
364              Org Context  TP Amount Type    CC In  CC Out Rev In Rev Out
365              -----------  ----------------  -----  ------ ------ -------
366              Provider     COST_TRANSFER     0      Y      0       0
367              Provider     REVENUE_TRANSFER  0      0      Y       0
368              Receiver     COST_TRANSFER     Y      0      0       0
369              Receiver     REVENUE_TRANSFER  0      0      0       Y
370           */
371 
372      CURSOR forecast_items IS
373      SELECT
374        l_organization_id,
375        pfi.project_organization_id,
376        pfi.project_org_id,
377        pfi.expenditure_organization_id,
378        pfi.expenditure_org_id,
379        pfi.project_id txn_project_id,
380        nvl(pfi.assignment_id,-1),
381        nvl(pfi.resource_id,-1),
382        gp.period_name,
383        gp.start_date,
384        gp.end_date,
385        nvl(sum(pfi.item_quantity),0),
386        DECODE(pfi.forecast_item_type,'U',0,
387          DECODE(pfi.expenditure_organization_id,pfi.project_organization_id,
388            DECODE(pfi.expenditure_organization_id,l_organization_id,
389              DECODE(pfi.expenditure_org_id,l_org_id,
390                NVL(SUM(pfi.expfunc_raw_cost),0),0),0),0))              raw_cost,
391        DECODE(pfi.forecast_item_type,'U',0,
392          DECODE(pfi.expenditure_organization_id, pfi.project_organization_id,
393            DECODE(pfi.expenditure_organization_id,l_organization_id,
394              DECODE(pfi.expenditure_org_id,l_org_id,
395                NVL(SUM(pfi.expfunc_burdened_cost),0),0),0),0))    burdened_cost,
396        DECODE(pfi.forecast_item_type,'U',0,
397          DECODE(pfi.expenditure_organization_id,pfi.project_organization_id,
398            DECODE(pfi.expenditure_org_id,l_org_id,0,
399              DECODE(pfi.expenditure_organization_id,l_organization_id,
400                NVL(SUM(pfi.expfunc_burdened_cost),0),0)),
401                  DECODE(pfi.expenditure_organization_id,l_organization_id,
402                    DECODE(pfi.expenditure_org_id,l_org_id,
403                      NVL(SUM(pfi.expfunc_burdened_cost),0),0),0))) lent_resource_cost,
404        DECODE(pfi.forecast_item_type,'U',
405            DECODE(pfi.expenditure_organization_id,l_organization_id,
406              DECODE(pfi.expenditure_org_id,l_org_id,
407                NVL(SUM(pfi.expfunc_burdened_cost),0),0),0),0) unassigned_time_cost,
408        DECODE(pfi.project_organization_id,l_organization_id,
409          DECODE(pfi.project_org_id,l_org_id,
410            DECODE(pfi.tp_amount_type,'COST_TRANSFER',
411              NVL(SUM(pfi.projfunc_transfer_price),0),0),0),0)        tp_cost_in,
412        DECODE(pfi.expenditure_organization_id,l_organization_id,
413          DECODE(pfi.expenditure_org_id,l_org_id,
414            DECODE(pfi.tp_amount_type,'COST_TRANSFER',
415              NVL(SUM(pfi.expfunc_transfer_price),0),0),0),0)        tp_cost_out,
416        DECODE(pfi.project_organization_id,pfi.expenditure_organization_id,
417          DECODE(pfi.project_organization_id,l_organization_id,
418            DECODE(pfi.project_org_id,l_org_id,
419              NVL(SUM(pfi.projfunc_revenue),0),0),0),0)                  revenue,
420        DECODE(pfi.project_organization_id,pfi.expenditure_organization_id,
421          DECODE(pfi.project_org_id,l_org_id,0,
422            DECODE(pfi.project_organization_id,l_organization_id,
423              NVL(SUM(pfi.projfunc_revenue),0),0)),
424                DECODE(pfi.project_organization_id,l_organization_id,
425                  DECODE(pfi.project_org_id,l_org_id,
426                    NVL(SUM(pfi.projfunc_revenue),0),0),0))     borrowed_revenue,
427        DECODE(pfi.expenditure_organization_id,l_organization_id,
428          DECODE(pfi.expenditure_org_id,l_org_id,
429            DECODE(pfi.tp_amount_type,'REVENUE_TRANSFER',
430              NVL(SUM(pfi.expfunc_transfer_price),0),0),0),0)          tp_rev_in,
431        DECODE(pfi.project_organization_id,l_organization_id,
432          DECODE(pfi.project_org_id,l_org_id,
433            DECODE(pfi.tp_amount_type,'REVENUE_TRANSFER',
434              NVL(sum(pfi.projfunc_transfer_price),0),0),0),0)        tp_rev_out
435        FROM pa_forecast_items pfi,
436             gl_date_period_map dpm,
437             gl_periods gp
438       WHERE pfi.project_organization_id = l_organization_id
439                      and nvl(pfi.project_org_id,-99) = l_org_id
440         AND pfi.forecast_item_type in ('A','R','U')
441         AND pfi.delete_flag     = 'N'
442         AND pfi.error_flag      = 'N'
443         AND pfi.item_date BETWEEN l_fcst_start_date
444                               AND l_fcst_end_date
445         AND pfi.forecast_amt_calc_flag||'' = 'Y'
446         AND dpm.period_set_name = l_period_set_name
447         AND dpm.period_type     = l_act_period_type
448         AND pfi.item_date       = dpm.accounting_date
449         AND dpm.period_set_name = gp.period_set_name
450         AND dpm.period_type     = gp.period_type
451         AND dpm.period_name     = gp.period_name
452         AND gp.adjustment_period_flag = 'N'
453         GROUP BY pfi.project_organization_id,
454                  pfi.project_org_id,
455                  pfi.expenditure_organization_id,
456                  pfi.expenditure_org_id,
457                  pfi.project_id,
458                  nvl(pfi.assignment_id,-1),
459                  nvl(pfi.resource_id,-1),
460                  pfi.tp_amount_type,
461                  pfi.forecast_item_type,
462                  gp.start_date,
463                  gp.end_date,
464                  gp.period_name
465     UNION  ALL -- bug 3106741  changed union to uinon all for performance benefit
466      SELECT
467        l_organization_id,
468        pfi.project_organization_id,
469        pfi.project_org_id,
470        pfi.expenditure_organization_id,
471        pfi.expenditure_org_id,
472        pfi.project_id txn_project_id,
473        nvl(pfi.assignment_id,-1),
474        nvl(pfi.resource_id,-1),
475        gp.period_name,
476        gp.start_date,
477        gp.end_date,
478        nvl(sum(pfi.item_quantity),0),
479        DECODE(pfi.forecast_item_type,'U',0,
480          DECODE(pfi.expenditure_organization_id,pfi.project_organization_id,
481            DECODE(pfi.expenditure_organization_id,l_organization_id,
482              DECODE(pfi.expenditure_org_id,l_org_id,
483                NVL(SUM(pfi.expfunc_raw_cost),0),0),0),0))              raw_cost,
484        DECODE(pfi.forecast_item_type,'U',0,
485          DECODE(pfi.expenditure_organization_id, pfi.project_organization_id,
486            DECODE(pfi.expenditure_organization_id,l_organization_id,
487              DECODE(pfi.expenditure_org_id,l_org_id,
488                NVL(SUM(pfi.expfunc_burdened_cost),0),0),0),0))    burdened_cost,
489        DECODE(pfi.forecast_item_type,'U',0,
490          DECODE(pfi.expenditure_organization_id,pfi.project_organization_id,
491            DECODE(pfi.expenditure_org_id,l_org_id,0,
492              DECODE(pfi.expenditure_organization_id,l_organization_id,
493                NVL(SUM(pfi.expfunc_burdened_cost),0),0)),
494                  DECODE(pfi.expenditure_organization_id,l_organization_id,
495                    DECODE(pfi.expenditure_org_id,l_org_id,
496                      NVL(SUM(pfi.expfunc_burdened_cost),0),0),0))) lent_resource_cost,
497        DECODE(pfi.forecast_item_type,'U',
498            DECODE(pfi.expenditure_organization_id,l_organization_id,
499              DECODE(pfi.expenditure_org_id,l_org_id,
500                NVL(SUM(pfi.expfunc_burdened_cost),0),0),0),0) unassigned_time_cost,
501        DECODE(pfi.project_organization_id,l_organization_id,
502          DECODE(pfi.project_org_id,l_org_id,
503            DECODE(pfi.tp_amount_type,'COST_TRANSFER',
504              NVL(SUM(pfi.projfunc_transfer_price),0),0),0),0)        tp_cost_in,
505        DECODE(pfi.expenditure_organization_id,l_organization_id,
506          DECODE(pfi.expenditure_org_id,l_org_id,
507            DECODE(pfi.tp_amount_type,'COST_TRANSFER',
508              NVL(SUM(pfi.expfunc_transfer_price),0),0),0),0)        tp_cost_out,
509        DECODE(pfi.project_organization_id,pfi.expenditure_organization_id,
510          DECODE(pfi.project_organization_id,l_organization_id,
511            DECODE(pfi.project_org_id,l_org_id,
512              NVL(SUM(pfi.projfunc_revenue),0),0),0),0)                  revenue,
513        DECODE(pfi.project_organization_id,pfi.expenditure_organization_id,
514          DECODE(pfi.project_org_id,l_org_id,0,
515            DECODE(pfi.project_organization_id,l_organization_id,
516              NVL(SUM(pfi.projfunc_revenue),0),0)),
517                DECODE(pfi.project_organization_id,l_organization_id,
518                  DECODE(pfi.project_org_id,l_org_id,
519                    NVL(SUM(pfi.projfunc_revenue),0),0),0))     borrowed_revenue,
520        DECODE(pfi.expenditure_organization_id,l_organization_id,
521          DECODE(pfi.expenditure_org_id,l_org_id,
522            DECODE(pfi.tp_amount_type,'REVENUE_TRANSFER',
523              NVL(SUM(pfi.expfunc_transfer_price),0),0),0),0)          tp_rev_in,
524        DECODE(pfi.project_organization_id,l_organization_id,
525          DECODE(pfi.project_org_id,l_org_id,
526            DECODE(pfi.tp_amount_type,'REVENUE_TRANSFER',
527              NVL(sum(pfi.projfunc_transfer_price),0),0),0),0)        tp_rev_out
528        FROM pa_forecast_items pfi,
529             gl_date_period_map dpm,
530             gl_periods gp
531       WHERE pfi.expenditure_organization_id = l_organization_id
532                      and nvl(pfi.expenditure_org_id,-99) = l_org_id
533         AND pfi.forecast_item_type in ('A','R','U')
534         AND pfi.delete_flag     = 'N'
535         AND pfi.error_flag      = 'N'
536         AND pfi.item_date BETWEEN l_fcst_start_date
537                               AND l_fcst_end_date
538         AND pfi.forecast_amt_calc_flag||'' = 'Y'
539         AND dpm.period_set_name = l_period_set_name
540         AND dpm.period_type     = l_act_period_type
541         AND pfi.item_date       = dpm.accounting_date
542         AND dpm.period_set_name = gp.period_set_name
543         AND dpm.period_type     = gp.period_type
544         AND dpm.period_name     = gp.period_name
545         AND gp.adjustment_period_flag = 'N'
546         GROUP BY pfi.project_organization_id,
547                  pfi.project_org_id,
548                  pfi.expenditure_organization_id,
549                  pfi.expenditure_org_id,
550                  pfi.project_id,
551                  nvl(pfi.assignment_id,-1),
552                  nvl(pfi.resource_id,-1),
553                  pfi.tp_amount_type,
554                  pfi.forecast_item_type,
555                  gp.start_date,
556                  gp.end_date,
557                  gp.period_name
558         ORDER BY 1,6,7,8,10,11,2,3,4,5;
559 
560  /* Record Definitions */
561     amt_rec pa_plan_matrix.amount_type_tabtyp;
562     budget_lines_rec budget_lines_record_table_type;
563 
564  /* PLSQL Table Declarations */
565 
566  /* Forecast Item PL/SQL tables */
567  l_fi_organization_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
568  l_fi_proj_organization_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
569  l_fi_proj_orgid_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
570  l_fi_exp_organization_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
571  l_fi_exp_orgid_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
572  l_fi_txn_project_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
573  l_fi_assignment_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
574  l_fi_resource_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
575 
576  l_fi_period_name_tab pa_fp_org_fcst_gen_pub.char240_data_type_table;
577  l_fi_start_date_tab pa_fp_org_fcst_gen_pub.date_data_type_table;
578  l_fi_end_date_tab pa_fp_org_fcst_gen_pub.date_data_type_table;
579  l_fi_item_quantity_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
580  l_fi_raw_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
581  l_fi_burdened_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
582  l_fi_tp_cost_in_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
583  l_fi_tp_cost_out_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
584  l_fi_revenue_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
585  l_fi_tp_rev_in_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
586  l_fi_tp_rev_out_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
587  l_fi_borrowed_revenue_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
588  l_fi_lent_resource_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
589  l_fi_unassigned_time_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
590 
591  /* Forecast Element PL/SQL Tables */
592  l_fe_forecast_element_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
593  l_fe_organization_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
594  l_fe_org_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
595  l_fe_budget_version_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
596  l_fe_project_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
597  l_fe_task_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
598  l_fe_pvdr_rcvr_code_tab pa_fp_org_fcst_gen_pub.char240_data_type_table;
599  l_fe_other_organization_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
600  l_fe_other_org_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
601  l_fe_txn_project_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
602  l_fe_assignment_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
603  l_fe_resource_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
604 
605  /* Forecast Lines PL/SQL Tables */
606  l_fl_forecast_line_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
607  l_fl_forecast_element_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
608  l_fl_budget_version_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
609  l_fl_project_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
610  l_fl_task_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
611  l_fl_period_name_tab pa_fp_org_fcst_gen_pub.char240_data_type_table;
612  l_fl_start_date_tab pa_fp_org_fcst_gen_pub.date_data_type_table;
613  l_fl_end_date_tab pa_fp_org_fcst_gen_pub.date_data_type_table;
614  l_fl_quantity_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
615  l_fl_raw_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
616  l_fl_burdened_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
617  l_fl_tp_cost_in_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
618  l_fl_tp_cost_out_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
619  l_fl_revenue_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
620  l_fl_tp_rev_in_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
621  l_fl_tp_rev_out_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
622  l_fl_borrowed_revenue_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
623  l_fl_lent_resource_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
624  l_fl_unassigned_time_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
625 
626  /* Budget Lines PL/SQL Tables */
627  l_bl_budget_version_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table; /* FPB2: budget_version_id */
628 
629  l_bl_res_asg_id_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
630  l_bl_start_date_tab pa_fp_org_fcst_gen_pub.date_data_type_table;
631  l_bl_end_date_tab pa_fp_org_fcst_gen_pub.date_data_type_table;
632  l_bl_period_name_tab pa_fp_org_fcst_gen_pub.char240_data_type_table;
633  l_bl_quantity_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
634  l_bl_raw_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
635  l_bl_burdened_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
636  l_bl_revenue_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
637  l_bl_borrowed_revenue_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
638  l_bl_tp_revenue_in_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
639  l_bl_tp_revenue_out_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
640  l_bl_lent_resource_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
641  l_bl_tp_cost_in_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
642  l_bl_tp_cost_out_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
643  l_bl_unassigned_time_cost_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
644  l_bl_utilization_percent_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
645  l_bl_utilization_hours_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
646  l_bl_capacity_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
647  l_bl_head_count_tab pa_fp_org_fcst_gen_pub.number_data_type_table;
648 
649   BEGIN
650 
651      IF P_PA_DEBUG_MODE = 'Y' THEN
652         pa_debug.init_err_stack('PA_FP_ORG_FCST_GEN_PUB.gen_org_fcst');
653      END IF;
654 
655      --fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
656      l_debug_mode := NVL(l_debug_mode, 'Y');
657 
658      IF P_PA_DEBUG_MODE = 'Y' THEN
659         pa_debug.set_process('gen_org_fcst: ' || 'PLSQL','LOG',l_debug_mode);
660      END IF;
661 
662                        l_stage := 100;
663                        -- hr_utility.trace(to_char(l_stage));
664 
665      -- get information from pa_forecast_options_all
666         pa_fp_org_fcst_utils.get_forecast_option_details
667         (  x_fcst_period_type           => l_org_fcst_period_type
668           ,x_period_set_name            => l_period_set_name
669           ,x_act_period_type            => l_act_period_type
670           ,x_org_projfunc_currency_code => l_org_projfunc_currency_code
671           ,x_number_of_periods          => l_number_of_periods
672           ,x_weighted_or_full_code      => l_weighted_or_full_code
673           ,x_org_proj_template_id       => l_org_proj_template_id
674           ,x_org_structure_version_id   => l_org_structure_version_id
675           ,x_fcst_start_date            => l_fcst_start_date
676           ,x_fcst_end_date              => l_fcst_end_date
677           ,x_org_id                     => l_org_id
678           ,x_return_status              => l_return_status
679           ,x_err_code                   => l_err_code);
680 
681            pa_debug.g_err_stage := l_stage||': Forecast Options Data: ';
682            IF P_PA_DEBUG_MODE = 'Y' THEN
683               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
684            END IF;
685            pa_debug.g_err_stage := 'l_org_fcst_period_type       ['||l_org_fcst_period_type||']';
686            IF P_PA_DEBUG_MODE = 'Y' THEN
687               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
688            END IF;
689            pa_debug.g_err_stage := 'l_period_set_name            ['||l_period_set_name||']';
690            IF P_PA_DEBUG_MODE = 'Y' THEN
691               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
692            END IF;
693            pa_debug.g_err_stage := 'l_act_period_type            ['||l_act_period_type||']';
694            IF P_PA_DEBUG_MODE = 'Y' THEN
695               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
696            END IF;
697            pa_debug.g_err_stage := 'l_org_projfunc_currency_code ['||l_org_projfunc_currency_code||']';
698            IF P_PA_DEBUG_MODE = 'Y' THEN
699               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
700            END IF;
701            pa_debug.g_err_stage := 'l_number_of_periods          ['||l_number_of_periods||']';
702            IF P_PA_DEBUG_MODE = 'Y' THEN
703               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
704            END IF;
705            pa_debug.g_err_stage := 'l_weighted_or_full_code      ['||l_weighted_or_full_code||']';
706            IF P_PA_DEBUG_MODE = 'Y' THEN
707               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
708            END IF;
709            pa_debug.g_err_stage := 'l_org_proj_template_id       ['||l_org_proj_template_id||']';
710            IF P_PA_DEBUG_MODE = 'Y' THEN
711               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
712            END IF;
713            pa_debug.g_err_stage := 'l_org_structure_version_id   ['||l_org_structure_version_id||']';
714            IF P_PA_DEBUG_MODE = 'Y' THEN
715               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
716            END IF;
717            pa_debug.g_err_stage := 'l_fcst_start_date            ['||l_fcst_start_date||']';
718            IF P_PA_DEBUG_MODE = 'Y' THEN
719               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
720            END IF;
721            pa_debug.g_err_stage := 'l_fcst_end_date              ['||l_fcst_end_date||']';
722            IF P_PA_DEBUG_MODE = 'Y' THEN
723               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
724            END IF;
725            pa_debug.g_err_stage := 'l_org_id                     ['||l_org_id||']';
726            IF P_PA_DEBUG_MODE = 'Y' THEN
727               pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
728            END IF;
729 
730           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS)
731            THEN
732            IF l_return_status = FND_API.G_RET_STS_ERROR THEN
733              pa_debug.g_err_stage := TO_CHAR(l_stage) ||
734                                      ': Error occured while Getting forecast Options Det [' ||
735                                      l_err_code|| ']';
736              IF P_PA_DEBUG_MODE = 'Y' THEN
737                 pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
738              END IF;
739              retcode := FND_API.G_RET_STS_ERROR;
740              PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
741                                   p_msg_name       => 'PA_FP_FCST_OPTION_ERR');
742            END IF;
743            RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
744           END IF;
745 
746 
747      /* Get resource list information */
748       BEGIN
749                        l_stage := 200;
750                        pa_debug.g_err_stage := l_stage||': Get resource list information';
751                        IF P_PA_DEBUG_MODE = 'Y' THEN
752                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
753                        END IF;
754                        -- hr_utility.trace(to_char(l_stage));
755 
756           l_resource_list_id := FND_PROFILE.VALUE('PA_FORECAST_RESOURCE_LIST');
757 
758           SELECT rlm.resource_list_member_id
759             INTO l_resource_list_member_id
760             FROM pa_resource_list_members rlm
761            WHERE rlm.resource_list_id = l_resource_list_id
762              AND rlm.resource_type_code = 'UNCATEGORIZED';
763       EXCEPTION
764           WHEN OTHERS THEN
765                        -- hr_utility.trace(to_char(l_stage)||'-'||SQLERRM);
766                        pa_debug.g_err_stage := TO_CHAR(l_stage)||'-'||SQLERRM;
767                        IF P_PA_DEBUG_MODE = 'Y' THEN
768                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
769                        END IF;
770                        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
771       END;
772 
773       BEGIN
774                        l_stage := 300;
775                        pa_debug.g_err_stage := l_stage||': Get Time Phased Code';
776                        IF P_PA_DEBUG_MODE = 'Y' THEN
777                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
778                        END IF;
779                        -- hr_utility.trace(to_char(l_stage));
780 
781         SELECT substr(l_org_fcst_period_type,1,1)
782           INTO l_time_phased_code
783           FROM sys.dual;
784 
785                        l_stage := 400;
786                        pa_debug.g_err_stage := l_stage||': Get Plan Type Id';
787                        IF P_PA_DEBUG_MODE = 'Y' THEN
788                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
789                        END IF;
790                        -- hr_utility.trace(to_char(l_stage));
791 
792         SELECT fin_plan_type_id
793           INTO l_fin_plan_type_id
794           FROM pa_fin_plan_types_b
795          WHERE fin_plan_type_code = 'ORG_FORECAST';
796 
797                        l_stage :=  500;
798                        pa_debug.g_err_stage := l_stage||': Get Amount Set Id';
799                        IF P_PA_DEBUG_MODE = 'Y' THEN
800                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
801                        END IF;
802                        -- hr_utility.trace(to_char(l_stage));
803 
804         SELECT fin_plan_amount_set_id
805           INTO l_fin_plan_amount_set_id
806           FROM pa_fin_plan_amount_sets
807          WHERE amount_set_type_code = 'ALL'
808            AND fin_plan_amount_set_id = 1;  /* Fix for 2744924. */
809 
810                        l_stage :=  600;
811                        pa_debug.g_err_stage := l_stage||': Derive BV Name';
812                        IF P_PA_DEBUG_MODE = 'Y' THEN
813                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
814                        END IF;
815                        -- hr_utility.trace(to_char(l_stage));
816 
817         SELECT meaning
818           INTO l_bv_version_name
819           FROM pa_lookups
820          WHERE lookup_type = 'TRANSLATION'
821            AND lookup_code = 'AUTO_GEN_PLAN_VERSION';
822 
823       EXCEPTION
824           WHEN OTHERS THEN
825                        -- hr_utility.trace(to_char(l_stage)||'-'||SQLERRM);
826                        pa_debug.g_err_stage := TO_CHAR(l_stage)||'-'||SQLERRM;
827                        IF P_PA_DEBUG_MODE = 'Y' THEN
828                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
829                        END IF;
830                        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
831       END;
832 
833       BEGIN
834        IF p_budget_version_id IS NULL THEN
835                        l_stage :=  700;
836                        pa_debug.g_err_stage := l_stage||': BudgetVersionId is NULL';
837                        IF P_PA_DEBUG_MODE = 'Y' THEN
838                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
839                        END IF;
840                        -- hr_utility.trace(to_char(l_stage));
841           IF p_starting_organization_id IS NULL THEN
842                        l_stage :=  800;
843                        pa_debug.g_err_stage := l_stage||': Start Org is NULL';
844                        IF P_PA_DEBUG_MODE = 'Y' THEN
845                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
846                        END IF;
847                        -- hr_utility.trace(to_char(l_stage));
848              OPEN specific_org_only;
849           ELSE
850                        l_stage :=  900;
851                        pa_debug.g_err_stage := l_stage||': Start Org is Not NULL';
852                        IF P_PA_DEBUG_MODE = 'Y' THEN
853                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
854                        END IF;
855                        -- hr_utility.trace(to_char(l_stage));
856              OPEN org_hierarchy;
857           END IF;
858        END IF;
859 
860       EXCEPTION
861          WHEN OTHERS THEN
862                        -- hr_utility.trace(to_char(l_stage)||'-'||SQLERRM);
863                        pa_debug.g_err_stage := TO_CHAR(l_stage)||'-'||SQLERRM;
864                        IF P_PA_DEBUG_MODE = 'Y' THEN
865                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
866                        END IF;
867                        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
868       END;
869 
870     l_budget_ctr := 0; /* This is required to let the loop run
871                           only once if budget version id is passed*/
872     LOOP
873     BEGIN
874       IF l_budget_ctr = 1 THEN
875          IF P_PA_DEBUG_MODE = 'Y' THEN
876             pa_debug.write_file('gen_org_fcst: ' || 'Exiting as Budget Version Process has failed');
877          END IF;
878          EXIT;
879       END IF;
880       savepoint org_project;
881       IF  p_budget_version_id IS NULL THEN
882                        l_stage := 1000;
883                        -- hr_utility.trace(to_char(l_stage));
884           IF p_starting_organization_id IS NULL THEN
885                        l_stage :=  1100;
886                        pa_debug.g_err_stage := l_stage||': Fetching from specific_org_only';
887                        IF P_PA_DEBUG_MODE = 'Y' THEN
888                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
889                        END IF;
890                        -- hr_utility.trace(to_char(l_stage));
891                 FETCH specific_org_only
892                  INTO l_organization_id
893                      ,l_org_name
894                      ,l_business_group_id
895                      ,l_org_location_id;
896 
897                 IF specific_org_only%NOTFOUND THEN
898                        pa_debug.g_err_stage := l_stage||': Exiting from specific_org_only FETCH';
899                        IF P_PA_DEBUG_MODE = 'Y' THEN
900                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
901                        END IF;
902                    EXIT;
903                 END IF;
904                        l_stage :=  1200;
905                        -- hr_utility.trace(to_char(l_stage));
906 
907                      IF l_org_location_id IS NULL THEN
908                        l_stage :=  1300;
909                        -- hr_utility.trace(to_char(l_stage));
910                         pa_debug.g_err_stage := l_stage||': Organization Id ['
911                                                      ||l_organization_id||
912                                                      '] has no location';
913                             IF P_PA_DEBUG_MODE = 'Y' THEN
914                                pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
915                             END IF;
916                             rollback to org_project;
917                             RAISE pa_fp_org_fcst_gen_pub.error_reloop;
918                      END IF;
919 
920                        l_stage :=  1400;
921 
922                      BEGIN
923                          SELECT 1
924                            INTO l_active_organization
925                            FROM  pa_organizations_project_v
926                           WHERE organization_id = l_organization_id
927                             AND   active_flag = 'Y'
928                             AND   TRUNC(SYSDATE) BETWEEN TRUNC(date_from)
929                                                      AND NVL(date_to, TRUNC(SYSDATE));
930                      EXCEPTION WHEN NO_DATA_FOUND THEN
931                        -- hr_utility.trace(to_char(l_stage)||'-'||SQLERRM);
932                        pa_debug.g_err_stage := l_stage||': Organization Id ['
933                                                            ||l_organization_id||
934                                                               '] is Not Active';
935                        IF P_PA_DEBUG_MODE = 'Y' THEN
936                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
937                        END IF;
938                        rollback to org_project;
939                        RAISE pa_fp_org_fcst_gen_pub.error_reloop;
940                      END;
941           ELSE
942                        l_stage :=  1500;
943                        pa_debug.g_err_stage := l_stage||': Fetching from org_hierarchy';
944                        IF P_PA_DEBUG_MODE = 'Y' THEN
945                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
946                        END IF;
947                        -- hr_utility.trace(to_char(l_stage));
948                 FETCH org_hierarchy
949                  INTO l_organization_id;
950                 IF org_hierarchy%NOTFOUND THEN
951                        pa_debug.g_err_stage := l_stage||': Exiting from org_hierarchy FETCH';
952                        IF P_PA_DEBUG_MODE = 'Y' THEN
953                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
954                        END IF;
955                    EXIT;
956                 END IF;
957                        l_stage :=  1600;
958                        -- hr_utility.trace(to_char(l_stage));
959 
960                      BEGIN
961                          SELECT 1
962                            INTO l_active_organization
963                            FROM  pa_organizations_project_v
964                           WHERE organization_id = l_organization_id
965                             AND   active_flag = 'Y'
966                             AND   TRUNC(SYSDATE) BETWEEN TRUNC(date_from)
967                                                      AND NVL(date_to, TRUNC(SYSDATE));
968                      EXCEPTION WHEN NO_DATA_FOUND THEN
969                        -- hr_utility.trace(to_char(l_stage)||'-'||SQLERRM);
970                        pa_debug.g_err_stage := l_stage||': Organization Id ['
971                                                            ||l_organization_id||
972                                                               '] is Not Active';
973                        IF P_PA_DEBUG_MODE = 'Y' THEN
974                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
975                        END IF;
976                        rollback to org_project;
977                        RAISE pa_fp_org_fcst_gen_pub.error_reloop;
978                      END;
979 
980                        l_stage :=  1700;
981                        -- hr_utility.trace(to_char(l_stage));
982 
983                 SELECT  hou.name
984                        ,hou.business_group_id
985                        ,hou.location_id
986                   INTO  l_org_name
987                        ,l_business_group_id
988                        ,l_org_location_id
989                   FROM  hr_all_organization_units hou
990                  WHERE  hou.organization_id = l_organization_id;
991 
992                      IF l_org_location_id IS NULL THEN
993                         pa_debug.g_err_stage := l_stage||': Organization Id ['
994                                                      ||l_organization_id||
995                                                      '] has no location';
996                             IF P_PA_DEBUG_MODE = 'Y' THEN
997                                pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
998                             END IF;
999                             rollback to org_project;
1000                             RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1001                      END IF;
1002           END IF;
1003 
1004           SELECT '-'||decode(substr(to_char(l_org_id),1,1),'-',
1005                  substr(to_char(l_org_id),2),to_char(l_org_id))||
1006                  '-'||to_char(l_business_group_id)
1007             INTO l_bg_org
1008             FROM sys.dual;
1009 
1010                        l_stage :=  1800;
1011                        -- hr_utility.trace(to_char(l_stage));
1012 
1013           -- Bug 7309811 - Use byte equivalent versions of substr and length functions
1014           SELECT substrb(l_org_name,1,(30 - lengthb(l_bg_org)))||l_bg_org
1015                 ,substrb(l_org_name,1,(25 - lengthb(l_bg_org)))||l_bg_org
1016             INTO l_project_name
1017                 ,l_project_number
1018             FROM DUAL;
1019 
1020                        -- hr_utility.trace(to_char(l_stage));
1021                          pa_debug.g_err_stage := l_stage||': Project Name is '||l_project_name;
1022                          IF P_PA_DEBUG_MODE = 'Y' THEN
1023                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1024                          END IF;
1025 
1026                        l_stage :=  1900;
1027                        pa_debug.g_err_stage := l_stage||': Get Org Project Info';
1028                        IF P_PA_DEBUG_MODE = 'Y' THEN
1029                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1030                        END IF;
1031           /* Get Project information if the project exists else create the
1032              project by copying the template as per forecasting options
1033              and then get the project info */
1034 
1035           pa_fp_org_fcst_utils.get_org_project_info
1036           ( p_organization_id            => l_organization_id
1037            ,x_org_project_id             => l_project_id
1038            ,x_return_status              => l_return_status
1039            ,x_err_code                   => l_err_code);
1040 
1041           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS)
1042            THEN
1043              pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1044                                      ': Error occured while Getting Project Info [' ||
1045                                      l_err_code|| ']';
1046              IF P_PA_DEBUG_MODE = 'Y' THEN
1047                 pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1048              END IF;
1049              pa_debug.g_err_stage := l_stage||': Organization Id = '||TO_CHAR(l_organization_id);
1050              IF P_PA_DEBUG_MODE = 'Y' THEN
1051                 pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1052              END IF;
1053              rollback to org_project;
1054              RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1055           END IF;
1056                        l_stage :=  2000;
1057                        -- hr_utility.trace(to_char(l_stage));
1058                          pa_debug.g_err_stage := l_stage||': Org Project ID is ['||l_project_id||']';
1059                          IF P_PA_DEBUG_MODE = 'Y' THEN
1060                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1061                          END IF;
1062 
1063           IF l_project_id > 0 THEN
1064 
1065                        l_stage :=  2100;
1066                        -- hr_utility.trace(to_char(l_stage));
1067                        pa_debug.g_err_stage := l_stage||': Get Org Task Info';
1068                        IF P_PA_DEBUG_MODE = 'Y' THEN
1069                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1070                        END IF;
1071 
1072              pa_fp_org_fcst_utils.get_org_task_info
1073              ( p_project_id      => l_project_id
1074               ,x_organization_id => l_task_organization_id
1075               ,x_org_task_id     => l_own_task_id
1076               ,x_return_status   => l_return_status
1077               ,x_err_code        => l_err_code);
1078 
1079               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1080                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1081                                          ': Error getting Own Task';
1082                  IF P_PA_DEBUG_MODE = 'Y' THEN
1083                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1084                  END IF;
1085                  /*
1086                  pa_debug.g_err_stage :=
1087                                 'l_project_id            ['||l_project_id             ||
1088                               '] l_organization_id       ['||l_organization_id        ||
1089                               '] l_org_id                ['||l_org_id                 ||
1090                               ']';
1091                  IF P_PA_DEBUG_MODE = 'Y' THEN
1092                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1093                  END IF;
1094                  */
1095                  rollback to org_project;
1096                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1097               END IF;
1098                        l_stage :=  2200;
1099              -- hr_utility.trace('2200: Own Task Id = '||to_char(l_own_task_id));
1100                        pa_debug.g_err_stage := l_stage||': Own Task Id = '||l_own_task_id;
1101                        IF P_PA_DEBUG_MODE = 'Y' THEN
1102                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1103                        END IF;
1104 
1105              IF l_own_task_id < 0 THEN
1106                        l_stage :=  2300;
1107                        -- hr_utility.trace(to_char(l_stage));
1108                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1109                                          ': Own Task Not Found';
1110                  IF P_PA_DEBUG_MODE = 'Y' THEN
1111                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1112                  END IF;
1113                  /*
1114                  pa_debug.g_err_stage :=
1115                                 'l_project_id            ['||l_project_id             ||
1116                               '] l_organization_id       ['||l_organization_id        ||
1117                               '] l_org_id                ['||l_org_id                 ||
1118                               ']';
1119                  IF P_PA_DEBUG_MODE = 'Y' THEN
1120                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1121                  END IF;
1122                  */
1123                  PA_UTILS.ADD_MESSAGE( p_app_short_name       => 'PA'
1124                                       ,p_msg_name             => 'PA_FP_OWN_TASK_NOT_FOUND');
1125                  rollback to org_project;
1126                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1127              END IF;
1128 
1129              IF l_task_organization_id <> l_organization_id THEN
1130                        l_stage :=  2400;
1131                        -- hr_utility.trace(to_char(l_stage));
1132                 UPDATE pa_tasks
1133                    SET carrying_out_organization_id = l_organization_id
1134                  WHERE project_id = l_project_id
1135                    AND task_id    = l_own_task_id;
1136              END IF;
1137           END IF;
1138 
1139           IF l_project_id < 0 THEN /* Organization Project not found */
1140                        l_stage :=  2500;
1141                        -- hr_utility.trace(to_char(l_stage));
1142                          pa_debug.g_err_stage := l_stage||': Create New Org Project for '||TO_CHAR(l_organization_id);
1143                          IF P_PA_DEBUG_MODE = 'Y' THEN
1144                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1145                          END IF;
1146 
1147              l_err_code := NULL;
1148              l_err_stage := NULL;
1149              l_err_stack := NULL;
1150 
1151              pa_project_core1.copy_project
1152              ( x_orig_project_id         => l_org_proj_template_id
1153               , x_project_name           => l_project_name
1154               , x_project_number         => l_project_number
1155               , x_description            => NULL
1156               , x_project_type           => NULL
1157               , x_project_status_code    => NULL
1158               , x_distribution_rule      => NULL
1159               , x_public_sector_flag     => 'N'
1160               , x_organization_id        => l_organization_id
1161               , x_start_date             => TRUNC(sysdate)
1162               , x_completion_date        => NULL
1163               , x_probability_member_id  => NULL
1164               , x_project_value          => NULL
1165               , x_expected_approval_date => NULL
1166               , x_agreement_currency     => NULL
1167               , x_agreement_amount       => NULL
1168               , x_agreement_org_id       => NULL
1169               , x_copy_task_flag         => 'Y'
1170               , x_copy_budget_flag       => 'N'
1171               , x_use_override_flag      => 'N'
1172               , x_copy_assignment_flag   => 'N'
1173               , x_template_flag          => 'N'
1174               , x_project_id             => l_project_id
1175               , x_err_code               => l_err_code
1176               , x_err_stage              => l_err_stage
1177               , x_err_stack              => l_err_stack
1178               , x_new_project_number     => l_new_project_number
1179               , x_team_template_id       => NULL
1180               , x_country_code           => NULL
1181               , x_region                 => NULL
1182               , x_city                   => NULL
1183               , x_opp_value_currency_code => NULL
1184               , x_org_project_copy_flag  => 'Y');
1185 
1186               IF l_err_code  > 0 THEN
1187                  pa_debug.g_err_stage := l_stage||': Error Creating Project [' || l_err_code || ']';
1188                  IF P_PA_DEBUG_MODE = 'Y' THEN
1189                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1190                  END IF;
1191                  pa_debug.g_err_stage := l_stage||': [' || l_err_stage|| ']';
1192                  IF P_PA_DEBUG_MODE = 'Y' THEN
1193                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1194                  END IF;
1195                  pa_debug.g_err_stage := l_stage||': [' || l_err_stack|| ']';
1196                  IF P_PA_DEBUG_MODE = 'Y' THEN
1197                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1198                  END IF;
1199                  pa_debug.g_err_stage := l_stage||': l_org_proj_template_id  ['||l_org_proj_template_id   || ']';
1200                  IF P_PA_DEBUG_MODE = 'Y' THEN
1201                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1202                  END IF;
1203                  pa_debug.g_err_stage := l_stage||': l_project_name          ['||l_project_name           || ']';
1204                  IF P_PA_DEBUG_MODE = 'Y' THEN
1205                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1206                  END IF;
1207                  pa_debug.g_err_stage := l_stage||': l_project_number        ['||l_project_number         || ']';
1208                  IF P_PA_DEBUG_MODE = 'Y' THEN
1209                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1210                  END IF;
1211                  pa_debug.g_err_stage := l_stage||': l_organization_id       ['||l_organization_id        || ']';
1212                  IF P_PA_DEBUG_MODE = 'Y' THEN
1213                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1214                  END IF;
1215                  pa_debug.g_err_stage := l_stage||': l_org_id                ['||l_org_id                 || ']';
1216                  IF P_PA_DEBUG_MODE = 'Y' THEN
1217                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1218                  END IF;
1219                  rollback to org_project;
1220                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1221               END IF;
1222 
1223                        l_stage :=  2600;
1224                        -- hr_utility.trace(to_char(l_stage));
1225                        pa_debug.g_err_stage := l_stage||': Get Org Task Info';
1226                        IF P_PA_DEBUG_MODE = 'Y' THEN
1227                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1228                        END IF;
1229              pa_fp_org_fcst_utils.get_org_task_info
1230              ( p_project_id      => l_project_id
1231               ,x_organization_id => l_task_organization_id
1232               ,x_org_task_id     => l_own_task_id
1233               ,x_return_status   => l_return_status
1234               ,x_err_code        => l_err_code);
1235 
1236              -- hr_utility.trace('Own Task Id = '||to_char(l_own_task_id));
1237 
1238               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1239                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1240                                          ': Error getting Own Task';
1241                  IF P_PA_DEBUG_MODE = 'Y' THEN
1242                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1243                  END IF;
1244                  pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1245                  IF P_PA_DEBUG_MODE = 'Y' THEN
1246                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1247                  END IF;
1248                  pa_debug.g_err_stage := 'l_organization_id       ['||l_organization_id        || ']';
1249                  IF P_PA_DEBUG_MODE = 'Y' THEN
1250                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1251                  END IF;
1252                  pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1253                  IF P_PA_DEBUG_MODE = 'Y' THEN
1254                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1255                  END IF;
1256                  rollback to org_project;
1257                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1258               END IF;
1259 
1260              IF l_own_task_id < 0 THEN
1261                        l_stage :=  2700;
1262                        -- hr_utility.trace(to_char(l_stage));
1263                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Own Task Not Found';
1264                  IF P_PA_DEBUG_MODE = 'Y' THEN
1265                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1266                  END IF;
1267                  pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1268                  IF P_PA_DEBUG_MODE = 'Y' THEN
1269                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1270                  END IF;
1271                  pa_debug.g_err_stage := 'l_organization_id       ['||l_organization_id        || ']';
1272                  IF P_PA_DEBUG_MODE = 'Y' THEN
1273                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1274                  END IF;
1275                  pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1276                  IF P_PA_DEBUG_MODE = 'Y' THEN
1277                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1278                  END IF;
1279                  PA_UTILS.ADD_MESSAGE( p_app_short_name       => 'PA'
1280                                       ,p_msg_name             => 'PA_FP_OWN_TASK_NOT_FOUND');
1281                  rollback to org_project;
1282                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1283              END IF;
1284 
1285              IF l_task_organization_id <> l_organization_id THEN
1286                        l_stage :=  2800;
1287                        -- hr_utility.trace(to_char(l_stage));
1288                 UPDATE pa_tasks
1289                    SET carrying_out_organization_id = l_organization_id
1290                  WHERE project_id = l_project_id
1291                    AND task_id    = l_own_task_id;
1292              END IF;
1293 
1294                        l_stage :=  2900;
1295                        -- hr_utility.trace(to_char(l_stage));
1296                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Create Proj Fin Plan Options at Project Level';
1297                  IF P_PA_DEBUG_MODE = 'Y' THEN
1298                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1299                  END IF;
1300 
1301               -- Create Proj Fin Plan Options at Project Level
1302                 l_proj_fp_options_id := NULL;
1303               pa_proj_fp_options_pkg.Insert_Row
1304               ( px_proj_fp_options_id         => l_proj_fp_options_id
1305                ,p_project_id                  => l_project_id
1306                ,p_fin_plan_option_level_code  => 'PROJECT'
1307                ,p_fin_plan_type_id            => NULL
1308                ,p_fin_plan_start_date         => NULL
1309                ,p_fin_plan_end_date           => NULL
1310                ,p_fin_plan_preference_code    => 'COST_AND_REV_SAME'
1311                ,p_cost_amount_set_id          => NULL
1312                ,p_revenue_amount_set_id       => NULL
1313                ,p_all_amount_set_id           => 1
1314                ,p_cost_fin_plan_level_code    => NULL
1315                ,p_cost_time_phased_code       => NULL
1316                ,p_cost_resource_list_id       => NULL
1317                ,p_revenue_fin_plan_level_code => NULL
1318                ,p_revenue_time_phased_code    => NULL
1319                ,p_revenue_resource_list_id    => NULL
1320                ,p_all_fin_plan_level_code     => 'L'
1321                ,p_all_time_phased_code        => l_time_phased_code
1322                ,p_all_resource_list_id        => l_resource_list_id
1323                ,p_report_labor_hrs_from_code  => 'COST'
1324                ,p_fin_plan_version_id         => NULL
1325                ,x_row_id                      => l_row_id
1326                ,x_return_status               => l_return_status);
1327 
1328                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1329                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1330                                           ': Error creating Proj FP Options';
1331                   IF P_PA_DEBUG_MODE = 'Y' THEN
1332                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1333                   END IF;
1334                   pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1335                   IF P_PA_DEBUG_MODE = 'Y' THEN
1336                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1337                   END IF;
1338                   pa_debug.g_err_stage := 'l_fin_plan_type_id      ['||l_fin_plan_type_id       || ']';
1339                   IF P_PA_DEBUG_MODE = 'Y' THEN
1340                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1341                   END IF;
1342                   pa_debug.g_err_stage := 'option_level_code       ['||'PROJECT'                || ']';
1343                   IF P_PA_DEBUG_MODE = 'Y' THEN
1344                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1345                   END IF;
1346                   pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1347                   IF P_PA_DEBUG_MODE = 'Y' THEN
1348                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1349                   END IF;
1350                   rollback to org_project;
1351                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1352                END IF;
1353 
1354                        l_stage :=  3000;
1355                        -- hr_utility.trace(to_char(l_stage));
1356                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Create Proj FP Options at Plan Type Level';
1357                  IF P_PA_DEBUG_MODE = 'Y' THEN
1358                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1359                  END IF;
1360 
1361               -- Create Proj Fin Plan Options at Plan Type Level
1362               l_proj_fp_options_id := NULL;
1363               pa_proj_fp_options_pkg.Insert_Row
1364               ( px_proj_fp_options_id         => l_proj_fp_options_id
1365                ,p_project_id                  => l_project_id
1366                ,p_fin_plan_option_level_code  => 'PLAN_TYPE'
1367                ,p_fin_plan_type_id            => l_fin_plan_type_id
1368                ,p_fin_plan_start_date         => NULL
1369                ,p_fin_plan_end_date           => NULL
1370                ,p_fin_plan_preference_code    => 'COST_AND_REV_SAME'
1371                ,p_cost_amount_set_id          => NULL
1372                ,p_revenue_amount_set_id       => NULL
1373                ,p_all_amount_set_id           => 1
1374                ,p_cost_fin_plan_level_code    => NULL
1375                ,p_cost_time_phased_code       => NULL
1376                ,p_cost_resource_list_id       => NULL
1377                ,p_revenue_fin_plan_level_code => NULL
1378                ,p_revenue_time_phased_code    => NULL
1379                ,p_revenue_resource_list_id    => NULL
1380                ,p_all_fin_plan_level_code     => 'L'
1381                ,p_all_time_phased_code        => l_time_phased_code
1382                ,p_all_resource_list_id        => l_resource_list_id
1383                ,p_report_labor_hrs_from_code  => 'COST'
1384                ,p_fin_plan_version_id         => NULL
1385                ,x_row_id                      => l_row_id
1386                ,x_return_status               => l_return_status);
1387 
1388                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1389                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1390                                           ': Error creating Plan FP Options';
1391                   IF P_PA_DEBUG_MODE = 'Y' THEN
1392                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1393                   END IF;
1394                   pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1395                   IF P_PA_DEBUG_MODE = 'Y' THEN
1396                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1397                   END IF;
1398                   pa_debug.g_err_stage := 'l_fin_plan_type_id      ['||l_fin_plan_type_id       || ']';
1399                   IF P_PA_DEBUG_MODE = 'Y' THEN
1400                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1401                   END IF;
1402                   pa_debug.g_err_stage := 'option_level_code       ['||'PROJECT'                || ']';
1403                   IF P_PA_DEBUG_MODE = 'Y' THEN
1404                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1405                   END IF;
1406                   pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1407                   IF P_PA_DEBUG_MODE = 'Y' THEN
1408                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1409                   END IF;
1410                   rollback to org_project;
1411                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1412                END IF;
1413 
1414                        l_stage :=  3100;
1415                        -- hr_utility.trace(to_char(l_stage));
1416 
1417            END IF; /* End of Organization Project Creation */
1418 
1419                        l_stage :=  3200;
1420                        -- hr_utility.trace(to_char(l_stage));
1421                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Check Existence of Period Profile for the following data: ';
1422                        IF P_PA_DEBUG_MODE = 'Y' THEN
1423                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1424                        END IF;
1425 
1426 
1427            -- Get Period Profile Information if exists
1428             pa_debug.g_err_stage := l_stage||': l_project_id           ['||l_project_id          ||']';
1429                          IF P_PA_DEBUG_MODE = 'Y' THEN
1430                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1431                          END IF;
1432             pa_debug.g_err_stage := l_stage||': period_profile_type    ['||'FINANCIAL_PLANNING'  ||']';
1433                          IF P_PA_DEBUG_MODE = 'Y' THEN
1434                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1435                          END IF;
1436             pa_debug.g_err_stage := l_stage||': l_org_fcst_period_type ['||l_org_fcst_period_type||']';
1437                          IF P_PA_DEBUG_MODE = 'Y' THEN
1438                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1439                          END IF;
1440             pa_debug.g_err_stage := l_stage||': l_period_set_name      ['||l_period_set_name     ||']';
1441                          IF P_PA_DEBUG_MODE = 'Y' THEN
1442                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1443                          END IF;
1444             pa_debug.g_err_stage := l_stage||': l_act_period_type      ['||l_act_period_type     ||']';
1445                          IF P_PA_DEBUG_MODE = 'Y' THEN
1446                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1447                          END IF;
1448             pa_debug.g_err_stage := l_stage||': l_fcst_start_date      ['||l_fcst_start_date     ||']';
1449                          IF P_PA_DEBUG_MODE = 'Y' THEN
1450                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1451                          END IF;
1452             pa_debug.g_err_stage := l_stage||': l_number_of_periods    ['||l_number_of_periods   ||']';
1453                          IF P_PA_DEBUG_MODE = 'Y' THEN
1454                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1455                          END IF;
1456 
1457 
1458            pa_fp_org_fcst_utils.get_period_profile
1459            ( p_project_id          => l_project_id
1460             ,p_period_profile_type => 'FINANCIAL_PLANNING'
1461             ,p_plan_period_type    => l_org_fcst_period_type
1462             ,p_period_set_name     => l_period_set_name
1463             ,p_act_period_type     => l_act_period_type
1464             ,p_start_date          => l_fcst_start_date
1465             ,p_number_of_periods   => l_number_of_periods
1466             ,x_period_profile_id   => l_period_profile_id
1467             ,x_return_status       => l_return_status
1468             ,x_err_code            => l_err_code);
1469 
1470                IF l_return_status =  FND_API.G_RET_STS_UNEXP_ERROR THEN
1471                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1472                                           ': Error in get_period_profile ';
1473                   IF P_PA_DEBUG_MODE = 'Y' THEN
1474                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1475                   END IF;
1476                   rollback to org_project;
1477                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1478                END IF;
1479 
1480                        l_stage :=  3300;
1481                        -- hr_utility.trace(to_char(l_stage));
1482 
1483            IF l_period_profile_id < 0 THEN
1484                        l_period_profile_id := NULL;
1485                        l_stage :=  3400;
1486                        -- hr_utility.trace(to_char(l_stage));
1487                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Creating Period Profile';
1488                        IF P_PA_DEBUG_MODE = 'Y' THEN
1489                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1490                        END IF;
1491 
1492               IF l_org_fcst_period_type = 'PA' THEN
1493                  /* Denorm pkg requires this */
1494                  -- hr_utility.trace(to_char(l_stage));
1495                  l_pa_period_type := l_act_period_type;
1496               END IF;
1497 
1498               -- Create Period Profile
1499               Pa_Prj_Period_Profile_Utils.maintain_prj_period_profile
1500               ( p_project_id          => l_project_id
1501                ,p_period_profile_type => 'FINANCIAL_PLANNING'
1502                ,p_plan_period_type    => l_org_fcst_period_type
1503                ,p_period_set_name     => l_period_set_name
1504                ,p_gl_period_type      => l_act_period_type
1505                ,p_pa_period_type      => l_pa_period_type
1506                ,p_start_date          => l_fcst_start_date
1507                ,px_end_date           => l_fcst_end_date
1508                ,px_period_profile_id  => l_period_profile_id
1509                ,p_commit_flag         => 'N'
1510                ,px_number_of_periods  => l_number_of_periods
1511                ,x_plan_start_date     => l_ppp_start_date
1512                ,x_plan_end_date       => l_ppp_end_date
1513                ,x_return_status       => l_return_status
1514                ,x_msg_count           => l_msg_count
1515                ,x_msg_data            => errbuff   );
1516 
1517                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1518                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1519                                           ': Error creating period profile';
1520                   IF P_PA_DEBUG_MODE = 'Y' THEN
1521                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1522                   END IF;
1523                   rollback to org_project;
1524                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1525                END IF;
1526            END IF;
1527 
1528                        l_stage :=  3500;
1529                        -- hr_utility.trace(to_char(l_stage));
1530                          pa_debug.g_err_stage := l_stage||': Period Profile Id ['||l_period_profile_id||']';
1531                          IF P_PA_DEBUG_MODE = 'Y' THEN
1532                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1533                          END IF;
1534 
1535            SELECT nvl(max(bv.version_number),0) + 1,
1536                   DECODE(nvl(max(bv.version_number),0) + 1,1,'Y','N')
1537              INTO l_bv_version_number
1538                  ,l_current_working_flag
1539              FROM pa_budget_versions bv
1540             WHERE bv.project_id = l_project_id
1541               AND bv.fin_plan_type_id = l_fin_plan_type_id
1542               AND bv.budget_status_code in ('W','S');
1543 
1544                        l_stage :=  3600;
1545                        -- hr_utility.trace(to_char(l_stage));
1546                          pa_debug.g_err_stage := l_stage||': BV Number    ['||l_bv_version_number   ||']';
1547                          IF P_PA_DEBUG_MODE = 'Y' THEN
1548                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1549                          END IF;
1550                          pa_debug.g_err_stage := l_stage||': Current Working Flag ['||l_current_working_flag||']';
1551                          IF P_PA_DEBUG_MODE = 'Y' THEN
1552                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1553                          END IF;
1554 
1555                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Create Budget Version';
1556                        IF P_PA_DEBUG_MODE = 'Y' THEN
1557                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1558                        END IF;
1559 
1560            -- Create Budget Version
1561                 l_budget_version_id := NULL;
1562               pa_fp_budget_versions_pkg.Insert_Row
1563               ( px_budget_version_id           => l_budget_version_id
1564                ,p_project_id                   => l_project_id
1565                ,p_budget_type_code             => NULL
1566                ,p_version_number               => l_bv_version_number
1567                ,p_budget_status_code           => 'W'
1568                ,p_current_flag                 => 'N'
1569                ,p_original_flag                => 'N'
1570                ,p_current_original_flag        => 'N'
1571                ,p_resource_accumulated_flag    => 'N'
1572                ,p_resource_list_id             => l_resource_list_id
1573                ,p_version_name                 => l_bv_version_name
1574                ,p_plan_run_date                => sysdate
1575                ,p_plan_processing_code         => 'S'
1576                ,p_period_profile_id            => l_period_profile_id
1577                ,p_fin_plan_type_id             => l_fin_plan_type_id
1578                ,p_current_working_flag         => l_current_working_flag
1579                ,p_version_type                 => 'ORG_FORECAST'
1580                ,x_row_id                       => l_row_id
1581                ,x_return_status                => l_return_status);
1582 
1583                        l_stage :=  3700;
1584                        -- hr_utility.trace(to_char(l_stage));
1585                          pa_debug.g_err_stage := l_stage||': Budget Version Id    ['||l_budget_version_id   ||']';
1586                          IF P_PA_DEBUG_MODE = 'Y' THEN
1587                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1588                          END IF;
1589                          pa_debug.g_err_stage := l_stage||': Resource List Id ['||l_resource_list_id||']';
1590                          IF P_PA_DEBUG_MODE = 'Y' THEN
1591                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1592                          END IF;
1593 
1594                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1595                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1596                                           ': Error creating Budget version';
1597                   IF P_PA_DEBUG_MODE = 'Y' THEN
1598                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1599                   END IF;
1600                   pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1601                   IF P_PA_DEBUG_MODE = 'Y' THEN
1602                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1603                   END IF;
1604                   pa_debug.g_err_stage := 'l_bv_version_number     ['||l_bv_version_number      || ']';
1605                   IF P_PA_DEBUG_MODE = 'Y' THEN
1606                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1607                   END IF;
1608                   pa_debug.g_err_stage := 'l_resource_list_id      ['||l_resource_list_id       || ']';
1609                   IF P_PA_DEBUG_MODE = 'Y' THEN
1610                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1611                   END IF;
1612                   pa_debug.g_err_stage := 'l_period_profile_id     ['||l_period_profile_id      || ']';
1613                   IF P_PA_DEBUG_MODE = 'Y' THEN
1614                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1615                   END IF;
1616                   pa_debug.g_err_stage := 'l_fin_plan_type_id      ['||l_fin_plan_type_id       || ']';
1617                   IF P_PA_DEBUG_MODE = 'Y' THEN
1618                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1619                   END IF;
1620                   pa_debug.g_err_stage := 'l_current_working_flag  ['||l_current_working_flag   || ']';
1621                   IF P_PA_DEBUG_MODE = 'Y' THEN
1622                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1623                   END IF;
1624                   pa_debug.g_err_stage := 'version_type            ['||'ORG_FORECAST'           || ']';
1625                   IF P_PA_DEBUG_MODE = 'Y' THEN
1626                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1627                   END IF;
1628                   rollback to org_project;
1629                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1630                END IF;
1631 
1632                        l_stage :=  3750;
1633                        -- hr_utility.trace(to_char(l_stage));
1634                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Create Proj Fin Plan Options at Plan Version Level';
1635                  IF P_PA_DEBUG_MODE = 'Y' THEN
1636                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1637                  END IF;
1638 
1639               -- Create Proj Fin Plan Options at Plan Version Level
1640                 l_proj_fp_options_id := NULL;
1641               pa_proj_fp_options_pkg.Insert_Row
1642               ( px_proj_fp_options_id         => l_proj_fp_options_id
1643                ,p_project_id                  => l_project_id
1644                ,p_fin_plan_option_level_code  => 'PLAN_VERSION'
1645                ,p_fin_plan_type_id            => l_fin_plan_type_id
1646                ,p_fin_plan_start_date         => l_fcst_start_date
1647                ,p_fin_plan_end_date           => l_fcst_end_date
1648                ,p_fin_plan_preference_code    => 'COST_AND_REV_SAME'
1649                ,p_cost_amount_set_id          => NULL
1650                ,p_revenue_amount_set_id       => NULL
1651                ,p_all_amount_set_id           => 1
1652                ,p_cost_fin_plan_level_code    => NULL
1653                ,p_cost_time_phased_code       => NULL
1654                ,p_cost_resource_list_id       => NULL
1655                ,p_revenue_fin_plan_level_code => NULL
1656                ,p_revenue_time_phased_code    => NULL
1657                ,p_revenue_resource_list_id    => NULL
1658                ,p_all_fin_plan_level_code     => 'L'
1659                ,p_all_time_phased_code        => l_time_phased_code
1660                ,p_all_resource_list_id        => l_resource_list_id
1661                ,p_report_labor_hrs_from_code  => 'COST'
1662                ,p_fin_plan_version_id         => l_budget_version_id
1663                ,x_row_id                      => l_row_id
1664                ,x_return_status               => l_return_status);
1665 
1666                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1667                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1668                                           ': Error creating Budget Version FP Options';
1669                   IF P_PA_DEBUG_MODE = 'Y' THEN
1670                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1671                   END IF;
1672                   pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1673                   IF P_PA_DEBUG_MODE = 'Y' THEN
1674                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1675                   END IF;
1676                   pa_debug.g_err_stage := 'l_fin_plan_type_id      ['||l_fin_plan_type_id       || ']';
1677                   IF P_PA_DEBUG_MODE = 'Y' THEN
1678                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1679                   END IF;
1680                   pa_debug.g_err_stage := 'option_level_code       ['||'PROJECT'                || ']';
1681                   IF P_PA_DEBUG_MODE = 'Y' THEN
1682                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1683                   END IF;
1684                   pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1685                   IF P_PA_DEBUG_MODE = 'Y' THEN
1686                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1687                   END IF;
1688                   rollback to org_project;
1689                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1690                END IF;
1691      ELSE  /* Budget version passed */
1692            l_budget_ctr := 1;
1693                        l_stage :=  3800;
1694                        -- hr_utility.trace(to_char(l_stage));
1695                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Get BudgetVersion Details';
1696                        IF P_PA_DEBUG_MODE = 'Y' THEN
1697                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1698                        END IF;
1699 
1700            SELECT bv.budget_version_id,
1701                   bv.project_id,
1702                   bv.record_version_number,
1703                   bv.period_profile_id,
1704                   pa.carrying_out_organization_id,
1705                   pa.projfunc_currency_code
1706              INTO l_budget_version_id,
1707                   l_project_id,
1708                   l_bv_rec_ver_num,
1709                   l_period_profile_id,
1710                   l_organization_id,
1711                   l_org_projfunc_currency_code
1712              FROM pa_budget_versions bv,
1713                   pa_projects pa
1714             WHERE bv.budget_version_id = p_budget_version_id
1715               AND pa.project_id = bv.project_id;
1716 
1717                        l_stage :=  3900;
1718                        -- hr_utility.trace(to_char(l_stage));
1719 
1720           IF l_project_id > 0 THEN
1721 
1722                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Get Org Task Info';
1723                        IF P_PA_DEBUG_MODE = 'Y' THEN
1724                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1725                        END IF;
1726 
1727              pa_fp_org_fcst_utils.get_org_task_info
1728              ( p_project_id      => l_project_id
1729               ,x_organization_id => l_task_organization_id
1730               ,x_org_task_id     => l_own_task_id
1731               ,x_return_status   => l_return_status
1732               ,x_err_code        => l_err_code);
1733 
1734              -- hr_utility.trace('Own Task Id = '||to_char(l_own_task_id));
1735 
1736               IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1737                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1738                                          ': Error getting Own Task';
1739                  IF P_PA_DEBUG_MODE = 'Y' THEN
1740                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1741                  END IF;
1742                  pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1743                  IF P_PA_DEBUG_MODE = 'Y' THEN
1744                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1745                  END IF;
1746                  pa_debug.g_err_stage := 'l_organization_id       ['||l_organization_id        || ']';
1747                  IF P_PA_DEBUG_MODE = 'Y' THEN
1748                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1749                  END IF;
1750                  pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1751                  IF P_PA_DEBUG_MODE = 'Y' THEN
1752                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1753                  END IF;
1754                  rollback to org_project;
1755                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1756               END IF;
1757 
1758              IF l_own_task_id < 0 THEN
1759                        l_stage :=  4000;
1760                        -- hr_utility.trace(to_char(l_stage));
1761                  pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1762                                          ': Own Task Not Found';
1763                  IF P_PA_DEBUG_MODE = 'Y' THEN
1764                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1765                  END IF;
1766                  pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
1767                  IF P_PA_DEBUG_MODE = 'Y' THEN
1768                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1769                  END IF;
1770                  pa_debug.g_err_stage := 'l_organization_id       ['||l_organization_id        || ']';
1771                  IF P_PA_DEBUG_MODE = 'Y' THEN
1772                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1773                  END IF;
1774                  pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
1775                  IF P_PA_DEBUG_MODE = 'Y' THEN
1776                     pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1777                  END IF;
1778                  PA_UTILS.ADD_MESSAGE( p_app_short_name       => 'PA'
1779                                       ,p_msg_name             => 'PA_FP_OWN_TASK_NOT_FOUND');
1780                  rollback to org_project;
1781                  RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1782              END IF;
1783 
1784              IF l_task_organization_id <> l_organization_id THEN
1785                        l_stage :=  4100;
1786                        -- hr_utility.trace(to_char(l_stage));
1787                 UPDATE pa_tasks
1788                    SET carrying_out_organization_id = l_organization_id
1789                  WHERE project_id = l_project_id
1790                    AND task_id    = l_own_task_id;
1791              END IF;
1792           END IF;
1793 
1794                        l_stage :=  4200;
1795                        -- hr_utility.trace(to_char(l_stage));
1796                      pa_debug.g_err_stage := l_stage||': Org Projfunc Currency Code ['||l_org_projfunc_currency_code||']';
1797                      IF P_PA_DEBUG_MODE = 'Y' THEN
1798                         pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1799                      END IF;
1800 
1801            pa_prj_period_profile_utils.get_prj_period_profile_dtls
1802            ( p_period_profile_id   => l_period_profile_id
1803             ,x_period_profile_type => l_period_profile_type
1804             ,x_plan_period_type    => l_org_fcst_period_type
1805             ,x_period_set_name     => l_period_set_name
1806             ,x_gl_period_type      => l_act_period_type
1807             ,x_plan_start_date     => l_fcst_start_date
1808             ,x_plan_end_date       => l_fcst_end_date
1809             ,x_number_of_periods   => l_number_of_periods
1810             ,x_return_status       => l_return_status
1811           --,x_msg_count           => x_msg_count
1812             ,x_msg_data            => errbuff   );
1813 
1814                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1815                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
1816                                           ': Error getting period profile info';
1817                   IF P_PA_DEBUG_MODE = 'Y' THEN
1818                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1819                   END IF;
1820                   pa_debug.g_err_stage :=
1821                                  'l_period_profile_id     ['||l_period_profile_id      || ']';
1822                   IF P_PA_DEBUG_MODE = 'Y' THEN
1823                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1824                   END IF;
1825                   rollback to org_project;
1826                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1827                END IF;
1828 
1829             /* DELETE existing data for budget version */
1830 
1831                        l_stage :=  4300;
1832                        -- hr_utility.trace(to_char(l_stage));
1833                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Deleting Existing Data';
1834                        IF P_PA_DEBUG_MODE = 'Y' THEN
1835                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1836                        END IF;
1837 
1838                pa_fin_plan_pub.Delete_Version_Helper
1839                (p_budget_version_id    => p_budget_version_id,
1840                 x_return_status        => l_return_status,
1841                 x_msg_count            => l_msg_count,
1842                 x_msg_data             => l_msg_data);
1843 
1844               IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
1845                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
1846               END IF;
1847                        l_stage :=  4900;
1848                        -- hr_utility.trace(to_char(l_stage));
1849             UPDATE pa_budget_versions
1850                SET
1851                version_name = DECODE(version_name,null,l_bv_version_name
1852                                      ||'-'||to_char(l_budget_version_id),
1853                                      version_name)
1854               ,last_update_date = sysdate
1855               ,last_updated_by = fnd_global.user_id
1856               ,last_update_login = fnd_global.login_id
1857               ,revenue = 0
1858               ,raw_cost = 0
1859               ,burdened_cost = 0
1860               ,labor_quantity = 0
1861               ,total_borrowed_revenue = 0
1862               ,total_tp_revenue_in = 0
1863               ,total_tp_revenue_out = 0
1864               ,total_lent_resource_cost = 0
1865               ,total_tp_cost_in = 0
1866               ,total_tp_cost_out = 0
1867               ,total_unassigned_time_cost = 0
1868               ,total_utilization_percent = 0
1869               ,total_utilization_hours = 0
1870               ,total_capacity = 0
1871               ,total_head_count = 0
1872               ,total_revenue_adj = 0
1873               ,total_cost_adj = 0
1874               ,total_utilization_adj = 0
1875               ,total_head_count_adj = 0
1876               WHERE budget_version_id = p_budget_version_id;
1877 
1878                        l_stage :=  5000;
1879                        -- hr_utility.trace(to_char(l_stage));
1880               COMMIT;
1881      END IF; /* Budget Version Check */
1882 
1883                        l_stage :=  5100;
1884                        -- hr_utility.trace(to_char(l_stage));
1885      IF l_org_fcst_period_type = 'PA' THEN /* Denorm pkg requires this */
1886                        l_stage :=  5200;
1887                        -- hr_utility.trace(to_char(l_stage));
1888         l_pa_period_type := l_act_period_type;
1889      END IF;
1890 
1891      /* Now we have the Organization Project and all the task for own
1892         and sub-tasks and period profile, budget version and plan
1893         options in place */
1894 
1895      /* The program flow from here is as follows:
1896       - Create Forecast Elements
1897       - Create Forecast Lines
1898       - Create Proj Denorm for forecast lines
1899       - Create Resource Assignments
1900       - Create Budget Lines
1901       - Update Resource Assignments
1902       - Update Budget Versions
1903       - Create Proj Denorm for budget lines
1904       */
1905                        l_stage :=  5300;
1906                        -- hr_utility.trace(to_char(l_stage));
1907                        pa_debug.g_err_stage := TO_CHAR(l_stage) ||': Populate amt_rec for Forecast Lines Denorm';
1908                        IF P_PA_DEBUG_MODE = 'Y' THEN
1909                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
1910                        END IF;
1911       /* Populate amt_rec for Forecast Lines Denorm */
1912      amt_rec.delete;
1913 
1914      amt_rec(1).amount_type_code := 'QUANTITY';
1915      amt_rec(1).amount_subtype_code := 'QUANTITY';
1916      amt_rec(1).amount_type_id := get_amttype_id(p_amt_typ_code => 'QUANTITY');
1917      amt_rec(1).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'QUANTITY');
1918 
1919      amt_rec(2).amount_type_code := 'COST';
1920      amt_rec(2).amount_subtype_code := 'OWN_PROJECT_COST';
1921      amt_rec(2).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
1922      amt_rec(2).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'OWN_PROJECT_COST');
1923 
1924      amt_rec(3).amount_type_code := 'COST';
1925      amt_rec(3).amount_subtype_code := 'TP_COST_IN';
1926      amt_rec(3).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
1927      amt_rec(3).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_COST_IN');
1928 
1929      amt_rec(4).amount_type_code := 'COST';
1930      amt_rec(4).amount_subtype_code := 'TP_COST_OUT';
1931      amt_rec(4).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
1932      amt_rec(4).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_COST_OUT');
1933 
1934      amt_rec(5).amount_type_code := 'COST';
1935      amt_rec(5).amount_subtype_code := 'LENT_RESOURCE_COST';
1936      amt_rec(5).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
1937      amt_rec(5).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'LENT_RESOURCE_COST');
1938 
1939      amt_rec(6).amount_type_code := 'COST';
1940      amt_rec(6).amount_subtype_code := 'UNASSIGNED_TIME_COST';
1941      amt_rec(6).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
1942      amt_rec(6).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'UNASSIGNED_TIME_COST');
1943 
1944      amt_rec(7).amount_type_code := 'REVENUE';
1945      amt_rec(7).amount_subtype_code := 'OWN_REVENUE';
1946      amt_rec(7).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
1947      amt_rec(7).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'OWN_REVENUE');
1948 
1949      amt_rec(8).amount_type_code := 'REVENUE';
1950      amt_rec(8).amount_subtype_code := 'BORROWED_REVENUE';
1951      amt_rec(8).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
1952      amt_rec(8).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'BORROWED_REVENUE');
1953 
1954      amt_rec(9).amount_type_code := 'REVENUE';
1955      amt_rec(9).amount_subtype_code := 'TP_REVENUE_IN';
1956      amt_rec(9).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
1957      amt_rec(9).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_REVENUE_IN');
1958 
1959      amt_rec(10).amount_type_code := 'REVENUE';
1960      amt_rec(10).amount_subtype_code := 'TP_REVENUE_OUT';
1961      amt_rec(10).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
1962      amt_rec(10).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_REVENUE_OUT');
1963 
1964          /* DELETE FROM ALL plsql tables */
1965 
1966          /* For forecast items */
1967          l_fi_organization_id_tab.delete;
1968          l_fi_proj_organization_tab.delete;
1969          l_fi_proj_orgid_tab.delete;
1970          l_fi_exp_organization_id_tab.delete;
1971          l_fi_exp_orgid_tab.delete;
1972          l_fi_txn_project_id_tab.delete;
1973          l_fi_assignment_id_tab.delete;
1974          l_fi_resource_id_tab.delete;
1975          l_fi_period_name_tab.delete;
1976          l_fi_start_date_tab.delete;
1977          l_fi_end_date_tab.delete;
1978          l_fi_item_quantity_tab.delete;
1979          l_fi_raw_cost_tab.delete;
1980          l_fi_burdened_cost_tab.delete;
1981          l_fi_tp_cost_in_tab.delete;
1982          l_fi_tp_cost_out_tab.delete;
1983          l_fi_revenue_tab.delete;
1984          l_fi_tp_rev_in_tab.delete;
1985          l_fi_tp_rev_out_tab.delete;
1986          l_fi_borrowed_revenue_tab.delete;
1987          l_fi_lent_resource_cost_tab.delete;
1988          l_fi_unassigned_time_cost_tab.delete;
1989 
1990          /* Forecast Element PL/SQL Tables */
1991          l_fe_forecast_element_id_tab.delete;
1992          l_fe_organization_id_tab.delete;
1993          l_fe_org_id_tab.delete;
1994          l_fe_budget_version_id_tab.delete;
1995          l_fe_project_id_tab.delete;
1996          l_fe_task_id_tab.delete;
1997          l_fe_pvdr_rcvr_code_tab.delete;
1998          l_fe_other_organization_id_tab.delete;
1999          l_fe_other_org_id_tab.delete;
2000          l_fe_txn_project_id_tab.delete;
2001          l_fe_assignment_id_tab.delete;
2002          l_fe_resource_id_tab.delete;
2003 
2004          /* Forecast Lines PL/SQL Tables */
2005          l_fl_forecast_line_id_tab.delete;
2006          l_fl_forecast_element_id_tab.delete;
2007          l_fl_budget_version_id_tab.delete;
2008          l_fl_project_id_tab.delete;
2009          l_fl_task_id_tab.delete;
2010          l_fl_period_name_tab.delete;
2011          l_fl_start_date_tab.delete;
2012          l_fl_end_date_tab.delete;
2013          l_fl_quantity_tab.delete;
2014          l_fl_raw_cost_tab.delete;
2015          l_fl_burdened_cost_tab.delete;
2016          l_fl_tp_cost_in_tab.delete;
2017          l_fl_tp_cost_out_tab.delete;
2018          l_fl_revenue_tab.delete;
2019          l_fl_tp_rev_in_tab.delete;
2020          l_fl_tp_rev_out_tab.delete;
2021          l_fl_borrowed_revenue_tab.delete;
2022          l_fl_lent_resource_cost_tab.delete;
2023          l_fl_unassigned_time_cost_tab.delete;
2024 
2025          /* Budget Lines PL/SQL Tables */
2026 
2027          l_bl_res_asg_id_tab.delete;
2028          l_bl_budget_version_id_tab.delete; /* FPB2 */
2029          l_bl_start_date_tab.delete;
2030          l_bl_end_date_tab.delete;
2031          l_bl_period_name_tab.delete;
2032          l_bl_quantity_tab.delete;
2033          l_bl_raw_cost_tab.delete;
2034          l_bl_burdened_cost_tab.delete;
2035          l_bl_revenue_tab.delete;
2036          l_bl_borrowed_revenue_tab.delete;
2037          l_bl_tp_revenue_in_tab.delete;
2038          l_bl_tp_revenue_out_tab.delete;
2039          l_bl_lent_resource_cost_tab.delete;
2040          l_bl_tp_cost_in_tab.delete;
2041          l_bl_tp_cost_out_tab.delete;
2042          l_bl_unassigned_time_cost_tab.delete;
2043          l_bl_utilization_percent_tab.delete;
2044          l_bl_utilization_hours_tab.delete;
2045          l_bl_capacity_tab.delete;
2046          l_bl_head_count_tab.delete;
2047 
2048          l_pfi_organization_id         := null;
2049          l_pfi_project_organization_id := null;
2050          l_pfi_project_org_id          := null;
2051          l_pfi_exp_organization_id     := null;
2052          l_pfi_exp_org_id              := null;
2053          l_pfi_txn_project_id          := null;
2054          l_pfi_assignment_id           := null;
2055          l_pfi_resource_id             := null;
2056          l_prv_forecast_element_id     := null;
2057 
2058        IF NOT forecast_items%ISOPEN THEN
2059          OPEN forecast_items;
2060        ELSE
2061          CLOSE forecast_items;
2062          OPEN forecast_items;
2063        END IF;
2064                        l_stage :=  5400;
2065                        -- hr_utility.trace(to_char(l_stage));
2066          LOOP
2067                  l_fi_organization_id_tab.delete;
2068                  l_fi_proj_organization_tab.delete;
2069                  l_fi_proj_orgid_tab.delete;
2070                  l_fi_exp_organization_id_tab.delete;
2071                  l_fi_exp_orgid_tab.delete;
2072                  l_fi_txn_project_id_tab.delete;
2073                  l_fi_assignment_id_tab.delete;
2074                  l_fi_resource_id_tab.delete;
2075                  l_fi_period_name_tab.delete;
2076                  l_fi_start_date_tab.delete;
2077                  l_fi_end_date_tab.delete;
2078                  l_fi_item_quantity_tab.delete;
2079                  l_fi_raw_cost_tab.delete;
2080                  l_fi_burdened_cost_tab.delete;
2081                  l_fi_tp_cost_in_tab.delete;
2082                  l_fi_tp_cost_out_tab.delete;
2083                  l_fi_revenue_tab.delete;
2084                  l_fi_tp_rev_in_tab.delete;
2085                  l_fi_tp_rev_out_tab.delete;
2086                  l_fi_borrowed_revenue_tab.delete;
2087                  l_fi_lent_resource_cost_tab.delete;
2088                  l_fi_unassigned_time_cost_tab.delete;
2089 
2090            FETCH forecast_items BULK COLLECT INTO
2091                  l_fi_organization_id_tab,
2092                  l_fi_proj_organization_tab,
2093                  l_fi_proj_orgid_tab,
2094                  l_fi_exp_organization_id_tab,
2095                  l_fi_exp_orgid_tab,
2096                  l_fi_txn_project_id_tab,
2097                  l_fi_assignment_id_tab,
2098                  l_fi_resource_id_tab,
2099                  l_fi_period_name_tab,
2100                  l_fi_start_date_tab,
2101                  l_fi_end_date_tab,
2102                  l_fi_item_quantity_tab,
2103                  l_fi_raw_cost_tab,
2104                  l_fi_burdened_cost_tab,
2105                  l_fi_lent_resource_cost_tab,
2106                  l_fi_unassigned_time_cost_tab,
2107                  l_fi_tp_cost_in_tab,
2108                  l_fi_tp_cost_out_tab,
2109                  l_fi_revenue_tab,
2110                  l_fi_borrowed_revenue_tab,
2111                  l_fi_tp_rev_in_tab,
2112                  l_fi_tp_rev_out_tab LIMIT 200;
2113 
2114                  IF NVL(l_fi_organization_id_tab.count,0) =0 THEN
2115                        l_stage :=  4200;
2116                        -- hr_utility.trace(to_char(l_stage));
2117                          pa_debug.g_err_stage := l_stage||': Exiting fi loop no rows to process';
2118                          IF P_PA_DEBUG_MODE = 'Y' THEN
2119                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2120                          END IF;
2121                        EXIT WHEN NVL(l_fi_organization_id_tab.count,0) =0;
2122                  END IF;
2123 
2124 
2125                        l_stage :=  5500;
2126                        -- hr_utility.trace(to_char(l_stage));
2127                        -- hr_utility.trace('Row fetched : '||to_char(l_fi_organization_id_tab.count));
2128 
2129           /* Transfer Price Logic as part of Select statement:
2130 
2131              Org Context  TP Amount Type  CC In  CC Out Rev In Rev Out
2132              -----------  --------------  -----  ------ ------ -------
2133              Provider     COST              0      Y      0       0
2134              Provider     REVENUE           0      0      Y       0
2135              Receiver     COST              Y      0      0       0
2136              Receiver     REVENUE           0      0      0       Y
2137           */
2138 
2139       FOR i in l_fi_organization_id_tab.first..l_fi_organization_id_tab.last LOOP
2140       BEGIN
2141                  -- hr_utility.trace('Orgn id=> '||to_char(l_fi_organization_id_tab(i)));
2142                  -- hr_utility.trace('Proj Orgn=> '||to_char(l_fi_proj_organization_tab(i)));
2143                  -- hr_utility.trace('Proj Orgid=> '||to_char(l_fi_proj_orgid_tab(i)));
2144                  -- hr_utility.trace('Exp Orgn=> '||to_char(l_fi_exp_organization_id_tab(i)));
2145                  -- hr_utility.trace('Exp Orgid=> '||to_char(l_fi_exp_orgid_tab(i)));
2146                  -- hr_utility.trace('Txn Proj=> '||to_char(l_fi_txn_project_id_tab(i)));
2147                  -- hr_utility.trace('Asg Id=> '||to_char(l_fi_assignment_id_tab(i)));
2148                  -- hr_utility.trace('Res Id=> '||to_char(l_fi_resource_id_tab(i)));
2149                  -- hr_utility.trace('Period=> '||l_fi_period_name_tab(i));
2150                  -- hr_utility.trace('Start Dt=> '||l_fi_start_date_tab(i));
2151                  -- hr_utility.trace('End Dt=> '||l_fi_end_date_tab(i));
2152                  -- hr_utility.trace('prv txn Id => '||to_char(l_pfi_txn_project_id));
2153                  -- hr_utility.trace('Weighted_full => '||l_weighted_or_full_code);
2154                  -- hr_utility.trace('End Dt=> '||l_fi_end_date_tab(i));
2155 
2156        IF (l_pfi_txn_project_id IS NULL OR
2157            l_pfi_txn_project_id <> l_fi_txn_project_id_tab(i)) THEN
2158            l_stage :=  5510;
2159            -- hr_utility.trace(to_char(l_stage));
2160                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2161                                           ': Getting Project Action Allowed';
2162                   IF P_PA_DEBUG_MODE = 'Y' THEN
2163                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2164                   END IF;
2165                   pa_debug.g_err_stage := 'l_project_id ['||l_fi_txn_project_id_tab(i)|| ']';
2166                   IF P_PA_DEBUG_MODE = 'Y' THEN
2167                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2168                   END IF;
2169 
2170            l_project_action_allowed := pa_project_utils.check_project_action_allowed
2171                                     ( x_project_id => l_fi_txn_project_id_tab(i)
2172                                      ,x_action_code => 'PROJ_ORG_FORECASTING');
2173        END IF;
2174 
2175        IF l_project_action_allowed = 'N' THEN
2176           l_stage :=  5520;
2177            -- hr_utility.trace(to_char(l_stage));
2178           pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2179                                           ': Project Action Not Allowed -- Skipping : '
2180                                           ||l_fi_txn_project_id_tab(i);
2181 
2182           IF P_PA_DEBUG_MODE = 'Y' THEN
2183              pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2184           END IF;
2185           l_pfi_txn_project_id              := l_fi_txn_project_id_tab(i);
2186 
2187           RAISE pa_fp_org_fcst_gen_pub.proj_action_reloop;
2188        END IF;
2189 
2190 
2191        IF (l_pfi_txn_project_id IS NULL OR
2192            l_pfi_txn_project_id <> l_fi_txn_project_id_tab(i)) THEN
2193           IF l_weighted_or_full_code = 'W' THEN
2194              pa_fp_org_fcst_utils.get_probability_percent
2195              ( p_project_id    => l_fi_txn_project_id_tab(i)
2196               ,x_prob_percent  => l_prob_percent
2197               ,x_return_status => l_return_status
2198               ,x_err_code      => l_err_code);
2199                        l_stage :=  5600;
2200                        -- hr_utility.trace(to_char(l_stage));
2201                        -- hr_utility.trace('Probability => '||to_char(l_prob_percent));
2202 
2203                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2204                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2205                                           ': Error getting probability percent';
2206                   IF P_PA_DEBUG_MODE = 'Y' THEN
2207                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2208                   END IF;
2209                   pa_debug.g_err_stage := 'l_project_id ['||l_project_id|| ']';
2210                   IF P_PA_DEBUG_MODE = 'Y' THEN
2211                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2212                   END IF;
2213                   rollback to org_project;
2214                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
2215                END IF;
2216           ELSE
2217              l_prob_percent := 100;
2218           END IF;
2219        END IF;
2220 
2221        IF (( l_pfi_organization_id IS NULL) /* First Iteration */         OR
2222            ( l_pfi_organization_id <>
2223                                      l_fi_organization_id_tab(i)          OR
2224              l_pfi_project_organization_id     <>
2225                                      l_fi_proj_organization_tab(i)        OR
2226              l_pfi_project_org_id              <>
2227                                      l_fi_proj_orgid_tab(i)               OR
2228              l_pfi_exp_organization_id <>
2229                                      l_fi_exp_organization_id_tab(i)      OR
2230              l_pfi_exp_org_id          <>
2231                                      l_fi_exp_orgid_tab(i)                OR
2232              l_pfi_txn_project_id              <>
2233                                      l_fi_txn_project_id_tab(i)           OR
2234              l_pfi_assignment_id               <>
2235                                      l_fi_assignment_id_tab(i)            OR
2236              l_pfi_resource_id                 <>
2237                                      l_fi_resource_id_tab(i))) THEN
2238 
2239                        l_stage :=  5700;
2240                        -- hr_utility.trace(to_char(l_stage));
2241 
2242            l_fe_ctr := nvl(l_fe_forecast_element_id_tab.last+1,1);
2243                        l_stage :=  5800;
2244                        -- hr_utility.trace(to_char(l_stage));
2245 
2246            SELECT pa_org_fcst_elements_s.NEXTVAL
2247              INTO l_fe_new_seq
2248              FROM dual;
2249 
2250                        l_stage :=  5900;
2251                        -- hr_utility.trace(to_char(l_stage));
2252            l_fe_forecast_element_id_tab(l_fe_ctr) := l_fe_new_seq;
2253            l_fe_organization_id_tab(l_fe_ctr)     := l_fi_organization_id_tab(i);
2254            l_fe_budget_version_id_tab(l_fe_ctr)   := l_budget_version_id;
2255            l_fe_project_id_tab(l_fe_ctr)          := l_project_id;
2256            l_fe_task_id_tab(l_fe_ctr)             := l_own_task_id;
2257 
2258            IF l_fi_exp_organization_id_tab(i) =
2259                                          l_fi_organization_id_tab(i) THEN
2260                        l_stage :=  6000;
2261                        -- hr_utility.trace(to_char(l_stage));
2262               l_fe_pvdr_rcvr_code_tab(l_fe_ctr) := 'P';
2263               l_fe_other_organization_id_tab(l_fe_ctr)
2264                                      := l_fi_proj_organization_tab(i);
2265               l_fe_other_org_id_tab(l_fe_ctr)
2266                                      := l_fi_proj_orgid_tab(i);
2267               l_fe_org_id_tab(l_fe_ctr)
2268                                      := l_fi_exp_orgid_tab(i);
2269            ELSE
2270                        l_stage :=  6100;
2271                        -- hr_utility.trace(to_char(l_stage));
2272               l_fe_pvdr_rcvr_code_tab(l_fe_ctr) := 'R';
2273               l_fe_other_organization_id_tab(l_fe_ctr)
2274                                  := l_fi_exp_organization_id_tab(i);
2275               l_fe_other_org_id_tab(l_fe_ctr)
2276                                      := l_fi_exp_orgid_tab(i);
2277               l_fe_org_id_tab(l_fe_ctr)
2278                                      := l_fi_proj_orgid_tab(i);
2279            END IF;
2280                        l_stage :=  6200;
2281                        -- hr_utility.trace(to_char(l_stage));
2282            l_fe_txn_project_id_tab(l_fe_ctr)  := l_fi_txn_project_id_tab(i);
2283            l_fe_assignment_id_tab(l_fe_ctr)   := l_fi_assignment_id_tab(i);
2284            l_fe_resource_id_tab(l_fe_ctr)     := l_fi_resource_id_tab(i);
2285 
2286            l_fl_ctr := nvl(l_fl_forecast_line_id_tab.last+1,1);
2287                        l_stage :=  6300;
2288                        -- hr_utility.trace(to_char(l_stage));
2289            SELECT pa_org_forecast_lines_s.NEXTVAL
2290              INTO l_fl_new_seq
2291              FROM DUAL;
2292 
2293                        l_stage :=  6400;
2294                        -- hr_utility.trace(to_char(l_stage));
2295 
2296            -- hr_utility.trace('FECounter=> '||l_fe_ctr);
2297            -- hr_utility.trace('FE ID => '||to_char(l_fe_forecast_element_id_tab(l_fe_ctr)));
2298            -- hr_utility.trace('Org Id => '||to_char(l_fe_organization_id_tab(l_fe_ctr)));
2299            -- hr_utility.trace('BV Id => '||to_char(l_fe_budget_version_id_tab(l_fe_ctr)));
2300            -- hr_utility.trace('Proj Id => '||to_char(l_fe_project_id_tab(l_fe_ctr)));
2301            -- hr_utility.trace('Task Id => '||to_char(l_fe_task_id_tab(l_fe_ctr)));
2302            -- hr_utility.trace('PR => '||l_fe_pvdr_rcvr_code_tab(l_fe_ctr));
2303            -- hr_utility.trace('OOrg => '||to_char(l_fe_other_organization_id_tab(l_fe_ctr)));
2304            -- hr_utility.trace('Txn Proj => '||to_char(l_fe_txn_project_id_tab(l_fe_ctr)));
2305            -- hr_utility.trace('Asg Id => '||to_char(l_fe_assignment_id_tab(l_fe_ctr)));
2306            -- hr_utility.trace('ResId => '||to_char(l_fe_resource_id_tab(l_fe_ctr)));
2307 
2308            l_fl_forecast_line_id_tab(l_fl_ctr) := l_fl_new_seq;
2309            l_fl_forecast_element_id_tab(l_fl_ctr) := l_fe_new_seq;
2310            l_fl_budget_version_id_tab(l_fl_ctr) := l_budget_version_id;
2311            l_fl_project_id_tab(l_fl_ctr) := l_project_id;
2312            l_fl_task_id_tab(l_fl_ctr) := l_own_task_id;
2313            l_fl_period_name_tab(l_fl_ctr) := l_fi_period_name_tab(i);
2314            l_fl_start_date_tab(l_fl_ctr) := l_fi_start_date_tab(i);
2315            l_fl_end_date_tab(l_fl_ctr) := l_fi_end_date_tab(i);
2316            l_fl_quantity_tab(l_fl_ctr) := l_fi_item_quantity_tab(i);
2317            l_fl_raw_cost_tab(l_fl_ctr) := (l_fi_raw_cost_tab(i)
2318                                            *l_prob_percent)/ 100;
2319            l_fl_burdened_cost_tab(l_fl_ctr) := (l_fi_burdened_cost_tab(i)
2320                                                 *l_prob_percent)/ 100;
2321            l_fl_tp_cost_in_tab(l_fl_ctr) := (l_fi_tp_cost_in_tab(i)
2322                                              *l_prob_percent)/ 100;
2323            l_fl_tp_cost_out_tab(l_fl_ctr) := (l_fi_tp_cost_out_tab(i)
2324                                               *l_prob_percent)/ 100;
2325            l_fl_revenue_tab(l_fl_ctr) := (l_fi_revenue_tab(i)*l_prob_percent)/
2326                                            100;
2327            l_fl_tp_rev_in_tab(l_fl_ctr) := (l_fi_tp_rev_in_tab(i)
2328                                             *l_prob_percent)/ 100;
2329            l_fl_tp_rev_out_tab(l_fl_ctr) := (l_fi_tp_rev_out_tab(i)
2330                                              *l_prob_percent)/ 100;
2331            l_fl_borrowed_revenue_tab(l_fl_ctr) := (l_fi_borrowed_revenue_tab(i)
2332                                                    *l_prob_percent)/ 100;
2333            l_fl_lent_resource_cost_tab(l_fl_ctr)
2334                                              := (l_fi_lent_resource_cost_tab(i)
2335                                                  *l_prob_percent)/ 100;
2336            l_fl_unassigned_time_cost_tab(l_fl_ctr)
2337                                          := (l_fi_unassigned_time_cost_tab(i)
2338                                              *l_prob_percent)/ 100;
2339 
2340                        l_stage :=  6500;
2341                        -- hr_utility.trace(to_char(l_stage));
2342            -- hr_utility.trace('FLCounter=> '||l_fl_ctr);
2343            -- hr_utility.trace('FLId => '||to_char(l_fl_forecast_line_id_tab(l_fl_ctr)));
2344            -- hr_utility.trace('FEId => '||to_char(l_fl_forecast_element_id_tab(l_fl_ctr)));
2345            -- hr_utility.trace('BVId => '||to_char(l_fl_budget_version_id_tab(l_fl_ctr)));
2346            -- hr_utility.trace('ProjId => '||to_char(l_fl_project_id_tab(l_fl_ctr)));
2347            -- hr_utility.trace('TaskId => '||to_char(l_fl_task_id_tab(l_fl_ctr)));
2348            -- hr_utility.trace('Period => '||l_fl_period_name_tab(l_fl_ctr));
2349            -- hr_utility.trace('StartDt => '||l_fl_start_date_tab(l_fl_ctr));
2350            -- hr_utility.trace('EndDt => '||l_fl_end_date_tab(l_fl_ctr));
2351            -- hr_utility.trace('Qty => '||to_char(l_fl_quantity_tab(l_fl_ctr)));
2352            -- hr_utility.trace('Raw => '||to_char(l_fl_raw_cost_tab(l_fl_ctr)));
2353            -- hr_utility.trace('BurCost => '||to_char(l_fl_burdened_cost_tab(l_fl_ctr)));
2354            -- hr_utility.trace('CostIn => '||to_char(l_fl_tp_cost_in_tab(l_fl_ctr)));
2355            -- hr_utility.trace('CostOut => '||to_char(l_fl_tp_cost_out_tab(l_fl_ctr)));
2356            -- hr_utility.trace('Rev => '||to_char(l_fl_revenue_tab(l_fl_ctr)));
2357            -- hr_utility.trace('RevIn => '||to_char(l_fl_tp_rev_in_tab(l_fl_ctr)));
2358            -- hr_utility.trace('RevOut => '||to_char(l_fl_tp_rev_out_tab(l_fl_ctr)));
2359            -- hr_utility.trace('BorRev => '||to_char(l_fl_borrowed_revenue_tab(l_fl_ctr)));
2360            -- hr_utility.trace('LentCost => '||to_char(l_fl_lent_resource_cost_tab(l_fl_ctr)));
2361            -- hr_utility.trace('TimeCost => '||to_char(l_fl_unassigned_time_cost_tab(l_fl_ctr)));
2362        ELSE
2363 
2364                        l_stage :=  6600;
2365                        -- hr_utility.trace(to_char(l_stage));
2366            l_fl_ctr := nvl(l_fl_forecast_line_id_tab.last+1,1);
2367            SELECT pa_org_forecast_lines_s.NEXTVAL
2368              INTO l_fl_new_seq
2369              FROM DUAL;
2370                        l_stage :=  6700;
2371                        -- hr_utility.trace(to_char(l_stage));
2372 
2373            l_fl_forecast_line_id_tab(l_fl_ctr)  := l_fl_new_seq;
2374            l_fl_forecast_element_id_tab(l_fl_ctr) := l_fe_new_seq;
2375            l_fl_budget_version_id_tab(l_fl_ctr) := l_budget_version_id;
2376            l_fl_project_id_tab(l_fl_ctr)        := l_project_id;
2377            l_fl_task_id_tab(l_fl_ctr)           := l_own_task_id;
2378            l_fl_period_name_tab(l_fl_ctr)       := l_fi_period_name_tab(i);
2379            l_fl_start_date_tab(l_fl_ctr)        := l_fi_start_date_tab(i);
2380            l_fl_end_date_tab(l_fl_ctr)          := l_fi_end_date_tab(i);
2381            l_fl_quantity_tab(l_fl_ctr) := l_fi_item_quantity_tab(i);
2382            l_fl_raw_cost_tab(l_fl_ctr) := (l_fi_raw_cost_tab(i)
2383                                            *l_prob_percent)/ 100;
2384            l_fl_burdened_cost_tab(l_fl_ctr) := (l_fi_burdened_cost_tab(i)
2385                                                 *l_prob_percent)/ 100;
2386            l_fl_tp_cost_in_tab(l_fl_ctr) := (l_fi_tp_cost_in_tab(i)
2387                                              *l_prob_percent)/ 100;
2388            l_fl_tp_cost_out_tab(l_fl_ctr) := (l_fi_tp_cost_out_tab(i)
2389                                               *l_prob_percent)/ 100;
2390            l_fl_revenue_tab(l_fl_ctr) := (l_fi_revenue_tab(i)*l_prob_percent)/
2391                                            100;
2392            l_fl_tp_rev_in_tab(l_fl_ctr) := (l_fi_tp_rev_in_tab(i)
2393                                             *l_prob_percent)/ 100;
2394            l_fl_tp_rev_out_tab(l_fl_ctr) := (l_fi_tp_rev_out_tab(i)
2395                                              *l_prob_percent)/ 100;
2396            l_fl_borrowed_revenue_tab(l_fl_ctr) := (l_fi_borrowed_revenue_tab(i)
2397                                                    *l_prob_percent)/ 100;
2398            l_fl_lent_resource_cost_tab(l_fl_ctr)
2399                                              := (l_fi_lent_resource_cost_tab(i)
2400                                                  *l_prob_percent)/ 100;
2401            l_fl_unassigned_time_cost_tab(l_fl_ctr)
2402                                          := (l_fi_unassigned_time_cost_tab(i)
2403                                              *l_prob_percent)/ 100;
2404 
2405                        l_stage :=  6800;
2406                        -- hr_utility.trace(to_char(l_stage));
2407            -- hr_utility.trace('FLCounter=> '||l_fl_ctr);
2408            -- hr_utility.trace('FLId => '||to_char(l_fl_forecast_line_id_tab(l_fl_ctr)));
2409            -- hr_utility.trace('FEId => '||to_char(l_fl_forecast_element_id_tab(l_fl_ctr)));
2410            -- hr_utility.trace('BVId => '||to_char(l_fl_budget_version_id_tab(l_fl_ctr)));
2411            -- hr_utility.trace('ProjId => '||to_char(l_fl_project_id_tab(l_fl_ctr)));
2412            -- hr_utility.trace('TaskId => '||to_char(l_fl_task_id_tab(l_fl_ctr)));
2413            -- hr_utility.trace('Period => '||l_fl_period_name_tab(l_fl_ctr));
2414            -- hr_utility.trace('StartDt => '||l_fl_start_date_tab(l_fl_ctr));
2415            -- hr_utility.trace('EndDt => '||l_fl_end_date_tab(l_fl_ctr));
2416            -- hr_utility.trace('Qty => '||to_char(l_fl_quantity_tab(l_fl_ctr)));
2417            -- hr_utility.trace('Raw => '||to_char(l_fl_raw_cost_tab(l_fl_ctr)));
2418            -- hr_utility.trace('BurCost => '||to_char(l_fl_burdened_cost_tab(l_fl_ctr)));
2419            -- hr_utility.trace('CostIn => '||to_char(l_fl_tp_cost_in_tab(l_fl_ctr)));
2420            -- hr_utility.trace('CostOut => '||to_char(l_fl_tp_cost_out_tab(l_fl_ctr)));
2421            -- hr_utility.trace('Rev => '||to_char(l_fl_revenue_tab(l_fl_ctr)));
2422            -- hr_utility.trace('RevIn => '||to_char(l_fl_tp_rev_in_tab(l_fl_ctr)));
2423            -- hr_utility.trace('RevOut => '||to_char(l_fl_tp_rev_out_tab(l_fl_ctr)));
2424            -- hr_utility.trace('BorRev => '||to_char(l_fl_borrowed_revenue_tab(l_fl_ctr)));
2425            -- hr_utility.trace('LentCost => '||to_char(l_fl_lent_resource_cost_tab(l_fl_ctr)));
2426            -- hr_utility.trace('TimeCost => '||to_char(l_fl_unassigned_time_cost_tab(l_fl_ctr)));
2427        END IF;
2428 
2429                        l_stage :=  6900;
2430                        -- hr_utility.trace(to_char(l_stage));
2431 
2432           l_pfi_organization_id             := l_fi_organization_id_tab(i);
2433           l_pfi_project_organization_id     :=
2434                                           l_fi_proj_organization_tab(i);
2435           l_pfi_project_org_id              :=
2436                                           l_fi_proj_orgid_tab(i);
2437           l_pfi_exp_organization_id :=
2438                                       l_fi_exp_organization_id_tab(i);
2439           l_pfi_exp_org_id          :=
2440                                       l_fi_exp_orgid_tab(i);
2441           l_pfi_txn_project_id              := l_fi_txn_project_id_tab(i);
2442           l_pfi_assignment_id               := l_fi_assignment_id_tab(i);
2443           l_pfi_resource_id                 := l_fi_resource_id_tab(i);
2444 
2445                        l_stage :=  7000;
2446                        -- hr_utility.trace(to_char(l_stage));
2447 
2448       EXCEPTION
2449         WHEN pa_fp_org_fcst_gen_pub.proj_action_reloop THEN
2450              -- hr_utility.trace('UserDefined Txn Project Skipping - '||to_char(l_stage)||'-'||SQLERRM);
2451              pa_debug.g_err_stage := l_stage||': UserDefined Txn Project Skipping - '||'['||SQLERRM||']';
2452              IF P_PA_DEBUG_MODE = 'Y' THEN
2453                 pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2454              END IF;
2455              NULL;
2456       END;
2457       END LOOP; -- l_fi_organization_id_tab
2458 
2459 
2460                        l_stage :=  7100;
2461                        -- hr_utility.trace(to_char(l_stage));
2462 
2463       /* Bulk Insert into Forecast Elements */
2464           pa_debug.g_err_stage := l_stage||': fe_count =  ['||l_fe_forecast_element_id_tab.count||']';
2465                   IF P_PA_DEBUG_MODE = 'Y' THEN
2466                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2467                   END IF;
2468 
2469           IF l_fe_forecast_element_id_tab.count > 0 THEN
2470                        l_stage :=  7200;
2471                        -- hr_utility.trace(to_char(l_stage));
2472 
2473 
2474           FORALL i in l_fe_forecast_element_id_tab.first..l_fe_forecast_element_id_tab.last
2475             INSERT INTO pa_org_fcst_elements
2476              ( forecast_element_id
2477               ,organization_id
2478               ,org_id
2479               ,budget_version_id
2480               ,project_id
2481               ,task_id
2482               ,provider_receiver_code
2483               ,other_organization_id
2484               ,other_org_id
2485               ,txn_project_id
2486               ,assignment_id
2487               ,resource_id
2488               ,record_version_number
2489               ,creation_date
2490               ,created_by
2491               ,last_update_login
2492               ,last_updated_by
2493               ,last_update_date
2494              ) VALUES (
2495                l_fe_forecast_element_id_tab(i)
2496               ,l_fe_organization_id_tab(i)
2497               ,l_fe_org_id_tab(i)
2498               ,l_fe_budget_version_id_tab(i)
2499               ,l_fe_project_id_tab(i)
2500               ,l_fe_task_id_tab(i)
2501               ,l_fe_pvdr_rcvr_code_tab(i)
2502               ,l_fe_other_organization_id_tab(i)
2503               ,l_fe_other_org_id_tab(i)
2504               ,l_fe_txn_project_id_tab(i)
2505               ,l_fe_assignment_id_tab(i)
2506               ,l_fe_resource_id_tab(i)
2507               ,1
2508               ,sysdate
2509               ,fnd_global.user_id
2510               ,fnd_global.login_id
2511               ,fnd_global.user_id
2512               ,sysdate);
2513 
2514               l_records_affected := SQL%ROWCOUNT;
2515 
2516               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2517                              ': After Inserting Forecast Elements' ;
2518               IF P_PA_DEBUG_MODE = 'Y' THEN
2519                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2520               END IF;
2521 
2522               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2523                     ': Inserted [' || TO_CHAR(l_records_affected) ||
2524                     '] Forecast Elements.';
2525               IF P_PA_DEBUG_MODE = 'Y' THEN
2526                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2527               END IF;
2528                        l_stage :=  7300;
2529                        -- hr_utility.trace(to_char(l_stage));
2530               END IF;
2531 
2532       /* Bulk Insert into Forecast Lines */
2533                        l_stage :=  7400;
2534                        -- hr_utility.trace(to_char(l_stage));
2535 
2536           pa_debug.g_err_stage := l_stage||': fl_count =  ['||l_fl_forecast_line_id_tab.count||']';
2537                   IF P_PA_DEBUG_MODE = 'Y' THEN
2538                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2539                   END IF;
2540 
2541           IF l_fl_forecast_line_id_tab.count > 0 THEN
2542           FORALL j in l_fl_forecast_line_id_tab.first..l_fl_forecast_line_id_tab.last
2543             INSERT INTO pa_org_forecast_lines
2544             ( forecast_line_id
2545              ,forecast_element_id
2546              ,budget_version_id
2547              ,project_id
2548              ,task_id
2549              ,period_name
2550              ,start_date
2551              ,end_date
2552              ,quantity
2553              ,raw_cost
2554              ,burdened_cost
2555              ,tp_cost_in
2556              ,tp_cost_out
2557              ,revenue
2558              ,tp_revenue_in
2559              ,tp_revenue_out
2560              ,borrowed_revenue
2561              ,lent_resource_cost
2562              ,unassigned_time_cost
2563              ,record_version_number
2564              ,creation_date
2565              ,created_by
2566              ,last_update_login
2567              ,last_updated_by
2568              ,last_update_date
2569             ) VALUES (
2570               l_fl_forecast_line_id_tab(j)
2571              ,l_fl_forecast_element_id_tab(j)
2572              ,l_fl_budget_version_id_tab(j)
2573              ,l_fl_project_id_tab(j)
2574              ,l_fl_task_id_tab(j)
2575              ,l_fl_period_name_tab(j)
2576              ,l_fl_start_date_tab(j)
2577              ,l_fl_end_date_tab(j)
2578              ,l_fl_quantity_tab(j)
2579              ,l_fl_raw_cost_tab(j)
2580              ,l_fl_burdened_cost_tab(j)
2581              ,l_fl_tp_cost_in_tab(j)
2582              ,l_fl_tp_cost_out_tab(j)
2583              ,l_fl_revenue_tab(j)
2584              ,l_fl_tp_rev_in_tab(j)
2585              ,l_fl_tp_rev_out_tab(j)
2586              ,l_fl_borrowed_revenue_tab(j)
2587              ,l_fl_lent_resource_cost_tab(j)
2588              ,l_fl_unassigned_time_cost_tab(j)
2589              ,1
2590              ,sysdate
2591              ,fnd_global.user_id
2592              ,fnd_global.login_id
2593              ,fnd_global.user_id
2594              ,sysdate);
2595 
2596               l_records_affected := SQL%ROWCOUNT;
2597 
2598                        l_stage :=  7500;
2599                        -- hr_utility.trace(to_char(l_stage));
2600               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2601                              ': After Inserting Forecast Lines' ;
2602               IF P_PA_DEBUG_MODE = 'Y' THEN
2603                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2604               END IF;
2605 
2606               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2607                     ': Inserted [' || TO_CHAR(l_records_affected) ||
2608                     '] Forecast Lines.';
2609               IF P_PA_DEBUG_MODE = 'Y' THEN
2610                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2611               END IF;
2612 
2613               END IF;
2614 
2615                        l_stage :=  7600;
2616                        -- hr_utility.trace(to_char(l_stage));
2617 
2618           DELETE FROM pa_fin_plan_lines_tmp;
2619 
2620                        l_stage :=  7700;
2621                        -- hr_utility.trace(to_char(l_stage));
2622 
2623                   pa_debug.g_err_stage := l_stage||': fl_count =  ['
2624                                                  ||l_fl_forecast_line_id_tab.count||']';
2625                   IF P_PA_DEBUG_MODE = 'Y' THEN
2626                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2627                   END IF;
2628 
2629         IF l_fl_forecast_line_id_tab.count > 0 THEN
2630           FORALL j in l_fl_forecast_line_id_tab.first..l_fl_forecast_line_id_tab.last
2631             INSERT INTO pa_fin_plan_lines_tmp
2632             ( resource_assignment_id
2633              ,object_id
2634              ,object_type_code
2635              ,period_name
2636              ,start_date
2637              ,end_date
2638              ,currency_type
2639              ,currency_code
2640              ,source_txn_currency_code /* Bug 2796261 */
2641              ,quantity
2642              ,raw_cost
2643              ,burdened_cost
2644              ,tp_cost_in
2645              ,tp_cost_out
2646              ,lent_resource_cost
2647              ,unassigned_time_cost
2648              ,cost_adj
2649              ,revenue
2650              ,borrowed_revenue
2651              ,tp_revenue_in
2652              ,tp_revenue_out
2653              ,revenue_adj
2654              ,utilization_percent
2655              ,utilization_adj
2656              ,utilization_hours
2657              ,capacity
2658              ,head_count
2659              ,head_count_adj
2660              ,margin
2661              ,margin_percentage)
2662             SELECT
2663               -1
2664              ,l_fl_forecast_element_id_tab(j)
2665              ,'FCST_ELEMENTS'
2666              ,l_fl_period_name_tab(j)
2667              ,l_fl_start_date_tab(j)
2668              ,l_fl_end_date_tab(j)
2669              ,'PROJ_FUNCTIONAL'
2670              ,l_org_projfunc_currency_code
2671              ,l_org_projfunc_currency_code  /* 2796261 */
2672              ,l_fl_quantity_tab(j)
2673              ,l_fl_raw_cost_tab(j)
2674              ,l_fl_burdened_cost_tab(j)
2675              ,l_fl_tp_cost_in_tab(j)
2676              ,l_fl_tp_cost_out_tab(j)
2677              ,l_fl_lent_resource_cost_tab(j)
2678              ,l_fl_unassigned_time_cost_tab(j)
2679              ,0
2680              ,l_fl_revenue_tab(j)
2681              ,l_fl_borrowed_revenue_tab(j)
2682              ,l_fl_tp_rev_in_tab(j)
2683              ,l_fl_tp_rev_out_tab(j)
2684              ,0
2685              ,0
2686              ,0
2687              ,0
2688              ,0
2689              ,0
2690              ,0
2691              ,DECODE(SIGN(
2692                l_fl_revenue_tab(j)+
2693                l_fl_borrowed_revenue_tab(j)+
2694                l_fl_tp_rev_in_tab(j)-
2695                l_fl_tp_rev_out_tab(j)),0,0,-1,0,
2696               (l_fl_revenue_tab(j)+
2697                l_fl_borrowed_revenue_tab(j)+
2698                l_fl_tp_rev_in_tab(j)-
2699                l_fl_tp_rev_out_tab(j)) -
2700               (l_fl_burdened_cost_tab(j)+
2701                l_fl_lent_resource_cost_tab(j)+
2702                l_fl_unassigned_time_cost_tab(j)+
2703                l_fl_tp_cost_in_tab(j)-
2704                l_fl_tp_cost_out_tab(j)))
2705              ,DECODE(SIGN(
2706                l_fl_revenue_tab(j)+
2707                l_fl_borrowed_revenue_tab(j)+
2708                l_fl_tp_rev_in_tab(j)-
2709                l_fl_tp_rev_out_tab(j)),0,0,-1,0,
2710               ((l_fl_revenue_tab(j)+
2711                l_fl_borrowed_revenue_tab(j)+
2712                l_fl_tp_rev_in_tab(j)-
2713                l_fl_tp_rev_out_tab(j)) -
2714               (l_fl_burdened_cost_tab(j)+
2715                l_fl_lent_resource_cost_tab(j)+
2716                l_fl_unassigned_time_cost_tab(j)+
2717                l_fl_tp_cost_in_tab(j)-
2718                l_fl_tp_cost_out_tab(j)))/
2719               (l_fl_revenue_tab(j)+
2720                l_fl_borrowed_revenue_tab(j)+
2721                l_fl_tp_rev_in_tab(j)-
2722                l_fl_tp_rev_out_tab(j)) * 100)
2723              FROM DUAL;
2724 
2725                        l_stage :=  7800;
2726                        -- hr_utility.trace(to_char(l_stage));
2727               l_records_affected := SQL%ROWCOUNT;
2728 
2729               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2730                              ': After Inserting Fin Plan Lines Tmp' ;
2731               IF P_PA_DEBUG_MODE = 'Y' THEN
2732                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2733               END IF;
2734 
2735               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2736                     ': Inserted [' || TO_CHAR(l_records_affected) ||
2737                     '] Fin Plan Lines Tmp.';
2738               IF P_PA_DEBUG_MODE = 'Y' THEN
2739                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2740               END IF;
2741 
2742           /* Populate Project Periods Denorm for Forecast Lines */
2743 
2744                        l_stage :=  7900;
2745                        -- hr_utility.trace(to_char(l_stage));
2746               Pa_Plan_Matrix.Maintain_Plan_Matrix
2747                ( p_amount_type_tab   => amt_rec
2748                 ,p_period_profile_id => l_period_profile_id
2749                 ,p_prior_period_flag => 'N'
2750                 ,p_commit_Flag       => 'N'
2751                 ,p_budget_version_id => l_budget_version_id
2752                 ,p_project_id        => l_project_id
2753                 ,p_debug_mode        => l_debug_mode
2754                 ,p_add_msg_in_stack  => 'Y'
2755                 ,x_return_status     => l_return_status
2756                 ,x_msg_count         => l_msg_count
2757                 ,x_msg_data          => errbuff   );
2758 
2759                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2760                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2761                                           ': Error creating Period Denorm';
2762                   IF P_PA_DEBUG_MODE = 'Y' THEN
2763                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2764                   END IF;
2765                   pa_debug.g_err_stage := 'l_period_profile_id     ['||l_period_profile_id      || ']';
2766                   IF P_PA_DEBUG_MODE = 'Y' THEN
2767                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2768                   END IF;
2769                   pa_debug.g_err_stage := 'l_budget_version_id     ['||l_budget_version_id      || ']';
2770                   IF P_PA_DEBUG_MODE = 'Y' THEN
2771                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2772                   END IF;
2773                   pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
2774                   IF P_PA_DEBUG_MODE = 'Y' THEN
2775                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2776                   END IF;
2777                   rollback to org_project;
2778                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
2779                END IF;
2780 
2781             END IF;
2782 
2783                        l_stage :=  8000;
2784                        -- hr_utility.trace(to_char(l_stage));
2785 
2786          /* For forecast items */
2787          l_fi_organization_id_tab.delete;
2788          l_fi_proj_organization_tab.delete;
2789          l_fi_proj_orgid_tab.delete;
2790          l_fi_exp_organization_id_tab.delete;
2791          l_fi_exp_orgid_tab.delete;
2792          l_fi_txn_project_id_tab.delete;
2793          l_fi_assignment_id_tab.delete;
2794          l_fi_resource_id_tab.delete;
2795          l_fi_period_name_tab.delete;
2796          l_fi_start_date_tab.delete;
2797          l_fi_end_date_tab.delete;
2798          l_fi_item_quantity_tab.delete;
2799          l_fi_raw_cost_tab.delete;
2800          l_fi_burdened_cost_tab.delete;
2801          l_fi_tp_cost_in_tab.delete;
2802          l_fi_tp_cost_out_tab.delete;
2803          l_fi_revenue_tab.delete;
2804          l_fi_tp_rev_in_tab.delete;
2805          l_fi_tp_rev_out_tab.delete;
2806          l_fi_borrowed_revenue_tab.delete;
2807          l_fi_lent_resource_cost_tab.delete;
2808          l_fi_unassigned_time_cost_tab.delete;
2809 
2810          /* Forecast Element PL/SQL Tables */
2811          l_fe_forecast_element_id_tab.delete;
2812          l_fe_organization_id_tab.delete;
2813          l_fe_org_id_tab.delete;
2814          l_fe_budget_version_id_tab.delete;
2815          l_fe_project_id_tab.delete;
2816          l_fe_task_id_tab.delete;
2817          l_fe_pvdr_rcvr_code_tab.delete;
2818          l_fe_other_organization_id_tab.delete;
2819          l_fe_other_org_id_tab.delete;
2820          l_fe_txn_project_id_tab.delete;
2821          l_fe_assignment_id_tab.delete;
2822          l_fe_resource_id_tab.delete;
2823 
2824          /* Forecast Lines PL/SQL Tables */
2825          l_fl_forecast_line_id_tab.delete;
2826          l_fl_forecast_element_id_tab.delete;
2827          l_fl_budget_version_id_tab.delete;
2828          l_fl_project_id_tab.delete;
2829          l_fl_task_id_tab.delete;
2830          l_fl_period_name_tab.delete;
2831          l_fl_start_date_tab.delete;
2832          l_fl_end_date_tab.delete;
2833          l_fl_quantity_tab.delete;
2834          l_fl_raw_cost_tab.delete;
2835          l_fl_burdened_cost_tab.delete;
2836          l_fl_tp_cost_in_tab.delete;
2837          l_fl_tp_cost_out_tab.delete;
2838          l_fl_revenue_tab.delete;
2839          l_fl_tp_rev_in_tab.delete;
2840          l_fl_tp_rev_out_tab.delete;
2841          l_fl_borrowed_revenue_tab.delete;
2842          l_fl_lent_resource_cost_tab.delete;
2843          l_fl_unassigned_time_cost_tab.delete;
2844 
2845      END LOOP; /*  BULK FETCH OF FORECAST ITEMS */
2846      CLOSE forecast_items;
2847 
2848                        l_stage :=  8100;
2849                        -- hr_utility.trace(to_char(l_stage));
2850                        pa_debug.g_err_stage := l_stage||': Populate amt_rec for Budget Lines Denorm';
2851                        IF P_PA_DEBUG_MODE = 'Y' THEN
2852                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2853                        END IF;
2854    /* Populate amt_rec for Budget Lines Denorm */
2855      amt_rec.delete;
2856 
2857      amt_rec(1).amount_type_code := 'QUANTITY';
2858      amt_rec(1).amount_subtype_code := 'QUANTITY';
2859      amt_rec(1).amount_type_id := get_amttype_id(p_amt_typ_code => 'QUANTITY');
2860      amt_rec(1).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'QUANTITY');
2861 
2862      amt_rec(2).amount_type_code := 'COST';
2863      amt_rec(2).amount_subtype_code := 'OWN_PROJECT_COST';
2864      amt_rec(2).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
2865      amt_rec(2).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'OWN_PROJECT_COST');
2866 
2867      amt_rec(3).amount_type_code := 'COST';
2868      amt_rec(3).amount_subtype_code := 'TP_COST_IN';
2869      amt_rec(3).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
2870      amt_rec(3).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_COST_IN');
2871 
2872      amt_rec(4).amount_type_code := 'COST';
2873      amt_rec(4).amount_subtype_code := 'TP_COST_OUT';
2874      amt_rec(4).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
2875      amt_rec(4).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_COST_OUT');
2876 
2877      amt_rec(5).amount_type_code := 'COST';
2878      amt_rec(5).amount_subtype_code := 'LENT_RESOURCE_COST';
2879      amt_rec(5).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
2880      amt_rec(5).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'LENT_RESOURCE_COST');
2881 
2882      amt_rec(6).amount_type_code := 'COST';
2883      amt_rec(6).amount_subtype_code := 'UNASSIGNED_TIME_COST';
2884      amt_rec(6).amount_type_id := get_amttype_id(p_amt_typ_code => 'COST');
2885      amt_rec(6).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'UNASSIGNED_TIME_COST');
2886 
2887      amt_rec(7).amount_type_code := 'REVENUE';
2888      amt_rec(7).amount_subtype_code := 'OWN_REVENUE';
2889      amt_rec(7).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
2890      amt_rec(7).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'OWN_REVENUE');
2891 
2892      amt_rec(8).amount_type_code := 'REVENUE';
2893      amt_rec(8).amount_subtype_code := 'BORROWED_REVENUE';
2894      amt_rec(8).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
2895      amt_rec(8).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'BORROWED_REVENUE');
2896 
2897      amt_rec(9).amount_type_code := 'REVENUE';
2898      amt_rec(9).amount_subtype_code := 'TP_REVENUE_IN';
2899      amt_rec(9).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
2900      amt_rec(9).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_REVENUE_IN');
2901 
2902      amt_rec(10).amount_type_code := 'REVENUE';
2903      amt_rec(10).amount_subtype_code := 'TP_REVENUE_OUT';
2904      amt_rec(10).amount_type_id := get_amttype_id(p_amt_typ_code => 'REVENUE');
2905      amt_rec(10).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'TP_REVENUE_OUT');
2906 
2907      amt_rec(11).amount_type_code := 'MARGIN';
2908      amt_rec(11).amount_subtype_code := 'MARGIN';
2909      amt_rec(11).amount_type_id := get_amttype_id(p_amt_typ_code => 'MARGIN');
2910      amt_rec(11).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'MARGIN');
2911 
2912      amt_rec(12).amount_type_code := 'MARGIN_PERCENT';
2913      amt_rec(12).amount_subtype_code := 'MARGIN_PERCENT';
2914      amt_rec(12).amount_type_id := get_amttype_id(p_amt_typ_code => 'MARGIN_PERCENT');
2915      amt_rec(12).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'MARGIN_PERCENT');
2916 
2917      amt_rec(13).amount_type_code := 'UTILIZATION';
2918      amt_rec(13).amount_subtype_code := 'UTILIZATION_PERCENT';
2919      amt_rec(13).amount_type_id := get_amttype_id(p_amt_typ_code => 'UTILIZATION');
2920      amt_rec(13).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'UTILIZATION_PERCENT');
2921 
2922     /*
2923      amt_rec(14).amount_type_code := 'UTILIZATION';
2924      amt_rec(14).amount_subtype_code := 'UTILIZATION_HOURS';
2925      amt_rec(14).amount_type_id := get_amttype_id(p_amt_typ_code => 'UTILIZATION');
2926      amt_rec(14).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'UTILIZATION_HOURS');
2927 
2928      amt_rec(15).amount_type_code := 'UTILIZATION';
2929      amt_rec(15).amount_subtype_code := 'CAPACITY';
2930      amt_rec(15).amount_type_id := get_amttype_id(p_amt_typ_code => 'UTILIZATION');
2931      amt_rec(15).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'CAPACITY');
2932     */
2933 
2934      amt_rec(14).amount_type_code := 'HEADCOUNT';
2935      amt_rec(14).amount_subtype_code := 'BEGIN_HEADCOUNT';
2936      amt_rec(14).amount_type_id := get_amttype_id(p_amt_typ_code => 'HEADCOUNT');
2937      amt_rec(14).amount_subtype_id := get_amttype_id(p_amt_typ_code => 'BEGIN_HEADCOUNT');
2938 
2939                        l_stage :=  8200;
2940                        -- hr_utility.trace(to_char(l_stage));
2941                        pa_debug.g_err_stage := l_stage||': Create Resource Assignments for Own Task';
2942                        IF P_PA_DEBUG_MODE = 'Y' THEN
2943                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2944                        END IF;
2945      /* Create Resource Assignments for Own Task */
2946 
2947         l_own_resource_assignment_id := NULL;
2948 
2949         pa_fp_resource_assignments_pkg.Insert_Row
2950         ( px_resource_assignment_id      => l_own_resource_assignment_id
2951          ,p_budget_version_id            => l_budget_version_id
2952          ,p_project_id                   => l_project_id
2953          ,p_task_id                      => l_own_task_id
2954          ,p_resource_list_member_id      => l_resource_list_member_id
2955          ,p_unit_of_measure              => 'HOURS'
2956          ,p_track_as_labor_flag          => 'Y'
2957          ,p_standard_bill_rate           => Null
2958          ,p_average_bill_rate            => Null
2959          ,p_average_cost_rate            => Null
2960          ,p_project_assignment_id        => -1
2961          ,p_plan_error_code              => Null
2962          ,p_total_plan_revenue           => 0
2963          ,p_total_plan_raw_cost          => 0
2964          ,p_total_plan_burdened_cost     => 0
2965          ,p_total_plan_quantity          => 0
2966          ,p_average_discount_percentage  => Null
2967          ,p_total_borrowed_revenue       => 0
2968          ,p_total_tp_revenue_in          => 0
2969          ,p_total_tp_revenue_out         => 0
2970          ,p_total_revenue_adj            => 0
2971          ,p_total_lent_resource_cost     => 0
2972          ,p_total_tp_cost_in             => 0
2973          ,p_total_tp_cost_out            => 0
2974          ,p_total_cost_adj               => 0
2975          ,p_total_unassigned_time_cost   => 0
2976          ,p_total_utilization_percent    => 0
2977          ,p_total_utilization_hours      => 0
2978          ,p_total_utilization_adj        => 0
2979          ,p_total_capacity               => 0
2980          ,p_total_head_count             => 0
2981          ,p_total_head_count_adj         => 0
2982          ,p_resource_assignment_type     => 'OWN'
2983          ,x_row_id                       => l_row_id
2984          ,x_return_status                => l_return_status );
2985 
2986                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2987                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
2988                                           ': Error creating Resource Assignment';
2989                   IF P_PA_DEBUG_MODE = 'Y' THEN
2990                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2991                   END IF;
2992                   pa_debug.g_err_stage := 'l_budget_version_id      ['||l_budget_version_id      || ']';
2993                   IF P_PA_DEBUG_MODE = 'Y' THEN
2994                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2995                   END IF;
2996                   pa_debug.g_err_stage := 'l_project_id             ['||l_project_id             || ']';
2997                   IF P_PA_DEBUG_MODE = 'Y' THEN
2998                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
2999                   END IF;
3000                   pa_debug.g_err_stage := 'l_own_task_id            ['||l_own_task_id            || ']';
3001                   IF P_PA_DEBUG_MODE = 'Y' THEN
3002                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3003                   END IF;
3004                   pa_debug.g_err_stage := 'l_resource_list_member_id['||l_resource_list_member_id|| ']';
3005                   IF P_PA_DEBUG_MODE = 'Y' THEN
3006                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3007                   END IF;
3008                   rollback to org_project;
3009                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
3010                END IF;
3011 
3012                        l_stage :=  8300;
3013                        -- hr_utility.trace(to_char(l_stage));
3014 
3015          l_bl_budget_version_id_tab.delete;     /* FPB2: budget_version_id */
3016          l_bl_res_asg_id_tab.delete;
3017          l_bl_start_date_tab.delete;
3018          l_bl_end_date_tab.delete;
3019          l_bl_period_name_tab.delete;
3020          l_bl_quantity_tab.delete;
3021          l_bl_raw_cost_tab.delete;
3022          l_bl_burdened_cost_tab.delete;
3023          l_bl_revenue_tab.delete;
3024          l_bl_borrowed_revenue_tab.delete;
3025          l_bl_tp_revenue_in_tab.delete;
3026          l_bl_tp_revenue_out_tab.delete;
3027          l_bl_lent_resource_cost_tab.delete;
3028          l_bl_tp_cost_in_tab.delete;
3029          l_bl_tp_cost_out_tab.delete;
3030          l_bl_unassigned_time_cost_tab.delete;
3031          l_bl_utilization_percent_tab.delete;
3032          l_bl_utilization_hours_tab.delete;
3033          l_bl_capacity_tab.delete;
3034          l_bl_head_count_tab.delete;
3035 
3036          IF NOT fl_lines_task%ISOPEN THEN
3037            OPEN fl_lines_task;
3038          ELSE
3039            CLOSE fl_lines_task;
3040            OPEN fl_lines_task;
3041          END IF;
3042 
3043                        l_stage :=  8400;
3044                        -- hr_utility.trace(to_char(l_stage));
3045 
3046          FETCH fl_lines_task BULK COLLECT INTO
3047            l_bl_budget_version_id_tab          /* FPB2: budget_version_id */
3048           ,l_bl_res_asg_id_tab
3049           ,l_bl_period_name_tab
3050           ,l_bl_start_date_tab
3051           ,l_bl_end_date_tab
3052           ,l_bl_quantity_tab
3053           ,l_bl_raw_cost_tab
3054           ,l_bl_burdened_cost_tab
3055           ,l_bl_revenue_tab
3056           ,l_bl_borrowed_revenue_tab
3057           ,l_bl_tp_revenue_in_tab
3058           ,l_bl_tp_revenue_out_tab
3059           ,l_bl_lent_resource_cost_tab
3060           ,l_bl_tp_cost_in_tab
3061           ,l_bl_tp_cost_out_tab
3062           ,l_bl_unassigned_time_cost_tab;
3063 
3064 
3065                        l_stage :=  8500;
3066                        -- hr_utility.trace(to_char(l_stage));
3067 
3068         IF NVL(l_bl_res_asg_id_tab.count,0) =0 THEN
3069                        l_stage :=  8600;
3070                        -- hr_utility.trace(to_char(l_stage));
3071                          pa_debug.g_err_stage := l_stage||': Exiting fl_lines_task loop no rows to process';
3072                          IF P_PA_DEBUG_MODE = 'Y' THEN
3073                             pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3074                          END IF;
3075         ELSE
3076 
3077           FOR i in l_bl_res_asg_id_tab.first..l_bl_res_asg_id_tab.last LOOP
3078                        l_stage :=  8700;
3079                        -- hr_utility.trace(to_char(l_stage));
3080 
3081            /* API call changed from pa_fp_org_fcst_utils.get_utilization_details
3082               to pa_pji_util_pkg.get_utilization_dtls to get the Utilization
3083               numbers from PJI data model if PJI is installed. */
3084 
3085            pa_pji_util_pkg.get_utilization_dtls
3086            ( p_org_id                => l_org_id
3087             ,p_organization_id       => l_organization_id
3088             ,p_period_type           => l_org_fcst_period_type
3089             ,p_period_set_name       => l_period_set_name
3090             ,p_period_name           => l_bl_period_name_tab(i)
3091             ,x_utl_hours             => l_bl_utilization_hours_tab(i)
3092             ,x_utl_capacity          => l_bl_capacity_tab(i)
3093             ,x_utl_percent           => l_bl_utilization_percent_tab(i)
3094             ,x_return_status         => l_return_status
3095             ,x_err_code              => l_err_code);
3096 
3097                -- hr_utility.trace(l_err_code);
3098 
3099                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3100                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3101                                           ':Error getting utilization details'||'-'||l_err_code;
3102                   IF P_PA_DEBUG_MODE = 'Y' THEN
3103                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3104                   END IF;
3105                   pa_debug.g_err_stage := 'l_org_id                ['||l_org_id                 || ']';
3106                   IF P_PA_DEBUG_MODE = 'Y' THEN
3107                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3108                   END IF;
3109                   pa_debug.g_err_stage := 'l_organization_id       ['||l_organization_id        || ']';
3110                   IF P_PA_DEBUG_MODE = 'Y' THEN
3111                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3112                   END IF;
3113                   pa_debug.g_err_stage := 'l_org_fcst_period_type  ['||l_org_fcst_period_type   || ']';
3114                   IF P_PA_DEBUG_MODE = 'Y' THEN
3115                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3116                   END IF;
3117                   pa_debug.g_err_stage := 'l_period_set_name       ['||l_period_set_name        || ']';
3118                   IF P_PA_DEBUG_MODE = 'Y' THEN
3119                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3120                   END IF;
3121                   pa_debug.g_err_stage := 'l_bl_period_name_tab    ['||l_bl_period_name_tab(i)  || ']';
3122                   IF P_PA_DEBUG_MODE = 'Y' THEN
3123                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3124                   END IF;
3125                   rollback to org_project;
3126                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
3127                END IF;
3128 
3129                        l_stage :=  8800;
3130                        -- hr_utility.trace(to_char(l_stage));
3131 
3132            pa_fp_org_fcst_utils.get_headcount
3133            ( p_organization_id       => l_organization_id
3134             ,p_effective_date        => l_bl_start_date_tab(i)
3135             ,x_headcount             => l_bl_head_count_tab(i)
3136             ,x_return_status         => l_return_status
3137             ,x_err_code              => l_err_code);
3138 
3139                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3140                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3141                                           ':Error getting Head Count';
3142                   IF P_PA_DEBUG_MODE = 'Y' THEN
3143                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3144                   END IF;
3145                   pa_debug.g_err_stage :=
3146                                  'l_organization_id       ['||l_organization_id        ||
3147                                '] l_bl_start_date_tab     ['||l_bl_start_date_tab(i)   ||
3148                                ']';
3149                   IF P_PA_DEBUG_MODE = 'Y' THEN
3150                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3151                   END IF;
3152                   rollback to org_project;
3153                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
3154                END IF;
3155                        l_stage :=  8900;
3156                        -- hr_utility.trace(to_char(l_stage));
3157           END LOOP;
3158           CLOSE fl_lines_task;
3159 
3160                        l_stage :=  9000;
3161                        -- hr_utility.trace(to_char(l_stage));
3162                        pa_debug.g_err_stage := l_stage||': Create Budget Lines for Own Task';
3163                        IF P_PA_DEBUG_MODE = 'Y' THEN
3164                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3165                        END IF;
3166 
3167           /* Bulk insert into Budget Lines for Own Numbers Phase I */
3168         IF l_bl_res_asg_id_tab.count > 0 THEN
3169           FORALL i in l_bl_res_asg_id_tab.first..l_bl_res_asg_id_tab.last
3170             INSERT INTO pa_budget_lines
3171              ( budget_line_id                        /* FPB2 */
3172               ,budget_version_id                     /* FPB2  */
3173               ,txn_currency_code                     /* FPB4 */
3174 	      ,resource_assignment_id
3175               ,period_name
3176               ,start_date
3177               ,end_date
3178               ,quantity
3179               ,raw_cost
3180               ,burdened_cost
3181               ,lent_resource_cost
3182               ,unassigned_time_cost
3183               ,tp_cost_in
3184               ,tp_cost_out
3185               ,revenue
3186               ,borrowed_revenue
3187               ,tp_revenue_in
3188               ,tp_revenue_out
3189               ,quantity_source
3190               ,raw_cost_source
3191               ,burdened_cost_source
3192               ,revenue_source
3193               ,utilization_percent
3194               ,utilization_hours
3195               ,capacity
3196               ,head_count
3197               ,creation_date
3198               ,created_by
3199               ,last_update_login
3200               ,last_updated_by
3201               ,last_update_date
3202              ) VALUES (
3203                pa_budget_lines_s.nextval     /* FPB2 */
3204               ,l_bl_budget_version_id_tab(i) /* FPB2 */
3205               ,l_org_projfunc_currency_code  /* FPB4 */
3206               ,l_bl_res_asg_id_tab(i)
3207               ,l_bl_period_name_tab(i)
3208               ,l_bl_start_date_tab(i)
3209               ,l_bl_end_date_tab(i)
3210               ,l_bl_quantity_tab(i)
3211               ,l_bl_raw_cost_tab(i)
3212               ,l_bl_burdened_cost_tab(i)
3213               ,l_bl_lent_resource_cost_tab(i)
3214               ,l_bl_unassigned_time_cost_tab(i)
3215               ,l_bl_tp_cost_in_tab(i)
3216               ,l_bl_tp_cost_out_tab(i)
3217               ,l_bl_revenue_tab(i)
3218               ,l_bl_borrowed_revenue_tab(i)
3219               ,l_bl_tp_revenue_in_tab(i)
3220               ,l_bl_tp_revenue_out_tab(i)
3221               ,'C'
3222               ,'C'
3223               ,'C'
3224               ,'C'
3225               ,l_bl_utilization_percent_tab(i)
3226               ,l_bl_utilization_hours_tab(i)
3227               ,l_bl_capacity_tab(i)
3228               ,l_bl_head_count_tab(i)
3229               ,sysdate
3230               ,fnd_global.user_id
3231               ,fnd_global.login_id
3232               ,fnd_global.user_id
3233               ,sysdate);
3234 
3235             END IF;
3236                        l_stage :=  9100;
3237                        -- hr_utility.trace(to_char(l_stage));
3238 
3239              /* Need to update Own Level Resource Assignment
3240                 from budget Lines */
3241 
3242              SELECT nvl(sum(bl.quantity),0)
3243                    ,nvl(sum(bl.raw_cost),0)
3244                    ,nvl(sum(bl.burdened_cost),0)
3245                    ,nvl(sum(bl.lent_resource_cost),0)
3246                    ,nvl(sum(bl.unassigned_time_cost),0)
3247                    ,nvl(sum(bl.tp_cost_in),0)
3248                    ,nvl(sum(bl.tp_cost_out),0)
3249                    ,nvl(sum(bl.revenue),0)
3250                    ,nvl(sum(bl.borrowed_revenue),0)
3251                    ,nvl(sum(bl.tp_revenue_in),0)
3252                    ,nvl(sum(bl.tp_revenue_out),0)
3253                    ,nvl(round(avg(bl.utilization_percent)),0)
3254                    ,nvl(sum(bl.utilization_hours),0)
3255                    ,nvl(sum(bl.capacity),0)
3256                    ,nvl(round(avg(bl.head_count)),0)
3257                INTO budget_lines_rec(1).quantity
3258                    ,budget_lines_rec(1).raw_cost
3259                    ,budget_lines_rec(1).burdened_cost
3260                    ,budget_lines_rec(1).lent_resource_cost
3261                    ,budget_lines_rec(1).unassigned_time_cost
3262                    ,budget_lines_rec(1).tp_cost_in
3263                    ,budget_lines_rec(1).tp_cost_out
3264                    ,budget_lines_rec(1).revenue
3265                    ,budget_lines_rec(1).borrowed_revenue
3266                    ,budget_lines_rec(1).tp_revenue_in
3267                    ,budget_lines_rec(1).tp_revenue_out
3268                    ,budget_lines_rec(1).utilization_percent
3269                    ,budget_lines_rec(1).utilization_hours
3270                    ,budget_lines_rec(1).capacity
3271                    ,budget_lines_rec(1).head_count
3272               FROM pa_budget_lines bl
3273              WHERE bl.resource_assignment_id = l_own_resource_assignment_id;
3274 
3275                        l_stage :=  9200;
3276                        -- hr_utility.trace(to_char(l_stage));
3277 
3278              IF budget_lines_rec.COUNT = 1 THEN
3279                        l_stage :=  9300;
3280                        -- hr_utility.trace(to_char(l_stage));
3281                        pa_debug.g_err_stage := l_stage||': Update Resource Assignments for Own Task';
3282                        IF P_PA_DEBUG_MODE = 'Y' THEN
3283                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3284                        END IF;
3285 
3286                 UPDATE pa_resource_assignments
3287                    SET
3288                    last_update_date = sysdate
3289                   ,last_updated_by = fnd_global.user_id
3290                   ,last_update_login = fnd_global.login_id
3291                   ,total_plan_revenue = budget_lines_rec(1).revenue
3292                   ,total_plan_raw_cost = budget_lines_rec(1).raw_cost
3293                   ,total_plan_burdened_cost = budget_lines_rec(1).burdened_cost
3294                   ,total_plan_quantity = budget_lines_rec(1).quantity
3295                   ,total_borrowed_revenue = budget_lines_rec(1).borrowed_revenue
3296                   ,total_tp_revenue_in = budget_lines_rec(1).tp_revenue_in
3297                   ,total_tp_revenue_out = budget_lines_rec(1).tp_revenue_out
3298                   ,total_lent_resource_cost = budget_lines_rec(1).lent_resource_cost
3299                   ,total_tp_cost_in = budget_lines_rec(1).tp_cost_in
3300                   ,total_tp_cost_out = budget_lines_rec(1).tp_cost_out
3301                   ,total_unassigned_time_cost = budget_lines_rec(1).unassigned_time_cost
3302                   ,total_utilization_percent = budget_lines_rec(1).utilization_percent
3303                   ,total_utilization_hours = budget_lines_rec(1).utilization_hours
3304                   ,total_capacity = budget_lines_rec(1).capacity
3305                   ,total_head_count = budget_lines_rec(1).head_count
3306                 WHERE resource_assignment_id = l_own_resource_assignment_id;
3307 
3308                        l_stage :=  9400;
3309                        -- hr_utility.trace(to_char(l_stage));
3310              END IF;
3311                        l_stage :=  9500;
3312                        -- hr_utility.trace(to_char(l_stage));
3313 
3314           budget_lines_rec.DELETE;
3315           DELETE FROM pa_fin_plan_lines_tmp;
3316 
3317                        l_stage :=  9600;
3318                        -- hr_utility.trace(to_char(l_stage));
3319 
3320           FORALL i in l_bl_res_asg_id_tab.first..l_bl_res_asg_id_tab.last
3321             INSERT INTO pa_fin_plan_lines_tmp
3322             ( resource_assignment_id
3323              ,object_id
3324              ,object_type_code
3325              ,period_name
3326              ,start_date
3327              ,end_date
3328              ,currency_type
3329              ,currency_code
3330              ,source_txn_currency_code /* Bug 2796261 */
3331              ,quantity
3332              ,raw_cost
3333              ,burdened_cost
3334              ,tp_cost_in
3335              ,tp_cost_out
3336              ,lent_resource_cost
3337              ,unassigned_time_cost
3338              ,cost_adj
3339              ,revenue
3340              ,borrowed_revenue
3341              ,tp_revenue_in
3342              ,tp_revenue_out
3343              ,revenue_adj
3344              ,utilization_percent
3345              ,utilization_adj
3346              ,utilization_hours
3347              ,capacity
3348              ,head_count
3349              ,head_count_adj
3350              ,margin
3351              ,margin_percentage)
3352             SELECT
3353               l_bl_res_asg_id_tab(i)
3354              ,l_bl_res_asg_id_tab(i)
3355              ,'RES_ASSIGNMENT'
3356              ,l_bl_period_name_tab(i)
3357              ,l_bl_start_date_tab(i)
3358              ,l_bl_end_date_tab(i)
3359              ,'PROJ_FUNCTIONAL'
3360              ,l_org_projfunc_currency_code
3361              ,l_org_projfunc_currency_code  /* 2796261 */
3362              ,l_bl_quantity_tab(i)
3363              ,l_bl_raw_cost_tab(i)
3364              ,l_bl_burdened_cost_tab(i)
3365              ,l_bl_tp_cost_in_tab(i)
3366              ,l_bl_tp_cost_out_tab(i)
3367              ,l_bl_lent_resource_cost_tab(i)
3368              ,l_bl_unassigned_time_cost_tab(i)
3369              ,0
3370              ,l_bl_revenue_tab(i)
3371              ,l_bl_borrowed_revenue_tab(i)
3372              ,l_bl_tp_revenue_in_tab(i)
3373              ,l_bl_tp_revenue_out_tab(i)
3374              ,0
3375              ,l_bl_utilization_percent_tab(i)
3376              ,0
3377              ,l_bl_utilization_hours_tab(i)
3378              ,l_bl_capacity_tab(i)
3379              ,l_bl_head_count_tab(i)
3380              ,0
3381              ,DECODE(SIGN(
3382                l_bl_revenue_tab(i)+
3383                l_bl_borrowed_revenue_tab(i)+
3384                l_bl_tp_revenue_in_tab(i)-
3385                l_bl_tp_revenue_out_tab(i)),0,0,-1,0,
3386               (l_bl_revenue_tab(i)+
3387                l_bl_borrowed_revenue_tab(i)+
3388                l_bl_tp_revenue_in_tab(i)-
3389                l_bl_tp_revenue_out_tab(i)) -
3390               (l_bl_burdened_cost_tab(i)+
3391                l_bl_lent_resource_cost_tab(i)+
3392                l_bl_unassigned_time_cost_tab(i)+
3393                l_bl_tp_cost_in_tab(i)-
3394                l_bl_tp_cost_out_tab(i)))
3395              ,DECODE(SIGN(
3396                l_bl_revenue_tab(i)+
3397                l_bl_borrowed_revenue_tab(i)+
3398                l_bl_tp_revenue_in_tab(i)-
3399                l_bl_tp_revenue_out_tab(i)),0,0,-1,0,
3400               ((l_bl_revenue_tab(i)+
3401                l_bl_borrowed_revenue_tab(i)+
3402                l_bl_tp_revenue_in_tab(i)-
3403                l_bl_tp_revenue_out_tab(i)) -
3404               (l_bl_burdened_cost_tab(i)+
3405                l_bl_lent_resource_cost_tab(i)+
3406                l_bl_unassigned_time_cost_tab(i)+
3407                l_bl_tp_cost_in_tab(i)-
3408                l_bl_tp_cost_out_tab(i)))/
3409               (l_bl_revenue_tab(i)+
3410                l_bl_borrowed_revenue_tab(i)+
3411                l_bl_tp_revenue_in_tab(i)-
3412                l_bl_tp_revenue_out_tab(i)) * 100)
3413              FROM DUAL;
3414 
3415                        l_stage :=  9700;
3416                        -- hr_utility.trace(to_char(l_stage));
3417               l_records_affected := SQL%ROWCOUNT;
3418 
3419               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3420                              ': After Inserting Fin Plan Lines Tmp' ;
3421               IF P_PA_DEBUG_MODE = 'Y' THEN
3422                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3423               END IF;
3424 
3425               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3426                     ': Inserted [' || TO_CHAR(l_records_affected) ||
3427                     '] Fin Plan Lines Tmp.';
3428               IF P_PA_DEBUG_MODE = 'Y' THEN
3429                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3430               END IF;
3431 
3432           /* Populate Project Periods Denorm for Budget Lines */
3433 
3434                        l_stage :=  9800;
3435                        -- hr_utility.trace(to_char(l_stage));
3436                        pa_debug.g_err_stage := l_stage||': Populate Project Periods Denorm for Budget Lines';
3437                        IF P_PA_DEBUG_MODE = 'Y' THEN
3438                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3439                        END IF;
3440               Pa_Plan_Matrix.Maintain_Plan_Matrix
3441                ( p_amount_type_tab   => amt_rec
3442                 ,p_period_profile_id => l_period_profile_id
3443                 ,p_prior_period_flag => 'N'
3444                 ,p_commit_Flag       => 'N'
3445                 ,p_budget_version_id => l_budget_version_id
3446                 ,p_project_id        => l_project_id
3447                 ,p_debug_mode        => l_debug_mode
3448                 ,p_add_msg_in_stack  => 'Y'
3449                 ,x_return_status     => l_return_status
3450                 ,x_msg_count         => l_msg_count
3451                 ,x_msg_data          => errbuff   );
3452 
3453                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3454                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3455                                           ':Error creating Period Denorm';
3456                   IF P_PA_DEBUG_MODE = 'Y' THEN
3457                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3458                   END IF;
3459                   pa_debug.g_err_stage := 'l_period_profile_id     ['||l_period_profile_id      || ']';
3460                   IF P_PA_DEBUG_MODE = 'Y' THEN
3461                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3462                   END IF;
3463                   pa_debug.g_err_stage := 'l_budget_version_id     ['||l_budget_version_id      || ']';
3464                   IF P_PA_DEBUG_MODE = 'Y' THEN
3465                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3466                   END IF;
3467                   pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
3468                   IF P_PA_DEBUG_MODE = 'Y' THEN
3469                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3470                   END IF;
3471                   rollback to org_project;
3472                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
3473                END IF;
3474        END IF; /* l_bl_res_asg_id_tab.count <> 0 */
3475 
3476                        l_stage :=  9900;
3477                        -- hr_utility.trace(to_char(l_stage));
3478                        pa_debug.g_err_stage := l_stage||': Create Resource Assignments for Project Task';
3479                        IF P_PA_DEBUG_MODE = 'Y' THEN
3480                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3481                        END IF;
3482 
3483         /* Now one pass for Project level numbers */
3484         /* Create Resource assignment with task_id = 0 */
3485 
3486         l_proj_resource_assignment_id := NULL;
3487 
3488         pa_fp_resource_assignments_pkg.Insert_Row
3489         ( px_resource_assignment_id      => l_proj_resource_assignment_id
3490          ,p_budget_version_id            => l_budget_version_id
3491          ,p_project_id                   => l_project_id
3492          ,p_task_id                      => 0
3493          ,p_resource_list_member_id      => l_resource_list_member_id
3494          ,p_unit_of_measure              => 'HOURS'
3495          ,p_track_as_labor_flag          => 'Y'
3496          ,p_standard_bill_rate           => Null
3497          ,p_average_bill_rate            => Null
3498          ,p_average_cost_rate            => Null
3499          ,p_project_assignment_id        => -1
3500          ,p_plan_error_code              => Null
3501          ,p_total_plan_revenue           => 0
3502          ,p_total_plan_raw_cost          => 0
3503          ,p_total_plan_burdened_cost     => 0
3504          ,p_total_plan_quantity          => 0
3505          ,p_average_discount_percentage  => Null
3506          ,p_total_borrowed_revenue       => 0
3507          ,p_total_tp_revenue_in          => 0
3508          ,p_total_tp_revenue_out         => 0
3509          ,p_total_revenue_adj            => 0
3510          ,p_total_lent_resource_cost     => 0
3511          ,p_total_tp_cost_in             => 0
3512          ,p_total_tp_cost_out            => 0
3513          ,p_total_cost_adj               => 0
3514          ,p_total_unassigned_time_cost   => 0
3515          ,p_total_utilization_percent    => 0
3516          ,p_total_utilization_hours      => 0
3517          ,p_total_utilization_adj        => 0
3518          ,p_total_capacity               => 0
3519          ,p_total_head_count             => 0
3520          ,p_total_head_count_adj         => 0
3521          ,p_resource_assignment_type     => 'PROJECT'
3522          ,x_row_id                       => l_row_id
3523          ,x_return_status                => l_return_status );
3524 
3525                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3526                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3527                                           ': Error creating Proj Resource Assignment';
3528                   IF P_PA_DEBUG_MODE = 'Y' THEN
3529                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3530                   END IF;
3531                   pa_debug.g_err_stage := 'l_budget_version_id      ['||l_budget_version_id      || ']';
3532                   IF P_PA_DEBUG_MODE = 'Y' THEN
3533                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3534                   END IF;
3535                   pa_debug.g_err_stage := 'l_project_id             ['||l_project_id             || ']';
3536                   IF P_PA_DEBUG_MODE = 'Y' THEN
3537                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3538                   END IF;
3539                   pa_debug.g_err_stage := 'l_resource_list_member_id['||l_resource_list_member_id|| ']';
3540                   IF P_PA_DEBUG_MODE = 'Y' THEN
3541                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3542                   END IF;
3543                   rollback to org_project;
3544                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
3545                END IF;
3546 
3547                        l_stage := 10000;
3548                        -- hr_utility.trace(to_char(l_stage));
3549 
3550       /* Budget Lines for Project Level Task */
3551 
3552          IF NOT bl_lines_project%ISOPEN THEN
3553            OPEN bl_lines_project;
3554          ELSE
3555            CLOSE bl_lines_project;
3556            OPEN bl_lines_project;
3557          END IF;
3558 
3559                        l_stage := 10100;
3560                        -- hr_utility.trace(to_char(l_stage));
3561 
3562          l_bl_budget_version_id_tab.delete;       /* FPB2: budget_version_id */
3563          l_bl_res_asg_id_tab.delete;
3564          l_bl_start_date_tab.delete;
3565          l_bl_end_date_tab.delete;
3566          l_bl_period_name_tab.delete;
3567          l_bl_quantity_tab.delete;
3568          l_bl_raw_cost_tab.delete;
3569          l_bl_burdened_cost_tab.delete;
3570          l_bl_revenue_tab.delete;
3571          l_bl_borrowed_revenue_tab.delete;
3572          l_bl_tp_revenue_in_tab.delete;
3573          l_bl_tp_revenue_out_tab.delete;
3574          l_bl_lent_resource_cost_tab.delete;
3575          l_bl_tp_cost_in_tab.delete;
3576          l_bl_tp_cost_out_tab.delete;
3577          l_bl_unassigned_time_cost_tab.delete;
3578          l_bl_utilization_percent_tab.delete;
3579          l_bl_utilization_hours_tab.delete;
3580          l_bl_capacity_tab.delete;
3581          l_bl_head_count_tab.delete;
3582 
3583          FETCH bl_lines_project BULK COLLECT INTO
3584            l_bl_budget_version_id_tab  /* FPB2: budget_version_id */
3585           ,l_bl_res_asg_id_tab
3586           ,l_bl_period_name_tab
3587           ,l_bl_start_date_tab
3588           ,l_bl_end_date_tab
3589           ,l_bl_quantity_tab
3590           ,l_bl_raw_cost_tab
3591           ,l_bl_burdened_cost_tab
3592           ,l_bl_revenue_tab
3593           ,l_bl_borrowed_revenue_tab
3594           ,l_bl_tp_revenue_in_tab
3595           ,l_bl_tp_revenue_out_tab
3596           ,l_bl_lent_resource_cost_tab
3597           ,l_bl_tp_cost_in_tab
3598           ,l_bl_tp_cost_out_tab
3599           ,l_bl_unassigned_time_cost_tab
3600           ,l_bl_utilization_percent_tab
3601           ,l_bl_utilization_hours_tab
3602           ,l_bl_capacity_tab
3603           ,l_bl_head_count_tab;
3604 
3605                        l_stage := 10200;
3606                        -- hr_utility.trace(to_char(l_stage));
3607        IF NVL(l_bl_res_asg_id_tab.count,0) =0 THEN
3608                        l_stage := 10300;
3609                        -- hr_utility.trace(to_char(l_stage));
3610        ELSE
3611           /* Bulk insert into Budget Lines for Project Numbers */
3612                        pa_debug.g_err_stage := l_stage||': Create Budget Lines for Project Level Task';
3613                        IF P_PA_DEBUG_MODE = 'Y' THEN
3614                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3615                        END IF;
3616           FORALL i in l_bl_res_asg_id_tab.first..l_bl_res_asg_id_tab.last
3617             INSERT INTO pa_budget_lines
3618              ( budget_line_id                 /* FPB2 budget_line_id */
3619               ,budget_version_id              /* FPB2 */
3620               ,txn_currency_code              /* FPB4 */
3621 	      ,resource_assignment_id
3622               ,period_name
3623               ,start_date
3624               ,end_date
3625               ,quantity
3626               ,raw_cost
3627               ,burdened_cost
3628               ,lent_resource_cost
3629               ,unassigned_time_cost
3630               ,tp_cost_in
3631               ,tp_cost_out
3632               ,revenue
3633               ,borrowed_revenue
3634               ,tp_revenue_in
3635               ,tp_revenue_out
3636               ,quantity_source
3637               ,raw_cost_source
3638               ,burdened_cost_source
3639               ,revenue_source
3640               ,utilization_percent
3641               ,utilization_hours
3642               ,capacity
3643               ,head_count
3644               ,creation_date
3645               ,created_by
3646               ,last_update_login
3647               ,last_updated_by
3648               ,last_update_date
3649              ) VALUES (
3650                pa_budget_lines_s.nextval          /* FPB2: budget_line_id */
3651               ,l_bl_budget_version_id_tab(i)      /* FPB2: budget_version_id */
3652               ,l_org_projfunc_currency_code       /* FPB4 */
3653               ,l_bl_res_asg_id_tab(i)
3654               ,l_bl_period_name_tab(i)
3655               ,l_bl_start_date_tab(i)
3656               ,l_bl_end_date_tab(i)
3657               ,l_bl_quantity_tab(i)
3658               ,l_bl_raw_cost_tab(i)
3659               ,l_bl_burdened_cost_tab(i)
3660               ,l_bl_lent_resource_cost_tab(i)
3661               ,l_bl_unassigned_time_cost_tab(i)
3662               ,l_bl_tp_cost_in_tab(i)
3663               ,l_bl_tp_cost_out_tab(i)
3664               ,l_bl_revenue_tab(i)
3665               ,l_bl_borrowed_revenue_tab(i)
3666               ,l_bl_tp_revenue_in_tab(i)
3667               ,l_bl_tp_revenue_out_tab(i)
3668               ,'C'
3669               ,'C'
3670               ,'C'
3671               ,'C'
3672               ,l_bl_utilization_percent_tab(i)
3673               ,l_bl_utilization_hours_tab(i)
3674               ,l_bl_capacity_tab(i)
3675               ,l_bl_head_count_tab(i)
3676               ,sysdate
3677               ,fnd_global.user_id
3678               ,fnd_global.login_id
3679               ,fnd_global.user_id
3680               ,sysdate);
3681 
3682                        l_stage := 10400;
3683                        -- hr_utility.trace(to_char(l_stage));
3684 
3685               /* Need to update Project Level Resource Assignment
3686                  from budget Lines */
3687 
3688               SELECT nvl(sum(bl.quantity),0)
3689                     ,nvl(sum(bl.raw_cost),0)
3690                     ,nvl(sum(bl.burdened_cost),0)
3691                     ,nvl(sum(bl.lent_resource_cost),0)
3692                     ,nvl(sum(bl.unassigned_time_cost),0)
3693                     ,nvl(sum(bl.tp_cost_in),0)
3694                     ,nvl(sum(bl.tp_cost_out),0)
3695                     ,nvl(sum(bl.revenue),0)
3696                     ,nvl(sum(bl.borrowed_revenue),0)
3697                     ,nvl(sum(bl.tp_revenue_in),0)
3698                     ,nvl(sum(bl.tp_revenue_out),0)
3699                     ,nvl(round(avg(bl.utilization_percent)),0)
3700                     ,nvl(sum(bl.utilization_hours),0)
3701                     ,nvl(sum(bl.capacity),0)
3702                     ,nvl(round(avg(bl.head_count)),0)
3703                 INTO budget_lines_rec(1).quantity
3704                     ,budget_lines_rec(1).raw_cost
3705                     ,budget_lines_rec(1).burdened_cost
3706                     ,budget_lines_rec(1).lent_resource_cost
3707                     ,budget_lines_rec(1).unassigned_time_cost
3708                     ,budget_lines_rec(1).tp_cost_in
3709                     ,budget_lines_rec(1).tp_cost_out
3710                     ,budget_lines_rec(1).revenue
3711                     ,budget_lines_rec(1).borrowed_revenue
3712                     ,budget_lines_rec(1).tp_revenue_in
3713                     ,budget_lines_rec(1).tp_revenue_out
3714                     ,budget_lines_rec(1).utilization_percent
3715                     ,budget_lines_rec(1).utilization_hours
3716                     ,budget_lines_rec(1).capacity
3717                     ,budget_lines_rec(1).head_count
3718                FROM pa_budget_lines bl
3719               WHERE bl.resource_assignment_id = l_proj_resource_assignment_id;
3720 
3721                        l_stage := 10500;
3722                        -- hr_utility.trace(to_char(l_stage));
3723 
3724      IF budget_lines_rec.COUNT = 1 THEN
3725                        l_stage := 10600;
3726                        -- hr_utility.trace(to_char(l_stage));
3727                        pa_debug.g_err_stage := l_stage||': Update Resource Assignments for Project Task';
3728                        IF P_PA_DEBUG_MODE = 'Y' THEN
3729                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3730                        END IF;
3731      UPDATE pa_resource_assignments
3732         SET
3733         last_update_date = sysdate
3734        ,last_updated_by = fnd_global.user_id
3735        ,last_update_login = fnd_global.login_id
3736        ,total_plan_revenue = budget_lines_rec(1).revenue
3737        ,total_plan_raw_cost = budget_lines_rec(1).raw_cost
3738        ,total_plan_burdened_cost = budget_lines_rec(1).burdened_cost
3739        ,total_plan_quantity = budget_lines_rec(1).quantity
3740        ,total_borrowed_revenue = budget_lines_rec(1).borrowed_revenue
3741        ,total_tp_revenue_in = budget_lines_rec(1).tp_revenue_in
3742        ,total_tp_revenue_out = budget_lines_rec(1).tp_revenue_out
3743        ,total_lent_resource_cost = budget_lines_rec(1).lent_resource_cost
3744        ,total_tp_cost_in = budget_lines_rec(1).tp_cost_in
3745        ,total_tp_cost_out = budget_lines_rec(1).tp_cost_out
3746        ,total_unassigned_time_cost = budget_lines_rec(1).unassigned_time_cost
3747        ,total_utilization_percent = budget_lines_rec(1).utilization_percent
3748        ,total_utilization_hours = budget_lines_rec(1).utilization_hours
3749        ,total_capacity = budget_lines_rec(1).capacity
3750        ,total_head_count = budget_lines_rec(1).head_count
3751        WHERE resource_assignment_id = l_proj_resource_assignment_id;
3752 
3753                        l_stage := 10700;
3754                        -- hr_utility.trace(to_char(l_stage));
3755      END IF;
3756 
3757      IF budget_lines_rec.COUNT = 1 THEN
3758                        l_stage := 10800;
3759                        -- hr_utility.trace(to_char(l_stage));
3760                        pa_debug.g_err_stage := l_stage||': Update Budget Version for Project Level Numbers';
3761                        IF P_PA_DEBUG_MODE = 'Y' THEN
3762                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3763                        END IF;
3764 
3765         UPDATE pa_budget_versions
3766            SET
3767            record_version_number = record_version_number+1
3768           ,version_name = DECODE(version_name,null,l_bv_version_name
3769                                  ||'-'||to_char(l_budget_version_id),
3770                                  version_name)
3771           ,last_update_date = sysdate
3772           ,last_updated_by = fnd_global.user_id
3773           ,last_update_login = fnd_global.login_id
3774           ,revenue = budget_lines_rec(1).revenue
3775           ,raw_cost = budget_lines_rec(1).raw_cost
3776           ,burdened_cost = budget_lines_rec(1).burdened_cost
3777           ,labor_quantity = budget_lines_rec(1).quantity
3778           ,total_borrowed_revenue = budget_lines_rec(1).borrowed_revenue
3779           ,total_tp_revenue_in = budget_lines_rec(1).tp_revenue_in
3780           ,total_tp_revenue_out = budget_lines_rec(1).tp_revenue_out
3781           ,total_lent_resource_cost = budget_lines_rec(1).lent_resource_cost
3782           ,total_tp_cost_in = budget_lines_rec(1).tp_cost_in
3783           ,total_tp_cost_out = budget_lines_rec(1).tp_cost_out
3784           ,total_unassigned_time_cost = budget_lines_rec(1).unassigned_time_cost
3785           ,total_utilization_percent = budget_lines_rec(1).utilization_percent
3786           ,total_utilization_hours = budget_lines_rec(1).utilization_hours
3787           ,total_capacity = budget_lines_rec(1).capacity
3788           ,total_head_count = budget_lines_rec(1).head_count
3789           ,plan_processing_code = 'G'
3790        WHERE budget_version_id = l_budget_version_id;
3791 
3792                        l_stage := 10900;
3793                        -- hr_utility.trace(to_char(l_stage));
3794      END IF;
3795 
3796                        l_stage := 11000;
3797                        -- hr_utility.trace(to_char(l_stage));
3798          budget_lines_rec.DELETE;
3799 
3800          DELETE FROM pa_fin_plan_lines_tmp;
3801 
3802                        l_stage := 11100;
3803                        -- hr_utility.trace(to_char(l_stage));
3804 
3805          FORALL i in l_bl_res_asg_id_tab.first..l_bl_res_asg_id_tab.last
3806            INSERT INTO pa_fin_plan_lines_tmp
3807             ( resource_assignment_id
3808              ,object_id
3809              ,object_type_code
3810              ,period_name
3811              ,start_date
3812              ,end_date
3813              ,currency_type
3814              ,currency_code
3815              ,source_txn_currency_code /* Bug 2796261 */
3816              ,quantity
3817              ,raw_cost
3818              ,burdened_cost
3819              ,tp_cost_in
3820              ,tp_cost_out
3821              ,lent_resource_cost
3822              ,unassigned_time_cost
3823              ,cost_adj
3824              ,revenue
3825              ,borrowed_revenue
3826              ,tp_revenue_in
3827              ,tp_revenue_out
3828              ,revenue_adj
3829              ,utilization_percent
3830              ,utilization_adj
3831              ,utilization_hours
3832              ,capacity
3833              ,head_count
3834              ,head_count_adj
3835              ,margin
3836              ,margin_percentage)
3837             SELECT
3838               l_bl_res_asg_id_tab(i)
3839              ,l_bl_res_asg_id_tab(i)
3840              ,'RES_ASSIGNMENT'
3841              ,l_bl_period_name_tab(i)
3842              ,l_bl_start_date_tab(i)
3843              ,l_bl_end_date_tab(i)
3844              ,'PROJ_FUNCTIONAL'
3845              ,l_org_projfunc_currency_code
3846              ,l_org_projfunc_currency_code  /* 2796261 */
3847              ,l_bl_quantity_tab(i)
3848              ,l_bl_raw_cost_tab(i)
3849              ,l_bl_burdened_cost_tab(i)
3850              ,l_bl_tp_cost_in_tab(i)
3851              ,l_bl_tp_cost_out_tab(i)
3852              ,l_bl_lent_resource_cost_tab(i)
3853              ,l_bl_unassigned_time_cost_tab(i)
3854              ,0
3855              ,l_bl_revenue_tab(i)
3856              ,l_bl_borrowed_revenue_tab(i)
3857              ,l_bl_tp_revenue_in_tab(i)
3858              ,l_bl_tp_revenue_out_tab(i)
3859              ,0
3860              ,l_bl_utilization_percent_tab(i)
3861              ,0
3862              ,l_bl_utilization_hours_tab(i)
3863              ,l_bl_capacity_tab(i)
3864              ,l_bl_head_count_tab(i)
3865              ,0
3866              ,DECODE(SIGN(
3867                l_bl_revenue_tab(i)+
3868                l_bl_borrowed_revenue_tab(i)+
3869                l_bl_tp_revenue_in_tab(i)-
3870                l_bl_tp_revenue_out_tab(i)),0,0,-1,0,
3871               (l_bl_revenue_tab(i)+
3872                l_bl_borrowed_revenue_tab(i)+
3873                l_bl_tp_revenue_in_tab(i)-
3874                l_bl_tp_revenue_out_tab(i)) -
3875               (l_bl_burdened_cost_tab(i)+
3876                l_bl_lent_resource_cost_tab(i)+
3877                l_bl_unassigned_time_cost_tab(i)+
3878                l_bl_tp_cost_in_tab(i)-
3879                l_bl_tp_cost_out_tab(i)))
3880              ,DECODE(SIGN(
3881                l_bl_revenue_tab(i)+
3882                l_bl_borrowed_revenue_tab(i)+
3883                l_bl_tp_revenue_in_tab(i)-
3884                l_bl_tp_revenue_out_tab(i)),0,0,-1,0,
3885               ((l_bl_revenue_tab(i)+
3886                l_bl_borrowed_revenue_tab(i)+
3887                l_bl_tp_revenue_in_tab(i)-
3888                l_bl_tp_revenue_out_tab(i)) -
3889               (l_bl_burdened_cost_tab(i)+
3890                l_bl_lent_resource_cost_tab(i)+
3891                l_bl_unassigned_time_cost_tab(i)+
3892                l_bl_tp_cost_in_tab(i)-
3893                l_bl_tp_cost_out_tab(i)))/
3894               (l_bl_revenue_tab(i)+
3895                l_bl_borrowed_revenue_tab(i)+
3896                l_bl_tp_revenue_in_tab(i)-
3897                l_bl_tp_revenue_out_tab(i)) * 100)
3898              FROM DUAL;
3899 
3900                        l_stage :=  11200;
3901                        -- hr_utility.trace(to_char(l_stage));
3902 
3903               l_records_affected := SQL%ROWCOUNT;
3904 
3905               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3906                              ': After Inserting Fin Plan Lines Tmp' ;
3907               IF P_PA_DEBUG_MODE = 'Y' THEN
3908                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3909               END IF;
3910 
3911               pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3912                     ': Inserted [' || TO_CHAR(l_records_affected) ||
3913                     '] Fin Plan Lines Tmp.';
3914               IF P_PA_DEBUG_MODE = 'Y' THEN
3915                  pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3916               END IF;
3917 
3918                        l_stage :=  11300;
3919                        -- hr_utility.trace(to_char(l_stage));
3920 
3921           /* Populate Project Periods Denorm for budget lines for Project */
3922               Pa_Plan_Matrix.Maintain_Plan_Matrix
3923                ( p_amount_type_tab   => amt_rec
3924                 ,p_period_profile_id => l_period_profile_id
3925                 ,p_prior_period_flag => 'N'
3926                 ,p_commit_Flag       => 'N'
3927                 ,x_return_status     => l_return_status
3928                 ,x_msg_count         => l_msg_count
3929                 ,x_msg_data          => errbuff
3930                 ,p_budget_version_id => l_budget_version_id
3931                 ,p_project_id        => l_project_id);
3932 
3933                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3934                   pa_debug.g_err_stage := TO_CHAR(l_stage) ||
3935                                           ':Error creating Period Denorm';
3936                   IF P_PA_DEBUG_MODE = 'Y' THEN
3937                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3938                   END IF;
3939                   pa_debug.g_err_stage :=
3940                                  'l_period_profile_id     ['||l_period_profile_id      ||
3941                                '] l_budget_version_id     ['||l_budget_version_id      ||
3942                                '] l_project_id            ['||l_project_id             ||
3943                                ']';
3944                   IF P_PA_DEBUG_MODE = 'Y' THEN
3945                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3946                   END IF;
3947                   rollback to org_project;
3948                   RAISE pa_fp_org_fcst_gen_pub.error_reloop;
3949                END IF;
3950         END IF;
3951                        l_stage :=  11400;
3952                        -- hr_utility.trace(to_char(l_stage));
3953 
3954         IF p_budget_version_id IS NOT NULL THEN
3955                        l_stage :=  11500;
3956                        -- hr_utility.trace(to_char(l_stage));
3957                        pa_debug.g_err_stage := l_stage||': Process Complete for budget version -- EXITING';
3958                        IF P_PA_DEBUG_MODE = 'Y' THEN
3959                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3960                        END IF;
3961            COMMIT;
3962            EXIT; -- need to force exit when generating for a budget version.
3963         END IF;
3964                        l_stage :=  11600;
3965                        -- hr_utility.trace(to_char(l_stage));
3966    EXCEPTION
3967         WHEN pa_fp_org_fcst_gen_pub.error_reloop THEN
3968              -- hr_utility.trace('UserDefined Skipping - '||to_char(l_stage)||'-'||SQLERRM);
3969              pa_debug.g_err_stage := l_stage||': UserDefined Skipping - '||'['||SQLERRM||']';
3970              IF P_PA_DEBUG_MODE = 'Y' THEN
3971                 pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3972              END IF;
3973              rollback to org_project;
3974                        IF l_budget_version_id > 0 THEN
3975                           l_budget_version_in_error := budget_version_in_error(l_budget_version_id);
3976                           pa_debug.g_err_stage := l_stage||': Budget Version has errored - '
3977                                                          ||'['||l_budget_version_id||']';
3978                           IF P_PA_DEBUG_MODE = 'Y' THEN
3979                              pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3980                           END IF;
3981                        END IF;
3982         WHEN OTHERS THEN
3983              -- hr_utility.trace('UnExpected Skipping - '||to_char(l_stage)||'-'||SQLERRM);
3984              pa_debug.g_err_stage := l_stage||': UnExpected Skipping - '||'['||SQLERRM||']';
3985              IF P_PA_DEBUG_MODE = 'Y' THEN
3986                 pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3987              END IF;
3988              pa_debug.g_err_stage := 'l_organization_id       ['||l_organization_id        || ']';
3989                   IF P_PA_DEBUG_MODE = 'Y' THEN
3990                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3991                   END IF;
3992              pa_debug.g_err_stage := 'l_budget_version_id     ['||l_budget_version_id      || ']';
3993                   IF P_PA_DEBUG_MODE = 'Y' THEN
3994                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3995                   END IF;
3996              pa_debug.g_err_stage := 'l_project_id            ['||l_project_id             || ']';
3997                   IF P_PA_DEBUG_MODE = 'Y' THEN
3998                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
3999                   END IF;
4000              pa_debug.g_err_stage := 'l_period_profile_id     ['||l_period_profile_id      || ']';
4001                   IF P_PA_DEBUG_MODE = 'Y' THEN
4002                      pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4003                   END IF;
4004                   rollback to org_project;
4005                        IF l_budget_version_id > 0 THEN
4006                           l_budget_version_in_error := budget_version_in_error(l_budget_version_id);
4007                           pa_debug.g_err_stage := l_stage||': Budget Version has errored - '
4008                                                          ||'['||l_budget_version_id||']';
4009                           IF P_PA_DEBUG_MODE = 'Y' THEN
4010                              pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4011                           END IF;
4012                        END IF;
4013    END;
4014    COMMIT;
4015    END LOOP; /* Org Loop */
4016 
4017     IF p_budget_version_id IS NULL THEN
4018                        l_stage :=  11700;
4019                        -- hr_utility.trace(to_char(l_stage));
4020       IF p_starting_organization_id IS NULL THEN
4021             CLOSE specific_org_only;
4022                        l_stage :=  11800;
4023                        -- hr_utility.trace(to_char(l_stage));
4024 
4025 	       -- R12 MOAC 4447573
4026 	       If P_PA_DEBUG_MODE = 'Y' THEN
4027         	   PA_DEBUG.Log_Message(p_message => 'Calling FND_REQUEST.set_org_id{'||l_org_id||'}');
4028      	       End If;
4029 	       FND_REQUEST.set_org_id(l_org_id);
4030 	       -- end of  R12 MOAC 4447573
4031 
4032                l_request_id := FND_REQUEST.submit_request
4033                (application                =>   'PA',
4034                 program                    =>   'PAFPEXRP',
4035                 description                =>   'PRC: List Organization Forecast Exceptions',
4036                 start_time                 =>   NULL,
4037                 sub_request                =>   false,
4038                 argument1                  =>   l_org_id,             -- P_ORG_ID
4039                 argument2                  =>   '02',                 -- P_SELECT_CRITERIA
4040                 argument3                  =>   NULL,                 -- P_PROJECT_FLAG
4041                 argument4                  =>   NULL,                 -- P_PROJECT_ID
4042                 argument5                  =>   NULL,                 -- P_ASSIGNMENT_ID
4043                 argument6                  =>   1,                    -- P_ORGANIZATION_FLAG
4044                 argument7                  =>   l_organization_id,    -- P_ORGANIZATION_ID
4045                 argument8                  =>   NULL,                 -- P_START_ORGANIZATION_FLAG
4046                 argument9                  =>   NULL);                -- P_START_ORGANIZATION_ID
4047             IF l_request_id = 0 then
4048                 l_return_status := FND_API.G_RET_STS_ERROR;
4049                 IF P_PA_DEBUG_MODE = 'Y' THEN
4050                    pa_debug.write_file('gen_org_fcst: ' || 'Report not successful [PAFPEXRP] l_request_id=0; ERROR');
4051                 END IF;
4052             ELSE
4053                pa_debug.g_err_stage := 'Exception Report Request Id is '||TO_CHAR(l_request_id);
4054                IF P_PA_DEBUG_MODE = 'Y' THEN
4055                   pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4056                END IF;
4057             END IF;
4058       ELSE
4059             CLOSE org_hierarchy;
4060                        l_stage :=  11900;
4061                        -- hr_utility.trace(to_char(l_stage));
4062 
4063 	       -- R12 MOAC 4447573
4064 	       If P_PA_DEBUG_MODE = 'Y' THEN
4065         	   PA_DEBUG.Log_Message(p_message => 'Calling FND_REQUEST.set_org_id{'||l_org_id||'}');
4066      	       End If;
4067 	       FND_REQUEST.set_org_id(l_org_id);
4068 	       -- end of  R12 MOAC 4447573
4069 
4070                l_request_id := FND_REQUEST.submit_request
4071                (application                =>   'PA',
4072                 program                    =>   'PAFPEXRP',
4073                 description                =>   'PRC: List Organization Forecast Exceptions',
4074                 start_time                 =>   NULL,
4075                 sub_request                =>   false,
4076                 argument1                  =>   l_org_id,                     -- P_ORG_ID
4077                 argument2                  =>   '03',                         -- P_SELECT_CRITERIA
4078                 argument3                  =>   NULL,                         -- P_PROJECT_FLAG
4079                 argument4                  =>   NULL,                         -- P_PROJECT_ID
4080                 argument5                  =>   NULL,                         -- P_ASSIGNMENT_ID
4081                 argument6                  =>   NULL,                         -- P_ORGANIZATION_FLAG
4082                 argument7                  =>   NULL,                         -- P_ORGANIZATION_ID
4083                 argument8                  =>   1,                            -- P_START_ORGANIZATION_FLAG
4084                 argument9                  =>   p_starting_organization_id);  -- P_START_ORGANIZATION_ID
4085             IF l_request_id = 0 then
4086                 l_return_status := FND_API.G_RET_STS_ERROR;
4087                 IF P_PA_DEBUG_MODE = 'Y' THEN
4088                    pa_debug.write_file('gen_org_fcst: ' || 'Report not successful [PAFPEXRP] l_request_id=0; ERROR');
4089                 END IF;
4090             ELSE
4091                pa_debug.g_err_stage := 'Exception Report Request Id is '||TO_CHAR(l_request_id);
4092                IF P_PA_DEBUG_MODE = 'Y' THEN
4093                   pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4094                END IF;
4095             END IF;
4096       END IF;
4097     ELSE
4098                        l_stage :=  12000;
4099                        -- hr_utility.trace(to_char(l_stage));
4100 
4101 	       -- R12 MOAC 4447573
4102 	       If P_PA_DEBUG_MODE = 'Y' THEN
4103         	   PA_DEBUG.Log_Message(p_message => 'Calling FND_REQUEST.set_org_id{'||l_org_id||'}');
4104      	       End If;
4105 	       FND_REQUEST.set_org_id(l_org_id);
4106 	       -- end of  R12 MOAC 4447573
4107 
4108                l_request_id := FND_REQUEST.submit_request
4109                (application                =>   'PA',
4110                 program                    =>   'PAFPEXRP',
4111                 description                =>   'PRC: List Organization Forecast Exceptions',
4112                 start_time                 =>   NULL,
4113                 sub_request                =>   false,
4114                 argument1                  =>   l_org_id,             -- P_ORG_ID
4115                 argument2                  =>   '02',                 -- P_SELECT_CRITERIA
4116                 argument3                  =>   NULL,                 -- P_PROJECT_FLAG
4117                 argument4                  =>   NULL,                 -- P_PROJECT_ID
4118                 argument5                  =>   NULL,                 -- P_ASSIGNMENT_ID
4119                 argument6                  =>   1,                    -- P_ORGANIZATION_FLAG
4120                 argument7                  =>   l_organization_id,    -- P_ORGANIZATION_ID
4121                 argument8                  =>   NULL,                 -- P_START_ORGANIZATION_FLAG
4122                 argument9                  =>   NULL);                -- P_START_ORGANIZATION_ID
4123 
4124             IF l_request_id = 0 then
4125                 l_return_status := FND_API.G_RET_STS_ERROR;
4126                 IF P_PA_DEBUG_MODE = 'Y' THEN
4127                    pa_debug.write_file('gen_org_fcst: ' || 'Report not successful [PAFPEXRP] l_request_id=0; ERROR');
4128                 END IF;
4129             ELSE
4130                pa_debug.g_err_stage := 'Exception Report Request Id is '||TO_CHAR(l_request_id);
4131                IF P_PA_DEBUG_MODE = 'Y' THEN
4132                   pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4133                END IF;
4134             END IF;
4135 
4136 
4137     END IF; -- p_budget_version_id IS NULL
4138 
4139     pa_debug.g_err_stage := TO_CHAR(l_stage)||': End of Generation Process';
4140     IF P_PA_DEBUG_MODE = 'Y' THEN
4141        pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4142     END IF;
4143     pa_debug.reset_err_stack;
4144 
4145  EXCEPTION
4146    WHEN OTHERS THEN
4147 	   FND_MSG_PUB.add_exc_msg
4148            ( p_pkg_name       => 'PA_FP_ORG_FCST_GEN_PKG.gen_org_fcst'
4149             ,p_procedure_name => PA_DEBUG.G_Err_Stack);
4150 
4151            retcode         := FND_API.G_RET_STS_UNEXP_ERROR;
4152            errbuff         := TO_CHAR(l_stage)||'['||SQLERRM||']';
4153 
4154                        -- hr_utility.trace(to_char(l_stage)||'-'||SQLERRM);
4155                        pa_debug.g_err_stage := TO_CHAR(l_stage)||'['||SQLERRM||']';
4156                        IF P_PA_DEBUG_MODE = 'Y' THEN
4157                           pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4158                        END IF;
4159                        IF l_budget_version_id > 0 THEN
4160                           l_budget_version_in_error := budget_version_in_error(l_budget_version_id);
4161                           pa_debug.g_err_stage := l_stage||': Budget Version has errored - '
4162                                                          ||'['||l_budget_version_id||']';
4163                           IF P_PA_DEBUG_MODE = 'Y' THEN
4164                              pa_debug.write_file('gen_org_fcst: ' || pa_debug.g_err_stage);
4165                           END IF;
4166                        END IF;
4167                        pa_debug.reset_err_stack;
4168                        RAISE;
4169  END gen_org_fcst;
4170 
4171 
4172 /*************************************************************************
4173 sgoteti 03/03/2005.This API was previously in PAFPCPFB.pls, Copied it here as this will be used
4174 only in Org Forecasting Context. The code is copied without any change from the version
4175 115.196 of PAFPCPFB.pls
4176 **************************************************************************/
4177 PROCEDURE create_res_task_maps(
4178           p_source_project_id         IN      NUMBER
4179          ,p_target_project_id         IN      NUMBER
4180          ,p_source_plan_version_id    IN      NUMBER
4181          ,p_adj_percentage            IN      NUMBER
4182          ,p_copy_mode                 IN      VARCHAR2 /* Bug 2920954 */
4183          ,p_calling_module            IN      VARCHAR2 /* Bug 2920954 */
4184          ,p_shift_days                IN      NUMBER  -- 3/28/2004 FP M Dev Phase II Effort
4185          ,x_return_status             OUT     NOCOPY VARCHAR2  --File.Sql.39 bug 4440895
4186          ,x_msg_count                 OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
4187          ,x_msg_data                  OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4188 
4189 AS
4190 
4191    l_msg_count       NUMBER := 0;
4192    l_data            VARCHAR2(2000);
4193    l_msg_data        VARCHAR2(2000);
4194    l_error_msg_code  VARCHAR2(2000);
4195    l_msg_index_out   NUMBER;
4196    l_return_status   VARCHAR2(2000);
4197    l_debug_mode      VARCHAR2(30);
4198    l_adj_percentage  NUMBER;
4199    l_shift_days      NUMBER;
4200 
4201    l_periods         NUMBER := 0;
4202    l_start_date      DATE;
4203    l_err_code        NUMBER;
4204    l_err_stage       VARCHAR2(2000);
4205    l_err_stack       VARCHAR2(2000);
4206 
4207    TYPE ra_map_tmp_tbl1_type IS TABLE OF
4208       pa_fp_ra_map_tmp.source_res_assignment_id%TYPE
4209    INDEX BY BINARY_INTEGER;
4210 
4211    TYPE ra_map_tmp_tbl2_type IS TABLE OF
4212       pa_fp_ra_map_tmp.target_res_assignment_id%TYPE
4213    INDEX BY BINARY_INTEGER;
4214 
4215    TYPE ra_map_tmp_tbl3_type IS TABLE OF
4216       pa_fp_ra_map_tmp.resource_assignment_id%TYPE
4217    INDEX BY BINARY_INTEGER;
4218 
4219    TYPE ra_map_tmp_tbl4_type IS TABLE OF
4220       pa_fp_ra_map_tmp.source_task_id%TYPE
4221    INDEX BY BINARY_INTEGER;
4222 
4223    TYPE ra_map_tmp_tbl5_type IS TABLE OF
4224          pa_fp_ra_map_tmp.target_task_id%TYPE
4225    INDEX BY BINARY_INTEGER;
4226 
4227 --Added the following table types for bug 3354518
4228 
4229    TYPE ra_map_tmp_tbl6_type IS TABLE OF
4230       pa_fp_ra_map_tmp.system_reference1%TYPE
4231    INDEX BY BINARY_INTEGER;
4232 
4233    TYPE ra_map_tmp_tbl7_type IS TABLE OF
4234       pa_fp_ra_map_tmp.system_reference2%TYPE
4235    INDEX BY BINARY_INTEGER;
4236 
4237    TYPE ra_map_tmp_tbl8_type IS TABLE OF
4238       pa_fp_ra_map_tmp.system_reference3%TYPE
4239    INDEX BY BINARY_INTEGER;
4240 
4241    TYPE ra_map_tmp_tbl9_type IS TABLE OF
4242       pa_fp_ra_map_tmp.planning_start_date%TYPE
4243    INDEX BY BINARY_INTEGER;
4244 
4245    TYPE ra_map_tmp_tbl10_type IS TABLE OF
4246          pa_fp_ra_map_tmp.planning_end_date%TYPE
4247    INDEX BY BINARY_INTEGER;
4248 
4249    TYPE ra_map_tmp_tbl11_type IS TABLE OF
4250       pa_fp_ra_map_tmp.schedule_start_date%TYPE
4251    INDEX BY BINARY_INTEGER;
4252 
4253    TYPE ra_map_tmp_tbl12_type IS TABLE OF
4254          pa_fp_ra_map_tmp.schedule_end_date%TYPE
4255    INDEX BY BINARY_INTEGER;
4256 
4257   -- Declared for bug 3615617
4258    TYPE ra_map_tmp_tbl13_type IS TABLE OF
4259          pa_fp_ra_map_tmp.system_reference4%TYPE
4260    INDEX BY BINARY_INTEGER;
4261 
4262    l_ra_map_tmp_tbl1 ra_map_tmp_tbl1_type;
4263    l_ra_map_tmp_tbl2 ra_map_tmp_tbl2_type;
4264    l_ra_map_tmp_tbl3 ra_map_tmp_tbl3_type;
4265    l_ra_map_tmp_tbl4 ra_map_tmp_tbl4_type;
4266    l_ra_map_tmp_tbl5 ra_map_tmp_tbl5_type;
4267 
4268 --Declared the following pl/sql tbls for bug 3354518
4269    l_ra_map_tmp_tbl6  ra_map_tmp_tbl6_type;
4270    l_ra_map_tmp_tbl7  ra_map_tmp_tbl7_type;
4271    l_ra_map_tmp_tbl8  ra_map_tmp_tbl8_type;
4272    l_ra_map_tmp_tbl9  ra_map_tmp_tbl9_type;
4273    l_ra_map_tmp_tbl10 ra_map_tmp_tbl10_type;
4274    l_ra_map_tmp_tbl11 ra_map_tmp_tbl11_type;
4275    l_ra_map_tmp_tbl12 ra_map_tmp_tbl12_type;
4276   -- Declared for bug 3615617
4277    l_ra_map_tmp_tbl13 ra_map_tmp_tbl13_type;
4278 
4279    l_source_project_id          pa_projects.project_id%TYPE;
4280    l_target_project_id          pa_projects.project_id%TYPE;
4281    l_source_plan_level_code     pa_proj_fp_options.all_fin_plan_level_code%TYPE ;
4282    l_target_struct_version_id   pa_proj_element_versions.element_version_id%TYPE;
4283    l_source_struct_version_id   pa_proj_element_versions.element_version_id%TYPE;
4284    l_target_time_phased_code    pa_proj_fp_options.all_time_phased_code%TYPE; -- bug 3841942
4285 
4286 
4287    -- cursor same_projects_cur is going to be used when the source and target projects are same
4288 
4289   CURSOR same_projects_cur IS
4290       SELECT resource_assignment_id
4291              ,pa_resource_assignments_s.nextval
4292              ,parent_assignment_id
4293              ,task_id
4294              ,task_id
4295              --Start of Changes for bug 3354518
4296              ,NULL
4297              ,NULL
4298              ,project_assignment_id
4299              ,planning_start_date
4300              ,planning_end_date
4301              ,schedule_start_date
4302              ,schedule_end_date
4303              --End of Changes for bug 3354518
4304              ,resource_list_member_id  -- bug 3615617  TARGET_RLM_ID
4305       FROM   pa_resource_assignments
4306       WHERE  budget_version_id = p_source_plan_version_id;
4307 
4308 
4309   /* Bug 2920954 - This cursor is used for FINPLAN when copy mode is B.
4310      In this case, we copy only the ras with amounts. Since this cursor
4311      is used only for baselie case, we dont have to check adj percentage
4312      during copying. In pa_resource_assignments, the amount columns
4313      would be Null if no budget lines "ever" existed. */
4314 
4315  /*  06-Jul-2004 Bug 3729657 Raja FP M Dev changes
4316      Resource assignments with no amounts also should be copied
4317   */
4318 
4319   CURSOR baseline_budgt_res_cur IS
4320       SELECT  pra.resource_assignment_id
4321              ,pa_resource_assignments_s.NEXTVAL
4322              ,pra.parent_assignment_id
4323              ,pra.task_id
4324              ,pra.task_id
4325              --Start of Changes for bug 3354518
4326              ,NULL
4327              ,NULL
4328              ,pra.project_assignment_id
4329              ,pra.planning_start_date
4330              ,pra.planning_end_date
4331              ,pra.schedule_start_date
4332              ,pra.schedule_end_date
4333              --End of Changes for bug 3354518
4334              ,resource_list_member_id  -- bug 3615617   TARGET_RLM_ID
4335       FROM   pa_resource_assignments pra
4336       WHERE  pra.budget_version_id = p_source_plan_version_id;
4337       /*** Bug 3729657
4338       AND    (pra.total_plan_quantity IS NOT NULL
4339                    OR pra.total_plan_raw_cost IS NOT NULL
4340                    OR pra.total_plan_burdened_cost IS NOT NULL
4341                    OR pra.total_plan_revenue  IS NOT NULL);
4342       ***/
4343    -- cursor diferent_projects_cur is going to be used when the source and target projects are different
4344    -- Reference to pa_tasks is changed to pa_struct_task_wbs_v (bug 3354518)
4345 
4346  CURSOR different_projects_task_cur IS
4347       SELECT pra.resource_assignment_id                    -- SOURCE_RES_ASSIGNMENT_ID
4348              ,pa_resource_assignments_s.nextval            -- TARGET_RES_ASSIGNMENT_ID
4349              ,pra.parent_assignment_id                     -- PARENT_ASSIGNMENT_ID
4350              ,pra.task_id                                  -- SOURCE_TASK_ID
4351              ,target_tasks.task_id                         -- TARGET_TASK_ID
4352              ,NULL                                         -- SOURCE ELEMENT_VERSION_ID
4353              ,NULL                                         -- TARGET ELEMENT_VERSION_ID
4354              ,-1                                           -- PROJECT_ASSIGNMENT_ID
4355              ,pra.planning_start_date + l_shift_days       -- PLANNING_START_DATE
4356              ,pra.planning_end_date   + l_shift_days       -- PLANNING_END_DATE
4357              /* It is assumed that this api and this cursor would not be called for TA/WP flow!
4358                 So, the schedule dates are stamped as planning start dates itself */
4359              ,pra.planning_start_date + l_shift_days       -- SCHEDULE_START_DATE
4360              ,pra.planning_end_date   + l_shift_days       -- SCHEDULE_END_DATE
4361              ,target_rlm.resource_list_member_id -- bug 3615617 TARGET_RLM_ID
4362       FROM   pa_resource_assignments pra
4363             ,pa_tasks    source_tasks
4364             ,pa_tasks    target_tasks
4365             ,pa_resource_list_members source_rlm   -- bug 3615617
4366             ,pa_resource_list_members  target_rlm   -- bug 3615617
4367       WHERE  pra.budget_version_id = p_source_plan_version_id
4368       AND    pra.project_id = p_source_project_id  --bug#2708524
4369       AND    source_tasks.task_id = pra.task_id
4370       AND    source_tasks.task_number = target_tasks.task_number
4371       AND    target_tasks.project_id = l_target_project_id
4372       AND    source_rlm.resource_list_member_id = pra.resource_list_member_id   -- bug 3615617
4373       AND    target_rlm.resource_list_id  = source_rlm.resource_list_id         -- bug 3615617
4374       AND    target_rlm.alias  = source_rlm.alias                               -- bug 3615617
4375       AND    target_rlm.object_type = source_rlm.object_type                    -- bug 3615617
4376       AND    target_rlm.object_id = Decode(target_rlm.object_type,              -- bug 3615617
4377                                              'RESOURCE_LIST', target_rlm.resource_list_id,
4378                                              'PROJECT',       l_target_project_id)
4379       AND    pra.task_id <> 0; -- bug 3615617 this is redundant but put for clarity
4380 
4381 
4382   --bug#2684748
4383   --This cursor is added to resolve the issue of resource
4384   --assignments being not populated when planning level
4385   --is PROJECT and source and target project id is different.
4386 
4387   CURSOR proj_plan_lev_diff_proj_cur IS
4388        SELECT resource_assignment_id                   -- SOURCE_RES_ASSIGNMENT_ID
4389              ,pa_resource_assignments_s.nextval        -- TARGET_RES_ASSIGNMENT_ID
4390              ,NULL                                     -- PARENT_ASSIGNMENT_ID
4391              ,0                                        -- SOURCE_TASK_ID
4392              ,0                                        -- TARGET_TASK_ID
4393            --Start of Changes for bug 3354518
4394              ,NULL                                     -- SOURCE ELEMENT_VERSION_ID
4395              ,NULL                                     -- TARGET ELEMENT_VERSION_ID
4396              ,-1                                       -- PROJECT_ASSIGNMENT_ID
4397              ,planning_start_date + l_shift_days       -- PLANNING_START_DATE
4398              ,planning_end_date + l_shift_days         -- PLANNING_END_DATE
4399              /* It is assumed that this api and this cursor would not be called for TA/WP flow!
4400                 So, the schedule dates are stamped as planning start dates itself */
4401              ,planning_start_date + l_shift_days       -- SCHEDULE_START_DATE
4402              ,planning_end_date + l_shift_days         -- SCHEDULE_END_DATE
4403            --End of Changes for bug 3354518
4404             ,target_rlm.resource_list_member_id  -- bug 3615617 TARGET_RLM_ID
4405       FROM   pa_resource_assignments pra
4406             ,pa_resource_list_members source_rlm   -- bug 3615617
4407             ,pa_resource_list_members  target_rlm   -- bug 3615617
4408       WHERE  pra.budget_version_id = p_source_plan_version_id
4409       AND    pra.task_id = 0
4410       AND    source_rlm.resource_list_member_id = pra.resource_list_member_id   -- bug 3615617
4411       AND    target_rlm.resource_list_id  = source_rlm.resource_list_id         -- bug 3615617
4412       AND    target_rlm.alias  = source_rlm.alias                               -- bug 3615617
4413       AND    target_rlm.object_type = source_rlm.object_type                    -- bug 3615617
4414       AND    target_rlm.object_id = Decode(target_rlm.object_type,              -- bug 3615617
4415                                              'RESOURCE_LIST', target_rlm.resource_list_id,
4416                                              'PROJECT',       l_target_project_id) ;
4417 BEGIN
4418 
4419     x_msg_count := 0;
4420     x_return_status := FND_API.G_RET_STS_SUCCESS;
4421     pa_debug.set_err_stack('pa_fp_org_fcst_gen_pub.Create_Res_Task_Maps');
4422     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4423     l_debug_mode := NVL(l_debug_mode, 'Y');
4424     pa_debug.set_process('PLSQL','LOG',l_debug_mode);
4425 
4426     -- Check for business rules violations
4427 
4428     IF P_PA_DEBUG_MODE = 'Y' THEN
4429        pa_debug.g_err_stage:='Validating input parameters';
4430        pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4431     END IF;
4432 
4433     -- Check for null source_plan__version_id
4434 
4435     IF (p_source_plan_version_id IS NULL) THEN
4436 
4437         IF P_PA_DEBUG_MODE = 'Y' THEN
4438             pa_debug.g_err_stage:='Source fin plan version id'||p_source_plan_version_id;
4439             pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,5);
4440         END IF;
4441 
4442         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4443                               p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
4444         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4445     END IF;
4446 
4447     IF P_PA_DEBUG_MODE = 'Y' THEN
4448         pa_debug.g_err_stage:='Parameter validation complete';
4449         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4450     END IF;
4451 
4452     -- If adjustment percentage is null make it zero.
4453     -- Similarly, if shift days is null make it zero
4454 
4455     l_adj_percentage := NVL(p_adj_percentage,0);
4456     l_shift_days     := NVL(p_shift_days,0);
4457 
4458     IF P_PA_DEBUG_MODE = 'Y' THEN
4459         pa_debug.g_err_stage:='Source fin plan version id'||p_source_plan_version_id;
4460         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4461 
4462         pa_debug.g_err_stage:='Adj_percentage = '||l_adj_percentage;
4463         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4464     END IF;
4465 
4466     --Fetch source project id
4467 
4468     IF P_PA_DEBUG_MODE = 'Y' THEN
4469         pa_debug.g_err_stage:='Fetching source project id ';
4470         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4471     END IF;
4472 
4473     SELECT  project_id
4474     INTO   l_source_project_id
4475     FROM   pa_budget_versions
4476     WHERE  budget_version_id = p_source_plan_version_id;
4477 
4478     --IF target project id isn't passed, copy source projecct to target project
4479 
4480     l_target_project_id := NVL(p_target_project_id,l_source_project_id);
4481 
4482     IF P_PA_DEBUG_MODE = 'Y' THEN
4483         pa_debug.g_err_stage:='Source_project_id = '||l_source_project_id;
4484         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4485 
4486         pa_debug.g_err_stage:='Target_project_id = '||l_target_project_id;
4487         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4488     END IF;
4489 
4490 
4491     -- Remove the old records, if any,from pa_fp_ra_map_tmp
4492 
4493     DELETE FROM pa_fp_ra_map_tmp;
4494 
4495     --Using bulk insert logic here
4496 
4497     IF (l_source_project_id = l_target_project_id) THEN
4498 
4499         /* Included conditional opening of cursor for bug# 2920954 */
4500 
4501         IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN AND
4502            p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED THEN
4503 
4504             IF P_PA_DEBUG_MODE = 'Y' THEN
4505                 pa_debug.g_err_stage:='opening baseline_budgt_res_cur';
4506                 pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4507             END IF;
4508 
4509             OPEN baseline_budgt_res_cur;
4510 
4511         ELSE
4512 
4513             IF P_PA_DEBUG_MODE = 'Y' THEN
4514                 pa_debug.g_err_stage:='opening same_projects_cur';
4515                 pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4516             END IF;
4517 
4518             OPEN same_projects_cur;
4519 
4520         END IF;
4521 
4522         /* Bug 3067254  The fetch statement should be inside the loop to avoid infinite loop*/
4523         LOOP
4524 
4525             --Doing the bulk fetch
4526 
4527             IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN AND
4528                p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED THEN
4529 
4530               IF P_PA_DEBUG_MODE = 'Y' THEN
4531                   pa_debug.g_err_stage:='fetching from cursor baseline_budgt_res_cur';
4532                   pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4533               END IF;
4534 
4535               FETCH baseline_budgt_res_cur BULK COLLECT INTO
4536                          l_ra_map_tmp_tbl1
4537                         ,l_ra_map_tmp_tbl2
4538                         ,l_ra_map_tmp_tbl3
4539                         ,l_ra_map_tmp_tbl4
4540                         ,l_ra_map_tmp_tbl5
4541                         --Added for bug 3354518
4542                         ,l_ra_map_tmp_tbl6
4543                         ,l_ra_map_tmp_tbl7
4544                         ,l_ra_map_tmp_tbl8
4545                         ,l_ra_map_tmp_tbl9
4546                         ,l_ra_map_tmp_tbl10
4547                         ,l_ra_map_tmp_tbl11
4548                         ,l_ra_map_tmp_tbl12
4549                         --Added for bug 3615617
4550                         ,l_ra_map_tmp_tbl13
4551               LIMIT g_plsql_max_array_size;
4552 
4553             ELSE
4554 
4555               IF P_PA_DEBUG_MODE = 'Y' THEN
4556                   pa_debug.g_err_stage:='fetching from same_projects_cur';
4557                   pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4558               END IF;
4559 
4560               FETCH same_projects_cur BULK COLLECT INTO
4561                         l_ra_map_tmp_tbl1
4562                         ,l_ra_map_tmp_tbl2
4563                         ,l_ra_map_tmp_tbl3
4564                         ,l_ra_map_tmp_tbl4
4565                         ,l_ra_map_tmp_tbl5
4566                         --Added for bug 3354518
4567                         ,l_ra_map_tmp_tbl6
4568                         ,l_ra_map_tmp_tbl7
4569                         ,l_ra_map_tmp_tbl8
4570                         ,l_ra_map_tmp_tbl9
4571                         ,l_ra_map_tmp_tbl10
4572                         ,l_ra_map_tmp_tbl11
4573                         ,l_ra_map_tmp_tbl12
4574                         --Added for bug 3615617
4575                         ,l_ra_map_tmp_tbl13
4576               LIMIT g_plsql_max_array_size;
4577 
4578             END IF;
4579 
4580             /* Bug 3067254  LOOP  */
4581 
4582             IF NVL(l_ra_map_tmp_tbl1.last,0) >= 1 THEN
4583 
4584              --Only if something is fetched
4585 
4586                   FORALL i in l_ra_map_tmp_tbl1.first..l_ra_map_tmp_tbl1.last
4587 
4588                   INSERT INTO pa_fp_ra_map_tmp
4589                             (source_res_assignment_id
4590                             ,target_res_assignment_id
4591                             ,resource_assignment_id --parent of source res_assignment_id
4592                             ,source_task_id
4593                             ,target_task_id
4594                             --Added for bug 3354518
4595                             ,system_reference1
4596                             ,system_reference2
4597                             ,system_reference3
4598                             ,planning_start_date
4599                             ,planning_end_date
4600                             ,schedule_start_date
4601                             ,schedule_end_date
4602                             ,system_reference4)   -- Bug 3615617
4603                   VALUES  (  l_ra_map_tmp_tbl1(i)
4604                             ,l_ra_map_tmp_tbl2(i)
4605                             ,l_ra_map_tmp_tbl3(i)
4606                             ,l_ra_map_tmp_tbl4(i)
4607                             ,l_ra_map_tmp_tbl5(i)
4608                             --Added for bug 3354518
4609                             ,l_ra_map_tmp_tbl6(i)
4610                             ,l_ra_map_tmp_tbl7(i)
4611                             ,l_ra_map_tmp_tbl8(i)
4612                             ,l_ra_map_tmp_tbl9(i)
4613                             ,l_ra_map_tmp_tbl10(i)
4614                             ,l_ra_map_tmp_tbl11(i)
4615                             ,l_ra_map_tmp_tbl12(i)
4616                             ,l_ra_map_tmp_tbl13(i));   -- Bug 3615617
4617 
4618             END IF;
4619 
4620             --exit loop if the recent fetch size is less than 200
4621 
4622             EXIT WHEN NVL(l_ra_map_tmp_tbl1.last,0)<g_plsql_max_array_size;
4623 
4624         END LOOP;
4625 
4626 
4627         IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN AND
4628            p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED THEN
4629 
4630           CLOSE baseline_budgt_res_cur;
4631 
4632           IF P_PA_DEBUG_MODE = 'Y' THEN
4633               pa_debug.g_err_stage:='cursor baseline_budgt_res_cur is closed';
4634               pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4635           END IF;
4636 
4637         ELSE
4638 
4639           CLOSE same_projects_cur;
4640 
4641           IF P_PA_DEBUG_MODE = 'Y' THEN
4642               pa_debug.g_err_stage:='cursor same_projects_cur is closed';
4643               pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4644           END IF;
4645 
4646         END IF;
4647 
4648     ELSE  -- if projects are different
4649 
4650         -- bug 3841942 for periodic time phased versions check if the shift days i/p
4651         -- is large enough to cause a shift in the periodic data
4652 
4653         l_target_time_phased_code :=
4654               PA_FIN_PLAN_UTILS.get_time_phased_code(p_source_plan_version_id);
4655 
4656         IF l_shift_days  <> 0
4657         AND l_target_time_phased_code IN (PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,
4658                                           PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P)
4659         THEN
4660             BEGIN
4661 
4662                  IF P_PA_DEBUG_MODE = 'Y' THEN
4663                       pa_debug.g_err_stage := 'Selecting project start date';
4664                       pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4665                  END IF;
4666 
4667                  SELECT p.start_date
4668                  INTO   l_start_date
4669                  FROM   pa_projects p
4670                  WHERE  p.project_id = l_source_project_id;
4671 
4672                  IF l_start_date IS NULL THEN
4673 
4674                       IF P_PA_DEBUG_MODE = 'Y' THEN
4675                            pa_debug.g_err_stage := 'Selecting task mininum start date';
4676                            pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4677                       END IF;
4678 
4679                       SELECt min(t.start_date)
4680                       INTO   l_start_date
4681                       FROM   pa_tasks t
4682                       WHERE  t.project_id = l_source_project_id;
4683 
4684                       IF l_start_date is NULL THEN
4685 
4686                            IF P_PA_DEBUG_MODE = 'Y' THEN
4687                                 pa_debug.g_err_stage := 'Selecting budget lines minimum start date';
4688                                 pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4689                            END IF;
4690 
4691                            SELECT min(bl.start_date)
4692                            INTO   l_start_Date
4693                            FROM   pa_budget_lines bl
4694                            WHERE  bl.budget_version_id = p_source_plan_version_id;
4695 
4696                       END IF;  /* Mininum Task start date is null */
4697 
4698                  END IF; /* Minimum Project start date is null */
4699             EXCEPTION
4700                WHEN OTHERS THEN
4701                    IF P_PA_DEBUG_MODE = 'Y' THEN
4702                         pa_debug.g_err_stage := 'Error while fetching start date ' || sqlerrm;
4703                         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,5);
4704                    END IF;
4705                    RAISE;
4706             END;
4707 
4708             -- If l_start_date is null then that implies there are no budget lines
4709             IF l_start_date IS NOT NULL THEN
4710                 -- Based on the shift_days check how much shift is required period wise
4711                 pa_budget_core.get_periods(
4712                               x_start_date1 => l_start_date,
4713                               x_start_date2 => l_start_date + l_shift_days,
4714                               x_period_type => l_target_time_phased_code,
4715                               x_periods     => l_periods,
4716                               x_err_code    => l_err_code,
4717                               x_err_stage   => l_err_stage,
4718                               x_err_stack   => l_err_stack);
4719                 IF l_err_code <> 0 THEN
4720                      IF P_PA_DEBUG_MODE = 'Y' THEN
4721                           pa_debug.g_err_stage := 'Exception raised by pa_budget_core.get_periods...';
4722                           pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,5);
4723                      END IF;
4724 
4725                      PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
4726                                           p_msg_name      => l_err_stage);
4727                      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
4728                 END IF;
4729 
4730                 -- If l_periods is 0 then budget line data does not require a shift so no
4731                 -- shift is required in the planning start and end dates as well
4732                 IF l_periods = 0 THEN
4733                     l_shift_days := 0;
4734                 END IF;
4735             END IF;
4736         END IF; -- IF l_shift_days  <> 0 AND l_target_time_phased_code IN ('G','P')
4737 
4738         IF P_PA_DEBUG_MODE = 'Y' THEN
4739             pa_debug.g_err_stage:='opening different_projects_task_cur ';
4740             pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4741         END IF;
4742 
4743         OPEN proj_plan_lev_diff_proj_cur ;
4744         LOOP
4745             --Doing the bulk fetch
4746             FETCH proj_plan_lev_diff_proj_cur BULK COLLECT INTO
4747                    l_ra_map_tmp_tbl1
4748                   ,l_ra_map_tmp_tbl2
4749                   ,l_ra_map_tmp_tbl3
4750                   ,l_ra_map_tmp_tbl4
4751                   ,l_ra_map_tmp_tbl5
4752                   --Added for bug 3354518
4753                   ,l_ra_map_tmp_tbl6
4754                   ,l_ra_map_tmp_tbl7
4755                   ,l_ra_map_tmp_tbl8
4756                   ,l_ra_map_tmp_tbl9
4757                   ,l_ra_map_tmp_tbl10
4758                   ,l_ra_map_tmp_tbl11
4759                   ,l_ra_map_tmp_tbl12
4760                   --Added for bug 3615617
4761                   ,l_ra_map_tmp_tbl13
4762             LIMIT g_plsql_max_array_size;
4763 
4764             IF NVL(l_ra_map_tmp_tbl1.last,0) >= 1 THEN
4765 
4766                /* only if something is fetched */
4767 
4768                 FORALL i IN l_ra_map_tmp_tbl1.first..l_ra_map_tmp_tbl1.last
4769 
4770                     INSERT INTO pa_fp_ra_map_tmp
4771                             (source_res_assignment_id
4772                              ,target_res_assignment_id
4773                              ,resource_assignment_id --parent of source res_assignment_id
4774                              ,source_task_id
4775                              ,target_task_id
4776                              --Added for bug 3354518
4777                              ,system_reference1
4778                              ,system_reference2
4779                              ,system_reference3
4780                              ,planning_start_date
4781                              ,planning_end_date
4782                              ,schedule_start_date
4783                              ,schedule_end_date
4784                             -- Added for bug 3615617
4785                              ,system_reference4
4786                              )
4787                     VALUES  (l_ra_map_tmp_tbl1(i)
4788                              ,l_ra_map_tmp_tbl2(i)
4789                              ,l_ra_map_tmp_tbl3(i)
4790                              ,l_ra_map_tmp_tbl4(i)
4791                              ,l_ra_map_tmp_tbl5(i)
4792                              --Added for bug 3354518
4793                              ,l_ra_map_tmp_tbl6(i)
4794                              ,l_ra_map_tmp_tbl7(i)
4795                              ,l_ra_map_tmp_tbl8(i)
4796                              ,l_ra_map_tmp_tbl9(i)
4797                              ,l_ra_map_tmp_tbl10(i)
4798                              ,l_ra_map_tmp_tbl11(i)
4799                              ,l_ra_map_tmp_tbl12(i)
4800                             -- Added for bug 3615617
4801                              ,l_ra_map_tmp_tbl13(i));
4802             END IF;
4803 
4804             --Exit loop if the recent fetch size is less than 200
4805 
4806             EXIT WHEN NVL(l_ra_map_tmp_tbl1.last,0) < g_plsql_max_array_size;
4807         END LOOP;
4808         CLOSE proj_plan_lev_diff_proj_cur ;
4809 
4810         -- If planning level is not project, task level resource assignments also should be copied
4811         l_source_plan_level_code := PA_FIN_PLAN_UTILS.Get_Fin_Plan_Level_Code(p_source_plan_version_id);
4812 
4813         IF l_source_plan_level_code <> 'P' THEN
4814             OPEN different_projects_task_cur;
4815             LOOP
4816                 --Doing the bulk fetch
4817                 FETCH different_projects_task_cur BULK COLLECT INTO
4818                      l_ra_map_tmp_tbl1
4819                     ,l_ra_map_tmp_tbl2
4820                     ,l_ra_map_tmp_tbl3
4821                     ,l_ra_map_tmp_tbl4
4822                     ,l_ra_map_tmp_tbl5
4823                     --Added for bug 3354518
4824                     ,l_ra_map_tmp_tbl6
4825                     ,l_ra_map_tmp_tbl7
4826                     ,l_ra_map_tmp_tbl8
4827                     ,l_ra_map_tmp_tbl9
4828                     ,l_ra_map_tmp_tbl10
4829                     ,l_ra_map_tmp_tbl11
4830                     ,l_ra_map_tmp_tbl12
4831                     --Added for bug 3615617
4832                     ,l_ra_map_tmp_tbl13
4833                 LIMIT g_plsql_max_array_size;
4834 
4835                 IF NVL(l_ra_map_tmp_tbl1.last,0) >= 1 THEN
4836 
4837                     /* only if something is fetched */
4838 
4839                     FORALL i IN l_ra_map_tmp_tbl1.first..l_ra_map_tmp_tbl1.last
4840 
4841                         INSERT INTO pa_fp_ra_map_tmp
4842                                 (source_res_assignment_id
4843                                  ,target_res_assignment_id
4844                                  ,resource_assignment_id --parent of source res_assignment_id
4845                                  ,source_task_id
4846                                  ,target_task_id
4847                                  --Added for bug 3354518
4848                                  ,system_reference1
4849                                  ,system_reference2
4850                                  ,system_reference3
4851                                  ,planning_start_date
4852                                  ,planning_end_date
4853                                  ,schedule_start_date
4854                                  ,schedule_end_date
4855                                 -- Added for bug 3615617
4856                                  ,system_reference4
4857                                  )
4858                         VALUES  (l_ra_map_tmp_tbl1(i)
4859                                  ,l_ra_map_tmp_tbl2(i)
4860                                  ,l_ra_map_tmp_tbl3(i)
4861                                  ,l_ra_map_tmp_tbl4(i)
4862                                  ,l_ra_map_tmp_tbl5(i)
4863                                  --Added for bug 3354518
4864                                  ,l_ra_map_tmp_tbl6(i)
4865                                  ,l_ra_map_tmp_tbl7(i)
4866                                  ,l_ra_map_tmp_tbl8(i)
4867                                  ,l_ra_map_tmp_tbl9(i)
4868                                  ,l_ra_map_tmp_tbl10(i)
4869                                  ,l_ra_map_tmp_tbl11(i)
4870                                  ,l_ra_map_tmp_tbl12(i)
4871                                 -- Added for bug 3615617
4872                                  ,l_ra_map_tmp_tbl13(i));
4873                 END IF;
4874 
4875                 --Exit loop if the recent fetch size is less than 200
4876 
4877                 EXIT WHEN NVL(l_ra_map_tmp_tbl1.last,0) < g_plsql_max_array_size;
4878             END LOOP;
4879             CLOSE different_projects_task_cur ;
4880             IF P_PA_DEBUG_MODE = 'Y' THEN
4881                 pa_debug.g_err_stage:='cursor different_projects_task_cur is closed';
4882                 pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4883             END IF;
4884         END IF;  -- task level planning
4885     END IF; -- same/different projects
4886 
4887     IF P_PA_DEBUG_MODE = 'Y' THEN
4888         pa_debug.g_err_stage:='Exiting Create_Res_Task_Maps';
4889         pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,3);
4890     END IF;
4891 
4892     pa_debug.reset_err_stack;
4893 
4894 EXCEPTION
4895 
4896  WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
4897       l_msg_count := FND_MSG_PUB.count_msg;
4898       IF l_msg_count = 1 THEN
4899            PA_INTERFACE_UTILS_PUB.get_messages
4900                  (p_encoded        => FND_API.G_TRUE
4901                   ,p_msg_index      => 1
4902                   ,p_msg_count      => l_msg_count
4903                   ,p_msg_data       => l_msg_data
4904                   ,p_data           => l_data
4905                   ,p_msg_index_out  => l_msg_index_out);
4906            x_msg_data := l_data;
4907            x_msg_count := l_msg_count;
4908       ELSE
4909           x_msg_count := l_msg_count;
4910       END IF;
4911        x_return_status := FND_API.G_RET_STS_ERROR;
4912        IF P_PA_DEBUG_MODE = 'Y' THEN
4913           pa_debug.g_err_stage:='Invalid Arguments Passed';
4914           pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,5);
4915        END IF;
4916        pa_debug.reset_err_stack;
4917        RAISE;
4918 
4919  WHEN Others THEN
4920       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4921       x_msg_count     := 1;
4922       x_msg_data      := SQLERRM;
4923       FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'pa_fp_org_fcst_gen_pub'
4924                               ,p_procedure_name  => 'CREATE_RES_TASK_MAPS');
4925       IF P_PA_DEBUG_MODE = 'Y' THEN
4926           pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
4927           pa_debug.write('create_res_task_maps: ' || g_module_name,pa_debug.g_err_stage,5);
4928       END IF;
4929       pa_debug.reset_err_stack;
4930       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4931 
4932 END CREATE_RES_TASK_MAPS;
4933 
4934 
4935 /*************************************************************************
4936 sgoteti 03/03/2005.This API was previously in PAFPCPFB.pls, Copied it here as this will be used
4937 only in Org Forecasting Context. (Note: copy_resource_assignments in latest PAFPCPFB.pls will not
4938 go thru pa_fp_ra_map_tmp). The code is copied without any change from the version 115.196 of
4939 PAFPCPFB.pls to reduce the impact. The parameter p_rbs_map_diff_flag can be considered as an
4940 obsolete parameter in org forecasting flow.
4941 **************************************************************************/
4942 
4943   PROCEDURE Copy_Resource_Assignments(
4944            p_source_plan_version_id    IN     NUMBER
4945           ,p_target_plan_version_id   IN     NUMBER
4946           ,p_adj_percentage           IN     NUMBER
4947           ,p_rbs_map_diff_flag        IN     VARCHAR2 DEFAULT 'N'
4948           ,p_calling_context          IN     VARCHAR2 DEFAULT NULL -- Bug 4065314
4949           ,x_return_status            OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4950           ,x_msg_count                OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
4951           ,x_msg_data                 OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4952   AS
4953        l_msg_count          NUMBER :=0;
4954        l_data               VARCHAR2(2000);
4955        l_msg_data           VARCHAR2(2000);
4956        l_error_msg_code     VARCHAR2(2000);
4957        l_msg_index_out      NUMBER;
4958        l_return_status      VARCHAR2(2000);
4959        l_debug_mode         VARCHAR2(30);
4960 
4961        l_adj_percentage     NUMBER ;
4962        l_target_project_id  pa_projects.project_id%TYPE;
4963        l_cost_flag      pa_fin_plan_amount_sets.raw_cost_flag%TYPE;
4964        l_revenue_flag       pa_fin_plan_amount_sets.revenue_flag%TYPE;
4965 
4966        l_tmp                NUMBER;
4967 
4968 
4969   BEGIN
4970 
4971      x_msg_count := 0;
4972      x_return_status := FND_API.G_RET_STS_SUCCESS;
4973      pa_debug.set_err_stack('pa_fp_org_fcst_gen_pub.Copy_Resource_Assignments');
4974      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4975      l_debug_mode := NVL(l_debug_mode, 'Y');
4976      pa_debug.set_process('PLSQL','LOG',l_debug_mode);
4977 
4978      /*
4979       * Check if  source_verion_id, target_version_id are NULL, if so throw
4980       * an error message
4981       */
4982 
4983     IF P_PA_DEBUG_MODE = 'Y' THEN
4984         pa_debug.g_err_stage := 'Checking for valid parameters:';
4985         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
4986     END IF;
4987 
4988     IF (p_source_plan_version_id IS NULL) OR
4989        (p_target_plan_version_id IS NULL)
4990     THEN
4991 
4992          IF P_PA_DEBUG_MODE = 'Y' THEN
4993              pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
4994              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
4995 
4996              pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
4997              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
4998          END IF;
4999 
5000          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
5001                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
5002          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5003     END IF;
5004 
5005     IF P_PA_DEBUG_MODE = 'Y' THEN
5006         pa_debug.g_err_stage := 'Parameter validation complete';
5007         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5008     END IF;
5009 
5010     --If adj_percentage is zero make it null
5011 
5012     l_adj_percentage := NVL(p_adj_percentage,0);
5013 
5014      --Fetching the flags of target version using fin_plan_prefernce_code
5015 
5016      IF P_PA_DEBUG_MODE = 'Y' THEN
5017          pa_debug.g_err_stage:='Fetching the raw_cost,burdened_cost and revenue flags of target_version';
5018          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5019      END IF;
5020 
5021      SELECT DECODE(fin_plan_preference_code
5022                    ,PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,'Y'
5023                    ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME , 'Y','N') --cost_flag
5024             ,DECODE(fin_plan_preference_code
5025                      ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY ,'Y'
5026                      ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME ,'Y','N')--revenue_flag
5027             ,project_id
5028      INTO   l_cost_flag
5029            ,l_revenue_flag
5030            ,l_target_project_id
5031      FROM   pa_proj_fp_options
5032      WHERE  fin_plan_version_id=p_target_plan_version_id;
5033 /*
5034     IF P_PA_DEBUG_MODE = 'Y' THEN
5035         pa_debug.g_err_stage:='l_cost_flag ='||l_cost_flag;
5036         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5037         pa_debug.g_err_stage:='l_revenue_flag ='||l_revenue_flag;
5038         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5039         pa_debug.g_err_stage:='l_target_project_id ='||l_target_project_id;
5040         pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5041     END IF;
5042 */
5043     --Inserting records into pa_resource_assignments using pa_fp_ra_map_tmp
5044 
5045      IF P_PA_DEBUG_MODE = 'Y' THEN
5046          pa_debug.g_err_stage:='Copying the source version records as target version records';
5047          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5048      END IF;
5049 
5050      -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
5051 
5052      --Bug 3974569. Need not have pa_rbs_plans_out_tmp in the FROM clause if the parameter is N
5053      IF P_PA_DEBUG_MODE = 'Y' THEN
5054          pa_debug.g_err_stage:='p_rbs_map_diff_flag '||p_rbs_map_diff_flag;
5055          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5056      END IF;
5057 
5058      IF p_rbs_map_diff_flag ='N' THEN
5059 
5060          INSERT INTO PA_RESOURCE_ASSIGNMENTS(
5061                   resource_assignment_id
5062                   ,budget_version_id
5063                   ,project_id
5064                   ,task_id
5065                   ,resource_list_member_id
5066                   ,last_update_date
5067                   ,last_updated_by
5068                   ,creation_date
5069                   ,created_by
5070                   ,last_update_login
5071                   ,unit_of_measure
5072                   ,track_as_labor_flag
5073                   ,total_plan_revenue
5074                   ,total_plan_raw_cost
5075                   ,total_plan_burdened_cost
5076                   ,total_plan_quantity
5077                   ,resource_assignment_type
5078                   ,total_project_raw_cost
5079                   ,total_project_burdened_cost
5080                   ,total_project_revenue
5081                   ,standard_bill_rate
5082                   ,average_bill_rate
5083                   ,average_cost_rate
5084                   ,project_assignment_id
5085                   ,plan_error_code
5086                   ,average_discount_percentage
5087                   ,total_borrowed_revenue
5088                   ,total_revenue_adj
5089                   ,total_lent_resource_cost
5090                   ,total_cost_adj
5091                   ,total_unassigned_time_cost
5092                   ,total_utilization_percent
5093                   ,total_utilization_hours
5094                   ,total_utilization_adj
5095                   ,total_capacity
5096                   ,total_head_count
5097                   ,total_head_count_adj
5098                   ,total_tp_revenue_in
5099                   ,total_tp_revenue_out
5100                   ,total_tp_cost_in
5101                   ,total_tp_cost_out
5102                   ,parent_assignment_id
5103                   ,wbs_element_version_id
5104                   ,rbs_element_id
5105                   ,planning_start_date
5106                   ,planning_end_date
5107                   ,schedule_start_date
5108                   ,schedule_end_date
5109                   ,spread_curve_id
5110                   ,etc_method_code
5111                   ,res_type_code
5112                   ,attribute_category
5113                   ,attribute1
5114                   ,attribute2
5115                   ,attribute3
5116                   ,attribute4
5117                   ,attribute5
5118                   ,attribute6
5119                   ,attribute7
5120                   ,attribute8
5121                   ,attribute9
5122                   ,attribute10
5123                   ,attribute11
5124                   ,attribute12
5125                   ,attribute13
5126                   ,attribute14
5127                   ,attribute15
5128                   ,attribute16
5129                   ,attribute17
5130                   ,attribute18
5131                   ,attribute19
5132                   ,attribute20
5133                   ,attribute21
5134                   ,attribute22
5135                   ,attribute23
5136                   ,attribute24
5137                   ,attribute25
5138                   ,attribute26
5139                   ,attribute27
5140                   ,attribute28
5141                   ,attribute29
5142                   ,attribute30
5143                   ,fc_res_type_code
5144                   ,resource_class_code
5145                   ,organization_id
5146                   ,job_id
5147                   ,person_id
5148                   ,expenditure_type
5149                   ,expenditure_category
5150                   ,revenue_category_code
5151                   ,event_type
5152                   ,supplier_id
5153                   ,non_labor_resource
5154                   ,bom_resource_id
5155                   ,inventory_item_id
5156                   ,item_category_id
5157                   ,record_version_number
5158                   ,transaction_source_code
5159                   ,mfc_cost_type_id
5160                   ,procure_resource_flag
5161                   ,assignment_description
5162                   ,incurred_by_res_flag
5163                   ,rate_job_id
5164                   ,rate_expenditure_type
5165                   ,ta_display_flag
5166                   ,sp_fixed_date
5167                   ,person_type_code
5168                   ,rate_based_flag
5169                   ,use_task_schedule_flag
5170                   ,rate_exp_func_curr_code
5171                   ,rate_expenditure_org_id
5172                   ,incur_by_res_class_code
5173                   ,incur_by_role_id
5174                   ,project_role_id
5175                   ,resource_class_flag
5176                   ,named_role
5177                   ,txn_accum_header_id
5178                   ,scheduled_delay --For Bug 3948128
5179 				  ,CBS_ELEMENT_ID -- bug#16200605
5180                   )
5181          SELECT   /*+ ORDERED USE_NL(PFRMT,PRA) INDEX(PRA PA_RESOURCE_ASSIGNMENTS_U1)*/ pfrmt.target_res_assignment_id  --Bug 2814165
5182                   ,p_target_plan_version_id
5183                   ,l_target_project_id
5184                   ,pfrmt.target_task_id
5185                   ,pfrmt.system_reference4 -- Bug 3615617 resource_list_member_id
5186                   ,sysdate
5187                   ,fnd_global.user_id
5188                   ,sysdate
5189                   ,fnd_global.user_id
5190                   ,fnd_global.login_id
5191                   ,pra.unit_of_measure
5192                   ,pra.track_as_labor_flag
5193                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
5194                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
5195                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
5196                   ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
5197                   ,pra.resource_assignment_type
5198                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
5199                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
5200                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
5201                   ,standard_bill_rate
5202                   ,average_bill_rate
5203                   ,average_cost_rate
5204                   ,pfrmt.system_reference3 -- Project assignment id of the target (Bug 3354518)
5205                   ,plan_error_code
5206                   ,average_discount_percentage
5207                   ,total_borrowed_revenue
5208                   ,total_revenue_adj
5209                   ,total_lent_resource_cost
5210                   ,total_cost_adj
5211                   ,total_unassigned_time_cost
5212                   ,total_utilization_percent
5213                   ,total_utilization_hours
5214                   ,total_utilization_adj
5215                   ,total_capacity
5216                   ,total_head_count
5217                   ,total_head_count_adj
5218                   ,total_tp_revenue_in
5219                   ,total_tp_revenue_out
5220                   ,total_tp_cost_in
5221                   ,total_tp_cost_out
5222                   ,pfrmt.parent_assignment_id
5223                   ,pfrmt.system_reference2 -- element version id of the target. (Bug 3354518)
5224                   ,pra.rbs_element_id
5225                   ,pfrmt.planning_start_date -- Planning start date of the target (Bug 3354518)
5226                   ,pfrmt.planning_end_date   -- Planning end date of the target  (Bug 3354518)
5227                   ,pfrmt.schedule_start_date
5228                   ,pfrmt.schedule_end_date
5229                   ,pra.spread_curve_id
5230                   ,pra.etc_method_code
5231                   ,pra.res_type_code
5232                   ,pra.attribute_category
5233                   ,pra.attribute1
5234                   ,pra.attribute2
5235                   ,pra.attribute3
5236                   ,pra.attribute4
5237                   ,pra.attribute5
5238                   ,pra.attribute6
5239                   ,pra.attribute7
5240                   ,pra.attribute8
5241                   ,pra.attribute9
5242                   ,pra.attribute10
5243                   ,pra.attribute11
5244                   ,pra.attribute12
5245                   ,pra.attribute13
5246                   ,pra.attribute14
5247                   ,pra.attribute15
5248                   ,pra.attribute16
5249                   ,pra.attribute17
5250                   ,pra.attribute18
5251                   ,pra.attribute19
5252                   ,pra.attribute20
5253                   ,pra.attribute21
5254                   ,pra.attribute22
5255                   ,pra.attribute23
5256                   ,pra.attribute24
5257                   ,pra.attribute25
5258                   ,pra.attribute26
5259                   ,pra.attribute27
5260                   ,pra.attribute28
5261                   ,pra.attribute29
5262                   ,pra.attribute30
5263                   ,pra.fc_res_type_code
5264                   ,pra.resource_class_code
5265                   ,pra.organization_id
5266                   ,pra.job_id
5267                   ,pra.person_id
5268                   ,pra.expenditure_type
5269                   ,pra.expenditure_category
5270                   ,pra.revenue_category_code
5271                   ,pra.event_type
5272                   ,pra.supplier_id
5273                   ,pra.non_labor_resource
5274                   ,pra.bom_resource_id
5275                   ,pra.inventory_item_id
5276                   ,pra.item_category_id
5277                   ,1    -- should be 1 in the target version being created
5278                   ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
5279                   ,pra.mfc_cost_type_id
5280                   ,pra.procure_resource_flag
5281                   ,pra.assignment_description
5282                   ,pra.incurred_by_res_flag
5283                   ,pra.rate_job_id
5284                   ,pra.rate_expenditure_type
5285                   ,pra.ta_display_flag
5286                   -- Bug 3820625 sp_fixed_date should also move as per planning_start_date
5287                   -- Least and greatest are used to make sure that sp_fixed_date is with in planning start and end dates
5288                   ,greatest(least(pra.sp_fixed_date + (pfrmt.planning_start_date - pra.planning_start_date),
5289                                   pfrmt.planning_end_date),
5290                             pfrmt.planning_start_date)
5291                   ,pra.person_type_code
5292                   ,pra.rate_based_flag
5293                   ,pra.use_task_schedule_flag
5294                   ,pra.rate_exp_func_curr_code
5295                   ,pra.rate_expenditure_org_id
5296                   ,pra.incur_by_res_class_code
5297                   ,pra.incur_by_role_id
5298                   ,pra.project_role_id
5299                   ,pra.resource_class_flag
5300                   ,pra.named_role
5301                   ,pra.txn_accum_header_id
5302                   ,scheduled_delay --For Bug 3948128
5303 				  ,pra.CBS_ELEMENT_ID  -- bug#16200605
5304          FROM     PA_FP_RA_MAP_TMP pfrmt          --Bug 2814165
5305                  ,PA_RESOURCE_ASSIGNMENTS pra
5306          WHERE    pra.resource_assignment_id = pfrmt.source_res_assignment_id
5307          AND      pra.budget_version_id      = p_source_plan_version_id ;
5308 
5309      --For Bug 3974569. Take rbs_element_id and txn_accum_header_id from pa_rbs_plans_out_tmp
5310      ELSIF p_rbs_map_diff_flag ='Y' THEN --IF p_rbs_map_diff_flag ='N' THEN
5311 
5312          IF P_PA_DEBUG_MODE = 'Y' THEN
5313 
5314              SELECT COUNT(*)
5315              INTO   l_tmp
5316              FROM   PA_FP_RA_MAP_TMP;
5317 
5318              pa_debug.g_err_stage:='PA_FP_RA_MAP_TMP count '||l_tmp;
5319              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5320 
5321              SELECT COUNT(*)
5322              INTO   l_tmp
5323              FROM   pa_rbs_plans_out_tmp;
5324 
5325              pa_debug.g_err_stage:='pa_rbs_plans_out_tmp count '||l_tmp;
5326              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5327 
5328          END IF;
5329 
5330          INSERT INTO PA_RESOURCE_ASSIGNMENTS(
5331                   resource_assignment_id
5332                   ,budget_version_id
5333                   ,project_id
5334                   ,task_id
5335                   ,resource_list_member_id
5336                   ,last_update_date
5337                   ,last_updated_by
5338                   ,creation_date
5339                   ,created_by
5340                   ,last_update_login
5341                   ,unit_of_measure
5342                   ,track_as_labor_flag
5343                   ,total_plan_revenue
5344                   ,total_plan_raw_cost
5345                   ,total_plan_burdened_cost
5346                   ,total_plan_quantity
5347                   ,resource_assignment_type
5348                   ,total_project_raw_cost
5349                   ,total_project_burdened_cost
5350                   ,total_project_revenue
5351                   ,standard_bill_rate
5352                   ,average_bill_rate
5353                   ,average_cost_rate
5354                   ,project_assignment_id
5355                   ,plan_error_code
5356                   ,average_discount_percentage
5357                   ,total_borrowed_revenue
5358                   ,total_revenue_adj
5359                   ,total_lent_resource_cost
5360                   ,total_cost_adj
5361                   ,total_unassigned_time_cost
5362                   ,total_utilization_percent
5363                   ,total_utilization_hours
5364                   ,total_utilization_adj
5365                   ,total_capacity
5366                   ,total_head_count
5367                   ,total_head_count_adj
5368                   ,total_tp_revenue_in
5369                   ,total_tp_revenue_out
5370                   ,total_tp_cost_in
5371                   ,total_tp_cost_out
5372                   ,parent_assignment_id
5373                   ,wbs_element_version_id
5374                   ,rbs_element_id
5375                   ,planning_start_date
5376                   ,planning_end_date
5377                   ,schedule_start_date
5378                   ,schedule_end_date
5379                   ,spread_curve_id
5380                   ,etc_method_code
5381                   ,res_type_code
5382                   ,attribute_category
5383                   ,attribute1
5384                   ,attribute2
5385                   ,attribute3
5386                   ,attribute4
5387                   ,attribute5
5388                   ,attribute6
5389                   ,attribute7
5390                   ,attribute8
5391                   ,attribute9
5392                   ,attribute10
5393                   ,attribute11
5394                   ,attribute12
5395                   ,attribute13
5396                   ,attribute14
5397                   ,attribute15
5398                   ,attribute16
5399                   ,attribute17
5400                   ,attribute18
5401                   ,attribute19
5402                   ,attribute20
5403                   ,attribute21
5404                   ,attribute22
5405                   ,attribute23
5406                   ,attribute24
5407                   ,attribute25
5408                   ,attribute26
5409                   ,attribute27
5410                   ,attribute28
5411                   ,attribute29
5412                   ,attribute30
5413                   ,fc_res_type_code
5414                   ,resource_class_code
5415                   ,organization_id
5416                   ,job_id
5417                   ,person_id
5418                   ,expenditure_type
5419                   ,expenditure_category
5420                   ,revenue_category_code
5421                   ,event_type
5422                   ,supplier_id
5423                   ,non_labor_resource
5424                   ,bom_resource_id
5425                   ,inventory_item_id
5426                   ,item_category_id
5427                   ,record_version_number
5428                   ,transaction_source_code
5429                   ,mfc_cost_type_id
5430                   ,procure_resource_flag
5431                   ,assignment_description
5432                   ,incurred_by_res_flag
5433                   ,rate_job_id
5434                   ,rate_expenditure_type
5435                   ,ta_display_flag
5436                   ,sp_fixed_date
5437                   ,person_type_code
5438                   ,rate_based_flag
5439                   ,use_task_schedule_flag
5440                   ,rate_exp_func_curr_code
5441                   ,rate_expenditure_org_id
5442                   ,incur_by_res_class_code
5443                   ,incur_by_role_id
5444                   ,project_role_id
5445                   ,resource_class_flag
5446                   ,named_role
5447                   ,txn_accum_header_id
5448                   ,scheduled_delay --For Bug 3948128
5449 				  ,CBS_ELEMENT_ID  -- bug#16200605
5450                   )
5451          SELECT   /*+ ORDERED USE_NL(PFRMT,PRA,RMAP) INDEX(PRA PA_RESOURCE_ASSIGNMENTS_U1)*/ pfrmt.target_res_assignment_id  --Bug 2814165
5452                   ,p_target_plan_version_id
5453                   ,l_target_project_id
5454                   ,pfrmt.target_task_id
5455                   ,pfrmt.system_reference4 -- Bug 3615617 resource_list_member_id
5456                   ,sysdate
5457                   ,fnd_global.user_id
5458                   ,sysdate
5459                   ,fnd_global.user_id
5460                   ,fnd_global.login_id
5461                   ,pra.unit_of_measure
5462                   ,pra.track_as_labor_flag
5463                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_plan_revenue,NULL),NULL)
5464                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_raw_cost,NULL),NULL)
5465                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_plan_burdened_cost,NULL),NULL)
5466                   ,DECODE(l_adj_percentage,0,total_plan_quantity,NULL)
5467                   ,pra.resource_assignment_type
5468                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_raw_cost,NULL),NULL)
5469                   ,DECODE(l_adj_percentage,0,DECODE(l_cost_flag,'Y',total_project_burdened_cost,NULL),NULL)
5470                   ,DECODE(l_adj_percentage,0,DECODE(l_revenue_flag,'Y',total_project_revenue,NULL),NULL)
5471                   ,standard_bill_rate
5472                   ,average_bill_rate
5473                   ,average_cost_rate
5474                   ,pfrmt.system_reference3 -- Project assignment id of the target (Bug 3354518)
5475                   ,plan_error_code
5476                   ,average_discount_percentage
5477                   ,total_borrowed_revenue
5478                   ,total_revenue_adj
5479                   ,total_lent_resource_cost
5480                   ,total_cost_adj
5481                   ,total_unassigned_time_cost
5482                   ,total_utilization_percent
5483                   ,total_utilization_hours
5484                   ,total_utilization_adj
5485                   ,total_capacity
5486                   ,total_head_count
5487                   ,total_head_count_adj
5488                   ,total_tp_revenue_in
5489                   ,total_tp_revenue_out
5490                   ,total_tp_cost_in
5491                   ,total_tp_cost_out
5492                   ,pfrmt.parent_assignment_id
5493                   ,pfrmt.system_reference2 -- element version id of the target. (Bug 3354518)
5494                   ,rmap.rbs_element_id
5495                   ,pfrmt.planning_start_date -- Planning start date of the target (Bug 3354518)
5496                   ,pfrmt.planning_end_date   -- Planning end date of the target  (Bug 3354518)
5497                   ,pfrmt.schedule_start_date
5498                   ,pfrmt.schedule_end_date
5499                   ,pra.spread_curve_id
5500                   ,pra.etc_method_code
5501                   ,pra.res_type_code
5502                   ,pra.attribute_category
5503                   ,pra.attribute1
5504                   ,pra.attribute2
5505                   ,pra.attribute3
5506                   ,pra.attribute4
5507                   ,pra.attribute5
5508                   ,pra.attribute6
5509                   ,pra.attribute7
5510                   ,pra.attribute8
5511                   ,pra.attribute9
5512                   ,pra.attribute10
5513                   ,pra.attribute11
5514                   ,pra.attribute12
5515                   ,pra.attribute13
5516                   ,pra.attribute14
5517                   ,pra.attribute15
5518                   ,pra.attribute16
5519                   ,pra.attribute17
5520                   ,pra.attribute18
5521                   ,pra.attribute19
5522                   ,pra.attribute20
5523                   ,pra.attribute21
5524                   ,pra.attribute22
5525                   ,pra.attribute23
5526                   ,pra.attribute24
5527                   ,pra.attribute25
5528                   ,pra.attribute26
5529                   ,pra.attribute27
5530                   ,pra.attribute28
5531                   ,pra.attribute29
5532                   ,pra.attribute30
5533                   ,pra.fc_res_type_code
5534                   ,pra.resource_class_code
5535                   ,pra.organization_id
5536                   ,pra.job_id
5537                   ,pra.person_id
5538                   ,pra.expenditure_type
5539                   ,pra.expenditure_category
5540                   ,pra.revenue_category_code
5541                   ,pra.event_type
5542                   ,pra.supplier_id
5543                   ,pra.non_labor_resource
5544                   ,pra.bom_resource_id
5545                   ,pra.inventory_item_id
5546                   ,pra.item_category_id
5547                   ,1    -- should be 1 in the target version being created
5548                   ,decode(p_calling_context, 'CREATE_VERSION', NULL, pra.transaction_source_code)
5549                   ,pra.mfc_cost_type_id
5550                   ,pra.procure_resource_flag
5551                   ,pra.assignment_description
5552                   ,pra.incurred_by_res_flag
5553                   ,pra.rate_job_id
5554                   ,pra.rate_expenditure_type
5555                   ,pra.ta_display_flag
5556                   -- Bug 3820625 sp_fixed_date should also move as per planning_start_date
5557                   -- Least and greatest are used to make sure that sp_fixed_date is with in planning start and end dates
5558                   ,greatest(least(pra.sp_fixed_date + (pfrmt.planning_start_date - pra.planning_start_date),
5559                                   pfrmt.planning_end_date),
5560                             pfrmt.planning_start_date)
5561                   ,pra.person_type_code
5562                   ,pra.rate_based_flag
5563                   ,pra.use_task_schedule_flag
5564                   ,pra.rate_exp_func_curr_code
5565                   ,pra.rate_expenditure_org_id
5566                   ,pra.incur_by_res_class_code
5567                   ,pra.incur_by_role_id
5568                   ,pra.project_role_id
5569                   ,pra.resource_class_flag
5570                   ,pra.named_role
5571                   ,rmap.txn_accum_header_id
5572                   ,scheduled_delay --For Bug 3948128
5573 				  ,pra.CBS_ELEMENT_ID  -- bug#16200605
5574          FROM     PA_FP_RA_MAP_TMP pfrmt          --Bug 2814165
5575                  ,PA_RESOURCE_ASSIGNMENTS pra
5576                  ,pa_rbs_plans_out_tmp rmap
5577          WHERE    pra.resource_assignment_id = pfrmt.source_res_assignment_id
5578          AND      pra.budget_version_id      = p_source_plan_version_id
5579          AND      rmap.source_id = pra.resource_assignment_id;
5580 
5581 
5582      END IF;--IF p_rbs_map_diff_flag ='N' THEN
5583 
5584      l_tmp := SQL%ROWCOUNT;
5585      IF P_PA_DEBUG_MODE = 'Y' THEN
5586          pa_debug.g_err_stage:='No. of records inserted into PRA '||l_tmp;
5587          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5588      END IF;
5589 
5590      -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
5591      IF P_PA_DEBUG_MODE = 'Y' THEN
5592          pa_debug.g_err_stage:='Exiting Copy_Resource_Assignments';
5593          pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,3);
5594      END IF;
5595      pa_debug.reset_err_stack;  -- bug:- 2815593
5596  EXCEPTION
5597 
5598     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
5599          l_msg_count := FND_MSG_PUB.count_msg;
5600          IF l_msg_count = 1 THEN
5601               PA_INTERFACE_UTILS_PUB.get_messages
5602                    (p_encoded         => FND_API.G_TRUE
5603                     ,p_msg_index      => 1
5604                     ,p_msg_count      => l_msg_count
5605                     ,p_msg_data       => l_msg_data
5606                     ,p_data           => l_data
5607                     ,p_msg_index_out  => l_msg_index_out);
5608                x_msg_data := l_data;
5609                x_msg_count := l_msg_count;
5610           ELSE
5611                x_msg_count := l_msg_count;
5612           END IF;
5613           x_return_status := FND_API.G_RET_STS_ERROR;
5614 
5615           IF P_PA_DEBUG_MODE = 'Y' THEN
5616               pa_debug.g_err_stage:='Invalid Arguments Passed';
5617               pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
5618           END IF;
5619           pa_debug.reset_err_stack;
5620           RAISE;
5621 
5622    WHEN Others THEN
5623          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5624          x_msg_count     := 1;
5625          x_msg_data      := SQLERRM;
5626          FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'pa_fp_org_fcst_gen_pub'
5627                           ,p_procedure_name  => 'COPY_RESOURCE_ASSIGNMENTS');
5628 
5629          IF P_PA_DEBUG_MODE = 'Y' THEN
5630              pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
5631              pa_debug.write('Copy_Resource_Assignments: ' || g_module_name,pa_debug.g_err_stage,5);
5632          END IF;
5633          pa_debug.reset_err_stack;
5634          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5635 
5636   END Copy_Resource_Assignments;
5637 
5638 /*===================================================================
5639 sgoteti 03/03/2005.This API was previously in PAFPCPFB.pls, Copied it here as this will be used
5640 only in Org Forecasting Context. (Note: Copy_Budget_Lines in latest PAFPCPFB.pls will not
5641 go thru pa_fp_ra_map_tmp). The code is copied without any change from the version 115.196 of P
5642 PAFPCPFB.pls to reduce the impact.
5643 ===================================================================*/
5644 
5645   PROCEDURE Copy_Budget_Lines(
5646              p_source_plan_version_id     IN  NUMBER
5647              ,p_target_plan_version_id   IN  NUMBER
5648              ,p_adj_percentage           IN  NUMBER
5649              ,x_return_status            OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5650              ,x_msg_count                OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
5651              ,x_msg_data                 OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
5652    AS
5653 
5654          l_msg_count          NUMBER :=0;
5655          l_data               VARCHAR2(2000);
5656          l_msg_data           VARCHAR2(2000);
5657          l_error_msg_code     VARCHAR2(2000);
5658          l_msg_index_out      NUMBER;
5659          l_return_status      VARCHAR2(2000);
5660          l_debug_mode         VARCHAR2(30);
5661 
5662          l_source_period_profile_id  pa_budget_versions.period_profile_id%TYPE;
5663          l_target_period_profile_id  pa_budget_versions.period_profile_id%TYPE;
5664 
5665          l_revenue_flag       pa_fin_plan_amount_sets.revenue_flag%type;
5666          l_cost_flag          pa_fin_plan_amount_sets.raw_cost_flag%type;
5667 
5668          l_adj_percentage            NUMBER ;
5669          l_period_profiles_same_flag VARCHAR2(1);
5670 
5671          -- Bug 3927244
5672          l_copy_actuals_flag    VARCHAR2(1) := 'Y';
5673          l_src_plan_class_code      pa_fin_plan_types_b.plan_class_code%TYPE;
5674          l_trg_plan_class_code      pa_fin_plan_types_b.plan_class_code%TYPE;
5675          l_wp_version_flag      pa_budget_versions.wp_version_flag%TYPE;
5676 
5677          l_etc_start_date       pa_budget_versions.etc_start_date%TYPE;
5678 
5679          CURSOR get_plan_class_code_csr(c_budget_version_id pa_budget_versions.budget_version_id%TYPE) IS
5680          SELECT pfb.plan_class_code,nvl(pbv.wp_version_flag,'N'),etc_start_date
5681          FROM   pa_fin_plan_types_b pfb,
5682                 pa_budget_versions  pbv
5683          WHERE  pbv.budget_version_id = c_budget_version_id
5684          AND    pbv.fin_plan_type_id  = pfb.fin_plan_type_id;
5685          -- Bug 3927244
5686 
5687    BEGIN
5688 
5689       x_msg_count := 0;
5690       x_return_status := FND_API.G_RET_STS_SUCCESS;
5691       pa_debug.set_err_stack('pa_fp_org_fcst_gen_pub.Copy_Budget_Lines');
5692       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
5693       l_debug_mode := NVL(l_debug_mode, 'Y');
5694       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
5695 
5696       -- Checking for all valid input parametrs
5697 
5698       IF P_PA_DEBUG_MODE = 'Y' THEN
5699           pa_debug.g_err_stage := 'Checking for valid parameters:';
5700           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5701       END IF;
5702 
5703       IF (p_source_plan_version_id IS NULL) OR
5704          (p_target_plan_version_id IS NULL)
5705       THEN
5706 
5707            IF P_PA_DEBUG_MODE = 'Y' THEN
5708                pa_debug.g_err_stage := 'Source_plan='||p_source_plan_version_id;
5709                pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5710                pa_debug.g_err_stage := 'Target_plan'||p_target_plan_version_id;
5711                pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
5712            END IF;
5713 
5714            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
5715                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
5716 
5717            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5718 
5719       END IF;
5720 
5721       IF P_PA_DEBUG_MODE = 'Y' THEN
5722           pa_debug.g_err_stage := 'Parameter validation complete';
5723           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5724       END IF;
5725 /*
5726       pa_debug.g_err_stage:='Source fin plan version id'||p_source_plan_version_id;
5727       IF P_PA_DEBUG_MODE = 'Y' THEN
5728          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5729       END IF;
5730       pa_debug.g_err_stage:='Target fin plan version id'||p_target_plan_version_id;
5731       IF P_PA_DEBUG_MODE = 'Y' THEN
5732          pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5733       END IF;
5734 */
5735       --make adj percentage zero if passed as null
5736 
5737       l_adj_percentage := NVL(p_adj_percentage,0);
5738 /*
5739        pa_debug.g_err_stage:='Adj_percentage'||l_adj_percentage;
5740        IF P_PA_DEBUG_MODE = 'Y' THEN
5741           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5742        END IF;
5743 */
5744        -- Fetching the flags of target version using fin_plan_prefernce_code
5745 
5746 
5747        IF P_PA_DEBUG_MODE = 'Y' THEN
5748            pa_debug.g_err_stage:='Fetching the raw_cost,burdened_cost and revenue flags of target_version';
5749            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5750        END IF;
5751 
5752        SELECT DECODE(fin_plan_preference_code          -- l_revenue_flag
5753                        ,PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY ,'Y'
5754                        ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME ,'Y','N')
5755               ,DECODE(fin_plan_preference_code          -- l_cost_flag
5756                       ,PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,'Y'
5757                       ,PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME , 'Y','N')
5758        INTO   l_revenue_flag
5759               ,l_cost_flag
5760        FROM   pa_proj_fp_options
5761        WHERE  fin_plan_version_id=p_target_plan_version_id;
5762 /*
5763        pa_debug.g_err_stage:='l_revenue_flag ='||l_revenue_flag;
5764        IF P_PA_DEBUG_MODE = 'Y' THEN
5765           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5766        END IF;
5767        pa_debug.g_err_stage:='l_cost_flag ='||l_cost_flag;
5768        IF P_PA_DEBUG_MODE = 'Y' THEN
5769           pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5770        END IF;
5771 */
5772        -- Checking if source and target version period profiles match
5773 
5774        /* FPB2: REVIEW */
5775 
5776        /** MRC Elimination changes: PA_MRC_FINPLAN.populate_bl_map_tmp */
5777        PA_FIN_PLAN_UTILS2.populate_bl_map_tmp
5778 				(p_source_fin_plan_version_id  => p_source_plan_version_id,
5779                                  x_return_status      => x_return_status,
5780                                  x_msg_count          => x_msg_count,
5781                                  x_msg_data           => x_msg_data);
5782 
5783        IF P_PA_DEBUG_MODE = 'Y' THEN
5784            pa_debug.g_err_stage:='Inserting  budget_lines';
5785            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
5786        END IF;
5787 
5788        -- Bug 3927244: Actuals need to be copied from forecast to forecast within the same project for FINPLAN versions
5789 
5790        OPEN  get_plan_class_code_csr(p_source_plan_version_id);
5791        FETCH get_plan_class_code_csr
5792        INTO  l_src_plan_class_code,l_wp_version_flag,l_etc_start_date;
5793        CLOSE get_plan_class_code_csr;
5794 
5795        OPEN  get_plan_class_code_csr(p_target_plan_version_id);
5796        FETCH get_plan_class_code_csr
5797        INTO  l_trg_plan_class_code,l_wp_version_flag,l_etc_start_date;
5798        CLOSE get_plan_class_code_csr;
5799 
5800        IF l_wp_version_flag='Y' OR l_src_plan_class_code <> PA_FP_CONSTANTS_PKG.G_PLAN_CLASS_FORECAST OR
5801           l_trg_plan_class_code <> PA_FP_CONSTANTS_PKG.G_PLAN_CLASS_FORECAST THEN
5802             l_copy_actuals_flag := 'N';
5803        END IF;
5804 
5805           -- End: Bug 3927244
5806 
5807        -- Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
5808        --Bug 4052403. For non rate-based transactions quantity should be same as raw cost if the version type is COST/ALL or
5809        --it should be revenue if the version type is REVENUE. This business rule will be taken care by the API
5810        --PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts which is called after this INSERT. Note that this has to be done only
5811        --when adjustment% is not null since the amounts in the source will be altered only when the user enters some adj %
5812        --Bug 4188225. PC/PFC buckets will be copied unconditionally (Removed the condition that checks for l_adj_percentage
5813        --being greater than 0 in order to copy)
5814        INSERT INTO PA_BUDGET_LINES(
5815                 budget_line_id             /* FPB2 */
5816                ,budget_version_id          /* FPB2 */
5817                ,resource_assignment_id
5818                ,start_date
5819                ,last_update_date
5820                ,last_updated_by
5821                ,creation_date
5822                ,created_by
5823                ,last_update_login
5824                ,end_date
5825                ,period_name
5826                ,quantity
5827                ,raw_cost
5828                ,burdened_cost
5829                ,revenue
5830                ,change_reason_code
5831                ,description
5832                ,attribute_category
5833                ,attribute1
5834                ,attribute2
5835                ,attribute3
5836                ,attribute4
5837                ,attribute5
5838                ,attribute6
5839                ,attribute7
5840                ,attribute8
5841                ,attribute9
5842                ,attribute10
5843                ,attribute11
5844                ,attribute12
5845                ,attribute13
5846                ,attribute14
5847                ,attribute15
5848                ,raw_cost_source
5849                ,burdened_cost_source
5850                ,quantity_source
5851                ,revenue_source
5852                ,pm_product_code
5853                ,pm_budget_line_reference
5854                ,cost_rejection_code
5855                ,revenue_rejection_code
5856                ,burden_rejection_code
5857                ,other_rejection_code
5858                ,code_combination_id
5859                ,ccid_gen_status_code
5860                ,ccid_gen_rej_message
5861                ,request_id
5862                ,borrowed_revenue
5863                ,tp_revenue_in
5864                ,tp_revenue_out
5865                ,revenue_adj
5866                ,lent_resource_cost
5867                ,tp_cost_in
5868                ,tp_cost_out
5869                ,cost_adj
5870                ,unassigned_time_cost
5871                ,utilization_percent
5872                ,utilization_hours
5873                ,utilization_adj
5874                ,capacity
5875                ,head_count
5876                ,head_count_adj
5877                ,projfunc_currency_code
5878                ,projfunc_cost_rate_type
5879                ,projfunc_cost_exchange_rate
5880                ,projfunc_cost_rate_date_type
5881                ,projfunc_cost_rate_date
5882                ,projfunc_rev_rate_type
5883                ,projfunc_rev_exchange_rate
5884                ,projfunc_rev_rate_date_type
5885                ,projfunc_rev_rate_date
5886                ,project_currency_code
5887                ,project_cost_rate_type
5888                ,project_cost_exchange_rate
5889                ,project_cost_rate_date_type
5890                ,project_cost_rate_date
5891                ,project_raw_cost
5892                ,project_burdened_cost
5893                ,project_rev_rate_type
5894                ,project_rev_exchange_rate
5895                ,project_rev_rate_date_type
5896                ,project_rev_rate_date
5897                ,project_revenue
5898                ,txn_raw_cost
5899                ,txn_burdened_cost
5900                ,txn_currency_code
5901                ,txn_revenue
5902                ,bucketing_period_code
5903                ,transfer_price_rate
5904                ,init_quantity
5905                ,init_quantity_source
5906                ,init_raw_cost
5907                ,init_burdened_cost
5908                ,init_revenue
5909                ,init_raw_cost_source
5910                ,init_burdened_cost_source
5911                ,init_revenue_source
5912                ,project_init_raw_cost
5913                ,project_init_burdened_cost
5914                ,project_init_revenue
5915                ,txn_init_raw_cost
5916                ,txn_init_burdened_cost
5917                ,txn_init_revenue
5918                ,txn_markup_percent
5919                ,txn_markup_percent_override
5920                ,txn_discount_percentage
5921                ,txn_standard_bill_rate
5922                ,txn_standard_cost_rate
5923                ,txn_cost_rate_override
5924                ,burden_cost_rate
5925                ,txn_bill_rate_override
5926                ,burden_cost_rate_override
5927                ,cost_ind_compiled_set_id
5928                ,pc_cur_conv_rejection_code
5929                ,pfc_cur_conv_rejection_code
5930 )
5931      SELECT     bmt.target_budget_line_id      /* FPB2 */
5932                ,p_target_plan_version_id     /* FPB2 */
5933                ,pfrmt.target_res_assignment_id
5934                ,pbl.start_date
5935                ,sysdate
5936                ,fnd_global.user_id
5937                ,sysdate
5938                ,fnd_global.user_id
5939                ,fnd_global.login_id
5940                ,pbl.end_date
5941                ,pbl.period_name
5942                ,pbl.quantity
5943                ,DECODE(l_cost_flag,'Y', raw_cost,NULL)
5944                ,DECODE(l_cost_flag,'Y', burdened_cost,NULL)
5945                ,DECODE(l_revenue_flag,'Y', revenue,NULL)
5946                ,pbl.change_reason_code
5947                ,description
5948                ,attribute_category
5949                ,attribute1
5950                ,attribute2
5951                ,attribute3
5952                ,attribute4
5953                ,attribute5
5954                ,attribute6
5955                ,attribute7
5956                ,attribute8
5957                ,attribute9
5958                ,attribute10
5959                ,attribute11
5960                ,attribute12
5961                ,attribute13
5962                ,attribute14
5963                ,attribute15
5964                ,DECODE(l_cost_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --raw_cost_souce
5965                ,DECODE(l_cost_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --burdened_cost_source
5966                ,PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P  --quantity_source
5967                ,DECODE(l_revenue_flag,'Y',PA_FP_CONSTANTS_PKG.G_AMOUNT_SOURCE_COPY_P,NULL) --revenue source
5968                ,pm_product_code
5969                ,pm_budget_line_reference
5970                ,DECODE(l_cost_flag, 'Y',cost_rejection_code, NULL)
5971                ,DECODE(l_revenue_flag, 'Y',revenue_rejection_code, NULL)
5972                ,DECODE(l_cost_flag,'Y',burden_rejection_code, NULL)
5973                ,other_rejection_code
5974                ,code_combination_id
5975                ,ccid_gen_status_code
5976                ,ccid_gen_rej_message
5977                ,fnd_global.conc_request_id
5978                ,borrowed_revenue
5979                ,tp_revenue_in
5980                ,tp_revenue_out
5981                ,revenue_adj
5982                ,lent_resource_cost
5983                ,tp_cost_in
5984                ,tp_cost_out
5985                ,cost_adj
5986                ,unassigned_time_cost
5987                ,utilization_percent
5988                ,utilization_hours
5989                ,utilization_adj
5990                ,capacity
5991                ,head_count
5992                ,head_count_adj
5993                ,projfunc_currency_code
5994                ,DECODE(l_cost_flag,'Y',projfunc_cost_rate_type,NULL)
5995                ,DECODE(l_cost_flag,'Y',projfunc_cost_exchange_rate,NULL)
5996                ,DECODE(l_cost_flag,'Y',projfunc_cost_rate_date_type,NULL)
5997                ,DECODE(l_cost_flag,'Y',projfunc_cost_rate_date,NULL)
5998                ,DECODE(l_revenue_flag,'Y',projfunc_rev_rate_type,NULL)
5999                ,DECODE(l_revenue_flag,'Y',projfunc_rev_exchange_rate,NULL)
6000                ,DECODE(l_revenue_flag,'Y',projfunc_rev_rate_date_type,NULL)
6001                ,DECODE(l_revenue_flag,'Y',projfunc_rev_rate_date,NULL)
6002                ,project_currency_code
6003                ,DECODE(l_cost_flag,'Y',project_cost_rate_type,NULL)
6004                ,DECODE(l_cost_flag,'Y',project_cost_exchange_rate,NULL)
6005                ,DECODE(l_cost_flag,'Y',project_cost_rate_date_type,NULL)
6006                ,DECODE(l_cost_flag,'Y',project_cost_rate_date,NULL)
6007                ,DECODE(l_cost_flag,'Y', project_raw_cost,NULL)
6008                ,DECODE(l_cost_flag,'Y', project_burdened_cost,NULL)
6009                ,DECODE(l_revenue_flag,'Y',project_rev_rate_type,NULL)
6010                ,DECODE(l_revenue_flag,'Y',project_rev_exchange_rate,NULL)
6011                ,DECODE(l_revenue_flag,'Y',project_rev_rate_date_type,NULL)
6012                ,DECODE(l_revenue_flag,'Y',project_rev_rate_date,NULL)
6013                ,DECODE(l_revenue_flag,'Y', project_revenue,NULL)
6014                ,DECODE(l_cost_flag,'Y',
6015                        decode(GREATEST(pbl.start_date,NVL(l_etc_start_date,pbl.start_date)),pbl.start_date,txn_raw_cost*(1+l_adj_percentage),txn_raw_cost),NULL)
6016                ,DECODE(l_cost_flag,'Y',
6017                        decode(GREATEST(pbl.start_date,NVL(l_etc_start_date,pbl.start_date)),pbl.start_date,txn_burdened_cost*(1+l_adj_percentage),txn_burdened_cost),NULL)
6018                ,txn_currency_code
6019                ,DECODE(l_revenue_flag,'Y',
6020                         decode(GREATEST(pbl.start_date,NVL(l_etc_start_date,pbl.start_date)),pbl.start_date,txn_revenue*(1+l_adj_percentage),txn_revenue),NULL)
6021                ,DECODE(l_period_profiles_same_flag,'Y',bucketing_period_code,NULL)
6022                ,transfer_price_rate
6023                ,decode(l_copy_actuals_flag,'N',NULL,pbl.init_quantity)              --init_quantity
6024                ,decode(l_copy_actuals_flag,'N',NULL,pbl.init_quantity_source)       --init_quantity_source
6025                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.init_raw_cost),NULL)                   --init_raw_cost
6026                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.init_burdened_cost),NULL)         --init_burdened_cost
6027                ,DECODE(l_revenue_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.init_revenue),NULL)                     --init_revenue
6028                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.init_raw_cost_source),NULL)            --init_raw_cost_source
6029                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.init_burdened_cost_source),NULL)  --init_burdened_cost_source
6030                ,DECODE(l_revenue_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.init_revenue_source),NULL)              --init_revenue_source
6031                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.project_init_raw_cost),NULL)           --project_init_raw_cost
6032                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.project_init_burdened_cost),NULL) --project_init_burdened_cost
6033                ,DECODE(l_revenue_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.project_init_revenue),NULL)             --project_init_revenue
6034                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.txn_init_raw_cost),NULL)               --txn_init_raw_cost
6035                ,DECODE(l_cost_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.txn_init_burdened_cost),NULL)     --txn_init_burdened_cost
6036                ,DECODE(l_revenue_flag,'Y',decode(l_copy_actuals_flag,'N',NULL,pbl.txn_init_revenue),NULL)                 --txn_init_revenue
6037                ,txn_markup_percent
6038                ,txn_markup_percent_override
6039                ,txn_discount_percentage
6040                ,Decode(l_revenue_flag,'Y',txn_standard_bill_rate,null) --txn_standard_bill_rate
6041                ,Decode(l_cost_flag,'Y',txn_standard_cost_rate,null) --txn_standard_cost_rate
6042                ,Decode(l_cost_flag,'Y',txn_cost_rate_override,null) --txn_cost_rate_override
6043                ,Decode(l_cost_flag,'Y',burden_cost_rate,null)       --burden_cost_rate
6044                ,Decode(l_revenue_flag,'Y',txn_bill_rate_override,null) --txn_bill_rate_override
6045                ,Decode(l_cost_flag,'Y',burden_cost_rate_override,null) --burden_cost_rate_override
6046                ,cost_ind_compiled_set_id
6047                ,Decode(l_adj_percentage,0,pc_cur_conv_rejection_code,null)
6048                ,Decode(l_adj_percentage,0,pfc_cur_conv_rejection_code,null)
6049        FROM PA_BUDGET_LINES  pbl
6050             ,PA_FP_RA_MAP_TMP pfrmt
6051             ,pa_fp_bl_map_tmp bmt       /* FPB2 */
6052        WHERE pbl.resource_assignment_id = pfrmt.source_res_assignment_id
6053          AND bmt.source_budget_line_id = pbl.budget_line_id    /* FPB2 */
6054          AND pbl.budget_version_id = p_source_plan_version_id;
6055 
6056        -- End, Bug 3362316, 08-JAN-2003: Added New FP.M Columns  --------------------------
6057 
6058        -- Bug 4035856 Call rounding api if l_adj_percentage is not zero
6059        IF l_adj_percentage <> 0 THEN
6060             PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts
6061                    (  p_budget_version_id     => p_target_plan_version_id
6062                      ,p_calling_context       => 'COPY_VERSION'
6063                      ,x_return_status         => l_return_status
6064                      ,x_msg_count             => l_msg_count
6065                      ,x_msg_data              => l_msg_data);
6066 
6067              IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
6068                  IF P_PA_debug_mode = 'Y' THEN
6069                       pa_debug.g_err_stage:= 'Error in PA_FP_MULTI_CURRENCY_PKG.Round_Budget_Line_Amounts';
6070                       pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
6071                  END IF;
6072                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6073              END IF;
6074        END IF;
6075 
6076        IF P_PA_DEBUG_MODE = 'Y' THEN
6077            pa_debug.g_err_stage:='Exiting Copy_Budget_Lines';
6078            pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,3);
6079        END IF;
6080        pa_debug.reset_err_stack;    -- bug:- 2815593
6081   EXCEPTION
6082 
6083       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
6084              l_msg_count := FND_MSG_PUB.count_msg;
6085              IF l_msg_count = 1 THEN
6086                PA_INTERFACE_UTILS_PUB.get_messages
6087                    (p_encoded        => FND_API.G_TRUE
6088                     ,p_msg_index      => 1
6089                     ,p_msg_count      => l_msg_count
6090                     ,p_msg_data       => l_msg_data
6091                     ,p_data           => l_data
6092                     ,p_msg_index_out  => l_msg_index_out);
6093                x_msg_data := l_data;
6094                x_msg_count := l_msg_count;
6095            ELSE
6096                x_msg_count := l_msg_count;
6097            END IF;
6098            x_return_status := FND_API.G_RET_STS_ERROR;
6099 
6100            IF P_PA_DEBUG_MODE = 'Y' THEN
6101                pa_debug.g_err_stage:='Invalid arguments passed';
6102                pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,5);
6103            END IF;
6104            pa_debug.reset_err_stack;
6105            RAISE;
6106 
6107     WHEN Others THEN
6108         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6109         x_msg_count     := 1;
6110         x_msg_data      := SQLERRM;
6111         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'pa_fp_org_fcst_gen_pub'
6112                             ,p_procedure_name  => 'COPY_BUDGET_LINES');
6113 
6114         IF P_PA_DEBUG_MODE = 'Y' THEN
6115             pa_debug.g_err_stage:='Unexpected error'||SQLERRM;
6116             pa_debug.write('Copy_Budget_Lines: ' || g_module_name,pa_debug.g_err_stage,6);
6117         END IF;
6118         pa_debug.reset_err_stack;
6119         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6120  END Copy_Budget_Lines;
6121 
6122 
6123 END pa_fp_org_fcst_gen_pub;