DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_FIN_PLAN_UTILS

Source


1 PACKAGE BODY pa_fin_plan_utils as
2 /* $Header: PAFPUTLB.pls 120.16.12010000.3 2008/10/21 13:57:43 vgovvala ship $
3    Start of Comments
4    Package name     : PA_FIN_PLAN_UTILS
5    Purpose          : utility API's for Org Forecast pages
6    History          :
7    NOTE             :
8    End of Comments
9 */
10 
11 l_module_name VARCHAR2(100) := 'pa.plsql.pa_fin_plan_utils';
12 Invalid_Arg_Exc  EXCEPTION;
13 Invalid_Call_Exc  EXCEPTION; /* Added for FP.M, Tracking bug no.3354518 */
14 
15 -- This function takes a lookup type and code, and returns the meaning
16 -- useful when wanting to populate VO's with the meaning for display
17 -- in a table
18 P_PA_DEBUG_MODE varchar2(1) := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
19 
20 --bug 5962744
21 
22 G_Chg_Reason    varchar2(80);
23 
24 function get_lookup_value
25     (p_lookup_type      pa_lookups.lookup_type%TYPE,
26      p_lookup_code      pa_lookups.lookup_code%TYPE) return VARCHAR2
27 is
28 l_return_value      VARCHAR2(80);
29 begin
30   select meaning
31     into l_return_value
32     from pa_lookups
33     where lookup_type = p_lookup_type and
34           lookup_code = p_lookup_code;
35   return l_return_value;
36 exception
37    WHEN NO_DATA_FOUND then
38       return null;
39    WHEN OTHERS then
40       return null;
41 end get_lookup_value;
42 
43 
44 procedure Check_Record_Version_Number
45     (p_unique_index             IN  NUMBER,
46      p_record_version_number    IN  NUMBER,
47      x_valid_flag               OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
48      x_return_status                OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
49      x_error_msg_code           OUT NOCOPY VARCHAR2) is --File.Sql.39 bug 4440895
50 l_record_version_number         pa_budget_versions.record_version_number%TYPE;
51 begin
52     x_return_status := FND_API.G_RET_STS_SUCCESS;
53     x_valid_flag := 'Y';
54     select
55         record_version_number
56     into
57         l_record_version_number
58     from
59         pa_budget_versions
60     where
61         budget_version_id=p_unique_index;
62     /* compare results */
63     if p_record_version_number is NULL then
64         x_return_status := FND_API.G_RET_STS_SUCCESS;
65         return;
66     elsif p_record_version_number <> l_record_version_number then
67         x_valid_flag := 'N';
68         x_return_status := FND_API.G_RET_STS_ERROR;
69         x_error_msg_code := 'PA_XC_RECORD_CHANGED';
70         return;
71     end if;
72 /*
73 exception
74     when others then
75       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
76       x_msg_count     := 1;
77       x_msg_data      := SQLERRM;
78       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_UTILS',
79                                p_procedure_name   => 'Check_Record_Version_Number');
80 */
81 end Check_Record_Version_Number;
82 
83 /* This function checks whether a plan type can be added to a project or not
84    This will be called from the Add Plan Type page. This is due to bug
85    2607945*/
86 FUNCTION Is_Plan_Type_Addition_Allowed
87           (p_project_id                   IN   pa_projects_all.project_id%TYPE
88           ,p_fin_plan_type_id             IN   pa_fin_plan_types_b.fin_plan_type_id%TYPE
89           ) RETURN VARCHAR2 IS
90 
91 l_valid_status                       VARCHAR2(1):='S';
92 l_migrated_frm_bdgt_typ_code       pa_fin_plan_types_b.migrated_frm_bdgt_typ_code%TYPE;
93 l_approved_cost_plan_type_flag     pa_fin_plan_types_b.approved_cost_plan_type_flag%TYPE;
94 l_approved_rev_plan_type_flag      pa_fin_plan_types_b.approved_rev_plan_type_flag%TYPE;
95 l_budget_version_id                pa_budget_Versions.budget_version_id%TYPE;
96 
97 BEGIN
98        IF P_PA_DEBUG_MODE = 'Y' THEN
99           pa_debug.init_err_stack ('pa_fin_plan_utils.is_plan_type_addition_allowed');
100        END IF;
101 
102 /* Changes for FP.M, Tracking Bug No - 3354518. Adding conditon in the where clause below to
103 check for new column use_for_workplan flag. This column indicates if a plan type is being
104 used for workplan or not. Since we cannot add a workplan plantype to a project for budgeting
105 we are introducing this check.Please note that this API is used for addition of finplan
106 types only*/
107 
108 SELECT migrated_frm_bdgt_typ_code
109              ,approved_cost_plan_type_flag
110              ,approved_rev_plan_type_flag
111        INTO l_migrated_frm_bdgt_typ_code
112             ,l_approved_cost_plan_type_flag
113             ,l_approved_rev_plan_type_flag
114        FROM pa_fin_plan_types_b
115        WHERE fin_plan_type_id = p_fin_plan_type_id
116          AND nvl(use_for_workplan_flag,'N')='N'; -- Added for Changes for FP.M, Tracking Bug No - 3354518
117 
118 
119       IF (l_migrated_frm_bdgt_typ_code IS NULL) THEN
120                  IF (l_approved_cost_plan_type_flag ='Y') THEN
121                     BEGIN
122                       SELECT budget_version_id
123                       INTO   l_budget_version_id
124                       FROM   pa_budget_versions
125                       WHERE  project_id = p_project_id
126                       AND    budget_type_code=PA_FP_CONSTANTS_PKG.G_BUDGET_TYPE_CODE_AC
127                       AND    rownum = 1;
128 
129                       RETURN ('PA_FP_AC_BUDGET_TYPE_EXISTS');
130 
131                     EXCEPTION
132                         WHEN NO_DATA_FOUND THEN
133                            l_valid_status :='S';
134                     END;
135                  END IF;
136 
137                  IF (l_approved_rev_plan_type_flag ='Y')  THEN
138                     BEGIN
139                       SELECT budget_version_id
140                       INTO   l_budget_version_id
141                       FROM   pa_budget_versions
142                       WHERE  project_id = p_project_id
143                       AND    budget_type_code=PA_FP_CONSTANTS_PKG.G_BUDGET_TYPE_CODE_AR
144                       AND    rownum = 1;
145 
146                       RETURN 'PA_FP_AR_BUDGET_TYPE_EXISTS';
147 
148                     EXCEPTION
149                          WHEN NO_DATA_FOUND THEN
150                             l_valid_status :='S';
151                     END;
152                  END IF;
153 
154 
155       ELSE
156               BEGIN
157                    SELECT budget_version_id
158                    INTO   l_budget_version_id
159                    FROM   pa_budget_versions
160                    WHERE  project_id = p_project_id
161                    AND    budget_type_code=l_migrated_frm_bdgt_typ_code
162                    AND    rownum = 1;
163 
164                    RETURN 'PA_FP_BUDGET_TYPE_NOT_UPGRADED';
165               EXCEPTION
166                     WHEN NO_DATA_FOUND THEN
167                           l_valid_status :='S';
168               END;
169       END IF;
170  IF P_PA_DEBUG_MODE = 'Y' THEN
171 	pa_debug.reset_err_stack;
172 	END IF;
173 RETURN l_valid_status;
174  EXCEPTION
175 
176 /* Changes for FP.M, Tracking Bug No - 3354518. Adding Exception handling for NO_DATA_FOUND
177 to return Failure Status as per the where clause added above for use_for_workplan_flag */
178      WHEN NO_DATA_FOUND THEN
179  IF P_PA_DEBUG_MODE = 'Y' THEN
180           pa_debug.reset_err_stack;
181  END IF;
182           RETURN 'F';
183 
184         WHEN OTHERS THEN
185  IF P_PA_DEBUG_MODE = 'Y' THEN
186           pa_debug.reset_err_stack;
187  END IF;
188           RETURN 'F';
189 
190  END is_plan_type_addition_allowed;
191 
192 function Retrieve_Record_Version_Number
193     (p_budget_version_id     IN   pa_budget_versions.budget_version_id%TYPE)
194     return number
195 is
196 l_record_version_number  pa_budget_versions.record_version_number%TYPE;
197 
198 begin
199     select
200         nvl(record_version_number, 0)
201     into
202         l_record_version_number
203     from
204         pa_budget_versions
205     where
206         budget_version_id=p_budget_version_id;
207     return l_record_version_number;
208 end Retrieve_Record_Version_Number;
209 
210 function Plan_Amount_Exists
211     (p_budget_version_id IN pa_budget_versions.budget_version_id%TYPE)
212     return varchar2 is
213    l_exists  varchar2(1) := 'N';
214 begin
215 
216   select 'Y'
217   into   l_exists
218   from   pa_budget_lines a,
219          pa_resource_assignments b
220   where  a.resource_assignment_id = b.resource_assignment_id
221   and    b.budget_version_id = p_budget_version_id
222   and    rownum < 2;
223 
224   return l_exists;
225 
226 exception
227   when no_data_found then
228     return 'N';
229 end Plan_Amount_Exists;
230 
231 
232 /*
233   API Name               : Plan_Amount_Exists_Task_Res
234   API Description   : Return 'Y' if at least one record exists in Resource Assignments (pa_resource_assignments)
235                            for the given Budget Version Id, Task Id, Resource List Member Id
236   API Created By    : Vthakkar
237   API Creation Date : 15-MAR-2004
238 */
239 
240 FUNCTION Plan_Amount_Exists_Task_Res
241                (p_budget_version_id         IN pa_budget_versions.budget_version_id%TYPE ,
242                 p_task_id                         IN pa_tasks.task_id%TYPE Default Null,
243                 p_resource_list_member_id   IN pa_resource_list_members.RESOURCE_LIST_MEMBER_ID%TYPE Default Null
244      ) RETURN VARCHAR2
245 IS
246    l_exists  varchar2(1) := 'N';
247 begin
248 
249   select 'Y'
250     into l_exists
251     from pa_budget_lines a,
252          pa_resource_assignments b
253    where a.resource_assignment_id = b.resource_assignment_id
254      and b.budget_version_id = p_budget_version_id
255       and b.task_id   = Nvl(p_task_id,b.task_id)
256       and b.resource_list_member_id = Nvl(p_resource_list_member_id,b.resource_list_member_id)
257      and rownum < 2;
258 
259   return l_exists;
260 
261 exception
262   when no_data_found then
263     return 'N';
264 end Plan_Amount_Exists_Task_Res;
265 
266 /*=============================================================================
267   This function is used to return resource list id of the finplan version
268 ==============================================================================*/
269 
270 Function Get_Resource_List_Id (
271          p_fin_plan_version_id  IN   pa_proj_fp_options.fin_plan_version_id %TYPE )
272 RETURN   pa_proj_fp_options.all_resource_list_id%TYPE
273 IS
274 
275    l_resource_list_id  pa_proj_fp_options.all_resource_list_id%TYPE;
276 
277 BEGIN
278 
279    SELECT DECODE(fin_plan_preference_code,
280                  PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, all_resource_list_id,
281                  PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY,         cost_resource_list_id,
282                  PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY,      revenue_resource_list_id)
283    INTO   l_resource_list_id
284    FROM   pa_proj_fp_options
285    WHERE  fin_plan_version_id = p_fin_plan_version_id;
286 
287    RETURN l_resource_list_id;
288 
289 END Get_Resource_List_Id;
290 
291 /*=============================================================================
292   This function is used to return time phased code of the finplan version.
293 
294   Bug :- 2634985. The api has been modified to return the time phasing details
295   irrespective of whether the budget version is of old busdget model or new
296   financial model.
297 ==============================================================================*/
298 
299 FUNCTION Get_Time_Phased_code (
300          p_fin_plan_version_id  IN   pa_proj_fp_options.fin_plan_version_id %TYPE )
301 RETURN   pa_proj_fp_options.all_time_phased_code%TYPE
302 IS
303 
304    l_budget_entry_method_code   pa_budget_versions.budget_entry_method_code%TYPE;
305    l_budget_type_code           pa_budget_versions.budget_type_code%TYPE;
306 
307    l_time_phased_code           pa_proj_fp_options.all_time_phased_code%TYPE;
308 
309 BEGIN
310 
311    -- Fetch the  budget_entry_method_code  of the budget version.
312    -- 1) If it is not null, then fetch time phasing code from pa_budget_entry_methods table
313    -- 2) If its null, fetch the tim pahsing code from the pa_proj_fp_options table.
314 
315    BEGIN
316            SELECT   budget_type_code,
317                     budget_entry_method_code
318            INTO     l_budget_type_code,
319                     l_budget_entry_method_code
320            FROM     pa_budget_versions
321            WHERE    budget_version_id = p_fin_plan_version_id;
322    EXCEPTION
323           WHEN OTHERS THEN
324               RETURN NULL;
325    END;
326 
327 
328    IF   l_budget_type_code  IS NOT NULL
329    THEN
330            BEGIN
331                    SELECT time_phased_type_code
332                    INTO   l_time_phased_code
333                    FROM   pa_budget_entry_methods
334                    WHERE  budget_entry_method_code = l_budget_entry_method_code;
335            EXCEPTION
336                   WHEN OTHERS THEN
337                       RETURN NULL;
338            END;
339    ELSE
340            BEGIN
341                    SELECT DECODE(fin_plan_preference_code,
342                                  PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, all_time_phased_code,
343                                  PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY,         cost_time_phased_code,
344                                  PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY,      revenue_time_phased_code)
345                    INTO   l_time_phased_code
346                    FROM   pa_proj_fp_options
347                    WHERE  fin_plan_version_id = p_fin_plan_version_id;
348            EXCEPTION
349                   WHEN OTHERS THEN
350                       RETURN NULL;
351            END;
352    END IF;
353 
354    RETURN l_time_phased_code;
355 
356 END Get_Time_Phased_Code;
357 
358 /*=============================================================================
359   This function is used to return plan level code of the finplan version
360 ==============================================================================*/
361 
362 FUNCTION Get_Fin_Plan_Level_Code(
363          p_fin_plan_version_id  IN   pa_proj_fp_options.fin_plan_version_id %TYPE )
364 RETURN   pa_proj_fp_options.all_fin_plan_level_code%TYPE
365 IS
366 
367    l_fin_plan_level_code  pa_proj_fp_options.all_fin_plan_level_code%TYPE;
368 
369 BEGIN
370 
371    SELECT DECODE(fin_plan_preference_code,
372                  PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, all_fin_plan_level_code,
373                  PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY,         cost_fin_plan_level_code,
374                  PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY,      revenue_fin_plan_level_code)
375    INTO   l_fin_plan_level_code
376    FROM   pa_proj_fp_options
377    WHERE  fin_plan_version_id = p_fin_plan_version_id;
378 
379    RETURN l_fin_plan_level_code;
380 
381 END Get_Fin_Plan_Level_code;
382 
383 
384 /*=============================================================================
385   This function is used to return multi currency flag for the finplan version
386 ==============================================================================*/
387 
388 FUNCTION Get_Multi_Curr_Flag(
389          p_fin_plan_version_id  IN   pa_proj_fp_options.fin_plan_version_id %TYPE )
390 RETURN   pa_proj_fp_options.plan_in_multi_curr_flag%TYPE
391 IS
392 
393    l_multi_curr_flag  pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
394 
395 BEGIN
396 
397    SELECT PLAN_IN_MULTI_CURR_FLAG
398    INTO   l_multi_curr_flag
399    FROM   pa_proj_fp_options
400    WHERE  fin_plan_version_id = p_fin_plan_version_id;
401 
402    RETURN l_multi_curr_flag;
403 
404 END Get_Multi_Curr_Flag;
405 
406 /*=============================================================================
407   This function returns planning level when an option id is input
408 ==============================================================================*/
409 
410 /* Changes for FP.M, Tracking Bug No - 3354518
411 Modifying the type of the IN parameter p_element_type
412 below as pa_fp_elements is being obsoleted. */
413 
414 FUNCTION GET_OPTION_PLANNING_LEVEL(
415          P_proj_fp_options_id  IN  pa_proj_fp_options.proj_fp_options_id%TYPE,
416 /*         p_element_type        IN  pa_fp_elements.element_type%TYPE)
417    Modified as part of Changes for FP.M, Tracking Bug No - 3354518*/
418          p_element_type        IN  pa_budget_versions.version_type%TYPE)
419 RETURN   pa_proj_fp_options.all_fin_plan_level_code%TYPE
420 IS
421    l_fin_plan_level_code  pa_proj_fp_options.all_fin_plan_level_code%TYPE;
422 
423 BEGIN
424 
425    SELECT DECODE(fin_plan_preference_code,
426                  PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, all_fin_plan_level_code,
427                  PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY,         cost_fin_plan_level_code,
428                  PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY,      revenue_fin_plan_level_code,
429                  PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP,
430                         DECODE(p_element_type,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST,cost_fin_plan_level_code,
431                                               PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE, revenue_fin_plan_level_code))
432    INTO   l_fin_plan_level_code
433    FROM   pa_proj_fp_options
434    WHERE  proj_fp_options_id = p_proj_fp_options_id;
435 
436    RETURN l_fin_plan_level_code;
437 
438 END GET_OPTION_PLANNING_LEVEL;
439 
440 
441 /*=============================================================================
442   This function is used to return amount set id  of the finplan version
443 ==============================================================================*/
444 
445 FUNCTION Get_Amount_Set_Id(
446          p_fin_plan_version_id  IN   pa_proj_fp_options.fin_plan_version_id %TYPE )
447 RETURN   pa_proj_fp_options.all_amount_set_id%TYPE
448 IS
449 
450    l_amount_set_id  pa_proj_fp_options.all_amount_set_id%TYPE;
451 
452 BEGIN
453 
454    SELECT DECODE(fin_plan_preference_code,
455                  PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME, all_amount_set_id,
456                  PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY,         cost_amount_set_id,
457                  PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY,      revenue_amount_set_id)
458    INTO   l_amount_set_id
459    FROM   pa_proj_fp_options
460    WHERE  fin_plan_version_id = p_fin_plan_version_id;
461 
462    RETURN l_amount_set_id;
463 
464 END Get_Amount_Set_Id;
465 
466 /*=============================================================================
467  Function Get_Period_Profile_End_Date
468  Created: 9/18/02 by dlai
469  Use: for Create Plan Version page VO query
470 ==============================================================================*/
471 
472 FUNCTION Get_Period_Profile_Start_Date
473         (p_period_profile_id    IN   pa_budget_versions.period_profile_id%TYPE)
474 return pa_proj_period_profiles.period_name1%TYPE is
475 l_start_date    pa_proj_period_profiles.period_name1%TYPE;
476 BEGIN
477   l_start_date := null;
478   if p_period_profile_id is not null then
479     select period_name1
480       into l_start_date
481       from pa_proj_period_profiles
482       where period_profile_id = p_period_profile_id;
483   end if; -- p_period_profile_id is not null
484   return l_start_date;
485 EXCEPTION
486   when no_data_found then
487     return l_start_date;
488 END Get_Period_Profile_Start_Date;
489 
490 
491 /*=============================================================================
492  Function Get_Period_Profile_End_Date
493  Created: 9/18/02 by dlai
494  Use: for Create Plan Version page VO query
495 ==============================================================================*/
496 FUNCTION Get_Period_Profile_End_Date
497         (p_period_profile_id    IN   pa_budget_versions.period_profile_id%TYPE)
498 return pa_proj_period_profiles.profile_end_period_name%TYPE is
499 l_end_date      pa_proj_period_profiles.profile_end_period_name%TYPE;
500 BEGIN
501   l_end_date := null;
502   if p_period_profile_id is not null then
503     select profile_end_period_name
504       into l_end_date
505       from pa_proj_period_profiles
506       where period_profile_id = p_period_profile_id;
507   end if; -- p_period_profile_id is not null
508   return l_end_date;
509 EXCEPTION
510   when no_data_found then
511     return l_end_date;
512 END Get_Period_Profile_End_Date;
513 
514 
515 /* This fuction will return  workplan budget version res_list_id */
516 FUNCTION Get_wp_bv_res_list_id
517    ( p_proj_structure_version_id NUMBER)
518 RETURN NUMBER IS
519 
520 l_wp_bv_res_list_id  NUMBER:=NULL;
521 
522 BEGIN
523     SELECT resource_list_id
524     INTO   l_wp_bv_res_list_id
525     FROM   pa_budget_versions
526     WHERE  project_structure_version_id=p_proj_structure_version_id AND
527     NVL(WP_VERSION_FLAG,'N')  = 'Y';
528 
529    RETURN l_wp_bv_res_list_id;
530 
531 EXCEPTION
532       WHEN  OTHERS THEN
533             RETURN l_wp_bv_res_list_id;
534 
535 END Get_wp_bv_res_list_id;
536 
537 /*=============================================================================
538    This function will return the time phase code
539    of the budget_version_id for a given wp_structure_version_id.
540    P->PA, G->Gl, N->None
541 ==============================================================================*/
542 FUNCTION Get_wp_bv_time_phase
543     (p_wp_structure_version_id IN PA_BUDGET_VERSIONS.PROJECT_STRUCTURE_VERSION_ID%TYPE)
544 RETURN VARCHAR2 IS
545      x_time_phased_code   PA_PROJ_FP_OPTIONS.COST_TIME_PHASED_CODE%TYPE;
546 BEGIN
547             SELECT  DECODE(BV.VERSION_TYPE,
548                            'COST', OPT.COST_TIME_PHASED_CODE,
549                            'REVENUE',OPT.REVENUE_TIME_PHASED_CODE,
550                            'ALL',OPT.ALL_TIME_PHASED_CODE)
551             INTO   x_time_phased_code
552             FROM   PA_BUDGET_VERSIONS BV, PA_PROJ_FP_OPTIONS OPT
553             WHERE  BV.BUDGET_VERSION_ID            = OPT.FIN_PLAN_VERSION_ID
554             AND    BV.PROJECT_STRUCTURE_VERSION_ID = p_wp_structure_version_id
555             AND    NVL(BV.WP_VERSION_FLAG,'N')  = 'Y';
556 
557             RETURN x_time_phased_code;
558 EXCEPTION
559     WHEN OTHERS THEN
560        RETURN null;
561 
562 END Get_wp_bv_time_phase;
563 
564 /*=============================================================================
565    This function will return the approved cost budget current baselined version.
566    If version is not available then it will return null value.
567 ==============================================================================*/
568 FUNCTION Get_app_budget_cost_cb_ver
569     (p_project_id     IN   pa_projects_all.project_id%TYPE)
570 RETURN NUMBER IS
571 
572   x_app_bdgt_cost_cb_ver  pa_budget_versions.budget_version_id%TYPE;
573 
574 BEGIN
575 
576      /* Bug 3955810.
577         In order to take care the old budget model also, removed the check
578         for version_type is not null. In old budget model, version_type was
579         not populated, but APPROVED_COST_PLAN_TYPE_FLAG is populated. */
580      SELECT budget_version_id
581      INTO   x_app_bdgt_cost_cb_ver
582      FROM   pa_budget_versions
583      WHERE  project_id     = p_project_id
584      AND    nvl(APPROVED_COST_PLAN_TYPE_FLAG,'N') = 'Y'
585      AND    budget_status_code                    = 'B'
586      AND    current_flag                          = 'Y';
587 
588      RETURN x_app_bdgt_cost_cb_ver;
589 
590 EXCEPTION
591     WHEN OTHERS THEN
592        RETURN null;
593 END Get_app_budget_cost_cb_ver;
594 
595 
596 
597 
598 /*=============================================================================
599  This api is used to return latest baselined version info for given project id,
600  plan type and version type
601 ==============================================================================*/
602 
603 PROCEDURE Get_Baselined_Version_Info(
604           p_project_id            IN   pa_projects_all.project_id%TYPE
605           ,p_fin_plan_type_id     IN   pa_budget_versions.fin_plan_type_id%TYPE
606           ,p_version_type         IN   pa_budget_versions.version_type%TYPE
607           ,x_fp_options_id         OUT  NOCOPY pa_proj_fp_options.proj_fp_options_id%TYPE --File.Sql.39 bug 4440895
608           ,x_fin_plan_version_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_version_id%TYPE --File.Sql.39 bug 4440895
609           ,x_return_status        OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
610           ,x_msg_count            OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
611           ,x_msg_data             OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
612 AS
613     --Start of variables used for debugging
614 
615     l_msg_count          NUMBER :=0;
616     l_data               VARCHAR2(2000);
617     l_msg_data           VARCHAR2(2000);
618     l_error_msg_code     VARCHAR2(30);
619     l_msg_index_out      NUMBER;
620     l_return_status      VARCHAR2(2000);
621     l_debug_mode         VARCHAR2(30);
622 
623     --End of variables used for debugging
624 
625     l_fp_preference_code    pa_proj_fp_options.fin_plan_preference_code%TYPE;
626     l_version_type          pa_budget_versions.version_type%TYPE;
627     l_baselined_version_id  pa_budget_versions.budget_version_id%TYPE;
628     l_fp_options_id         pa_proj_fp_options.proj_fp_options_id%TYPE;
629 
630 
631 BEGIN
632 
633  IF P_PA_DEBUG_MODE = 'Y' THEN
634     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Baselined_Version_Info');
635  END IF;
636     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
637     l_debug_mode := NVL(l_debug_mode, 'Y');
638     IF P_PA_DEBUG_MODE = 'Y' THEN
639        pa_debug.set_process('Get_Baselined_Version_Info: ' || 'PLSQL','LOG',l_debug_mode);
640     END IF;
641     x_msg_count := 0;
642     x_return_status := FND_API.G_RET_STS_SUCCESS;
643 
644     -- Check for business rules violations
645 
646     pa_debug.g_err_stage:='Validating input parameters';
647     IF P_PA_DEBUG_MODE = 'Y' THEN
648        pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
649     END IF;
650 
651     IF (p_project_id IS NULL) OR
652        (p_fin_plan_type_id IS NULL)
653     THEN
654 
655              pa_debug.g_err_stage:='Project_id = '||p_project_id;
656              IF P_PA_DEBUG_MODE = 'Y' THEN
657                 pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
658              END IF;
659              pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
660              IF P_PA_DEBUG_MODE = 'Y' THEN
661                 pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
662              END IF;
663 
664              PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
665                                    p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
666 
667              RAISE Invalid_Arg_Exc;
668 
669     END IF;
670 
671     --Fetch fin plan preference code
672 
673     pa_debug.g_err_stage:='Fetching fin plan preference code ';
674     IF P_PA_DEBUG_MODE = 'Y' THEN
675        pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
676     END IF;
677 
678     SELECT fin_plan_preference_code
679     INTO   l_fp_preference_code
680     FROM   pa_proj_fp_options
681     WHERE  project_id = p_project_id
682     AND    fin_plan_type_id = p_fin_plan_type_id
683     AND    fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
684 
685     IF (l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) AND
686 
687        (p_version_type IS NULL) THEN
688 
689           --In this case version_type should be passed and so raise error
690 
691           pa_debug.g_err_stage:='Version_Type = '||p_version_type;
692           IF P_PA_DEBUG_MODE = 'Y' THEN
693              pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
694           END IF;
695 
696           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
697                       p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
698 
699           RAISE Invalid_Arg_Exc;
700 
701     END IF;
702 
703     pa_debug.g_err_stage:='Parameter validation complete ';
704     IF P_PA_DEBUG_MODE = 'Y' THEN
705        pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
706     END IF;
707 
708     --Fetch  l_element_type ifn't passed and could be derived
709 
710     IF p_version_type IS NULL THEN
711 
712       IF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME THEN
713 
714          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL;
715 
716       ELSIF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY THEN
717 
718          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST;
719 
720       ELSIF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY THEN
721 
722          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE;
723 
724       END IF;
725 
726     END IF;
727 
728     --get baselined version if any
729 
730     pa_debug.g_err_stage:='Fetching Baselined Version';
731     IF P_PA_DEBUG_MODE = 'Y' THEN
732        pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
733     END IF;
734 
735     BEGIN
736 
737         SELECT budget_version_id
738         INTO   l_baselined_version_id
739         FROM   pa_budget_versions
740         WHERE  project_id = p_project_id
741         AND    fin_plan_type_id = p_fin_plan_type_id
742         AND    version_type = NVL(p_version_type,l_version_type)
743         AND    current_flag = 'Y'
744         AND    ci_id IS NULL;         -- <Patchset M:B and F impact changes : AMG:> -- Added an extra clause ci_id IS NULL--Bug # 3507156
745 
746         --Fetch fp options id using plan version id
747 
748         pa_debug.g_err_stage:='Fetching fp options id';
749         IF P_PA_DEBUG_MODE = 'Y' THEN
750            pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
751         END IF;
752 
753         SELECT proj_fp_options_id
754         INTO   l_fp_options_id
755         FROM   pa_proj_fp_options
756         WHERE  fin_plan_version_id = l_baselined_version_id;
757 
758     EXCEPTION
759 
760        WHEN NO_DATA_FOUND THEN
761 
762              l_baselined_version_id := NULL;
763 
764              l_fp_options_id := NULL;
765 
766     END;
767 
768     --return the parameters to calling program
769 
770     x_fin_plan_version_id := l_baselined_version_id;
771 
772     x_fp_options_id := l_fp_options_id;
773 
774     pa_debug.g_err_stage:='Exiting Get_Baselined_Version_Info';
775     IF P_PA_DEBUG_MODE = 'Y' THEN
776        pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
777        pa_debug.reset_err_stack;
778     END IF;
779 EXCEPTION
780 
781      WHEN Invalid_Arg_Exc THEN
782 
783           l_msg_count := FND_MSG_PUB.count_msg;
784 
785           IF l_msg_count = 1 THEN
786 
787                PA_INTERFACE_UTILS_PUB.get_messages
788                      (p_encoded        => FND_API.G_TRUE
789                       ,p_msg_index      => 1
790                       ,p_msg_count      => l_msg_count
791                       ,p_msg_data       => l_msg_data
792                       ,p_data           => l_data
793                       ,p_msg_index_out  => l_msg_index_out);
794 
795                x_msg_data := l_data;
796 
797                x_msg_count := l_msg_count;
798           ELSE
799 
800               x_msg_count := l_msg_count;
801 
802           END IF;
803 
804            x_return_status := FND_API.G_RET_STS_ERROR;
805 
806            pa_debug.g_err_stage:='Invalid Arguments Passed';
807            IF P_PA_DEBUG_MODE = 'Y' THEN
808               pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
809               pa_debug.reset_err_stack;
810 	END IF;
811            RAISE;
812 
813      WHEN Others THEN
814 
815           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
816           x_msg_count     := 1;
817           x_msg_data      := SQLERRM;
818 
819           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
820                                   ,p_procedure_name  => 'GET_BASELINED_VERSION_INFO');
821 
822           pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
823           IF P_PA_DEBUG_MODE = 'Y' THEN
824              pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
825           pa_debug.reset_err_stack;
826 	END IF;
827           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
828 
829 
830 END Get_Baselined_Version_Info;
831 
832 
833 --=========================================================================
834 --bug #3224177
835 --      Refer to Update "16-JAN-04 sagarwal" in the history above.
836 --      This has been added as part of code merge
837 -- This procedure deletes the Financial Planning data pertaining
838 -- to the project id given as input parameter from
839 --      1. pa_fp_txn_currencies
840 --      2. pa_proj_fp_options
841 --      3. pa_fp_elements
842 --      4. pa_proj_period_profiles
843 --==========================================================================
844 
845 procedure Delete_Fp_Options(
846  p_project_id            IN       PA_FP_TXN_CURRENCIES.PROJECT_ID%TYPE
847   , x_err_code           IN OUT   NOCOPY NUMBER) --File.Sql.39 bug 4440895
848   is
849   begin
850 
851   -- delete from pa_proj_fp_options table
852     delete from pa_proj_fp_options where project_id=p_project_id;
853 
854   -- delete from pa_fp_txn_currencies table
855     delete from pa_fp_txn_currencies where project_id=p_project_id;
856 
857 /* Changes for FPM, Tracking Bug No - 3354518
858    Commenting out code below   for delete statment from pa_fp_elements
859    as this table is getting obsoleted */
860    -- delete from pa_fp_elements table
861 /*    delete from pa_fp_elements where project_id=p_project_id; */
862 
863     -- delete from pa_proj_period_profiles table
864     delete from pa_proj_period_profiles where project_id=p_project_id;
865 
866     /* Bug 3683382 this delete is not required functionally as records can not
867        exist for a project level option in this table
868     -- delete from pa_resource_assignments table
869     delete from pa_resource_assignments where project_id = p_project_id;
870     */
871 
872    /*start of bug 3342975 Refer to Update "16-JAN-04 sagarwal"
873     in the history above. This has been added as part of code merge */
874     -- delete from pa_fp_excluded_elements table
875 
876 /* Changes for FPM, Tracking Bug No - 3354518
877    Commenting out code below   for delete statment from pa_fp_excluded_elements
878    as this table is getting obsoleted */
879 /* delete from pa_fp_excluded_elements where project_id = p_project_id; */
880 
881     -- delete from PA_FP_UPGRADE_AUDIT table
882     delete from PA_FP_UPGRADE_AUDIT where project_id = p_project_id;
883 
884       /*end of bug 3342975 */
885    exception
886         when others then
887                  x_err_code := SQLCODE;
888                 rollback;
889          return;
890 
891   end Delete_Fp_Options;
892 --=========================================================================
893 --bug #3224177
894 --      Refer to Update "16-JAN-04 sagarwal" in the history above.
895 --      This has been added as part of code merge
896 -- This procedure updates the Project Currency in pa_fp_txn_currencies
897 -- whenever Porject currency is updated
898 
899 -- FP M Phase II Development
900 -- Bug 3583619
901 --     Whenever there is a change in PC for a project, the change should be
902 --     propogated for workplan settings as well. In this case, validation api
903 --     takes care of not allowing the change if there is already any amount.
904 --     But, we can not disallow the change for the fact that there is workplan
905 --     plan type attached or workplan versions are present as user has no way
906 --     deleting the workplan plan type attached. This implies that all the fp
907 --     options of the project should be updated when this api is called.
908 --     Rewritten the complete api to update multiple options
909 --==========================================================================
910 PROCEDURE Update_Txn_Currencies
911     (p_project_id        IN        PA_FP_TXN_CURRENCIES.PROJECT_ID%TYPE
912      ,p_proj_curr_code   IN        PA_FP_TXN_CURRENCIES.TXN_CURRENCY_CODE%TYPE)
913 is
914      cursor get_all_fp_options_cur is
915         select proj_fp_options_id
916         from   pa_proj_fp_options
917         where  project_id = p_project_id;
918 
919      get_all_fp_options_rec  get_all_fp_options_cur%ROWTYPE;
920 
921      cursor get_project_currency (c_proj_fp_options_id NUMBER)is
922         select fp_txn_currency_id,
923                txn_currency_code
924         from  pa_fp_txn_currencies
925         where project_id = p_project_id
926         and   project_currency_flag='Y'
927         and   proj_fp_options_id = c_proj_fp_options_id;
928 
929      cursor get_proj_func_currency (c_proj_fp_options_id NUMBER)is
930         select fp_txn_currency_id,
931                txn_currency_code
932         from  pa_fp_txn_currencies
933         where project_id = p_project_id
934         and   projfunc_currency_flag='Y'
935         and   proj_fp_options_id = c_proj_fp_options_id;
936 
937      cursor check_proj_currency_exists (c_proj_fp_options_id NUMBER)is
938          select fp_txn_currency_id
939          from   pa_fp_txn_currencies
940          where  project_id = p_project_id
941          and    txn_currency_code = p_proj_curr_code
942          and    project_currency_flag='N'
943          and    proj_fp_options_id = c_proj_fp_options_id;
944 
945      /* Bug 5364011: The following code is introduced to update the plan_in_multi_curr_flag as 'Y'
946         in pa_proj_fp_options, if the newly entered project currency is different from the
947         existing project funtional currency. */
948      TYPE plan_in_multi_curr_tbl IS TABLE OF pa_proj_fp_options.proj_fp_options_id%TYPE
949      INDEX BY BINARY_INTEGER;
950      l_plan_in_multi_curr_tbl plan_in_multi_curr_tbl;
951      cnt NUMBER := 0;
952 
953      l_proj_curr_code       pa_fp_txn_currencies.txn_currency_code%TYPE;
954      l_projfunc_curr_code   pa_fp_txn_currencies.txn_currency_code%TYPE;
955      l_txn_currency_id      NUMBER;
956      l_pc_currency_id       NUMBER;
957      l_pfc_currency_id      NUMBER;
958 
959  begin
960      open get_all_fp_options_cur;
961      loop
962          l_pc_currency_id := NULL;
963          l_proj_curr_code := NULL;
964          l_txn_currency_id := NULL;
965          l_projfunc_curr_code := NULL;
966          l_pfc_currency_id   := NULL;
967 
968          fetch get_all_fp_options_cur
969           into  get_all_fp_options_rec;
970 
971          exit when get_all_fp_options_cur%NOTFOUND;
972 
973          -- for each of the options found update project currency
974 
975          open get_project_currency(get_all_fp_options_rec.proj_fp_options_id);
976            fetch get_project_currency
977             into l_pc_currency_id,l_proj_curr_code;
978          close get_project_currency;
979 
980          open get_proj_func_currency(get_all_fp_options_rec.proj_fp_options_id);
981            fetch get_proj_func_currency
982            into l_pfc_currency_id,l_projfunc_curr_code;
983          close get_proj_func_currency;
984 
985          if l_proj_curr_code is not null then
986              if trim(l_proj_curr_code) <> trim(p_proj_curr_code) then
987 
988                  open check_proj_currency_exists(get_all_fp_options_rec.proj_fp_options_id);
989                   fetch check_proj_currency_exists
990                    into l_txn_currency_id;
991                  close check_proj_currency_exists;
992 
993                  If trim(l_proj_curr_code) <> trim(l_projfunc_curr_code) then
994 
995                       -- if project currency is not equal to project functional currency
996 
997                        if (l_txn_currency_id is not NULL) then
998 
999                            -- delete the old project currency
1000 
1001                            delete from pa_fp_txn_currencies
1002                            where fp_txn_currency_id = l_txn_currency_id;
1003 
1004                            -- check if the new project currency selected by user is PFC
1005                            if (l_pfc_currency_id <> l_txn_currency_id) then
1006 
1007                                update pa_fp_txn_currencies
1008                                set txn_currency_code = p_proj_curr_code
1009                                where fp_txn_currency_id = l_pc_currency_id;
1010 
1011                            else
1012                                -- new PC selected by user is PFC
1013 
1014                                update pa_fp_txn_currencies
1015                                set txn_currency_code = p_proj_curr_code,
1016                                    projfunc_currency_flag = 'Y'
1017                                where fp_txn_currency_id = l_pc_currency_id;
1018                            end if; -- END FOR l_pfc_currency_id <> l_txn_currency_id
1019                        else
1020                            update pa_fp_txn_currencies
1021                            set txn_currency_code = p_proj_curr_code
1022                            where fp_txn_currency_id = l_pc_currency_id;
1023 
1024                        end if;
1025                  else
1026                      -- project currency and project functional currency are the same
1027                      -- update PC flag to N
1028 
1029                      update pa_fp_txn_currencies
1030                      set project_currency_flag='N'
1031                      where fp_txn_currency_id = l_pc_currency_id;
1032 
1033                      if (l_txn_currency_id is not NULL) then
1034                          -- if already existing txn currency is selected as PC
1035                          update pa_fp_txn_currencies
1036                          set project_currency_flag='Y'
1037                          where fp_txn_currency_id = l_txn_currency_id;
1038                      else
1039                          -- insert the new PC
1040 
1041                          INSERT INTO PA_FP_TXN_CURRENCIES (
1042                                               fp_txn_currency_id
1043                                               ,proj_fp_options_id
1044                                               ,project_id
1045                                               ,fin_plan_type_id
1046                                               ,fin_plan_version_id
1047                                               ,txn_currency_code
1048                                               ,default_rev_curr_flag
1049                                               ,default_cost_curr_flag
1050                                               ,default_all_curr_flag
1051                                               ,project_currency_flag
1052                                               ,projfunc_currency_flag
1053                                               ,last_update_date
1054                                               ,last_updated_by
1055                                               ,creation_date
1056                                               ,created_by
1057                                               ,last_update_login
1058                                               ,project_cost_exchange_rate
1059                                               ,project_rev_exchange_rate
1060                                               ,projfunc_cost_exchange_Rate
1061                                               ,projfunc_rev_exchange_Rate
1062                                               )
1063                                   SELECT pa_fp_txn_currencies_s.NEXTVAL
1064                                  ,      PROJ_FP_OPTIONS_ID
1065                                  ,      PROJECT_ID
1066                                  ,      FIN_PLAN_TYPE_ID
1067                                  ,      FIN_PLAN_VERSION_ID
1068                                  ,      p_proj_curr_code
1069                                  ,      DEFAULT_REV_CURR_FLAG
1070                                  ,      DEFAULT_COST_CURR_FLAG
1071                                  ,      DEFAULT_ALL_CURR_FLAG
1072                                  ,     'Y'
1073                                  ,     'N'
1074                                  ,      sysdate
1075                                  ,      fnd_global.user_id
1076                                  ,      sysdate
1077                                  ,      fnd_global.user_id
1078                                  ,      fnd_global.login_id
1079                                  ,      PROJECT_COST_EXCHANGE_RATE
1080                                  ,      PROJECT_REV_EXCHANGE_RATE
1081                                  ,      PROJFUNC_COST_EXCHANGE_RATE
1082                                  ,      PROJFUNC_REV_EXCHANGE_RATE
1083                                  FROM PA_FP_TXN_CURRENCIES
1084                                  where fp_txn_currency_id = l_pc_currency_id;
1085                      end if; -- l_txn_currency_id is not NULL ends
1086                  end if; -- l_proj_curr_code <> l_projfunc_curr_code ends
1087              end if; -- trim(l_proj_curr_code) <> trim(p_proj_curr_code)
1088          end if;
1089 
1090          /* Bug 5364011: The following code is introduced to update the plan_in_multi_curr_flag as 'Y'
1091             in pa_proj_fp_options, if the newly entered project currency is different from the existing
1092             project functional currency. */
1093             IF trim(p_proj_curr_code) <> trim(l_projfunc_curr_code) THEN
1094                 cnt := cnt+1;
1095                 l_plan_in_multi_curr_tbl(cnt) := get_all_fp_options_rec.proj_fp_options_id;
1096             END IF;
1097 
1098      end loop;
1099          /* Bug 5364011: The following code is introduced to update the plan_in_multi_curr_flag as 'Y'
1100             in pa_proj_fp_options, if the newly entered project currency is different from the existing
1101             project functional currency. */
1102            IF l_plan_in_multi_curr_tbl.COUNT > 0 THEN
1103               FORALL opt IN l_plan_in_multi_curr_tbl.FIRST..l_plan_in_multi_curr_tbl.LAST
1104                  UPDATE pa_proj_fp_options
1105                  SET    plan_in_multi_curr_flag = 'Y',
1106                         record_version_number = record_version_number+1
1107                  WHERE  proj_fp_options_id = l_plan_in_multi_curr_tbl(opt);
1108            END IF;
1109            l_plan_in_multi_curr_tbl.DELETE;
1110 
1111      close get_all_fp_options_cur;
1112 
1113 
1114 exception
1115         when others then
1116         rollback;
1117         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1118 
1119 end Update_Txn_Currencies;
1120 
1121 
1122 /*=============================================================================
1123  This api is used to return current working version info for given plan type,
1124  project id and version type
1125 ==============================================================================*/
1126 
1127 PROCEDURE Get_Curr_Working_Version_Info(
1128           p_project_id            IN   pa_projects_all.project_id%TYPE
1129           ,p_fin_plan_type_id     IN   pa_budget_versions.fin_plan_type_id%TYPE
1130           ,p_version_type         IN   pa_budget_versions.version_type%TYPE
1131           ,x_fp_options_id         OUT  NOCOPY pa_proj_fp_options.proj_fp_options_id%TYPE --File.Sql.39 bug 4440895
1132           ,x_fin_plan_version_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_version_id%TYPE --File.Sql.39 bug 4440895
1133           ,x_return_status        OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1134           ,x_msg_count            OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
1135           ,x_msg_data             OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1136 AS
1137 
1138     --Start of variables used for debugging
1139 
1140     l_msg_count          NUMBER :=0;
1141     l_data               VARCHAR2(2000);
1142     l_msg_data           VARCHAR2(2000);
1143     l_error_msg_code     VARCHAR2(30);
1144     l_msg_index_out      NUMBER;
1145     l_return_status      VARCHAR2(2000);
1146     l_debug_mode         VARCHAR2(30);
1147 
1148     --End of variables used for debugging
1149 
1150 
1151     l_fp_preference_code  pa_proj_fp_options.fin_plan_preference_code%TYPE;
1152     l_version_type        pa_budget_versions.version_type%TYPE;
1153     l_current_working_version_id pa_budget_versions.budget_version_id%TYPE;
1154     l_fp_options_id       pa_proj_fp_options.proj_fp_options_id%TYPE;
1155 
1156 
1157 BEGIN
1158 
1159    IF P_PA_DEBUG_MODE = 'Y' THEN
1160     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info');
1161    END IF;
1162     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1163     l_debug_mode := NVL(l_debug_mode, 'Y');
1164     IF P_PA_DEBUG_MODE = 'Y' THEN
1165        pa_debug.set_process('Get_Curr_Working_Version_Info: ' || 'PLSQL','LOG',l_debug_mode);
1166     END IF;
1167     x_msg_count := 0;
1168     x_return_status := FND_API.G_RET_STS_SUCCESS;
1169 
1170     -- Check for business rules violations
1171 
1172     pa_debug.g_err_stage:='Validating input parameters';
1173     IF P_PA_DEBUG_MODE = 'Y' THEN
1174        pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1175     END IF;
1176 
1177     IF (p_project_id IS NULL) OR
1178        (p_fin_plan_type_id IS NULL)
1179     THEN
1180 
1181              pa_debug.g_err_stage:='Project_id = '||p_project_id;
1182              IF P_PA_DEBUG_MODE = 'Y' THEN
1183                 pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1184              END IF;
1185              pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
1186              IF P_PA_DEBUG_MODE = 'Y' THEN
1187                 pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1188              END IF;
1189 
1190              PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1191                                    p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1192 
1193              RAISE Invalid_Arg_Exc;
1194 
1195     END IF;
1196 
1197     --Fetch fin plan preference code
1198 
1199     pa_debug.g_err_stage:='Fetching fin plan preference code ';
1200     IF P_PA_DEBUG_MODE = 'Y' THEN
1201        pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1202     END IF;
1203 
1204     SELECT fin_plan_preference_code
1205     INTO   l_fp_preference_code
1206     FROM   pa_proj_fp_options
1207     WHERE  project_id = p_project_id
1208     AND    fin_plan_type_id = p_fin_plan_type_id
1209     AND    fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
1210 
1211     IF (l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) AND
1212 
1213        (p_version_type IS NULL) THEN
1214 
1215           --In this case version_type should be passed and so raise error
1216 
1217           pa_debug.g_err_stage:='Version_Type = '||p_version_type;
1218           IF P_PA_DEBUG_MODE = 'Y' THEN
1219              pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1220           END IF;
1221 
1222           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1223                       p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1224 
1225           RAISE Invalid_Arg_Exc;
1226 
1227     END IF;
1228 
1229     pa_debug.g_err_stage:='Parameter validation complete ';
1230     IF P_PA_DEBUG_MODE = 'Y' THEN
1231        pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1232     END IF;
1233 
1234     --Fetch  l_element_type ifn't passed and could be derived
1235 
1236     IF p_version_type IS NULL THEN
1237 
1238       IF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME THEN
1239 
1240          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL;
1241 
1242       ELSIF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY THEN
1243 
1244          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST;
1245 
1246       ELSIF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY THEN
1247 
1248          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE;
1249 
1250       END IF;
1251 
1252     END IF;
1253 
1254     --Fetch the current working version
1255 
1256     BEGIN
1257 
1258         pa_debug.g_err_stage:='Fetching current working Version';
1259         IF P_PA_DEBUG_MODE = 'Y' THEN
1260            pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1261         END IF;
1262 
1263         SELECT budget_version_id
1264         INTO   l_current_working_version_id
1265         FROM   pa_budget_versions
1266         WHERE  project_id = p_project_id
1267         AND    fin_plan_type_id = p_fin_plan_type_id
1268         AND    version_type = NVL(p_version_type,l_version_type)
1269         AND    current_working_flag = 'Y'
1270         AND    ci_id IS NULL;         -- <Patchset M:B and F impact changes : AMG:> -- Added an extra clause ci_id IS NULL--Bug # 3507156
1271 
1272         --Fetch fp options id using plan version id
1273 
1274         pa_debug.g_err_stage:='Fetching fp option id';
1275         IF P_PA_DEBUG_MODE = 'Y' THEN
1276            pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1277         END IF;
1278 
1279         SELECT proj_fp_options_id
1280         INTO   l_fp_options_id
1281         FROM   pa_proj_fp_options
1282         WHERE  fin_plan_version_id = l_current_working_version_id;
1283 
1284     EXCEPTION
1285 
1286        WHEN NO_DATA_FOUND THEN
1287 
1288              l_current_working_version_id := NULL;
1289 
1290              l_fp_options_id := NULL;
1291 
1292     END;
1293 
1294     --return the parameters to calling programme
1295 
1296     x_fin_plan_version_id := l_current_working_version_id;
1297 
1298     x_fp_options_id := l_fp_options_id;
1299 
1300     pa_debug.g_err_stage:='Exiting Get_Curr_Working _Version_Info';
1301     IF P_PA_DEBUG_MODE = 'Y' THEN
1302        pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1303     pa_debug.reset_err_stack;
1304 END IF;
1305 EXCEPTION
1306 
1307      WHEN Invalid_Arg_Exc THEN
1308 
1309           l_msg_count := FND_MSG_PUB.count_msg;
1310 
1311           IF l_msg_count = 1 THEN
1312 
1313                PA_INTERFACE_UTILS_PUB.get_messages
1314                      (p_encoded        => FND_API.G_TRUE
1315                       ,p_msg_index      => 1
1316                       ,p_msg_count      => l_msg_count
1317                       ,p_msg_data       => l_msg_data
1318                       ,p_data           => l_data
1319                       ,p_msg_index_out  => l_msg_index_out);
1320 
1321                x_msg_data := l_data;
1322 
1323                x_msg_count := l_msg_count;
1324           ELSE
1325 
1326               x_msg_count := l_msg_count;
1327 
1328           END IF;
1329 
1330            x_return_status := FND_API.G_RET_STS_ERROR;
1331 
1332            pa_debug.g_err_stage:='Invalid Arguments Passed';
1333            IF P_PA_DEBUG_MODE = 'Y' THEN
1334               pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1335 
1336            pa_debug.reset_err_stack;
1337 	END IF;
1338            RAISE;
1339 
1340      WHEN Others THEN
1341 
1342           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1343           x_msg_count     := 1;
1344           x_msg_data      := SQLERRM;
1345 
1346           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
1347                                   ,p_procedure_name  => 'Get_Curr_Working_Version_Info');
1348 
1349           pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
1350           IF P_PA_DEBUG_MODE = 'Y' THEN
1351              pa_debug.write('Get_Curr_Working_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1352 
1353           pa_debug.reset_err_stack;
1354 	END IF;
1355           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1356 
1357 
1358 END Get_Curr_Working_Version_Info;
1359 
1360 /*=============================================================================
1361  This api is used to return approved cost plan type for given project
1362 ==============================================================================*/
1363 
1364 
1365 PROCEDURE Get_Appr_Cost_Plan_Type_Info(
1366           p_project_id     IN   pa_projects_all.project_id%TYPE
1367           ,x_plan_type_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
1368           ,x_return_status OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1369           ,x_msg_count     OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
1370           ,x_msg_data      OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1371 AS
1372 
1373     --Start of variables used for debugging
1374 
1375     l_msg_count          NUMBER :=0;
1376     l_data               VARCHAR2(2000);
1377     l_msg_data           VARCHAR2(2000);
1378     l_error_msg_code     VARCHAR2(30);
1379     l_msg_index_out      NUMBER;
1380     l_return_status      VARCHAR2(2000);
1381     l_debug_mode         VARCHAR2(30);
1382 
1383     --End of variables used for debugging
1384 
1385     l_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
1386 
1387 
1388 BEGIN
1389 
1390  IF P_PA_DEBUG_MODE = 'Y' THEN
1391     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Appr_Cost_Plan_Type_Info');
1392 END IF;
1393     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1394     l_debug_mode := NVL(l_debug_mode, 'Y');
1395     IF P_PA_DEBUG_MODE = 'Y' THEN
1396        pa_debug.set_process('Get_Appr_Cost_Plan_Type_Info: ' || 'PLSQL','LOG',l_debug_mode);
1397     END IF;
1398     x_msg_count := 0;
1399     x_return_status := FND_API.G_RET_STS_SUCCESS;
1400 
1401     -- Check for business rules violations
1402 
1403     pa_debug.g_err_stage:='Validating input parameters';
1404     IF P_PA_DEBUG_MODE = 'Y' THEN
1405        pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1406     END IF;
1407 
1408     -- project_id can't be null
1409 
1410     IF (p_project_id IS NULL)   THEN
1411 
1412         pa_debug.g_err_stage:='Project_id = '||p_project_id;
1413         IF P_PA_DEBUG_MODE = 'Y' THEN
1414            pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1415         END IF;
1416 
1417         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1418                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1419 
1420         RAISE Invalid_Arg_Exc;
1421 
1422     END IF;
1423 
1424     pa_debug.g_err_stage:='Parameter validation complete ';
1425     IF P_PA_DEBUG_MODE = 'Y' THEN
1426        pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1427     END IF;
1428 
1429     --Fetch approved plan type id
1430 
1431     BEGIN
1432 
1433         pa_debug.g_err_stage:='Fetching approved cost plan type id';
1434         IF P_PA_DEBUG_MODE = 'Y' THEN
1435            pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1436         END IF;
1437 
1438         SELECT fin_plan_type_id
1439         INTO   l_fin_plan_type_id
1440         FROM   pa_proj_fp_options
1441         WHERE  project_id = p_project_id
1442           AND  fin_plan_option_level_code = 'PLAN_TYPE'
1443           AND  approved_cost_plan_type_flag = 'Y';
1444 
1445     EXCEPTION
1446 
1447          WHEN  NO_DATA_FOUND THEN
1448 
1449                l_fin_plan_type_id := NULL;
1450 
1451     END;
1452 
1453     --return the plan type id
1454 
1455     x_plan_type_id := l_fin_plan_type_id ;
1456 
1457     pa_debug.g_err_stage:='Exiting Get_Appr_Cost_Plan_Type_Info';
1458     IF P_PA_DEBUG_MODE = 'Y' THEN
1459        pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1460 
1461     pa_debug.reset_err_stack;
1462 END IF;
1463 EXCEPTION
1464 
1465      WHEN Invalid_Arg_Exc THEN
1466 
1467           l_msg_count := FND_MSG_PUB.count_msg;
1468 
1469           IF l_msg_count = 1 THEN
1470 
1471                PA_INTERFACE_UTILS_PUB.get_messages
1472                      (p_encoded        => FND_API.G_TRUE
1473                       ,p_msg_index      => 1
1474                       ,p_msg_count      => l_msg_count
1475                       ,p_msg_data       => l_msg_data
1476                       ,p_data           => l_data
1477                       ,p_msg_index_out  => l_msg_index_out);
1478 
1479                x_msg_data := l_data;
1480 
1481                x_msg_count := l_msg_count;
1482           ELSE
1483 
1484               x_msg_count := l_msg_count;
1485 
1486           END IF;
1487 
1488            x_return_status := FND_API.G_RET_STS_ERROR;
1489 
1490            pa_debug.g_err_stage:='Invalid Arguments Passed';
1491            IF P_PA_DEBUG_MODE = 'Y' THEN
1492               pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1493 
1494            pa_debug.reset_err_stack;
1495 	END IF;
1496            RAISE;
1497 
1498      WHEN Others THEN
1499 
1500           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1501           x_msg_count     := 1;
1502           x_msg_data      := SQLERRM;
1503 
1504           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
1505                                   ,p_procedure_name  => 'Get_Appr_Cost_Plan_Type_Info');
1506 
1507           pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
1508           IF P_PA_DEBUG_MODE = 'Y' THEN
1509              pa_debug.write('Get_Appr_Cost_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1510 
1511           pa_debug.reset_err_stack;
1512 	END IF;
1513           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1514 
1515 END Get_Appr_Cost_Plan_Type_Info;
1516 
1517 /*=============================================================================
1518  This api is used to return approved rev plan type for given project
1519 ==============================================================================*/
1520 
1521 PROCEDURE Get_Appr_Rev_Plan_Type_Info(
1522           p_project_id     IN   pa_projects_all.project_id%TYPE
1523           ,x_plan_type_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
1524           ,x_return_status OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1525           ,x_msg_count     OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
1526           ,x_msg_data      OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1527 AS
1528     --Start of variables used for debugging
1529 
1530     l_msg_count          NUMBER :=0;
1531     l_data               VARCHAR2(2000);
1532     l_msg_data           VARCHAR2(2000);
1533     l_error_msg_code     VARCHAR2(30);
1534     l_msg_index_out      NUMBER;
1535     l_return_status      VARCHAR2(2000);
1536     l_debug_mode         VARCHAR2(30);
1537 
1538     --End of variables used for debugging
1539 
1540     l_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
1541 
1542 
1543 BEGIN
1544 
1545  IF P_PA_DEBUG_MODE = 'Y' THEN
1546     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Appr_Rev_Plan_Type_Info');
1547 END IF;
1548     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1549     l_debug_mode := NVL(l_debug_mode, 'Y');
1550     IF P_PA_DEBUG_MODE = 'Y' THEN
1551        pa_debug.set_process('Get_Appr_Rev_Plan_Type_Info: ' || 'PLSQL','LOG',l_debug_mode);
1552     END IF;
1553     x_msg_count := 0;
1554     x_return_status := FND_API.G_RET_STS_SUCCESS;
1555 
1556     -- Check for business rules violations
1557 
1558     pa_debug.g_err_stage:='Validating input parameters';
1559     IF P_PA_DEBUG_MODE = 'Y' THEN
1560        pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1561     END IF;
1562 
1563     -- project_id can't be null
1564 
1565     IF (p_project_id IS NULL)   THEN
1566 
1567         pa_debug.g_err_stage:='Project_id = '||p_project_id;
1568         IF P_PA_DEBUG_MODE = 'Y' THEN
1569            pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1570         END IF;
1571 
1572         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1573                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
1574 
1575         RAISE Invalid_Arg_Exc;
1576 
1577     END IF;
1578 
1579     pa_debug.g_err_stage:='Parameter validation complete ';
1580     IF P_PA_DEBUG_MODE = 'Y' THEN
1581        pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1582     END IF;
1583 
1584     --Fetch approved plan type id
1585 
1586     BEGIN
1587 
1588         pa_debug.g_err_stage:='Fetching approved rev plan type id';
1589         IF P_PA_DEBUG_MODE = 'Y' THEN
1590            pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1591         END IF;
1592 
1593         SELECT fin_plan_type_id
1594         INTO   l_fin_plan_type_id
1595         FROM   pa_proj_fp_options
1596         WHERE  project_id = p_project_id
1597           AND  fin_plan_option_level_code = 'PLAN_TYPE'
1598           AND  approved_rev_plan_type_flag = 'Y';
1599 
1600     EXCEPTION
1601 
1602         WHEN NO_DATA_FOUND THEN
1603 
1604               l_fin_plan_type_id := NULL;
1605 
1606     END;
1607 
1608     --return the plan type id
1609 
1610     x_plan_type_id := l_fin_plan_type_id ;
1611 
1612     pa_debug.g_err_stage:='Exiting Get_Appr_Rev_Plan_Type_Info';
1613     IF P_PA_DEBUG_MODE = 'Y' THEN
1614 	       pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,3);
1615 	    pa_debug.reset_err_stack;
1616 	END IF;
1617 EXCEPTION
1618 
1619      WHEN Invalid_Arg_Exc THEN
1620 
1621           l_msg_count := FND_MSG_PUB.count_msg;
1622 
1623           IF l_msg_count = 1 THEN
1624 
1625                PA_INTERFACE_UTILS_PUB.get_messages
1626 	                     (p_encoded        => FND_API.G_TRUE
1627                       ,p_msg_index      => 1
1628                       ,p_msg_count      => l_msg_count
1629                       ,p_msg_data       => l_msg_data
1630                       ,p_data           => l_data
1631                       ,p_msg_index_out  => l_msg_index_out);
1632 
1633                x_msg_data := l_data;
1634 
1635                x_msg_count := l_msg_count;
1636           ELSE
1637 
1638               x_msg_count := l_msg_count;
1639 
1640           END IF;
1641 
1642            x_return_status := FND_API.G_RET_STS_ERROR;
1643 
1644            pa_debug.g_err_stage:='Invalid Arguments Passed';
1645            IF P_PA_DEBUG_MODE = 'Y' THEN
1646               pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1647 
1648            pa_debug.reset_err_stack;
1649 	END IF;
1650            RAISE;
1651 
1652      WHEN Others THEN
1653 
1654           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1655           x_msg_count     := 1;
1656           x_msg_data      := SQLERRM;
1657 
1658           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
1659                                   ,p_procedure_name  => 'Get_Appr_Rev_Plan_Type_Info');
1660 
1661           pa_debug.g_err_stage:='Unexpeted Error'||SQLERRM;
1662           IF P_PA_DEBUG_MODE = 'Y' THEN
1663              pa_debug.write('Get_Appr_Rev_Plan_Type_Info: ' || l_module_name,pa_debug.g_err_stage,5);
1664           pa_debug.reset_err_stack;
1665 END IF;
1666           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1667 
1668 END Get_Appr_Rev_Plan_Type_Info;
1669 
1670 /*=================================================================================
1671 The procedure gets the amount set id for the combination of in flags if exists or
1672 creates a new record in PA_FIN_PLAN_AMOUNT_SETS and returns it.
1673 
1674 ===================================================================================*/
1675 PROCEDURE GET_OR_CREATE_AMOUNT_SET_ID
1676 (
1677         p_raw_cost_flag          IN  pa_fin_plan_amount_sets.raw_cost_flag%TYPE,
1678         p_burdened_cost_flag     IN  pa_fin_plan_amount_sets.burdened_cost_flag%TYPE,
1679         p_revenue_flag           IN  pa_fin_plan_amount_sets.revenue_flag%TYPE,
1680         p_cost_qty_flag          IN  pa_fin_plan_amount_sets.cost_qty_flag%TYPE,
1681         p_revenue_qty_flag       IN  pa_fin_plan_amount_sets.revenue_qty_flag%TYPE,
1682         p_all_qty_flag           IN  pa_fin_plan_amount_sets.all_qty_flag%TYPE,
1683         p_plan_pref_code         IN  pa_proj_fp_options.fin_plan_preference_code%TYPE,
1684 /* Changes for FP.M, Tracking Bug No - 3354518
1685 Adding three new IN parameters p_bill_rate_flag,
1686 p_cost_rate_flag, p_burden_rate below for
1687 new columns in pa_fin_plan_amount_sets */
1688         p_bill_rate_flag         IN  pa_fin_plan_amount_sets.bill_rate_flag%TYPE,
1689          p_cost_rate_flag         IN  pa_fin_plan_amount_sets.cost_rate_flag%TYPE,
1690          p_burden_rate_flag       IN  pa_fin_plan_amount_sets.burden_rate_flag%TYPE,
1691         x_cost_amount_set_id     OUT NOCOPY pa_fin_plan_amount_sets.fin_plan_amount_set_id%TYPE, --File.Sql.39 bug 4440895
1692         x_revenue_amount_set_id  OUT NOCOPY pa_fin_plan_amount_sets.fin_plan_amount_set_id%TYPE, --File.Sql.39 bug 4440895
1693         x_all_amount_set_id      OUT NOCOPY pa_fin_plan_amount_sets.fin_plan_amount_set_id%TYPE, --File.Sql.39 bug 4440895
1694         x_message_count          OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
1695         x_return_status          OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
1696         x_message_data           OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1697 )
1698 IS
1699 
1700 pragma autonomous_transaction;
1701 
1702 l_status          VARCHAR2(10);
1703 l_debug_mode      VARCHAR2(30);
1704 l_msg_count       NUMBER := 0;
1705 l_data            VARCHAR2(2000);
1706 l_msg_data        VARCHAR2(2000);
1707 l_error_msg_code  VARCHAR2(30);
1708 l_msg_index_out   NUMBER;
1709 l_return_status   VARCHAR2(2000);
1710 
1711 
1712 
1713 BEGIN
1714  IF P_PA_DEBUG_MODE = 'Y' THEN
1715         pa_debug.set_err_stack ('PA_FIN_PLAN_UTILS.GET_OR_CREATE_AMOUNT_SET_ID');
1716 END IF;
1717         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1718         l_debug_mode := NVL(l_debug_mode, 'Y');
1719         IF P_PA_DEBUG_MODE = 'Y' THEN
1720            pa_debug.set_process('GET_OR_CREATE_AMOUNT_SET_ID: ' || 'PLSQL','LOG',l_debug_mode);
1721         END IF;
1722         x_message_count := 0;
1723 
1724         x_return_status := FND_API.G_RET_STS_SUCCESS;
1725 
1726 
1727         -- Check for business rules violations
1728 
1729         IF P_PA_DEBUG_MODE = 'Y' THEN
1730            pa_debug.g_err_stage := 'Parameter Validation';
1731            pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
1732         END IF;
1733 
1734         -- Check for all flags and preference code being null
1735 
1736 /* Changes for FP.M, Tracking Bug No - 3354518
1737 Adding checks for null value for three new
1738 parameters p_bill_rate_flag,p_cost_rate_flag,
1739 and p_burden_rate below based on the new
1740 columns in pa_fin_plan_amount_sets */
1741 
1742         IF  p_raw_cost_flag is null or
1743             p_burdened_cost_flag is null or
1744             p_revenue_flag is null or
1745             p_cost_qty_flag is null or
1746             p_revenue_qty_flag is null or
1747             p_all_qty_flag is null or
1748             p_plan_pref_code is null or
1749              p_bill_rate_flag is null or
1750             p_cost_rate_flag is null or
1751             p_burden_rate_flag is null
1752             THEN
1753 
1754             IF P_PA_DEBUG_MODE = 'Y' THEN
1755                 pa_debug.g_err_stage := 'All null flags or preference code is null';
1756                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1757 
1758                 pa_debug.g_err_stage := 'preference code is ' || p_plan_pref_code;
1759                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1760 
1761                 pa_debug.g_err_stage := 'raw cost flag is ' || p_raw_cost_flag;
1762                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1763 
1764                 pa_debug.g_err_stage := 'burdened cost flag is ' || p_burdened_cost_flag;
1765                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1766 
1767                 pa_debug.g_err_stage := 'cost quantity flag is ' || p_cost_qty_flag;
1768                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1769 
1770                 pa_debug.g_err_stage := 'revenue quantity flag is ' || p_revenue_qty_flag;
1771                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1772 
1773                 pa_debug.g_err_stage := 'all quantity flag is ' || p_all_qty_flag;
1774                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1775 
1776                 pa_debug.g_err_stage := 'revenue flag is ' || p_revenue_flag;
1777                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1778             END IF;
1779 
1780 /* Changes for FP.M, Tracking Bug No - 3354518
1781 Adding debug code for three new
1782 parameters p_bill_rate_flag,p_cost_rate_flag,
1783 and p_burden_rate below based on the new
1784 columns in pa_fin_plan_amount_sets */
1785 
1786 
1787             IF P_PA_DEBUG_MODE = 'Y' THEN
1788                 pa_debug.g_err_stage := 'bill rate flag is ' || p_bill_rate_flag;
1789                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1790 
1791                 pa_debug.g_err_stage := 'cost rate flag is ' || p_cost_rate_flag;
1792                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1793 
1794                 pa_debug.g_err_stage := 'burden rate flag is ' || p_burden_rate_flag;
1795                 pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
1796             END IF;
1797 /* Changes for FP.M, Tracking Bug No - 3354518 End here */
1798 
1799 
1800               PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
1801                                  p_msg_name            => 'PA_FP_INV_PARAM_PASSED');
1802 
1803             RAISE Invalid_Arg_Exc;
1804 
1805         END IF;
1806 
1807         -- End of business rule validations.
1808 
1809         pa_debug.g_err_stage := 'Get or Create cost amount set id';
1810         IF P_PA_DEBUG_MODE = 'Y' THEN
1811            pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
1812         END IF;
1813 
1814         IF (p_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY or p_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) then
1815           BEGIN
1816 
1817 /* Changes for FP.M, Tracking Bug No - 3354518
1818 Appending where clause for three new column bill_rate_flag,
1819 cost_rate_flag, burden_rate added to  pa_fin_plan_amount_sets
1820 below*/
1821             select fin_plan_amount_set_id
1822                   into x_cost_amount_set_id
1823                   from pa_fin_plan_amount_sets
1824                   where
1825                   raw_cost_flag=p_raw_cost_flag and
1826                   burdened_cost_flag=p_burdened_cost_flag and
1827                   cost_qty_flag=p_cost_qty_flag and
1828                   revenue_flag = 'N' and
1829                   revenue_qty_flag = 'N' and
1830                   all_qty_flag = 'N' and
1831 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
1832                   bill_rate_flag = 'N' and
1833                   cost_rate_flag = p_cost_rate_flag and
1834                   burden_rate_flag = p_burden_rate_flag and
1835 /* Changes for FPM End here ,Tracking Bug No - 3354518*/
1836                   amount_set_type_code=PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST;
1837 
1838                   l_status := 'OLD';
1839               EXCEPTION
1840                   when NO_DATA_FOUND then
1841                            l_status := 'NEW';
1842               END;
1843 
1844                  pa_debug.g_err_stage := 'Create cost amount set id';
1845                  IF P_PA_DEBUG_MODE = 'Y' THEN
1846                     pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
1847                  END IF;
1848 
1849 /* Changes for FP.M, Tracking Bug No - 3354518
1850 Adding three new column bill_rate_flag,cost_rate_flag,
1851 burden_rate to the insert statment below */
1852 
1853               IF l_status = 'NEW' THEN
1854                   INSERT INTO PA_FIN_PLAN_AMOUNT_SETS (
1855                   FIN_PLAN_AMOUNT_SET_ID,
1856                   AMOUNT_SET_TYPE_CODE,
1857                   RAW_COST_FLAG,
1858                   BURDENED_COST_FLAG,
1859                   COST_QTY_FLAG,
1860                   REVENUE_FLAG,
1861                   REVENUE_QTY_FLAG,
1862                   ALL_QTY_FLAG,
1863                   TP_COST_FLAG,
1864                   TP_REVENUE_FLAG,
1865                   UTIL_PERCENT_FLAG,
1866                   UTIL_HOURS_FLAG,
1867                   CAPACITY_FLAG,
1868                   PRE_DEFINED_FLAG,
1869 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
1870                   BILL_RATE_FLAG,
1871                   COST_RATE_FLAG,
1872                   BURDEN_RATE_FLAG,
1873 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
1874                   LAST_UPDATE_DATE,
1875                   LAST_UPDATED_BY,
1876                   CREATION_DATE,
1877                   CREATED_BY,
1878                   LAST_UPDATE_LOGIN
1879                    )
1880                   VALUES (
1881                   pa_fin_plan_amount_sets_s.NEXTVAL,
1882                   'COST',
1883                   p_raw_cost_flag,
1884                   p_burdened_cost_flag,
1885                   p_cost_qty_flag,
1886                   'N',
1887                   'N',
1888                   'N',
1889                   'N',
1890                   'N',
1891                   'N',
1892                   'N',
1893                   'N',
1894                   'N',
1895 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
1896                   'N',
1897                    p_cost_rate_flag,
1898                    p_burden_rate_flag,
1899 /* Changes for FPM End here ,Tracking Bug No - 3354518*/
1900                   SYSDATE,
1901                   fnd_global.user_id,
1902                   sysdate,
1903                   fnd_global.user_id,
1904                   fnd_global.login_id) RETURNING FIN_PLAN_AMOUNT_SET_ID INTO x_cost_amount_set_id;
1905 
1906                   --SELECT pa_fin_plan_amount_sets_s.currval
1907                   --into x_cost_amount_set_id
1908                   --from dual;
1909 
1910                 END IF;
1911         END IF; -- cost only
1912 
1913 
1914         pa_debug.g_err_stage := 'Get or Create revenue amount set id';
1915         IF P_PA_DEBUG_MODE = 'Y' THEN
1916            pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
1917         END IF;
1918 
1919         IF p_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY or p_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP THEN
1920                 BEGIN
1921 
1922 /* Changes for FP.M, Tracking Bug No - 3354518
1923 Appending where clause for three new column bill_rate_flag,
1924 cost_rate_flag, burden_rate added to  pa_fin_plan_amount_sets
1925 below*/
1926                   select fin_plan_amount_set_id
1927                   into x_revenue_amount_set_id
1928                   from pa_fin_plan_amount_sets
1929                   where
1930                   revenue_flag=p_revenue_flag and
1931                   revenue_qty_flag=p_revenue_qty_flag and
1932                   raw_cost_flag = 'N' and
1933                   burdened_cost_flag = 'N' and
1934                   cost_qty_flag = 'N' and
1935                   all_qty_flag = 'N' and
1936 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
1937                   bill_rate_flag = p_bill_rate_flag and
1938                   cost_rate_flag = 'N' and
1939                   burden_rate_flag = 'N' and
1940 /* Changes for FPM End here ,Tracking Bug No - 3354518*/
1941                   amount_set_type_code = 'REVENUE';
1942 
1943                   l_status := 'OLD';
1944                 EXCEPTION
1945                   when NO_DATA_FOUND then
1946                            l_status := 'NEW';
1947                 END;
1948 
1949                 pa_debug.g_err_stage := 'Create revenue amount set id';
1950                 IF P_PA_DEBUG_MODE = 'Y' THEN
1951                    pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
1952                 END IF;
1953 
1954                 IF l_status = 'NEW' THEN
1955 
1956 /* Changes for FP.M, Tracking Bug No - 3354518
1957 Adding three new column bill_rate_flag,cost_rate_flag,
1958 burden_rate to the insert statment below */
1959 
1960             INSERT INTO PA_FIN_PLAN_AMOUNT_SETS (
1961                       FIN_PLAN_AMOUNT_SET_ID,
1962                       AMOUNT_SET_TYPE_CODE,
1963                       RAW_COST_FLAG,
1964                       BURDENED_COST_FLAG,
1965                       COST_QTY_FLAG,
1966                       REVENUE_FLAG,
1967                       REVENUE_QTY_FLAG,
1968                       ALL_QTY_FLAG,
1969                       TP_COST_FLAG,
1970                       TP_REVENUE_FLAG,
1971                       UTIL_PERCENT_FLAG,
1972                       UTIL_HOURS_FLAG,
1973                       CAPACITY_FLAG,
1974                       PRE_DEFINED_FLAG,
1975     /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
1976                       BILL_RATE_FLAG,
1977                       COST_RATE_FLAG,
1978                       BURDEN_RATE_FLAG,
1979     /* Changes for FPM End here ,Tracking Bug No - 3354518*/
1980                       LAST_UPDATE_DATE,
1981                       LAST_UPDATED_BY,
1982                       CREATION_DATE,
1983                       CREATED_BY,
1984                       LAST_UPDATE_LOGIN
1985                   )
1986                   VALUES (
1987                       pa_fin_plan_amount_sets_s.NEXTVAL,
1988                       'REVENUE',
1989                       'N',
1990                       'N',
1991                       'N',
1992                       p_revenue_flag,
1993                       p_revenue_qty_flag,
1994                       'N',
1995                       'N',
1996                       'N',
1997                       'N',
1998                       'N',
1999                       'N',
2000                       'N',
2001     /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
2002                       p_bill_rate_flag,
2003               'N',
2004               'N',
2005     /* Changes for FPM End here ,Tracking Bug No - 3354518*/
2006                       SYSDATE,
2007                       fnd_global.user_id,
2008                       sysdate,
2009                       fnd_global.user_id,
2010                       fnd_global.login_id)
2011                   RETURNING FIN_PLAN_AMOUNT_SET_ID INTO x_revenue_amount_set_id;
2012 
2013                   --select pa_fin_plan_amount_sets_s.currval
2014                   --into x_revenue_amount_set_id
2015                   --from dual;
2016 
2017                 END IF;
2018         END IF; -- revenue only
2019 
2020         pa_debug.g_err_stage := 'Get or Create all amount set id';
2021         IF P_PA_DEBUG_MODE = 'Y' THEN
2022            pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
2023         END IF;
2024 
2025         IF p_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME THEN
2026              BEGIN
2027 /* Changes for FP.M, Tracking Bug No - 3354518
2028 Appending where clause for three new column bill_rate_flag,
2029 cost_rate_flag, burden_rate added to pa_fin_plan_amount_sets
2030 below*/
2031                   select fin_plan_amount_set_id
2032                   into x_all_amount_set_id
2033                   from pa_fin_plan_amount_sets
2034                   where
2035                   raw_cost_flag=p_raw_cost_flag and
2036                   burdened_cost_flag=p_burdened_cost_flag and
2037                   revenue_flag=p_revenue_flag and
2038                   all_qty_flag=p_all_qty_flag and
2039                   cost_qty_flag = 'N' and
2040                   revenue_qty_flag = 'N' and
2041 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
2042                   bill_rate_flag = p_bill_rate_flag and
2043                   cost_rate_flag = p_cost_rate_flag and
2044                   burden_rate_flag = p_burden_rate_flag and
2045 /* Changes for FPM End here ,Tracking Bug No - 3354518*/
2046                   amount_set_type_code = 'ALL';
2047 
2048 
2049                   l_status := 'OLD';
2050               EXCEPTION
2051                   when NO_DATA_FOUND then
2052                            l_status := 'NEW';
2053               END;
2054 
2055                 pa_debug.g_err_stage := 'Create cost amount set id';
2056                 IF P_PA_DEBUG_MODE = 'Y' THEN
2057                    pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,3);
2058                 END IF;
2059 
2060                 IF l_status = 'NEW' THEN
2061  /* Changes for FP.M, Tracking Bug No - 3354518
2062 Adding three new column bill_rate_flag,cost_rate_flag,
2063 burden_rate to the insert statment below */
2064                  INSERT INTO PA_FIN_PLAN_AMOUNT_SETS (
2065                       FIN_PLAN_AMOUNT_SET_ID,
2066                       AMOUNT_SET_TYPE_CODE,
2067                       RAW_COST_FLAG,
2068                       BURDENED_COST_FLAG,
2069                       COST_QTY_FLAG,
2070                       REVENUE_FLAG,
2071                       REVENUE_QTY_FLAG,
2072                       ALL_QTY_FLAG ,
2073                       TP_COST_FLAG,
2074                       TP_REVENUE_FLAG,
2075                       UTIL_PERCENT_FLAG,
2076                       UTIL_HOURS_FLAG,
2077                       CAPACITY_FLAG,
2078                       PRE_DEFINED_FLAG,
2079     /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
2080                       BILL_RATE_FLAG,
2081                       COST_RATE_FLAG,
2082                       BURDEN_RATE_FLAG,
2083     /* Changes for FPM End here ,Tracking Bug No - 3354518*/
2084                       LAST_UPDATE_DATE,
2085                       LAST_UPDATED_BY,
2086                       CREATION_DATE,
2087                       CREATED_BY,
2088                       LAST_UPDATE_LOGIN
2089                   )
2090                   VALUES (
2091                       pa_fin_plan_amount_sets_s.NEXTVAL,
2092                       'ALL',
2093                       p_raw_cost_flag,
2094                       p_burdened_cost_flag,
2095                       'N',
2096                       p_revenue_flag,
2097                       'N',
2098                       p_all_qty_flag,
2099                       'N',
2100                       'N',
2101                       'N',
2102                       'N',
2103                       'N',
2104                       'N',
2105     /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
2106                       p_bill_rate_flag,
2107                       p_cost_rate_flag,
2108                       p_burden_rate_flag,
2109     /* Changes for FPM End here ,Tracking Bug No - 3354518*/
2110                       SYSDATE,
2111                       fnd_global.user_id,
2112                       sysdate,
2113                       fnd_global.user_id,
2114                       fnd_global.login_id)
2115                   RETURNING FIN_PLAN_AMOUNT_SET_ID INTO x_all_amount_set_id;
2116 
2117                   --select pa_fin_plan_amount_sets_s.currval
2118                   --into x_all_amount_set_id
2119                   --from dual;
2120          END IF;
2121    END IF; -- cost and revenue
2122 
2123    commit;
2124  IF P_PA_DEBUG_MODE = 'Y' THEN
2125    pa_debug.reset_err_stack;
2126 END IF;
2127 EXCEPTION
2128    WHEN Invalid_Arg_Exc THEN
2129 
2130         l_msg_count := FND_MSG_PUB.count_msg;
2131 
2132         IF l_msg_count = 1 THEN
2133 
2134              PA_INTERFACE_UTILS_PUB.get_messages
2135                    (p_encoded        => FND_API.G_TRUE
2136                     ,p_msg_index      => 1
2137                     ,p_msg_count      => l_msg_count
2138                     ,p_msg_data       => l_msg_data
2139                     ,p_data           => l_data
2140                     ,p_msg_index_out  => l_msg_index_out);
2141 
2142              x_message_data := l_data;
2143              x_message_count := l_msg_count;
2144 
2145         ELSE
2146 
2147             x_message_count := l_msg_count;
2148 
2149         END IF;
2150 
2151         x_return_status:= FND_API.G_RET_STS_ERROR;
2152 
2153  IF P_PA_DEBUG_MODE = 'Y' THEN
2154         pa_debug.reset_err_stack;
2155   END IF;
2156         rollback;
2157 
2158  WHEN Others THEN
2159 
2160         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2161         x_message_count     := 1;
2162         x_message_data      := SQLERRM;
2163 
2164         FND_MSG_PUB.add_exc_msg( p_pkg_name       => 'PA_FIN_PLAN_UTILS'
2165                                 ,p_procedure_name => 'GET_OR_CREATE_AMOUNT_SET_ID');
2166 
2167         pa_debug.g_err_stage:='Unexpected Error';
2168         IF P_PA_DEBUG_MODE = 'Y' THEN
2169            pa_debug.write('GET_OR_CREATE_AMOUNT_SET_ID: ' || l_module_name,pa_debug.g_err_stage,5);
2170 
2171         pa_debug.reset_err_stack;
2172 	END IF;
2173         rollback;
2174         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2175 
2176 END GET_OR_CREATE_AMOUNT_SET_ID;
2177 
2178 /*=================================================================================
2179 Given the amount set id the procedure return the flags.
2180 ===================================================================================*/
2181 PROCEDURE GET_PLAN_AMOUNT_FLAGS(
2182       P_AMOUNT_SET_ID       IN  PA_FIN_PLAN_AMOUNT_SETS.fin_plan_amount_set_id%TYPE,
2183       X_RAW_COST_FLAG       OUT NOCOPY PA_FIN_PLAN_AMOUNT_SETS.raw_cost_flag%TYPE, --File.Sql.39 bug 4440895
2184       X_BURDENED_FLAG       OUT NOCOPY PA_FIN_PLAN_AMOUNT_SETS.burdened_cost_flag%TYPE, --File.Sql.39 bug 4440895
2185       X_REVENUE_FLAG        OUT NOCOPY PA_FIN_PLAN_AMOUNT_SETS.revenue_flag%TYPE, --File.Sql.39 bug 4440895
2186       X_COST_QUANTITY_FLAG  OUT NOCOPY PA_FIN_PLAN_AMOUNT_SETS.cost_qty_flag%TYPE, --File.Sql.39 bug 4440895
2187       X_REV_QUANTITY_FLAG   OUT NOCOPY PA_FIN_PLAN_AMOUNT_SETS.revenue_qty_flag%TYPE, --File.Sql.39 bug 4440895
2188       X_ALL_QUANTITY_FLAG   OUT NOCOPY PA_FIN_PLAN_AMOUNT_SETS.all_qty_flag%TYPE, --File.Sql.39 bug 4440895
2189 /* Changes for FP.M, Tracking Bug No - 3354518
2190 Adding three new OUT parameters x_bill_rate_flag,
2191 x_cost_rate_flag, x_burden_rate below for
2192 new columns in pa_fin_plan_amount_sets */
2193       X_BILL_RATE_FLAG      OUT  NOCOPY pa_fin_plan_amount_sets.bill_rate_flag%TYPE, --File.Sql.39 bug 4440895
2194       X_COST_RATE_FLAG      OUT  NOCOPY pa_fin_plan_amount_sets.cost_rate_flag%TYPE, --File.Sql.39 bug 4440895
2195       X_BURDEN_RATE_FLAG    OUT  NOCOPY pa_fin_plan_amount_sets.burden_rate_flag%TYPE,           --File.Sql.39 bug 4440895
2196       x_message_count       OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
2197       x_return_status       OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
2198       x_message_data        OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
2199 IS
2200         l_debug_mode      VARCHAR2(30);
2201         l_msg_count       NUMBER := 0;
2202         l_data            VARCHAR2(2000);
2203         l_msg_data        VARCHAR2(2000);
2204         l_error_msg_code  VARCHAR2(30);
2205         l_msg_index_out   NUMBER;
2206         l_return_status   VARCHAR2(2000);
2207 
2208 BEGIN
2209 
2210 	 IF P_PA_DEBUG_MODE = 'Y' THEN
2211         pa_debug.set_err_stack ('PA_FIN_PLAN_UTILS.GET_PLAN_AMOUNT_FLAGS');
2212 	END IF;
2213         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
2214         l_debug_mode := NVL(l_debug_mode, 'Y');
2215         IF P_PA_DEBUG_MODE = 'Y' THEN
2216            pa_debug.set_process('GET_PLAN_AMOUNT_FLAGS: ' || 'PLSQL','LOG',l_debug_mode);
2217         END IF;
2218         x_message_count := 0;
2219 
2220         x_return_status := FND_API.G_RET_STS_SUCCESS;
2221 
2222 
2223         -- Check for business rules violations
2224 
2225         pa_debug.g_err_stage := 'Parameter Validation';
2226         IF P_PA_DEBUG_MODE = 'Y' THEN
2227            pa_debug.write('GET_PLAN_AMOUNT_FLAGS: ' || l_module_name,pa_debug.g_err_stage,3);
2228         END IF;
2229 
2230         -- Check for amount set id being null
2231 
2232 
2233         IF  P_AMOUNT_SET_ID is null THEN
2234 
2235             pa_debug.g_err_stage := 'Check for null AMOUNT SET ID';
2236             IF P_PA_DEBUG_MODE = 'Y' THEN
2237                pa_debug.write('GET_PLAN_AMOUNT_FLAGS: ' || l_module_name,pa_debug.g_err_stage,5);
2238             END IF;
2239 
2240             PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
2241                                  p_msg_name            => 'PA_FP_INV_PARAM_PASSED');
2242 
2243             RAISE Invalid_Arg_Exc;
2244 
2245         END IF;
2246 /* Changes for FP.M, Tracking Bug No - 3354518
2247 Appending where clause for three new column bill_rate_flag,
2248 cost_rate_flag, burden_rate added to pa_fin_plan_amount_sets
2249 below*/
2250 
2251         select raw_cost_flag,
2252           burdened_cost_flag,
2253           revenue_flag,
2254           cost_qty_flag,
2255           revenue_qty_flag,
2256           all_qty_flag,
2257 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
2258           bill_rate_flag,
2259           cost_rate_flag,
2260           burden_rate_flag
2261 /* Changes for FPM End here ,Tracking Bug No - 3354518*/
2262         into
2263           X_RAW_COST_FLAG,
2264           X_BURDENED_FLAG,
2265           X_REVENUE_FLAG,
2266           X_COST_QUANTITY_FLAG,
2267           X_REV_QUANTITY_FLAG,
2268           X_ALL_QUANTITY_FLAG,
2269 /* Changes for FPM Start here ,Tracking Bug No - 3354518*/
2270           X_BILL_RATE_FLAG,
2271           X_COST_RATE_FLAG,
2272           X_BURDEN_RATE_FLAG
2273 /* Changes for FPM End here ,Tracking Bug No - 3354518*/
2274         from
2275           PA_FIN_PLAN_AMOUNT_SETS
2276         where
2277           fin_plan_amount_set_id = P_AMOUNT_SET_ID;
2278 
2279  IF P_PA_DEBUG_MODE = 'Y' THEN
2280         pa_debug.reset_err_stack;
2281  END IF;
2282 EXCEPTION
2283    WHEN Invalid_Arg_Exc THEN
2284 
2285         l_msg_count := FND_MSG_PUB.count_msg;
2286 
2287         IF l_msg_count = 1 THEN
2288 
2289              PA_INTERFACE_UTILS_PUB.get_messages
2290                    (p_encoded        => FND_API.G_TRUE
2291                     ,p_msg_index      => 1
2292                     ,p_msg_count      => l_msg_count
2293                     ,p_msg_data       => l_msg_data
2294                     ,p_data           => l_data
2295                     ,p_msg_index_out  => l_msg_index_out);
2296 
2297              x_message_data := l_data;
2298              x_message_count := l_msg_count;
2299 
2300         ELSE
2301 
2302             x_message_count := l_msg_count;
2303 
2304         END IF;
2305 
2306         x_return_status:= FND_API.G_RET_STS_ERROR;
2307 
2308  IF P_PA_DEBUG_MODE = 'Y' THEN
2309         pa_debug.reset_err_stack;
2310 END IF;
2311         RAISE;
2312 
2313  WHEN Others THEN
2314 
2315         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2316         x_message_count     := 1;
2317         x_message_data      := SQLERRM;
2318 
2319         FND_MSG_PUB.add_exc_msg( p_pkg_name       => 'PA_FIN_PLAN_UTILS'
2320                                 ,p_procedure_name => 'GET_PLAN_AMOUNT_FLAGS');
2321 
2322         pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
2323         IF P_PA_DEBUG_MODE = 'Y' THEN
2324            pa_debug.write('GET_PLAN_AMOUNT_FLAGS: ' || l_module_name,pa_debug.g_err_stage,5);
2325 
2326         pa_debug.reset_err_stack;
2327        END IF;
2328         rollback;
2329         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2330 
2331 END GET_PLAN_AMOUNT_FLAGS;
2332 
2333 /* =====================================================
2334    FUNCTION is_orgforecast_plan
2335    Takes as input a budget version id, and returns 'Y' if
2336    its PLAN_TYPE_CODE is 'ORG_FORECAST'.  Otherwise, returns 'N'
2337    ===================================================== */
2338 FUNCTION is_orgforecast_plan
2339     (p_budget_version_id    IN  pa_budget_versions.budget_version_id%TYPE)
2340 return VARCHAR2
2341 is
2342 
2343 l_plan_type_code    pa_fin_plan_types_b.fin_plan_type_code%TYPE;
2344 l_return_value      VARCHAR2(1);
2345 BEGIN
2346   l_return_value := 'N';
2347   select pt.fin_plan_type_code
2348     into l_plan_type_code
2349     from pa_budget_versions bv,
2350          pa_fin_plan_types_b pt
2351     where bv.budget_version_id = p_budget_version_id and
2352           bv.fin_plan_type_id = pt.fin_plan_type_id;
2353   if l_plan_type_code = 'ORG_FORECAST' then
2354     l_return_value := 'Y';
2355   end if;
2356   return(l_return_value);
2357 
2358 EXCEPTION
2359      WHEN NO_DATA_FOUND THEN
2360           return(l_return_value);
2361      WHEN OTHERS THEN
2362           return(l_return_value);
2363 END is_orgforecast_plan;
2364 
2365 
2366 /* =====================================================
2367    FUNCTION get_person_name
2368    Takes as input the person_id, and returns the person
2369    name.  Returns null if name not found.
2370    ===================================================== */
2371 FUNCTION get_person_name (p_person_id  IN  NUMBER) return VARCHAR2 is
2372 
2373 l_person_name   VARCHAR2(240);
2374 
2375 BEGIN
2376   select full_name
2377     into l_person_name
2378     from per_people_x
2379     where person_id = p_person_id;
2380   return l_person_name;
2381 
2382 EXCEPTION
2383   WHEN  NO_DATA_FOUND THEN
2384     IF p_person_id = -98 THEN
2385         return 'PeriodProfileRefresh'; /* Added this IF block for bug 2746379 */
2386     ELSIF  p_person_id = -99 THEN
2387         return 'WBSRefresh'; /* Added this IF block for bug 3123826 */
2388     ELSE
2389         return null;
2390     END IF;
2391   WHEN OTHERS THEN
2392     return null;
2393 END get_person_name;
2394 
2395 /* =====================================================
2396    PROCEDURE  Get_Peceding_Suceeding_Prd_Info
2397    Procedure which returns the start date and enddate
2398    period info of Succeeding and Preceding periods
2399    ===================================================== */
2400 
2401 
2402 PROCEDURE Get_Peceding_Suceeding_Pd_Info
2403       (   p_resource_assignment_id     IN  pa_budget_lines.RESOURCE_ASSIGNMENT_ID%TYPE
2404          ,p_txn_currency_code          IN  pa_budget_lines.TXN_CURRENCY_CODE%TYPE
2405          ,x_preceding_prd_start_date  OUT  NOCOPY DATE --File.Sql.39 bug 4440895
2406          ,x_preceding_prd_end_date    OUT  NOCOPY DATE --File.Sql.39 bug 4440895
2407          ,x_succeeding_prd_start_date OUT  NOCOPY DATE --File.Sql.39 bug 4440895
2408          ,x_succeeding_prd_end_date   OUT  NOCOPY DATE --File.Sql.39 bug 4440895
2409          ,x_return_status             OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2410          ,x_msg_count                 OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
2411          ,x_msg_data                  OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2412       ) IS
2413 
2414   CURSOR period_info_cur IS
2415   SELECT start_date
2416         ,end_date
2417         ,bucketing_period_code
2418     FROM pa_budget_lines
2419    WHERE resource_assignment_id = p_resource_assignment_id
2420      AND txn_currency_code = p_txn_currency_code
2421      AND bucketing_period_code in ('SD','PD');
2422 
2423      l_period_info_rec    period_info_cur%ROWTYPE;
2424      l_return_status      VARCHAR2(2000);
2425      l_msg_count          NUMBER :=0;
2426      l_msg_data           VARCHAR2(2000);
2427      l_data               VARCHAR2(2000);
2428      l_msg_index_out      NUMBER;
2429      l_debug_mode         VARCHAR2(30);
2430      l_module_name VARCHAR2(100) := 'pa.plsql.PA_FIN_PLAN_UTILS';
2431 
2432  BEGIN
2433 
2434      x_msg_count := 0;
2435      x_return_status := FND_API.G_RET_STS_SUCCESS;
2436 
2437  IF P_PA_DEBUG_MODE = 'Y' THEN
2438      pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Peceding_Suceeding_Prd_Info');
2439 END IF;
2440      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
2441      l_debug_mode := NVL(l_debug_mode, 'Y');
2442      IF P_PA_DEBUG_MODE = 'Y' THEN
2443         pa_debug.set_process('Get_Peceding_Suceeding_Pd_Info: ' || 'PLSQL','LOG',l_debug_mode);
2444      END IF;
2445 
2446      -- Check for not null parameters
2447 
2448      pa_debug.g_err_stage := 'Checking for valid parameters:';
2449      IF P_PA_DEBUG_MODE = 'Y' THEN
2450         pa_debug.write('Get_Peceding_Suceeding_Pd_Info: ' || l_module_name,pa_debug.g_err_stage,3);
2451      END IF;
2452 
2453      IF (p_resource_assignment_id  IS NULL) OR
2454         (p_txn_currency_code IS NULL)
2455      THEN
2456 
2457          pa_debug.g_err_stage := 'resource_assignment_id='||to_char(p_resource_assignment_id);
2458          IF P_PA_DEBUG_MODE = 'Y' THEN
2459             pa_debug.write('Get_Peceding_Suceeding_Pd_Info: ' || l_module_name,pa_debug.g_err_stage,5);
2460          END IF;
2461          pa_debug.g_err_stage := 'txn currency code ='||p_txn_currency_code;
2462          IF P_PA_DEBUG_MODE = 'Y' THEN
2463             pa_debug.write('Get_Peceding_Suceeding_Pd_Info: ' || l_module_name,pa_debug.g_err_stage,5);
2464          END IF;
2465 
2466          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
2467                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
2468 
2469          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2470 
2471      END IF;
2472 
2473       FOR  l_period_info_rec IN  period_info_cur LOOP
2474               IF ( l_period_info_rec.bucketing_period_code = 'PD' ) THEN
2475                        x_preceding_prd_start_date := l_period_info_rec.start_date ;
2476                        x_preceding_prd_end_date := l_period_info_rec.end_date ;
2477               ELSIF ( l_period_info_rec.bucketing_period_code = 'SD') THEN
2478                        x_succeeding_prd_start_date := l_period_info_rec.start_date ;
2479                        x_succeeding_prd_end_date := l_period_info_rec.end_date ;
2480               END IF;
2481 
2482      END LOOP;
2483  IF P_PA_DEBUG_MODE = 'Y' THEN
2484      pa_debug.reset_err_stack;
2485  END IF;
2486  EXCEPTION
2487 
2488    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
2489 
2490         l_msg_count := FND_MSG_PUB.count_msg;
2491 
2492         IF l_msg_count = 1 THEN
2493 
2494              PA_INTERFACE_UTILS_PUB.get_messages
2495                    (p_encoded        => FND_API.G_TRUE
2496                     ,p_msg_index      => 1
2497                     ,p_msg_count      => l_msg_count
2498                     ,p_msg_data       => l_msg_data
2499                     ,p_data           => l_data
2500                     ,p_msg_index_out  => l_msg_index_out);
2501 
2502              x_msg_data := l_data;
2503              x_msg_count := l_msg_count;
2504 
2505         ELSE
2506 
2507             x_msg_count := l_msg_count;
2508 
2509         END IF;
2510 
2511          pa_debug.g_err_stage:='Invalid Arguments Passed';
2512          IF P_PA_DEBUG_MODE = 'Y' THEN
2513             pa_debug.write('Get_Peceding_Suceeding_Pd_Info: ' || l_module_name,pa_debug.g_err_stage,5);
2514          END IF;
2515 
2516          x_return_status:= FND_API.G_RET_STS_ERROR;
2517 
2518  IF P_PA_DEBUG_MODE = 'Y' THEN
2519          pa_debug.reset_err_stack;
2520 END IF;
2521          RAISE;
2522 
2523    WHEN Others THEN
2524 
2525         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2526         x_msg_count     := 1;
2527         x_msg_data      := SQLERRM;
2528 
2529         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_UTILS'
2530                         ,p_procedure_name  => 'Get_Peceding_Suceeding_Pd_Info');
2531 
2532         pa_debug.g_err_stage:='Unexpected Error';
2533         IF P_PA_DEBUG_MODE = 'Y' THEN
2534            pa_debug.write('Get_Peceding_Suceeding_Pd_Info: ' || l_module_name,pa_debug.g_err_stage,5);
2535         pa_debug.reset_err_stack;
2536         END IF;
2537         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2538 
2539  END Get_Peceding_Suceeding_Pd_Info;
2540 
2541 
2542 /* =====================================================
2543    PROCEDURE  Get_Element_Proj_PF_Amounts
2544    Returns the sum of raw,burdened,revenue and quantity in
2545    project and project Functional currencies.
2546    ===================================================== */
2547 
2548  PROCEDURE Get_Element_Proj_PF_Amounts
2549          (
2550            p_resource_assignment_id       IN   pa_budget_lines.RESOURCE_ASSIGNMENT_ID%TYPE
2551           ,p_txn_currency_code            IN   pa_budget_lines.TXN_CURRENCY_CODE%TYPE
2552           ,x_quantity                     OUT  NOCOPY pa_budget_lines.QUANTITY%TYPE --File.Sql.39 bug 4440895
2553           ,x_project_raw_cost             OUT  NOCOPY pa_budget_lines.TXN_RAW_COST%TYPE --File.Sql.39 bug 4440895
2554           ,x_project_burdened_cost        OUT  NOCOPY pa_budget_lines.TXN_BURDENED_COST%TYPE --File.Sql.39 bug 4440895
2555           ,x_project_revenue              OUT  NOCOPY pa_budget_lines.TXN_REVENUE%TYPE --File.Sql.39 bug 4440895
2556           ,x_projfunc_raw_cost            OUT  NOCOPY pa_budget_lines.TXN_RAW_COST%TYPE --File.Sql.39 bug 4440895
2557           ,x_projfunc_burdened_cost       OUT  NOCOPY pa_budget_lines.TXN_BURDENED_COST%TYPE --File.Sql.39 bug 4440895
2558           ,x_projfunc_revenue             OUT  NOCOPY pa_budget_lines.TXN_REVENUE%TYPE --File.Sql.39 bug 4440895
2559           ,x_return_status                OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2560           ,x_msg_count                    OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
2561           ,x_msg_data                     OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2562           )
2563   IS
2564 
2565        l_return_status      VARCHAR2(2000);
2566        l_msg_count          NUMBER :=0;
2567        l_msg_data           VARCHAR2(2000);
2568        l_data               VARCHAR2(2000);
2569        l_msg_index_out      NUMBER;
2570        l_debug_mode         VARCHAR2(30);
2571        l_module_name VARCHAR2(100) := 'pa.plsql.PA_FIN_PLAN_UTILS';
2572 
2573   BEGIN
2574      x_msg_count := 0;
2575      x_return_status := FND_API.G_RET_STS_SUCCESS;
2576 
2577   IF P_PA_DEBUG_MODE = 'Y' THEN
2578      pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Element_Prj_Pf_Amounts');
2579   END IF;
2580      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
2581      l_debug_mode := NVL(l_debug_mode, 'Y');
2582      IF P_PA_DEBUG_MODE = 'Y' THEN
2583         pa_debug.set_process('Get_Element_Proj_PF_Amounts: ' || 'PLSQL','LOG',l_debug_mode);
2584      END IF;
2585 
2586      -- Check for not null parameters
2587 
2588      pa_debug.g_err_stage := 'Checking for valid parameters:';
2589      IF P_PA_DEBUG_MODE = 'Y' THEN
2590         pa_debug.write('Get_Element_Proj_PF_Amounts: ' || l_module_name,pa_debug.g_err_stage,3);
2591      END IF;
2592 
2593      IF (p_resource_assignment_id  IS NULL) OR
2594         (p_txn_currency_code IS NULL)
2595      THEN
2596 
2597          pa_debug.g_err_stage := 'resource_assignment_id='||to_char(p_resource_assignment_id);
2598          IF P_PA_DEBUG_MODE = 'Y' THEN
2599             pa_debug.write('Get_Element_Proj_PF_Amounts: ' || l_module_name,pa_debug.g_err_stage,5);
2600          END IF;
2601          pa_debug.g_err_stage := 'txn currency code ='||p_txn_currency_code;
2602          IF P_PA_DEBUG_MODE = 'Y' THEN
2603             pa_debug.write('Get_Element_Proj_PF_Amounts: ' || l_module_name,pa_debug.g_err_stage,5);
2604          END IF;
2605 
2606          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
2607                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
2608 
2609          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2610 
2611      END IF;
2612 
2613      BEGIN
2614      SELECT sum(nvl(QUANTITY,0))
2615            ,sum(nvl(RAW_COST,0))
2616            ,sum(nvl(BURDENED_COST,0))
2617            ,sum(nvl(REVENUE,0))
2618            ,sum(nvl(PROJECT_RAW_COST,0))
2619            ,sum(nvl(PROJECT_BURDENED_COST,0))
2620            ,sum(nvl(PROJECT_REVENUE,0))
2621       INTO  x_quantity
2622            ,x_projfunc_raw_cost
2623            ,x_projfunc_burdened_cost
2624            ,x_projfunc_revenue
2625            ,x_project_raw_cost
2626            ,x_project_burdened_cost
2627            ,x_project_revenue
2628       FROM pa_budget_lines
2629      WHERE resource_assignment_id = p_resource_assignment_id
2630        AND txn_currency_code = p_txn_currency_code ;
2631     EXCEPTION
2632 
2633             WHEN NO_DATA_FOUND THEN
2634             pa_debug.g_err_stage :='Invalid Combination of res. Assgnt Id and Txn currency code ';
2635             IF P_PA_DEBUG_MODE = 'Y' THEN
2636                pa_debug.write('Get_Element_Proj_PF_Amounts: ' || l_module_name,pa_debug.g_err_stage,1);
2637             END IF;
2638             x_return_status := FND_API.G_RET_STS_ERROR;
2639             PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
2640                                  p_msg_name => 'PA_BUDGET_LINE_NOT_FOUND' );
2641             RAISE PA_FP_ELEMENTS_PUB.Invalid_Arg_Exc;
2642 
2643           END;
2644  IF P_PA_DEBUG_MODE = 'Y' THEN
2645    pa_debug.reset_err_stack;
2646 END IF;
2647    EXCEPTION
2648 
2649     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
2650 
2651              l_msg_count := FND_MSG_PUB.count_msg;
2652 
2653              IF l_msg_count = 1 THEN
2654 
2655                   PA_INTERFACE_UTILS_PUB.get_messages
2656                         (p_encoded        => FND_API.G_TRUE
2657                          ,p_msg_index      => 1
2658                          ,p_msg_count      => l_msg_count
2659                          ,p_msg_data       => l_msg_data
2660                          ,p_data           => l_data
2661                          ,p_msg_index_out  => l_msg_index_out);
2662 
2663                   x_msg_data := l_data;
2664                   x_msg_count := l_msg_count;
2665 
2666              ELSE
2667 
2668                  x_msg_count := l_msg_count;
2669 
2670              END IF;
2671 
2672               pa_debug.g_err_stage:='Invalid Arguments Passed';
2673               IF P_PA_DEBUG_MODE = 'Y' THEN
2674                  pa_debug.write('Get_Element_Proj_PF_Amounts: ' || l_module_name,pa_debug.g_err_stage,5);
2675               END IF;
2676 
2677               x_return_status:= FND_API.G_RET_STS_ERROR;
2678 
2679  IF P_PA_DEBUG_MODE = 'Y' THEN
2680               pa_debug.reset_err_stack;
2681   END IF;
2682               RAISE;
2683 
2684         WHEN Others THEN
2685 
2686              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2687              x_msg_count     := 1;
2688              x_msg_data      := SQLERRM;
2689 
2690              FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_UTILS'
2691                              ,p_procedure_name  => 'Get_Element_Proj_PF_Amounts');
2692 
2693              pa_debug.g_err_stage:='Unexpected Error';
2694              IF P_PA_DEBUG_MODE = 'Y' THEN
2695                 pa_debug.write('Get_Element_Proj_PF_Amounts: ' || l_module_name,pa_debug.g_err_stage,5);
2696              pa_debug.reset_err_stack;
2697 	END IF;
2698         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2699 
2700  END  Get_Element_Proj_PF_Amounts ;
2701 
2702  PROCEDURE Check_Version_Name_Or_id
2703           (
2704            p_budget_version_id            IN   pa_budget_versions.BUDGET_VERSION_ID%TYPE
2705           ,p_project_id                   IN   pa_budget_versions.project_id%TYPE                -- Bug 2770562
2706           ,p_version_name                 IN   pa_budget_versions.VERSION_NAME%TYPE
2707           ,p_check_id_flag                IN   VARCHAR2
2708           ,x_budget_version_id            OUT  NOCOPY pa_budget_versions.BUDGET_VERSION_ID%TYPE --File.Sql.39 bug 4440895
2709           ,x_return_status                OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2710           ,x_msg_count                    OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
2711           ,x_msg_data                     OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2712           )
2713 
2714   IS
2715   l_msg_count                     NUMBER := 0;
2716   l_data                          VARCHAR2(2000);
2717   l_msg_data                      VARCHAR2(2000);
2718   l_msg_index_out                 NUMBER;
2719   l_debug_mode                    VARCHAR2(1);
2720 
2721   BEGIN
2722         x_msg_count := 0;
2723         x_return_status := FND_API.G_RET_STS_SUCCESS;
2724  IF P_PA_DEBUG_MODE = 'Y' THEN
2725         pa_debug.init_err_stack ('pa_fin_plan_utils.check_version_name_or_id');
2726  END IF;
2727         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
2728         l_debug_mode := NVL(l_debug_mode, 'N');
2729         IF l_debug_mode = 'Y' THEN
2730              pa_debug.g_err_stage:= 'Validating input parameters';
2731              pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
2732         END IF;
2733 
2734         IF (p_project_id IS NULL)                       -- Bug 2770562
2735         THEN
2736              IF l_debug_mode = 'Y' THEN
2737                      pa_debug.g_err_stage:= 'project id is null ';
2738                      pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
2739              END IF;
2740              PA_UTILS.ADD_MESSAGE
2741                    (p_app_short_name => 'PA',
2742                      p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
2743              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2744 
2745         END IF;
2746 
2747 
2748         IF p_budget_version_id IS NOT NULL AND p_budget_version_id <> FND_API.G_MISS_NUM THEN
2749           IF p_check_id_flag = 'Y' THEN
2750             SELECT budget_version_id
2751             INTO   x_budget_version_id
2752             FROM   pa_budget_versions
2753             WHERE  budget_version_id = p_budget_version_id;
2754           ELSIF p_check_id_flag = 'N' THEN
2755              x_budget_version_id := p_budget_version_id;
2756           END IF;
2757         ELSE
2758            IF (p_version_name IS NOT NULL) THEN
2759              SELECT budget_version_id
2760              INTO   x_budget_version_id
2761              FROM   pa_budget_versions
2762              WHERE  version_name = p_version_name
2763              AND    project_id = p_project_id ;            -- Bug 2770562
2764           ELSE
2765              x_budget_version_id := NULL;
2766           END IF;
2767         END IF;
2768         x_return_status := FND_API.G_RET_STS_SUCCESS;
2769 	 IF P_PA_DEBUG_MODE = 'Y' THEN
2770              pa_debug.reset_err_stack;
2771 	 END IF;
2772  EXCEPTION
2773       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN               -- Bug 2770562
2774 
2775           x_return_status := FND_API.G_RET_STS_ERROR;
2776           l_msg_count := FND_MSG_PUB.count_msg;
2777 
2778           IF l_msg_count = 1 and x_msg_data IS NULL THEN
2779                PA_INTERFACE_UTILS_PUB.get_messages
2780                    (p_encoded        => FND_API.G_TRUE
2781                    ,p_msg_index      => 1
2782                    ,p_msg_count      => l_msg_count
2783                    ,p_msg_data       => l_msg_data
2784                    ,p_data           => l_data
2785                    ,p_msg_index_out  => l_msg_index_out);
2786                x_msg_data := l_data;
2787                x_msg_count := l_msg_count;
2788           ELSE
2789                x_msg_count := l_msg_count;
2790           END IF;
2791 	 IF P_PA_DEBUG_MODE = 'Y' THEN
2792           pa_debug.reset_err_stack;
2793 	END IF;
2794           x_budget_version_id := null;
2795           RETURN;
2796 
2797         WHEN NO_DATA_FOUND THEN
2798           x_return_status     := FND_API.G_RET_STS_ERROR;
2799           x_msg_count         := 1;
2800           PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
2801                                p_msg_name            => 'PA_FP_VERSION_NAME_AMBIGOUS');
2802           x_budget_version_id := NULL;
2803         WHEN TOO_MANY_ROWS THEN
2804           x_return_status     := FND_API.G_RET_STS_ERROR;
2805           x_msg_count         := 1;
2806           PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
2807                                p_msg_name            => 'PA_FP_VERSION_NAME_AMBIGOUS');
2808           x_budget_version_id := NULL;
2809         WHEN OTHERS THEN
2810           FND_MSG_PUB.ADD_EXC_MSG (p_pkg_name       => 'PA_FIN_PLAN_UTILS',
2811                                    p_procedure_name => pa_debug.g_err_stack );
2812           x_return_status     := FND_API.G_RET_STS_UNEXP_ERROR;
2813           x_msg_data          := SQLERRM;
2814           x_budget_version_id := NULL;
2815           RAISE;
2816  END Check_Version_Name_Or_Id;
2817 
2818 PROCEDURE Check_Currency_Name_Or_Code
2819           (
2820            p_txn_currency_code            IN   pa_fp_txn_currencies.txn_currency_code%TYPE
2821           ,p_currency_code_name           IN   VARCHAR2
2822           ,p_check_id_flag                IN   VARCHAR2
2823           ,x_txn_currency_code            OUT  NOCOPY pa_fp_txn_currencies.txn_currency_code%TYPE --File.Sql.39 bug 4440895
2824           ,x_return_status                OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2825           ,x_msg_count                    OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
2826           ,x_msg_data                     OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
2827           ) IS
2828  BEGIN
2829  IF P_PA_DEBUG_MODE = 'Y' THEN
2830         pa_debug.init_err_stack ('pa_fin_plan_utils.Check_Currency_Name_Or_Code');
2831  END IF;
2832         IF p_txn_currency_code IS NOT NULL  THEN
2833           pa_debug.g_err_stage:='Txn Currency Code is not null';
2834           IF P_PA_DEBUG_MODE = 'Y' THEN
2835              pa_debug.write('Check_Currency_Name_Or_Code: ' || l_module_name,pa_debug.g_err_stage,3);
2836           END IF;
2837 
2838           IF p_check_id_flag = 'Y' THEN
2839             SELECT txn_currency_code
2840             INTO  x_txn_currency_code
2841             FROM  pa_fp_txn_currencies
2842             WHERE  txn_currency_code = p_txn_currency_code;
2843           ELSIF p_check_id_flag = 'N' THEN
2844              x_txn_currency_code := p_txn_currency_code;
2845           END IF;
2846         ELSE
2847            pa_debug.g_err_stage:='Txn Currency Code is  null';
2848            IF P_PA_DEBUG_MODE = 'Y' THEN
2849               pa_debug.write('Check_Currency_Name_Or_Code: ' || l_module_name,pa_debug.g_err_stage,3);
2850            END IF;
2851 
2852            IF (p_currency_code_name IS NOT NULL) THEN
2853              pa_debug.g_err_stage:='Currency Code Name String is not null';
2854              IF P_PA_DEBUG_MODE = 'Y' THEN
2855                 pa_debug.write('Check_Currency_Name_Or_Code: ' || l_module_name,pa_debug.g_err_stage,3);
2856              END IF;
2857 
2858              -- Bug 4874283 - performance fix.  use TL table and rewrite select
2859              -- so that the NAME, LANGUAGE index can be used, if appropriate
2860              --
2861              -- SELECT currency_code
2862              -- INTO   x_txn_currency_code
2863              -- FROM   fnd_currencies_vl
2864              -- WHERE  p_currency_code_name = currency_code || ' - ' || name;
2865 
2866              SELECT currency_code
2867              INTO   x_txn_currency_code
2868              FROM   fnd_currencies_tl
2869              WHERE  name = replace(p_currency_code_name, currency_code || ' - ')
2870              AND    language = USERENV('LANG');
2871 
2872           ELSE
2873              pa_debug.g_err_stage:='Currency Code Name String is  null';
2874              IF P_PA_DEBUG_MODE = 'Y' THEN
2875                 pa_debug.write('Check_Currency_Name_Or_Code: ' || l_module_name,pa_debug.g_err_stage,3);
2876              END IF;
2877              x_txn_currency_code := NULL;
2878           END IF;
2879         END IF;
2880         x_return_status := FND_API.G_RET_STS_SUCCESS;
2881 	 IF P_PA_DEBUG_MODE = 'Y' THEN
2882 	        pa_debug.reset_err_stack;
2883 	  END IF;
2884  EXCEPTION
2885         WHEN NO_DATA_FOUND THEN
2886           x_return_status     := FND_API.G_RET_STS_ERROR;
2887           x_msg_count         := 1;
2888           x_msg_data          := 'PA_FP_CURR_INVALID';
2889           x_txn_currency_code := NULL;
2890         WHEN TOO_MANY_ROWS THEN
2891           x_return_status     := FND_API.G_RET_STS_ERROR;
2892           x_msg_count         := 1;
2893           x_msg_data          := 'PA_FP_CURR_INVALID';
2894           x_txn_currency_code := NULL;
2895         WHEN OTHERS THEN
2896           fnd_msg_pub.add_exc_msg
2897            (p_pkg_name => 'PA_FIN_PLAN_UTILS',
2898             p_procedure_name => pa_debug.g_err_stack );
2899             x_return_status:= FND_API.G_RET_STS_UNEXP_ERROR;
2900             x_txn_currency_code := NULL;
2901          RAISE;
2902  END Check_Currency_Name_Or_Code;
2903 
2904 /* Changes for FP.M, Tracking Bug No - 3354518
2905 Replacing all references of PA_TASKS by PA_STRUCT_TASK_WBS_V*/
2906 /* Commenting code below for FP.M changes, Tracking Bug No - 3354518 */
2907 /*PROCEDURE check_task_name_or_id
2908     (p_project_id       IN  pa_tasks.project_id%TYPE,
2909      p_task_id          IN  pa_tasks.task_id%TYPE,
2910      p_task_name        IN  pa_tasks.task_name%TYPE,
2911      p_check_id_flag    IN  VARCHAR2,
2912      x_task_id          OUT pa_tasks.task_id%TYPE,
2913      x_return_status    OUT VARCHAR2,
2914      x_msg_count        OUT NUMBER,
2915      x_error_msg        OUT VARCHAR2)*/
2916 /* Rewriting procedure declaration below to refer to pa_struct_task_wbs_v
2917 instead of pa_tasks - as part of worplan structure model changes in FP.M */
2918 PROCEDURE check_task_name_or_id
2919     (p_project_id       IN  PA_STRUCT_TASK_WBS_V.project_id%TYPE,
2920      p_task_id          IN  PA_STRUCT_TASK_WBS_V.task_id%TYPE,
2921      p_task_name        IN  PA_STRUCT_TASK_WBS_V.task_name%TYPE,
2922      p_check_id_flag    IN  VARCHAR2,
2923      x_task_id          OUT NOCOPY PA_STRUCT_TASK_WBS_V.task_id%TYPE, --File.Sql.39 bug 4440895
2924      x_return_status    OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
2925      x_msg_count        OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
2926      x_error_msg        OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
2927 is
2928   l_msg_index_out       NUMBER;
2929 BEGIN
2930  IF P_PA_DEBUG_MODE = 'Y' THEN
2931     pa_debug.init_err_stack ('pa_fin_plan_utils.check_task_name_or_id');
2932  END IF;
2933     if p_task_id is not null AND p_task_id <> FND_API.G_MISS_NUM then
2934     if p_check_id_flag = 'Y' then
2935       -- validate the id that was passed in
2936       select task_id
2937         into x_task_id
2938         from PA_STRUCT_TASK_WBS_V -- Changes for FP.M, Tracking Bug No - 3354518
2939         where task_id = p_task_id;
2940     elsif p_check_id_flag = 'N' then
2941       -- just return the p_task_id, since we're not validating
2942       x_task_id := p_task_id;
2943     end if; -- p_check_id_flag
2944   else
2945     if p_task_name is not null then
2946       -- p_task_id = null, so we need to find the id
2947       select task_id
2948         into x_task_id
2949         from PA_STRUCT_TASK_WBS_V  -- Changes for FP.M, Tracking Bug No - 3354518
2950         where project_id = p_project_id and
2951               task_name = p_task_name;
2952     else
2953       x_task_id := null;
2954     end if;
2955   end if; -- p_task_id is null
2956   x_return_status := FND_API.G_RET_STS_SUCCESS;
2957 	 IF P_PA_DEBUG_MODE = 'Y' THEN
2958 	  pa_debug.reset_err_stack;
2959 	 END IF;
2960 EXCEPTION
2961     WHEN NO_DATA_FOUND THEN
2962       x_task_id := NULL;
2963       x_return_status := FND_API.G_RET_STS_ERROR;
2964       x_msg_count := 1;
2965       x_error_msg := 'PA_FP_TASK_NAME_AMBIGUOUS';
2966       PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
2967                            p_msg_name            => 'PA_FP_TASK_NAME_AMBIGUOUS');
2968       if x_msg_count = 1 then
2969                 PA_INTERFACE_UTILS_PUB.get_messages
2970                      (p_encoded        => FND_API.G_TRUE,
2971                       p_msg_index      => 1,
2972                       p_data           => x_error_msg,
2973                       p_msg_index_out  => l_msg_index_out);
2974       end if;
2975     WHEN TOO_MANY_ROWS THEN
2976       x_task_id := NULL;
2977       x_return_status := FND_API.G_RET_STS_ERROR;
2978       x_msg_count := 1;
2979       x_error_msg := 'PA_FP_TASK_NAME_AMBIGUOUS';
2980       PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
2981                            p_msg_name            => 'PA_FP_TASK_NAME_AMBIGUOUS');
2982       if x_msg_count = 1 then
2983                 PA_INTERFACE_UTILS_PUB.get_messages
2984                      (p_encoded        => FND_API.G_TRUE,
2985                       p_msg_index      => 1,
2986                       p_data           => x_error_msg,
2987                       p_msg_index_out  => l_msg_index_out);
2988       end if;
2989     WHEN OTHERS THEN
2990       x_task_id := NULL;
2991       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2992       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
2993                               p_procedure_name  => 'check_task_name_or_id');
2994       RAISE;
2995 END check_task_name_or_id;
2996 
2997 /* Changes for FP.M, Tracking Bug No - 3354518
2998    The procedure check_resource_gp_name_or_id is being obsoleted as the
2999    concept of Resource group is no longer there in case of the New dev
3000    model of FP.M. However we are adding code in the procedure below to raise
3001    a exception unconditionally for tracking/debuging purposes at the moment.
3002    Basically to note any calls made to this procedure. Eventually we shall be
3003    commenting out this procedure because of its nonusage.  */
3004 -- returns RESOURCE_LIST_MEMBER_ID of resource group
3005 PROCEDURE check_resource_gp_name_or_id
3006     (p_resource_id      IN  pa_resources.resource_id%TYPE,
3007      p_resource_name    IN  pa_resources.name%TYPE,
3008      p_check_id_flag    IN  VARCHAR2,
3009      x_resource_id      OUT NOCOPY pa_resource_list_members.resource_list_member_id%TYPE, --File.Sql.39 bug 4440895
3010      x_return_status    OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3011      x_msg_count        OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
3012      x_error_msg        OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
3013 is
3014   l_msg_index_out       NUMBER;
3015 BEGIN
3016  IF P_PA_DEBUG_MODE = 'Y' THEN
3017     pa_debug.init_err_stack ('pa_fin_plan_utils.check_resource_gp_name_or_id');
3018  END IF;
3019     raise Invalid_Call_Exc; /* Changes for FP.M, Tracking Bug No - 3354518 */
3020     /*** bug 3683382 this piece code would never be executed as there is a immediate raise
3021     if p_resource_id is not null AND p_resource_id <> FND_API.G_MISS_NUM then
3022         if p_check_id_flag = 'Y' then
3023           -- validate the id that was passed in
3024           select resource_list_member_id
3025             into x_resource_id
3026             from pa_resource_list_members
3027             where resource_list_member_id = p_resource_id;
3028         elsif p_check_id_flag = 'N' then
3029           -- just return the p_resource_id, since we're not validating
3030           x_resource_id := p_resource_id;
3031         end if; -- p_check_id_flag
3032     else
3033         if p_resource_name is not null then
3034           -- p_resource_id = null, so we need to find the id
3035           select rlm.resource_list_member_id
3036             into x_resource_id
3037             from pa_resources r,
3038                  pa_resource_list_members rlm
3039             where r.name = p_resource_name and
3040                   r.resource_id = rlm.resource_id and
3041                   rlm.parent_member_id is null;
3042         else
3043           x_resource_id := null;
3044         end if;
3045     end if; -- p_resource_id is null
3046   bug 3683382 ***/
3047   x_return_status := FND_API.G_RET_STS_SUCCESS;
3048 	 IF P_PA_DEBUG_MODE = 'Y' THEN
3049 	  pa_debug.reset_err_stack;
3050 	END IF;
3051 EXCEPTION
3052     WHEN NO_DATA_FOUND THEN
3053       x_resource_id := NULL;
3054       x_return_status := FND_API.G_RET_STS_ERROR;
3055       x_msg_count := 1;
3056       x_error_msg := 'PA_FP_RES_NAME_AMBIGUOUS';
3057       PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3058                            p_msg_name            => 'PA_FP_RES_NAME_AMBIGUOUS');
3059       if x_msg_count = 1 then
3060                 PA_INTERFACE_UTILS_PUB.get_messages
3061                      (p_encoded        => FND_API.G_TRUE,
3062                       p_msg_index      => 1,
3063                       p_data           => x_error_msg,
3064                       p_msg_index_out  => l_msg_index_out);
3065       end if;
3066     WHEN TOO_MANY_ROWS THEN
3067       x_resource_id := NULL;
3068       x_return_status := FND_API.G_RET_STS_ERROR;
3069       x_msg_count := 1;
3070       x_error_msg := 'PA_FP_RES_NAME_AMBIGUOUS';
3071       PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3072                            p_msg_name            => 'PA_FP_RES_NAME_AMBIGUOUS');
3073       if x_msg_count = 1 then
3074                 PA_INTERFACE_UTILS_PUB.get_messages
3075                      (p_encoded        => FND_API.G_TRUE,
3076                       p_msg_index      => 1,
3077                       p_data           => x_error_msg,
3078                       p_msg_index_out  => l_msg_index_out);
3079       end if;
3080     WHEN Invalid_Call_Exc THEN  /* Changes for FP.M, Tracking Bug No - 3354518, Adding Exception handling block for Invalid_Call_Exc */
3081       x_resource_id := NULL;
3082       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3083       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
3084                               p_procedure_name  => 'check_resource_gp_name_or_id');
3085       RAISE;
3086     WHEN OTHERS THEN
3087       x_resource_id := NULL;
3088       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3089       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
3090                               p_procedure_name  => 'check_resource_gp_name_or_id');
3091       RAISE;
3092 END check_resource_gp_name_or_id;
3093 
3094 PROCEDURE check_resource_name_or_id
3095     (p_resource_id      IN  pa_resources.resource_id%TYPE,
3096      p_resource_name    IN  pa_resources.name%TYPE,
3097      p_check_id_flag    IN  VARCHAR2,
3098      x_resource_id      OUT NOCOPY pa_resources.resource_id%TYPE, --File.Sql.39 bug 4440895
3099      x_return_status    OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3100      x_msg_count        OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
3101      x_error_msg        OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
3102 is
3103   l_msg_index_out       NUMBER;
3104 BEGIN
3105  IF P_PA_DEBUG_MODE = 'Y' THEN
3106   pa_debug.init_err_stack ('pa_fin_plan_utils.check_resource_name_or_id');
3107  END IF;
3108   if p_resource_id is not null AND p_resource_id <> FND_API.G_MISS_NUM then
3109     if p_check_id_flag = 'Y' then
3110       -- validate the id that was passed in
3111       select resource_id
3112         into x_resource_id
3113         from pa_resources
3114         where resource_id = p_resource_id;
3115     elsif p_check_id_flag = 'N' then
3116       -- just return the p_resource_id, since we're not validating
3117       x_resource_id := p_resource_id;
3118     end if; -- p_check_id_flag
3119   else
3120     if p_resource_name is not null then
3121       -- p_resource_id = null, so we need to find the id
3122       select resource_id
3123         into x_resource_id
3124         from pa_resources
3125         where name = p_resource_name;
3126     else
3127       x_resource_id := null;
3128     end if;
3129   end if; -- p_resource_id is null
3130   x_return_status := FND_API.G_RET_STS_SUCCESS;
3131 	 IF P_PA_DEBUG_MODE = 'Y' THEN
3132 	  pa_debug.reset_err_stack;
3133 	END IF;
3134 EXCEPTION
3135     WHEN NO_DATA_FOUND THEN
3136       x_resource_id := NULL;
3137       x_return_status := FND_API.G_RET_STS_ERROR;
3138       x_msg_count := 1;
3139       x_error_msg := 'PA_FP_RES_NAME_AMBIGUOUS';
3140       PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3141                            p_msg_name            => 'PA_FP_RES_NAME_AMBIGUOUS');
3142       if x_msg_count = 1 then
3143                 PA_INTERFACE_UTILS_PUB.get_messages
3144                      (p_encoded        => FND_API.G_TRUE,
3145                       p_msg_index      => 1,
3146                       p_data           => x_error_msg,
3147                       p_msg_index_out  => l_msg_index_out);
3148       end if;
3149     WHEN TOO_MANY_ROWS THEN
3150       x_resource_id := NULL;
3151       x_return_status := FND_API.G_RET_STS_ERROR;
3152       x_msg_count := 1;
3153       x_error_msg := 'PA_FP_RES_NAME_AMBIGUOUS';
3154       PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3155                            p_msg_name            => 'PA_FP_RES_NAME_AMBIGUOUS');
3156       if x_msg_count = 1 then
3157                 PA_INTERFACE_UTILS_PUB.get_messages
3158                      (p_encoded        => FND_API.G_TRUE,
3159                       p_msg_index      => 1,
3160                       p_data           => x_error_msg,
3161                       p_msg_index_out  => l_msg_index_out);
3162       end if;
3163     WHEN OTHERS THEN
3164       x_resource_id := NULL;
3165       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3166       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
3167                               p_procedure_name  => 'check_resource_name_or_id');
3168       RAISE;
3169 END check_resource_name_or_id;
3170 
3171 
3172 FUNCTION Check_Proj_Fp_Options_Exists
3173   (p_project_id PA_PROJ_FP_OPTIONS.PROJECT_ID%type)
3174     return NUMBER
3175   IS
3176   l_dummy number;
3177 BEGIN
3178      select 1
3179         into   l_dummy
3180         from   sys.dual
3181         where  exists
3182         /* Changes for FP.M, Tracking Bug No - 3354518
3183            Adding conditon in the where clause below to
3184            check for new column use_for_workplan flag.
3185            This column indicates if a plan type is being
3186            used for workplan or not.
3187         So adding a join to pa_fin_plan_types_b and checking status of use_for_workplan_flag.
3188            Without this check the function would return success status even if WP plantype exists */
3189             (select 1 from pa_proj_fp_options pfo, pa_fin_plan_types_b pft -- Added pa_fin_plan_types_b for FP.M changes
3190                 where pfo.project_id = p_project_id
3191           /*Changes for FP.M start here */
3192             and pfo.fin_plan_option_level_code='PLAN_TYPE' /*bug 3224177 added fin_plan_option_level_code check*/
3193             and nvl(pfo.fin_plan_type_id,-99) = pft.fin_plan_type_id
3194             and nvl(pft.use_for_workplan_flag,'N') = 'N');
3195           /*Changes for FP.M end here */
3196         return 1;
3197 
3198 EXCEPTION
3199      WHEN NO_DATA_FOUND THEN
3200          return 0;
3201      WHEN OTHERS THEn
3202          return SQLCODE;
3203 END Check_Proj_Fp_Options_Exists;
3204 
3205 
3206 
3207 /* =================================================================
3208    FUNCTION get_amttype_id:  Created 9/14/02 by Danny Lai
3209    This function takes in an amount type code and returns the id
3210    associated with it.
3211    ================================================================= */
3212 FUNCTION get_amttype_id
3213   ( p_amt_typ_code     IN pa_amount_types_b.amount_type_code%TYPE) RETURN NUMBER
3214 is
3215     l_amount_type_id pa_amount_types_b.amount_type_id%TYPE;
3216     l_amt_code pa_fp_org_fcst_gen_pub.char240_data_type_table;   /* manoj: referred to pa_fp_org_fcst_gen_pub */
3217     l_amt_id   pa_fp_org_fcst_gen_pub.number_data_type_table;    /* manoj: referred to pa_fp_org_fcst_gen_pub */
3218 
3219     l_debug_mode VARCHAR2(30);
3220 
3221     CURSOR get_amt_det IS
3222     SELECT atb.amount_type_id
3223           ,atb.amount_type_code
3224       FROM pa_amount_types_b atb
3225      WHERE atb.amount_type_class = 'R';
3226 
3227     l_stage number := 0;
3228 
3229 BEGIN
3230  IF P_PA_DEBUG_MODE = 'Y' THEN
3231      pa_debug.init_err_stack('pa_fin_plan_utils.get_amttype_id');
3232  END IF;
3233 
3234      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
3235      l_debug_mode := NVL(l_debug_mode, 'Y');
3236 
3237      IF P_PA_DEBUG_MODE = 'Y' THEN
3238         pa_debug.set_process('get_amttype_id: ' || 'PLSQL','LOG',l_debug_mode);
3239      END IF;
3240 
3241        l_amount_type_id := -99;
3242 
3243        IF l_amt_code.last IS NULL THEN
3244           OPEN get_amt_det;
3245           LOOP
3246               FETCH get_amt_det into l_amt_id(nvl(l_amt_id.last+1,1))
3247                                     ,l_amt_code(nvl(l_amt_code.last+1,1));
3248               EXIT WHEN get_amt_det%NOTFOUND;
3249           END LOOP;
3250           CLOSE get_amt_det;
3251        END IF;
3252 
3253        IF l_amt_code.last IS NOT NULL THEN
3254           FOR i in l_amt_id.first..l_amt_id.last LOOP
3255               IF l_amt_code(i) = p_amt_typ_code THEN
3256                  l_amount_type_id := l_amt_id(i);
3257               END IF;
3258           END LOOP;
3259        END IF;
3260        IF l_amount_type_id = -99 THEN
3261                  pa_debug.g_err_stage := 'p_amt_typ_code         ['||p_amt_typ_code          ||']';
3262                  IF P_PA_DEBUG_MODE = 'Y' THEN
3263                     pa_debug.write_file('get_amttype_id: ' || pa_debug.g_err_stage);
3264                  END IF;
3265        END IF;
3266 	 IF P_PA_DEBUG_MODE = 'Y' THEN
3267 	       pa_debug.reset_err_stack;
3268 	  END IF;
3269        RETURN(l_amount_type_id);
3270 
3271 EXCEPTION
3272      WHEN OTHERS THEN
3273           FND_MSG_PUB.add_exc_msg(
3274               p_pkg_name => 'pa_fin_plan_utils.get_amttype_id'
3275              ,p_procedure_name => PA_DEBUG.G_Err_Stack);
3276 
3277               IF P_PA_DEBUG_MODE = 'Y' THEN
3278                  pa_debug.write_file('get_amttype_id: ' || SQLERRM);
3279                  pa_debug.reset_err_stack;
3280      	      END IF;
3281               RAISE;
3282 END get_amttype_id;
3283 
3284 /*=============================================================================
3285   Procedure Check_Locked_By_User:  Created 09/10/2002 by Danny Lai
3286   This function accepts a userid and a budget_version_id.
3287   If the budget version is locked by the user, x_locked_by_userid = 'Y'
3288   Otherwise, x_locked_by_userid = 'N' and x_locked_by_userid stores the user
3289   who has the version locked.
3290 ==============================================================================*/
3291 
3292 PROCEDURE Check_Locked_By_User
3293         (p_user_id              IN      NUMBER,
3294          p_budget_version_id    IN      pa_budget_versions.budget_version_id%TYPE,
3295          x_is_locked_by_userid  OUT     NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3296          x_locked_by_person_id  OUT     NOCOPY NUMBER, --File.Sql.39 bug 4440895
3297          x_return_status        OUT     NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3298          x_msg_count            OUT     NOCOPY NUMBER, --File.Sql.39 bug 4440895
3299          x_msg_data             OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
3300 IS
3301 
3302 cursor budget_csr is
3303   select locked_by_person_id,
3304          budget_status_code
3305     from pa_budget_versions
3306     where budget_version_id = p_budget_version_id;
3307 budget_rec budget_csr%ROWTYPE;
3308 
3309 l_person_id     pa_budget_versions.locked_by_person_id%TYPE;
3310 l_resource_id   NUMBER;
3311 l_resource_name per_all_people_f.full_name%TYPE; -- VARCHAR2(80); for bug # 2933777
3312 
3313 -- local error handling variables
3314 l_msg_count     NUMBER := 0;
3315 l_msg_data      VARCHAR2(2000);
3316 l_data          VARCHAR2(2000);
3317 l_msg_index_out NUMBER;
3318 l_module_name  VARCHAR2(100) := 'pa.plsql.pa_fin_plan_utils';
3319 
3320 BEGIN
3321   x_msg_count := 0;
3322   x_return_status := FND_API.G_RET_STS_SUCCESS;
3323  IF P_PA_DEBUG_MODE = 'Y' THEN
3324   pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Check_Locked_By_User');
3325  END IF;
3326   /* CHECK FOR BUSINESS RULES VIOLATIONS */
3327 
3328   -- Check for VALID USER ID
3329 IF P_PA_DEBUG_MODE = 'Y' THEN
3330   pa_debug.g_err_stage := 'calling get user info';
3331   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3332 END IF;
3333   /*
3334   Bug 2933777 : l_resource_name is obtained from the following procedure. But is not required
3335   for processing in this API. Hence UTF 8 impact is limited to fetching the value into a
3336   variable of correct length.
3337   */
3338   PA_COMP_PROFILE_PUB.GET_USER_INFO
3339           (p_user_id         => p_user_id,
3340            x_person_id       => l_person_id,
3341            x_resource_id     => l_resource_id,
3342            x_resource_name   => l_resource_name);
3343 IF P_PA_DEBUG_MODE = 'Y' THEN
3344   pa_debug.g_err_stage := 'l_person_id = ' || l_person_id;
3345   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3346 END IF;
3347   if l_person_id is null then
3348     PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3349                          p_msg_name            => 'PA_FP_BAD_USER_ID');
3350     x_return_status := FND_API.G_RET_STS_ERROR;
3351   end if; -- error with p_user_id
3352 
3353   -- Check for VALID BUDGET VERSION ID
3354 IF P_PA_DEBUG_MODE = 'Y' THEN
3355   pa_debug.g_err_stage := 'opening budget_csr';
3356   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3357 END IF;
3358   open budget_csr;
3359   fetch budget_csr into budget_rec;
3360   if budget_csr%NOTFOUND then
3361 	IF P_PA_DEBUG_MODE = 'Y' THEN
3362 	    pa_debug.g_err_stage := 'budget_csr notfound true';
3363 	    pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3364 	END IF;
3365     PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3366                          p_msg_name            => 'PA_FP_INVALID_PLAN_VERSION');
3367     x_return_status := FND_API.G_RET_STS_ERROR;
3368   end if; -- invalid budget_version_id
3369   close budget_csr;
3370 
3371   /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
3372     l_msg_count := FND_MSG_PUB.count_msg;
3373     if x_return_status <> FND_API.G_RET_STS_SUCCESS then
3374 	IF P_PA_DEBUG_MODE = 'Y' THEN
3375 	        pa_debug.g_err_stage := 'l_msg_count = ' || l_msg_count;
3376 	        pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3377 	END IF;
3378         x_return_status := FND_API.G_RET_STS_ERROR;
3379         if l_msg_count = 1 then
3380              PA_INTERFACE_UTILS_PUB.get_messages
3381                  (p_encoded        => FND_API.G_TRUE,
3382                   p_msg_index      => 1,
3383                   p_msg_count      => l_msg_count,
3384                   p_msg_data       => l_msg_data,
3385                   p_data           => l_data,
3386                   p_msg_index_out  => l_msg_index_out);
3387              x_msg_data := l_data;
3388              x_msg_count := l_msg_count;
3389             else
3390              x_msg_count := l_msg_count;
3391         end if;
3392 	 IF P_PA_DEBUG_MODE = 'Y' THEN
3393 	        pa_debug.reset_err_stack;
3394 	END IF;
3395         return;
3396     end if;
3397 
3398   /* If NO VIOLATIONS, proceed */
3399 
3400   -- BASELINED VERSIONS ARE NEVER LOCKED BY ANYONE
3401   if budget_rec.budget_status_code = 'B' then
3402 	IF P_PA_DEBUG_MODE = 'Y' THEN
3403 	    pa_debug.g_err_stage := 'budget_rec.budget_status_code  = ' || budget_rec.budget_status_code;
3404 	    pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3405 	END IF;
3406     x_is_locked_by_userid := 'N';
3407   else
3408 	IF P_PA_DEBUG_MODE = 'Y' THEN
3409 	    pa_debug.g_err_stage := 'budget_rec.locked_by_person_id  = ' || budget_rec.locked_by_person_id;
3410 	    pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3411 	END IF;
3412     if budget_rec.locked_by_person_id is null then
3413       -- BUDGET IS UNLOCKED
3414 
3415       x_is_locked_by_userid := 'N';
3416       x_locked_by_person_id := null;
3417 
3418     -- BUDGET IS LOCKED: LOOK FOR MATCH
3419     else
3420       if budget_rec.locked_by_person_id = l_person_id then
3421         -- FOUND MATCH
3422         x_is_locked_by_userid := 'Y';
3423         x_locked_by_person_id := l_person_id;
3424       else
3425         -- NO MATCH: VERSION IS LOCKED BY SOMEONE ELSE
3426         x_is_locked_by_userid := 'N';
3427         -- BUG FIX 2829725: incorrect locked_by_user_id
3428         --x_locked_by_person_id := l_person_id;
3429         x_locked_by_person_id := budget_rec.locked_by_person_id;
3430       end if; -- matching person id's
3431     end if; -- locked_by_person_id is null
3432   end if; -- budget_status_code
3433 IF P_PA_DEBUG_MODE = 'Y' THEN
3434   pa_debug.g_err_stage := 'exiting check_locked_by_user';
3435   pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
3436   pa_debug.reset_err_stack;
3437 END IF;
3438 EXCEPTION
3439   WHEN OTHERS THEN
3440       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3441       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
3442                               p_procedure_name  => 'Check_Locked_By_User');
3443  IF P_PA_DEBUG_MODE = 'Y' THEN
3444       pa_debug.reset_err_stack;
3445  END IF;
3446       RAISE;
3447 END Check_Locked_By_User;
3448 
3449 
3450 /*=============================================================================
3451   Procedure Check_Both_Locked_By_User:  Created 09/10/2002 by Danny Lai
3452   This function accepts a userid and TWO budget_version_id's.
3453   If the budget version is locked by the user, x_locked_by_userid = 'Y'
3454   Otherwise, x_locked_by_userid = 'N'
3455   (this procedure calls Check_Locked_By_User twice)
3456 ==============================================================================*/
3457 
3458 PROCEDURE Check_Both_Locked_By_User
3459         (p_user_id              IN      NUMBER,
3460          p_budget_version_id1   IN      pa_budget_versions.budget_version_id%TYPE,
3461          p_budget_version_id2   IN      pa_budget_versions.budget_version_id%TYPE,
3462          x_is_locked_by_userid  OUT     NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3463          x_return_status        OUT     NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3464          x_msg_count            OUT     NOCOPY NUMBER, --File.Sql.39 bug 4440895
3465          x_msg_data             OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
3466 IS
3467 l_is_locked_by_userid1 VARCHAR2(1);
3468 l_is_locked_by_userid2 VARCHAR2(1);
3469 l_locked_by_person_id   pa_budget_versions.locked_by_person_id%TYPE;
3470 
3471 -- local error handling variables
3472 l_return_status VARCHAR2(1);
3473 l_msg_count     NUMBER := 0;
3474 l_msg_data      VARCHAR2(2000);
3475 l_data          VARCHAR2(2000);
3476 l_msg_index_out NUMBER;
3477 
3478 BEGIN
3479   x_msg_count := 0;
3480   x_return_status := FND_API.G_RET_STS_SUCCESS;
3481  IF P_PA_DEBUG_MODE = 'Y' THEN
3482   pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Check_Locked_By_User');
3483 END IF;
3484   pa_fin_plan_utils.Check_Locked_By_User
3485         (p_user_id              => p_user_id,
3486          p_budget_version_id    => p_budget_version_id1,
3487          x_is_locked_by_userid  => l_is_locked_by_userid1,
3488          x_locked_by_person_id  => l_locked_by_person_id,
3489          x_return_status        => l_return_status,
3490          x_msg_count            => l_msg_count,
3491          x_msg_data             => l_msg_data);
3492   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3493     raise pa_fin_plan_utils.Check_Locked_By_User_Exception;
3494   end if;
3495 
3496   pa_fin_plan_utils.Check_Locked_By_User
3497         (p_user_id              => p_user_id,
3498          p_budget_version_id    => p_budget_version_id2,
3499          x_is_locked_by_userid  => l_is_locked_by_userid2,
3500          x_locked_by_person_id  => l_locked_by_person_id,
3501          x_return_status        => l_return_status,
3502          x_msg_count            => l_msg_count,
3503          x_msg_data             => l_msg_data);
3504   if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3505     raise pa_fin_plan_utils.Check_Locked_By_User_Exception;
3506   end if;
3507 
3508   if l_is_locked_by_userid1 = 'Y' and l_is_locked_by_userid2 = 'Y' then
3509     x_is_locked_by_userid := 'Y';
3510   else
3511     x_is_locked_by_userid := 'N';
3512   end if;
3513  IF P_PA_DEBUG_MODE = 'Y' THEN
3514   pa_debug.reset_err_stack;
3515  END IF;
3516 EXCEPTION
3517   WHEN pa_fin_plan_utils.Check_Locked_By_User_Exception THEN
3518       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3519       IF P_PA_DEBUG_MODE = 'Y' THEN
3520          pa_debug.write_file('Check_Both_Locked_By_User: ' || 'Check_Locked_By_User_Exception reached');
3521       END IF;
3522       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
3523                               p_procedure_name  => 'Check_Both_Locked_By_User');
3524   WHEN OTHERS THEN
3525       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3526       FND_MSG_PUB.add_exc_msg(p_pkg_name        => 'PA_FIN_PLAN_UTILS',
3527                               p_procedure_name  => 'Check_Both_Locked_By_User');
3528       RAISE;
3529 
3530 END Check_Both_Locked_By_User;
3531 
3532 FUNCTION check_budget_trans_exists
3533         (p_project_id           IN      pa_projects_all.project_id%TYPE)
3534         return VARCHAR2 is
3535 l_return VARCHAR2(1) := 'N';
3536 BEGIN
3537 /* Bug 3106741
3538    Modified the second exists clause to avoid FT scan on pa_fp_txn_currencies
3539    table. As an index is available on proj_fp_options_id for the above table,
3540    pa_proj_fp_options table has been included to fetch all the options that
3541    belong to a given project.
3542  */
3543 
3544 /* Commenting out the select statment below for FP.M, Tracking Bug No - 3354518
3545    The Select statement is modified and re-written below.
3546    Please note that the similar change is also done for bug no - 3224177 in version 115.117.
3547    The Select statment below has lot of redundant code which can be simplified
3548    to check for the existence of PLAN_TYPE fp_options only haveing use_for_workplan
3549    not set to 'Y'. Please note that this API will not be called for workplan Usage*/
3550 /*
3551   Select 'Y'
3552   Into   l_return
3553   From   dual
3554   Where  exists (Select 'x'
3555                 from    pa_budget_lines bl,
3556                         pa_budget_versions bv
3557                 where   bl.budget_version_id = bv.budget_version_id
3558                 and     bv.project_id = p_project_id)
3559           OR  exists (Select 'x'                       -- included for bug 3224177
3560                from  pa_proj_fp_options pfo,
3561                         pa_projects_all pa where
3562                         pfo.project_id = pa.project_id and
3563                               pa.project_id = p_project_id and
3564                               pfo.fin_plan_option_level_code = 'PLAN_TYPE');
3565 /* commented for bug 3224177 starts
3566   OR    exists (Select 'x'
3567                from     pa_fp_txn_currencies fpcurr,
3568                         pa_proj_fp_options pfo, -- bug 3106741
3569                         pa_projects_all pa
3570                where    pa.project_currency_code = fpcurr.txn_currency_code
3571                and      pa.project_id = fpcurr.project_id
3572                and      fpcurr.project_currency_flag = 'Y'
3573                and      pfo.proj_fp_options_id = fpcurr.proj_fp_options_id -- bug 3106741
3574                and      pfo.project_id = pa.project_id -- bug 3106741
3575                and      pa.project_id = p_project_id
3576             and      pfo.fin_plan_option_level_code = 'PLAN_TYPE'  ); end of bug 3224177 comment*/
3577 
3578 
3579  /* Changes for FP.M, Tracking Bug No - 3354518 End here */
3580  /* Modified Select Clause */
3581   Select 'Y'
3582   Into   l_return
3583   From   dual
3584   Where  exists (Select 'x'
3585                 from    pa_budget_lines bl,
3586                         pa_budget_versions bv
3587                 where   bl.budget_version_id = bv.budget_version_id
3588                 and     bv.project_id = p_project_id)
3589    OR  exists (Select   'x'                       -- included for bug 3224177
3590                  from   pa_proj_fp_options pfo, pa_fin_plan_types_b pft ,
3591                         pa_projects_all pa
3592              where   pfo.project_id = pa.project_id and
3593                         pa.project_id = p_project_id and
3594                         /* Commented out the below for bug 5364011*/
3595 --                        pfo.fin_plan_option_level_code = 'PLAN_TYPE' and
3596                         pfo.fin_plan_option_level_code = 'PLAN_VERSION' and -- Bug 5364011.
3597                pfo.fin_plan_type_id = pft.fin_plan_type_id and
3598                nvl(pft.use_for_workplan_flag,'N') = 'N');
3599 
3600   /* Changes for FP.M, Tracking Bug No - 3354518 End here */
3601 
3602   return l_return;
3603 Exception
3604   When No_Data_Found Then
3605     return l_return;
3606 END check_budget_trans_exists;
3607 
3608 FUNCTION enable_auto_baseline
3609         (p_project_id           IN      pa_projects_all.project_id%TYPE)
3610         return VARCHAR2 is
3611 Cursor c1 is
3612 Select  'Y'
3613 from   pa_proj_fp_options po /* Bug# 2665767 - Plan type option alone can be checked */
3614 where  po.approved_rev_plan_type_flag = 'Y'
3615 and    po.fin_plan_preference_code = 'COST_AND_REV_SAME'
3616 and    po.fin_plan_option_level_code = 'PLAN_TYPE'
3617 and    po.project_id = p_project_id;
3618 
3619 c1_rec   c1%rowtype;
3620 l_return VARCHAR2(1);
3621 BEGIN
3622   open c1;
3623   fetch c1 into c1_rec;
3624   if c1%notfound then
3625     close c1;
3626     l_return := 'Y';
3627   else
3628     close c1;
3629     l_return := 'N';
3630   end if;
3631   return l_return;
3632 END enable_auto_baseline;
3633 
3634 
3635 PROCEDURE Get_Resource_List_Info
3636          (p_resource_list_id           IN   pa_resource_lists.RESOURCE_LIST_ID%TYPE
3637          ,x_res_list_is_uncategorized  OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3638          ,x_is_resource_list_grouped   OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3639          ,x_group_resource_type_id     OUT  NOCOPY pa_resource_lists.GROUP_RESOURCE_TYPE_ID%TYPE --File.Sql.39 bug 4440895
3640          ,x_return_status              OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3641          ,x_msg_count                  OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
3642          ,x_msg_data                   OUT  NOCOPY VARCHAR2) IS --File.Sql.39 bug 4440895
3643 
3644 
3645        l_return_status      VARCHAR2(2000);
3646        l_msg_count          NUMBER :=0;
3647        l_msg_data           VARCHAR2(2000);
3648        l_data               VARCHAR2(2000);
3649        l_msg_index_out      NUMBER;
3650        l_debug_mode         VARCHAR2(30);
3651 
3652 BEGIN
3653      x_msg_count := 0;
3654      x_return_status := FND_API.G_RET_STS_SUCCESS;
3655 
3656  IF P_PA_DEBUG_MODE = 'Y' THEN
3657      pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Resource_List_Info');
3658 END IF;
3659      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
3660      l_debug_mode := NVL(l_debug_mode, 'Y');
3661      IF P_PA_DEBUG_MODE = 'Y' THEN
3662         pa_debug.set_process('Get_Resource_List_Info: ' || 'PLSQL','LOG',l_debug_mode);
3663      END IF;
3664 
3665      -- Check for not null parameters
3666 
3667      pa_debug.g_err_stage := 'Checking for valid parameters:';
3668      IF P_PA_DEBUG_MODE = 'Y' THEN
3669         pa_debug.write('Get_Resource_List_Info: ' || l_module_name,pa_debug.g_err_stage,3);
3670      END IF;
3671 
3672      IF (p_resource_list_id  IS NULL)
3673 
3674      THEN
3675 
3676          pa_debug.g_err_stage := 'resource list id ='||to_char(p_resource_list_id);
3677          IF P_PA_DEBUG_MODE = 'Y' THEN
3678             pa_debug.write('Get_Resource_List_Info: ' || l_module_name,pa_debug.g_err_stage,5);
3679          END IF;
3680          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
3681                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
3682 
3683          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3684 
3685      END IF;
3686 
3687      SELECT nvl(uncategorized_flag,'N')
3688            ,decode (group_resource_type_id,0,'N','Y')
3689            ,group_resource_type_id
3690        INTO x_res_list_is_uncategorized
3691            ,x_is_resource_list_grouped
3692            ,x_group_resource_type_id
3693        FROM pa_resource_lists
3694       WHERE resource_list_id = p_resource_list_id ;
3695 
3696  IF P_PA_DEBUG_MODE = 'Y' THEN
3697       pa_debug.reset_err_stack;
3698   END IF;
3699  EXCEPTION
3700      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
3701 
3702              l_msg_count := FND_MSG_PUB.count_msg;
3703 
3704              IF l_msg_count = 1 THEN
3705 
3706                   PA_INTERFACE_UTILS_PUB.get_messages
3707                         (p_encoded        => FND_API.G_TRUE
3708                          ,p_msg_index      => 1
3709                          ,p_msg_count      => l_msg_count
3710                          ,p_msg_data       => l_msg_data
3711                          ,p_data           => l_data
3712                          ,p_msg_index_out  => l_msg_index_out);
3713 
3714                   x_msg_data := l_data;
3715                   x_msg_count := l_msg_count;
3716 
3717              ELSE
3718 
3719                  x_msg_count := l_msg_count;
3720 
3721              END IF;
3722 
3723               pa_debug.g_err_stage:='Invalid Arguments Passed';
3724               IF P_PA_DEBUG_MODE = 'Y' THEN
3725                  pa_debug.write('Get_Resource_List_Info: ' || l_module_name,pa_debug.g_err_stage,5);
3726               END IF;
3727 
3728               x_return_status:= FND_API.G_RET_STS_ERROR;
3729 
3730  IF P_PA_DEBUG_MODE = 'Y' THEN
3731               pa_debug.reset_err_stack;
3732  END IF;
3733               RAISE;
3734 
3735         WHEN Others THEN
3736 
3737              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3738              x_msg_count     := 1;
3739              x_msg_data      := SQLERRM;
3740 
3741              FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_UTILS'
3742                              ,p_procedure_name  => 'Get_Resource_List_Info');
3743 
3744              pa_debug.g_err_stage:='Unexpected Error';
3745              IF P_PA_DEBUG_MODE = 'Y' THEN
3746                 pa_debug.write('Get_Resource_List_Info: ' || l_module_name,pa_debug.g_err_stage,5);
3747 
3748              pa_debug.reset_err_stack;
3749 	     END IF;
3750         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3751 
3752 
3753 END Get_Resource_List_Info ;
3754 
3755 
3756 /* Changes for FPM, Tracking Bug - 3354518
3757    Adding Procedure Get_Resource_List_Info below.
3758    Please note that this proceedure is a overloaded procedure.
3759    The reason behind overloading this procedure below is the
3760    is the addiditon of three fields use_for_wp_flag,control_flag
3761    and migration_code to pa_resource_lists_all_bg */
3762 PROCEDURE Get_Resource_List_Info
3763          (p_resource_list_id           IN   pa_resource_lists.RESOURCE_LIST_ID%TYPE
3764          ,x_res_list_is_uncategorized  OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3765          ,x_is_resource_list_grouped   OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3766          ,x_group_resource_type_id     OUT  NOCOPY pa_resource_lists.GROUP_RESOURCE_TYPE_ID%TYPE --File.Sql.39 bug 4440895
3767          ,x_use_for_wp_flag            OUT  NOCOPY pa_resource_lists_all_bg.use_for_wp_flag%TYPE /*New Column added for FPM */ --File.Sql.39 bug 4440895
3768          ,x_control_flag               OUT  NOCOPY pa_resource_lists_all_bg.control_flag%TYPE /*New Column added for FPM */ --File.Sql.39 bug 4440895
3769          ,x_migration_code             OUT  NOCOPY pa_resource_lists_all_bg.migration_code%TYPE /*New Column added for FPM */ --File.Sql.39 bug 4440895
3770          ,x_return_status              OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3771          ,x_msg_count                  OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
3772          ,x_msg_data                   OUT  NOCOPY VARCHAR2) IS --File.Sql.39 bug 4440895
3773 
3774 
3775        l_return_status      VARCHAR2(2000);
3776        l_msg_count          NUMBER :=0;
3777        l_msg_data           VARCHAR2(2000);
3778        l_data               VARCHAR2(2000);
3779        l_msg_index_out      NUMBER;
3780        l_debug_mode         VARCHAR2(30);
3781 
3782 BEGIN
3783      x_msg_count := 0;
3784      x_return_status := FND_API.G_RET_STS_SUCCESS;
3785 
3786  IF P_PA_DEBUG_MODE = 'Y' THEN
3787      pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Resource_List_Info');
3788 END IF;
3789      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
3790      l_debug_mode := NVL(l_debug_mode, 'Y');
3791      IF P_PA_DEBUG_MODE = 'Y' THEN
3792         pa_debug.set_process('Get_Resource_List_Info(Overloaded): ' || 'PLSQL','LOG',l_debug_mode);
3793      END IF;
3794 
3795      -- Check for not null parameters
3796 
3797      pa_debug.g_err_stage := 'Checking for valid parameters:';
3798      IF P_PA_DEBUG_MODE = 'Y' THEN
3799         pa_debug.write('Get_Resource_List_Info(Overloaded): ' || l_module_name,pa_debug.g_err_stage,3);
3800      END IF;
3801 
3802      IF (p_resource_list_id  IS NULL)
3803 
3804      THEN
3805 
3806          pa_debug.g_err_stage := 'resource list id ='||to_char(p_resource_list_id);
3807          IF P_PA_DEBUG_MODE = 'Y' THEN
3808             pa_debug.write('Get_Resource_List_Info(Overloaded): ' || l_module_name,pa_debug.g_err_stage,5);
3809          END IF;
3810          PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
3811                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
3812 
3813          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3814 
3815      END IF;
3816 
3817      SELECT nvl(uncategorized_flag,'N')
3818            ,decode (group_resource_type_id,0,'N','Y')
3819            ,group_resource_type_id
3820         ,use_for_wp_flag
3821            ,control_flag
3822         ,migration_code
3823        INTO x_res_list_is_uncategorized
3824            ,x_is_resource_list_grouped
3825            ,x_group_resource_type_id
3826         ,x_use_for_wp_flag
3827            ,x_control_flag
3828            ,x_migration_code
3829        FROM pa_resource_lists_all_bg
3830       WHERE resource_list_id = p_resource_list_id ;
3831 
3832  IF P_PA_DEBUG_MODE = 'Y' THEN
3833       pa_debug.reset_err_stack;
3834  END IF;
3835  EXCEPTION
3836      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
3837 
3838              l_msg_count := FND_MSG_PUB.count_msg;
3839 
3840              IF l_msg_count = 1 THEN
3841 
3842                   PA_INTERFACE_UTILS_PUB.get_messages
3843                         (p_encoded        => FND_API.G_TRUE
3844                          ,p_msg_index      => 1
3845                          ,p_msg_count      => l_msg_count
3846                          ,p_msg_data       => l_msg_data
3847                          ,p_data           => l_data
3848                          ,p_msg_index_out  => l_msg_index_out);
3849 
3850                   x_msg_data := l_data;
3851                   x_msg_count := l_msg_count;
3852 
3853              ELSE
3854 
3855                  x_msg_count := l_msg_count;
3856 
3857              END IF;
3858 
3859               pa_debug.g_err_stage:='Invalid Arguments Passed';
3860               IF P_PA_DEBUG_MODE = 'Y' THEN
3861                  pa_debug.write('Get_Resource_List_Info(Overloaded): ' || l_module_name,pa_debug.g_err_stage,5);
3862               END IF;
3863 
3864               x_return_status:= FND_API.G_RET_STS_ERROR;
3865 
3866  IF P_PA_DEBUG_MODE = 'Y' THEN
3867               pa_debug.reset_err_stack;
3868  END IF;
3869               RAISE;
3870 
3871         WHEN Others THEN
3872 
3873              x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3874              x_msg_count     := 1;
3875              x_msg_data      := SQLERRM;
3876 
3877              FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_UTILS'
3878                              ,p_procedure_name  => 'Get_Resource_List_Info');
3879 
3880              pa_debug.g_err_stage:='Unexpected Error';
3881              IF P_PA_DEBUG_MODE = 'Y' THEN
3882                 pa_debug.write('Get_Resource_List_Info: ' || l_module_name,pa_debug.g_err_stage,5);
3883                 pa_debug.reset_err_stack;
3884 	     END IF;
3885         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3886 
3887 
3888 END Get_Resource_List_Info ;
3889 
3890 /* This api should be used only for budgets created using self-service. */
3891 PROCEDURE Get_Uncat_Resource_List_Info
3892          (x_resource_list_id           OUT   NOCOPY pa_resource_lists.RESOURCE_LIST_ID%TYPE --File.Sql.39 bug 4440895
3893          ,x_resource_list_member_id    OUT   NOCOPY pa_resource_list_members.RESOURCE_LIST_MEMBER_ID%TYPE --File.Sql.39 bug 4440895
3894          ,x_track_as_labor_flag        OUT   NOCOPY pa_resource_list_members.TRACK_AS_LABOR_FLAG%TYPE --File.Sql.39 bug 4440895
3895          ,x_unit_of_measure            OUT   NOCOPY pa_resources.UNIT_OF_MEASURE%TYPE --File.Sql.39 bug 4440895
3896          ,x_return_status              OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
3897          ,x_msg_count                  OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
3898          ,x_msg_data                   OUT  NOCOPY VARCHAR2) IS --File.Sql.39 bug 4440895
3899 
3900     l_debug_mode         VARCHAR2(30);
3901     l_business_group_id  pa_resource_lists_all_bg.business_group_id%TYPE; -- bug 2760675
3902 
3903 BEGIN
3904 
3905  IF P_PA_DEBUG_MODE = 'Y' THEN
3906     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Uncat_Resource_List_Info');
3907   END IF;
3908     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
3909     l_debug_mode := NVL(l_debug_mode, 'Y');
3910 
3911     IF P_PA_DEBUG_MODE = 'Y' THEN
3912       pa_debug.set_process('PLSQL','LOG',l_debug_mode);
3913     END IF;
3914 
3915     x_msg_count := 0;
3916     x_return_status := FND_API.G_RET_STS_SUCCESS;
3917 
3918     IF P_PA_DEBUG_MODE = 'Y' THEN
3919       pa_debug.g_err_stage:='Executing the uncat res list info select...';
3920       pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
3921     END IF;
3922 
3923     l_business_group_id := pa_utils.business_group_id; -- bug 2760675
3924 
3925     -- performance bug fix 2788668
3926 
3927  /* 4/22 TEMPORARY FIX: added where clause to prevent multiple rows
3928   * from being returned:  prlm.object_type='RESOURCE_LIST'
3929   * The final fix is pending discussion w/architects.  This fix was made
3930   * so Q/A can continue
3931   */
3932 
3933  /* Bug 4052562 - Pa_resources is obsolete for FP M. Also, there would
3934  *  be 4 default rlms for every resource list. As such, in FP M, the
3935  *  uncat rlm would have unit of measure as DOLLARS and this is available
3936  *  in rlm table itself. Track_as_labor_flag is obsolete for FP M.
3937  *  This change would also improve the performance and RLM's N5 index would be
3938  *  used.
3939  */
3940 --  select /*+ Use_NL(prlm,pr) index(prlm, PA_RESOURCE_LIST_MEMBERS_N1) */
3941   select pbg.resource_list_id,
3942          prlm.track_as_labor_flag,
3943          prlm.resource_list_member_id,
3944          prlm.unit_of_measure
3945     into x_resource_list_id,
3946         x_track_as_labor_flag,
3947         x_resource_list_member_id,
3948         x_unit_of_measure
3949     from pa_resource_lists_all_bg pbg,
3950         pa_resource_list_members prlm
3951     where pbg.business_group_id = l_business_group_id and -- bug 2760675  pa_utils.business_group_id and
3952           pbg.uncategorized_flag = 'Y' and
3953           prlm.resource_list_id = pbg.resource_list_id and
3954           prlm.object_id = pbg.resource_list_id and
3955           prlm.object_type = 'RESOURCE_LIST' and
3956           prlm.resource_class_code = 'FINANCIAL_ELEMENTS' and
3957           prlm.resource_class_flag = 'Y';
3958 /*
3959         SELECT pbg.resource_list_id
3960               ,prlm.track_as_labor_flag
3961               ,prlm.resource_list_member_id
3962               ,pr.unit_of_measure
3963          INTO x_resource_list_id
3964               ,x_track_as_labor_flag
3965               ,x_resource_list_member_id
3966               ,x_unit_of_measure
3967          FROM pa_resource_lists_all_bg pbg
3968              ,pa_resource_list_members prlm
3969              ,pa_resources pr
3970         WHERE prlm.resource_list_id = pbg.resource_list_id
3971           AND pbg.resource_list_id = prlm.resource_list_id
3972           AND prlm.resource_id = pr.resource_id
3973           AND pbg.uncategorized_flag = 'Y'
3974           AND pbg.business_group_id =  pa_utils.business_group_id;
3975 */
3976  IF P_PA_DEBUG_MODE = 'Y' THEN
3977     pa_debug.reset_err_stack;
3978  END IF;
3979 EXCEPTION
3980   WHEN NO_DATA_FOUND THEN
3981 
3982         IF P_PA_DEBUG_MODE = 'Y' THEN
3983           pa_debug.g_err_stage:='Uncat Res List could not be found!!!';
3984           pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
3985         END IF;
3986 
3987         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
3988                              p_msg_name => 'PA_BU_NO_UNCAT_RESOURCE_LIST' );
3989  IF P_PA_DEBUG_MODE = 'Y' THEN
3990         pa_debug.reset_err_stack;
3991  END IF;
3992         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3993 
3994 END Get_Uncat_Resource_List_Info;
3995 
3996 /*=============================================================================
3997  This api is a wrapper over the api Get_Appr_Cost_Plan_Type_Info in order to
3998  consider the effects of upgrade. As the user might go for the partial upgrade
3999  of budget types in a project, it is necessary to check for AC plan type in the
4000  old model.
4001  Return value :
4002    If an Approved cost plan type is attached to a project then the API returns
4003    a non-negative, non-zero value. else returns NULL.
4004 ==============================================================================*/
4005 
4006 
4007 PROCEDURE Is_AC_PT_Attached_After_UPG(
4008           p_project_id     IN   pa_projects_all.project_id%TYPE
4009           ,x_plan_type_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
4010           ,x_return_status OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4011           ,x_msg_count     OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
4012           ,x_msg_data      OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4013 AS
4014 
4015     --Start of variables used for debugging
4016 
4017     l_msg_count          NUMBER :=0;
4018     l_data               VARCHAR2(2000);
4019     l_msg_data           VARCHAR2(2000);
4020     l_error_msg_code     VARCHAR2(30);
4021     l_msg_index_out      NUMBER;
4022     l_return_status      VARCHAR2(2000);
4023     l_debug_mode         VARCHAR2(30);
4024 
4025     --End of variables used for debugging
4026 
4027     l_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
4028     l_budget_type_code   pa_budget_types.budget_type_code%Type;
4029     l_ac_budget_type_code pa_budget_types.budget_type_code%TYPE := 'AC'; --Bug 3764635.
4030 
4031 BEGIN
4032 
4033  IF P_PA_DEBUG_MODE = 'Y' THEN
4034     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Is_AC_PT_Attached_After_UPG');
4035  END IF;
4036     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4037     l_debug_mode := NVL(l_debug_mode, 'Y');
4038     IF P_PA_DEBUG_MODE = 'Y' THEN
4039        pa_debug.set_process('Is_AC_PT_Attached_After_UPG: ' || 'PLSQL','LOG',l_debug_mode);
4040     END IF;
4041     x_msg_count := 0;
4042     x_return_status := FND_API.G_RET_STS_SUCCESS;
4043 
4044     -- Check for business rules violations
4045 
4046     pa_debug.g_err_stage:='Validating input parameters';
4047     IF P_PA_DEBUG_MODE = 'Y' THEN
4048        pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4049     END IF;
4050 
4051     -- project_id can't be null
4052 
4053     IF (p_project_id IS NULL)   THEN
4054 
4055         pa_debug.g_err_stage:='Project_id = '||p_project_id;
4056         IF P_PA_DEBUG_MODE = 'Y' THEN
4057            pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,5);
4058         END IF;
4059 
4060         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4061                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
4062 
4063         RAISE Invalid_Arg_Exc;
4064 
4065     END IF;
4066 
4067     pa_debug.g_err_stage:='Parameter validation complete ';
4068     IF P_PA_DEBUG_MODE = 'Y' THEN
4069        pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4070     END IF;
4071 
4072     --Fetch approved plan type id
4073 
4074     BEGIN
4075 
4076         pa_debug.g_err_stage:='Fetching approved cost plan type id from the new model';
4077         IF P_PA_DEBUG_MODE = 'Y' THEN
4078            pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4079         END IF;
4080 
4081         Get_Appr_Cost_Plan_Type_Info(
4082                    p_project_id    => p_project_id
4083                   ,x_plan_type_id  => l_fin_plan_type_id
4084                   ,x_return_status => x_return_status
4085                   ,x_msg_count     => x_msg_count
4086                   ,x_msg_data      => x_msg_data);
4087 
4088 
4089 
4090         IF l_fin_plan_type_id is NULL THEN -- AC plan type doesnot exist in the new model.
4091                                       -- So check in the old model.
4092             BEGIN
4093 
4094                 select bud.budget_type_code
4095                 into l_budget_type_code
4096                 from pa_budget_types bud
4097                 where bud.budget_type_code = l_ac_budget_type_code --Bug 3764635.
4098                 and exists
4099                 (
4100                   select budget_version_id
4101                   from pa_budget_versions
4102                   where project_id = p_project_id         -- project id.
4103                   and budget_type_code = bud.budget_type_code
4104                 );
4105 
4106                 l_fin_plan_type_id := 1;
4107 
4108              EXCEPTION
4109 
4110                 WHEN NO_DATA_FOUND THEN
4111                         l_fin_plan_type_id := NULL;
4112              END;
4113 
4114         END IF;
4115 
4116     END;
4117 
4118     --return the plan type id
4119 
4120     x_plan_type_id := l_fin_plan_type_id ;
4121 
4122     pa_debug.g_err_stage:='Exiting Is_AC_PT_Attached_After_UPG';
4123     IF P_PA_DEBUG_MODE = 'Y' THEN
4124        pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4125        pa_debug.reset_err_stack;
4126     END IF;
4127 EXCEPTION
4128 
4129      WHEN Invalid_Arg_Exc THEN
4130 
4131           l_msg_count := FND_MSG_PUB.count_msg;
4132 
4133           IF l_msg_count = 1 THEN
4134 
4135                PA_INTERFACE_UTILS_PUB.get_messages
4136                      (p_encoded        => FND_API.G_TRUE
4137                       ,p_msg_index      => 1
4138                       ,p_msg_count      => l_msg_count
4139                       ,p_msg_data       => l_msg_data
4140                       ,p_data           => l_data
4141                       ,p_msg_index_out  => l_msg_index_out);
4142 
4143                x_msg_data := l_data;
4144 
4145                x_msg_count := l_msg_count;
4146           ELSE
4147 
4148               x_msg_count := l_msg_count;
4149 
4150           END IF;
4151 
4152            x_return_status := FND_API.G_RET_STS_ERROR;
4153 
4154            pa_debug.g_err_stage:='Invalid Arguments Passed';
4155            IF P_PA_DEBUG_MODE = 'Y' THEN
4156               pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,5);
4157                pa_debug.reset_err_stack;
4158 	   END IF;
4159            RAISE;
4160 
4161      WHEN Others THEN
4162 
4163           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4164           x_msg_count     := 1;
4165           x_msg_data      := SQLERRM;
4166 
4167           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
4168                                   ,p_procedure_name  => 'Is_AC_PT_Attached_After_UPG');
4169 
4170           pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
4171           IF P_PA_DEBUG_MODE = 'Y' THEN
4172              pa_debug.write('Is_AC_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,5);
4173              pa_debug.reset_err_stack;
4174 	  END IF;
4175           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4176 
4177 END Is_AC_PT_Attached_After_UPG;
4178 
4179 
4180 /*=============================================================================
4181 This api is a wrapper over the api Get_Appr_Rev_Plan_Type_Info in order to
4182  consider the effects of upgrade. As the user might go for the partial upgrade
4183  of budget types in a project, it is necessary to check for AR plan type in the
4184  old model.
4185  Return value :
4186    If an Approved revenue plan type is attached to a project then the API returns
4187    a non-negative, non-zero value. else returns NULL.
4188 ==============================================================================*/
4189 
4190 PROCEDURE Is_AR_PT_Attached_After_UPG(
4191           p_project_id     IN   pa_projects_all.project_id%TYPE
4192           ,x_plan_type_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
4193           ,x_return_status OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4194           ,x_msg_count     OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
4195           ,x_msg_data      OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4196 AS
4197     --Start of variables used for debugging
4198 
4199     l_msg_count          NUMBER :=0;
4200     l_data               VARCHAR2(2000);
4201     l_msg_data           VARCHAR2(2000);
4202     l_error_msg_code     VARCHAR2(30);
4203     l_msg_index_out      NUMBER;
4204     l_return_status      VARCHAR2(2000);
4205     l_debug_mode         VARCHAR2(30);
4206 
4207     --End of variables used for debugging
4208 
4209     l_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
4210     l_budget_type_code   pa_budget_types.budget_type_code%TYPE;
4211     l_ar_budget_type_code pa_budget_types.budget_type_code%TYPE := 'AR'; --Bug 3764635.
4212 
4213 BEGIN
4214 
4215  IF P_PA_DEBUG_MODE = 'Y' THEN
4216     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Is_AR_PT_Attached_After_UPG');
4217  END IF;
4218     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4219     l_debug_mode := NVL(l_debug_mode, 'Y');
4220     IF P_PA_DEBUG_MODE = 'Y' THEN
4221        pa_debug.set_process('Is_AR_PT_Attached_After_UPG: ' || 'PLSQL','LOG',l_debug_mode);
4222     END IF;
4223     x_msg_count := 0;
4224     x_return_status := FND_API.G_RET_STS_SUCCESS;
4225 
4226     -- Check for business rules violations
4227 
4228     pa_debug.g_err_stage:='Validating input parameters';
4229     IF P_PA_DEBUG_MODE = 'Y' THEN
4230        pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4231     END IF;
4232 
4233     -- project_id can't be null
4234 
4235     IF (p_project_id IS NULL)   THEN
4236 
4237         pa_debug.g_err_stage:='Project_id = '||p_project_id;
4238         IF P_PA_DEBUG_MODE = 'Y' THEN
4239            pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,5);
4240         END IF;
4241 
4242         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4243                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
4244 
4245         RAISE Invalid_Arg_Exc;
4246 
4247     END IF;
4248 
4249     pa_debug.g_err_stage:='Parameter validation complete ';
4250     IF P_PA_DEBUG_MODE = 'Y' THEN
4251        pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4252     END IF;
4253 
4254     --Fetch approved plan type id
4255 
4256     BEGIN
4257 
4258         pa_debug.g_err_stage:='Fetching approved revenue plan type id from the new model';
4259         IF P_PA_DEBUG_MODE = 'Y' THEN
4260            pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4261         END IF;
4262 
4263         Get_Appr_Rev_Plan_Type_Info(
4264                    p_project_id    => p_project_id
4265                   ,x_plan_type_id  => l_fin_plan_type_id
4266                   ,x_return_status => x_return_status
4267                   ,x_msg_count     => x_msg_count
4268                   ,x_msg_data      => x_msg_data);
4269 
4270 
4271 
4272         IF l_fin_plan_type_id is NULL THEN -- AR plan type doesnot exist in the new model.
4273                                       -- So check in the old model.
4274             BEGIN
4275 
4276                 select bud.budget_type_code
4277                 into l_budget_type_code
4278                 from pa_budget_types bud
4279                 where bud.budget_type_code = l_ar_budget_type_code --Bug 3764635.
4280                 and exists
4281                 (
4282                   select budget_version_id
4283                   from pa_budget_versions
4284                   where project_id = p_project_id         -- project id.
4285                   and budget_type_code = bud.budget_type_code
4286                 );
4287 
4288                 l_fin_plan_type_id := 1;
4289 
4290              EXCEPTION
4291 
4292                 WHEN NO_DATA_FOUND THEN
4293                         l_fin_plan_type_id := NULL;
4294              END;
4295 
4296         END IF;
4297 
4298     END;
4299 
4300     --return the plan type id
4301 
4302     x_plan_type_id := l_fin_plan_type_id ;
4303 
4304     pa_debug.g_err_stage:='Exiting Is_AR_PT_Attached_After_UPG';
4305     IF P_PA_DEBUG_MODE = 'Y' THEN
4306        pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,3);
4307     pa_debug.reset_err_stack;
4308 	END IF;
4309 EXCEPTION
4310 
4311      WHEN Invalid_Arg_Exc THEN
4312 
4313           l_msg_count := FND_MSG_PUB.count_msg;
4314 
4315           IF l_msg_count = 1 THEN
4316 
4317                PA_INTERFACE_UTILS_PUB.get_messages
4318                      (p_encoded        => FND_API.G_TRUE
4319                       ,p_msg_index      => 1
4320                       ,p_msg_count      => l_msg_count
4321                       ,p_msg_data       => l_msg_data
4322                       ,p_data           => l_data
4323                       ,p_msg_index_out  => l_msg_index_out);
4324 
4325                x_msg_data := l_data;
4326 
4327                x_msg_count := l_msg_count;
4328           ELSE
4329 
4330               x_msg_count := l_msg_count;
4331 
4332           END IF;
4333 
4334            x_return_status := FND_API.G_RET_STS_ERROR;
4335 
4336            pa_debug.g_err_stage:='Invalid Arguments Passed';
4337            IF P_PA_DEBUG_MODE = 'Y' THEN
4338               pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,5);
4339 
4340            pa_debug.reset_err_stack;
4341 	END IF;
4342            RAISE;
4343 
4344      WHEN Others THEN
4345 
4346           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4347           x_msg_count     := 1;
4348           x_msg_data      := SQLERRM;
4349 
4350           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
4351                                   ,p_procedure_name  => 'Is_AR_PT_Attached_After_UPG');
4352 
4353           pa_debug.g_err_stage:='Unexpeted Error'||SQLERRM;
4354           IF P_PA_DEBUG_MODE = 'Y' THEN
4355              pa_debug.write('Is_AR_PT_Attached_After_UPG: ' || l_module_name,pa_debug.g_err_stage,5);
4356 
4357           pa_debug.reset_err_stack;
4358 	END IF;
4359           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4360 
4361 END Is_AR_PT_Attached_After_UPG;
4362 
4363 /*=============================================================================
4364  Function Get_budget_version_number
4365  The function returns the max version number available for the given project_id,
4366  fin_plan_type_id and version_type combination
4367 
4368  06-Jul-2004 Raja  bug 3677924
4369                    p_lock_required_flag input value is not being used. Modified
4370                    the code such that lock is acquired only if this parameter
4371                    is passed as 'Y'.
4372 ==============================================================================*/
4373 
4374 PROCEDURE Get_Max_Budget_Version_Number
4375         (p_project_id         IN      pa_budget_versions.project_id%TYPE
4376         ,p_fin_plan_type_id   IN      pa_budget_versions.fin_plan_type_id%TYPE
4377         ,p_version_type       IN      pa_budget_versions.version_type%TYPE
4378         ,p_copy_mode          IN      VARCHAR2
4379         ,p_ci_id              IN      NUMBER
4380         ,p_lock_required_flag IN      VARCHAR2
4381         ,x_version_number     OUT     NOCOPY pa_budget_versions.version_number%TYPE --File.Sql.39 bug 4440895
4382         ,x_return_status      OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4383         ,x_msg_count          OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
4384         ,x_msg_data           OUT     NOCOPY VARCHAR2) IS --File.Sql.39 bug 4440895
4385 
4386 l_msg_count          NUMBER :=0;
4387 l_data               VARCHAR2(2000);
4388 l_msg_data           VARCHAR2(2000);
4389 l_error_msg_code     VARCHAR2(30);
4390 l_msg_index_out      NUMBER;
4391 l_return_status      VARCHAR2(2000);
4392 l_debug_mode         VARCHAR2(30);
4393 
4394 l_version_number        pa_budget_versions.version_number%TYPE;
4395 
4396 BEGIN
4397 
4398     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4399     l_debug_mode := NVL(l_debug_mode, 'Y');
4400   IF P_PA_DEBUG_MODE = 'Y' THEN
4401     pa_debug.set_curr_function( p_function =>'Get_Max_Budget_Version_Number',
4402                                    p_debug_mode => l_debug_mode);
4403   END IF;
4404     x_msg_count := 0;
4405     x_return_status := FND_API.G_RET_STS_SUCCESS;
4406 
4407     -- Check for NOT NULL parameters
4408 
4409     IF (p_project_id       IS NULL) OR
4410        (p_fin_plan_type_id IS NULL) OR
4411        (p_version_type     IS NULL) OR
4412        (p_copy_mode        IS NULL)
4413     THEN
4414 
4415         IF P_PA_DEBUG_MODE = 'Y' THEN
4416             pa_debug.g_err_stage:='Project_id = '||p_project_id;
4417             pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,5);
4418 
4419             pa_debug.g_err_stage:='p_fin_plan_type_id = '||p_fin_plan_type_id;
4420             pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,5);
4421 
4422             pa_debug.g_err_stage:='p_version_type = '||p_version_type;
4423             pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,5);
4424 
4425             pa_debug.g_err_stage:='p_copy_mode = '||p_copy_mode;
4426             pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,5);
4427         END IF;
4428 
4429         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4430                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
4431 
4432         RAISE Invalid_Arg_Exc;
4433 
4434     END IF;
4435 
4436     IF  p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING THEN
4437         IF p_ci_id IS NULL THEN
4438             BEGIN
4439                 SELECT NVL(max(version_number),0)
4440                 INTO   l_version_number
4441                 FROM   pa_budget_versions
4442                 WHERE  project_id = p_project_id
4443                 AND    fin_plan_type_id = p_fin_plan_type_id
4444                 AND    version_type = p_version_type
4445                 AND    budget_status_code IN (PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
4446                                          PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_SUBMITTED)
4447                 AND    ci_id IS NULL;
4448             EXCEPTION
4449                 WHEN OTHERS THEN
4450 
4451                            IF P_PA_DEBUG_MODE = 'Y' THEN
4452                                pa_debug.g_err_stage:='Error while fetching max version number';
4453                                pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,3);
4454 
4455                 	   pa_debug.reset_curr_function;
4456 		END IF;
4457                             RAISE;
4458             END;
4459 
4460             IF l_version_number <> 0 AND nvl(p_lock_required_flag, 'N') = 'Y' THEN  --bug 3677924
4461             BEGIN
4462                 SELECT version_number
4463                 INTO   l_version_number
4464                 FROM   pa_budget_versions
4465                 WHERE  project_id = p_project_id
4466                 AND    fin_plan_type_id = p_fin_plan_type_id
4467                 AND    version_type = p_version_type
4468                 AND    budget_status_code IN (PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
4469                              PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_SUBMITTED)
4470                 AND    ci_id is null
4471                 AND    version_number = l_version_number
4472                 FOR    UPDATE;
4473             EXCEPTION
4474                 WHEN OTHERS THEN
4475 
4476                            IF P_PA_DEBUG_MODE = 'Y' THEN
4477                                pa_debug.g_err_stage:='Error while fetching version number';
4478                                pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,3);
4479 
4480                 	   pa_debug.reset_curr_function;
4481 			END IF;
4482                             RAISE;
4483             END;
4484             END IF;
4485         ELSE
4486             SELECT NVL(max(version_number),0)
4487             INTO   l_version_number
4488             FROM   pa_budget_versions
4489             WHERE  project_id = p_project_id
4490             AND    fin_plan_type_id = p_fin_plan_type_id
4491             AND    version_type = p_version_type
4492             AND    budget_status_code IN (PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
4493                                          PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_SUBMITTED)
4494             AND    ci_id = p_ci_id;
4495 
4496             IF l_version_number <> 0 AND nvl(p_lock_required_flag, 'N') = 'Y' THEN   -- bug 3677924
4497                 SELECT version_number
4498                 INTO   l_version_number
4499                 FROM   pa_budget_versions
4500                 WHERE  project_id = p_project_id
4501                 AND    fin_plan_type_id = p_fin_plan_type_id
4502                 AND    version_type = p_version_type
4503                 AND    budget_status_code IN (PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING,
4504                              PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_SUBMITTED)
4505                 AND    ci_id = p_ci_id
4506                 AND    version_number = l_version_number
4507                 FOR    UPDATE;
4508             END IF;
4509         END IF;
4510     ELSIF  p_copy_mode = PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED THEN
4511 
4512            SELECT NVL(max(version_number),0)
4513            INTO   l_version_number
4514            FROM   pa_budget_versions
4515            WHERE  project_id = p_project_id
4516            AND    fin_plan_type_id = p_fin_plan_type_id
4517            AND    version_type = p_version_type
4518            AND    budget_status_code IN (PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_BASELINED);
4519 
4520     END IF;
4521 
4522     x_version_number:= l_version_number;
4523  IF P_PA_DEBUG_MODE = 'Y' THEN
4524     pa_debug.reset_curr_function;
4525 END IF;
4526 EXCEPTION
4527      WHEN Invalid_Arg_Exc THEN
4528 
4529           l_msg_count := FND_MSG_PUB.count_msg;
4530           IF l_msg_count = 1 THEN
4531                PA_INTERFACE_UTILS_PUB.get_messages
4532                      (p_encoded        => FND_API.G_TRUE
4533                       ,p_msg_index      => 1
4534                       ,p_msg_count      => l_msg_count
4535                       ,p_msg_data       => l_msg_data
4536                       ,p_data           => l_data
4537                       ,p_msg_index_out  => l_msg_index_out);
4538                x_msg_data := l_data;
4539                x_msg_count := l_msg_count;
4540           ELSE
4541               x_msg_count := l_msg_count;
4542           END IF;
4543           x_return_status := FND_API.G_RET_STS_ERROR;
4544           pa_debug.g_err_stage:='Invalid Arguments Passed';
4545           IF P_PA_DEBUG_MODE = 'Y' THEN
4546              pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,5);
4547           pa_debug.reset_curr_function;
4548 	END IF;
4549           RETURN;
4550 
4551      WHEN Others THEN
4552 
4553           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4554           x_msg_count     := 1;
4555           x_msg_data      := SQLERRM;
4556           FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
4557                                   ,p_procedure_name  => 'Get_Budget_Version_Number');
4558           pa_debug.g_err_stage:='Unexpeted Error'||SQLERRM;
4559           IF P_PA_DEBUG_MODE = 'Y' THEN
4560              pa_debug.write('Get_Max_Budget_Version_Number: ' || l_module_name,pa_debug.g_err_stage,5);
4561           pa_debug.reset_curr_function;
4562 	END IF;
4563           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4564 
4565 END Get_Max_Budget_Version_Number;
4566 
4567 FUNCTION get_period_start_date (p_input_date IN pa_periods_all.start_date%TYPE,
4568                                 p_time_phased_code IN pa_proj_fp_options.cost_time_phased_Code%TYPE) RETURN DATE
4569 IS
4570 l_start_date pa_periods_all.start_date%TYPE;
4571 BEGIN
4572 	IF P_PA_DEBUG_MODE = 'Y' THEN
4573         pa_debug.g_err_stage := 'Inside get_period_start_date and input date is '||p_input_date||' time phasing is : '||p_time_phased_code;
4574         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4575 	END IF;
4576         IF p_time_phased_code = PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P THEN
4577             IF p_input_date IS NOT NULL THEN
4578                 SELECT start_date
4579                 INTO   l_start_date
4580                 FROM   pa_periods
4581                 WHERE  p_input_date between start_date and end_date;
4582             END IF;
4583         ELSIF p_time_phased_code = PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G THEN
4584             IF p_input_date IS NOT NULL THEN
4585                 SELECT  g.start_date
4586                 INTO    l_start_date
4587                 FROM    PA_IMPLEMENTATIONS  i,
4588                         GL_PERIOD_STATUSES g
4589                 WHERE   g.set_of_books_id = i.set_of_books_id
4590                   AND   g.application_id = pa_period_process_pkg.application_id
4591                   AND   g.adjustment_period_flag = 'N'
4592                   AND   p_input_date between  g.start_date and g.end_date;
4593             END IF;
4594         END IF;
4595 return l_start_date;
4596 /* Bug 2644537 */
4597 EXCEPTION
4598         WHEN NO_DATA_FOUND THEN
4599                 return NULL;
4600 END;
4601 
4602 FUNCTION get_period_end_date (p_input_date IN pa_periods_all.end_date%TYPE,
4603                               p_time_phased_code IN pa_proj_fp_options.cost_time_phased_Code%TYPE) RETURN DATE
4604 IS
4605 l_end_date pa_periods_all.end_date%TYPE;
4606 BEGIN
4607 IF P_PA_DEBUG_MODE = 'Y' THEN
4608         pa_debug.g_err_stage := 'Inside get_period_end_date and input date is '||p_input_date||' time phasing is : '||p_time_phased_code;
4609         pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
4610 END IF;
4611         IF p_time_phased_code = PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P THEN
4612             IF p_input_date IS NOT NULL THEN
4613                 SELECT end_date
4614                 INTO   l_end_date
4615                 FROM   pa_periods
4616                 WHERE  p_input_date between start_date and end_date;
4617             END IF;
4618 
4619         ELSIF p_time_phased_code = PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G THEN
4620             IF p_input_date IS NOT NULL THEN
4621                 SELECT  g.end_date
4622                 INTO    l_end_date
4623                 FROM    PA_IMPLEMENTATIONS  i,
4624                         GL_PERIOD_STATUSES g
4625                 WHERE   g.set_of_books_id = i.set_of_books_id
4626                   AND   g.application_id = pa_period_process_pkg.application_id
4627                   AND   g.adjustment_period_flag = 'N'
4628                   AND   p_input_date between  g.start_date and g.end_date;
4629             END IF;
4630         END IF;
4631 return l_end_date;
4632 /* Bug 2644537 */
4633 EXCEPTION
4634         WHEN NO_DATA_FOUND THEN
4635                 return NULL;
4636 END;
4637 
4638 /*==============================================================================
4639    This api returns the Current Baselined Version for a given project and
4640    budget_type_code or fin_plan_type combination.
4641 
4642    1)If the plan type is COST_AND_REV_SAME, then it returns 'ALL' version
4643    2)If it is COST_ONLY or COST_AND_REV_SEP then it returns 'COST'  version
4644  ===============================================================================*/
4645 
4646 PROCEDURE GET_COST_BASE_VERSION_INFO
4647    (  p_project_id              IN      pa_budget_versions.project_id%TYPE
4648      ,p_fin_plan_Type_id        IN      pa_budget_versions.fin_plan_type_id%TYPE
4649      ,p_budget_type_code        IN      pa_budget_versions.budget_type_code%TYPE
4650      ,x_budget_version_id       OUT     NOCOPY pa_budget_versions.budget_version_id%TYPE --File.Sql.39 bug 4440895
4651      ,x_return_status           OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4652      ,x_msg_count               OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
4653      ,x_msg_data                OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4654 AS
4655 
4656 l_msg_count                     NUMBER := 0;
4657 l_data                          VARCHAR2(2000);
4658 l_msg_data                      VARCHAR2(2000);
4659 l_error_msg_code                VARCHAR2(30);
4660 l_msg_index_out                 NUMBER;
4661 l_return_status                 VARCHAR2(2000);
4662 l_debug_mode                    VARCHAR2(30);
4663 l_err_code                      NUMBER;
4664 l_err_stage                     VARCHAR2(2000);
4665 l_err_stack                     VARCHAR2(2000);
4666 
4667 l_fin_plan_preference_code      pa_proj_fp_options.fin_plan_preference_code%TYPE;
4668 l_fp_options_id                 pa_proj_fp_options.proj_fp_options_id%TYPE;
4669 
4670 l_version_type                  pa_budget_versions.version_type%TYPE;
4671 
4672 BEGIN
4673 
4674       x_msg_count := 0;
4675       x_return_status := FND_API.G_RET_STS_SUCCESS;
4676  IF P_PA_DEBUG_MODE = 'Y' THEN
4677       pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.GET_COST_BASE_VERSION_INFO');
4678  END IF;
4679       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4680       l_debug_mode := NVL(l_debug_mode, 'Y');
4681       IF P_PA_DEBUG_MODE = 'Y' THEN
4682          pa_debug.set_process('GET_COST_BASE_VERSION_INFO: ' || 'PLSQL','LOG',l_debug_mode);
4683       END IF;
4684 
4685       -- Check for business rules violations
4686 
4687       pa_debug.g_err_stage:= 'Validating input parameters';
4688       IF P_PA_DEBUG_MODE = 'Y' THEN
4689          pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
4690       END IF;
4691 
4692       --Check if plan version id is null
4693 
4694       IF (p_project_id IS NULL) OR
4695          ((p_budget_type_code IS NULL ) AND (p_fin_plan_type_id IS NULL)) OR
4696          ((p_budget_type_code IS NOT NULL ) AND (p_fin_plan_type_id IS NOT NULL))
4697       THEN
4698 
4699                    pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
4700                    IF P_PA_DEBUG_MODE = 'Y' THEN
4701                       pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4702                    END IF;
4703                    pa_debug.g_err_stage:= 'p_budget_type_code = '|| p_budget_type_code;
4704                    IF P_PA_DEBUG_MODE = 'Y' THEN
4705                       pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4706                    END IF;
4707                    pa_debug.g_err_stage:= 'p_fin_plan_type_id = '|| p_fin_plan_type_id;
4708                    IF P_PA_DEBUG_MODE = 'Y' THEN
4709                       pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4710                    END IF;
4711 
4712                    PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4713                                           p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
4714 
4715                    RAISE PA_FP_ELEMENTS_PUB.Invalid_Arg_Exc;
4716 
4717       END IF;
4718 
4719       --If plan type id is passed then give the COST version OR ALL version based upon the fin_plan_type_id
4720 
4721       IF p_fin_plan_type_id IS NOT NULL
4722       THEN
4723 
4724                     -- Fetch fin_plan_preference code of the plan type to determine the version that has to be fetched.
4725 
4726                     BEGIN
4727                             SELECT fin_plan_preference_code
4728                             INTO   l_fin_plan_preference_code
4729                             FROM   pa_proj_fp_options
4730                             WHERE  project_id = p_project_id
4731                             AND    fin_plan_type_id = p_fin_plan_type_id
4732                             AND    fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
4733                     EXCEPTION
4734                             WHEN others THEN
4735                                   pa_debug.g_err_stage:= 'While fetching Preference Code'||SQLERRM;
4736                                   IF P_PA_DEBUG_MODE = 'Y' THEN
4737                                      pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4738                                   END IF;
4739                                   RAISE ;
4740                     END;
4741 
4742                     -- Based on  fin_plan_preference code, fetch all version for cost_and_rev_same plan type and
4743                     -- for other plan types fetch cost version..
4744 
4745                     IF  l_fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME
4746                     THEN
4747                                   l_version_type := PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_ALL;
4748                     ELSE
4749                                   l_version_type := PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_COST;
4750                     END IF;
4751 
4752                     PA_FIN_PLAN_UTILS.Get_Baselined_Version_Info(
4753                                    p_project_id             =>    p_project_id
4754                                   ,p_fin_plan_type_id       =>    p_fin_plan_type_id
4755                                   ,p_version_type           =>    l_version_type
4756                                   ,x_fp_options_id          =>    l_fp_options_id
4757                                   ,x_fin_plan_version_id    =>    x_budget_version_id
4758                                   ,x_return_status          =>    x_return_status
4759                                   ,x_msg_count              =>    x_msg_count
4760                                   ,x_msg_data               =>    x_msg_data );
4761 
4762                    IF    x_budget_version_id IS NULL
4763                    THEN
4764                           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4765                           x_msg_count     := 1;
4766                           x_msg_data      := 'PA_BU_CORE_NO_VERSION_ID';
4767                    END IF;
4768 
4769       ELSE
4770                    --If Budget type code is passed then give the appopriate  baselined version
4771 
4772                    pa_budget_utils.get_baselined_version_id (
4773                                  x_project_id            =>   p_project_id
4774                                 ,x_budget_type_code      =>   p_budget_type_code
4775                                 ,x_budget_version_id     =>   x_budget_version_id
4776                                 ,x_err_code              =>   l_err_code
4777                                 ,x_err_stage             =>   l_err_stage
4778                                 ,x_err_stack             =>   l_err_stack );
4779 
4780                    --Initialise the out parameters
4781 
4782                    IF    l_err_code <> 0
4783                    THEN
4784                           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4785                           x_msg_count     := 1;
4786                           x_msg_data      := l_err_stage;
4787                    END IF;
4788        END IF;
4789 
4790       pa_debug.g_err_stage:= 'Exiting GET_COST_BASE_VERSION_INFO';
4791       IF P_PA_DEBUG_MODE = 'Y' THEN
4792          pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
4793 
4794       pa_debug.reset_err_stack;
4795 	END IF;
4796   EXCEPTION
4797 
4798      WHEN PA_FP_ELEMENTS_PUB.Invalid_Arg_Exc THEN
4799 
4800            l_msg_count := FND_MSG_PUB.count_msg;
4801            IF l_msg_count = 1 THEN
4802                 PA_INTERFACE_UTILS_PUB.get_messages
4803                      (p_encoded        => FND_API.G_TRUE
4804                       ,p_msg_index      => 1
4805                       ,p_msg_count      => l_msg_count
4806                       ,p_msg_data       => l_msg_data
4807                       ,p_data           => l_data
4808                       ,p_msg_index_out  => l_msg_index_out);
4809                 x_msg_data := l_data;
4810                 x_msg_count := l_msg_count;
4811            ELSE
4812                 x_msg_count := l_msg_count;
4813            END IF;
4814 
4815            pa_debug.g_err_stage:= 'Invalid Arguments Passed';
4816            IF P_PA_DEBUG_MODE = 'Y' THEN
4817               pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4818            END IF;
4819 
4820            x_return_status := FND_API.G_RET_STS_ERROR;
4821  IF P_PA_DEBUG_MODE = 'Y' THEN
4822            pa_debug.reset_err_stack;
4823  END IF;
4824    WHEN others THEN
4825 
4826           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4827           x_msg_count     := 1;
4828           x_msg_data      := SQLERRM;
4829           FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
4830                                   ,p_procedure_name  => 'GET_COST_BASE_VERSION_INFO');
4831           pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
4832           IF P_PA_DEBUG_MODE = 'Y' THEN
4833              pa_debug.write('GET_COST_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4834           pa_debug.reset_err_stack;
4835 	END IF;
4836 END GET_COST_BASE_VERSION_INFO;
4837 
4838 /*==============================================================================
4839    This api returns the Current Baselined Version for a given project and
4840    budget_type_code or fin_plan_type combination.
4841 
4842    1)If the plan type is COST_AND_REV_SAME, then it returns 'ALL' version
4843    2)If it is REVENUE_ONLY or COST_AND_REV_SEP then it returns 'REVENUE'  version
4844  ===============================================================================*/
4845 
4846 PROCEDURE GET_REV_BASE_VERSION_INFO
4847    (  p_project_id              IN      pa_budget_versions.project_id%TYPE
4848      ,p_fin_plan_Type_id        IN      pa_budget_versions.fin_plan_type_id%TYPE
4849      ,p_budget_type_code        IN      pa_budget_versions.budget_type_code%TYPE
4850      ,x_budget_version_id       OUT     NOCOPY pa_budget_versions.budget_version_id%TYPE --File.Sql.39 bug 4440895
4851      ,x_return_status           OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4852      ,x_msg_count               OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
4853      ,x_msg_data                OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4854 AS
4855 
4856 l_msg_count                     NUMBER := 0;
4857 l_data                          VARCHAR2(2000);
4858 l_msg_data                      VARCHAR2(2000);
4859 l_error_msg_code                VARCHAR2(30);
4860 l_msg_index_out                 NUMBER;
4861 l_return_status                 VARCHAR2(2000);
4862 l_debug_mode                    VARCHAR2(30);
4863 l_err_code                      NUMBER;
4864 l_err_stage                     VARCHAR2(2000);
4865 l_err_stack                     VARCHAR2(2000);
4866 
4867 l_fin_plan_preference_code      pa_proj_fp_options.fin_plan_preference_code%TYPE;
4868 l_fp_options_id                 pa_proj_fp_options.proj_fp_options_id%TYPE;
4869 
4870 l_version_type                  pa_budget_versions.version_type%TYPE;
4871 
4872 BEGIN
4873 
4874       x_msg_count := 0;
4875       x_return_status := FND_API.G_RET_STS_SUCCESS;
4876  IF P_PA_DEBUG_MODE = 'Y' THEN
4877       pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.GET_REV_BASE_VERSION_INFO');
4878  END IF;
4879       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4880       l_debug_mode := NVL(l_debug_mode, 'Y');
4881       IF P_PA_DEBUG_MODE = 'Y' THEN
4882          pa_debug.set_process('GET_REV_BASE_VERSION_INFO: ' || 'PLSQL','LOG',l_debug_mode);
4883       END IF;
4884 
4885       -- Check for business rules violations
4886 
4887       pa_debug.g_err_stage:= 'Validating input parameters';
4888       IF P_PA_DEBUG_MODE = 'Y' THEN
4889          pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
4890       END IF;
4891 
4892       --Check if plan version id is null
4893 
4894       IF (p_project_id IS NULL) OR
4895          ((p_budget_type_code IS NULL ) AND (p_fin_plan_type_id IS NULL)) OR
4896          ((p_budget_type_code IS NOT NULL ) AND (p_fin_plan_type_id IS NOT NULL))
4897       THEN
4898 
4899                    pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
4900                    IF P_PA_DEBUG_MODE = 'Y' THEN
4901                       pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4902                    END IF;
4903                    pa_debug.g_err_stage:= 'p_budget_type_code = '|| p_budget_type_code;
4904                    IF P_PA_DEBUG_MODE = 'Y' THEN
4905                       pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4906                    END IF;
4907                    pa_debug.g_err_stage:= 'p_fin_plan_type_id = '|| p_fin_plan_type_id;
4908                    IF P_PA_DEBUG_MODE = 'Y' THEN
4909                       pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4910                    END IF;
4911 
4912                    PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
4913                                           p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
4914 
4915                    RAISE PA_FP_ELEMENTS_PUB.Invalid_Arg_Exc;
4916 
4917       END IF;
4918 
4919       --If plan type id is passed then give the COST version OR ALL version based upon the fin_plan_type_id
4920 
4921       IF p_fin_plan_type_id IS NOT NULL
4922       THEN
4923 
4924                     -- Fetch fin_plan_preference code of the plan type to determine the version that has to be fetched.
4925 
4926                     BEGIN
4927                             SELECT fin_plan_preference_code
4928                             INTO   l_fin_plan_preference_code
4929                             FROM   pa_proj_fp_options
4930                             WHERE  project_id = p_project_id
4931                             AND    fin_plan_type_id = p_fin_plan_type_id
4932                             AND    fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
4933                     EXCEPTION
4934                             WHEN others THEN
4935 
4936                                   pa_debug.g_err_stage:= 'While fetching Preference Code'||SQLERRM;
4937                                   IF P_PA_DEBUG_MODE = 'Y' THEN
4938                                      pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
4939                                   END IF;
4940                                   RAISE;
4941                     END;
4942 
4943                     -- Based on  fin_plan_preference code, fetch all version for cost_and_rev_same plan type and
4944                     -- for other plan types fetch cost version..
4945 
4946                     IF  l_fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME
4947                     THEN
4948                                   l_version_type := PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_ALL;
4949                     ELSE
4950                                   l_version_type := PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_REVENUE;
4951                     END IF;
4952 
4953                     PA_FIN_PLAN_UTILS.Get_Baselined_Version_Info(
4954                                    p_project_id             =>    p_project_id
4955                                   ,p_fin_plan_type_id       =>    p_fin_plan_type_id
4956                                   ,p_version_type           =>    l_version_type
4957                                   ,x_fp_options_id          =>    l_fp_options_id
4958                                   ,x_fin_plan_version_id    =>    x_budget_version_id
4959                                   ,x_return_status          =>    x_return_status
4960                                   ,x_msg_count              =>    x_msg_count
4961                                   ,x_msg_data               =>    x_msg_data );
4962 
4963                     IF    x_budget_version_id IS NULL
4964                     THEN
4965                            x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4966                            x_msg_count     := 1;
4967                            x_msg_data      := 'PA_BU_CORE_NO_VERSION_ID';
4968                     END IF;
4969 
4970 
4971       ELSE
4972                    --If Budget type code is passed then give the appopriate  baselined version
4973 
4974                    pa_budget_utils.get_baselined_version_id (
4975                                  x_project_id            =>   p_project_id
4976                                 ,x_budget_type_code      =>   p_budget_type_code
4977                                 ,x_budget_version_id     =>   x_budget_version_id
4978                                 ,x_err_code              =>   l_err_code
4979                                 ,x_err_stage             =>   l_err_stage
4980                                 ,x_err_stack             =>   l_err_stack );
4981 
4982                    --Initialise the out parameters
4983 
4984                    IF    l_err_code <> 0
4985                    THEN
4986                           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4987                           x_msg_count     := 1;
4988                           x_msg_data      := l_err_stage;
4989                    END IF;
4990       END IF;
4991 
4992 
4993       pa_debug.g_err_stage:= 'Exiting GET_REV_BASE_VERSION_INFO';
4994       IF P_PA_DEBUG_MODE = 'Y' THEN
4995          pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
4996          pa_debug.reset_err_stack;
4997 	END IF;
4998   EXCEPTION
4999 
5000      WHEN PA_FP_ELEMENTS_PUB.Invalid_Arg_Exc THEN
5001 
5002            l_msg_count := FND_MSG_PUB.count_msg;
5003            IF l_msg_count = 1 THEN
5004                 PA_INTERFACE_UTILS_PUB.get_messages
5005                      (p_encoded        => FND_API.G_TRUE
5006                       ,p_msg_index      => 1
5007                       ,p_msg_count      => l_msg_count
5008                       ,p_msg_data       => l_msg_data
5009                       ,p_data           => l_data
5010                       ,p_msg_index_out  => l_msg_index_out);
5011                 x_msg_data := l_data;
5012                 x_msg_count := l_msg_count;
5013            ELSE
5014                 x_msg_count := l_msg_count;
5015            END IF;
5016 
5017            pa_debug.g_err_stage:= 'Invalid Arguments Passed';
5018            IF P_PA_DEBUG_MODE = 'Y' THEN
5019               pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5020            END IF;
5021 
5022            x_return_status := FND_API.G_RET_STS_ERROR;
5023  IF P_PA_DEBUG_MODE = 'Y' THEN
5024            pa_debug.reset_err_stack;
5025  END IF;
5026    WHEN others THEN
5027 
5028           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5029           x_msg_count     := 1;
5030           x_msg_data      := SQLERRM;
5031           FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
5032                                   ,p_procedure_name  => 'GET_REV_BASE_VERSION_INFO');
5033           pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
5034           IF P_PA_DEBUG_MODE = 'Y' THEN
5035              pa_debug.write('GET_REV_BASE_VERSION_INFO: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5036           pa_debug.reset_err_stack;
5037 	END IF;
5038 END GET_REV_BASE_VERSION_INFO;
5039 
5040 /*==============================================================================
5041  Following two function just calls the usual pa_debug.acquire/release_user_lock
5042  but in AUTONOMOUS mode .This two functions are added as part of fix for #2622476.
5043  ==============================================================================*/
5044 
5045 FUNCTION ACQUIRE_USER_LOCK
5046    ( X_LOCK_NAME     IN      VARCHAR2 )
5047 RETURN NUMBER
5048 IS
5049   PRAGMA AUTONOMOUS_TRANSACTION;
5050 BEGIN
5051 
5052    RETURN pa_debug.Acquire_User_Lock(X_LOCK_NAME) ;
5053 
5054 END ACQUIRE_USER_LOCK ;
5055 
5056 FUNCTION RELEASE_USER_LOCK
5057    ( X_LOCK_NAME     IN      VARCHAR2 )
5058 RETURN NUMBER
5059 IS
5060   PRAGMA AUTONOMOUS_TRANSACTION;
5061 BEGIN
5062 
5063   RETURN pa_debug.Release_User_Lock(X_LOCK_NAME) ;
5064 
5065 END RELEASE_USER_LOCK ;
5066 
5067 /*==================================================================
5068    This api converts the estimated amounts of a control item version
5069    entered in project currency to project functional currency.
5070  ==================================================================*/
5071 
5072 PROCEDURE get_converted_amounts
5073    (  p_budget_version_id       IN   pa_budget_versions.budget_version_id%TYPE
5074      ,p_txn_raw_cost            IN   pa_budget_versions.est_project_raw_cost%TYPE
5075      ,p_txn_burdened_cost       IN   pa_budget_versions.est_project_burdened_cost%TYPE
5076      ,p_txn_revenue             IN   pa_budget_versions.est_project_revenue%TYPE
5077      ,p_txn_currency_Code       IN   pa_projects_all.project_currency_code%TYPE
5078      ,p_project_currency_code   IN   pa_projects_all.project_currency_code%TYPE
5079      ,p_projfunc_currency_code  IN   pa_projects_all.projfunc_currency_code%TYPE
5080      ,x_project_raw_cost        OUT  NOCOPY pa_budget_versions.est_projfunc_raw_cost%TYPE --File.Sql.39 bug 4440895
5081      ,x_project_burdened_cost   OUT  NOCOPY pa_budget_versions.est_projfunc_burdened_cost%TYPE --File.Sql.39 bug 4440895
5082      ,x_project_revenue         OUT  NOCOPY pa_budget_versions.est_projfunc_revenue%TYPE --File.Sql.39 bug 4440895
5083      ,x_projfunc_raw_cost       OUT  NOCOPY pa_budget_versions.est_projfunc_raw_cost%TYPE --File.Sql.39 bug 4440895
5084      ,x_projfunc_burdened_cost  OUT  NOCOPY pa_budget_versions.est_projfunc_burdened_cost%TYPE --File.Sql.39 bug 4440895
5085      ,x_projfunc_revenue        OUT  NOCOPY pa_budget_versions.est_projfunc_revenue%TYPE --File.Sql.39 bug 4440895
5086      ,x_return_status           OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5087      ,x_msg_count               OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
5088      ,x_msg_data                OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
5089 AS
5090 
5091 l_msg_count                     NUMBER := 0;
5092 l_data                          VARCHAR2(2000);
5093 l_msg_data                      VARCHAR2(2000);
5094 l_error_msg_code                VARCHAR2(30);
5095 l_msg_index_out                 NUMBER;
5096 l_return_status                 VARCHAR2(2000);
5097 l_debug_mode                    VARCHAR2(30);
5098 
5099 BEGIN
5100 
5101       x_msg_count := 0;
5102       x_return_status := FND_API.G_RET_STS_SUCCESS;
5103  IF P_PA_DEBUG_MODE = 'Y' THEN
5104       pa_debug.set_err_stack('PA_FIN_PLAN_UITLS.get_converted_amounts');
5105  END IF;
5106       fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
5107       l_debug_mode := NVL(l_debug_mode, 'Y');
5108       IF P_PA_DEBUG_MODE = 'Y' THEN
5109          pa_debug.set_process('get_converted_amounts: ' || 'PLSQL','LOG',l_debug_mode);
5110       END IF;
5111 
5112       -- Check for business rules violations
5113 
5114       pa_debug.g_err_stage:= 'Validating input parameters';
5115       IF P_PA_DEBUG_MODE = 'Y' THEN
5116          pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,
5117                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
5118       END IF;
5119 
5120       --Check if plan version id is null
5121 
5122       IF   (p_budget_version_id      IS NULL) OR
5123            (p_txn_currency_Code      IS NULL) OR
5124            (p_project_currency_Code  IS NULL) OR
5125            (p_projfunc_currency_code IS NULL)
5126       THEN
5127 
5128               pa_debug.g_err_stage:= 'p_budget_version_id = '|| p_budget_version_id;
5129               IF P_PA_DEBUG_MODE = 'Y' THEN
5130                  pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5131               END IF;
5132               pa_debug.g_err_stage:= 'p_txn_currency_Code = '|| p_txn_currency_Code;
5133               IF P_PA_DEBUG_MODE = 'Y' THEN
5134                  pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5135               END IF;
5136               pa_debug.g_err_stage:= 'p_project_currency_Code = '|| p_project_currency_Code;
5137               IF P_PA_DEBUG_MODE = 'Y' THEN
5138                  pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5139               END IF;
5140               pa_debug.g_err_stage:= 'p_projfunc_currency_code = '|| p_projfunc_currency_code;
5141               IF P_PA_DEBUG_MODE = 'Y' THEN
5142                  pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5143               END IF;
5144 
5145               PA_UTILS.ADD_MESSAGE
5146                      (p_app_short_name => 'PA',
5147                       p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
5148 
5149               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5150       END IF;
5151 
5152       DELETE FROM pa_fp_rollup_tmp;
5153       INSERT INTO pa_fp_rollup_tmp(
5154              resource_assignment_id,
5155              start_date,
5156              end_date,
5157              txn_currency_code,
5158              project_currency_code,
5159              projfunc_currency_code,
5160              txn_raw_cost,
5161              txn_burdened_cost,
5162              txn_revenue             )
5163       VALUES(
5164               -1,
5165               sysdate,
5166               sysdate,
5167               p_project_currency_Code,
5168               p_project_currency_Code,
5169               p_projfunc_currency_Code,
5170               p_txn_raw_cost,
5171               p_txn_burdened_cost,
5172               p_txn_revenue      );
5173 
5174       PA_FP_MULTI_CURRENCY_PKG.convert_txn_currency
5175         ( p_budget_version_id       =>      p_budget_version_id
5176          ,p_entire_version          =>      'N'
5177          ,x_return_status           =>      x_return_status
5178          ,x_msg_count               =>      x_msg_count
5179          ,x_msg_data                =>      x_msg_data );
5180 
5181       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5182          RETURN;
5183       END IF;
5184 
5185       SELECT PROJFUNC_RAW_COST,
5186              PROJFUNC_BURDENED_COST,
5187              PROJFUNC_REVENUE,
5188              PROJECT_RAW_COST,
5189              PROJECT_BURDENED_COST,
5190              PROJECT_REVENUE
5191       INTO
5192          x_projfunc_raw_cost,
5193          x_projfunc_burdened_cost,
5194          x_projfunc_revenue,
5195          x_project_raw_cost,
5196          x_project_burdened_cost,
5197          x_project_revenue
5198       FROM Pa_Fp_Rollup_Tmp
5199       WHERE RESOURCE_ASSIGNMENT_ID = -1;
5200 
5201       pa_debug.g_err_stage:= 'Exiting get_converted_amounts';
5202       IF P_PA_DEBUG_MODE = 'Y' THEN
5203          pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
5204          pa_debug.reset_err_stack;
5205       END IF;
5206   EXCEPTION
5207 
5208      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
5209 
5210            x_return_status := FND_API.G_RET_STS_ERROR;
5211            l_msg_count := FND_MSG_PUB.count_msg;
5212            IF l_msg_count = 1 THEN
5213                 PA_INTERFACE_UTILS_PUB.get_messages
5214                       (p_encoded        => FND_API.G_TRUE
5215                       ,p_msg_index      => 1
5216                       ,p_msg_count      => l_msg_count
5217                       ,p_msg_data       => l_msg_data
5218                       ,p_data           => l_data
5219                       ,p_msg_index_out  => l_msg_index_out);
5220                 x_msg_data := l_data;
5221                 x_msg_count := l_msg_count;
5222            ELSE
5223                 x_msg_count := l_msg_count;
5224            END IF;
5225 
5226            pa_debug.g_err_stage:= 'Invalid Arguments Passed';
5227            IF P_PA_DEBUG_MODE = 'Y' THEN
5228               pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5229            pa_debug.reset_err_stack;
5230 	END IF;
5231            RAISE;
5232 
5233    WHEN others THEN
5234 
5235           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5236           x_msg_count     := 1;
5237           x_msg_data      := SQLERRM;
5238           FND_MSG_PUB.add_exc_msg
5239                           ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
5240                            ,p_procedure_name  => 'conv_est_amounts_of_ci_version'
5241                            ,p_error_text      => sqlerrm);
5242           pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
5243           IF P_PA_DEBUG_MODE = 'Y' THEN
5244              pa_debug.write('get_converted_amounts: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5245           pa_debug.reset_err_stack;
5246 	END IF;
5247           RAISE;
5248 
5249 END get_converted_amounts;
5250 
5251 ------------------------------------------------------------------------------
5252 
5253 --  =================================================
5254 
5255 --Name:                 check_proj_fin_plan_exists
5256 --Type:                 Function
5257 --
5258 --Description:  This function is called primarily from Billing and Projects Maintenance packages
5259 --
5260 --                  If both the p_plan_type_id and the p_version_type IN-parameters are passed with
5261 --                  NON-null values, then the FP logic must check for the plan_type and appropriate
5262 --                  Approved budget flags based on x_plan_type_code.
5263 --
5264 --              Valid Values for x_plan_type_code are:
5265 --              AC -- Approved Cost Budget   AR -- Approved Revenue Budget
5266 --
5267 --              For x_budget_status_code = B(aseline)
5268 --              1.  As per design doc, if 'AC' or 'AR' plan types passed as x_plan_type_code
5269 --                  AND X_FIN_PLAN_TYPE_ID IS NOT NULL,
5270 --                    THEN
5271 --                       Use the approved_cost/rev_plan_type_flags to determine if ANY AC/AR baselined
5272 --                       budgets have been created for FP model.
5273 --                       IF not then if version_type is passed as 'ALL' return success as both Cost and
5274 --                          revenue versions will be baselined together and as it may be the first time
5275 --                          such a case is being created, there would be no baseline versions existing.
5276 
5277   function check_proj_fin_plan_exists (x_project_id             IN number,
5278                                        x_budget_version_id      IN number,
5279                                        x_budget_status_code     IN varchar2,
5280                                        x_plan_type_code         IN varchar2,
5281                                        x_fin_plan_type_id       IN NUMBER,
5282                                        x_version_type           IN VARCHAR2
5283                                     )
5284   return number
5285 
5286   is
5287 
5288      dummy number := 0;
5289 
5290 BEGIN
5291 
5292 
5293 
5294      -- Check for Valid Budget_Status_Code ---------------------------
5295 
5296      IF nvl(x_budget_status_code,'X') <> 'B'
5297         THEN
5298           dummy := 0;
5299           RETURN dummy;
5300      END IF;
5301 
5302  -- FP Model --------------
5303 
5304 
5305          IF (  NVL(x_plan_type_code,'X') IN ('AC','AR')  ) THEN
5306                IF (x_plan_type_code = 'AC') THEN
5307 
5308                      BEGIN
5309                         select 1
5310                         into   dummy
5311                         from   dual
5312                         where  exists
5313                         (select 1
5314                          from   pa_budget_versions bv
5315                          where  bv.project_id = x_project_id
5316                          --and    bv.fin_plan_type_id = x_fin_plan_type_id
5317                          and    bv.approved_cost_plan_type_flag = 'Y'
5318                          and    bV.current_flag = 'Y');
5319 
5320                       EXCEPTION WHEN NO_DATA_FOUND THEN
5321                         IF x_version_type = 'ALL' THEN
5322                            dummy := 1;
5323                         ELSE
5324                            dummy := 0;
5325                         END IF;
5326                      END;
5327 
5328                ELSE -- Must be 'AR'
5329 
5330                      BEGIN
5331                         select 1
5332                         into   dummy
5333                         from   dual
5334                         where  exists
5335                         (select 1
5336                          from   pa_budget_versions bv
5337                          where  bv.project_id = x_project_id
5338                          --and    bv.fin_plan_type_id = x_fin_plan_type_id
5339                          and    bv.approved_rev_plan_type_flag = 'Y'
5340                          and    bV.current_flag = 'Y');
5341 
5342                      EXCEPTION WHEN NO_DATA_FOUND THEN
5343                         IF x_version_type = 'ALL' THEN
5344                            dummy := 1;
5345                         ELSE
5346                            dummy := 0;
5347                         END IF;
5348                      END;
5349                END IF;
5350          END IF; -- (NVL(x_plan_type_code,'X') IN ('AC','AR'))
5351 
5352     RETURN dummy;
5353 Exception when others THEN
5354     return sqlcode;
5355 END check_proj_fin_plan_exists;
5356 
5357 ------------------------------------------------------------------------------
5358 
5359 --  =================================================
5360 
5361 --Name:                 check_task_fin_plan_exists
5362 --Type:                 Function
5363 --
5364 --Description:  This function is called primarily from Billing and Projects Maintenance packages
5365 --
5366 --                  If both the p_plan_type_id and the p_version_type IN-parameters are passed with
5367 --                  NON-null values, then the FP logic must check for the plan_type and appropriate
5368 --                  Approved budget flags based on x_plan_type_code.
5369 --
5370 --              Valid Values for x_plan_type_code are:
5371 --              AC -- Approved Cost Budget   AR -- Approved Revenue Budget
5372 --
5373 --              For x_budget_status_code = B(aseline)
5374 --              1.  As per design doc, if 'AC' or 'AR' plan types passed as x_plan_type_code
5375 --                  AND X_FIN_PLAN_TYPE_ID IS NOT NULL,
5376 --                    THEN
5377 --                       Use the approved_cost/rev_plan_type_flags to determine if ANY AC/AR baselined
5378 --                       budgets have been created for FP model.
5379 --                       IF not then if version_type is passed as 'ALL' return success as both Cost and
5380 --                          revenue versions will be baselined together and as it may be the first time
5381 --                          such a case is being created, there would be no baseline versions existing.
5382 
5383   function check_task_fin_plan_exists (x_task_id                IN number,
5384                                        x_budget_version_id      IN number,
5385                                        x_budget_status_code     IN varchar2,
5386                                        x_plan_type_code         IN varchar2,
5387                                        x_fin_plan_type_id       IN NUMBER,
5388                                        x_version_type           IN VARCHAR2 )
5389   return number
5390 
5391   is
5392 
5393      dummy number := 0;
5394 
5395 BEGIN
5396 
5397 
5398 
5399      -- Check for Valid Budget_Status_Code ---------------------------
5400 
5401      IF nvl(x_budget_status_code,'X') <> 'B'
5402         THEN
5403           dummy := 0;
5404           RETURN dummy;
5405      END IF;
5406 
5407  -- FP Model --------------
5408 
5409 
5410          IF (  NVL(x_plan_type_code,'X') IN ('AC','AR')  ) THEN
5411                IF (x_plan_type_code = 'AC') THEN
5412 
5413                      BEGIN
5414 
5415                         select 1
5416                           into   dummy
5417                           from   dual
5418                           where  exists
5419                           (select 1
5420                            from   pa_budget_versions bv
5421                                   , pa_tasks t
5422                                   , pa_resource_assignments a
5423                            where  a.budget_version_id = bv.budget_version_id
5424                            and    a.task_id = t.task_id
5425                            and    a.resource_assignment_type = 'USER_ENTERED'
5426                            and    t.top_task_id = x_task_id
5427                            --and    bv.fin_plan_type_id = x_fin_plan_type_id
5428                            and    bv.approved_cost_plan_type_flag = 'Y'
5429                            and    bV.current_flag = 'Y');
5430 
5431                       EXCEPTION WHEN NO_DATA_FOUND THEN
5432                         IF x_version_type = 'ALL' THEN
5433                            dummy := 1;
5434                         ELSE
5435                            dummy := 0;
5436                         END IF;
5437                      END;
5438 
5439                ELSE -- Must be 'AR'
5440 
5441                      BEGIN
5442                         select 1
5443                           into   dummy
5444                           from   dual
5445                           where  exists
5446                           (select 1
5447                            from   pa_budget_versions bv
5448                                   , pa_tasks t
5449                                   , pa_resource_assignments a
5450                            where  a.budget_version_id = bv.budget_version_id
5451                            and    a.task_id = t.task_id
5452                            and    a.resource_assignment_type = 'USER_ENTERED'
5453                            and    t.top_task_id = x_task_id
5454                            --and    bv.fin_plan_type_id = x_fin_plan_type_id
5455                            and    bv.approved_rev_plan_type_flag = 'Y'
5456                            and    bV.current_flag = 'Y');
5457 
5458                      EXCEPTION WHEN NO_DATA_FOUND THEN
5459                         IF x_version_type = 'ALL' THEN
5460                            dummy := 1;
5461                         ELSE
5462                            dummy := 0;
5463                         END IF;
5464                      END;
5465                END IF;
5466          END IF; -- (NVL(x_plan_type_code,'X') IN ('AC','AR'))
5467 
5468     RETURN dummy;
5469 Exception when others THEN
5470     return sqlcode;
5471 END check_task_fin_plan_exists;
5472 /*==================================================================
5473    This api returns the start and end dates for the given period name
5474    along with the plan period type, ie., PA/GL period.
5475    If the period is not found the api raises error.
5476  ==================================================================*/
5477 
5478 PROCEDURE Get_Period_Details
5479    (  p_period_name           IN   pa_periods.period_name%TYPE
5480 /*  Changes for FPM. Tracking Bug - 3354518
5481     Modifying the datatype of parameter p_plan_period_type below to varchar2 */
5482 /*   ,p_plan_period_type      IN   pa_proj_period_profiles.plan_period_type%TYPE */
5483      ,p_plan_period_type      IN   VARCHAR2
5484      ,x_start_date            OUT  NOCOPY DATE --File.Sql.39 bug 4440895
5485      ,x_end_date              OUT  NOCOPY DATE --File.Sql.39 bug 4440895
5486      ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5487      ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
5488      ,x_msg_data              OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
5489 AS
5490 
5491 l_msg_count                     NUMBER := 0;
5492 l_data                          VARCHAR2(2000);
5493 l_msg_data                      VARCHAR2(2000);
5494 l_msg_index_out                 NUMBER;
5495 l_return_status                 VARCHAR2(2000);
5496 
5497 BEGIN
5498 
5499       x_msg_count := 0;
5500       x_return_status := FND_API.G_RET_STS_SUCCESS;
5501  IF P_PA_DEBUG_MODE = 'Y' THEN
5502       pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Period_Details');
5503       pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
5504  END IF;
5505       -- Check for NOT NULL parameters
5506       IF (p_period_name IS NULL) OR
5507          (p_plan_period_type IS NULL)
5508       THEN
5509           IF p_pa_debug_mode = 'Y' THEN
5510                 pa_debug.g_err_stage:= 'p_period_name = '|| p_period_name;
5511                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5512                 pa_debug.g_err_stage:= 'p_plan_period_type = '|| p_plan_period_type;
5513                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5514           END IF;
5515           PA_UTILS.ADD_MESSAGE
5516                  (p_app_short_name => 'PA',
5517                   p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
5518 
5519           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5520       END IF;
5521 
5522       IF     p_plan_period_type = 'PA' THEN
5523 
5524              BEGIN
5525                    SELECT  start_date
5526                           ,end_date
5527                    INTO    x_start_date
5528                           ,x_end_date
5529                    FROM   pa_periods
5530                    WHERE  period_name = p_period_name;
5531              EXCEPTION
5532                  WHEN OTHERS THEN
5533                         IF p_pa_debug_mode = 'Y' THEN
5534                                 pa_debug.g_err_stage:= 'Error while fetching the details of pa_period'||SQLERRM;
5535                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5536                         END IF;
5537                         RAISE;
5538              END;
5539       ELSIF  p_plan_period_type = 'GL' THEN
5540 
5541              BEGIN
5542                    SELECT  start_date
5543                           ,end_date
5544                    INTO    x_start_date
5545                           ,x_end_date
5546                    FROM    gl_period_statuses g
5547                           ,pa_implementations i
5548                    WHERE  g.application_id = pa_period_process_pkg.application_id
5549                    AND    g.set_of_books_id = i.set_of_books_id
5550                    AND    g.adjustment_period_flag = 'N'
5551                    AND    g.period_name = p_period_name;
5552 
5553              EXCEPTION
5554                  WHEN OTHERS THEN
5555                         IF p_pa_debug_mode = 'Y' THEN
5556                                 pa_debug.g_err_stage:= 'Error while fetching the details of gl_period'||SQLERRM;
5557                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5558                         END IF;
5559                         RAISE;
5560              END;
5561       END IF;
5562 
5563       IF p_pa_debug_mode = 'Y' THEN
5564               pa_debug.g_err_stage:= 'Exiting Get_Period_Details';
5565               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
5566 	      pa_debug.reset_err_stack;
5567 	END IF;
5568  EXCEPTION
5569 
5570      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
5571 
5572            x_return_status := FND_API.G_RET_STS_ERROR;
5573            l_msg_count := FND_MSG_PUB.count_msg;
5574            IF l_msg_count = 1 THEN
5575                 PA_INTERFACE_UTILS_PUB.get_messages
5576                       (p_encoded        => FND_API.G_TRUE
5577                       ,p_msg_index      => 1
5578                       ,p_msg_count      => l_msg_count
5579                       ,p_msg_data       => l_msg_data
5580                       ,p_data           => l_data
5581                       ,p_msg_index_out  => l_msg_index_out);
5582                 x_msg_data := l_data;
5583                 x_msg_count := l_msg_count;
5584            ELSE
5585                 x_msg_count := l_msg_count;
5586            END IF;
5587 	 IF P_PA_DEBUG_MODE = 'Y' THEN
5588            pa_debug.reset_err_stack;
5589 	END IF;
5590            RAISE;
5591 
5592    WHEN others THEN
5593 
5594           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5595           x_msg_count     := 1;
5596           x_msg_data      := SQLERRM;
5597           FND_MSG_PUB.add_exc_msg
5598                           ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
5599                            ,p_procedure_name  => 'Get_Period_Details'
5600                            ,p_error_text      => SQLERRM);
5601           IF p_pa_debug_mode = 'Y' THEN
5602                   pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
5603                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5604           pa_debug.reset_err_stack;
5605 	END IF;
5606           RAISE;
5607 END Get_Period_Details;
5608 
5609 /*==================================================================
5610    This api retruns the period that is p_number_of_periods away from
5611    the p_period_name.
5612 
5613    i) If the number_of_periods is positive it returns
5614       the period retuned has a start date greater than start date of
5615       the input period.
5616    ii)If the number_of_periods is negative the period returned has a
5617       start date less than the inpt period start date
5618  ==================================================================*/
5619 
5620 /*  Changes for FPM. Tracking Bug - 3354518
5621     Modifying the datatype of parameter p_plan_period_type below to varchar2
5622     and x_shifted_period_start_date and x_shifted_period_end_date as date*/
5623 
5624 PROCEDURE Get_Shifted_Period (
5625         p_period_name                   IN      pa_periods.period_name%TYPE
5626 /*     ,p_plan_period_type              IN      pa_proj_period_profiles.plan_period_type%TYPE */
5627        ,p_plan_period_type              IN      VARCHAR2
5628        ,p_number_of_periods             IN      NUMBER
5629        ,x_shifted_period                OUT     NOCOPY pa_periods.period_name%TYPE --File.Sql.39 bug 4440895
5630 /*     ,x_shifted_period_start_date     OUT   pa_proj_period_profiles.period1_start_date%TYPE
5631        ,x_shifted_period_end_date       OUT     pa_proj_period_profiles.period1_end_date%TYPE */
5632        ,x_shifted_period_start_date     OUT     NOCOPY DATE   --File.Sql.39 bug 4440895
5633        ,x_shifted_period_end_date       OUT     NOCOPY DATE --File.Sql.39 bug 4440895
5634        ,x_return_status                 OUT     NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5635        ,x_msg_count                     OUT     NOCOPY NUMBER --File.Sql.39 bug 4440895
5636        ,x_msg_data                      OUT     NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
5637 AS
5638 l_msg_count                     NUMBER := 0;
5639 l_data                          VARCHAR2(2000);
5640 l_msg_data                      VARCHAR2(2000);
5641 l_msg_index_out                 NUMBER;
5642 l_return_status                 VARCHAR2(2000);
5643 
5644 l_start_date                    pa_periods.start_date%TYPE;
5645 l_end_date                      pa_periods.end_date%TYPE;
5646 
5647 BEGIN
5648       x_msg_count := 0;
5649       x_return_status := FND_API.G_RET_STS_SUCCESS;
5650  IF P_PA_DEBUG_MODE = 'Y' THEN
5651       pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Shifted_Period');
5652       pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
5653 END IF;
5654       -- check for not null parameters
5655       IF (p_period_name IS NULL)      OR
5656          (p_plan_period_type IS NULL) OR
5657          (p_number_of_periods IS NULL)
5658       THEN
5659             IF p_pa_debug_mode = 'Y' THEN
5660                 pa_debug.g_err_stage:= 'p_period_name = '|| p_period_name;
5661                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5662                 pa_debug.g_err_stage:= 'p_plan_period_type = '|| p_plan_period_type;
5663                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5664                 pa_debug.g_err_stage:= 'p_number_of_periods = '|| p_number_of_periods;
5665                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5666             END IF;
5667             PA_UTILS.ADD_MESSAGE
5668                    (p_app_short_name => 'PA',
5669                     p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
5670 
5671             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5672       END IF;
5673 
5674       -- Fetch the start and end dates of the input period name
5675 
5676       Pa_Fin_Plan_Utils.Get_Period_Details(
5677                 p_period_name       =>   p_period_name
5678                ,p_plan_period_type  =>   p_plan_period_type
5679                ,x_start_date        =>   l_start_date
5680                ,x_end_date          =>   l_end_date
5681                ,x_return_status     =>   l_return_status
5682                ,x_msg_count         =>   l_msg_count
5683                ,x_msg_data          =>   l_msg_data );
5684 
5685       IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
5686            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5687       END IF;
5688 
5689       IF p_plan_period_type = 'PA' THEN
5690            BEGIN
5691                    IF p_number_of_periods > 0 THEN
5692 
5693                        SELECT  period_name
5694                               ,start_date
5695                               ,end_date
5696                        INTO    x_shifted_period
5697                               ,x_shifted_period_start_date
5698                               ,x_shifted_period_end_date
5699                        FROM   pa_periods a
5700                        WHERE  p_number_of_periods = (SELECT COUNT(*) FROM pa_periods b
5701                                                      WHERE  b.start_date < a.start_date
5702                                                      AND    b.start_date >= l_start_date )
5703 			/* bug fix:5090115: added this to avoid FTS on pa_periods */
5704 			AND a.start_date >= l_start_date ;
5705 
5706                    ELSIF p_number_of_periods < 0 THEN
5707 
5708                        SELECT  /*+ index(a pa_periods_u2) */
5709 			       period_name
5710                               ,start_date
5711                               ,end_date
5712                        INTO    x_shifted_period
5713                               ,x_shifted_period_start_date
5714                               ,x_shifted_period_end_date
5715                        FROM   pa_periods a
5716                        WHERE  ABS(p_number_of_periods) = (SELECT COUNT(*) FROM pa_periods b
5717                                                           WHERE  b.start_date > a.start_date
5718                                                           AND    b.start_date <= l_start_date )
5719 			/* bug fix:5090115: added this to avoid FTS on pa_periods */
5720 			AND a.start_date <= l_start_date ;
5721 
5722                    ELSIF  p_number_of_periods = 0 THEN
5723 
5724                         x_shifted_period             :=   p_period_name;
5725                         x_shifted_period_start_date  :=   l_start_date;
5726                         x_shifted_period_end_date    :=   l_end_date;
5727 
5728                    END IF;
5729            EXCEPTION
5730                    /*Fix for bug 2753123 starts */
5731                    WHEN NO_DATA_FOUND THEN
5732                        IF p_pa_debug_mode = 'Y' THEN
5733                                     pa_debug.g_err_stage:= 'Failed in shifting PA profile as Periods do not exist .'||SQLERRM;
5734                                     pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5735                        END IF;
5736                        PA_UTILS.ADD_MESSAGE
5737                              (p_app_short_name => 'PA',
5738                              p_msg_name       => 'PA_BU_INVALID_NEW_PERIOD');
5739                        RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5740                     /*Fix for bug 2753123 ends */
5741                    WHEN OTHERS THEN
5742                         IF p_pa_debug_mode = 'Y' THEN
5743                                 pa_debug.g_err_stage:= 'Unexp error while fetching shifted PA period'||SQLERRM;
5744                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5745                         END IF;
5746                         RAISE;
5747            END;
5748       ELSIF p_plan_period_type = 'GL' THEN
5749 
5750             BEGIN
5751                IF p_number_of_periods > 0 THEN
5752                        SELECT  period_name
5753                               ,start_date
5754                               ,end_date
5755                        INTO    x_shifted_period
5756                               ,x_shifted_period_start_date
5757                               ,x_shifted_period_end_date
5758                        FROM   gl_period_statuses g1
5759                              ,pa_implementations i
5760                        WHERE  g1.application_id = pa_period_process_pkg.application_id
5761                        AND    g1.set_of_books_id = i.set_of_books_id
5762                        AND    g1.adjustment_period_flag = 'N'
5763                        AND    p_number_of_periods = (SELECT COUNT(*)
5764                                                      FROM   gl_period_statuses g2
5765                                                            ,pa_implementations i2
5766                                                      WHERE  g2.adjustment_period_flag = 'N'
5767                                                      AND    g2.application_id =pa_period_process_pkg.application_id
5768                                                      AND    g2.set_of_books_id = i2.set_of_books_id
5769                                                      AND    g2.start_date < g1.start_date
5770                                                      AND    g2.start_date >= l_start_date);
5771 
5772                ELSIF p_number_of_periods < 0 THEN
5773 
5774                        SELECT  period_name
5775                               ,start_date
5776                               ,end_date
5777                        INTO   x_shifted_period
5778                               ,x_shifted_period_start_date
5779                               ,x_shifted_period_end_date
5780                        FROM   gl_period_statuses g1
5781                              ,pa_implementations i
5782                        WHERE  g1.application_id = pa_period_process_pkg.application_id
5783                        AND    g1.set_of_books_id = i.set_of_books_id
5784                        AND    g1.adjustment_period_flag = 'N'
5785                        AND    abs(p_number_of_periods) = (SELECT COUNT(*)
5786                                                           FROM   gl_period_statuses g2
5787                                                                 ,pa_implementations i2
5788                                                           WHERE  g2.adjustment_period_flag = 'N'
5789                                                           AND    g2.application_id = pa_period_process_pkg.application_id
5790                                                           AND    g2.set_of_books_id = i2.set_of_books_id
5791                                                           AND    g2.start_date > g1.start_date
5792                                                           AND    g2.start_date <= l_start_date);
5793                  ELSIF  p_number_of_periods = 0 THEN
5794 
5795                         x_shifted_period             :=   p_period_name;
5796                         x_shifted_period_start_date  :=   l_start_date;
5797                         x_shifted_period_end_date    :=   l_end_date;
5798 
5799                  END IF;
5800             EXCEPTION
5801                /*Fix for bug 2753123 starts */
5802                WHEN NO_DATA_FOUND THEN
5803                        IF p_pa_debug_mode = 'Y' THEN
5804                             pa_debug.g_err_stage:= 'Failed in shifting GL profile as Periods do not exist .'||SQLERRM;
5805                             pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5806                         END IF;
5807                         PA_UTILS.ADD_MESSAGE
5808                              (p_app_short_name => 'PA',
5809                              p_msg_name       => 'PA_BU_INVALID_NEW_PERIOD');
5810                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5811                /*Fix for bug 2753123 ends */
5812                WHEN OTHERS THEN
5813                         IF p_pa_debug_mode = 'Y' THEN
5814                                 pa_debug.g_err_stage:= 'Unexp error while fetching shifted GL period'||SQLERRM;
5815                                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5816                         END IF;
5817                         RAISE;
5818             END;
5819 
5820       END IF;
5821 
5822       IF p_pa_debug_mode = 'Y' THEN
5823               pa_debug.g_err_stage:= 'Exiting Get_Shifted_Period';
5824               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
5825       pa_debug.reset_err_stack;
5826 	END IF;
5827   EXCEPTION
5828 
5829      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
5830 
5831            x_return_status := FND_API.G_RET_STS_ERROR;
5832            l_msg_count := FND_MSG_PUB.count_msg;
5833            IF l_msg_count = 1 THEN
5834                 PA_INTERFACE_UTILS_PUB.get_messages
5835                       (p_encoded        => FND_API.G_TRUE
5836                       ,p_msg_index      => 1
5837                       ,p_msg_count      => l_msg_count
5838                       ,p_msg_data       => l_msg_data
5839                       ,p_data           => l_data
5840                       ,p_msg_index_out  => l_msg_index_out);
5841                 x_msg_data := l_data;
5842                 x_msg_count := l_msg_count;
5843 
5844            ELSE
5845                 x_msg_count := l_msg_count;
5846            END IF;
5847  IF P_PA_DEBUG_MODE = 'Y' THEN
5848            pa_debug.reset_err_stack;
5849  END IF;
5850            RAISE;
5851 
5852    WHEN others THEN
5853 
5854           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5855           x_msg_count     := 1;
5856           x_msg_data      := SQLERRM;
5857           FND_MSG_PUB.add_exc_msg
5858                           ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
5859                            ,p_procedure_name  => 'Get_Shifted_Period'
5860                            ,p_error_text      => SQLERRM);
5861           IF p_pa_debug_mode = 'Y' THEN
5862                   pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
5863                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5864           pa_debug.reset_err_stack;
5865 	END IF;
5866           RAISE;
5867 END Get_Shifted_Period;
5868 
5869 /* This function takes the returns est_quantity or labor_quantity (planned qty) from pa_budget_lines
5870    based on the parameter p_quantity ('ESTIMATED','PLANNED'). This values are return for CI version
5871    if p_version_code is CTRL_ITEM_VERSION. If p_version_code CURRENT_BASELINED_VERSION then the qty
5872    is returned from the current baselined version */
5873 /* Bug 3749781: New allowed value for p_version_code  : CURRENT_WORKING_VERSION
5874                                   and p_quantity_type : EQUIPMENT added */
5875 
5876 FUNCTION Get_Approved_Budget_Ver_Qty (
5877         p_project_id                     IN NUMBER
5878        ,p_version_code                  IN  VARCHAR2 /* CTRL_ITEM_VERSION or CURRENT_BASELINED_VERSION or CURRENT_WORKING_VERSION*/
5879        ,p_quantity_type                 IN  VARCHAR2 /* ESTIMATED or PLANNED or EQUIPMENT */
5880        ,p_ci_id                         IN  NUMBER)
5881     RETURN pa_budget_lines.quantity%TYPE is
5882 
5883 /* cur_ci_ver, cur_working_ver and cur_baselined_ver should have the same number and datatype of columns */
5884 
5885 cursor cur_ci_ver(c_version_type     pa_budget_versions.version_type%TYPE,
5886                   c_ci_id            pa_budget_versions.ci_id%TYPE) IS
5887 SELECT budget_version_id, labor_quantity, est_quantity, equipment_quantity
5888 FROM   pa_budget_versions
5889 WHERE  project_id = p_project_id
5890 AND    version_type = nvl(c_version_type,version_type)
5891 AND    ci_id = c_ci_id;
5892 
5893 /* cur_ci_ver and cur_baselined_ver should have the same number and datatype of columns */
5894 
5895 cursor cur_baselined_ver(c_version_type     pa_budget_versions.version_type%TYPE) IS
5896 SELECT budget_version_id, labor_quantity, est_quantity, equipment_quantity
5897 FROM   pa_budget_versions
5898 WHERE  project_id = p_project_id
5899 AND    current_flag = 'Y'
5900 AND    version_type = nvl(c_version_type,version_type)
5901 AND    (NVL(Approved_Cost_Plan_Type_Flag ,'N') = 'Y' OR
5902        NVL(Approved_Rev_Plan_Type_Flag  ,'N') = 'Y' );
5903 
5904 cursor cur_working_ver(c_version_type     pa_budget_versions.version_type%TYPE) IS
5905 SELECT budget_version_id, labor_quantity, est_quantity, equipment_quantity
5906 FROM   pa_budget_versions
5907 WHERE  project_id = p_project_id
5908 AND    current_working_flag = 'Y'
5909 AND    version_type = nvl(c_version_type,version_type)
5910 AND    (NVL(Approved_Cost_Plan_Type_Flag ,'N') = 'Y' OR
5911        NVL(Approved_Rev_Plan_Type_Flag  ,'N') = 'Y' );
5912 
5913 cur_ci_ver_rec                  cur_ci_ver%rowtype;
5914 l_ver_count                     NUMBER;
5915 
5916 C_BASELINED_VERSION   CONSTANT  VARCHAR2(30) := 'CURRENT_BASELINED_VERSION';
5917 C_WORKING_VERSION     CONSTANT  VARCHAR2(30) := 'CURRENT_WORKING_VERSION';
5918 C_CTRL_ITEM_VERSION   CONSTANT  VARCHAR2(30) := 'CTRL_ITEM_VERSION';
5919 C_ESTIMATED_QUANTITY  CONSTANT  VARCHAR2(30) := 'ESTIMATED';
5920 C_PLANNED_QUANTITY    CONSTANT  VARCHAR2(30) := 'PLANNED';
5921 C_EQUIPMENT_QUANTITY  CONSTANT  VARCHAR2(30) := 'EQUIPMENT';
5922 
5923 BEGIN
5924 
5925       IF p_pa_debug_mode = 'Y' THEN
5926               pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Approved_Budget_Ver_Qty');
5927               pa_debug.set_process('PLSQL','LOG',p_pa_debug_mode);
5928       END IF;
5929 
5930       -- Check for business rules violations
5931 
5932       IF p_pa_debug_mode = 'Y' THEN
5933               pa_debug.g_err_stage:= 'Validating input parameters';
5934               pa_debug.write(l_module_name,pa_debug.g_err_stage,
5935                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
5936       END IF;
5937 
5938       IF ((p_project_id IS NULL) OR (p_version_code IS NULL) OR (p_quantity_type IS NULL)) OR
5939            ((p_version_code = C_CTRL_ITEM_VERSION) AND (p_ci_id IS NULL)) THEN
5940               IF p_pa_debug_mode = 'Y' THEN
5941                         pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
5942                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
5943                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5944                         pa_debug.g_err_stage:= 'p_version_code = '|| p_version_code;
5945                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
5946                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5947                         pa_debug.g_err_stage:= 'p_quantity_type = '|| p_quantity_type;
5948                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
5949                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5950                         pa_debug.g_err_stage:= 'p_ci_id = '|| p_ci_id;
5951                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
5952                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5953               END IF;
5954               PA_UTILS.ADD_MESSAGE
5955                       (p_app_short_name => 'PA',
5956                        p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
5957               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5958 
5959       END IF;
5960 
5961       IF p_version_code = C_CTRL_ITEM_VERSION THEN
5962 
5963           /* Check the number of version types attached to the ci id.
5964              It would be two, if preference code of the appr budget type is COST_AND_REV_SEP
5965              OR
5966              two appr budget plan types are attached to the project (one cost appr plan type and
5967              another crev appr plan type). So, when the count is two,
5968              only the cost version is considered for deriving the qty info.
5969              If the count is count is one, the qty info should be taken from the available version */
5970 
5971           SELECT count(1)
5972           INTO   l_ver_count
5973           FROM   pa_budget_versions
5974           WHERE  project_id = p_project_id
5975           AND    ci_id = p_ci_id;
5976 
5977           IF l_ver_count = 2 THEN
5978 
5979               OPEN cur_ci_ver(PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_COST, p_ci_id);
5980               FETCH cur_ci_ver INTO cur_ci_ver_rec;
5981               IF cur_ci_ver%NOTFOUND THEN
5982                   IF p_pa_debug_mode = 'Y' THEN
5983                       pa_debug.g_err_stage:= 'Could not fetch cost ci version details!!!...';
5984                       pa_debug.write(l_module_name,pa_debug.g_err_stage,
5985                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
5986                       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5987                   END IF;
5988               END IF;
5989               CLOSE cur_ci_ver;
5990 
5991           ELSIF l_ver_count = 1 THEN
5992 
5993               OPEN cur_ci_ver(null, p_ci_id);
5994               FETCH cur_ci_ver INTO cur_ci_ver_rec;
5995               IF cur_ci_ver%NOTFOUND THEN
5996                   IF p_pa_debug_mode = 'Y' THEN
5997                       pa_debug.g_err_stage:= 'Could not fetch ci version details!!!...';
5998                       pa_debug.write(l_module_name,pa_debug.g_err_stage,
5999                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
6000                       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6001                   END IF;
6002               END IF;
6003               CLOSE cur_ci_ver;
6004 
6005           ELSIF l_ver_count <> 0 THEN
6006 
6007               /* There should not be a case where there more more than 2 versions !! */
6008 
6009               IF p_pa_debug_mode = 'Y' THEN
6010                   pa_debug.g_err_stage:= 'More than 2 ctrl item versions for the project!!!';
6011                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
6012                                              PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
6013                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6014               END IF;
6015           END IF;
6016 
6017       ELSIF p_version_code = C_BASELINED_VERSION THEN
6018 
6019           SELECT count(1)
6020           INTO   l_ver_count
6021           FROM   pa_budget_versions
6022           WHERE  project_id = p_project_id
6023           AND    current_flag = 'Y'
6024           AND    (Approved_Cost_Plan_Type_Flag = 'Y' OR
6025                  Approved_Rev_Plan_Type_Flag = 'Y' );
6026 
6027           IF l_ver_count = 2 THEN
6028 
6029               OPEN cur_baselined_ver(PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_COST);
6030               FETCH cur_baselined_ver INTO cur_ci_ver_rec;
6031               CLOSE cur_baselined_ver;
6032 
6033           ELSIF l_ver_count = 1 THEN
6034 
6035               OPEN cur_baselined_ver(null);
6036               FETCH cur_baselined_ver INTO cur_ci_ver_rec;
6037               CLOSE cur_baselined_ver;
6038 
6039           ELSIF l_ver_count <> 0 THEN
6040 
6041               /* There should not be a case where there more more than 2 versions !! */
6042 
6043               IF p_pa_debug_mode = 'Y' THEN
6044                   pa_debug.g_err_stage:= 'More than 2 current baselined item versions for the project!!!';
6045                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
6046                                              PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
6047                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6048               END IF;
6049 
6050           END IF;
6051 
6052       ELSIF p_version_code = C_WORKING_VERSION THEN
6053 
6054           SELECT count(1)
6055           INTO   l_ver_count
6056           FROM   pa_budget_versions
6057           WHERE  project_id = p_project_id
6058           AND    current_working_flag = 'Y'
6059           AND    (Approved_Cost_Plan_Type_Flag = 'Y' OR
6060                  Approved_Rev_Plan_Type_Flag = 'Y' );
6061 
6062           IF l_ver_count = 2 THEN
6063 
6064               OPEN cur_working_ver(PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_COST);
6065               FETCH cur_working_ver INTO cur_ci_ver_rec;
6066               CLOSE cur_working_ver;
6067 
6068           ELSIF l_ver_count = 1 THEN
6069 
6070               OPEN cur_working_ver(null);
6071               FETCH cur_working_ver INTO cur_ci_ver_rec;
6072               CLOSE cur_working_ver;
6073 
6074           ELSIF l_ver_count <> 0 THEN
6075 
6076               /* There should not be a case where there more more than 2 versions !! */
6077 
6078               IF p_pa_debug_mode = 'Y' THEN
6079                   pa_debug.g_err_stage:= 'More than 2 current working versions for the project!!!';
6080                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
6081                                              PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
6082                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6083               END IF;
6084 
6085           END IF;
6086 
6087 
6088       END IF;
6089 
6090       IF p_pa_debug_mode = 'Y' THEN
6091               pa_debug.g_err_stage:= 'Exiting Pa_Fin_Plan_Utils.Get_Approved_Budget_Ver_Qty';
6092               pa_debug.write(l_module_name,pa_debug.g_err_stage,
6093                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
6094               pa_debug.reset_err_stack;
6095 
6096       END IF;
6097 
6098       IF p_quantity_type = C_ESTIMATED_QUANTITY THEN
6099         return cur_ci_ver_rec.est_quantity;
6100       ELSIF p_quantity_type = C_PLANNED_QUANTITY THEN
6101           return cur_ci_ver_rec.labor_quantity;
6102       ELSIF p_quantity_type = C_EQUIPMENT_QUANTITY THEN
6103           return cur_ci_ver_rec.equipment_quantity;
6104       ELSE
6105           return Null;
6106       END IF;
6107 
6108 
6109  EXCEPTION
6110 
6111      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
6112 
6113            IF p_pa_debug_mode = 'Y' THEN
6114                    pa_debug.g_err_stage := 'Invalid arg exception ..';
6115                    pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
6116                    pa_debug.reset_err_stack;
6117            END IF;
6118            RAISE;
6119 
6120    WHEN others THEN
6121 
6122           FND_MSG_PUB.add_exc_msg
6123                           ( p_pkg_name        => 'Pa_Fin_Plan_Utils'
6124                            ,p_procedure_name  => 'Get_Approved_Budget_Ver_Qty'
6125                            ,p_error_text      => sqlerrm);
6126 
6127           IF p_pa_debug_mode = 'Y' THEN
6128               pa_debug.g_err_stage:= 'Unexpected Error'||sqlerrm;
6129               pa_debug.write(l_module_name,pa_debug.g_err_stage,
6130                                      PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
6131               pa_debug.reset_err_stack;
6132 
6133           END IF;
6134           RAISE;
6135 
6136 END Get_Approved_Budget_Ver_Qty;
6137 
6138 
6139 /*This API is internally used to validate whether all the rate types and rate date types passed
6140   are valid or not. This is called from VALIDATE_CURRENCY_ATTRIBUTES when it is called from
6141   AMG
6142 */
6143 PROCEDURE VALIDATE_INPUT_PARAMS
6144                      (p_project_cost_rate_type             IN  pa_proj_fp_options.project_cost_rate_type%TYPE
6145                      ,p_project_cost_rate_date_typ         IN  pa_proj_fp_options.project_cost_rate_date_type%TYPE
6146                      ,p_projfunc_cost_rate_type            IN  pa_proj_fp_options.projfunc_cost_rate_type%TYPE
6147                      ,p_projfunc_cost_rate_date_typ        IN  pa_proj_fp_options.projfunc_cost_rate_date_type%TYPE
6148                      ,p_project_rev_rate_type              IN  pa_proj_fp_options.project_rev_rate_type%TYPE
6149                      ,p_project_rev_rate_date_typ          IN  pa_proj_fp_options.project_rev_rate_date_type%TYPE
6150                      ,p_projfunc_rev_rate_type             IN  pa_proj_fp_options.projfunc_rev_rate_type%TYPE
6151                      ,p_projfunc_rev_rate_date_typ         IN  pa_proj_fp_options.projfunc_rev_rate_date_type%TYPE
6152                      ,p_project_currency_code              IN  pa_projects_all.project_currency_code%TYPE
6153                      ,p_projfunc_currency_code             IN  pa_projects_all.projfunc_currency_code%TYPE
6154                      ,p_txn_currency_code                  IN  pa_budget_lines.txn_currency_code%TYPE
6155                      ,x_return_status                      OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6156                      ,x_msg_count                          OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
6157                      ,x_msg_data                           OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6158                      ) IS
6159 
6160 l_msg_count                     NUMBER := 0;
6161 l_data                          VARCHAR2(2000);
6162 l_msg_data                      VARCHAR2(2000);
6163 l_msg_index_out                 NUMBER;
6164 l_return_status                 VARCHAR2(2000);
6165 l_debug_mode                    VARCHAR2(30);
6166 l_exists                        VARCHAR2(1);
6167 
6168 BEGIN
6169 
6170     x_msg_count := 0;
6171     x_return_status := FND_API.G_RET_STS_SUCCESS;
6172  IF P_PA_DEBUG_MODE = 'Y' THEN
6173     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.validate_input_params');
6174  END IF;
6175     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
6176     l_debug_mode := NVL(l_debug_mode, 'Y');
6177     IF l_debug_mode = 'Y' THEN
6178         pa_debug.set_process('PLSQL','LOG',l_debug_mode);
6179         pa_debug.g_err_stage:='About to validate the values for currency conversion attributes passed for AMG';
6180         pa_debug.write('validate_input_params: ' || l_module_name,pa_debug.g_err_stage,3);
6181     END IF;
6182 
6183     /*Validate Rate Type*/
6184     BEGIN
6185         SELECT 'Y'
6186         INTO   l_exists
6187         FROM    dual
6188         WHERE   EXISTS (SELECT 'X'
6189                     FROM   pa_conversion_types_v
6190                     WHERE  ((p_project_cost_rate_type IS NULL
6191                              OR p_project_cost_rate_type=conversion_type)  OR
6192 
6193                              p_project_currency_code IN( p_txn_currency_code
6194                                                         ,p_projfunc_currency_code))
6195                     AND    rownum=1)
6196 
6197         AND     EXISTS (SELECT 'X'
6198                     FROM   pa_conversion_types_v
6199                     WHERE  ((p_projfunc_cost_rate_type IS NULL
6200                              OR p_projfunc_cost_rate_type=conversion_type) OR
6201 
6202                              p_projfunc_currency_code = p_txn_currency_code )
6203                     AND    rownum=1)
6204 
6205         AND     EXISTS (SELECT 'X'
6206                     FROM   pa_conversion_types_v
6207                     WHERE  ((p_project_rev_rate_type IS NULL
6208                              OR p_project_rev_rate_type=conversion_type)  OR
6209 
6210                              p_project_currency_code IN( p_txn_currency_code
6211                                                         ,p_projfunc_currency_code))
6212                     AND    rownum=1)
6213 
6214         AND     EXISTS (SELECT 'X'
6215                     FROM   pa_conversion_types_v
6216                     WHERE  ((p_projfunc_rev_rate_type IS NULL
6217                              OR p_projfunc_rev_rate_type=conversion_type)  OR
6218 
6219                               p_projfunc_currency_code = p_txn_currency_code )
6220                     AND    rownum=1);
6221 
6222     EXCEPTION
6223 
6224        WHEN NO_DATA_FOUND  THEN
6225           X_RETURN_STATUS :=  FND_API.G_RET_STS_ERROR;
6226           PA_UTILS.add_message
6227             (p_app_short_name => 'PA',
6228              p_msg_name       => 'PA_FP_INCORRECT_RATE_TYPE_AMG',
6229              p_token1         => 'TASK',
6230              p_value1         =>  pa_budget_pvt.g_task_number,
6231              p_token2         => 'SOURCE_NAME',
6232              p_value2         => pa_budget_pvt.g_resource_alias,
6233              p_token3         => 'START_DATE',
6234              p_value3         => to_char(pa_budget_pvt.g_start_date));
6235 END;
6236 
6237     /*VALIDATE RATE DATE TYPE*/
6238     BEGIN
6239         SELECT 'Y'
6240         INTO l_exists
6241         FROM    dual
6242         WHERE   EXISTS (SELECT 'X'
6243                     FROM   pa_lookups
6244                     WHERE  lookup_type='PA_FP_RATE_DATE_TYPE'
6245                     AND    ((p_project_cost_rate_date_typ IS NULL
6246                              OR p_project_cost_rate_date_typ=lookup_code) OR
6247 
6248                              p_project_currency_code IN( p_txn_currency_code
6249                                                         ,p_projfunc_currency_code))
6250                     AND    rownum=1)
6251 
6252         AND     EXISTS (SELECT 'X'
6253                     FROM   pa_lookups
6254                     WHERE  lookup_type='PA_FP_RATE_DATE_TYPE'
6255                     AND    ((p_projfunc_cost_rate_date_typ IS NULL
6256                              OR p_projfunc_cost_rate_date_typ=lookup_code)  OR
6257 
6258                              p_projfunc_currency_code = p_txn_currency_code )
6259                     AND    rownum=1)
6260 
6261         AND     EXISTS (SELECT 'X'
6262                     FROM   pa_lookups
6263                     WHERE  lookup_type='PA_FP_RATE_DATE_TYPE'
6264                     AND    ((p_project_rev_rate_date_typ IS NULL
6265                              OR p_project_rev_rate_date_typ=lookup_code) OR
6266 
6267                             p_project_currency_code IN( p_txn_currency_code
6268                                                        ,p_projfunc_currency_code))
6269                     AND    rownum=1)
6270 
6271         AND     EXISTS (SELECT 'X'
6272                     FROM   pa_lookups
6273                     WHERE  lookup_type='PA_FP_RATE_DATE_TYPE'
6274                     AND    ((p_projfunc_rev_rate_date_typ IS NULL
6275                              OR p_projfunc_rev_rate_date_typ=lookup_code) OR
6276 
6277                              p_projfunc_currency_code = p_txn_currency_code )
6278                     AND    rownum=1)  ;
6279 
6280     EXCEPTION
6281 
6282        WHEN NO_DATA_FOUND  THEN
6283           X_RETURN_STATUS :=  FND_API.G_RET_STS_ERROR;
6284           PA_UTILS.add_message
6285             (p_app_short_name => 'PA',
6286              p_msg_name       => 'PA_FP_INVALID_RATE_DT_TYPE_AMG',
6287              p_token1         => 'TASK',
6288              p_value1         =>  pa_budget_pvt.g_task_number,
6289              p_token2         => 'SOURCE_NAME',
6290              p_value2         => pa_budget_pvt.g_resource_alias,
6291              p_token3         => 'START_DATE',
6292              p_value3         => to_char(pa_budget_pvt.g_start_date));
6293 
6294     END;
6295 
6296     IF l_debug_mode='Y' THEN
6297         pa_debug.g_err_stage:= 'Exiting validate_input_params';
6298         pa_debug.write(l_module_name,pa_debug.g_err_stage,
6299                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
6300 
6301 	   pa_debug.reset_err_stack;
6302 	END IF;
6303 EXCEPTION
6304 
6305 WHEN OTHERS THEN
6306         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6307         x_msg_count     := 1;
6308         x_msg_data      := SQLERRM;
6309 
6310         FND_MSG_PUB.add_exc_msg
6311          ( p_pkg_name       => 'PA_FIN_PLAN_UTILS'
6312           ,p_procedure_name => 'validate_input_params'
6313           ,p_error_text     => sqlerrm);
6314 
6315         pa_debug.G_Err_Stack := SQLERRM;
6316         IF P_PA_DEBUG_MODE = 'Y' THEN
6317             pa_debug.write('validate_input_params' || l_module_name,pa_debug.G_Err_Stack,4);
6318         pa_debug.reset_err_stack;
6319 	END IF;
6320         RAISE;
6321 
6322 END  validate_input_params;
6323 
6324 /*This procedure validates a set of conversion attributes. It returns  a validity code indicating
6325   the validity of conversion attributes. The values of validity code are
6326 
6327   RATE_TYPE_NULL       which indicates that the rate type is null
6328   RATE_DATE_TYPE_NULL  which indicates that the rate date type is null
6329   RATE_DATE_NULL       which indicates that the rate date is null
6330   VALID_CONV_ATTR      which indicates that the attributes passed are valid.
6331   NULL_ATTR            which indicates that the attributes passed are null
6332   RATE_NULL            which indicates that the rate passed is null
6333 */
6334 
6335 PROCEDURE VALIDATE_CONV_ATTRIBUTES
6336           ( px_rate_type         IN OUT  NOCOPY pa_proj_fp_options.projfunc_cost_rate_type%TYPE --File.Sql.39 bug 4440895
6337            ,px_rate_date_type    IN OUT  NOCOPY pa_proj_fp_options.projfunc_cost_rate_date_type%TYPE --File.Sql.39 bug 4440895
6338            ,px_rate_date         IN OUT  NOCOPY pa_proj_fp_options.projfunc_cost_rate_date%TYPE --File.Sql.39 bug 4440895
6339            ,px_rate              IN OUT  NOCOPY pa_budget_lines.project_cost_exchange_rate%TYPE --File.Sql.39 bug 4440895
6340            ,p_amount_type_code   IN      VARCHAR2
6341            ,p_currency_type_code IN      VARCHAR2
6342            ,p_calling_context    IN      VARCHAR2
6343            ,x_first_error_code      OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6344            ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6345            ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
6346            ,x_msg_data              OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6347           )  IS
6348 l_msg_count                     NUMBER := 0;
6349 l_data                          VARCHAR2(2000);
6350 l_msg_data                      VARCHAR2(2000);
6351 l_msg_index_out                 NUMBER;
6352 l_return_status                 VARCHAR2(2000);
6353 l_debug_mode                    VARCHAR2(30);
6354 
6355 BEGIN
6356     x_msg_count := 0;
6357     x_return_status := FND_API.G_RET_STS_SUCCESS;
6358  IF P_PA_DEBUG_MODE = 'Y' THEN
6359     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.VALIDATE_CONV_ATTRIBUTES');
6360  END IF;
6361     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
6362     l_debug_mode := NVL(l_debug_mode, 'Y');
6363     IF l_debug_mode = 'Y' THEN
6364         pa_debug.set_process('PLSQL','LOG',l_debug_mode);
6365         pa_debug.g_err_stage:='Validating the given set of conversion attributes';
6366         pa_debug.write('validate_set_of_conv_attrs: ' || l_module_name,pa_debug.g_err_stage,3);
6367     END IF;
6368 
6369     IF((px_rate_type IS NULL)      AND
6370        (px_rate_date_type IS NULL) AND
6371        (px_rate_date IS NULL) )    THEN
6372 
6373           /* Null Combination of conversion attributes is valid in the case of create update plan type
6374              pages. Hence this check is made
6375           */
6376 
6377           IF l_debug_mode = 'Y' THEN
6378                pa_debug.g_err_stage:='All the attributes are null';
6379                pa_debug.write('validate_set_of_conv_attrs: ' || l_module_name,pa_debug.g_err_stage,3);
6380           END IF;
6381 
6382           IF(p_calling_context=PA_FP_CONSTANTS_PKG.G_CR_UP_PLAN_TYPE_PAGE) THEN
6383                /* Do Nothing as this will be checked in validate_currency_attributes */
6384                NULL;
6385           ELSE
6386                x_return_status := FND_API.G_RET_STS_ERROR;
6387                /*PA_UTILS.ADD_MESSAGE
6388                              (p_app_short_name => 'PA',
6389                               p_msg_name       => 'PA_FP_RATE_TYPE_REQ');
6390                */
6391                /*
6392                   NOTE: The following message is different from the first error code that is being
6393                   passed back. This specific message is being used as this accepts tokens.
6394                */
6395                IF  (p_calling_context=PA_FP_CONSTANTS_PKG.G_AMG_API_DETAIL ) THEN
6396 
6397                      PA_UTILS.ADD_MESSAGE
6398                       (p_app_short_name => 'PA',
6399                         p_msg_name      => 'PA_FP_INVALID_RATE_TYPE_AMG',
6400                         p_token1        => 'TASK',
6401                         p_value1        =>  pa_budget_pvt.g_task_number,
6402                         p_token2        => 'SOURCE_NAME',
6403                         p_value2        =>  pa_budget_pvt.g_resource_alias,
6404                         p_token3        => 'START_DATE',
6405                         p_value3        => to_char(pa_budget_pvt.g_start_date),
6406                         p_token4        => 'COST_REV',
6407                         p_value4        => p_amount_type_code,
6408                         p_token5        => 'PROJECT_PROJFUNC',
6409                         p_value5        => p_currency_type_code );
6410 
6411 
6412                ELSE
6413 
6414                      PA_UTILS.ADD_MESSAGE
6415                             (p_app_short_name => 'PA',
6416                               p_msg_name      => 'PA_FP_INVALID_RATE_TYPE',
6417                               p_token1        => 'COST_REV',
6418                               p_value1        => p_amount_type_code,
6419                               p_token2        => 'PROJECT_PROJFUNC',
6420                               p_value2        => p_currency_type_code );
6421 
6422                END IF;
6423 
6424                /* for any other context error messages need to be added */
6425           END IF;
6426 
6427           x_first_error_code := 'PA_FP_RATE_TYPE_REQ';
6428 
6429     ELSIF (px_rate_type IS NULL) THEN
6430 
6431           IF l_debug_mode = 'Y' THEN
6432                pa_debug.g_err_stage:='Rate Type is Null';
6433                pa_debug.write('validate_set_of_conv_attrs: ' || l_module_name,pa_debug.g_err_stage,3);
6434           END IF;
6435 
6436           x_return_status := FND_API.G_RET_STS_ERROR;
6437           IF  (p_calling_context=PA_FP_CONSTANTS_PKG.G_AMG_API_DETAIL ) THEN
6438 
6439                 PA_UTILS.ADD_MESSAGE
6440                       (p_app_short_name => 'PA',
6441                         p_msg_name      => 'PA_FP_INVALID_RATE_TYPE_AMG',
6442                         p_token1        => 'TASK',
6443                         p_value1        =>  pa_budget_pvt.g_task_number,
6444                         p_token2        => 'SOURCE_NAME',
6445                         p_value2        =>  pa_budget_pvt.g_resource_alias,
6446                         p_token3        => 'START_DATE',
6447                         p_value3        => to_char(pa_budget_pvt.g_start_date),
6448                         p_token4        => 'COST_REV',
6449                         p_value4        => p_amount_type_code,
6450                         p_token5        => 'PROJECT_PROJFUNC',
6451                         p_value5        => p_currency_type_code );
6452           ELSE
6453 
6454                 PA_UTILS.ADD_MESSAGE
6455                       (p_app_short_name => 'PA',
6456                         p_msg_name      => 'PA_FP_INVALID_RATE_TYPE',
6457                         p_token1        => 'COST_REV',
6458                         p_value1        => p_amount_type_code,
6459                         p_token2        => 'PROJECT_PROJFUNC',
6460                         p_value2        => p_currency_type_code );
6461 
6462           END IF;
6463 
6464           IF x_first_error_code IS NULL THEN
6465                x_first_error_code := 'PA_FP_INVALID_RATE_TYPE';
6466           END IF;
6467 
6468     ELSIF (px_rate_type = PA_FP_CONSTANTS_PKG.G_RATE_TYPE_USER ) THEN
6469 
6470           IF l_debug_mode = 'Y' THEN
6471                pa_debug.g_err_stage:='Rate Type is User';
6472                pa_debug.write('validate_set_of_conv_attrs: ' || l_module_name,pa_debug.g_err_stage,3);
6473           END IF;
6474           --Added the constant G_AMG_API_HEADER as part of changes due to finplan model in AMG
6475           IF (px_rate IS NULL AND nvl(p_calling_context,'-99') NOT IN ( PA_FP_CONSTANTS_PKG.G_CR_UP_PLAN_TYPE_PAGE
6476                                                                         ,PA_FP_CONSTANTS_PKG.G_AMG_API_HEADER) )THEN
6477 
6478                /* on create update plan type it is allowed that when rate type is user there is no rate defined
6479                */
6480                x_return_status := FND_API.G_RET_STS_ERROR;
6481 
6482                IF  (p_calling_context=PA_FP_CONSTANTS_PKG.G_AMG_API_DETAIL ) THEN
6483                      PA_UTILS.ADD_MESSAGE
6484                       (p_app_short_name => 'PA',
6485                         p_msg_name      => 'PA_FP_USER_EXCH_RATE_REQ_AMG',
6486                         p_token1        => 'TASK',
6487                         p_value1        =>  pa_budget_pvt.g_task_number,
6488                         p_token2        => 'SOURCE_NAME',
6489                         p_value2        =>  pa_budget_pvt.g_resource_alias,
6490                         p_token3        => 'START_DATE',
6491                         p_value3        => to_char(pa_budget_pvt.g_start_date),
6492                         p_token4        => 'COST_REV',
6493                         p_value4        => p_amount_type_code,
6494                         p_token5        => 'PROJECT_PROJFUNC',
6495                         p_value5        => p_currency_type_code );
6496                ELSE
6497                /*
6498                    NOTE : The following error message that is being used does not take tokens.
6499                    As of now for WEBADI and Create Update Plan type context, this is not an
6500                    issue. But when this api is being used for the other contexts like AMG and
6501                    edit plan line details page,... this message should be changed so as to accept
6502                    tokens.
6503                */
6504                      PA_UTILS.ADD_MESSAGE
6505                           (p_app_short_name => 'PA',
6506                            p_msg_name      => 'PA_FP_USER_EXCH_RATE_REQ',
6507                            p_token1        => 'COST_REV',
6508                            p_value1        => p_amount_type_code,
6509                            p_token2        => 'PROJECT_PROJFUNC',
6510                            p_value2        => p_currency_type_code );
6511               END IF;
6512 
6513 
6514           ELSE
6515 
6516                /* Null out the Rate Date Type and Rate Date */
6517                px_rate_date_type := null;
6518                px_rate_date := null;
6519 --             x_validity_code:=PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR; Not required
6520 
6521           END IF;
6522 
6523           IF x_first_error_code IS NULL THEN
6524                x_first_error_code := 'PA_FP_USER_EXCH_RATE_REQ';
6525           END IF;
6526 
6527     /* this means that rate type is not null and its value is not user */
6528     ELSIF (px_rate_date_type IS NULL) THEN
6529 
6530           IF l_debug_mode = 'Y' THEN
6531                pa_debug.g_err_stage:='Rate Date Type is Null';
6532                pa_debug.write('validate_set_of_conv_attrs: ' || l_module_name,pa_debug.g_err_stage,3);
6533           END IF;
6534 
6535           x_return_status := FND_API.G_RET_STS_ERROR;
6536           IF  (p_calling_context=PA_FP_CONSTANTS_PKG.G_AMG_API_DETAIL ) THEN
6537 
6538                 PA_UTILS.ADD_MESSAGE
6539                       (p_app_short_name => 'PA',
6540                         p_msg_name      => 'PA_FP_INVALID_RATE_DT_TYP_AMG',
6541                         p_token1        => 'TASK',
6542                         p_value1        =>  pa_budget_pvt.g_task_number,
6543                         p_token2        => 'SOURCE_NAME',
6544                         p_value2        =>  pa_budget_pvt.g_resource_alias,
6545                         p_token3        => 'START_DATE',
6546                         p_value3        => to_char(pa_budget_pvt.g_start_date),
6547                         p_token4        => 'COST_REV',
6548                         p_value4        => p_amount_type_code,
6549                         p_token5        => 'PROJECT_PROJFUNC',
6550                         p_value5        => p_currency_type_code );
6551           ELSE
6552                 PA_UTILS.ADD_MESSAGE
6553                               (p_app_short_name => 'PA',
6554                                 p_msg_name      => 'PA_FP_INVALID_RATE_DATE_TYPE',
6555                                 p_token1        => 'COST_REV',
6556                                 p_value1        => p_amount_type_code,
6557                                 p_token2        => 'PROJECT_PROJFUNC',
6558                                 p_value2        => p_currency_type_code );
6559 
6560           END IF;
6561 
6562           IF x_first_error_code IS NULL THEN
6563                x_first_error_code := 'PA_FP_INVALID_RATE_DATE_TYPE';
6564           END IF;
6565 
6566     /* this means that rate type is not null and its value is not user and rate_date_type value is FIXED */
6567     ELSIF px_rate_date_type = PA_FP_CONSTANTS_PKG.G_RATE_DATE_TYPE_FIXED_DATE THEN
6568 
6569           IF l_debug_mode = 'Y' THEN
6570                pa_debug.g_err_stage:='Rate Date Type is Fixed';
6571                pa_debug.write('validate_set_of_conv_attrs: ' || l_module_name,pa_debug.g_err_stage,3);
6572           END IF;
6573 
6574           /* Rate Date Should not be null */
6575           IF (px_rate_date IS NULL) THEN
6576 
6577                x_return_status := FND_API.G_RET_STS_ERROR;
6578                IF  (p_calling_context=PA_FP_CONSTANTS_PKG.G_AMG_API_DETAIL ) THEN
6579                      PA_UTILS.ADD_MESSAGE
6580                             (p_app_short_name => 'PA',
6581                               p_msg_name      => 'PA_FP_INVALID_RATE_DATE_AMG',
6582                               p_token1        => 'TASK',
6583                               p_value1        =>  pa_budget_pvt.g_task_number,
6584                               p_token2        => 'SOURCE_NAME',
6585                               p_value2        =>  pa_budget_pvt.g_resource_alias,
6586                               p_token3        => 'START_DATE',
6587                               p_value3        => to_char(pa_budget_pvt.g_start_date),
6588                               p_token4        => 'COST_REV',
6589                               p_value4        => p_amount_type_code,
6590                               p_token5        => 'PROJECT_PROJFUNC',
6591                               p_value5        => p_currency_type_code );
6592                ELSE
6593                      PA_UTILS.ADD_MESSAGE
6594                                 (p_app_short_name => 'PA',
6595                                   p_msg_name      => 'PA_FP_INVALID_RATE_DATE',
6596                                   p_token1        => 'COST_REV',
6597                                   p_value1        => p_amount_type_code,
6598                                   p_token2        => 'PROJECT_PROJFUNC',
6599                                   p_value2        => p_currency_type_code );
6600                END IF;
6601 
6602                IF x_first_error_code IS NULL THEN
6603                     x_first_error_code := 'PA_FP_INVALID_RATE_DATE';
6604                END IF;
6605           ELSE
6606                px_rate:=null;
6607 --             x_validity_code := PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR; Not required
6608           END IF;
6609 
6610     /* This means that rate type is not null and its value is not user
6611        and rate_date_type is not null and its value is NOT FIXED. This is a valid set.*/
6612     ELSE
6613           /* CHECK IF THIS NEEDS TO BE DONE IN CASE ITS CALLED FROM EDIT PLAN LINE PAGES */
6614           px_rate_date := null;
6615           px_rate := null;
6616 --        x_validity_code:=PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR;
6617 
6618     END IF;
6619 
6620     IF l_debug_mode = 'Y' THEN
6621         pa_debug.g_err_stage:= 'Exiting validate_conv_attributes';
6622         pa_debug.write(l_module_name,pa_debug.g_err_stage,
6623                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
6624 
6625     pa_debug.reset_err_stack;
6626 END IF;
6627 EXCEPTION
6628     WHEN OTHERS THEN
6629         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6630         x_msg_count     := 1;
6631         x_msg_data      := SQLERRM;
6632 
6633         FND_MSG_PUB.add_exc_msg
6634          ( p_pkg_name       => 'PA_FIN_PLAN_UTILS'
6635           ,p_procedure_name => 'VALIDATE_CONV_ATTRIBUTES'
6636           ,p_error_text     => sqlerrm);
6637 
6638         pa_debug.G_Err_Stack := SQLERRM;
6639         IF P_PA_DEBUG_MODE = 'Y' THEN
6640             pa_debug.write('CHECK_MRC_INSTALL: ' || l_module_name,pa_debug.G_Err_Stack,4);
6641         pa_debug.reset_err_stack;
6642 	END IF;
6643         RAISE;
6644 
6645 END  VALIDATE_CONV_ATTRIBUTES;
6646 
6647 /* This method is called for validating the currency attributes. In addition to the 12 conversion
6648    attributes this procedure also takes the context from which it is called , PC and PFC as
6649    parameters. This method  in turn calls  VALIDATE_CONV_ATTRIBUTES. The values for context are
6650    CR_UP_PLAN_TYPE_PAGE (for create Update plan type page)
6651    AMG_API (for AMG APIs)
6652 */
6653 
6654 PROCEDURE VALIDATE_CURRENCY_ATTRIBUTES
6655           ( px_project_cost_rate_type        IN OUT  NOCOPY pa_proj_fp_options.project_cost_rate_type%TYPE --File.Sql.39 bug 4440895
6656            ,px_project_cost_rate_date_typ    IN OUT  NOCOPY pa_proj_fp_options.project_cost_rate_date_type%TYPE --File.Sql.39 bug 4440895
6657            ,px_project_cost_rate_date        IN OUT  NOCOPY pa_proj_fp_options.project_cost_rate_date%TYPE --File.Sql.39 bug 4440895
6658            ,px_project_cost_exchange_rate    IN OUT  NOCOPY pa_budget_lines.project_cost_exchange_rate%TYPE --File.Sql.39 bug 4440895
6659            ,px_projfunc_cost_rate_type       IN OUT  NOCOPY pa_proj_fp_options.projfunc_cost_rate_type%TYPE --File.Sql.39 bug 4440895
6660            ,px_projfunc_cost_rate_date_typ   IN OUT  NOCOPY pa_proj_fp_options.projfunc_cost_rate_date_type%TYPE --File.Sql.39 bug 4440895
6661            ,px_projfunc_cost_rate_date       IN OUT  NOCOPY pa_proj_fp_options.projfunc_cost_rate_date%TYPE --File.Sql.39 bug 4440895
6662            ,px_projfunc_cost_exchange_rate   IN OUT  NOCOPY pa_budget_lines.projfunc_cost_exchange_rate%TYPE --File.Sql.39 bug 4440895
6663            ,px_project_rev_rate_type         IN OUT  NOCOPY pa_proj_fp_options.project_rev_rate_type%TYPE --File.Sql.39 bug 4440895
6664            ,px_project_rev_rate_date_typ     IN OUT  NOCOPY pa_proj_fp_options.project_rev_rate_date_type%TYPE --File.Sql.39 bug 4440895
6665            ,px_project_rev_rate_date         IN OUT  NOCOPY pa_proj_fp_options.project_rev_rate_date%TYPE --File.Sql.39 bug 4440895
6666            ,px_project_rev_exchange_rate     IN OUT  NOCOPY pa_budget_lines.project_rev_exchange_rate%TYPE --File.Sql.39 bug 4440895
6667            ,px_projfunc_rev_rate_type        IN OUT  NOCOPY pa_proj_fp_options.projfunc_rev_rate_type%TYPE --File.Sql.39 bug 4440895
6668            ,px_projfunc_rev_rate_date_typ    IN OUT  NOCOPY pa_proj_fp_options.projfunc_rev_rate_date_type%TYPE --File.Sql.39 bug 4440895
6669            ,px_projfunc_rev_rate_date        IN OUT  NOCOPY pa_proj_fp_options.projfunc_rev_rate_date%TYPE --File.Sql.39 bug 4440895
6670            ,px_projfunc_rev_exchange_rate    IN OUT  NOCOPY pa_budget_lines.projfunc_rev_exchange_rate%TYPE --File.Sql.39 bug 4440895
6671            ,p_project_currency_code          IN      pa_projects_all.project_currency_code%TYPE
6672            ,p_projfunc_currency_code         IN      pa_projects_all.projfunc_currency_code%TYPE
6673            ,p_txn_currency_code              IN      pa_projects_all.projfunc_currency_code%TYPE
6674            ,p_context                        IN      VARCHAR2
6675            ,p_attrs_to_be_validated          IN      VARCHAR2  -- valid values are COST, REVENUE , BOTH
6676            ,x_return_status                     OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6677            ,x_msg_count                         OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
6678            ,x_msg_data                          OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6679           )IS
6680 
6681 l_msg_count                     NUMBER := 0;
6682 l_data                          VARCHAR2(2000);
6683 l_msg_data                      VARCHAR2(2000);
6684 l_msg_index_out                 NUMBER;
6685 l_return_status                 VARCHAR2(2000);
6686 l_debug_mode                    VARCHAR2(30);
6687 /*
6688 l_loop_count                    NUMBER;
6689 l_rate_type                     pa_proj_fp_options.project_cost_rate_type%TYPE;
6690 l_rate_date_type                pa_proj_fp_options.project_cost_rate_date_type%TYPE;
6691 l_rate_date                     pa_proj_fp_options.project_cost_rate_date%TYPE;
6692 l_rate                          pa_budget_lines.project_rev_exchange_rate%TYPE ;
6693 l_validity_code                 VARCHAR2(30);*/
6694 l_pc_cost_validity_code         VARCHAR2(30) := PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR;
6695 l_pc_rev_validity_code          VARCHAR2(30) := PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR;
6696 l_pfc_cost_validity_code        VARCHAR2(30) := PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR;
6697 l_pfc_rev_validity_code         VARCHAR2(30) := PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR;
6698 l_project_token                 fnd_new_messages.message_text%TYPE; --bug 2848406 VARCHAR2(30);
6699 l_projfunc_token                fnd_new_messages.message_text%TYPE; --bug 2848406 VARCHAR2(30);
6700 l_cost_token                    fnd_new_messages.message_text%TYPE; --bug 2848406 VARCHAR2(30);
6701 l_rev_token                     fnd_new_messages.message_text%TYPE; --bug 2848406 VARCHAR2(30);
6702 /*
6703 l_project_projfunc_token        VARCHAR2(30);
6704 l_cost_rev_token                VARCHAR2(30);
6705 */
6706 l_any_error_occurred_flag       VARCHAR2(1);
6707 l_first_error_code              VARCHAR2(30); /* used for webADI */
6708 
6709 BEGIN
6710     x_msg_count := 0;
6711     x_return_status := FND_API.G_RET_STS_SUCCESS;
6712  IF P_PA_DEBUG_MODE = 'Y' THEN
6713     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.VALIDATE_CURRENCY_ATTRIBUTES');
6714  END IF;
6715     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
6716     l_debug_mode := NVL(l_debug_mode, 'Y');
6717     IF l_debug_mode = 'Y' THEN
6718     pa_debug.set_process('PLSQL','LOG',l_debug_mode);
6719     END IF;
6720     IF (p_project_currency_code IS NULL OR
6721        p_projfunc_currency_code IS NULL OR
6722        p_context                IS NULL OR
6723        p_attrs_to_be_validated  IS NULL ) THEN
6724 
6725           IF l_debug_mode = 'Y' THEN
6726              pa_debug.g_err_stage:='p_project_currency_code = ' || p_project_currency_code;
6727              pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
6728 
6729              pa_debug.g_err_stage:='p_projfunc_currency_code = ' || p_projfunc_currency_code;
6730              pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
6731 
6732              pa_debug.g_err_stage:='p_context = ' || p_context;
6733              pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
6734 
6735              pa_debug.g_err_stage:='p_attrs_to_be_validated = ' || p_attrs_to_be_validated;
6736              pa_debug.write('Get_Baselined_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
6737           END IF;
6738 
6739           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
6740                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
6741 
6742           IF l_debug_mode = 'Y' THEN
6743              pa_debug.g_err_stage := 'Invalid parameters passed' ;
6744              pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,1);
6745           END IF;
6746           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6747 
6748     END IF;
6749 
6750 
6751     IF l_debug_mode = 'Y' THEN
6752         pa_debug.g_err_stage:='About to validate the currency conversion attributes';
6753         pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,3);
6754     END IF;
6755 
6756     /*Get the message tokens that may be required while validating the attributes*/
6757 
6758     FND_MESSAGE.SET_NAME ('PA',PA_FP_CONSTANTS_PKG.G_COST_TOKEN_MESSAGE);
6759     l_cost_token := FND_MESSAGE.GET;
6760 
6761     FND_MESSAGE.SET_NAME ('PA',PA_FP_CONSTANTS_PKG.G_REV_TOKEN_MESSAGE);
6762     l_rev_token := FND_MESSAGE.GET;
6763 
6764     FND_MESSAGE.SET_NAME ('PA',PA_FP_CONSTANTS_PKG.G_PROJECT_TOKEN_MESSAGE);
6765     l_project_token := FND_MESSAGE.GET;
6766 
6767     FND_MESSAGE.SET_NAME ('PA',PA_FP_CONSTANTS_PKG.G_PROJFUNC_TOKEN_MESSAGE);
6768     l_projfunc_token := FND_MESSAGE.GET;
6769 
6770     IF (p_context=PA_FP_CONSTANTS_PKG.G_AMG_API_HEADER OR
6771         p_context=PA_FP_CONSTANTS_PKG.G_AMG_API_DETAIL ) THEN
6772 
6773         VALIDATE_INPUT_PARAMS(p_project_cost_rate_type      =>     px_project_cost_rate_type
6774                              ,p_project_cost_rate_date_typ  =>     px_project_cost_rate_date_typ
6775                              ,p_projfunc_cost_rate_type     =>     px_projfunc_cost_rate_type
6776                              ,p_projfunc_cost_rate_date_typ =>     px_projfunc_cost_rate_date_typ
6777                              ,p_project_rev_rate_type       =>     px_project_rev_rate_type
6778                              ,p_project_rev_rate_date_typ   =>     px_project_rev_rate_date_typ
6779                              ,p_projfunc_rev_rate_type      =>     px_projfunc_rev_rate_type
6780                              ,p_projfunc_rev_rate_date_typ  =>     px_projfunc_rev_rate_date_typ
6781                              ,p_project_currency_code       =>     p_project_currency_code
6782                              ,p_projfunc_currency_code      =>     p_projfunc_currency_code
6783                              ,p_txn_currency_code           =>     p_txn_currency_code
6784                              ,x_return_status               =>     x_return_status
6785                              ,x_msg_count                   =>     x_msg_count
6786                              ,x_msg_data                    =>     x_msg_data);
6787 
6788          /* Throw the error if the above API is not successfully executed */
6789          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6790 
6791             IF l_debug_mode = 'Y' THEN
6792                pa_debug.g_err_stage := 'Values for rate type and rate date types are not valied' ;
6793                pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,1);
6794             END IF;
6795             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6796 
6797          END IF;
6798     END IF;
6799 
6800     /* In the following IF conditions the set of conversion attributes to be validated are
6801        populated in to local variables. Depending on the parameter p_attrs_to_be_validated
6802        Either Cost or Revenue or Both are validatead
6803     */
6804 
6805     /* initialize any error occurred flag to N */
6806     l_any_error_occurred_flag := 'N';
6807 
6808     /* Initialize the globals to null in webadi context */
6809     IF p_context = PA_FP_CONSTANTS_PKG.G_WEBADI THEN
6810             g_first_error_code     := NULL;
6811             g_pc_pfc_context       := NULL;
6812             g_cost_rev_context     := NULL;
6813     END IF;
6814 
6815     IF(p_attrs_to_be_validated =  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST OR
6816        p_attrs_to_be_validated =  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_BOTH) THEN
6817 
6818      -- Txn curr code <> PFC
6819         --IF PFC needs to be validated THEN
6820      IF nvl(p_txn_currency_code,'-99') <> p_projfunc_currency_code THEN
6821                 /* Validate the project functional Cost attributes*/
6822                 VALIDATE_CONV_ATTRIBUTES( px_rate_type        => px_projfunc_cost_rate_type
6823                                          ,px_rate_date_type   => px_projfunc_cost_rate_date_typ
6824                                          ,px_rate_date        => px_projfunc_cost_rate_date
6825                                          ,px_rate             => px_projfunc_cost_exchange_rate
6826                                          ,p_amount_type_code  => l_cost_token
6827                                          ,p_currency_type_code=> l_projfunc_token
6828                                          ,p_calling_context   => p_context
6829                                          ,x_first_error_code  => l_first_error_code
6830                                          ,x_return_status     => x_return_status
6831                                          ,x_msg_count         => x_msg_count
6832                                          ,x_msg_data          => x_msg_data);
6833 
6834                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6835                    IF l_debug_mode = 'Y' THEN
6836                       pa_debug.g_err_stage := 'failed for PFC COST attributes' ;
6837                       pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,3);
6838                    END IF;
6839                    l_any_error_occurred_flag := 'Y';
6840 
6841                    /* webADI looks only for the first error message */
6842                    --IF p_context = PA_FP_CONSTANTS_PKG.G_WEBADI and
6843                    IF g_first_error_code IS NULL and
6844                       l_first_error_code IS NOT NULL THEN
6845 
6846                       g_first_error_code := l_first_error_code;
6847                       g_pc_pfc_context     := PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_PROJFUNC;
6848                       g_cost_rev_context   := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST;
6849 
6850                       /* No further processing is required in context of webadi */
6851                       --RETURN;
6852 
6853                    END IF;
6854                 END IF;
6855 
6856                 l_pfc_cost_validity_code := nvl(l_first_error_code,PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR);
6857 
6858         END IF;
6859 
6860         --IF PC needs to be validated THEN
6861      IF nvl(p_txn_currency_code,'-99') <> p_project_currency_code AND
6862             p_projfunc_currency_code <> p_project_currency_code THEN
6863                 /* Validate the project functional Cost attributes*/
6864                 VALIDATE_CONV_ATTRIBUTES( px_rate_type        => px_project_cost_rate_type
6865                                          ,px_rate_date_type   => px_project_cost_rate_date_typ
6866                                          ,px_rate_date        => px_project_cost_rate_date
6867                                          ,px_rate             => px_project_cost_exchange_rate
6868                                          ,p_amount_type_code  => l_cost_token
6869                                          ,p_currency_type_code=> l_project_token
6870                                          ,p_calling_context   => p_context
6871                                          ,x_first_error_code  => l_first_error_code
6872                                          ,x_return_status     => x_return_status
6873                                          ,x_msg_count         => x_msg_count
6874                                          ,x_msg_data          => x_msg_data);
6875 
6876                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6877                    IF l_debug_mode = 'Y' THEN
6878                       pa_debug.g_err_stage := 'failed for PC COST attributes' ;
6879                       pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,3);
6880                    END IF;
6881                    l_any_error_occurred_flag := 'Y';
6882 
6883                    /* webADI looks only for the first error message */
6884                    --IF p_context = PA_FP_CONSTANTS_PKG.G_WEBADI and
6885                    IF g_first_error_code IS NULL and
6886                       l_first_error_code IS NOT NULL
6887                    THEN
6888                       g_first_error_code   := l_first_error_code;
6889                       g_pc_pfc_context     := PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_PROJECT;
6890                       g_cost_rev_context   := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST;
6891 
6892                       /* No further processing is required in context of webadi */
6893                    --   RETURN;
6894                    END IF;
6895 
6896                 END IF;
6897                 l_pc_cost_validity_code := nvl(l_first_error_code,PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR);
6898         END IF;
6899 
6900         -- If PC = PFC copy the PFC attributes to PC.  WEBADI UT
6901         IF p_project_currency_code = p_projfunc_currency_code THEN
6902              px_project_cost_rate_type     := px_projfunc_cost_rate_type;
6903              px_project_cost_rate_date_typ := px_projfunc_cost_rate_date_typ;
6904              px_project_cost_rate_date     := px_projfunc_cost_rate_date;
6905              px_project_cost_exchange_rate := px_projfunc_cost_exchange_rate;
6906         END IF;
6907 
6908     END IF; -- element type cost or both.
6909 
6910     IF(p_attrs_to_be_validated =  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE OR
6911        p_attrs_to_be_validated =  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_BOTH) THEN
6912 
6913         --IF PFC needs to be validated THEN
6914      IF nvl(p_txn_currency_code,'-99') <> p_projfunc_currency_code THEN
6915                 /* Validate the project functional Cost attributes*/
6916                 VALIDATE_CONV_ATTRIBUTES( px_rate_type        => px_projfunc_rev_rate_type
6917                                          ,px_rate_date_type   => px_projfunc_rev_rate_date_typ
6918                                          ,px_rate_date        => px_projfunc_rev_rate_date
6919                                          ,px_rate             => px_projfunc_rev_exchange_rate
6920                                          ,p_amount_type_code  => l_rev_token
6921                                          ,p_currency_type_code=> l_projfunc_token
6922                                          ,p_calling_context   => p_context
6923                                          ,x_first_error_code  => l_first_error_code
6924                                          ,x_return_status     => x_return_status
6925                                          ,x_msg_count         => x_msg_count
6926                                          ,x_msg_data          => x_msg_data);
6927                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6928                    IF l_debug_mode = 'Y' THEN
6929                       pa_debug.g_err_stage := 'failed for PC COST attributes' ;
6930                       pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,3);
6931                    END IF;
6932                    l_any_error_occurred_flag := 'Y';
6933 
6934                    /* webADI looks only for the first error message */
6935                    --IF p_context = PA_FP_CONSTANTS_PKG.G_WEBADI and
6936                    IF g_first_error_code IS NULL and
6937                       l_first_error_code IS NOT NULL
6938                    THEN
6939                       g_first_error_code := l_first_error_code;
6940                       g_pc_pfc_context     := PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_PROJFUNC;
6941                       g_cost_rev_context   := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE;
6942 
6943                       /* No further processing is required in context of webadi */
6944                       --RETURN;
6945                    END IF;
6946 
6947                 END IF;
6948 
6949                 l_pfc_rev_validity_code := nvl(l_first_error_code,PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR);
6950         END IF;
6951 
6952         --IF PC needs to be validated THEN
6953      IF nvl(p_txn_currency_code,'-99') <> p_project_currency_code AND
6954            p_projfunc_currency_code <> p_project_currency_code THEN
6955                 /* Validate the project functional Cost attributes*/
6956                 VALIDATE_CONV_ATTRIBUTES( px_rate_type        => px_project_rev_rate_type
6957                                          ,px_rate_date_type   => px_project_rev_rate_date_typ
6958                                          ,px_rate_date        => px_project_rev_rate_date
6959                                          ,px_rate             => px_project_rev_exchange_rate
6960                                          ,p_amount_type_code  => l_rev_token
6961                                          ,p_currency_type_code=> l_project_token
6962                                          ,p_calling_context   => p_context
6963                                          ,x_first_error_code  => l_first_error_code
6964                                          ,x_return_status     => x_return_status
6965                                          ,x_msg_count         => x_msg_count
6966                                          ,x_msg_data          => x_msg_data);
6967 
6968                 IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6969                    IF l_debug_mode = 'Y' THEN
6970                       pa_debug.g_err_stage := 'failed for PC COST attributes' ;
6971                       pa_debug.write('validate_currency_attributes: ' || l_module_name,pa_debug.g_err_stage,3);
6972                    END IF;
6973                    l_any_error_occurred_flag := 'Y';
6974 
6975                    /* webADI looks only for the first error message */
6976                    --IF p_context = PA_FP_CONSTANTS_PKG.G_WEBADI and
6977                    IF g_first_error_code IS NULL and
6978                       l_first_error_code IS NOT NULL
6979                    THEN
6980                       g_first_error_code := l_first_error_code;
6981                       g_pc_pfc_context     := PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_PROJECT;
6982                       g_cost_rev_context   := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE;
6983 
6984                       /* No further processing is required in context of webadi */
6985                       --RETURN;
6986 
6987                    END IF;
6988 
6989                 END IF;
6990                 l_pc_rev_validity_code := nvl(l_first_error_code,PA_FP_CONSTANTS_PKG.G_VALID_CONV_ATTR);
6991         END IF;
6992 
6993         -- If PC = PFC copy the PFC attributes to PC.  WEBADI UT
6994         IF p_project_currency_code = p_projfunc_currency_code THEN
6995              px_project_rev_rate_type     := px_projfunc_rev_rate_type;
6996              px_project_rev_rate_date_typ := px_projfunc_rev_rate_date_typ;
6997              px_project_rev_rate_date     := px_projfunc_rev_rate_date;
6998              px_project_rev_exchange_rate := px_projfunc_rev_exchange_rate;
6999         END IF;
7000 
7001     END IF;
7002 
7003    /*Do the Additional validations required in the case of Create / Update plan type page*/
7004 
7005    IF l_debug_mode='Y' THEN
7006       pa_debug.g_err_stage:= 'p_context is '||p_context;
7007       pa_debug.write(l_module_name,pa_debug.g_err_stage,
7008                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7009    END IF;
7010 
7011    IF (p_context=PA_FP_CONSTANTS_PKG.G_CR_UP_PLAN_TYPE_PAGE) THEN
7012 
7013       /*Either all the cost attributes should be null or both project and project functional
7014         cost attributes should be valid
7015       */
7016 
7017       IF(l_pfc_cost_validity_code = 'PA_FP_RATE_TYPE_REQ' AND
7018          nvl(l_pc_cost_validity_code,'-99') <> 'PA_FP_RATE_TYPE_REQ'  ) THEN
7019 
7020           x_return_status := FND_API.G_RET_STS_ERROR;
7021           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
7022                                 p_msg_name      => 'PA_FP_INVALID_RATE_TYPE',
7023                                 p_token1        => 'COST_REV',
7024                                 p_value1        => l_cost_token,
7025                                 p_token2        => 'PROJECT_PROJFUNC',
7026                                 p_value2        => l_projfunc_token );
7027 
7028       ELSIF(l_pc_cost_validity_code = 'PA_FP_RATE_TYPE_REQ'  AND
7029             nvl(l_pfc_cost_validity_code,'-99') <> 'PA_FP_RATE_TYPE_REQ') THEN
7030 
7031           x_return_status := FND_API.G_RET_STS_ERROR;
7032           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
7033                                 p_msg_name      => 'PA_FP_INVALID_RATE_TYPE',
7034                                 p_token1        => 'COST_REV',
7035                                 p_value1        => l_cost_token,
7036                                 p_token2        => 'PROJECT_PROJFUNC',
7037                                 p_value2        => l_project_token );
7038       END IF;
7039 
7040       /*Either all the revenue attributes should be null or both project and project functional
7041         revene attributes should be valid
7042       */
7043 
7044       IF(l_pfc_rev_validity_code = 'PA_FP_RATE_TYPE_REQ' AND
7045          nvl(l_pc_rev_validity_code,'-99') <> 'PA_FP_RATE_TYPE_REQ'  ) THEN
7046 
7047           x_return_status := FND_API.G_RET_STS_ERROR;
7048           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
7049                                 p_msg_name      => 'PA_FP_INVALID_RATE_TYPE',
7050                                 p_token1        => 'COST_REV',
7051                                 p_value1        => l_rev_token,
7052                                 p_token2        => 'PROJECT_PROJFUNC',
7053                                 p_value2        => l_projfunc_token );
7054 
7055       ELSIF(l_pc_rev_validity_code = 'PA_FP_RATE_TYPE_REQ' AND
7056             nvl(l_pfc_rev_validity_code,'-99') <> 'PA_FP_RATE_TYPE_REQ' ) THEN
7057 
7058           x_return_status := FND_API.G_RET_STS_ERROR;
7059           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
7060                                 p_msg_name      => 'PA_FP_INVALID_RATE_TYPE',
7061                                 p_token1        => 'COST_REV',
7062                                 p_value1        => l_rev_token,
7063                                 p_token2        => 'PROJECT_PROJFUNC',
7064                                 p_value2        => l_project_token );
7065 
7066       END IF;
7067 
7068    END IF;
7069 
7070    IF l_any_error_occurred_flag = 'Y' THEN
7071       IF l_debug_mode='Y' THEN
7072           pa_debug.g_err_stage:= 'some of the conversion attributes failed.. Returning error';
7073           pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7074       END IF;
7075 
7076       x_return_status := FND_API.G_RET_STS_ERROR;
7077    END IF;
7078 
7079    IF l_debug_mode='Y' THEN
7080        pa_debug.g_err_stage:= 'Exiting validate_currency_attributes';
7081        pa_debug.write(l_module_name,pa_debug.g_err_stage,
7082                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7083 
7084    pa_debug.reset_err_stack;
7085 END IF;
7086 EXCEPTION
7087     WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7088       x_return_status := FND_API.G_RET_STS_ERROR;
7089       l_msg_count := FND_MSG_PUB.count_msg;
7090       IF l_msg_count = 1 THEN
7091              PA_INTERFACE_UTILS_PUB.get_messages
7092                  (p_encoded        => FND_API.G_TRUE,
7093                   p_msg_index      => 1,
7094                   p_msg_count      => l_msg_count,
7095                   p_msg_data       => l_msg_data,
7096                   p_data           => l_data,
7097                   p_msg_index_out  => l_msg_index_out);
7098 
7099              x_msg_data  := l_data;
7100              x_msg_count := l_msg_count;
7101       ELSE
7102              x_msg_count := l_msg_count;
7103       END IF;
7104  IF P_PA_DEBUG_MODE = 'Y' THEN
7105       pa_debug.reset_err_stack;
7106   END IF;
7107       RETURN;
7108 
7109     WHEN OTHERS THEN
7110         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7111         x_msg_count     := 1;
7112         x_msg_data      := SQLERRM;
7113 
7114         FND_MSG_PUB.add_exc_msg
7115          ( p_pkg_name       => 'PA_FIN_PLAN_UTILS'
7116           ,p_procedure_name => 'VALIDATE_CURRENCY_ATTRIBUTES'
7117           ,p_error_text     => sqlerrm);
7118 
7119         pa_debug.G_Err_Stack := SQLERRM;
7120         IF P_PA_DEBUG_MODE = 'Y' THEN
7121             pa_debug.write('CHECK_MRC_INSTALL: ' || l_module_name,pa_debug.G_Err_Stack,4);
7122         pa_debug.reset_err_stack;
7123 	END IF;
7124         RAISE;
7125 
7126 END  VALIDATE_CURRENCY_ATTRIBUTES;
7127 
7128 /*==================================================================
7129    This api retrieves the plan type id and the option for it given
7130    the plan version id. This API is included for the bug 2728552.
7131  ==================================================================*/
7132 
7133 PROCEDURE GET_PLAN_TYPE_OPTS_FOR_VER
7134    (
7135          p_plan_version_id        IN     pa_proj_fp_options.fin_plan_version_id%TYPE
7136         ,x_fin_plan_type_id      OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
7137         ,x_plan_type_option_id   OUT  NOCOPY pa_proj_fp_options.proj_fp_options_id%TYPE --File.Sql.39 bug 4440895
7138            ,x_version_type          OUT  NOCOPY pa_budget_versions.version_type%TYPE --File.Sql.39 bug 4440895
7139            ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7140            ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
7141            ,x_msg_data              OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7142 
7143    )
7144 AS
7145 
7146 l_msg_count                     NUMBER := 0;
7147 l_data                          VARCHAR2(2000);
7148 l_msg_data                      VARCHAR2(2000);
7149 l_msg_index_out                 NUMBER;
7150 l_debug_mode                    VARCHAR2(1);
7151 
7152 cursor plan_type_cur(c_version_id pa_proj_fp_options.fin_plan_version_id%TYPE) is
7153         select o.fin_plan_type_id,o.proj_fp_options_id,v.version_type
7154         from pa_proj_fp_options o,pa_budget_versions v
7155         where o.fin_plan_type_id = v.fin_plan_type_id
7156         and   o.project_id       = v.project_id
7157         and   v.budget_version_id = c_version_id
7158         and   o.fin_plan_option_level_code = 'PLAN_TYPE';
7159 
7160 BEGIN
7161 
7162       x_msg_count := 0;
7163       x_return_status := FND_API.G_RET_STS_SUCCESS;
7164       l_debug_mode := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
7165       IF l_debug_mode = 'Y' THEN
7166               pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.GET_PLAN_TYPE_OPTS_FOR_VER');
7167               pa_debug.set_process('PLSQL','LOG',l_debug_mode);
7168       END IF;
7169 
7170       -- Check for business rules violations
7171       IF l_debug_mode = 'Y' THEN
7172               pa_debug.g_err_stage:= 'Entering PA_FIN_PLAN_UTILS.GET_PLAN_TYPE_OPTS_FOR_VER';
7173               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7174                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7175       END IF;
7176 
7177       IF l_debug_mode = 'Y' THEN
7178               pa_debug.g_err_stage:= 'Validating input parameters';
7179               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7180                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7181       END IF;
7182 
7183       IF (p_plan_version_id IS NULL) THEN
7184               IF l_debug_mode = 'Y' THEN
7185                         pa_debug.g_err_stage:= 'plan version id is null';
7186                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
7187                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7188               END IF;
7189               PA_UTILS.ADD_MESSAGE
7190                       (p_app_short_name => 'PA',
7191                         p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
7192               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7193 
7194       END IF;
7195 
7196       open plan_type_cur(p_plan_version_id);
7197       fetch plan_type_cur
7198       into x_fin_plan_type_id,x_plan_type_option_id,x_version_type;
7199 
7200       IF plan_type_cur%NOTFOUND THEN
7201               IF l_debug_mode = 'Y' THEN
7202                         pa_debug.g_err_stage:= 'Plan type record not found';
7203                         pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7204               END IF;
7205            CLOSE plan_type_cur;
7206            RAISE NO_DATA_FOUND;
7207       END IF;
7208 
7209       CLOSE plan_type_cur;
7210 
7211       IF l_debug_mode = 'Y' THEN
7212              pa_debug.g_err_stage:= 'Plan type id->'||x_fin_plan_type_id;
7213              pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7214           pa_debug.g_err_stage:= 'Plan type option id->'||x_plan_type_option_id;
7215              pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7216       END IF;
7217 
7218       IF l_debug_mode = 'Y' THEN
7219               pa_debug.g_err_stage:= 'Exiting GET_PLAN_TYPE_OPTS_FOR_VER';
7220               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7221                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7222               pa_debug.reset_err_stack;
7223 
7224       END IF;
7225 
7226  EXCEPTION
7227 
7228      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7229 
7230            x_return_status := FND_API.G_RET_STS_ERROR;
7231            l_msg_count := FND_MSG_PUB.count_msg;
7232 
7233            IF l_msg_count = 1 and x_msg_data IS NULL THEN
7234                 PA_INTERFACE_UTILS_PUB.get_messages
7235                       (p_encoded        => FND_API.G_TRUE
7236                       ,p_msg_index      => 1
7237                       ,p_msg_count      => l_msg_count
7238                       ,p_msg_data       => l_msg_data
7239                       ,p_data           => l_data
7240                       ,p_msg_index_out  => l_msg_index_out);
7241                 x_msg_data := l_data;
7242                 x_msg_count := l_msg_count;
7243            ELSE
7244                 x_msg_count := l_msg_count;
7245            END IF;
7246            IF l_debug_mode = 'Y' THEN
7247                    pa_debug.reset_err_stack;
7248            END IF;
7249            RETURN;
7250 
7251    WHEN others THEN
7252 
7253           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7254           x_msg_count     := 1;
7255           x_msg_data      := SQLERRM;
7256 
7257           FND_MSG_PUB.add_exc_msg
7258                           ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
7259                            ,p_procedure_name  => 'GET_PLAN_TYPE_OPTS_FOR_VER'
7260                            ,p_error_text      => x_msg_data);
7261 
7262           IF l_debug_mode = 'Y' THEN
7263               pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
7264               pa_debug.write(L_module_name,pa_debug.g_err_stage,
7265                                      PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7266               pa_debug.reset_err_stack;
7267 
7268           END IF;
7269           RAISE;
7270 
7271 END GET_PLAN_TYPE_OPTS_FOR_VER;
7272 
7273 /*============================================================================
7274   This api is used to return the project and projfunc currency codes and the
7275   cost and bill rate types defined for them.
7276   If they aren't available at pa_projects_all table, we fetch them from the
7277   implementations table. If they are not avaialable, null would be returned.
7278  =============================================================================*/
7279 
7280 PROCEDURE Get_Project_Curr_Attributes
7281    (  p_project_id                      IN   pa_projects_all.project_id%TYPE
7282      ,x_multi_currency_billing_flag     OUT  NOCOPY pa_projects_all.multi_currency_billing_flag%TYPE --File.Sql.39 bug 4440895
7283      ,x_project_currency_code           OUT  NOCOPY pa_projects_all.project_currency_code%TYPE --File.Sql.39 bug 4440895
7284      ,x_projfunc_currency_code          OUT  NOCOPY pa_projects_all.projfunc_currency_code%TYPE --File.Sql.39 bug 4440895
7285      ,x_project_cost_rate_type          OUT  NOCOPY pa_projects_all.project_rate_type%TYPE --File.Sql.39 bug 4440895
7286      ,x_projfunc_cost_rate_type         OUT  NOCOPY pa_projects_all.projfunc_cost_rate_type%TYPE --File.Sql.39 bug 4440895
7287      ,x_project_bil_rate_type           OUT  NOCOPY pa_projects_all.project_bil_rate_type%TYPE --File.Sql.39 bug 4440895
7288      ,x_projfunc_bil_rate_type          OUT  NOCOPY pa_projects_all.projfunc_bil_rate_type%TYPE --File.Sql.39 bug 4440895
7289      ,x_return_status                   OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7290      ,x_msg_count                       OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
7291      ,x_msg_data                        OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
7292 AS
7293 
7294 l_msg_count                     NUMBER := 0;
7295 l_data                          VARCHAR2(2000);
7296 l_msg_data                      VARCHAR2(2000);
7297 l_msg_index_out                 NUMBER;
7298 
7299 BEGIN
7300       x_msg_count := 0;
7301       x_return_status := FND_API.G_RET_STS_SUCCESS;
7302 IF P_PA_DEBUG_MODE = 'Y' THEN
7303       PA_DEBUG.Set_Curr_Function( p_function   => 'Get_Project_Curr_Attributes',
7304                                   p_debug_mode => p_pa_debug_mode );
7305 END IF;
7306       -- Check for NOT NULL parameters
7307       IF (p_project_id IS NULL)
7308       THEN
7309             IF p_pa_debug_mode = 'Y' THEN
7310                 pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
7311                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7312                 pa_debug.g_err_stage:= 'Invalid Arguments Passed';
7313                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7314             END IF;
7315             PA_UTILS.ADD_MESSAGE
7316                    (p_app_short_name => 'PA',
7317                     p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
7318             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7319       END IF;
7320 
7321       -- Fetch the cost rate types for project currency and projfunc currency
7322 
7323       IF p_pa_debug_mode = 'Y' THEN
7324               pa_debug.g_err_stage:= 'Fetching cost rate types for project = '||p_project_id;
7325               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7326       END IF;
7327 
7328       SELECT  p.multi_currency_billing_flag
7329              ,p.project_currency_code
7330              ,p.projfunc_currency_code
7331              ,NVL(p.project_rate_type,i.default_rate_type)       project_cost_rate_type
7332              ,NVL(p.projfunc_cost_rate_type,i.default_rate_type) projfunc_cost_rate_type
7333              ,p.project_bil_rate_type
7334              ,p.projfunc_bil_rate_type
7335       INTO    x_multi_currency_billing_flag
7336              ,x_project_currency_code
7337              ,x_projfunc_currency_code
7338              ,x_project_cost_rate_type
7339              ,x_projfunc_cost_rate_type
7340              ,x_project_bil_rate_type
7341              ,x_projfunc_bil_rate_type
7342       FROM   pa_projects_all p
7343              ,pa_implementations_all i
7344       WHERE  p.project_id = p_project_id
7345       --AND    NVL(p.org_id,-99) = NVL(i.org_id,-99);
7346 	 AND    p.org_id = i.org_id; /* Bug 3174677: Added the NVL ,Refer to Update
7347                                                        "16-JAN-04 sagarwal" in the history above.
7348 											This has been added as part of code merge */
7349 
7350       IF p_pa_debug_mode = 'Y' THEN
7351               pa_debug.g_err_stage:= 'Exiting Get_Project_Curr_Attributes';
7352               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7353       pa_debug.reset_curr_function;
7354 	END IF;
7355   EXCEPTION
7356 
7357      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7358 
7359            x_return_status := FND_API.G_RET_STS_ERROR;
7360            l_msg_count := FND_MSG_PUB.count_msg;
7361            IF l_msg_count = 1 THEN
7362                 PA_INTERFACE_UTILS_PUB.get_messages
7363                       (p_encoded        => FND_API.G_TRUE
7364                       ,p_msg_index      => 1
7365                       ,p_msg_count      => l_msg_count
7366                       ,p_msg_data       => l_msg_data
7367                       ,p_data           => l_data
7368                       ,p_msg_index_out  => l_msg_index_out);
7369                 x_msg_data := l_data;
7370                 x_msg_count := l_msg_count;
7371            ELSE
7372                 x_msg_count := l_msg_count;
7373            END IF;
7374  IF P_PA_DEBUG_MODE = 'Y' THEN
7375            pa_debug.reset_curr_function;
7376 END IF;
7377            RAISE;
7378    WHEN others THEN
7379 
7380           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7381           x_msg_count     := 1;
7382           x_msg_data      := SQLERRM;
7383           FND_MSG_PUB.add_exc_msg
7384                           ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
7385                            ,p_procedure_name  => 'Get_Project_Curr_Attributes'
7386                            ,p_error_text      => SQLERRM);
7387           IF p_pa_debug_mode = 'Y' THEN
7388                   pa_debug.g_err_stage:= 'Unexpected Error'||SQLERRM;
7389                   pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7390           pa_debug.reset_curr_function;
7391 	END IF;
7392           RAISE;
7393 END Get_Project_Curr_Attributes;
7394 
7395 PROCEDURE IsRevVersionCreationAllowed
7396     ( p_project_id                      IN   pa_projects_all.project_id%TYPE
7397      ,p_fin_plan_type_id                IN   pa_fin_plan_types_b.fin_plan_type_id%TYPE
7398      ,x_creation_allowed                OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7399      ,x_return_status                   OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7400      ,x_msg_count                       OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
7401      ,x_msg_data                        OUT  NOCOPY VARCHAR2) AS --File.Sql.39 bug 4440895
7402 
7403 l_msg_count                     NUMBER := 0;
7404 l_data                          VARCHAR2(2000);
7405 l_msg_data                      VARCHAR2(2000);
7406 l_msg_index_out                 NUMBER;
7407 l_debug_mode                  VARCHAR2(1);
7408 
7409 /* Changes for FP.M, Tracking Bug No - 3354518
7410    Added use_for_workplan_flag column from pa_proj_fp_options
7411    in the select statment below
7412    Please note that this API will not be called for Workplan Usage*/
7413 
7414 cursor autobaseline_appr_rev_info_cur is
7415 SELECT nvl(pr.baseline_funding_flag,'N') baseline_funding_flag, pfo.approved_rev_plan_type_flag,
7416        pft.use_for_workplan_flag -- Added for FP.M ,Tracking Bug No - 3354518
7417 FROM   pa_projects_all pr, pa_proj_fp_options pfo,
7418        pa_fin_plan_types_b pft -- Added for FP.M ,Tracking Bug No - 3354518
7419 WHERE  pr.project_id = pfo.project_id
7420 AND    pfo.fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE
7421 AND    pfo.fin_plan_type_id = p_fin_plan_type_id
7422 AND    pfo.project_id = p_project_id
7423 AND    pft.fin_plan_type_id = p_fin_plan_type_id; -- Added for FP.M ,Tracking Bug No - 3354518
7424 
7425 cur_rec autobaseline_appr_rev_info_cur%ROWTYPE;
7426 
7427 BEGIN
7428 
7429       x_msg_count := 0;
7430       x_return_status := FND_API.G_RET_STS_SUCCESS;
7431       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
7432 
7433 IF P_PA_DEBUG_MODE = 'Y' THEN
7434       pa_Debug.set_curr_function( p_function   => 'IsRevVersionCreationAllowed',
7435                                   p_debug_mode => l_debug_mode );
7436 END IF;
7437       -- Check for business rules violations
7438 
7439       IF l_debug_mode = 'Y' THEN
7440               pa_debug.g_err_stage:= 'Validating input parameters';
7441               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7442                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7443       END IF;
7444 
7445       IF (p_project_id IS NULL) OR (p_fin_plan_type_id IS NULL)
7446       THEN
7447               IF l_debug_mode = 'Y' THEN
7448                         pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
7449                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
7450                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7451                         pa_debug.g_err_stage:= 'p_fin_plan_type_id = '|| p_fin_plan_type_id;
7452                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
7453                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7454               END IF;
7455               PA_UTILS.ADD_MESSAGE
7456                       (p_app_short_name => 'PA',
7457                        p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
7458               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7459 
7460       END IF;
7461 
7462       x_creation_allowed := 'Y';
7463 
7464       open autobaseline_appr_rev_info_cur;
7465       fetch autobaseline_appr_rev_info_cur into cur_rec;
7466       IF autobaseline_appr_rev_info_cur%NOTFOUND THEN
7467               RAISE NO_DATA_FOUND;
7468       END IF;
7469 
7470 /* Changes for FP.M, Tracking Bug No - 3354518
7471    Added check use_for_workplan_flag column below in the
7472    since we cannot create revenue version for a workplan type
7473       Please note that this API will not be called for Workplan Usage*/
7474 
7475       IF (cur_rec.baseline_funding_flag = 'Y' and cur_rec.approved_rev_plan_type_flag = 'Y') or (cur_rec.use_for_workplan_flag = 'Y') THEN
7476            x_creation_allowed := 'N';
7477       END IF;
7478 
7479       IF l_debug_mode = 'Y' THEN
7480               pa_debug.g_err_stage:= 'Exiting IsRevVersionCreationAllowed';
7481               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7482                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7483       pa_debug.reset_curr_function;
7484 	END IF;
7485  EXCEPTION
7486 
7487    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7488 
7489         x_return_status := FND_API.G_RET_STS_ERROR;
7490         l_msg_count := FND_MSG_PUB.count_msg;
7491 
7492         IF autobaseline_appr_rev_info_cur%ISOPEN THEN
7493             CLOSE autobaseline_appr_rev_info_cur;
7494         END IF;
7495 
7496         IF l_msg_count = 1 and x_msg_data IS NULL THEN
7497              PA_INTERFACE_UTILS_PUB.get_messages
7498                    (p_encoded        => FND_API.G_TRUE
7499                    ,p_msg_index      => 1
7500                    ,p_msg_count      => l_msg_count
7501                    ,p_msg_data       => l_msg_data
7502                    ,p_data           => l_data
7503                    ,p_msg_index_out  => l_msg_index_out);
7504              x_msg_data := l_data;
7505              x_msg_count := l_msg_count;
7506         ELSE
7507              x_msg_count := l_msg_count;
7508         END IF;
7509  IF P_PA_DEBUG_MODE = 'Y' THEN
7510         pa_debug.reset_curr_function;
7511 END IF;
7512         RETURN;
7513 
7514    WHEN others THEN
7515 
7516         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7517         x_msg_count     := 1;
7518         x_msg_data      := SQLERRM;
7519 
7520         IF autobaseline_appr_rev_info_cur%ISOPEN THEN
7521             CLOSE autobaseline_appr_rev_info_cur;
7522         END IF;
7523 
7524         FND_MSG_PUB.add_exc_msg
7525                         ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
7526                          ,p_procedure_name  => 'IsRevVersionCreationAllowed'
7527                          ,p_error_text      => x_msg_data);
7528 
7529         IF l_debug_mode = 'Y' THEN
7530             pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
7531             pa_debug.write(l_module_name,pa_debug.g_err_stage,
7532                                    PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7533         pa_debug.reset_curr_function;
7534 	END IF;
7535         RAISE;
7536 
7537 END IsRevVersionCreationAllowed;
7538 
7539 
7540 /*==================================================================
7541    This api takes the lookup type and lookup meaning as the input and
7542    returns the lookup code.
7543  ==================================================================*/
7544 
7545 PROCEDURE GET_LOOKUP_CODE
7546           (
7547                  p_lookup_type                      IN   pa_lookups.lookup_type%TYPE
7548                 ,p_lookup_meaning                   IN   pa_lookups.meaning%TYPE
7549                 ,x_lookup_code                      OUT  NOCOPY pa_lookups.lookup_code%TYPE --File.Sql.39 bug 4440895
7550                 ,x_return_status                    OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7551                 ,x_msg_count                        OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
7552                 ,x_msg_data                         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7553           )
7554 AS
7555 
7556 l_msg_count                     NUMBER := 0;
7557 l_data                          VARCHAR2(2000);
7558 l_msg_data                      VARCHAR2(2000);
7559 l_msg_index_out                 NUMBER;
7560 l_debug_mode                    VARCHAR2(1);
7561 
7562 cursor lookups_cur is
7563 select lookup_code
7564 from   pa_lookups
7565 where  lookup_type = p_lookup_type
7566 and    meaning     = p_lookup_meaning;
7567 
7568 BEGIN
7569 
7570       x_msg_count := 0;
7571       x_return_status := FND_API.G_RET_STS_SUCCESS;
7572       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
7573 
7574 IF P_PA_DEBUG_MODE = 'Y' THEN
7575       pa_debug.set_curr_function( p_function   => 'GET_LOOKUP_CODE',
7576                                   p_debug_mode => l_debug_mode );
7577 END IF;
7578       -- Check for business rules violations
7579 
7580       IF l_debug_mode = 'Y' THEN
7581               pa_debug.g_err_stage:= 'Validating input parameters';
7582               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7583                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7584       END IF;
7585 
7586       IF (p_lookup_type IS NULL) OR
7587          (p_lookup_meaning IS NULL)
7588       THEN
7589               IF l_debug_mode = 'Y' THEN
7590                         pa_debug.g_err_stage:= 'p_lookup_type = '|| p_lookup_type;
7591                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
7592                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7593                         pa_debug.g_err_stage:= 'p_lookup_meaning = '|| p_lookup_meaning;
7594                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
7595                                                  PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7596               END IF;
7597               PA_UTILS.ADD_MESSAGE
7598                       (p_app_short_name => 'PA',
7599                         p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
7600               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7601 
7602       END IF;
7603 
7604       open lookups_cur;
7605       fetch lookups_cur
7606       into x_lookup_code;
7607 
7608       IF lookups_cur%NOTFOUND THEN
7609               IF l_debug_mode = 'Y' THEN
7610                         pa_debug.g_err_stage:= 'could not obtain lookup code';
7611                         pa_debug.write(l_module_name,pa_debug.g_err_stage,
7612                                                         PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7613               END IF;
7614               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7615       END IF;
7616 
7617       close lookups_cur;
7618 
7619       IF l_debug_mode = 'Y' THEN
7620               pa_debug.g_err_stage:= 'Exiting GET_LOOKUP_CODE';
7621               pa_debug.write(l_module_name,pa_debug.g_err_stage,
7622                                          PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
7623       pa_debug.reset_curr_function;
7624 	END IF;
7625  EXCEPTION
7626 
7627    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7628 
7629         x_return_status := FND_API.G_RET_STS_ERROR;
7630         l_msg_count := FND_MSG_PUB.count_msg;
7631 
7632         IF lookups_cur%ISOPEN THEN
7633                 close lookups_cur;
7634         END IF;
7635 
7636         IF l_msg_count = 1 and x_msg_data IS NULL THEN
7637              PA_INTERFACE_UTILS_PUB.get_messages
7638                    (p_encoded        => FND_API.G_TRUE
7639                    ,p_msg_index      => 1
7640                    ,p_msg_count      => l_msg_count
7641                    ,p_msg_data       => l_msg_data
7642                    ,p_data           => l_data
7643                    ,p_msg_index_out  => l_msg_index_out);
7644              x_msg_data := l_data;
7645              x_msg_count := l_msg_count;
7646         ELSE
7647              x_msg_count := l_msg_count;
7648         END IF;
7649  IF P_PA_DEBUG_MODE = 'Y' THEN
7650         pa_debug.reset_curr_function;
7651 END IF;
7652         RETURN;
7653 
7654    WHEN others THEN
7655 
7656         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7657         x_msg_count     := 1;
7658         x_msg_data      := SQLERRM;
7659 
7660         IF lookups_cur%ISOPEN THEN
7661                 close lookups_cur;
7662         END IF;
7663 
7664         FND_MSG_PUB.add_exc_msg
7665                         ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
7666                          ,p_procedure_name  => 'GET_LOOKUP_CODE'
7667                          ,p_error_text      => x_msg_data);
7668 
7669         IF l_debug_mode = 'Y' THEN
7670             pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
7671             pa_debug.write(l_module_name,pa_debug.g_err_stage,
7672                                    PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7673         pa_debug.reset_curr_function;
7674 	END IF;
7675         RAISE;
7676 
7677 END GET_LOOKUP_CODE;
7678 
7679 FUNCTION HAS_PLANNABLE_ELEMENTS
7680          (p_budget_version_id IN   pa_budget_versions.budget_version_id%TYPE)
7681 RETURN VARCHAR2 is
7682 
7683 cursor cur_check_elements is
7684 SELECT 'Y'
7685 FROM   dual
7686 WHERE  EXISTS (SELECT 'x'
7687                FROM   pa_resource_assignments
7688                WHERE  budget_version_id = p_budget_version_id);
7689 
7690 l_exists varchar2(1) := 'N';
7691 
7692 BEGIN
7693 
7694     OPEN cur_check_elements;
7695     FETCH cur_check_elements INTO l_exists;
7696     RETURN l_exists;
7697     IF cur_check_elements%ISOPEN THEN
7698         close cur_check_elements;
7699     END IF;
7700 
7701 END HAS_PLANNABLE_ELEMENTS;
7702 
7703 --Given the project id and fin plan type id this procedure
7704 --derives the version type if it is not passed
7705 --Vesion type should be passed when the preference code of the plan type is
7706 --COST_AND_REV_SEP, else an error will be thrown
7707 PROCEDURE get_version_type
7708 ( p_project_id               IN     pa_projects_all.project_id%TYPE
7709  ,p_fin_plan_type_id         IN     pa_proj_fp_options.fin_plan_type_id%TYPE
7710  ,px_version_type            IN OUT NOCOPY pa_budget_Versions.version_type%TYPE --File.Sql.39 bug 4440895
7711  ,x_return_status            OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7712  ,x_msg_count                OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
7713  ,x_msg_data                 OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7714  )IS
7715 
7716       CURSOR  l_proj_fp_options_csr
7717       IS
7718       SELECT fin_plan_preference_code
7719       FROM   pa_proj_fp_options
7720       WHERE  project_id=p_project_id
7721       AND    fin_plan_type_id=p_fin_plan_type_id
7722       AND    fin_plan_option_level_code= PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
7723 
7724       l_proj_fp_options_rec           l_proj_fp_options_csr%ROWTYPE;
7725 
7726       l_msg_count                     NUMBER := 0;
7727       l_data                          VARCHAR2(2000);
7728       l_msg_data                      VARCHAR2(2000);
7729       l_msg_index_out                 NUMBER;
7730       l_debug_mode                    VARCHAR2(1);
7731 
7732       l_debug_level2         CONSTANT NUMBER := 2;
7733       l_debug_level3         CONSTANT NUMBER := 3;
7734       l_debug_level4         CONSTANT NUMBER := 4;
7735       l_debug_level5         CONSTANT NUMBER := 5;
7736       l_fin_plan_type_name            pa_fin_plan_types_tl.name%TYPE;
7737       l_segment1                      pa_projects_all.segment1%TYPE;
7738 
7739 BEGIN
7740 
7741       x_msg_count := 0;
7742       x_return_status := FND_API.G_RET_STS_SUCCESS;
7743       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
7744 
7745 IF P_PA_DEBUG_MODE = 'Y' THEN
7746       pa_debug.set_curr_function( p_function   => 'get_version_type',
7747                                  p_debug_mode  => l_debug_mode );
7748 END IF;
7749 
7750       BEGIN --Added for bug 4224464
7751       -- Get the name of the plan type
7752       SELECT name
7753       INTO   l_fin_plan_type_name
7754       FROM   pa_fin_plan_types_vl
7755       WHERE  fin_plan_type_id = p_fin_plan_type_id;
7756 
7757       EXCEPTION
7758 	WHEN NO_DATA_FOUND THEN   -- bug 3454650
7759 
7760        PA_UTILS.ADD_MESSAGE
7761           (p_app_short_name => 'PA',
7762            p_msg_name       => 'PA_FP_INVALID_PLAN_TYPE',
7763            p_token1         => 'PROJECT_ID',
7764            p_value1         =>  p_project_id,
7765            p_token2         => 'PLAN_TYPE_ID',
7766            p_value2         =>  p_fin_plan_type_id,
7767            p_token3         => 'VERSION_TYPE',
7768            p_value3         =>  px_version_type);
7769 
7770        x_return_status := FND_API.G_RET_STS_ERROR;
7771        RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7772 
7773       END;
7774 
7775       -- Get the segment1 of the project
7776       SELECT segment1
7777       INTO   l_segment1
7778       FROM   pa_projects_all
7779       WHERE  project_id = p_project_id ;
7780 
7781       OPEN  l_proj_fp_options_csr;
7782       FETCH l_proj_fp_options_csr
7783       INTO  l_proj_fp_options_rec;
7784 
7785       IF l_proj_fp_options_csr%NOTFOUND THEN
7786 
7787             IF l_debug_mode = 'Y' THEN
7788                   pa_debug.g_err_stage:= 'p_project_id = '|| p_project_id;
7789                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
7790                                      PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7791                   pa_debug.g_err_stage:= 'p_fin_plan_type_id = '|| p_fin_plan_type_id;
7792                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
7793                                      PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
7794             END IF;
7795 
7796             PA_UTILS.ADD_MESSAGE
7797                    (p_app_short_name => 'PA',
7798                     p_msg_name       => 'PA_FP_NO_PLAN_TYPE_OPTION',
7799                     p_token1         => 'PROJECT',
7800                     p_value1         =>  l_segment1,
7801                     p_token2         => 'PLAN_TYPE',
7802                     p_value2         =>  l_fin_plan_type_name);
7803 
7804 
7805             CLOSE l_proj_fp_options_csr;
7806             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7807 
7808       END IF;
7809       CLOSE l_proj_fp_options_csr;
7810 
7811       IF (l_proj_fp_options_rec.fin_plan_preference_code
7812                                                        =PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY) THEN
7813 
7814             IF ( px_version_type IS NULL ) THEN
7815 
7816                   px_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST ;
7817 
7818             ELSIF(px_version_type <> PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST) THEN
7819 
7820                   IF l_debug_mode = 'Y' THEN
7821                         pa_debug.g_err_stage := 'Version type passed is '||px_version_type ;
7822                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7823                   END IF;
7824 
7825                   PA_UTILS.ADD_MESSAGE
7826                      (p_app_short_name => 'PA',
7827                       p_msg_name       => 'PA_FP_INVALID_VERSION_TYPE',
7828                       p_token1         => 'PROJECT',
7829                       p_value1         =>  l_segment1,
7830                       p_token2         => 'PLAN_TYPE',
7831                       p_value2         =>  l_fin_plan_type_name,
7832                       p_token3         => 'VERSION_TYPE',
7833                       p_value3         =>  px_version_type);
7834 
7835                   x_return_status := FND_API.G_RET_STS_ERROR;
7836                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7837 
7838             END IF;
7839 
7840       ELSIF (l_proj_fp_options_rec.fin_plan_preference_code
7841                                          =PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY) THEN
7842 
7843             IF ( px_version_type IS NULL) THEN
7844 
7845                   px_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE ;
7846 
7847             ELSIF(px_version_type <> PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE) THEN
7848 
7849                   IF l_debug_mode = 'Y' THEN
7850                         pa_debug.g_err_stage := 'Version type passed is '||px_version_type ;
7851                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7852                   END IF;
7853 
7854                   PA_UTILS.ADD_MESSAGE
7855                      (p_app_short_name => 'PA',
7856                       p_msg_name       => 'PA_FP_INVALID_VERSION_TYPE',
7857                       p_token1         => 'PROJECT',
7858                       p_value1         =>  l_segment1,
7859                       p_token2         => 'PLAN_TYPE',
7860                       p_value2         =>  l_fin_plan_type_name,
7861                       p_token3         => 'VERSION_TYPE',
7862                       p_value3         =>  px_version_type);
7863                   x_return_status := FND_API.G_RET_STS_ERROR;
7864                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7865 
7866             END IF;
7867 
7868       ELSIF (l_proj_fp_options_rec.fin_plan_preference_code
7869                                    =PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME) THEN
7870 
7871             IF ( px_version_type IS NULL ) THEN
7872 
7873                   px_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL ;
7874 
7875             ELSIF(px_version_type <> PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL) THEN
7876 
7877                   IF l_debug_mode = 'Y' THEN
7878                         pa_debug.g_err_stage := 'Version type passed is '||px_version_type ;
7879                         pa_debug.write(l_module_name,pa_debug.g_err_stage, l_debug_level3);
7880                   END IF;
7881 
7882                   PA_UTILS.ADD_MESSAGE
7883                      (p_app_short_name => 'PA',
7884                       p_msg_name       => 'PA_FP_INVALID_VERSION_TYPE',
7885                       p_token1         => 'PROJECT',
7886                       p_value1         =>  l_segment1,
7887                       p_token2         => 'PLAN_TYPE',
7888                       p_value2         =>  l_fin_plan_type_name,
7889                       p_token3         => 'VERSION_TYPE',
7890                       p_value3         =>  px_version_type);
7891 
7892                   x_return_status := FND_API.G_RET_STS_ERROR;
7893                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7894 
7895             END IF;
7896 
7897       ELSIF (l_proj_fp_options_rec.fin_plan_preference_code
7898                                  =PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) THEN
7899 
7900             IF( px_version_type IS NULL) THEN
7901 
7902                   IF l_debug_mode = 'Y' THEN
7903                          pa_debug.g_err_stage := 'Version type passed is null' ;
7904                          pa_debug.write( l_module_name,pa_debug.g_err_stage, l_debug_level3);
7905                   END IF;
7906 
7907                   PA_UTILS.ADD_MESSAGE
7908                      (p_app_short_name => 'PA',
7909                       p_msg_name       => 'PA_FP_VERSION_NULL_AT_CRS_PT',
7910                       p_token1         => 'PROJECT',
7911                       p_value1         =>  l_segment1,
7912                       p_token2         => 'PLAN_TYPE',
7913                       p_value2         =>  l_fin_plan_type_name);
7914                   x_return_status := FND_API.G_RET_STS_ERROR;
7915                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7916 
7917             ELSIF( px_version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST
7918                   OR px_version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE) THEN
7919 
7920                       px_version_type:= px_version_type ;
7921             ELSE-- version type is neither COST nor REVENUE
7922 
7923                   IF l_debug_mode = 'Y' THEN
7924                         pa_debug.g_err_stage := 'Version type passed is '||px_version_type ;
7925                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
7926                   END IF;
7927 
7928                   PA_UTILS.ADD_MESSAGE
7929                      (p_app_short_name => 'PA',
7930                       p_msg_name       => 'PA_FP_INVALID_VERSION_TYPE',
7931                       p_token1         => 'PROJECT',
7932                       p_value1         =>  l_segment1,
7933                       p_token2         => 'PLAN_TYPE',
7934                       p_value2         =>  l_fin_plan_type_name,
7935                       p_token3         => 'VERSION_TYPE',
7936                       p_value3         =>  px_version_type);
7937 
7938                   x_return_status := FND_API.G_RET_STS_ERROR;
7939                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7940 
7941             END IF;
7942 
7943       END IF;--Version type derivation ends
7944 
7945       IF l_debug_mode = 'Y' THEN
7946             pa_debug.g_err_stage:= 'Leaving get version type';
7947             pa_debug.write(l_module_name,pa_debug.g_err_stage,
7948                               l_debug_level3);
7949 
7950       pa_debug.reset_curr_function;
7951 	END IF;
7952 EXCEPTION
7953 
7954 WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7955 
7956       IF x_return_status IS NULL OR
7957          x_return_status = FND_API.G_RET_STS_SUCCESS THEN
7958            x_return_status := FND_API.G_RET_STS_ERROR;
7959       END IF;
7960       l_msg_count := FND_MSG_PUB.count_msg;
7961 
7962       IF l_msg_count = 1 and x_msg_data IS NULL THEN
7963           PA_INTERFACE_UTILS_PUB.get_messages
7964               (p_encoded        => FND_API.G_TRUE
7965               ,p_msg_index      => 1
7966               ,p_msg_count      => l_msg_count
7967               ,p_msg_data       => l_msg_data
7968               ,p_data           => l_data
7969               ,p_msg_index_out  => l_msg_index_out);
7970           x_msg_data := l_data;
7971           x_msg_count := l_msg_count;
7972       ELSE
7973           x_msg_count := l_msg_count;
7974       END IF;
7975  IF P_PA_DEBUG_MODE = 'Y' THEN
7976       pa_debug.reset_curr_function;
7977 END IF;
7978       RETURN;
7979 
7980 WHEN others THEN
7981 
7982       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7983       x_msg_count     := 1;
7984       x_msg_data      := SQLERRM;
7985 
7986       FND_MSG_PUB.add_exc_msg
7987                    ( p_pkg_name        => 'pa_fin_plan_utils'
7988                     ,p_procedure_name  => 'get_Version_type'
7989                     ,p_error_text      => x_msg_data);
7990 
7991       IF l_debug_mode = 'Y' THEN
7992           pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
7993           pa_debug.write(l_module_name,pa_debug.g_err_stage,
7994                               l_debug_level5);
7995       pa_debug.reset_curr_function;
7996       END IF;
7997       RAISE;
7998 END get_version_type;
7999 
8000 
8001 -- This procedure returns the budget version id
8002 -- given the project id ,plan type id, version type and version number
8003 -- If found returns valid budget version id
8004 -- Null is returned other wise
8005 PROCEDURE get_version_id
8006 ( p_project_id               IN   pa_projects_all.project_id%TYPE
8007  ,p_fin_plan_type_id         IN   pa_proj_fp_options.fin_plan_type_id%TYPE
8008  ,p_version_type             IN   pa_budget_Versions.version_type%TYPE
8009  ,p_version_number           IN   pa_budget_Versions.version_number%TYPE
8010  ,x_budget_version_id        OUT  NOCOPY pa_budget_Versions.budget_version_id%TYPE --File.Sql.39 bug 4440895
8011  ,x_ci_id                    OUT  NOCOPY pa_budget_Versions.ci_id%TYPE --File.Sql.39 bug 4440895
8012  ,x_return_status            OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8013  ,x_msg_count                OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
8014  ,x_msg_data                 OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8015  )IS
8016 
8017       l_msg_count                     NUMBER := 0;
8018       l_data                          VARCHAR2(2000);
8019       l_msg_data                      VARCHAR2(2000);
8020       l_msg_index_out                 NUMBER;
8021       l_debug_mode                    VARCHAR2(1);
8022 
8023       l_debug_level3                  CONSTANT NUMBER := 3;
8024       l_debug_level5                  CONSTANT NUMBER := 5;
8025       l_module                        VARCHAR2(100) := l_module_name || 'get_version_id';
8026 
8027       CURSOR l_budget_version_id_csr
8028       IS
8029       SELECT budget_version_id,
8030              ci_id
8031       FROM   pa_budget_versions
8032       WHERE  project_id=p_project_id
8033       AND    fin_plan_type_id=p_fin_plan_type_id
8034       AND    version_type=p_version_type
8035       AND    version_number=p_version_number
8036       AND    budget_status_code='W';
8037 
8038 BEGIN
8039 
8040       x_msg_count := 0;
8041       x_return_status := FND_API.G_RET_STS_SUCCESS;
8042       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
8043 
8044 IF P_PA_DEBUG_MODE = 'Y' THEN
8045       pa_debug.set_curr_function( p_function   => 'get_version_id',p_debug_mode => l_debug_mode );
8046 END IF;
8047       -- Check for business rules violations
8048 
8049       IF l_debug_mode = 'Y' THEN
8050           pa_debug.g_err_stage:= 'Validating input parameters';
8051           pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8052       END IF;
8053 
8054       IF (p_project_id        IS NULL OR
8055           p_fin_plan_type_id  IS NULL OR
8056           p_version_type      IS NULL OR
8057           p_version_number    IS NULL ) THEN
8058 
8059             IF l_debug_mode = 'Y' THEN
8060                   pa_debug.g_err_stage:= 'Project Id is ' || p_project_id;
8061                   pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8062             END IF;
8063 
8064             IF l_debug_mode = 'Y' THEN
8065                   pa_debug.g_err_stage:= 'p_fin_plan_type_id is '||p_fin_plan_type_id;
8066                   pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8067             END IF;
8068 
8069             IF l_debug_mode = 'Y' THEN
8070                   pa_debug.g_err_stage:= 'p_version_type is '||p_version_type;
8071                   pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8072             END IF;
8073 
8074             IF l_debug_mode = 'Y' THEN
8075                   pa_debug.g_err_stage:= 'p_version_number is ' ||p_version_number;
8076                   pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8077             END IF;
8078 
8079             PA_UTILS.ADD_MESSAGE
8080                 (p_app_short_name => 'PA',
8081                    p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
8082             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8083 
8084       END IF;
8085 
8086       OPEN  l_budget_version_id_csr ;
8087       FETCH l_budget_version_id_csr INTO x_budget_version_id,x_ci_id;
8088       CLOSE l_budget_version_id_csr;
8089 
8090       IF l_debug_mode = 'Y' THEN
8091             pa_debug.g_err_stage:= 'Exiting get_version_id';
8092             pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8093             pa_debug.reset_curr_function;
8094       END IF;
8095 
8096 EXCEPTION
8097 WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8098 
8099      x_return_status := FND_API.G_RET_STS_ERROR;
8100      l_msg_count := FND_MSG_PUB.count_msg;
8101 
8102      IF l_msg_count = 1 and x_msg_data IS NULL THEN
8103           PA_INTERFACE_UTILS_PUB.get_messages
8104               (p_encoded        => FND_API.G_TRUE
8105               ,p_msg_index      => 1
8106               ,p_msg_count      => l_msg_count
8107               ,p_msg_data       => l_msg_data
8108               ,p_data           => l_data
8109               ,p_msg_index_out  => l_msg_index_out);
8110           x_msg_data := l_data;
8111           x_msg_count := l_msg_count;
8112      ELSE
8113           x_msg_count := l_msg_count;
8114      END IF;
8115  IF P_PA_DEBUG_MODE = 'Y' THEN
8116      pa_debug.reset_curr_function;
8117 END IF;
8118      RETURN;
8119 
8120 WHEN others THEN
8121 
8122      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8123      x_msg_count     := 1;
8124      x_msg_data      := SQLERRM;
8125 
8126      FND_MSG_PUB.add_exc_msg
8127                    ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
8128                     ,p_procedure_name  => 'get_version_id'
8129                     ,p_error_text      => x_msg_data);
8130 
8131      IF l_debug_mode = 'Y' THEN
8132           pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
8133           pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level5);
8134      pa_debug.reset_curr_function;
8135      END IF;
8136      RAISE;
8137 END get_version_id;
8138 
8139 -- This procedure accepts budget version id and checks the follwing
8140 -- 1.is the project enabled for auto baselining
8141 -- 2. is the version approved for revenue and is the version type revenue
8142 -- If both 1 and 2 are met it returns F for the parameter x_result
8143 -- T is returned otherise
8144 
8145 PROCEDURE perform_autobasline_checks
8146 ( p_budget_version_id     IN  pa_budget_versions.budget_version_id%TYPE
8147  ,x_result                OUT NOCOPY VARCHAR2     --File.Sql.39 bug 4440895
8148  ,x_return_status         OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8149  ,x_msg_count             OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
8150  ,x_msg_data              OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
8151 IS
8152       l_msg_count                     NUMBER := 0;
8153       l_data                          VARCHAR2(2000);
8154       l_msg_data                      VARCHAR2(2000);
8155       l_msg_index_out                 NUMBER;
8156       l_debug_mode                    VARCHAR2(1);
8157 
8158       l_debug_level3                  CONSTANT NUMBER := 3;
8159       l_debug_level5                  CONSTANT NUMBER := 5;
8160       l_module                        VARCHAR2(100) := l_module_name || 'get_version_id';
8161 
8162       CURSOR l_autobaseline_check_csr
8163       IS
8164       SELECT  pbv.budget_type_code
8165              ,pbv.fin_plan_type_id
8166              ,pbv.version_type
8167              ,pfo.approved_rev_plan_type_flag
8168              ,p.baseline_funding_flag
8169       FROM    pa_budget_versions pbv
8170              ,pa_proj_fp_options pfo
8171              ,pa_projects_all p
8172       WHERE   pbv.budget_version_id=p_budget_version_id
8173       AND     pfo.fin_plan_version_id(+)=pbv.budget_version_id
8174       AND     p.project_id=pbv.project_id;
8175 
8176       l_autobaseline_check_rec        l_autobaseline_check_csr%ROWTYPE;
8177 BEGIN
8178 
8179       x_msg_count := 0;
8180       x_return_status := FND_API.G_RET_STS_SUCCESS;
8181       x_result:='T';
8182       l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
8183 
8184 
8185 IF l_debug_mode = 'Y' THEN
8186       pa_debug.set_curr_function( p_function   => 'perform_autobasline_checks',p_debug_mode => l_debug_mode );
8187           pa_debug.g_err_stage:= 'Validating input parameters';
8188           pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8189       END IF;
8190 
8191       IF (p_budget_version_id  IS NULL ) THEN
8192 
8193             IF l_debug_mode = 'Y' THEN
8194                   pa_debug.g_err_stage:= 'p_budget_version_id is ' ||p_budget_version_id;
8195                   pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8196             END IF;
8197 
8198             PA_UTILS.ADD_MESSAGE
8199                 (p_app_short_name => 'PA',
8200                    p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
8201             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8202 
8203       END IF;
8204 
8205       OPEN  l_autobaseline_check_csr;
8206       FETCH l_autobaseline_check_csr INTO l_autobaseline_check_rec;
8207       IF (l_autobaseline_check_csr%NOTFOUND) THEN
8208 
8209             IF l_debug_mode = 'Y' THEN
8210                   pa_debug.g_err_stage:= 'p_budget_version_id is ' || p_budget_version_id;
8211                   pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8212             END IF;
8213             CLOSE l_autobaseline_check_csr;
8214             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8215 
8216       END IF;
8217       CLOSE l_autobaseline_check_csr;
8218 
8219       IF (l_autobaseline_check_rec.baseline_funding_flag='Y')  THEN
8220             IF nvl( l_autobaseline_check_rec.budget_type_code,'N')=
8221                                PA_FP_CONSTANTS_PKG.G_BUDGET_TYPE_CODE_AR   THEN
8222 
8223                   IF l_debug_mode = 'Y' THEN
8224                         pa_debug.g_err_stage := 'Auto base line error in budget model' ;
8225                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8226                   END IF;
8227 
8228                   x_result := 'F';
8229 
8230             ELSIF  (l_autobaseline_check_rec.budget_type_code IS NULL AND
8231                     l_autobaseline_check_rec.approved_rev_plan_type_flag = 'Y' AND
8232                     l_autobaseline_check_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE) THEN
8233 
8234                   IF l_debug_mode = 'Y' THEN
8235                         pa_debug.g_err_stage := 'Auto base line error in finplan model' ;
8236                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8237                   END IF;
8238 
8239                   x_result := 'F';
8240 
8241             END IF;
8242 
8243       END IF;
8244 
8245       IF l_debug_mode = 'Y' THEN
8246             pa_debug.g_err_stage:= 'Exiting perform_autobasline_checks ';
8247             pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level3);
8248       pa_debug.reset_curr_function;
8249       END IF;
8250 EXCEPTION
8251 WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8252 
8253      x_return_status := FND_API.G_RET_STS_ERROR;
8254      l_msg_count := FND_MSG_PUB.count_msg;
8255 
8256      IF l_msg_count = 1 and x_msg_data IS NULL THEN
8257           PA_INTERFACE_UTILS_PUB.get_messages
8258               (p_encoded        => FND_API.G_TRUE
8259               ,p_msg_index      => 1
8260               ,p_msg_count      => l_msg_count
8261               ,p_msg_data       => l_msg_data
8262               ,p_data           => l_data
8263               ,p_msg_index_out  => l_msg_index_out);
8264           x_msg_data := l_data;
8265           x_msg_count := l_msg_count;
8266      ELSE
8267           x_msg_count := l_msg_count;
8268      END IF;
8269  IF P_PA_DEBUG_MODE = 'Y' THEN
8270      pa_debug.reset_curr_function;
8271 END IF;
8272      RETURN;
8273 
8274 WHEN others THEN
8275 
8276      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8277      x_msg_count     := 1;
8278      x_msg_data      := SQLERRM;
8279 
8280      FND_MSG_PUB.add_exc_msg
8281                    ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
8282                     ,p_procedure_name  => 'perform_autobasline_checks'
8283                     ,p_error_text      => x_msg_data);
8284 
8285      IF l_debug_mode = 'Y' THEN
8286           pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
8287           pa_debug.write(l_module,pa_debug.g_err_stage,l_debug_level5);
8288      pa_debug.reset_curr_function;
8289 	END IF;
8290      RAISE;
8291 END perform_autobasline_checks;
8292 
8293 /*==================================================================
8294    This api derives verion type for a given budget type code.
8295    If the budget amount code is 'c' , it returns 'COST'
8296    If the budget amount code is 'R' , it returns 'REVENUE'
8297    If the budget amount code is 'ALL' , it returns 'ALL'
8298  ==================================================================*/
8299 
8300 PROCEDURE get_version_type_for_bdgt_type
8301    (  p_budget_type_code      IN   pa_budget_versions.budget_type_code%TYPE
8302      ,x_version_type          OUT  NOCOPY pa_budget_versions.version_type%TYPE --File.Sql.39 bug 4440895
8303      ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8304      ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
8305      ,x_msg_data              OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
8306 AS
8307 
8308 l_msg_count                     NUMBER := 0;
8309 l_data                          VARCHAR2(2000);
8310 l_msg_data                      VARCHAR2(2000);
8311 l_msg_index_out                 NUMBER;
8312 l_return_status                 VARCHAR2(2000);
8313 
8314 BEGIN
8315       x_msg_count := 0;
8316       x_return_status := FND_API.G_RET_STS_SUCCESS;
8317 IF p_pa_debug_mode = 'Y' THEN
8318       PA_DEBUG.Set_Curr_Function( p_function   => 'Get_version_type_for_bdgt_type',
8319                                   p_debug_mode => p_pa_debug_mode );
8320 END IF;
8321       -- Check for NOT NULL parameters
8322       IF (p_budget_type_code IS NULL)
8323       THEN
8324             IF p_pa_debug_mode = 'Y' THEN
8325                 pa_debug.g_err_stage:= 'p_budget_type_code = '|| p_budget_type_code;
8326                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8327             END IF;
8328             PA_UTILS.ADD_MESSAGE
8329                    (p_app_short_name => 'PA',
8330                     p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
8331             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8332       END IF;
8333 
8334       -- derive version type using the budget amount code
8335 
8336       BEGIN
8337            SELECT DECODE(budget_amount_code, 'C',   PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_COST,
8338                                              'R',   PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_REVENUE,
8339                                              'ALL', PA_FP_CONSTANTS_PKG.G_VERSION_TYPE_ALL)
8340            INTO   x_version_type
8341            FROM   pa_budget_types
8342            WHERE  budget_type_code = p_budget_type_code;
8343       EXCEPTION
8344            WHEN NO_DATA_FOUND THEN
8345                 IF p_pa_debug_mode = 'Y' THEN
8346                      pa_debug.g_err_stage:= 'no data found error in get_version_type_for_bdgt_type'||SQLERRM;
8347                      pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8348                 END IF;
8349 
8350                 PA_UTILS.ADD_MESSAGE(
8351                        p_app_short_name  => 'PA'
8352                       ,p_msg_name        => 'PA_BUDGET_TYPE_IS_INVALID' );
8353 
8354                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8355       END;
8356 
8357       IF p_pa_debug_mode = 'Y' THEN
8358               pa_debug.g_err_stage:= 'Exiting get_version_type_for_bdgt_type';
8359               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8360       pa_debug.reset_curr_function;
8361 	END IF;
8362   EXCEPTION
8363 
8364      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8365 
8366            x_return_status := FND_API.G_RET_STS_ERROR;
8367            l_msg_count := FND_MSG_PUB.count_msg;
8368            IF l_msg_count = 1 THEN
8369                 PA_INTERFACE_UTILS_PUB.get_messages
8370                       (p_encoded        => FND_API.G_TRUE
8371                       ,p_msg_index      => 1
8372                       ,p_msg_count      => l_msg_count
8373                       ,p_msg_data       => l_msg_data
8374                       ,p_data           => l_data
8375                       ,p_msg_index_out  => l_msg_index_out);
8376                 x_msg_data := l_data;
8377                 x_msg_count := l_msg_count;
8378            ELSE
8379                 x_msg_count := l_msg_count;
8380            END IF;
8381 	 IF P_PA_DEBUG_MODE = 'Y' THEN
8382            pa_debug.reset_curr_function;
8383 	END IF;
8384            RETURN;
8385    WHEN others THEN
8386 
8387           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8388           x_msg_count     := 1;
8389           x_msg_data      := SQLERRM;
8390           FND_MSG_PUB.add_exc_msg
8391                           ( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
8392                            ,p_procedure_name  => 'get_version_type_for_bdgt_type'
8393                            ,p_error_text      => sqlerrm);
8394           IF p_pa_debug_mode = 'Y' THEN
8395                pa_debug.g_err_stage:= 'unexp error in get_version_type_for_bdgt_type'||SQLERRM;
8396                pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8397           pa_debug.reset_curr_function;
8398 	END IF;
8399           RAISE;
8400 END get_version_type_for_bdgt_type;
8401 
8402 --
8403 --    03-JUN-03 jwhite   Bug 2955756 (Merged from branch 85, Post-K Rollup QA)
8404 --                       For the validate_editable_bv procedure,  there appeared
8405 --                       to be three issues:
8406 --
8407 --                       1) For one OA session, when the user submits a plan, the
8408 --                          message stack count is incremented by one.
8409 --
8410 --                          Therefore, removed logic assuming one one message in
8411 --                          stack.
8412 --
8413 --                       2) When user encounters properly rendered submit error from
8414 --                          this api and then changes the plan back to working
8415 --                          and then logs out and logs back in as a different
8416 --                          user to test second user-locked error in this api, the previous
8417 --                          submit error improperly reappears.
8418 --
8419 --                          FND_MSG_PUB.initialize seemed to address this issue.
8420 --
8421 --                       3) Sometimes, but not always, passing G_TRUE for the
8422 --                          non-encoded (no-token) submit message appeared to
8423 --                          result in a "null" being rendered on the attachments
8424 --                          page.
8425 --
8426 --                          For the submit error, pass p_encoded as G_FALSE.
8427 --
8428 --
8429 --     22-JUL-03 jwhite   Bug 3057564
8430 --                        For the validate_editable_bv procedure,
8431 --                        add new logic and message code for plan
8432 --                        version locked by a process.
8433 
8434 --     03-Nov-03 dbora    Bug 3986129: FP.M Web ADI Dev changes made to call this
8435 --                        for Web ADI related validations as well.
8436 --                        new parameter p_context is added with default value of
8437 --                        'ATTACHEMENT'. Other valid value is 'WEBADI'.
8438 --
8439 /*  PROCEDURE validate_editable_bv
8440  *  This procedure tests whether a budget version is editable.  It is editable if all
8441  *  of the following are true:
8442  *  1. The budget version is not in a Submitted status
8443  *  2. The budget version is unlocked, or locked by the logged-in user
8444  *  If a budget version is NOT editable, the an error message will be added to the stack:
8445  *  PA_FP_ATTACH_SUBMITTED - if version is in a Submitted status
8446  *  PA_FP_ATTACH_LOCKED_BY_USER - if version is locked by another user
8447  *
8448  *  USED BY:
8449  *  - Attachments for Financial Planning (Post-K one-off)
8450  *  - pa_fp_webadi_pkg.validate_header_info (FP.M)
8451  */
8452 PROCEDURE validate_editable_bv
8453     (p_budget_version_id     IN  pa_budget_versions.budget_version_id%TYPE,
8454      p_user_id               IN  NUMBER,
8455 
8456      --Bug 3986129: FP.M Web ADI Dev changes, a new parameter added
8457      p_context               IN  VARCHAR2,
8458      p_excel_calling_mode    IN  VARCHAR2,
8459      x_locked_by_person_id   OUT NOCOPY pa_budget_versions.locked_by_person_id%TYPE, --File.Sql.39 bug 4440895
8460      x_err_code              OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
8461      x_return_status         OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
8462      x_msg_count             OUT NOCOPY NUMBER,   --File.Sql.39 bug 4440895
8463      x_msg_data              OUT NOCOPY VARCHAR2) is --File.Sql.39 bug 4440895
8464 
8465 l_person_id                   NUMBER(15);
8466 l_resource_id                 NUMBER(15);
8467 l_resource_name               VARCHAR2(240);
8468 
8469 l_budget_status_code          pa_budget_versions.budget_status_code%TYPE;
8470 l_locked_by_person_id         pa_budget_versions.locked_by_person_id%TYPE;
8471 l_locked_by_name              VARCHAR2(240);
8472 
8473 -- error-handling variables
8474 l_msg_count               NUMBER(15);
8475 l_msg_data                VARCHAR2(2000);
8476 l_data                    VARCHAR2(2000);
8477 l_msg_index_out           NUMBER(15);
8478 
8479    -- Bug 3057564, jwhite, 22-JUL-03 -----------------------------
8480    l_request_id                pa_budget_versions.request_id%TYPE;
8481 
8482 -- Bug 3986129: FP.M Web ADI Dev. Added the followings
8483 l_edit_after_baseline_flag        pa_fin_plan_types_b.edit_after_baseline_flag%TYPE;
8484 l_project_id                      pa_projects_all.project_id%TYPE;
8485 l_fin_plan_type_id                pa_fin_plan_types_b.fin_plan_type_id%TYPE;
8486 l_version_type                    pa_budget_versions.version_type%TYPE;
8487 is_edit_allowed                   VARCHAR2(1) := 'Y';
8488 
8489 l_debug_mode                 VARCHAR2(1);
8490 l_debug_level3               CONSTANT NUMBER := 3;
8491 l_module_name                VARCHAR2(100) := 'validate_editable_bv' ;
8492 l_plan_processing_code       pa_budget_versions.plan_processing_code%TYPE;
8493 
8494 BEGIN
8495 
8496   fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
8497 
8498   FND_MSG_PUB.initialize;
8499 
8500   IF l_debug_mode = 'Y' THEN
8501        pa_debug.Set_Curr_Function
8502              ( p_function    => l_module_name,
8503                p_debug_mode  => l_debug_mode);
8504   END IF;
8505   IF l_debug_mode = 'Y' THEN
8506        pa_debug.g_err_stage:='Entering validate_editable_bv';
8507        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8508        pa_debug.g_err_stage:='p_context passed: ' || p_context;
8509        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8510   END IF;
8511 
8512   x_return_status := FND_API.G_RET_STS_SUCCESS;
8513 
8514   -- get the person_id: used for locking checks
8515   PA_COMP_PROFILE_PUB.GET_USER_INFO
8516           (p_user_id         => p_user_id,
8517            x_person_id       => l_person_id,
8518            x_resource_id     => l_resource_id,
8519            x_resource_name   => l_resource_name);
8520 
8521     SELECT budget_status_code,
8522            locked_by_person_id,
8523            request_id,           -- Bug 3057564
8524            plan_processing_code
8525     INTO   l_budget_status_code,
8526            x_locked_by_person_id,
8527            l_request_id,         -- Bug 3057564
8528            l_plan_processing_code
8529     FROM   pa_budget_versions
8530     WHERE  budget_version_id = p_budget_version_id;
8531 
8532   if l_budget_status_code = 'S' then
8533         if p_context <> 'WEBADI' then
8534              IF p_context <> PA_FP_CONSTANTS_PKG.G_AMG_API THEN
8535 
8536                  x_err_code := 'PA_FP_ATTACH_SUBMITTED';
8537 
8538                  PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
8539                                       p_msg_name            => 'PA_FP_ATTACH_SUBMITTED');
8540 
8541                  l_msg_count := FND_MSG_PUB.Count_Msg;
8542 
8543                   -- bug 2955756 --------------------------------------
8544 
8545                   PA_INTERFACE_UTILS_PUB.get_messages
8546                        (p_encoded        => FND_API.G_FALSE
8547                         ,p_msg_index      => 1
8548                         ,p_msg_count      => l_msg_count
8549                         ,p_msg_data       => l_msg_data
8550                         ,p_data           => l_data
8551                         ,p_msg_index_out  => l_msg_index_out);
8552 
8553                   x_msg_data := l_data;
8554                   x_msg_count := l_msg_count;
8555 
8556                   -- ---------------------------------------------------
8557 
8558 
8559                  /*
8560                   if l_msg_count = 1 then
8561                       PA_INTERFACE_UTILS_PUB.get_messages
8562                              (p_encoded        => FND_API.G_TRUE
8563                              ,p_msg_index      => 1
8564                              ,p_msg_count      => l_msg_count
8565                              ,p_msg_data       => l_msg_data
8566                              ,p_data           => l_data
8567                              ,p_msg_index_out  => l_msg_index_out);
8568                       x_msg_data := l_data;
8569                       x_msg_count := l_msg_count;
8570                   else
8571                       x_msg_count := l_msg_count;
8572                   end if;
8573                 */
8574 
8575                 /*
8576                    if x_msg_count = 1 then
8577                       PA_INTERFACE_UTILS_PUB.get_messages
8578                              (p_encoded        => FND_API.G_TRUE,
8579                               p_msg_index      => 1,
8580                               p_data           => x_msg_data,
8581                               p_msg_index_out  => l_msg_index_out);
8582                    end if;
8583                 */
8584                 RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8585             ELSE
8586                 -- AMG context
8587                 x_err_code := 'PA_FP_SUBMITTED_VERSION';
8588 
8589                  PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
8590                                       p_msg_name            => 'PA_FP_SUBMITTED_VERSION');
8591 
8592                  l_msg_count := FND_MSG_PUB.Count_Msg;
8593 
8594                   PA_INTERFACE_UTILS_PUB.get_messages
8595                        (p_encoded        => FND_API.G_FALSE
8596                         ,p_msg_index      => 1
8597                         ,p_msg_count      => l_msg_count
8598                         ,p_msg_data       => l_msg_data
8599                         ,p_data           => l_data
8600                         ,p_msg_index_out  => l_msg_index_out);
8601 
8602                   x_msg_data := l_data;
8603                   x_msg_count := l_msg_count;
8604 
8605                   RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8606             END IF;
8607        ELSE -- p_context = 'WEBADI'
8608               IF l_debug_mode = 'Y' THEN
8609                    pa_debug.g_err_stage:='Web ADI: Submitted Error';
8610                    pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8611                    pa_debug.g_err_stage:='Populating Error Flag - Code';
8612                    pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8613               END IF;
8614 
8615               x_err_code := 'PA_FP_WA_BV_SUBMITTED_ERR';
8616 
8617               RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8618        end if; -- p_context
8619   end if; -- version submitted
8620 
8621    -- Bug 3057564, jwhite, 22-JUL-03 -----------------------------
8622    -- Add logic for plan version locked by a process
8623 
8624    IF ( ( nvl( x_locked_by_person_id,0) = -98 )
8625        AND ( l_request_id is NOT NULL )    ) THEN
8626         IF p_context <> PA_FP_CONSTANTS_PKG.G_AMG_API THEN
8627             x_err_code := 'PA_FP_ATTACH_LOCKED_BY_PRC';
8628 
8629             PA_UTILS.ADD_MESSAGE
8630                 ( p_app_short_name => 'PA',
8631                   p_msg_name       => 'PA_FP_ATTACH_LOCKED_BY_PRC');
8632 
8633             l_msg_count := FND_MSG_PUB.Count_Msg;
8634 
8635             PA_INTERFACE_UTILS_PUB.get_messages
8636                  (p_encoded        => FND_API.G_FALSE
8637                   ,p_msg_index      => 1
8638                   ,p_msg_count      => l_msg_count
8639                   ,p_msg_data       => l_msg_data
8640                   ,p_data           => l_data
8641                   ,p_msg_index_out  => l_msg_index_out);
8642 
8643             x_msg_data := l_data;
8644             x_msg_count := l_msg_count;
8645 
8646             RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8647         ELSE
8648             -- AMG context
8649             x_err_code := 'PA_FP_LOCKED_BY_PROCESSING';
8650 
8651             PA_UTILS.ADD_MESSAGE
8652                 ( p_app_short_name => 'PA',
8653                   p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
8654 
8655             l_msg_count := FND_MSG_PUB.Count_Msg;
8656 
8657             PA_INTERFACE_UTILS_PUB.get_messages
8658                  (p_encoded        => FND_API.G_FALSE
8659                   ,p_msg_index      => 1
8660                   ,p_msg_count      => l_msg_count
8661                   ,p_msg_data       => l_msg_data
8662                   ,p_data           => l_data
8663                   ,p_msg_index_out  => l_msg_index_out);
8664 
8665             x_msg_data := l_data;
8666             x_msg_count := l_msg_count;
8667 
8668             RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8669         END IF;
8670    END IF; -- locked by process
8671 
8672    -- End Bug 3057564 --------------------------------------------
8673 
8674   -- checking plan_processing_code for webadi context
8675   IF p_context = 'WEBADI' THEN
8676       IF p_excel_calling_mode = 'STANDARD' THEN
8677           IF l_plan_processing_code = 'XLUP' OR
8678              l_plan_processing_code = 'XLUE' THEN
8679                  -- the version is locked for processing or the version
8680                  -- has some processing errors.
8681                  IF l_debug_mode = 'Y' THEN
8682                      pa_debug.g_err_stage:='Web ADI: Process Locked Error';
8683                      pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8684                      pa_debug.g_err_stage:='Populating Error Flag - Code';
8685                      pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8686                  END IF;
8687 
8688                x_err_code := 'PA_FP_WA_BV_LOCKED_PRC_ERR';
8689 
8690                RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8691           END IF;
8692       END IF;
8693   END IF;
8694 
8695   if x_locked_by_person_id is not null  then
8696     if l_person_id <> x_locked_by_person_id then
8697 
8698         if p_context <> 'WEBADI' then
8699             IF p_context <> PA_FP_CONSTANTS_PKG.G_AMG_API THEN
8700                 x_err_code := 'PA_FP_ATTACH_LOCKED_BY_USER';
8701                 -- BUG FIX 2933867: use locked_by_person_id for error msg
8702                 l_locked_by_name := pa_fin_plan_utils.get_person_name(x_locked_by_person_id);
8703                 PA_UTILS.ADD_MESSAGE
8704                   ( p_app_short_name => 'PA',
8705                     p_msg_name       => 'PA_FP_ATTACH_LOCKED_BY_USER',
8706                     p_token1         => 'PERSON_NAME',
8707                     p_value1         => l_locked_by_name);
8708                 /*
8709                  PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
8710                                       p_msg_name            => 'PA_FP_ATTACH_LOCKED_BY_USER');
8711                 */
8712 
8713                 l_msg_count := FND_MSG_PUB.Count_Msg;
8714 
8715 
8716                 -- bug 2955756 --------------------------------------
8717 
8718                 PA_INTERFACE_UTILS_PUB.get_messages
8719                        (p_encoded        => FND_API.G_TRUE
8720                         ,p_msg_index      => 1
8721                         ,p_msg_count      => l_msg_count
8722                         ,p_msg_data       => l_msg_data
8723                         ,p_data           => l_data
8724                         ,p_msg_index_out  => l_msg_index_out);
8725 
8726                 x_msg_data := l_data;
8727                 x_msg_count := l_msg_count;
8728 
8729                 -- ---------------------------------------------------
8730 
8731                 /*
8732                    if l_msg_count = 1 then
8733                      PA_INTERFACE_UTILS_PUB.get_messages
8734                        (p_encoded        => FND_API.G_TRUE
8735                         ,p_msg_index      => 1
8736                         ,p_msg_count      => l_msg_count
8737                         ,p_msg_data       => l_msg_data
8738                         ,p_data           => l_data
8739                         ,p_msg_index_out  => l_msg_index_out);
8740                     x_msg_data := l_data;
8741                     x_msg_count := l_msg_count;
8742                    else
8743                     x_msg_count := l_msg_count;
8744                    end if;
8745                 */
8746 
8747                 /*
8748                    x_msg_count := FND_MSG_PUB.Count_Msg;
8749                    if x_msg_count = 1 then
8750                       PA_INTERFACE_UTILS_PUB.get_messages
8751                          (p_encoded        => FND_API.G_TRUE,
8752                           p_msg_index      => 1,
8753                           p_data           => x_msg_data,
8754                           p_msg_index_out  => l_msg_index_out);
8755                    end if;
8756                 */
8757                 RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8758             ELSE
8759                 -- AMG context
8760                 x_err_code := 'PA_FP_LOCKED_BY_USER';
8761 
8762                 PA_UTILS.ADD_MESSAGE
8763                     ( p_app_short_name => 'PA',
8764                       p_msg_name       => 'PA_FP_LOCKED_BY_USER');
8765 
8766                 l_msg_count := FND_MSG_PUB.Count_Msg;
8767 
8768                 PA_INTERFACE_UTILS_PUB.get_messages
8769                      (p_encoded        => FND_API.G_FALSE
8770                       ,p_msg_index      => 1
8771                       ,p_msg_count      => l_msg_count
8772                       ,p_msg_data       => l_msg_data
8773                       ,p_data           => l_data
8774                       ,p_msg_index_out  => l_msg_index_out);
8775 
8776                 x_msg_data := l_data;
8777                 x_msg_count := l_msg_count;
8778 
8779                 RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8780             END IF;
8781         ELSE -- p_context = 'WEBADI'
8782                IF l_debug_mode = 'Y' THEN
8783                      pa_debug.g_err_stage:='Web ADI: BV Locked Error';
8784                      pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8785                      pa_debug.g_err_stage:='Populating Error Flag - Code';
8786                      pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8787                END IF;
8788 
8789                x_err_code := 'PA_FP_WA_BV_LOCKED_ERR';
8790 
8791                RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8792         end if; -- p_context
8793     end if;  -- version locked by another user
8794   end if;
8795 
8796   -- Bug 3986129: FP.M Web ADI Dev. Added additional check for 'allow_edit_after_baseline flag
8797   IF l_debug_mode = 'Y' THEN
8798        pa_debug.g_err_stage:='Validating for Allow Edit after Baseline';
8799        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8800   END IF;
8801 
8802   BEGIN
8803        SELECT Nvl(fpt.edit_after_baseline_flag, 'N'),
8804               bv.project_id,
8805               bv.fin_plan_type_id,
8806               bv.version_type
8807        INTO   l_edit_after_baseline_flag,
8808               l_project_id,
8809               l_fin_plan_type_id,
8810               l_version_type
8811        FROM   pa_fin_plan_types_b fpt,
8812               pa_budget_versions  bv
8813        WHERE  bv.budget_version_id = p_budget_version_id
8814        AND    bv.fin_plan_type_id = fpt.fin_plan_type_id;
8815 
8816    EXCEPTION
8817          WHEN NO_DATA_FOUND THEN
8818               IF l_debug_mode = 'Y' THEN
8819                  pa_debug.g_err_stage:='No data found for edit after baseline flag';
8820                  pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8821               END IF;
8822               RAISE;
8823          WHEN OTHERS THEN
8824                   x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8825                   x_msg_count     := 1;
8826                   x_msg_data      := SQLERRM;
8827                   FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'pa_fin_plan_utils',
8828                                            p_procedure_name   => 'validate_editable_bv');
8829  IF P_PA_DEBUG_MODE = 'Y' THEN
8830                   pa_debug.reset_err_stack;
8831   END IF;
8832    END;
8833 
8834    IF l_edit_after_baseline_flag = 'N' THEN
8835          -- a singular select to check if there is any already baselined version
8836          -- for that plan type and version type
8837          -- is_edit_allowed flag is defaulted to 'Y'
8838 
8839          BEGIN
8840               SELECT 'N'
8841               INTO   is_edit_allowed
8842               FROM   DUAL
8843               WHERE  EXISTS ( SELECT 'X'
8844                               FROM   pa_budget_versions a
8845                               WHERE  a.project_id = l_project_id
8846                               AND    a.fin_plan_type_id = l_fin_plan_type_id
8847                               AND    a.version_type = l_version_type
8848                               AND    a.budget_status_code = 'B');
8849          EXCEPTION
8850                WHEN NO_DATA_FOUND THEN
8851                     is_edit_allowed := 'Y';
8852          END;
8853 
8854          IF is_edit_allowed = 'N' THEN
8855                IF p_context <> 'WEBADI' THEN
8856                     -- use the messages for attachment
8857                     x_err_code := 'PA_FP_PLAN_TYPE_NON_EDITABLE';
8858 
8859                     PA_UTILS.ADD_MESSAGE
8860                     ( p_app_short_name => 'PA',
8861                       p_msg_name       => 'PA_FP_PLAN_TYPE_NON_EDITABLE');
8862 
8863                     l_msg_count := FND_MSG_PUB.Count_Msg;
8864 
8865                     PA_INTERFACE_UTILS_PUB.get_messages
8866                           (p_encoded        => FND_API.G_TRUE
8867                           ,p_msg_index      => 1
8868                           ,p_msg_count      => l_msg_count
8869                           ,p_msg_data       => l_msg_data
8870                           ,p_data           => l_data
8871                           ,p_msg_index_out  => l_msg_index_out);
8872 
8873                     x_msg_data := l_data;
8874                     x_msg_count := l_msg_count;
8875 
8876                     RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8877                ELSE -- p_context = 'WEBADI'
8878                     IF l_debug_mode = 'Y' THEN
8879                         pa_debug.g_err_stage:='Web ADI: BV Non Edit Error';
8880                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8881                         pa_debug.g_err_stage:='Populating Error Flag - Code';
8882                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8883                     END IF;
8884                     -- Use new messages for the context of WEBADI
8885                     x_err_code := 'PA_FP_WA_BV_BL_NON_EDIT';
8886 
8887                     RAISE PA_FP_CONSTANTS_PKG.Just_Ret_Exc;
8888                END IF; -- p_context
8889          END IF;
8890    END IF; -- edit_after_baseline
8891 
8892   IF l_debug_mode = 'Y' THEN
8893        pa_debug.g_err_stage:='Leaving validate_editable_bv';
8894        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
8895        pa_debug.Reset_Curr_Function;
8896   END IF;
8897 
8898 EXCEPTION
8899   WHEN PA_FP_CONSTANTS_PKG.Just_Ret_Exc THEN
8900       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8901 
8902       IF l_debug_mode = 'Y' THEN
8903             pa_debug.Reset_Curr_Function;
8904       END IF;
8905       RETURN;
8906 
8907   WHEN NO_DATA_FOUND THEN
8908       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8909       x_msg_count     := 1;
8910       x_msg_data      := SQLERRM;
8911       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'pa_fin_plan_utils',
8912                                p_procedure_name   => 'validate_editable_bv');
8913  IF P_PA_DEBUG_MODE = 'Y' THEN
8914       pa_debug.reset_err_stack;
8915  END IF;
8916       raise FND_API.G_EXC_UNEXPECTED_ERROR;
8917   WHEN OTHERS THEN
8918       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8919       x_msg_count     := 1;
8920       x_msg_data      := SQLERRM;
8921       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'pa_fin_plan_utils',
8922                                p_procedure_name   => 'validate_editable_bv');
8923  IF P_PA_DEBUG_MODE = 'Y' THEN
8924       pa_debug.reset_err_stack;
8925   END IF;
8926       RAISE;
8927 END validate_editable_bv;
8928 
8929 /* Bug 2920954 - This is a new API that does the processing necessary to check if a
8930    task can be deleted from old budgets model, organization forecasting, and new
8931    Budgeting and Forecasting perspective. For old budgets model and organization
8932    forecasting, presence of a task in resource assignments table implies that amounts
8933    exist for the task and so the task can not be deleted. For financial planning model,
8934    since records exists in pa_resource_assignments even when no budget lines exists,
8935    pa_fp_elements table has to be verified to check if plan amounts exist for a task.
8936    If p_validation_mode is U,
8937      p_task_id should not be present in BASELINED versions and should not be present in
8938      other versions with amounts
8939    If p_validation_mode is R,
8940      p_task_id should not be present in any version.
8941    Bug 2993894, in Restricted mode deletion of a task is not allowed if the task is
8942    referenced for any of the options(project/plan type/ plan version) in pa_fp_elements table.
8943  */
8944 
8945 PROCEDURE check_delete_task_ok
8946      ( /* p_task_id               IN   pa_tasks.task_id%TYPE Commenting out NOCOPY for to replace  --File.Sql.39 bug 4440895
8947      pa_tasks by PA_STRUCT_TASK_WBS_V as part of FP.M, Tracking Bug No - 3354518 */
8948      p_task_id               IN   pa_struct_task_wbs_v.task_id%TYPE
8949      ,p_validation_mode       IN   VARCHAR2
8950      ,x_return_status         OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8951      ,x_msg_count             OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
8952      ,x_msg_data              OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
8953 
8954 AS
8955 
8956 l_msg_count                     NUMBER := 0;
8957 l_data                          VARCHAR2(2000);
8958 l_msg_data                      VARCHAR2(2000);
8959 l_msg_index_out                 NUMBER;
8960 l_debug_mode                    VARCHAR2(1);
8961 
8962 l_debug_level2                  CONSTANT NUMBER := 2;
8963 l_debug_level3                  CONSTANT NUMBER := 3;
8964 l_debug_level4                  CONSTANT NUMBER := 4;
8965 l_debug_level5                  CONSTANT NUMBER := 5;
8966 
8967 l_validation_success            VARCHAR2(1) := 'Y'; /* Y when delete is allowed, N when delete is not allowed */
8968 
8969 /* Changes for FP.M, Tracking Bug No - 3354518
8970 Replacing all references of PA_TASKS by PA_STRUCT_TASK_WBS_V
8971 and of PA_FP_ELEMNTS by PA_RESOURCE_ASSIGNMENTS
8972 as PA_FP_ELEMENTS is being obsoleted*/
8973 
8974 
8975 CURSOR delete_task_R_mode_cur IS
8976 SELECT 'N' validation_success  /* If cursor returns a record, deletion is not allowed */
8977 FROM   DUAL
8978 WHERE  EXISTS (
8979  /*  Commenting out as part of FP.M, Tracking Bug No - 3354518
8980      Since We will now check the existence of a budget version
8981      (having wp_version_flag = 'N') using pa_budget_version and
8982      pa_resource_assignments */
8983 
8984      /*    SELECT 1
8985                  FROM   pa_fp_elements fe */
8986                         /* Bug 2993894 ,pa_budget_versions bv */ -- Commenting out code for FP.M, Tracking Bug No - 3354518
8987         /*         WHERE  fe.task_id IN
8988                            (SELECT pt.task_id
8989                             FROM  PA_TASKS pt */ -- Commenting out code for FP.M, Tracking Bug No - 3354518
8990                    /* pa_tasks pt Commenting out for to replace pa_tasks by PA_STRUCT_TASK_WBS_V
8991                              as part of FP.M, Tracking Bug No - 3354518 */
8992      /*            CONNECT BY PRIOR pt.task_id = pt.parent_task_id
8993                             START WITH pt.task_id = p_task_id)*/ -- Commenting out code for FP.M, Tracking Bug No - 3354518
8994                  /* Bug 2993894 AND    bv.budget_version_id = fe.fin_plan_version_id */
8995        /*        UNION ALL  */ -- Commenting out code for FP.M, Tracking Bug No - 3354518
8996                  SELECT 1
8997                  FROM   pa_resource_assignments r,
8998                         pa_budget_versions bv
8999                  WHERE  r.task_id IN
9000                           (SELECT pt.task_id  /*Changing refernece of pa_struct_task_wbs_v below to pa_tasks*/
9001                            FROM   PA_TASKS pt /*Reverting changes for FPM, view pa_struct_task_wbs_v cannot be used in connect by clause*/
9002                   CONNECT BY PRIOR pt.task_id = pt.parent_task_id
9003                            START WITH pt.task_id = p_task_id)
9004                  AND    bv.budget_version_id = r.budget_version_id
9005                  AND    nvl(bv.wp_version_flag,'N') = 'N'); -- Added for FP.M, Tracking Bug No - 3354518
9006 -- Commenting Out code Below for FOM DEv Changes -- Bug 3640517 -- Starts
9007 /*                 AND    (bv.budget_type_code IS NOT NULL
9008                          OR
9009                          bv.fin_plan_type_id IN (SELECT fpt.fin_plan_type_id
9010                                                   FROM   pa_fin_plan_types_b fpt
9011                                                   WHERE  fpt.fin_plan_type_code = 'ORG_FORECAST')));
9012 */
9013 -- Commenting Out code Above for FPM DEv Changes -- Bug 3640517 -- Ends
9014 
9015 /* Commenting out cursor for FPM chnages - Tracking bug - 3354518 */
9016 /* The above cursor delete_task_R_mode_cur shall be used for both restricted as well as unrestructed mode */
9017 /*
9018 CURSOR delete_task_U_mode_cur IS
9019 SELECT 'N' validation_success -- If cursor returns a record, deletion is not allowed
9020 FROM   DUAL
9021 WHERE  EXISTS (
9022                  SELECT 1
9023                  FROM    -- pa_fp_elements fe Commenting out for to replace pa_fp_elements by PA_RESOURCE_ASSIGNMENTS as part of FP.M, Tracking Bug No - 3354518
9024                pa_resource_assignments fe,
9025                         pa_budget_versions bv
9026                  WHERE  fe.task_id IN
9027                            (SELECT pt.task_id
9028                             FROM   PA_STRUCT_TASK_WBS_V pt
9029                    -- pa_tasks pt Commenting out for to replace pa_tasks by PA_STRUCT_TASK_WBS_V as part of FP.M, Tracking Bug No - 3354518
9030                    CONNECT BY PRIOR pt.task_id = pt.parent_task_id
9031                             START WITH pt.task_id = p_task_id)
9032                  -- AND    bv.budget_version_id = fe.fin_plan_version_id
9033                  -- Part of Changes for FP.M, Tracking Bug No - 3354518 , replace fin_plan_version_id by budget_version_id
9034                   AND    bv.budget_version_id = fe.budget_version_id
9035                  AND    (fe.plan_amount_exists_flag = 'Y' OR bv.budget_status_code = 'B')
9036            AND     nvl(bv.wp_version_flag,'N') = 'N' -- Added for FP.M, Tracking Bug No - 3354518
9037                  UNION ALL
9038                  SELECT 1
9039                  FROM   pa_resource_assignments r,
9040                         pa_budget_versions bv
9041                  WHERE  r.task_id IN
9042                           (SELECT pt.task_id
9043                            FROM    PA_STRUCT_TASK_WBS_V pt
9044                   -- pa_tasks pt Commenting out for to replace pa_tasks by PA_STRUCT_TASK_WBS_V as part of FP.M, Tracking Bug No - 3354518
9045                CONNECT BY PRIOR pt.task_id = pt.parent_task_id
9046                            START WITH pt.task_id = p_task_id)
9047                  AND    bv.budget_version_id = r.budget_version_id
9048            AND     nvl(bv.wp_version_flag,'N') = 'N' -- Added for FP.M, Tracking Bug No - 3354518
9049                  AND    (bv.budget_type_code IS NOT NULL
9050                          OR
9051                          bv.fin_plan_type_id IN (SELECT fpt.fin_plan_type_id
9052                                                   FROM   pa_fin_plan_types_b fpt
9053                                                   WHERE  fpt.fin_plan_type_code = 'ORG_FORECAST')));
9054 */
9055 
9056 
9057 
9058 BEGIN
9059 
9060      x_msg_count := 0;
9061      x_return_status := FND_API.G_RET_STS_SUCCESS;
9062      l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
9063  IF l_debug_mode = 'Y' THEN
9064      pa_debug.set_curr_function( p_function   => 'check_delete_task_ok',
9065                                  p_debug_mode => l_debug_mode );
9066 
9067      -- Check for business rules violations
9068           pa_debug.g_err_stage:= 'Validating input parameters';
9069           pa_debug.write(l_module_name,pa_debug.g_err_stage,
9070                                      l_debug_level3);
9071      END IF;
9072 
9073      IF (p_task_id IS NULL)
9074      THEN
9075           IF l_debug_mode = 'Y' THEN
9076                   pa_debug.g_err_stage:= 'p_task_id = '|| p_task_id;
9077                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
9078                                            l_debug_level5);
9079                   pa_debug.g_err_stage:= 'p_validation_mode = '|| p_validation_mode;
9080                   pa_debug.write(l_module_name,pa_debug.g_err_stage,
9081                                            l_debug_level5);
9082           END IF;
9083           PA_UTILS.ADD_MESSAGE
9084                 (p_app_short_name => 'PA',
9085                   p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
9086           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9087 
9088      END IF;
9089 
9090      IF l_debug_mode = 'Y' THEN
9091              pa_debug.g_err_stage:= 'p_validation_mode = '|| p_validation_mode;
9092              pa_debug.write(l_module_name,pa_debug.g_err_stage,
9093                                       l_debug_level5);
9094      END IF;
9095 
9096 
9097 /* Commenting out code below for FPM chnages - Tracking bug - 3354518 */
9098 /* The  cursor delete_task_R_mode_cur shall be used for both restricted as well as unrestructed mode */
9099 
9100 /*
9101 IF p_validation_mode = 'U' THEN
9102 
9103           OPEN delete_task_U_mode_cur;
9104           FETCH delete_task_U_mode_cur into l_validation_success;
9105           CLOSE delete_task_U_mode_cur;
9106 
9107      ELSIF p_validation_mode = 'R' THEN
9108 
9109           OPEN delete_task_R_mode_cur;
9110           FETCH delete_task_R_mode_cur into l_validation_success;
9111           CLOSE delete_task_R_mode_cur;
9112 
9113      ELSE
9114 
9115           Raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9116 END IF;
9117 */
9118 
9119 
9120 IF ((p_validation_mode = 'U') OR ( p_validation_mode = 'R' )) THEN
9121 
9122           OPEN delete_task_R_mode_cur;
9123           FETCH delete_task_R_mode_cur into l_validation_success;
9124           CLOSE delete_task_R_mode_cur;
9125 ELSE
9126 
9127           Raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9128 END IF;
9129 
9130 
9131 
9132      IF l_debug_mode = 'Y' THEN
9133              pa_debug.g_err_stage:= 'l_validation_success = '|| l_validation_success;
9134              pa_debug.write(l_module_name,pa_debug.g_err_stage,
9135                                       l_debug_level5);
9136      END IF;
9137 
9138      IF l_validation_success = 'N' THEN
9139           PA_UTILS.ADD_MESSAGE
9140                 (p_app_short_name => 'PA',
9141                   p_msg_name      => 'PA_TSK_BUDGET_EXIST');
9142           RAISE FND_API.G_Exc_Error;
9143      END IF;
9144 
9145      IF l_debug_mode = 'Y' THEN
9146           pa_debug.g_err_stage:= 'Exiting check_delete_task_ok';
9147           pa_debug.write(l_module_name,pa_debug.g_err_stage,
9148                                    l_debug_level3);
9149      pa_debug.reset_curr_function;
9150 	END IF;
9151 EXCEPTION
9152 
9153 WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
9154 
9155      x_return_status := FND_API.G_RET_STS_ERROR;
9156      l_msg_count := FND_MSG_PUB.count_msg;
9157 
9158      IF l_msg_count = 1 and x_msg_data IS NULL THEN
9159           PA_INTERFACE_UTILS_PUB.get_messages
9160               (p_encoded        => FND_API.G_TRUE
9161               ,p_msg_index      => 1
9162               ,p_msg_count      => l_msg_count
9163               ,p_msg_data       => l_msg_data
9164               ,p_data           => l_data
9165               ,p_msg_index_out  => l_msg_index_out);
9166           x_msg_data := l_data;
9167           x_msg_count := l_msg_count;
9168      ELSE
9169           x_msg_count := l_msg_count;
9170      END IF;
9171  IF P_PA_DEBUG_MODE = 'Y' THEN
9172      pa_debug.reset_curr_function;
9173 END IF;
9174      RETURN;
9175 
9176 WHEN FND_API.G_Exc_Error THEN
9177 
9178      x_return_status := FND_API.G_RET_STS_ERROR;
9179      l_msg_count := FND_MSG_PUB.count_msg;
9180 
9181      IF l_msg_count = 1 and x_msg_data IS NULL THEN
9182           PA_INTERFACE_UTILS_PUB.get_messages
9183               (p_encoded        => FND_API.G_TRUE
9184               ,p_msg_index      => 1
9185               ,p_msg_count      => l_msg_count
9186               ,p_msg_data       => l_msg_data
9187               ,p_data           => l_data
9188               ,p_msg_index_out  => l_msg_index_out);
9189           x_msg_data := l_data;
9190           x_msg_count := l_msg_count;
9191      ELSE
9192           x_msg_count := l_msg_count;
9193      END IF;
9194  IF P_PA_DEBUG_MODE = 'Y' THEN
9195      pa_debug.reset_curr_function;
9196 END IF;
9197      RETURN;
9198 
9199 WHEN others THEN
9200 
9201      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9202      x_msg_count     := 1;
9203      x_msg_data      := SQLERRM;
9204 
9205      FND_MSG_PUB.add_exc_msg
9206                    ( p_pkg_name        => 'pa_fin_plan_utils'
9207                     ,p_procedure_name  => 'check_delete_task_ok'
9208                     ,p_error_text      => x_msg_data);
9209 
9210      IF l_debug_mode = 'Y' THEN
9211           pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
9212           pa_debug.write(l_module_name,pa_debug.g_err_stage,
9213                               l_debug_level5);
9214      pa_debug.reset_curr_function;
9215 	END IF;
9216      RAISE;
9217 END check_delete_task_ok;
9218 
9219 PROCEDURE check_reparent_task_ok
9220      (p_task_id               IN   pa_tasks.task_id%TYPE
9221      ,p_old_parent_task_id    IN   pa_tasks.task_id%TYPE
9222      ,p_new_parent_task_id    IN   pa_tasks.task_id%TYPE
9223      ,p_validation_mode       IN   VARCHAR2
9224      ,x_return_status        OUT   NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
9225      ,x_msg_count            OUT   NOCOPY NUMBER --File.Sql.39 bug 4440895
9226      ,x_msg_data             OUT   NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
9227 AS
9228 
9229 -- validation variables
9230 l_is_parent_lowest_task       NUMBER(1);
9231 l_parent_top_task_id          pa_tasks.top_task_id%TYPE;
9232 l_task_name                   pa_tasks.task_name%TYPE;
9233 l_old_parent_task_name        pa_tasks.task_name%TYPE;
9234 l_new_parent_task_name        pa_tasks.task_name%TYPE;
9235 
9236 -- debugging variables
9237 l_module_name                 VARCHAR2(100) := 'check_reparent_task_ok';
9238 l_return_status               VARCHAR2(1);
9239 l_msg_count                   NUMBER := 0;
9240 l_data                        VARCHAR2(2000);
9241 l_msg_data                    VARCHAR2(2000);
9242 l_msg_index_out               NUMBER;
9243 l_debug_mode                   VARCHAR2(1);
9244 
9245 cursor task_ra_csr (tid  pa_tasks.task_id%TYPE) is
9246 select 1
9247   from pa_resource_assignments
9248   where task_id = tid;
9249 task_ra_rec task_ra_csr%ROWTYPE;
9250 
9251 BEGIN
9252      x_msg_count := 0;
9253      x_return_status := FND_API.G_RET_STS_SUCCESS;
9254      l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
9255  IF l_debug_mode = 'Y' THEN
9256      pa_debug.set_curr_function( p_function   => 'check_reparent_task_ok',
9257                                  p_debug_mode => l_debug_mode );
9258      -- check for business rules violations
9259 
9260           pa_debug.g_err_stage:= 'Validating input parameters';
9261           pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
9262      END IF;
9263 
9264      IF (p_task_id IS NULL) or (p_old_parent_task_id is NULL) or (p_new_parent_task_id is NULL) THEN
9265           IF l_debug_mode = 'Y' THEN
9266                   pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
9267           END IF;
9268           PA_UTILS.ADD_MESSAGE
9269                 (p_app_short_name => 'PA',
9270                  p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
9271           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9272      END IF;
9273      -- end of business rules violations check
9274 
9275      IF (p_old_parent_task_id = p_new_parent_task_id) THEN
9276          -- not really a re-parenting procedure, so just return successfully
9277  IF P_PA_DEBUG_MODE = 'Y' THEN
9278       pa_debug.reset_curr_function;
9279 END IF;
9280          return;
9281      END IF; -- old parent = new parent
9282 
9283      -- VALIDATION: Affected task
9284      pa_fin_plan_utils.check_delete_task_ok
9285         (p_task_id               => p_task_id,
9286       p_validation_mode   => p_validation_mode,
9287 --         x_delete_task_ok_flag   => l_delete_task_ok_flag,
9288            x_return_status     => l_return_status,
9289            x_msg_count         => l_msg_count,
9290            x_msg_data          => l_msg_data);
9291      IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
9292          x_return_status := FND_API.G_RET_STS_ERROR;
9293       x_msg_count := l_msg_count;
9294       x_msg_data := l_msg_data;
9295 /* NO NEED TO ADD ERROR MESSAGE TO STACK:
9296    Justification: Error message will be added by check_delete_task_ok
9297          PA_UTILS.ADD_MESSAGE
9298              ( p_app_short_name => 'PA',
9299                p_msg_name       => 'PA_FP_REPARENT_ERR_TASK',
9300                p_token1         => 'TASK_NAME',
9301                p_value1         => l_task_name);
9302          l_msg_count := FND_MSG_PUB.count_msg;
9303          IF l_msg_count = 1 and x_msg_data IS NULL THEN
9304               PA_INTERFACE_UTILS_PUB.get_messages
9305                   (p_encoded        => FND_API.G_TRUE,
9306                    p_msg_index      => 1,
9307                    p_msg_count      => l_msg_count,
9308                    p_msg_data       => l_msg_data,
9309                    p_data           => l_data,
9310                    p_msg_index_out  => l_msg_index_out);
9311               x_msg_data := l_data;
9312               x_msg_count := l_msg_count;
9313          ELSE
9314               x_msg_count := l_msg_count;
9315          END IF;
9316 */
9317  IF P_PA_DEBUG_MODE = 'Y' THEN
9318       pa_debug.reset_curr_function;
9319 END IF;
9320          return;
9321      END IF; -- validation: affected task
9322 
9323 /*   UPDATE FROM VEJAYARA:
9324      Validation done for old_parent_task_id is not required. This is because,
9325      we dont expect to have any lines in RA table for parent_task_id when
9326      amounts dont exists for the impacted_Task_id (checked by the call to
9327      check_delete_task_ok). If there are other lowest tasks with amounts, the
9328      old_parent_task_id will have a record in RA table and it is ok to have
9329      that record. Presence of that record is not business violation for reparenting.
9330 
9331      -- VALIDATION: Old parent task
9332      open task_ra_csr(p_old_parent_task_id);
9333      fetch task_ra_csr into task_ra_rec;
9334      IF task_ra_csr%FOUND then
9335          -- records in pa_resource_assignments: VALIDATION FAILED
9336          x_return_status := FND_API.G_RET_STS_ERROR;
9337          BEGIN
9338            select task_name
9339              into l_task_name
9340              from pa_tasks
9341              where task_id = p_task_id;
9342            select task_name
9343              into l_old_parent_task_name
9344              from pa_tasks
9345              where task_id = p_old_parent_task_id;
9346          EXCEPTION
9347            when NO_DATA_FOUND then
9348                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9349                PA_UTILS.ADD_MESSAGE
9350                      (p_app_short_name => 'PA',
9351                       p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
9352                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9353          END;
9354          PA_UTILS.ADD_MESSAGE
9355              ( p_app_short_name => 'PA',
9356                p_msg_name       => 'PA_FP_REPARENT_ERR_OLDPRT',
9357                p_token1         => 'TASK_NAME',
9358                p_value1         => l_task_name,
9359                p_token2         => 'PARENT_TASK_NAME',
9360                p_value2         => l_old_parent_task_name);
9361          l_msg_count := FND_MSG_PUB.count_msg;
9362          IF l_msg_count = 1 and x_msg_data IS NULL THEN
9363               PA_INTERFACE_UTILS_PUB.get_messages
9364                   (p_encoded        => FND_API.G_TRUE,
9365                    p_msg_index      => 1,
9366                    p_msg_count      => l_msg_count,
9367                    p_msg_data       => l_msg_data,
9368                    p_data           => l_data,
9369                    p_msg_index_out  => l_msg_index_out);
9370               x_msg_data := l_data;
9371               x_msg_count := l_msg_count;
9372          ELSE
9373               x_msg_count := l_msg_count;
9374          END IF;
9375          close task_ra_csr;
9376  IF P_PA_DEBUG_MODE = 'Y' THEN
9377       pa_debug.reset_curr_function;
9378 END IF;
9379          return;
9380      END IF;
9381      close task_ra_csr;
9382 */
9383 
9384      -- VALIDATION: New parent task
9385      select top_task_id
9386        into l_parent_top_task_id
9387        from pa_tasks
9388        where task_id = p_new_parent_task_id;
9389      l_is_parent_lowest_task := pa_budget_utils2.check_task_lowest_in_budgets
9390             (x_task_id            => p_new_parent_task_id,
9391              x_top_task_id        => l_parent_top_task_id,
9392              x_validation_mode    => p_validation_mode);
9393      IF (l_is_parent_lowest_task = 1) THEN
9394          -- new parent task is a plannable lowest task: VALIDATION FAILURE
9395          x_return_status := FND_API.G_RET_STS_ERROR;
9396          BEGIN
9397            select task_name
9398              into l_task_name
9399              from pa_tasks
9400              where task_id = p_task_id;
9401            select task_name
9402              into l_new_parent_task_name
9403              from pa_tasks
9404              where task_id = p_new_parent_task_id;
9405          EXCEPTION
9406            when NO_DATA_FOUND then
9407                x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9408                PA_UTILS.ADD_MESSAGE
9409                      (p_app_short_name => 'PA',
9410                       p_msg_name     => 'PA_FP_INV_PARAM_PASSED');
9411                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9412          END;
9413          PA_UTILS.ADD_MESSAGE
9414              ( p_app_short_name => 'PA',
9415                p_msg_name       => 'PA_FP_REPARENT_ERR_NEWPRT',
9416 --               p_token1         => 'TASK_NAME',
9417 --               p_value1         => l_task_name,
9418                p_token1         => 'PARENT_TASK_NAME',
9419                p_value1         => l_new_parent_task_name);
9420          l_msg_count := FND_MSG_PUB.count_msg;
9421          IF l_msg_count = 1 and x_msg_data IS NULL THEN
9422               PA_INTERFACE_UTILS_PUB.get_messages
9423                   (p_encoded        => FND_API.G_TRUE,
9424                    p_msg_index      => 1,
9425                    p_msg_count      => l_msg_count,
9426                    p_msg_data       => l_msg_data,
9427                    p_data           => l_data,
9428                    p_msg_index_out  => l_msg_index_out);
9429               x_msg_data := l_data;
9430               x_msg_count := l_msg_count;
9431          ELSE
9432               x_msg_count := l_msg_count;
9433          END IF;
9434  IF P_PA_DEBUG_MODE = 'Y' THEN
9435       pa_debug.reset_curr_function;
9436 END IF;
9437          return;
9438      END IF;
9439  IF P_PA_DEBUG_MODE = 'Y' THEN
9440      pa_debug.reset_curr_function;
9441 END IF;
9442 EXCEPTION
9443   WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
9444      x_return_status := FND_API.G_RET_STS_ERROR;
9445      l_msg_count := FND_MSG_PUB.count_msg;
9446      IF l_msg_count = 1 and x_msg_data IS NULL THEN
9447           PA_INTERFACE_UTILS_PUB.get_messages
9448               (p_encoded        => FND_API.G_TRUE
9449               ,p_msg_index      => 1
9450               ,p_msg_count      => l_msg_count
9451               ,p_msg_data       => l_msg_data
9452               ,p_data           => l_data
9453               ,p_msg_index_out  => l_msg_index_out);
9454           x_msg_data := l_data;
9455           x_msg_count := l_msg_count;
9456      ELSE
9457           x_msg_count := l_msg_count;
9458      END IF;
9459  IF P_PA_DEBUG_MODE = 'Y' THEN
9460      pa_debug.reset_curr_function;
9461 END IF;
9462      RETURN;
9463   WHEN others THEN
9464      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9465      x_msg_count     := 1;
9466      x_msg_data      := SQLERRM;
9467      FND_MSG_PUB.add_exc_msg
9468                    ( p_pkg_name        => 'pa_fin_plan_utils'
9469                     ,p_procedure_name  => 'check_reparent_task_ok'
9470                     ,p_error_text      => x_msg_data);
9471      IF l_debug_mode = 'Y' THEN
9472           pa_debug.g_err_stage:= 'Unexpected Error'||x_msg_data;
9473           pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
9474      pa_debug.reset_curr_function;
9475 END IF;
9476      RAISE;
9477 END check_reparent_task_ok;
9478 
9479 /* Part of changes for FP.M, Tracking Bug No - 3354518
9480 This function is being called by PA_FP_ELEMENTS_PUB currently to verify,
9481 if PA_FP_ELEMENTS contains a entry for this task.
9482 This procedure is now obsoleted.
9483 However noticing that this procedure is re-usable,
9484 we change all references to pa_tasks to pa_struct_task_wbs_v
9485 and all references of pa_fp_elements to pa_resource_assignments*/
9486 
9487 FUNCTION check_task_in_fp_option
9488     (
9489   /*  p_task_id                IN   pa_tasks.task_id%TYPE  */
9490   /*  Changing reference of pa_tasks to pa_struct_task_wbs_v  */
9491       p_task_id                IN   pa_struct_task_wbs_v.task_id%TYPE)
9492 
9493     RETURN VARCHAR2 AS
9494 
9495      l_exists VARCHAR2(1) := 'N';
9496 
9497      CURSOR C1 IS
9498      SELECT 'Y'
9499      FROM   DUAL
9500   /*  Changing reference of pa_fp_elements to pa_resource_assignments  */
9501      WHERE  EXISTS (SELECT 'X' FROM pa_resource_assignments WHERE TASK_ID = P_TASK_ID);
9502 /*     WHERE  EXISTS (SELECT 'X' FROM PA_FP_ELEMENTS WHERE TASK_ID = P_TASK_ID); */
9503 
9504 BEGIN
9505 
9506      OPEN C1;
9507      FETCH C1 INTO l_exists;
9508      CLOSE C1;
9509 
9510      RETURN l_exists;
9511 
9512 END check_task_in_fp_option;
9513 
9514 
9515 /* End of changes for FPM, Tracking Bug No - 3354518 */
9516 
9517 --Name:        Get_Budgeted_Amount
9518 --Type:                  Function
9519 --
9520 --Description:          This function is used by Capital-Project calling
9521 --                      objects to get the following:
9522 --
9523 --                      1) PROEJCT or LOWEST-LEVEL TASK for a given project
9524 --                      2) COST budget or FP plan version
9525 --                      3) Cost TOTAL amounts
9526 --                      4) RETURN RAW or BURDENED cost amount
9527 --
9528 --                      Amount is the project functional amount for the
9529 --                      the CURRNET BASELINED budget/plan version.
9530 --
9531 --                      Since this is an internal function, validations are NOT
9532 --                      performed on the IN-parameters:
9533 --
9534 --                         Indeed, as directed by management, if the calling object
9535 --                         wants cost amounts for an approved cost plan type,
9536 --                         it is the responsibility of the calling object to pass
9537 --                         a fin_plan_type_id for an approved cost plan type.
9538 --
9539 --                      As per design, if no data found, zero is returned.
9540 --
9541 --
9542 --
9543 --Called subprograms:    None.
9544 --
9545 --Notes:
9546 --                      Accuracy is important for this function.
9547 --
9548 --                      PROJECT-level amounts:
9549 --
9550 --                      1)    The old model SQL is coded in light of historical
9551 --                            quality issues with the Budget Form population of the
9552 --                            pa_budget_versions denormalized amounts.
9553 --
9554 --                      2)    The new model SQL is coded with the assumption that
9555 --                            there should be few bugs, if any, for the project-level
9556 --                            denormalized columns. Additionally, since there are far more
9557 --                            budget lines (multiple row types) for FP plans, performance
9558 --                            is more of a consideration.
9559 --
9560 --                      TASK-level amounts:
9561 --
9562 --                      The SQLs are coded more with accuracy in mind than performance.
9563 --
9564 --History:
9565 --    27-MAY-03 jwhite  - Created
9566 --
9567 --    29-MAY-03 jwhite  - As directed by managment, substituted a
9568 --                        version_type filter for approved_cost filter.
9569 --
9570 -- IN Parameters
9571 --    p_project_id              - Always passed.
9572 --
9573 --    p_task_id                 - Passed as NULL if project-level amounts requested.
9574 --
9575 --    p_fin_plan_type_id        - Query FP model if p_fin_plan_type_id is N-O-T null.
9576 --
9577 --    p_budget_type_code        - Query pre-FP model if p_fin_plan_type_id is NULL.
9578 --
9579 --    p_amount_type             - Passed as 'R' to return raw cost; 'B' to return burdened cost.
9580 
9581 
9582 FUNCTION Get_Budgeted_Amount
9583    (
9584      p_project_id              IN   pa_projects_all.project_id%TYPE
9585      , p_task_id               IN   pa_tasks.task_id%TYPE
9586      , p_fin_plan_type_id      IN   pa_proj_fp_options.fin_plan_type_id%TYPE
9587      , p_budget_type_code      IN   pa_budget_versions.budget_type_code%TYPE
9588      , p_amount_type           IN   VARCHAR2
9589    )  RETURN NUMBER
9590 IS
9591 
9592 
9593 
9594      -- Project-Level Amounts --------------------------
9595      --
9596 
9597      -- OLD (Pre-FP) Model
9598 
9599      CURSOR projOld_csr
9600      IS
9601      SELECT   sum(l.raw_cost)
9602               , sum(l.burdened_cost)
9603      FROM     pa_budget_versions v
9604               , pa_resource_assignments a
9605               , pa_budget_lines  l
9606      WHERE    v.project_id = p_project_id
9607      AND      v.budget_type_code = p_budget_type_code
9608      AND      v.current_flag  = 'Y'
9609      AND      v.budget_version_id = a.budget_version_id
9610      AND      a.resource_assignment_id = l.resource_assignment_id;
9611 
9612 
9613      -- NEW FP Model
9614 
9615      CURSOR projNewFp_csr
9616      IS
9617      SELECT   raw_cost
9618               , burdened_cost
9619      FROM     pa_budget_versions
9620      WHERE    project_id = p_project_id
9621      AND      fin_plan_type_id  = p_fin_plan_type_id
9622      AND      current_flag  = 'Y'
9623      AND      version_type IN ('COST','ALL');
9624 
9625 
9626 
9627      -- Task-Level Amounts ------------------------------------
9628      --
9629 
9630      -- OLD (Pre-FP) Model M-U-S-T Join to Budget Lines
9631 
9632      CURSOR taskOld_csr
9633      IS
9634      SELECT   sum(l.raw_cost)
9635               , sum(l.burdened_cost)
9636      FROM     pa_budget_versions v
9637               , pa_resource_assignments a
9638               , pa_budget_lines  l
9639      WHERE    v.project_id = p_project_id
9640      AND      v.budget_type_code = p_budget_type_code
9641      AND      v.current_flag  = 'Y'
9642      AND      v.budget_version_id = a.budget_version_id
9643      AND      a.task_id = p_task_id
9644      AND      a.resource_assignment_id = l.resource_assignment_id;
9645 
9646 
9647      -- NEW FP Model Can join to PA_RESOURCE_ASSIGNMENTS for Denormalized Amounts
9648      --
9649      -- !!!   * * *   PLEASE NOTE   * * *    !!!
9650      --    For this cursor, the pa_resource_assignments table contains multiple
9651      --    row types for a given baselined version,i.e.:
9652      --
9653      --      ROLLED_UP
9654      --      USER_ENTERED
9655      --
9656      --    The ROLLED_UP rows may be subject to a numerous bugs over time, given
9657      --    the complexity of the logic. Therefore, the rolled-up amounts may
9658      --    often be incorrect over time.
9659      --
9660      --    As a result, this cursor favors accuracy over performance and
9661      --    joins to 'USER_ENTERED' rows.
9662      --
9663      --
9664 
9665      CURSOR taskNewFp_csr
9666      IS
9667      SELECT   sum(a.TOTAL_PLAN_RAW_COST)
9668               , sum(a.TOTAL_PLAN_BURDENED_COST)
9669      FROM     pa_budget_versions v
9670               , pa_resource_assignments a
9671      WHERE    v.project_id = p_project_id
9672      AND      v.fin_plan_type_id = p_fin_plan_type_id
9673      AND      v.current_flag  = 'Y'
9674      AND      v.budget_version_id = a.budget_version_id
9675      AND      a.task_id = p_task_id
9676      AND      a.RESOURCE_ASSIGNMENT_TYPE = 'USER_ENTERED'
9677      AND      version_type IN ('COST','ALL');
9678 
9679 
9680 
9681 
9682      l_raw_cost       NUMBER := 0;
9683      l_burdened_cost  NUMBER := 0;
9684 
9685 
9686 BEGIN
9687 
9688 
9689    IF ( p_fin_plan_type_id IS NULL)
9690      THEN
9691 
9692       --  OLD Model Processing ----------------------
9693 
9694       IF ( p_task_id IS NULL )
9695         THEN
9696 
9697           -- Get Project-level Amounts
9698 
9699           OPEN  projOld_csr;
9700           FETCH projOld_csr INTO l_raw_cost, l_burdened_cost;
9701           CLOSE projOld_csr;
9702 
9703       ELSE
9704 
9705           -- Get Task-level Amounts
9706 
9707 
9708           OPEN  taskOld_csr;
9709           FETCH taskOld_csr INTO l_raw_cost, l_burdened_cost;
9710           CLOSE taskOld_csr;
9711 
9712 
9713       END IF; -- p_task_id IS NULL
9714 
9715 
9716 
9717    ELSE
9718 
9719       --  NEW FP Model Processing ---------------------
9720 
9721       IF ( p_task_id IS NULL )
9722         THEN
9723 
9724           -- Get Project-level Amounts
9725 
9726           OPEN  projNewFp_csr;
9727           FETCH projNewFp_csr INTO l_raw_cost, l_burdened_cost;
9728           CLOSE projNewFp_csr;
9729 
9730       ELSE
9731 
9732           -- Get Task-level Amounts
9733 
9734 
9735           OPEN  taskNewFp_csr;
9736           FETCH taskNewFp_csr INTO l_raw_cost, l_burdened_cost;
9737           CLOSE taskNewFp_csr;
9738 
9739 
9740       END IF; -- p_task_id IS NULL
9741 
9742 
9743 
9744    END IF; -- p_fin_plan_type_id IS NULL
9745 
9746 
9747 
9748    -- Conditionally RETURN Raw or Burdended Cost
9749 
9750    IF ( p_amount_type = 'R')
9751     THEN
9752        -- return raw cost
9753 
9754        RETURN l_raw_cost;
9755 
9756     ELSE
9757       -- return BURDENED cost
9758 
9759        RETURN l_burdened_cost;
9760 
9761    END IF; -- p_amount_type = 'R'
9762 
9763 
9764 
9765    EXCEPTION
9766       WHEN NO_DATA_FOUND then
9767         RETURN 0;
9768    WHEN OTHERS then
9769         RETURN 0;
9770 
9771 END Get_Budgeted_Amount;
9772 
9773 PROCEDURE Check_if_plan_type_editable (
9774  P_project_id            In              Number
9775 ,P_fin_plan_type_id      IN              Number
9776 ,P_version_type          IN              VARCHAR2
9777 ,X_editable_flag         OUT             NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
9778 ,X_return_status         OUT             NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
9779 ,X_msg_count             OUT             NOCOPY NUMBER --File.Sql.39 bug 4440895
9780 ,X_msg_data              OUT             NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
9781 
9782 AS
9783 
9784     -- Start of variables used for debugging purpose
9785 
9786      l_msg_count          NUMBER :=0;
9787      l_data               VARCHAR2(2000);
9788      l_msg_data           VARCHAR2(2000);
9789      l_error_msg_code     VARCHAR2(30);
9790      l_msg_index_out      NUMBER;
9791      l_return_status      VARCHAR2(2000);
9792      l_debug_mode         VARCHAR2(30);
9793 
9794     -- End of variables used for debugging purpose
9795 
9796 
9797 
9798 BEGIN
9799 
9800      FND_MSG_PUB.initialize;
9801      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
9802      l_debug_mode := NVL(l_debug_mode, 'Y');
9803 IF P_PA_DEBUG_MODE = 'Y' THEN
9804      pa_debug.set_curr_function( p_function => 'Check_if_plan_type_editable',
9805                                      p_debug_mode => l_debug_mode );
9806      END IF;
9807      x_msg_count := 0;
9808      x_return_status := FND_API.G_RET_STS_SUCCESS;
9809      -- Check for business rules violations
9810 
9811 
9812      IF P_PA_DEBUG_MODE = 'Y' THEN
9813          pa_debug.g_err_stage:='Validating input parameters';
9814          pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,3);
9815      END IF;
9816 
9817      -- Check if project id, fp option id and Version type are null
9818 
9819      IF (p_project_id       IS NULL) OR
9820         (p_fin_plan_type_id IS NULL) OR
9821         (p_version_type IS NULL)
9822      THEN
9823 
9824 
9825          IF P_PA_DEBUG_MODE = 'Y' THEN
9826              pa_debug.g_err_stage:='Project_id = '||p_project_id;
9827              pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,5);
9828 
9829              pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
9830              pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,5);
9831 
9832              pa_debug.g_err_stage:='Version_type = '||p_version_type;
9833              pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,5);
9834          END IF;
9835 
9836 
9837          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
9838                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
9839 
9840 
9841          IF P_PA_DEBUG_MODE = 'Y' THEN
9842              pa_debug.g_err_stage:='Invalid Arguments Passed';
9843              pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,5);
9844          END IF;
9845          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
9846 
9847      END IF;
9848 
9849      BEGIN
9850          select edit_after_baseline_flag
9851          into   x_editable_flag
9852          from pa_fin_plan_types_b
9853          where fin_plan_type_id = p_fin_plan_type_id;
9854      EXCEPTION
9855          WHEN OTHERS THEN
9856 
9857                            IF P_PA_DEBUG_MODE = 'Y' THEN
9858                                pa_debug.g_err_stage:='Error while fetching edit_after_baseline_flag';
9859                                pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,3);
9860                            END IF;
9861                            RAISE;
9862      END;
9863 
9864      IF nvl(x_editable_flag,'Y') <> 'Y' THEN
9865 
9866          BEGIN
9867           select 'N'
9868              into  x_editable_flag
9869              from  dual where exists (
9870                  select 1
9871                  from   pa_budget_versions
9872                  where  project_id = p_project_id
9873                  and    fin_plan_type_id = p_fin_plan_type_id
9874                  and    version_type = p_version_type
9875                  and    budget_status_code = 'B' );
9876          EXCEPTION
9877              WHEN NO_DATA_FOUND THEN
9878 
9879                            IF P_PA_DEBUG_MODE = 'Y' THEN
9880                                pa_debug.g_err_stage:='No base versions exist';
9881                                pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,3);
9882                            END IF;
9883 
9884                            x_editable_flag := 'Y';
9885 
9886          END;
9887      ELSE
9888          x_editable_flag := 'Y';
9889      END IF;
9890 
9891      IF P_PA_DEBUG_MODE = 'Y' THEN
9892          pa_debug.g_err_stage:='Exiting Check_if_plan_type_editable:';
9893          pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,3);
9894 
9895     --Reset the error stack
9896 
9897 	     pa_debug.reset_curr_function;
9898 	END IF;
9899 EXCEPTION
9900 
9901    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
9902           l_msg_count := FND_MSG_PUB.count_msg;
9903           IF l_msg_count = 1 THEN
9904                PA_INTERFACE_UTILS_PUB.get_messages
9905                      (p_encoded        => FND_API.G_TRUE
9906                       ,p_msg_index      => 1
9907                       ,p_msg_count      => l_msg_count
9908                       ,p_msg_data       => l_msg_data
9909                       ,p_data           => l_data
9910                       ,p_msg_index_out  => l_msg_index_out);
9911                x_msg_data := l_data;
9912                x_msg_count := l_msg_count;
9913           ELSE
9914               x_msg_count := l_msg_count;
9915           END IF;
9916            x_return_status := FND_API.G_RET_STS_ERROR;
9917 --           pa_debug.g_err_stage:='Invalid Arguments Passed';
9918 --           pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
9919  IF P_PA_DEBUG_MODE = 'Y' THEN
9920            pa_debug.reset_curr_function;
9921 END IF;
9922            RETURN;
9923 
9924      WHEN Others THEN
9925           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9926           x_msg_count     := 1;
9927           x_msg_data      := SQLERRM;
9928           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
9929                                   ,p_procedure_name  => 'CHECK_IF_PLAN_TYPE_EDITABLE');
9930           IF P_PA_DEBUG_MODE = 'Y' THEN
9931               pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
9932               pa_debug.write('Check_if_plan_type_editable: ' || l_module_name,pa_debug.g_err_stage,5);
9933           pa_debug.reset_curr_function;
9934 	END IF;
9935           RAISE;
9936 
9937 END Check_if_plan_type_editable;
9938 
9939 PROCEDURE End_date_active_val
9940     (p_start_date_active              IN     Date,
9941      p_end_date_active                IN     Date,
9942      x_return_status                  OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
9943      x_msg_count                      OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
9944      x_msg_data                       OUT    NOCOPY VARCHAR2) is --File.Sql.39 bug 4440895
9945  l_msg_count       NUMBER;
9946  l_msg_index_out   NUMBER;
9947  l_data            VARCHAR2(2000);
9948  l_msg_data        VARCHAR2(2000);
9949 BEGIN
9950 
9951   IF p_start_date_active IS NULL THEN
9952         /* Start date must be entered */
9953         x_return_status := FND_API.G_RET_STS_ERROR;
9954         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
9955                              p_msg_name            => 'PA_MANDATORY_INFO_MISSING');
9956   END IF;
9957 
9958   IF p_start_date_active > nvl(p_end_date_active,p_start_date_active) THEN
9959         /* The End Date cannot be earlier than the Start Date.  */
9960         x_return_status := FND_API.G_RET_STS_ERROR;
9961         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
9962                              p_msg_name            => 'PA_INVALID_END_DATE');
9963   END IF;
9964 
9965     l_msg_count := FND_MSG_PUB.count_msg;
9966 
9967     IF l_msg_count > 0 THEN
9968         IF l_msg_count = 1 THEN
9969              PA_INTERFACE_UTILS_PUB.get_messages
9970                  (p_encoded        => FND_API.G_TRUE,
9971                   p_msg_index      => 1,
9972                   p_msg_count      => l_msg_count,
9973                   p_msg_data       => l_msg_data,
9974                   p_data           => l_data,
9975                   p_msg_index_out  => l_msg_index_out);
9976              x_msg_data  := l_data;
9977              x_msg_count := l_msg_count;
9978         ELSE
9979              x_msg_count := l_msg_count;
9980         END IF;
9981         return;
9982     ELSE
9983       x_return_status := FND_API.G_RET_STS_SUCCESS;
9984     END IF;
9985 
9986 EXCEPTION
9987     WHEN OTHERS THEN
9988       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
9989       x_msg_count     := 1;
9990       x_msg_data      := SQLERRM;
9991       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_UTILS',
9992                                p_procedure_name   => 'end_date_active_val');
9993       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
9994 END End_date_active_val;
9995 
9996 /*=============================================================================
9997  This api is used to return current original version info for given plan type,
9998  project id and version type
9999 ==============================================================================*/
10000 
10001 PROCEDURE Get_Curr_Original_Version_Info(
10002           p_project_id            IN   pa_projects_all.project_id%TYPE
10003           ,p_fin_plan_type_id     IN   pa_budget_versions.fin_plan_type_id%TYPE
10004           ,p_version_type         IN   pa_budget_versions.version_type%TYPE
10005           ,x_fp_options_id        OUT  NOCOPY pa_proj_fp_options.proj_fp_options_id%TYPE --File.Sql.39 bug 4440895
10006           ,x_fin_plan_version_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_version_id%TYPE --File.Sql.39 bug 4440895
10007           ,x_return_status        OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10008           ,x_msg_count            OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10009           ,x_msg_data             OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
10010 AS
10011 
10012     --Start of variables used for debugging
10013 
10014     l_msg_count          NUMBER :=0;
10015     l_data               VARCHAR2(2000);
10016     l_msg_data           VARCHAR2(2000);
10017     l_error_msg_code     VARCHAR2(30);
10018     l_msg_index_out      NUMBER;
10019     l_return_status      VARCHAR2(2000);
10020     l_debug_mode         VARCHAR2(30);
10021 
10022     --End of variables used for debugging
10023 
10024     l_fp_preference_code            pa_proj_fp_options.fin_plan_preference_code%TYPE;
10025     l_version_type                  pa_budget_versions.version_type%TYPE;
10026     l_current_original_version_id   pa_budget_versions.budget_version_id%TYPE;
10027     l_fp_options_id                 pa_proj_fp_options.proj_fp_options_id%TYPE;
10028 
10029 BEGIN
10030 
10031     x_msg_count := 0;
10032     x_return_status := FND_API.G_RET_STS_SUCCESS;
10033 
10034     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10035     l_debug_mode := NVL(l_debug_mode, 'Y');
10036 
10037     IF l_debug_mode = 'Y' THEN
10038        pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.Get_Curr_Original_Version_Info');
10039        pa_debug.set_process('Get_Curr_Original_Version_Info: ' || 'PLSQL','LOG',l_debug_mode);
10040     END IF;
10041 
10042     -- Check for business rules violations
10043 
10044     IF l_debug_mode = 'Y' THEN
10045        pa_debug.g_err_stage:='Validating input parameters';
10046        pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
10047     END IF;
10048 
10049     IF (p_project_id       IS NULL) OR
10050        (p_fin_plan_type_id IS NULL)
10051     THEN
10052 
10053              IF l_debug_mode = 'Y' THEN
10054                 pa_debug.g_err_stage:='Project_id = '||p_project_id;
10055                 pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
10056 
10057                 pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
10058                 pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
10059              END IF;
10060 
10061              PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10062                                    p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
10063 
10064              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10065 
10066     END IF;
10067 
10068     --Fetch fin plan preference code
10069 
10070     IF l_debug_mode = 'Y' THEN
10071        pa_debug.g_err_stage:='Fetching fin plan preference code ';
10072        pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
10073     END IF;
10074 
10075     SELECT fin_plan_preference_code
10076     INTO   l_fp_preference_code
10077     FROM   pa_proj_fp_options
10078     WHERE  project_id = p_project_id
10079     AND    fin_plan_type_id = p_fin_plan_type_id
10080     AND    fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
10081 
10082     IF (l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) AND
10083        (p_version_type IS NULL)
10084     THEN
10085 
10086           --In this case version_type should be passed and so raise error
10087 
10088           IF l_debug_mode = 'Y' THEN
10089              pa_debug.g_err_stage:='Version_Type = '||p_version_type;
10090              pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
10091           END IF;
10092 
10093           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10094                       p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
10095 
10096           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10097 
10098     END IF;
10099 
10100     IF l_debug_mode = 'Y' THEN
10101        pa_debug.g_err_stage:='Parameter validation complete ';
10102        pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
10103     END IF;
10104 
10105     --Fetch  l_element_type ifn't passed and could be derived
10106 
10107     IF p_version_type IS NULL THEN
10108 
10109       IF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME THEN
10110 
10111          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL;
10112 
10113       ELSIF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY THEN
10114 
10115          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST;
10116 
10117       ELSIF l_fp_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY THEN
10118 
10119          l_version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE;
10120 
10121       END IF;
10122 
10123     END IF;
10124 
10125     --Fetch the current original version
10126 
10127     BEGIN
10128 
10129         IF l_debug_mode = 'Y' THEN
10130            pa_debug.g_err_stage:='Fetching current original Version';
10131            pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
10132         END IF;
10133 
10134         SELECT budget_version_id
10135         INTO   l_current_original_version_id
10136         FROM   pa_budget_versions
10137         WHERE  project_id = p_project_id
10138         AND    fin_plan_type_id = p_fin_plan_type_id
10139         AND    version_type = NVL(p_version_type,l_version_type)
10140         AND    budget_status_code = 'B'
10141         AND    current_original_flag = 'Y';
10142 
10143         --Fetch fp options id using plan version id
10144 
10145         IF l_debug_mode = 'Y' THEN
10146            pa_debug.g_err_stage:='Fetching fp option id';
10147            pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
10148         END IF;
10149 
10150         SELECT proj_fp_options_id
10151         INTO   l_fp_options_id
10152         FROM   pa_proj_fp_options
10153         WHERE  fin_plan_version_id = l_current_original_version_id;
10154 
10155     EXCEPTION
10156 
10157       WHEN NO_DATA_FOUND THEN
10158 
10159              l_current_original_version_id := NULL;
10160              l_fp_options_id := NULL;
10161 
10162     END;
10163 
10164     -- return the parameters to calling programme
10165     x_fin_plan_version_id := l_current_original_version_id;
10166     x_fp_options_id := l_fp_options_id;
10167 
10168     IF l_debug_mode = 'Y' THEN
10169        pa_debug.g_err_stage:='Exiting Get_Curr_Original_Version_Info';
10170        pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,3);
10171        pa_debug.reset_err_stack;
10172     END IF;
10173 
10174 EXCEPTION
10175 
10176      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10177           l_msg_count := FND_MSG_PUB.count_msg;
10178           IF l_msg_count = 1 THEN
10179                PA_INTERFACE_UTILS_PUB.get_messages
10180                      (p_encoded        => FND_API.G_TRUE
10181                       ,p_msg_index      => 1
10182                       ,p_msg_count      => l_msg_count
10183                       ,p_msg_data       => l_msg_data
10184                       ,p_data           => l_data
10185                       ,p_msg_index_out  => l_msg_index_out);
10186 
10187                x_msg_data := l_data;
10188                x_msg_count := l_msg_count;
10189           ELSE
10190               x_msg_count := l_msg_count;
10191           END IF;
10192 
10193           x_return_status := FND_API.G_RET_STS_ERROR;
10194 
10195           IF l_debug_mode = 'Y' THEN
10196              pa_debug.g_err_stage:='Invalid Arguments Passed';
10197              pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
10198 
10199              -- reset error stack
10200              pa_debug.reset_err_stack;
10201           END IF;
10202           RETURN;
10203 
10204      WHEN Others THEN
10205           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10206           x_msg_count     := 1;
10207           x_msg_data      := SQLERRM;
10208 
10209           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
10210                                   ,p_procedure_name  => 'Get_Curr_Original_Version_Info');
10211 
10212           IF l_debug_mode = 'Y' THEN
10213              pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
10214              pa_debug.write('Get_Curr_Original_Version_Info: ' || l_module_name,pa_debug.g_err_stage,5);
10215 
10216              -- reset error stack
10217              pa_debug.reset_err_stack;
10218           END IF;
10219           RAISE;
10220 END Get_Curr_Original_Version_Info;
10221 
10222 /*=============================================================================
10223  This api is used to derive actual_amts_thru_period for a version. The api also
10224  returns first future PA/GL periods if they are available. This api is called
10225  from plan setting pages to maintain Include unspent amount through period lov.
10226 ==============================================================================*/
10227 
10228 PROCEDURE GET_ACTUAL_AMTS_THRU_PERIOD(
10229            p_budget_version_id       IN   pa_budget_versions.budget_version_id%TYPE
10230           ,x_record_version_number   OUT   NOCOPY pa_budget_versions.record_version_number%TYPE --File.Sql.39 bug 4440895
10231           ,x_actual_amts_thru_period OUT   NOCOPY pa_budget_versions.actual_amts_thru_period%TYPE --File.Sql.39 bug 4440895
10232           ,x_first_future_pa_period  OUT   NOCOPY pa_periods_all.period_name%TYPE --File.Sql.39 bug 4440895
10233           ,x_first_future_gl_period  OUT   NOCOPY pa_periods_all.period_name%TYPE --File.Sql.39 bug 4440895
10234           ,x_return_status           OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10235           ,x_msg_count               OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10236           ,x_msg_data                OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
10237 AS
10238 
10239     --Start of variables used for debugging
10240 
10241     l_return_status      VARCHAR2(1);
10242     l_msg_count          NUMBER := 0;
10243     l_msg_data           VARCHAR2(2000);
10244     l_data               VARCHAR2(2000);
10245     l_msg_index_out      NUMBER;
10246     l_debug_mode         VARCHAR2(30);
10247 
10248     --End of variables used for debugging
10249 
10250     CURSOR future_gl_periods_cur  IS
10251     SELECT gl.period_name
10252     FROM   pa_implementations i
10253            , gl_period_statuses gl
10254     WHERE  gl.application_id     = PA_Period_Process_PKG.Application_ID
10255     AND    gl.set_of_books_id    = i.set_of_books_id
10256     AND    gl.adjustment_period_flag = 'N'
10257     AND    closing_status = 'F'
10258     ORDER BY gl.start_date;
10259 
10260     CURSOR future_pa_periods_cur    IS
10261     SELECT period_name
10262     FROM   pa_periods
10263     WHERE  status = 'F'
10264     ORDER BY start_date;
10265 
10266 BEGIN
10267 
10268     x_msg_count := 0;
10269     x_return_status := FND_API.G_RET_STS_SUCCESS;
10270 
10271     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10272     l_debug_mode := NVL(l_debug_mode, 'Y');
10273 
10274     -- Set curr function
10275 
10276  IF l_debug_mode = 'Y' THEN
10277     pa_debug.set_curr_function(
10278                 p_function   =>'PA_FIN_PLAN_UTILS.GET_ACTUAL_AMTS_THRU_PERIOD'
10279                ,p_debug_mode => l_debug_mode );
10280 
10281     -- Check for business rules violations
10282         pa_debug.g_err_stage:='Validating input parameters';
10283         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10284     END IF;
10285 
10286     IF (p_budget_version_id IS NULL)
10287     THEN
10288 
10289         IF l_debug_mode = 'Y' THEN
10290            pa_debug.g_err_stage:='p_budget_version_id = '|| p_budget_version_id;
10291            pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,5);
10292         END IF;
10293 
10294         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10295                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
10296 
10297         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10298 
10299     END IF;
10300 
10301     -- Using bv id derive ACTUAL_AMTS_THRU_PERIOD from versions table
10302     IF l_debug_mode = 'Y' THEN
10303         pa_debug.g_err_stage:='Fetching actual_amts_thru_period';
10304         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10305     END IF;
10306 
10307     SELECT actual_amts_thru_period
10308            ,record_version_number
10309     INTO   x_actual_amts_thru_period
10310            ,x_record_version_number
10311     FROM   pa_budget_versions
10312     WHERE  budget_version_id = p_budget_version_id;
10313 
10314     -- Fetch first future PA period
10315     IF l_debug_mode = 'Y' THEN
10316         pa_debug.g_err_stage:='Fetching first future PA period';
10317         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10318     END IF;
10319 
10320     -- Fetch the first period. There might not be any future period
10321     OPEN  future_pa_periods_cur;
10322     FETCH future_pa_periods_cur INTO x_first_future_pa_period;
10323     CLOSE future_pa_periods_cur;
10324 
10325     -- Fetch first future GL period
10326     IF l_debug_mode = 'Y' THEN
10327         pa_debug.g_err_stage:='first future PA period = '|| x_first_future_pa_period;
10328         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10329         pa_debug.g_err_stage:='Fetching first future GL period';
10330         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10331     END IF;
10332 
10333     -- Fetch first gl period, there might not be any gl period
10334     OPEN  future_pa_periods_cur;
10335     FETCH future_pa_periods_cur INTO x_first_future_gl_period;
10336     CLOSE future_pa_periods_cur;
10337 
10338     IF l_debug_mode = 'Y' THEN
10339         pa_debug.g_err_stage:='first future GL period = '||x_first_future_gl_period;
10340         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10341         pa_debug.g_err_stage:='Exiting GET_ACTUAL_AMTS_THRU_PERIOD';
10342         pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,3);
10343 
10344     -- Reset curr function
10345 	    pa_debug.reset_curr_function();
10346 	END IF;
10347 EXCEPTION
10348 
10349    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10350        l_msg_count := FND_MSG_PUB.count_msg;
10351        IF l_msg_count = 1 THEN
10352            PA_INTERFACE_UTILS_PUB.get_messages
10353                  (p_encoded        => FND_API.G_TRUE
10354                   ,p_msg_index      => 1
10355                   ,p_msg_count      => l_msg_count
10356                   ,p_msg_data       => l_msg_data
10357                   ,p_data           => l_data
10358                   ,p_msg_index_out  => l_msg_index_out);
10359 
10360            x_msg_data := l_data;
10361            x_msg_count := l_msg_count;
10362        ELSE
10363            x_msg_count := l_msg_count;
10364        END IF;
10365 
10366        x_return_status := FND_API.G_RET_STS_ERROR;
10367 
10368        IF l_debug_mode = 'Y' THEN
10369            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
10370            pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,5);
10371 
10372        -- Reset curr function
10373        pa_debug.reset_curr_function();
10374 	END IF;
10375        RETURN;
10376    WHEN Others THEN
10377        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10378        x_msg_count     := 1;
10379        x_msg_data      := SQLERRM;
10380 
10381        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
10382                                ,p_procedure_name  => 'GET_ACTUAL_AMTS_THRU_PERIOD');
10383 
10384        IF l_debug_mode = 'Y' THEN
10385            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
10386            pa_debug.write('GET_ACTUAL_AMTS_THRU_PERIOD: ' || l_module_name,pa_debug.g_err_stage,5);
10387 
10388        -- Reset curr function
10389        pa_debug.Reset_Curr_Function();
10390 	END IF;
10391        RAISE;
10392 END GET_ACTUAL_AMTS_THRU_PERIOD;
10393 
10394 /* To determine if a task is a planning element or not */
10395 -- Modified for Bug 3840993 --sagarwal
10396 FUNCTION IS_TASK_A_PLANNING_ELEMENT(
10397            p_budget_version_id        IN pa_budget_versions.budget_version_id%TYPE
10398           ,p_task_id                  IN pa_tasks.task_id%TYPE)
10399 RETURN VARCHAR2 IS
10400 l_exists               VARCHAR2(1);
10401 l_structure_version_id pa_budget_versions.project_structure_version_id%TYPE;
10402 
10403 BEGIN
10404      /* Commented and modified for bug#5614245
10405         SELECT NVL(project_structure_version_id, PA_PROJECT_STRUCTURE_UTILS.GET_FIN_STRUC_VER_ID(project_id )) */
10406      SELECT NVL(project_structure_version_id, pa_planning_element_utils.get_fin_struct_id(project_id, budget_version_id) )
10407      INTO   l_structure_version_id
10408      FROM   pa_budget_versions
10409      WHERE  budget_Version_id=p_budget_version_id;
10410 
10411     IF p_task_id <> 0 THEN -- For Task Level Records
10412        SELECT 'Y'
10413        INTO   l_exists
10414        FROM   DUAL
10415        WHERE  EXISTS (SELECT 'x'
10416                       FROM  PA_RESOURCE_ASSIGNMENTS a,  pa_proj_element_versions pelm
10417                       WHERE a.budget_version_id = p_budget_version_id
10418                       AND   a.task_id = pelm.proj_element_id
10419                       AND   a.resource_class_code = PA_FP_CONSTANTS_PKG.G_RESOURCE_CLASS_CODE_FIN
10420                       AND   a.task_id = p_task_id
10421                       AND   a.resource_class_flag = 'Y'
10422                       AND   pelm.parent_structure_version_id= l_structure_version_id);
10423     ELSE -- For Project Level Records
10424        SELECT 'Y'
10425        INTO   l_exists
10426        FROM   DUAL
10427        WHERE  EXISTS (SELECT 'x'
10428                       FROM  PA_RESOURCE_ASSIGNMENTS a
10429                       WHERE a.budget_version_id = p_budget_version_id
10430                       AND   a.resource_class_code = PA_FP_CONSTANTS_PKG.G_RESOURCE_CLASS_CODE_FIN
10431                       AND   a.task_id = p_task_id
10432                       AND   a.resource_class_flag = 'Y');
10433     END IF;
10434 
10435      RETURN l_exists;
10436 EXCEPTION
10437 WHEN NO_DATA_FOUND THEN
10438      RETURN 'N';
10439 WHEN OTHERS THEN
10440      RETURN Null;
10441 END IS_TASK_A_PLANNING_ELEMENT;
10442 
10443 /* To determine if a task has resources attached to it as planning element */
10444 
10445 FUNCTION IS_RESOURCE_ATTACHED_TO_TASK(
10446           p_budget_version_id        IN pa_budget_versions.budget_version_id%TYPE
10447           ,p_task_id                 IN pa_resource_assignments.task_id%TYPE)
10448          --,p_wbs_element_version_id   IN pa_resource_assignments.wbs_element_version_id%TYPE)
10449 RETURN VARCHAR2 IS
10450 l_exists VARCHAR2(1);
10451 BEGIN
10452 -- sagarwal -- Removed redundant join to pa_budget_versions from select statement below
10453      SELECT 'Y'
10454      INTO   l_exists
10455      FROM   DUAL
10456      WHERE  EXISTS (SELECT 'x'
10457                     FROM  PA_RESOURCE_ASSIGNMENTS a
10458                     WHERE a.budget_version_id = p_budget_version_id
10459                     AND   a.task_id           = p_task_id
10460                     --Commented for bug 3793136
10461                     --AND   a.wbs_element_version_id = p_wbs_element_version_id
10462                     AND   NOT(a.resource_class_code = PA_FP_CONSTANTS_PKG.G_RESOURCE_CLASS_CODE_FIN
10463                            and a.resource_class_flag = 'Y')    );
10464 
10465      RETURN l_exists;
10466 EXCEPTION
10467 WHEN NO_DATA_FOUND THEN
10468      RETURN 'N';
10469 WHEN OTHERS THEN
10470      RETURN Null;
10471 END IS_RESOURCE_ATTACHED_TO_TASK;
10472 
10473 /*====================================================================
10474   To determince if a resource list can be updated for a workplan. If
10475   progress exists for the strucuture or task, its not allowed
10476   irrespective of  versioning is enabled or disbled. Else if versioning
10477   is disabled then if task assignments exist then its not allowed.
10478   Rest of the cases it can be changed.
10479 
10480   Bug 3619687 Changed the validations such that
10481     1. Check if versioning is enabled
10482         a. if published version exists change is not allowed
10483         b. else allow change
10484     2. If versioning disabled case,
10485         a. if progress exists against project or any of the tasks
10486            change is not allowed
10487         b. else allow change
10488  ====================================================================*/
10489 
10490 PROCEDURE IS_WP_RL_UPDATEABLE(
10491            p_project_id                     IN   pa_budget_versions.project_id%TYPE
10492           ,x_wp_rl_update_allowed_flag      OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10493           ,x_reason_msg_code                OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10494           ,x_return_status                  OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10495           ,x_msg_count                      OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10496           ,x_msg_data                       OUT  NOCOPY VARCHAR2)AS --File.Sql.39 bug 4440895
10497 
10498     --Start of variables used for debugging
10499 
10500     l_return_status      VARCHAR2(1);
10501     l_msg_count          NUMBER := 0;
10502     l_msg_data           VARCHAR2(2000);
10503     l_data               VARCHAR2(2000);
10504     l_msg_index_out      NUMBER;
10505     l_debug_mode         VARCHAR2(30);
10506 
10507     --End of variables used for debugging
10508 
10509     l_progress_exists          VARCHAR2(1);
10510     l_is_sharing_enabled       VARCHAR2(1);
10511     l_is_versioning_enabled    VARCHAR2(1);
10512     l_structure_id             NUMBER;
10513     l_task_id                  NUMBER;
10514 
10515     CURSOR get_wp_id IS
10516       SELECT ppe.proj_element_id
10517         from pa_proj_elements ppe,
10518              pa_proj_structure_types ppst,
10519              pa_structure_types pst
10520        where ppe.project_id = p_project_id
10521          and ppe.proj_element_id = ppst.proj_element_id
10522          and ppst.structure_type_id = pst.structure_type_id
10523          and pst.structure_type_class_code = 'WORKPLAN';
10524 
10525     CURSOR get_tasks (c_structure_id pa_proj_elements.proj_element_id%TYPE) IS
10526       SELECT ppev1.proj_element_id
10527         FROM pa_proj_element_versions ppev1,
10528              pa_proj_element_versions ppev2
10529        WHERE ppev2.object_type = 'PA_STRUCTURES'
10530          AND ppev2.project_id = p_project_id
10531          AND ppev2.proj_element_id = c_structure_id
10532          AND ppev1.parent_structure_version_id = ppev2.element_version_id
10533          AND ppev1.object_type = 'PA_TASKS';
10534       /*** Bug 3683382 modified the query for performance
10535       SELECT proj_element_id
10536         from pa_proj_elements
10537        where project_id = p_project_id
10538          and object_type = 'PA_TASKS'
10539          and proj_element_id IN (
10540              select ppev1.proj_element_id
10541                from pa_proj_element_versions ppev1,
10542                     pa_proj_element_versions ppev2
10543               where ppev2.object_type = 'PA_STRUCTURES'
10544                 and ppev2.project_id = p_project_id
10545                 and ppev2.proj_element_id = c_structure_id
10546                 and ppev1.parent_structure_version_id = ppev2.element_version_id);
10547        3683382 ***/
10548 
10549 BEGIN
10550     x_msg_count := 0;
10551     x_return_status := FND_API.G_RET_STS_SUCCESS;
10552 
10553     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10554     l_debug_mode := NVL(l_debug_mode, 'Y');
10555 
10556     -- Set curr function
10557  IF l_debug_mode = 'Y' THEN
10558     pa_debug.set_curr_function(
10559                 p_function   =>'PA_FIN_PLAN_UTILS.IS_WP_RL_UPDATEABLE'
10560                ,p_debug_mode => l_debug_mode );
10561 END IF;
10562 
10563     IF (p_project_id IS NULL)
10564     THEN
10565 
10566         IF l_debug_mode = 'Y' THEN
10567            pa_debug.g_err_stage:='Project_id = '|| p_project_id;
10568            pa_debug.write('IS_WP_RL_UPDATEABLE: ' || l_module_name,pa_debug.g_err_stage,5);
10569         END IF;
10570 
10571         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10572                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
10573 
10574         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10575 
10576     END IF;
10577 
10578     -- Check if versioning is enabled or not
10579     l_is_versioning_enabled :=
10580             PA_WORKPLAN_ATTR_UTILS.Check_Wp_Versioning_Enabled(p_project_id);
10581 
10582     IF l_is_versioning_enabled = 'Y' THEN
10583 
10584         -- If published version exists then res list update is not allowed
10585         IF ('Y' = PA_PROJ_TASK_STRUC_PUB.Published_version_exists(p_project_id))
10586         THEN
10587             x_wp_rl_update_allowed_flag  :=  'N';
10588             x_reason_msg_code := 'PA_FP_PRL_PUBLISHED_VER_EXISTS';
10589         ELSE
10590             -- publised version does not exist
10591             -- for a versioning enabled case, progress can be entered only after publish
10592             x_wp_rl_update_allowed_flag  :=  'Y';
10593             x_reason_msg_code :=  null;
10594         END IF;
10595 
10596     ELSE -- versioning disabled structure
10597 
10598         -- Check if progress exists against project or any task
10599         l_progress_exists := 'N'; -- initialise to 'N'
10600 
10601         -- Fetch workplan structure id
10602         OPEN get_wp_id;
10603         FETCH get_wp_id into l_structure_id;
10604         CLOSE get_wp_id;
10605 
10606         IF (PA_PROJECT_STRUCTURE_UTILS.check_proj_progress_exist
10607                 (p_project_id, l_structure_id, 'WORKPLAN') = 'Y')    -- Added a new parameter while calling 'check_proj_progress_exist' for the BUG 6914708
10608         THEN
10609             l_progress_exists := 'Y';
10610         ELSE
10611              OPEN get_tasks(l_structure_id);
10612              LOOP
10613                FETCH get_tasks INTO l_task_id;
10614                EXIT WHEN get_tasks%NOTFOUND;
10615 
10616                IF (PA_PROJECT_STRUCTURE_UTILS.check_task_progress_exist(l_task_id) = 'Y')
10617                THEN
10618                  l_progress_exists := 'Y';
10619                  EXIT;
10620                END IF;
10621              END LOOP;
10622              CLOSE get_tasks;
10623         END IF;
10624 
10625         IF l_progress_exists ='Y' THEN
10626 
10627              -- Progress exists and hence resource list cannot be changed
10628              x_wp_rl_update_allowed_flag  :=  'N';
10629              x_reason_msg_code :=  'PA_FP_PRL_PROGRESS_EXISTS_ERR';
10630 
10631         ELSE -- Progress does not exist
10632 
10633              x_wp_rl_update_allowed_flag  :=  'Y';
10634              x_reason_msg_code :=  null;
10635 
10636         END IF;     -- progress
10637     END IF; -- versioning
10638 
10639     -- reset curr function
10640  IF P_PA_DEBUG_MODE = 'Y' THEN
10641     pa_debug.reset_curr_function();
10642 END IF;
10643 EXCEPTION
10644 
10645    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10646        l_msg_count := FND_MSG_PUB.count_msg;
10647        IF l_msg_count = 1 THEN
10648            PA_INTERFACE_UTILS_PUB.get_messages
10649                  (p_encoded        => FND_API.G_TRUE
10650                   ,p_msg_index      => 1
10651                   ,p_msg_count      => l_msg_count
10652                   ,p_msg_data       => l_msg_data
10653                   ,p_data           => l_data
10654                   ,p_msg_index_out  => l_msg_index_out);
10655 
10656            x_msg_data := l_data;
10657            x_msg_count := l_msg_count;
10658        ELSE
10659            x_msg_count := l_msg_count;
10660        END IF;
10661 
10662        x_return_status := FND_API.G_RET_STS_ERROR;
10663 
10664        IF l_debug_mode = 'Y' THEN
10665            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
10666            pa_debug.write('IS_WP_RL_UPDATEABLE: ' || l_module_name,pa_debug.g_err_stage,5);
10667 
10668        -- reset curr function
10669            pa_debug.reset_curr_function();
10670 	END IF;
10671        RETURN;
10672    WHEN Others THEN
10673        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10674        x_msg_count     := 1;
10675        x_msg_data      := SQLERRM;
10676 
10677        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
10678                                ,p_procedure_name  => 'IS_WP_RL_UPDATEABLE');
10679 
10680        IF l_debug_mode = 'Y' THEN
10681            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
10682            pa_debug.write('IS_WP_RL_UPDATEABLE: ' || l_module_name,pa_debug.g_err_stage,5);
10683        -- reset curr function
10684            pa_debug.Reset_Curr_Function();
10685 	END IF;
10686        RAISE;
10687 
10688 END IS_WP_RL_UPDATEABLE;
10689 
10690 /*=============================================================================
10691 This api checks if any plan type marked for primary forecast cost usage has been
10692 attached to the project and returns id of that plan type if found. Else null
10693 would be returned
10694 ==============================================================================*/
10695 
10696 PROCEDURE IS_PRI_FCST_COST_PT_ATTACHED(
10697           p_project_id     IN   pa_projects_all.project_id%TYPE
10698           ,x_plan_type_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
10699           ,x_return_status OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10700           ,x_msg_count     OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10701           ,x_msg_data      OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
10702 AS
10703 
10704     --Start of variables used for debugging
10705 
10706     l_return_status      VARCHAR2(1);
10707     l_msg_count          NUMBER := 0;
10708     l_msg_data           VARCHAR2(2000);
10709     l_data               VARCHAR2(2000);
10710     l_msg_index_out      NUMBER;
10711     l_debug_mode         VARCHAR2(30);
10712 
10713     --End of variables used for debugging
10714 
10715 BEGIN
10716 
10717     x_msg_count := 0;
10718     x_return_status := FND_API.G_RET_STS_SUCCESS;
10719 
10720     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10721     l_debug_mode := NVL(l_debug_mode, 'Y');
10722 
10723     -- Set curr function
10724  IF l_debug_mode = 'Y' THEN
10725     pa_debug.set_curr_function(
10726                 p_function   =>'PA_FIN_PLAN_UTILS.IS_PRI_FCST_COST_PT_ATTACHED'
10727                ,p_debug_mode => l_debug_mode );
10728 
10729     -- Check for business rules violations
10730 
10731         pa_debug.g_err_stage:='Validating input parameters';
10732         pa_debug.write('IS_PRI_FCST_COST_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,3);
10733     END IF;
10734 
10735     IF (p_project_id IS NULL)
10736     THEN
10737 
10738         IF l_debug_mode = 'Y' THEN
10739            pa_debug.g_err_stage:='Project_id = '||p_project_id;
10740            pa_debug.write('IS_PRI_FCST_COST_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,5);
10741         END IF;
10742 
10743         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10744                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
10745 
10746         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10747 
10748     END IF;
10749 
10750     BEGIN
10751         SELECT fin_plan_type_id
10752         INTO   x_plan_type_id
10753         FROM   pa_proj_fp_options
10754         WHERE  project_id = p_project_id
10755           AND  fin_plan_option_level_code = 'PLAN_TYPE'
10756           AND  nvl(primary_cost_forecast_flag,'N') = 'Y' ;
10757     EXCEPTION
10758         WHEN NO_DATA_FOUND THEN
10759             x_plan_type_id := null;
10760     END;
10761 
10762     IF l_debug_mode = 'Y' THEN
10763         pa_debug.g_err_stage:='Exiting IS_PRI_FCST_COST_PT_ATTACHED';
10764         pa_debug.write('IS_PRI_FCST_COST_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,3);
10765        -- reset curr function
10766        pa_debug.reset_curr_function();
10767     END IF;
10768 EXCEPTION
10769 
10770    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10771        l_msg_count := FND_MSG_PUB.count_msg;
10772        IF l_msg_count = 1 THEN
10773            PA_INTERFACE_UTILS_PUB.get_messages
10774                  (p_encoded        => FND_API.G_TRUE
10775                   ,p_msg_index      => 1
10776                   ,p_msg_count      => l_msg_count
10777                   ,p_msg_data       => l_msg_data
10778                   ,p_data           => l_data
10779                   ,p_msg_index_out  => l_msg_index_out);
10780 
10781            x_msg_data := l_data;
10782            x_msg_count := l_msg_count;
10783        ELSE
10784            x_msg_count := l_msg_count;
10785        END IF;
10786 
10787        x_return_status := FND_API.G_RET_STS_ERROR;
10788 
10789        IF l_debug_mode = 'Y' THEN
10790            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
10791            pa_debug.write('IS_PRI_FCST_COST_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,5);
10792        -- reset curr function
10793           pa_debug.reset_curr_function();
10794 	END IF;
10795        RETURN;
10796    WHEN Others THEN
10797        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10798        x_msg_count     := 1;
10799        x_msg_data      := SQLERRM;
10800 
10801        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
10802                                ,p_procedure_name  => 'IS_PRI_FCST_COST_PT_ATTACHED');
10803 
10804        IF l_debug_mode = 'Y' THEN
10805            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
10806            pa_debug.write('IS_PRI_FCST_COST_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,5);
10807        -- reset curr function
10808           pa_debug.Reset_Curr_Function();
10809 	END IF;
10810        RAISE;
10811 END IS_PRI_FCST_COST_PT_ATTACHED;
10812 
10813 /*=============================================================================
10814 This api checks if any plan type marked for primary forecast revenue usage has
10815 been attached to the project and returns id of that plan type if found. Else null
10816 would be returned
10817 ==============================================================================*/
10818 
10819 PROCEDURE IS_PRI_FCST_REV_PT_ATTACHED(
10820           p_project_id     IN   pa_projects_all.project_id%TYPE
10821           ,x_plan_type_id  OUT  NOCOPY pa_proj_fp_options.fin_plan_type_id%TYPE --File.Sql.39 bug 4440895
10822           ,x_return_status OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
10823           ,x_msg_count     OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
10824           ,x_msg_data      OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
10825 AS
10826 
10827     --Start of variables used for debugging
10828 
10829     l_return_status      VARCHAR2(1);
10830     l_msg_count          NUMBER := 0;
10831     l_msg_data           VARCHAR2(2000);
10832     l_data               VARCHAR2(2000);
10833     l_msg_index_out      NUMBER;
10834     l_debug_mode         VARCHAR2(30);
10835 
10836     --End of variables used for debugging
10837 
10838 BEGIN
10839 
10840     x_msg_count := 0;
10841     x_return_status := FND_API.G_RET_STS_SUCCESS;
10842 
10843     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
10844     l_debug_mode := NVL(l_debug_mode, 'Y');
10845 
10846     -- Set curr function
10847  IF l_debug_mode = 'Y' THEN
10848     pa_debug.set_curr_function(
10849                 p_function   =>'PA_FIN_PLAN_UTILS.IS_PRI_FCST_REV_PT_ATTACHED'
10850                ,p_debug_mode => l_debug_mode );
10851 
10852     -- Check for business rules violations
10853         pa_debug.g_err_stage:='Validating input parameters';
10854         pa_debug.write('IS_PRI_FCST_REV_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,3);
10855     END IF;
10856 
10857     IF (p_project_id IS NULL)
10858     THEN
10859 
10860         IF l_debug_mode = 'Y' THEN
10861            pa_debug.g_err_stage:='Project_id = '||p_project_id;
10862            pa_debug.write('IS_PRI_FCST_REV_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,5);
10863         END IF;
10864 
10865         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
10866                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
10867 
10868         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
10869 
10870     END IF;
10871 
10872     BEGIN
10873         SELECT fin_plan_type_id
10874         INTO   x_plan_type_id
10875         FROM   pa_proj_fp_options
10876         WHERE  project_id = p_project_id
10877         AND    fin_plan_option_level_code = 'PLAN_TYPE'
10878         AND    nvl(primary_rev_forecast_flag,'N') = 'Y' ;
10879     EXCEPTION
10880         WHEN NO_DATA_FOUND THEN
10881             x_plan_type_id := null;
10882     END;
10883 
10884     IF l_debug_mode = 'Y' THEN
10885         pa_debug.g_err_stage:='Exiting IS_PRI_FCST_REV_PT_ATTACHED';
10886         pa_debug.write('IS_PRI_FCST_REV_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,3);
10887     -- reset curr function
10888        pa_debug.reset_curr_function();
10889    END IF;
10890 EXCEPTION
10891 
10892    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
10893        l_msg_count := FND_MSG_PUB.count_msg;
10894        IF l_msg_count = 1 THEN
10895            PA_INTERFACE_UTILS_PUB.get_messages
10896                  (p_encoded        => FND_API.G_TRUE
10897                   ,p_msg_index      => 1
10898                   ,p_msg_count      => l_msg_count
10899                   ,p_msg_data       => l_msg_data
10900                   ,p_data           => l_data
10901                   ,p_msg_index_out  => l_msg_index_out);
10902 
10903            x_msg_data := l_data;
10904            x_msg_count := l_msg_count;
10905        ELSE
10906            x_msg_count := l_msg_count;
10907        END IF;
10908 
10909        x_return_status := FND_API.G_RET_STS_ERROR;
10910 
10911        IF l_debug_mode = 'Y' THEN
10912            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
10913            pa_debug.write('IS_PRI_FCST_REV_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,5);
10914        -- reset curr function
10915           pa_debug.reset_curr_function();
10916        END IF;
10917        RETURN;
10918    WHEN Others THEN
10919        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10920        x_msg_count     := 1;
10921        x_msg_data      := SQLERRM;
10922 
10923        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
10924                                ,p_procedure_name  => 'IS_PRI_FCST_REV_PT_ATTACHED');
10925 
10926        IF l_debug_mode = 'Y' THEN
10927            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
10928            pa_debug.write('IS_PRI_FCST_REV_PT_ATTACHED: ' || l_module_name,pa_debug.g_err_stage,5);
10929        -- reset curr function
10930          pa_debug.Reset_Curr_Function();
10931        END IF;
10932        RAISE;
10933 END IS_PRI_FCST_REV_PT_ATTACHED;
10934 
10935 FUNCTION is_wp_resource_list
10936          (p_project_id       IN   pa_projects_all.project_id%TYPE
10937          ,p_resource_list_id IN
10938 pa_resource_lists_all_bg.resource_list_id%TYPE) RETURN VARCHAR2 IS
10939 l_wp_resource_list_flag VARCHAR2(1);
10940 BEGIN
10941      BEGIN
10942           select  'Y'
10943           into    l_wp_resource_list_flag
10944           from    dual
10945           where   exists (select 'x'
10946                           from   pa_proj_fp_options a, pa_fin_plan_types_b b
10947                           where  a.project_id = p_project_id
10948                           and    a.fin_plan_option_level_code <> 'PROJECT'
10949                           and    a.fin_plan_type_id = b.fin_plan_type_id
10950                           and    (a.cost_resource_list_id = p_resource_list_id or
10951                                  a.revenue_resource_list_id = p_resource_list_id or
10952                                  a.all_resource_list_id = p_resource_list_id)
10953                           and    b.use_for_workplan_flag = 'Y');
10954      EXCEPTION
10955           WHEN NO_DATA_FOUND THEN
10956                l_wp_resource_list_flag := 'N';
10957      END;
10958      return l_wp_resource_list_flag;
10959 END is_wp_resource_list;
10960 
10961 FUNCTION is_fp_resource_list
10962          (p_project_id       IN   pa_projects_all.project_id%TYPE
10963          ,p_resource_list_id IN pa_resource_lists_all_bg.resource_list_id%TYPE)
10964 RETURN VARCHAR2 IS
10965 l_fp_resource_list_flag VARCHAR2(1);
10966 BEGIN
10967      BEGIN
10968           select  'Y'
10969           into    l_fp_resource_list_flag
10970           from    dual
10971           where   exists (select 'x'
10972                           from   pa_proj_fp_options a
10973                           where  a.project_id = p_project_id
10974                           and    not exists (select 'x'
10975                                              from   pa_fin_plan_types_b b
10976                                              where  a.fin_plan_type_id = b.fin_plan_type_id
10977                                              and    b.use_for_workplan_flag = 'Y')
10978                           and    (a.cost_resource_list_id = p_resource_list_id or
10979                                   a.revenue_resource_list_id = p_resource_list_id or
10980                                   a.all_resource_list_id = p_resource_list_id));
10981      EXCEPTION
10982           WHEN NO_DATA_FOUND THEN
10983                l_fp_resource_list_flag := 'N';
10984      END;
10985      return l_fp_resource_list_flag;
10986 END is_fp_resource_list;
10987 
10988 /* Returns the current working version ids for a given plan type.
10989    Returns -1 if the plan type cannot have a version of that version type
10990    Returns NULL if no working version exists for the plan type*/
10991 
10992 PROCEDURE GET_CURR_WORKING_VERSION_IDS(P_fin_plan_type_id         IN       Pa_fin_plan_types_b.fin_plan_type_id%TYPE       -- Id of the plan types
10993                                       ,P_project_id               IN       Pa_budget_versions.project_id%TYPE         -- Id of the Project
10994                                       ,X_cost_budget_version_id   OUT      NOCOPY Pa_budget_versions.budget_version_id%TYPE  -- ID of the cost version associated with the CI --File.Sql.39 bug 4440895
10995                                       ,X_rev_budget_version_id    OUT      NOCOPY Pa_budget_versions.budget_version_id%TYPE  -- ID of the revenue version associated with the CI --File.Sql.39 bug 4440895
10996                                       ,X_all_budget_version_id    OUT      NOCOPY Pa_budget_versions.budget_version_id%TYPE  -- ID of the all version associated with the CI --File.Sql.39 bug 4440895
10997                                       ,x_return_status            OUT      NOCOPY VARCHAR2  -- Indicates the exit status of the API --File.Sql.39 bug 4440895
10998                                       ,x_msg_data                 OUT      NOCOPY VARCHAR2  -- Indicates the error occurred --File.Sql.39 bug 4440895
10999                                       ,X_msg_count                OUT      NOCOPY NUMBER)   -- Indicates the number of error messages --File.Sql.39 bug 4440895
11000 IS
11001      l_plan_pref_code  pa_proj_fp_options.fin_plan_preference_code%TYPE;
11002      l_fp_options_id   pa_proj_fp_options.proj_fp_options_id%TYPE;
11003 
11004      -- Start of variables used for debugging purpose
11005 
11006       l_msg_count          NUMBER :=0;
11007       l_data               VARCHAR2(2000);
11008       l_msg_data           VARCHAR2(2000);
11009       l_msg_index_out      NUMBER;
11010       l_return_status      VARCHAR2(2000);
11011       l_debug_mode         VARCHAR2(1);
11012       l_debug_level3       CONSTANT NUMBER := 3;
11013       l_debug_level5       CONSTANT NUMBER := 5;
11014       l_mod_name           VARCHAR2(100) := l_module_name || '.GET_CURR_WORKING_VERSION_IDS'  ;
11015       l_token_name         VARCHAR2(30) :='PROCEDURENAME';
11016 
11017     -- End of variables used for debugging purpose
11018 
11019 BEGIN
11020 
11021      IF l_debug_mode = 'Y' THEN
11022      pa_debug.set_curr_function( p_function   => 'GET_CURR_WORKING_VERSION_IDS',
11023                                      p_debug_mode => P_PA_debug_mode );
11024      END IF;
11025 
11026      x_msg_count := 0;
11027      x_return_status := FND_API.G_RET_STS_SUCCESS;
11028 
11029      -- Validate that the input parameters are not null
11030      IF P_fin_plan_type_id IS NULL OR P_project_id IS NULL THEN
11031           IF P_PA_debug_mode = 'Y' THEN
11032               pa_debug.g_err_stage:='fin_plan_type_id = '||p_fin_plan_type_id;
11033               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11034 
11035               pa_debug.g_err_stage:='project_id = '||p_project_id;
11036               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11037 
11038           END IF;
11039 
11040 
11041           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11042                                p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11043                                p_token1 => l_token_name,
11044                                p_value1 => l_mod_name);
11045 
11046 
11047           IF P_PA_debug_mode = 'Y' THEN
11048               pa_debug.g_err_stage:='Invalid Arguments Passed';
11049               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11050           END IF;
11051           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11052      END IF;
11053 
11054      IF P_PA_debug_mode = 'Y' THEN
11055             pa_debug.g_err_stage:= 'Entering GET_CURR_WORKING_VERSION_IDS';
11056             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11057     END IF;
11058 
11059      BEGIN
11060           SELECT  fin_plan_preference_code
11061           INTO    l_plan_pref_code
11062           FROM    pa_proj_fp_options
11063           WHERE   fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE
11064           AND     project_id = p_project_id
11065           AND     fin_plan_type_id = p_fin_plan_type_id;
11066      EXCEPTION
11067           WHEN NO_DATA_FOUND THEN
11068                IF P_PA_debug_mode = 'Y' THEN
11069                     pa_debug.g_err_stage:='No data found while getting fin_plan_preference_code ';
11070                     pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11071                END IF;
11072                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11073      END;
11074 
11075      X_cost_budget_version_id := -1;
11076      X_rev_budget_version_id  := -1;
11077      X_all_budget_version_id  := -1;
11078 
11079      -- Get_Curr_Working_Version_Info returns NULL if there is no working version of that version type
11080      If l_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY  OR
11081         l_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP  then
11082           PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info(p_project_id           =>  p_project_id
11083                                                          ,p_fin_plan_type_id     =>  p_fin_plan_type_id
11084                                                          ,p_version_type         =>  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST
11085                                                          ,x_fp_options_id        =>  l_fp_options_id
11086                                                          ,x_fin_plan_version_id  =>  X_cost_budget_version_id
11087                                                          ,x_return_status        =>  l_return_status
11088                                                          ,x_msg_count            =>  l_msg_count
11089                                                          ,x_msg_data             =>  l_msg_data);
11090 
11091           IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11092                 IF P_PA_debug_mode = 'Y' THEN
11093                      pa_debug.g_err_stage:= 'Error in Get_Curr_Working_Version_Info: COST context';
11094                      pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11095                 END IF;
11096                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11097           END IF;
11098      END IF;
11099      If l_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY  OR
11100            l_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP  then
11101           PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info(p_project_id           =>  p_project_id
11102                                                          ,p_fin_plan_type_id     =>  p_fin_plan_type_id
11103                                                          ,p_version_type         =>  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
11104                                                          ,x_fp_options_id        =>  l_fp_options_id
11105                                                          ,x_fin_plan_version_id  =>  X_rev_budget_version_id
11106                                                          ,x_return_status        =>  l_return_status
11107                                                          ,x_msg_count            =>  l_msg_count
11108                                                          ,x_msg_data             =>  l_msg_data);
11109           IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11110                 IF P_PA_debug_mode = 'Y' THEN
11111                      pa_debug.g_err_stage:= 'Error in Get_Curr_Working_Version_Info: REV context';
11112                      pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11113                 END IF;
11114                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11115           END IF;
11116      END IF;
11117      If l_plan_pref_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME    then
11118           PA_FIN_PLAN_UTILS.Get_Curr_Working_Version_Info(p_project_id           =>  p_project_id
11119                                                          ,p_fin_plan_type_id     =>  p_fin_plan_type_id
11120                                                          ,p_version_type         =>  PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL
11121                                                          ,x_fp_options_id        =>  l_fp_options_id
11122                                                          ,x_fin_plan_version_id  =>  x_all_budget_version_id
11123                                                          ,x_return_status        =>  l_return_status
11124                                                          ,x_msg_count            =>  l_msg_count
11125                                                          ,x_msg_data             =>  l_msg_data);
11126 
11127           IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11128                 IF P_PA_debug_mode = 'Y' THEN
11129                      pa_debug.g_err_stage:= 'Error in Get_Curr_Working_Version_Info: ALL context';
11130                      pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11131                 END IF;
11132                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11133           END IF;
11134 
11135 
11136      END IF;
11137 
11138      IF P_PA_debug_mode = 'Y' THEN
11139                  pa_debug.g_err_stage:= 'Exiting GET_CURR_WORKING_VERSION_IDS';
11140                  pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11141             pa_debug.reset_curr_function;
11142      END IF;
11143 EXCEPTION
11144       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
11145           l_msg_count := FND_MSG_PUB.count_msg;
11146 
11147           IF l_msg_count = 1 and x_msg_data IS NULL THEN
11148                PA_INTERFACE_UTILS_PUB.get_messages
11149                      (p_encoded        => FND_API.G_TRUE
11150                       ,p_msg_index      => 1
11151                       ,p_msg_count      => l_msg_count
11152                       ,p_msg_data       => l_msg_data
11153                       ,p_data           => l_data
11154                       ,p_msg_index_out  => l_msg_index_out);
11155                x_msg_data := l_data;
11156                x_msg_count := l_msg_count;
11157           ELSE
11158               x_msg_count := l_msg_count;
11159           END IF;
11160            x_return_status := FND_API.G_RET_STS_ERROR;
11161 	 IF P_PA_DEBUG_MODE = 'Y' THEN
11162            pa_debug.reset_curr_function;
11163 	END IF;
11164            RETURN;
11165 
11166      WHEN Others THEN
11167           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11168           x_msg_count     := 1;
11169           x_msg_data      := SQLERRM;
11170 
11171           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
11172                                   ,p_procedure_name  => 'GET_CURR_WORKING_VERSION_IDS');
11173           IF P_PA_DEBUG_MODE = 'Y' THEN
11174                pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
11175                pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11176           pa_debug.reset_curr_function;
11177 	END IF;
11178           RAISE;
11179 
11180 END GET_CURR_WORKING_VERSION_IDS;
11181 
11182 
11183 /* Returns the various OUT parameters for the current working version(s) OR
11184    for a change order
11185 
11186    Depending upon the value of p_version_type  labor/equipement hours either from the cost version or revenue version
11187    will be returned.Valid values are NULL, 'COST', 'REVENUE' AND 'ALL'. If p_version_type is null labor/equipment hours will be
11188    returned from the cost version. For bug 3662077
11189 */
11190 -- Added New Params for Quantity in GET_PROJ_IMPACT_AMOUNTS - Bug 3902176
11191 
11192 -- p_version parameter was earlier used to retieve the cost or revenue or all quantity figures.
11193 -- Since cost and revenue quantity figures are now both alreayd being retrieved and are passed
11194 -- in separate out params, p_version parameter is no longer required.
11195 -- Commenting out references of p_version_type_below - Bug 3902176
11196 
11197 PROCEDURE GET_PROJ_IMPACT_AMOUNTS(p_cost_budget_version_id  IN   Pa_budget_versions.budget_version_id%TYPE            --  ID of the cost version associated with the CI
11198                                  ,p_rev_budget_version_id   IN   Pa_budget_versions.budget_version_id%TYPE            --  ID of the revenue version associated with the CI
11199                                  ,p_all_budget_version_id   IN   Pa_budget_versions.budget_version_id%TYPE            --  ID of the all version associated with the CI
11200 --                                 ,p_version_type            IN   pa_budget_versions.version_type%TYPE
11201                                  ,X_proj_raw_cost           OUT  NOCOPY Pa_budget_versions.total_project_raw_cost%TYPE       --  Raw Cost in PC --File.Sql.39 bug 4440895
11202                                  ,X_proj_burdened_cost      OUT  NOCOPY Pa_budget_versions.total_project_burdened_cost%TYPE  --  Burdened Cost in PC --File.Sql.39 bug 4440895
11203                                  ,X_proj_revenue            OUT  NOCOPY Pa_budget_versions.total_project_revenue%TYPE        --  Revenue in PC --File.Sql.39 bug 4440895
11204                                  ,X_labor_hrs_cost          OUT  NOCOPY Pa_budget_versions.labor_quantity%TYPE               --  Labor Hours Cost --File.Sql.39 bug 4440895
11205                                  ,X_equipment_hrs_cost      OUT  NOCOPY Pa_budget_versions.equipment_quantity%TYPE           --  Equipment Hours Cost --File.Sql.39 bug 4440895
11206                                  ,X_labor_hrs_rev           OUT  NOCOPY Pa_budget_versions.labor_quantity%TYPE               --  Labor Hours Revenue --File.Sql.39 bug 4440895
11207                                  ,X_equipment_hrs_rev       OUT  NOCOPY Pa_budget_versions.equipment_quantity%TYPE           --  Equipment Hours Revenue --File.Sql.39 bug 4440895
11208                                  ,X_margin                  OUT  NOCOPY Number                                               --  Margin --File.Sql.39 bug 4440895
11209                                  ,X_margin_percent          OUT  NOCOPY Number                                               --  Margin percent --File.Sql.39 bug 4440895
11210                                  ,X_margin_derived_from_code OUT  NOCOPY pa_proj_fp_options.margin_derived_from_code%TYPE     --  margin_derived_from_code - Bug 3734840 --File.Sql.39 bug 4440895
11211                                  ,x_return_status           OUT  NOCOPY VARCHAR2                                             --  Indicates the exit status of the API --File.Sql.39 bug 4440895
11212                                  ,x_msg_data                OUT  NOCOPY VARCHAR2                                             --  Indicates the error occurred --File.Sql.39 bug 4440895
11213                                  ,X_msg_count               OUT  NOCOPY NUMBER)                                              --  Indicates the number of error messages --File.Sql.39 bug 4440895
11214 IS
11215 l_margin_derived_from_code    pa_proj_fp_options.margin_derived_from_code%TYPE;
11216 l_budget_version_id           Pa_budget_versions.budget_version_id%TYPE;
11217 
11218 -- Start of variables used for debugging purpose
11219 
11220       l_msg_count          NUMBER :=0;
11221       l_data               VARCHAR2(2000);
11222       l_msg_data           VARCHAR2(2000);
11223       l_msg_index_out      NUMBER;
11224       l_return_status      VARCHAR2(2000);
11225       l_debug_mode         VARCHAR2(1);
11226       l_debug_level3       CONSTANT NUMBER := 3;
11227       l_debug_level5       CONSTANT NUMBER := 5;
11228       l_mod_name           VARCHAR2(100) := l_module_name || '.GET_PROJ_IMPACT_AMOUNTS' ;
11229       l_token_name         VARCHAR2(30) :='PROCEDURENAME';
11230 
11231 -- End of variables used for debugging purpose
11232 
11233 
11234 BEGIN
11235 
11236   IF P_PA_DEBUG_MODE = 'Y' THEN
11237     pa_debug.set_curr_function( p_function   => 'GET_PROJ_IMPACT_AMOUNTS',
11238                                      p_debug_mode => P_PA_debug_mode );
11239 END IF;
11240     x_msg_count := 0;
11241     x_return_status := FND_API.G_RET_STS_SUCCESS;
11242 
11243     -- See if all the input parameters passed are invalid
11244     IF nvl(p_all_budget_version_id,-1) = -1 AND nvl(p_cost_budget_version_id,-1) = -1
11245     AND nvl(p_rev_budget_version_id,-1) = -1 THEN
11246          IF P_PA_DEBUG_MODE = 'Y' THEN
11247                pa_debug.g_err_stage:='Invalid Arguments Passed';
11248                pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11249          END IF;
11250 
11251 
11252          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11253                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11254                                 p_token1 => l_token_name,
11255                                 p_value1 => l_mod_name);
11256 
11257          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11258     END IF;
11259 
11260 
11261 -- Bug 3902176
11262 /*
11263      --p_version_type should be either COST or REVENUE if not null
11264      IF p_version_type IS NOT NULL AND
11265         p_version_type NOT IN ('COST', 'REVENUE','ALL') THEN
11266 
11267         IF P_PA_debug_mode = 'Y' THEN
11268             pa_debug.g_err_stage:='p_version_type = '||p_version_type;
11269             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11270          END IF;
11271 
11272 
11273          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11274                             p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11275                             p_token1 => l_token_name,
11276                             p_value1 => l_mod_name);
11277 
11278 
11279          IF P_PA_debug_mode = 'Y' THEN
11280             pa_debug.g_err_stage:='Invalid Arguments Passed';
11281             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11282          END IF;
11283          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11284 
11285     END IF;
11286 */
11287 
11288     IF P_PA_debug_mode = 'Y' THEN
11289             pa_debug.g_err_stage:= 'Entering GET_PROJ_IMPACT_AMOUNTS';
11290             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11291     END IF;
11292 
11293     /* l_budget_version_id is used to get margin_derived_from_code.
11294        This is derived if an 'ALL' version exists or if both 'cost' and revenue' versions exist because
11295        margin has a meaning only in these two cases*/
11296     IF nvl(p_all_budget_version_id,-1) <> -1 THEN
11297          l_budget_version_id := p_all_budget_version_id;
11298     ELSIF nvl(p_cost_budget_version_id,-1) <> -1 AND nvl(p_rev_budget_version_id,-1) <> -1 THEN
11299          l_budget_version_id := p_cost_budget_version_id;
11300     END IF;
11301 
11302     IF l_budget_version_id IS NOT NULL THEN
11303          BEGIN
11304                SELECT    nvl(pfo.margin_derived_from_code,'B')
11305                INTO      l_margin_derived_from_code
11306                FROM      pa_proj_fp_options pfo
11307                WHERE     pfo.fin_plan_version_id=l_budget_version_id ;
11308          EXCEPTION
11309               WHEN NO_DATA_FOUND THEN
11310                      IF P_PA_DEBUG_MODE = 'Y' THEN
11311                           pa_debug.g_err_stage:='No Data Found while fetching margin_derived_from_code';
11312                           pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11313                      END IF;
11314                      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11315          END;
11316     END IF;
11317 
11318     x_margin_derived_from_code := l_margin_derived_from_code; -- Bug 3734840
11319 
11320     X_proj_raw_cost      := NULL;
11321     X_proj_burdened_cost := NULL;
11322     X_proj_revenue       := NULL;
11323     X_labor_hrs_cost     := NULL;
11324     X_equipment_hrs_cost := NULL;
11325     X_labor_hrs_rev      := NULL;
11326     X_equipment_hrs_rev  := NULL;
11327     X_margin             := NULL;
11328     X_margin_percent     := NULL;
11329 
11330  -- Modified Sqls Below for 3902176.
11331  -- Please note that for a ALL Version, x_xxxx_hrs_cost and x_xxxx_hrs_rev will contain the same figures
11332  -- of the ALL version. However all code will use x_xxxx_hrs_cost figures for ALL version references.
11333 
11334         IF NVL(p_cost_budget_version_id,-1) <> -1 or NVL(p_all_budget_version_id,-1) <> -1 THEN
11335              BEGIN
11336                 SELECT    labor_quantity,
11337                           Equipment_quantity,
11338                           Total_project_raw_cost,
11339                           Total_project_burdened_cost
11340                 INTO      x_labor_hrs_cost,
11341                           x_equipment_hrs_cost,
11342                           x_proj_raw_cost,
11343                           x_proj_burdened_cost
11344                 FROM      pa_budget_versions
11345                 WHERE     budget_version_id = decode(nvl(p_cost_budget_version_id,-1),-1,p_all_budget_version_id,p_cost_budget_version_id);
11346              EXCEPTION
11347                      WHEN NO_DATA_FOUND THEN
11348                                IF P_PA_DEBUG_MODE = 'Y' THEN
11349                                    pa_debug.g_err_stage:='No data found while fetching quantity and amounts. Context: Cost/All version type';
11350                                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11351                                END IF;
11352                                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11353              END;
11354         END IF;
11355         IF NVL(p_rev_budget_version_id,-1) <> -1 or NVL(p_all_budget_version_id,-1) <> -1 THEN
11356              BEGIN
11357                      SELECT    labor_quantity,
11358                                Equipment_quantity,
11359                                Total_project_revenue
11360                      INTO      x_labor_hrs_rev,
11361                                x_equipment_hrs_rev,
11362                                x_proj_revenue
11363                      FROM      pa_budget_versions
11364                      WHERE     budget_version_id = decode(nvl(p_rev_budget_version_id,-1),-1,p_all_budget_version_id,p_rev_budget_version_id);
11365              EXCEPTION
11366                      WHEN NO_DATA_FOUND THEN
11367                                IF P_PA_DEBUG_MODE = 'Y' THEN
11368                                    pa_debug.g_err_stage:='No data found while fetching quantity and amounts. Context: Rev/All version type';
11369                                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11370                                END IF;
11371                                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11372              END;
11373          END IF;
11374 
11375 -- Bug 3902176.
11376 -- Below code-logic for deriving quantity is not required anymore as both Cost and Revenue Quantity
11377 -- are now being derived and passed as separate params.
11378 /*
11379      --If the verison type passed is REVENUE then the labor/equipment hours should be derived from the REVENUE version
11380      --Otherwise cost labor/equipement hours will be returned if exist and revenue labor/equipment hours will be
11381      --returned if cost labor/equipement hours do not exist. Bug 3662077
11382      IF p_version_type='REVENUE' THEN
11383 
11384         x_labor_hrs := l_labor_hrs;
11385         x_equipment_hrs := l_equipment_hrs;
11386 
11387      ELSIF NVL(p_cost_budget_version_id,-1) = -1 AND NVL(p_all_budget_version_id,-1) = -1  THEN
11388         x_labor_hrs := l_labor_hrs;
11389         x_equipment_hrs := l_equipment_hrs;
11390      END IF;
11391 */
11392 
11393     IF l_budget_version_id IS NOT NULL THEN
11394          IF l_margin_derived_from_code = PA_FP_CONSTANTS_PKG.G_MARGIN_DERIVED_FROM_CODE_R THEN
11395               x_margin := nvl(x_proj_revenue,0) - nvl(x_proj_raw_cost,0);
11396          ELSE
11397               x_margin := nvl(x_proj_revenue,0) - nvl(x_proj_burdened_cost,0);
11398          END IF;
11399 
11400          IF x_proj_revenue IS NULL or x_proj_revenue = 0 THEN
11401               x_margin_percent := x_proj_revenue;
11402          ELSE
11403               x_margin_percent := (x_margin/x_proj_revenue)*100;
11404          END IF;
11405     END IF;
11406 
11407 
11408     IF P_PA_debug_mode = 'Y' THEN
11409             pa_debug.g_err_stage:= 'Exiting GET_PROJ_IMPACT_AMOUNTS';
11410             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11411     pa_debug.reset_curr_function;
11412     END IF;
11413 EXCEPTION
11414       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
11415           l_msg_count := FND_MSG_PUB.count_msg;
11416 
11417           IF l_msg_count = 1 and x_msg_data IS NULL THEN
11418                PA_INTERFACE_UTILS_PUB.get_messages
11419                      (p_encoded        => FND_API.G_TRUE
11420                       ,p_msg_index      => 1
11421                       ,p_msg_count      => l_msg_count
11422                       ,p_msg_data       => l_msg_data
11423                       ,p_data           => l_data
11424                       ,p_msg_index_out  => l_msg_index_out);
11425                x_msg_data := l_data;
11426                x_msg_count := l_msg_count;
11427           ELSE
11428               x_msg_count := l_msg_count;
11429           END IF;
11430            x_return_status := FND_API.G_RET_STS_ERROR;
11431 
11432  IF P_PA_DEBUG_MODE = 'Y' THEN
11433            pa_debug.reset_curr_function;
11434 END IF;
11435            RETURN;
11436 
11437      WHEN Others THEN
11438           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11439           x_msg_count     := 1;
11440           x_msg_data      := SQLERRM;
11441 
11442           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
11443                                   ,p_procedure_name  => 'GET_PROJ_IMPACT_AMOUNTS');
11444           IF P_PA_DEBUG_MODE = 'Y' THEN
11445                pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
11446                pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11447           pa_debug.reset_curr_function;
11448 	END IF;
11449           RAISE;
11450 
11451 
11452 END GET_PROJ_IMPACT_AMOUNTS;
11453 
11454 
11455 /* PROCEDURE GET_SUMMARY_AMOUNTS
11456    -----------------------------
11457    Mandatory input parameters: 1. p_context,p_project_id and p_ci_id
11458                                          OR
11459                                2. p_context,p_project_id and p_fin_plan_type_id
11460 
11461    Valid values for p_context: 1. PA_FP_CONSTANTS_PKG.G_CI_VERSION_AMOUNTS. (In this case p_ci_id has to be passed)
11462                                2. PA_FP_CONSTANTS_PKG.G_PLAN_TYPE_CWV_AMOUNTS. (In this case p_fin_plan_type_id has to be passed)
11463 
11464    Depending on the value of p_context, the API returns the OUT parameters with respect to either the CI versions of the passed ci_id
11465    or the current working version of the fin_plan_type_id in the context of a project (p_project_id)
11466 
11467    Depending upon the value of p_version_type  labor/equipement hours either from the cost version or revenue version
11468    will be returned.Valid values are NULL, 'COST', 'REVENUE' AND 'ALL'. If p_version_type is null labor/equipment hours will be
11469    returned from the cost version. For bug 3662077*/
11470 
11471 -- Added New Params for Quantity in Get_Summary_Amounts - Bug 3902176
11472 
11473 -- p_version parameter was earlier used to retieve the cost or revenue or all quantity figures.
11474 -- Since cost and revenue quantity figures are now both alreayd being retrieved and are passed
11475 -- in separate out params, p_version parameter is no longer required.
11476 -- Commenting out references of p_version_type_below - Bug 3902176
11477 
11478 PROCEDURE GET_SUMMARY_AMOUNTS(p_context                 IN              VARCHAR2
11479                              ,P_project_id              IN              Pa_projects_all.project_id%TYPE                           --  Id of the project .
11480                              ,P_ci_id                   IN              Pa_budget_versions.ci_id%TYPE  DEFAULT  NULL              --  Controm item id of the change document
11481                              ,P_fin_plan_type_id        IN              Pa_fin_plan_types_b.fin_plan_type_id%TYPE  DEFAULT  NULL
11482 --                             ,p_version_type            IN              pa_budget_versions.version_type%TYPE
11483                              ,X_proj_raw_cost           OUT             NOCOPY Pa_budget_versions.total_project_raw_cost%TYPE            --  Raw Cost in PC --File.Sql.39 bug 4440895
11484                              ,X_proj_burdened_cost      OUT             NOCOPY Pa_budget_versions.total_project_burdened_cost%TYPE       --  Burdened Cost in PC --File.Sql.39 bug 4440895
11485                              ,X_proj_revenue            OUT             NOCOPY Pa_budget_versions.total_project_revenue%TYPE             --  Revenue in PC --File.Sql.39 bug 4440895
11486                              ,X_margin                  OUT             NOCOPY NUMBER                                                    --  MARGIN --File.Sql.39 bug 4440895
11487                              ,X_margin_percent          OUT             NOCOPY NUMBER                                                    --  MARGIN percent --File.Sql.39 bug 4440895
11488                              ,X_labor_hrs_cost          OUT             NOCOPY Pa_budget_versions.labor_quantity%TYPE                    --  Labor Hours Cost --File.Sql.39 bug 4440895
11489                              ,X_equipment_hrs_cost      OUT             NOCOPY Pa_budget_versions.equipment_quantity%TYPE                --  Equipment Hours Cost --File.Sql.39 bug 4440895
11490                              ,X_labor_hrs_rev           OUT             NOCOPY Pa_budget_versions.labor_quantity%TYPE                    --  Labor Hours Revenue --File.Sql.39 bug 4440895
11491                              ,X_equipment_hrs_rev       OUT             NOCOPY Pa_budget_versions.equipment_quantity%TYPE                --  Equipment Hours Revenue --File.Sql.39 bug 4440895
11492                              ,X_cost_budget_version_id  OUT             NOCOPY Pa_budget_versions.budget_version_id%TYPE                 --  Cost Budget Verison Id --File.Sql.39 bug 4440895
11493                              ,X_rev_budget_version_id   OUT             NOCOPY Pa_budget_versions.budget_version_id%TYPE                 --  Revenue Budget Verison Id --File.Sql.39 bug 4440895
11494                              ,X_all_budget_version_id   OUT             NOCOPY Pa_budget_versions.budget_version_id%TYPE                 --  All Budget Verison Id --File.Sql.39 bug 4440895
11495                              ,X_margin_derived_from_code OUT             NOCOPY pa_proj_fp_options.margin_derived_from_code%TYPE          --  margin_derived_from_code of cost version - Bug 3734840 --File.Sql.39 bug 4440895
11496                              ,x_return_status           OUT             NOCOPY VARCHAR2                                                  --  Indicates the exit status of the API --File.Sql.39 bug 4440895
11497                              ,x_msg_data                OUT             NOCOPY VARCHAR2                                                  --  Indicates the error occurred --File.Sql.39 bug 4440895
11498                              ,X_msg_count               OUT             NOCOPY NUMBER)                                                   --  Indicates the number of error messages --File.Sql.39 bug 4440895
11499 IS
11500      -- Start of variables used for debugging purpose
11501 
11502       l_msg_count          NUMBER :=0;
11503       l_data               VARCHAR2(2000);
11504       l_msg_data           VARCHAR2(2000);
11505       l_msg_index_out      NUMBER;
11506       l_return_status      VARCHAR2(2000);
11507       l_debug_mode         VARCHAR2(1);
11508       l_debug_level3       CONSTANT NUMBER := 3;
11509       l_debug_level5       CONSTANT NUMBER := 5;
11510       l_mod_name           VARCHAR2(100) := l_module_name || '.GET_SUMMARY_AMOUNTS' ;
11511       l_token_name         VARCHAR2(30) :='PROCEDURENAME';
11512 
11513       -- End of variables used for debugging purpose
11514 
11515 BEGIN
11516 
11517  IF P_PA_DEBUG_MODE = 'Y' THEN
11518      pa_debug.set_curr_function( p_function   => 'GET_SUMMARY_AMOUNTS',
11519                                  p_debug_mode => P_PA_debug_mode );
11520 END IF;
11521      x_msg_count := 0;
11522      x_return_status := FND_API.G_RET_STS_SUCCESS;
11523 
11524      -- Check for business rules violations
11525 
11526      IF P_PA_debug_mode = 'Y' THEN
11527          pa_debug.g_err_stage:='Validating input parameters';
11528          pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11529      END IF;
11530 
11531      -- Check if p_context or project id is NULL
11532      IF p_context IS NULL OR p_project_id IS NULL THEN
11533 
11534           IF P_PA_debug_mode = 'Y' THEN
11535               pa_debug.g_err_stage:='Ci_id = '||p_ci_id;
11536               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11537 
11538               pa_debug.g_err_stage:='project_id = '||p_project_id;
11539               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11540 
11541           END IF;
11542 
11543 
11544           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11545                                p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11546                                p_token1 => l_token_name,
11547                                p_value1 => l_mod_name);
11548 
11549 
11550           IF P_PA_debug_mode = 'Y' THEN
11551               pa_debug.g_err_stage:='Invalid Arguments Passed';
11552               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11553           END IF;
11554           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11555 
11556      END IF;
11557 
11558 
11559 -- Bug 3902176 -- p_version_type is not passed anymore
11560 /*
11561      --p_version_type should be either COST or REVENUE if not null
11562      IF p_version_type IS NOT NULL AND
11563         p_version_type NOT IN ('COST', 'REVENUE','ALL') THEN
11564 
11565         IF P_PA_debug_mode = 'Y' THEN
11566             pa_debug.g_err_stage:='Ci_id = '||p_ci_id;
11567             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11568 
11569             pa_debug.g_err_stage:='p_context = '||p_context;
11570             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11571 
11572          END IF;
11573 
11574 
11575          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11576                             p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11577                             p_token1 => l_token_name,
11578                             p_value1 => l_mod_name);
11579 
11580 
11581          IF P_PA_debug_mode = 'Y' THEN
11582             pa_debug.g_err_stage:='Invalid Arguments Passed';
11583             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11584          END IF;
11585          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11586 
11587      END IF;
11588 */
11589 
11590 
11591      -- Check if fin_plan_type_id and ci id are null
11592 
11593      IF p_context = PA_FP_CONSTANTS_PKG.G_CI_VERSION_AMOUNTS THEN
11594           IF (p_ci_id IS NULL)
11595           THEN
11596 
11597                IF P_PA_debug_mode = 'Y' THEN
11598                    pa_debug.g_err_stage:='Ci_id = '||p_ci_id;
11599                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11600 
11601                    pa_debug.g_err_stage:='p_context = '||p_context;
11602                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11603 
11604                END IF;
11605 
11606 
11607                PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11608                                     p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11609                                     p_token1 => l_token_name,
11610                                     p_value1 => l_mod_name);
11611 
11612 
11613                IF P_PA_debug_mode = 'Y' THEN
11614                    pa_debug.g_err_stage:='Invalid Arguments Passed';
11615                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11616                END IF;
11617                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11618 
11619           END IF;
11620 
11621       ELSIF p_context = PA_FP_CONSTANTS_PKG.G_PLAN_TYPE_CWV_AMOUNTS THEN
11622            IF (p_fin_plan_type_id IS NULL)
11623            THEN
11624 
11625                IF P_PA_debug_mode = 'Y' THEN
11626                    pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
11627                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11628 
11629                    pa_debug.g_err_stage:='p_context = '||p_context;
11630                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11631 
11632                END IF;
11633 
11634 
11635                PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11636                                     p_msg_name      => 'PA_FP_INV_PARAM_PASSED',
11637                                     p_token1 => l_token_name,
11638                                     p_value1 => l_mod_name);
11639 
11640 
11641                IF P_PA_debug_mode = 'Y' THEN
11642                    pa_debug.g_err_stage:='Invalid Arguments Passed';
11643                    pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11644                END IF;
11645                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11646 
11647             END IF;
11648 
11649       END IF;
11650 
11651       IF P_PA_debug_mode = 'Y' THEN
11652             pa_debug.g_err_stage:= 'Entering GET_SUMMARY_AMOUNTS';
11653             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11654       END IF;
11655 
11656       IF p_context = PA_FP_CONSTANTS_PKG.G_CI_VERSION_AMOUNTS THEN
11657            Pa_Fp_Control_Items_Utils.GET_CI_VERSIONS(P_ci_id                  => p_ci_id,
11658                                                      X_cost_budget_version_id => x_cost_budget_version_id,
11659                                                      X_rev_budget_version_id  => x_rev_budget_version_id,
11660                                                      X_all_budget_version_id  => x_all_budget_version_id,
11661                                                      x_return_status          => l_return_status,
11662                                                      x_msg_data               => l_msg_data,
11663                                                      X_msg_count              => l_msg_count);
11664 
11665            IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11666                 IF P_PA_debug_mode = 'Y' THEN
11667                      pa_debug.g_err_stage:= 'Error in GET_CI_VERSIONS';
11668                      pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11669                 END IF;
11670                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11671            END IF;
11672 
11673 -- Added New Params for Quantity(Cost/Rev) in call to GET_PROJ_IMPACT_AMOUNTS - Bug 3902176
11674            PA_FIN_PLAN_UTILS.GET_PROJ_IMPACT_AMOUNTS(p_cost_budget_version_id => x_cost_budget_version_id
11675                                                     ,p_rev_budget_version_id  => x_rev_budget_version_id
11676                                                     ,p_all_budget_version_id  => x_all_budget_version_id
11677 --                                                    ,p_version_type           => p_version_type  -- Bug 3902176
11678                                                     ,X_proj_raw_cost          => x_proj_raw_cost
11679                                                     ,X_proj_burdened_cost     => x_proj_burdened_cost
11680                                                     ,X_proj_revenue           => x_proj_revenue
11681                                                     ,x_labor_hrs_cost         => x_labor_hrs_cost
11682                                                     ,x_equipment_hrs_cost     => x_equipment_hrs_cost
11683                                                     ,x_labor_hrs_rev          => x_labor_hrs_rev
11684                                                     ,x_equipment_hrs_rev      => x_equipment_hrs_rev
11685                                                     ,X_margin                 => x_margin
11686                                                     ,X_margin_percent         => x_margin_percent
11687                                                     ,x_margin_derived_from_code => x_margin_derived_from_code -- Bug 3734840
11688                                                     ,x_return_status          => l_return_status
11689                                                     ,x_msg_data               => l_msg_data
11690                                                     ,X_msg_count              => l_msg_count);
11691 
11692            IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11693                 IF P_PA_debug_mode = 'Y' THEN
11694                      pa_debug.g_err_stage:= 'Error in GET_PROJ_IMPACT_AMOUNTS';
11695                      pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11696                 END IF;
11697                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11698            END IF;
11699       ELSIF p_context = PA_FP_CONSTANTS_PKG.G_PLAN_TYPE_CWV_AMOUNTS THEN
11700             pa_fin_plan_utils.GET_CURR_WORKING_VERSION_IDS(P_fin_plan_type_id         => P_fin_plan_type_id
11701                                                           ,P_project_id               => p_project_id
11702                                                           ,X_cost_budget_version_id   => x_cost_budget_version_id
11703                                                           ,X_rev_budget_version_id    => x_rev_budget_version_id
11704                                                           ,X_all_budget_version_id    => x_all_budget_version_id
11705                                                           ,x_return_status            => l_return_status
11706                                                           ,x_msg_data                 => l_msg_data
11707                                                           ,X_msg_count                => l_msg_count);
11708 
11709             IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11710                 IF P_PA_debug_mode = 'Y' THEN
11711                      pa_debug.g_err_stage:= 'Error in GET_CURR_WORKING_VERSION_IDS';
11712                      pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11713                 END IF;
11714                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11715             END IF;
11716 
11717            --Get the project impact amounts only if the current working version exists. Bug 3661627
11718            IF nvl(x_all_budget_version_id,-1) <> -1 OR nvl(x_cost_budget_version_id,-1) <> -1
11719               OR nvl(x_rev_budget_version_id,-1) <> -1 THEN
11720 
11721 -- Added New Params for Quantity(Cost/Rev) in call to GET_PROJ_IMPACT_AMOUNTS - Bug 3902176
11722                 PA_FIN_PLAN_UTILS.GET_PROJ_IMPACT_AMOUNTS(p_cost_budget_version_id => x_cost_budget_version_id
11723                                                          ,p_rev_budget_version_id  => x_rev_budget_version_id
11724                                                          ,p_all_budget_version_id  => x_all_budget_version_id
11725 --                                                         ,p_version_type           => p_version_type  -- Bug 3902176
11726                                                          ,X_proj_raw_cost          => x_proj_raw_cost
11727                                                          ,X_proj_burdened_cost     => x_proj_burdened_cost
11728                                                          ,X_proj_revenue           => x_proj_revenue
11729                                                          ,x_labor_hrs_cost         => x_labor_hrs_cost
11730                                                          ,x_equipment_hrs_cost     => x_equipment_hrs_cost
11731                                                          ,x_labor_hrs_rev          => x_labor_hrs_rev
11732                                                          ,x_equipment_hrs_rev      => x_equipment_hrs_rev
11733                                                          ,X_margin                 => x_margin
11734                                                          ,X_margin_percent         => x_margin_percent
11735                                                          ,x_margin_derived_from_code => x_margin_derived_from_code -- Bug 3734840
11736                                                          ,x_return_status          => l_return_status
11737                                                          ,x_msg_data               => l_msg_data
11738                                                          ,X_msg_count              => l_msg_count);
11739 
11740                IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
11741                     IF P_PA_debug_mode = 'Y' THEN
11742                          pa_debug.g_err_stage:= 'Error in GET_PROJ_IMPACT_AMOUNTS';
11743                          pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11744                     END IF;
11745                    RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
11746                END IF;
11747            END IF;
11748       END IF;
11749 
11750 
11751       IF P_PA_debug_mode = 'Y' THEN
11752            pa_debug.g_err_stage:= 'Exiting GET_SUMMARY_AMOUNTS';
11753            pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11754 
11755 	      pa_debug.reset_curr_function;
11756 	END IF;
11757 EXCEPTION
11758       WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
11759           l_msg_count := FND_MSG_PUB.count_msg;
11760 
11761           IF l_msg_count = 1 and x_msg_data IS NULL THEN
11762                PA_INTERFACE_UTILS_PUB.get_messages
11763                      (p_encoded        => FND_API.G_TRUE
11764                       ,p_msg_index      => 1
11765                       ,p_msg_count      => l_msg_count
11766                       ,p_msg_data       => l_msg_data
11767                       ,p_data           => l_data
11768                       ,p_msg_index_out  => l_msg_index_out);
11769                x_msg_data := l_data;
11770                x_msg_count := l_msg_count;
11771           ELSE
11772               x_msg_count := l_msg_count;
11773           END IF;
11774            x_return_status := FND_API.G_RET_STS_ERROR;
11775 
11776 IF P_PA_DEBUG_MODE = 'Y' THEN
11777            pa_debug.reset_curr_function;
11778 END IF;
11779            RETURN;
11780 
11781      WHEN Others THEN
11782           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
11783           x_msg_count     := 1;
11784           x_msg_data      := SQLERRM;
11785 
11786           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
11787                                   ,p_procedure_name  => 'GET_SUMMARY_AMOUNTS');
11788           IF P_PA_DEBUG_MODE = 'Y' THEN
11789                pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
11790                pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
11791           pa_debug.reset_curr_function;
11792 	END IF;
11793           RAISE;
11794 
11795 
11796 END GET_SUMMARY_AMOUNTS;
11797 
11798 
11799 /* Function returns 'Y' if budget version has budget lines with rejection code. */
11800 FUNCTION does_bv_have_rej_lines(p_budget_version_id IN pa_budget_versions.budget_version_id%TYPE)
11801 RETURN VARCHAR2
11802 IS
11803    l_exists  varchar2(1) := 'N';
11804 begin
11805     select 'Y'
11806     into  l_exists
11807     from  dual
11808     where exists
11809     (select 1
11810     from  pa_budget_lines
11811     where budget_version_id = p_budget_version_id
11812     and   (cost_rejection_code IS NOT NULL
11813     OR    revenue_rejection_code IS NOT NULL
11814     OR    burden_rejection_code IS NOT NULL
11815     OR    other_rejection_code IS NOT NULL
11816     OR    pc_cur_conv_rejection_code IS NOT NULL
11817     OR    pfc_cur_conv_rejection_code IS NOT NULL));
11818 
11819     return l_exists;
11820 
11821 exception
11822     when no_data_found then
11823         return 'N';
11824 
11825 end does_bv_have_rej_lines;
11826 
11827 --------------------------------------------------------------------------------
11828 -- This API is called during deleting a Rate Sch to check if the Rate Schedule
11829 -- is being reference by any Plan Type or not.
11830 -- In case if it is referenced then the 'N' is returned , or else 'Y' is returned
11831 --------------------------------------------------------------------------------
11832 FUNCTION check_delete_sch_ok(
11833          p_bill_rate_sch_id      IN   pa_std_bill_rate_schedules_all.bill_rate_sch_id%TYPE)
11834 RETURN VARCHAR2 IS
11835   --Start of variables used for debugging
11836       l_debug_mode         VARCHAR2(30);
11837       l_debug_level3       CONSTANT NUMBER := 3;
11838       l_debug_level5       CONSTANT NUMBER := 5;
11839       l_mod_name           VARCHAR2(100) := l_module_name;
11840   --End of variables used for debugging
11841 
11842       l_delete_ok        VARCHAR2(1);
11843 
11844 BEGIN
11845 
11846      l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
11847 
11848   -- Input Paramter Validations
11849      IF P_PA_debug_mode = 'Y' THEN
11850          pa_debug.g_err_stage:='Validating input parameters';
11851          pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11852      END IF;
11853 
11854      IF (p_bill_rate_sch_id IS NULL)
11855      THEN
11856          IF P_PA_debug_mode = 'Y' THEN
11857              pa_debug.g_err_stage:='p_bill_rate_sch_id is NULL';
11858              pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11859          END IF;
11860      END IF;
11861 
11862      BEGIN
11863       SELECT 'N'
11864         INTO l_delete_ok
11865         FROM DUAL
11866        WHERE EXISTS (SELECT 1
11867                        FROM PA_PROJ_FP_OPTIONS
11868                       WHERE RES_CLASS_RAW_COST_SCH_ID = p_bill_rate_sch_id OR
11869                             RES_CLASS_BILL_RATE_SCH_ID = p_bill_rate_sch_id OR
11870                             COST_EMP_RATE_SCH_ID = p_bill_rate_sch_id OR
11871                             COST_JOB_RATE_SCH_ID = p_bill_rate_sch_id OR
11872                             COST_NON_LABOR_RES_RATE_SCH_ID = p_bill_rate_sch_id OR
11873                             COST_RES_CLASS_RATE_SCH_ID = p_bill_rate_sch_id OR
11874                             REV_EMP_RATE_SCH_ID = p_bill_rate_sch_id OR
11875                             REV_JOB_RATE_SCH_ID = p_bill_rate_sch_id OR
11876                             REV_NON_LABOR_RES_RATE_SCH_ID = p_bill_rate_sch_id OR
11877                             REV_RES_CLASS_RATE_SCH_ID = p_bill_rate_sch_id);
11878      EXCEPTION
11879          WHEN NO_DATA_FOUND THEN
11880            l_delete_ok := 'Y';
11881      END;
11882 
11883     RETURN l_delete_ok;
11884 
11885 END check_delete_sch_ok;
11886 -----------------------------------------------------------------------------------
11887 
11888 --------------------------------------------------------------------------------
11889 -- This API is called during deleting a Burden Rate Sch to check if the Burden Rate
11890 -- Schedule is being reference by any Plan Type or not.
11891 -- In case if it is referenced then the 'N' is returned , or else 'Y' is returned
11892 --------------------------------------------------------------------------------
11893 FUNCTION check_delete_burd_sch_ok(
11894          p_ind_rate_sch_id      IN   pa_ind_rate_schedules_all_bg.ind_rate_sch_id%TYPE)
11895 RETURN VARCHAR2
11896 IS
11897   --Start of variables used for debugging
11898       l_debug_mode         VARCHAR2(30);
11899       l_debug_level3       CONSTANT NUMBER := 3;
11900       l_debug_level5       CONSTANT NUMBER := 5;
11901       l_mod_name           VARCHAR2(100) := l_module_name;
11902   --End of variables used for debugging
11903 
11904       l_delete_ok        VARCHAR2(1);
11905 
11906 BEGIN
11907 
11908      l_debug_mode  := NVL(FND_PROFILE.value('PA_DEBUG_MODE'),'N');
11909 
11910   -- Input Paramter Validations
11911      IF P_PA_debug_mode = 'Y' THEN
11912          pa_debug.g_err_stage:='Validating input parameters';
11913          pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11914      END IF;
11915 
11916      IF (p_ind_rate_sch_id IS NULL)
11917      THEN
11918          IF P_PA_debug_mode = 'Y' THEN
11919              pa_debug.g_err_stage:='p_ind_rate_sch_id is NULL';
11920              pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11921          END IF;
11922      END IF;
11923 
11924      BEGIN
11925       SELECT 'N'
11926         INTO l_delete_ok
11927         FROM DUAL
11928        WHERE EXISTS (SELECT 1
11929                        FROM PA_PROJ_FP_OPTIONS
11930                       WHERE COST_BURDEN_RATE_SCH_ID = p_ind_rate_sch_id);
11931 
11932      EXCEPTION
11933          WHEN NO_DATA_FOUND THEN
11934            l_delete_ok := 'Y';
11935      END;
11936 
11937     RETURN l_delete_ok;
11938 
11939 END check_delete_burd_sch_ok;
11940 
11941 
11942 /* -------------------------------------------------------------------------------------------
11943  * FUNCTION: Validate_Uncheck_MC_Flag
11944  * Function to check for the validity of the event of unchecking of 'Plan in Multi Currency'
11945  * check box in the 'Edit Planning Options' screen. This api is called just before committing
11946  * the changes done in the page and is called for both workplan and budgeting and forecasting
11947  * context and this is indicated by the value of input parameter p_context, for which the
11948  * valid values are 'WORKPLAN' and 'FINPLAN'. If the context is 'WORKPLAN' the input parameter
11949  * p_budget_version_id would be null. The api returns 'Y' if the event is valid and allowed
11950  * and returns 'N' otherwise.
11951  *--------------------------------------------------------------------------------------------*/
11952  FUNCTION Validate_Uncheck_MC_Flag (
11953               p_project_id             IN         pa_projects_all.project_id%TYPE,
11954               p_context                IN         VARCHAR2,
11955               p_budget_version_id      IN         pa_budget_versions.budget_version_id%TYPE)
11956  RETURN  VARCHAR2
11957  IS
11958       --Start of variables used for debugging
11959       l_debug_mode         VARCHAR2(30);
11960       l_debug_level2       CONSTANT NUMBER := 2;
11961       l_debug_level3       CONSTANT NUMBER := 3;
11962       l_debug_level5       CONSTANT NUMBER := 5;
11963       l_mod_name           VARCHAR2(100)   := l_module_name || ':Validate_Uncheck_MC_Flag';
11964       l_token_name         VARCHAR2(30) :='PROCEDURENAME';
11965       --End of variables used for debugging
11966       l_currency_code      VARCHAR2(30);
11967       is_valid_flag        VARCHAR2(30);
11968 
11969  BEGIN
11970      IF P_PA_DEBUG_MODE = 'Y' THEN
11971       pa_debug.set_curr_function( p_function   => 'Validate_Uncheck_MC_Flag',
11972                                   p_debug_mode => P_PA_DEBUG_MODE );
11973 
11974             pa_debug.g_err_stage:='Entering Validate_Uncheck_MC_Flag';
11975             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level2);
11976 
11977       -- Checking, if the input parameters are null
11978             pa_debug.g_err_stage:='Validating Input Parameters';
11979             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11980       END IF;
11981 
11982       IF p_project_id IS NULL OR p_context IS NULL THEN
11983           IF P_PA_debug_mode = 'Y' THEN
11984               pa_debug.g_err_stage:='p_project_id = '||p_project_id;
11985               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11986 
11987               pa_debug.g_err_stage:='p_context = '||p_context;
11988               pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
11989 
11990           END IF;
11991 
11992           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
11993                                p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
11994                                p_token1         => l_token_name,
11995                                p_value1         => l_mod_name);
11996 
11997 
11998           IF P_PA_DEBUG_MODE = 'Y' THEN
11999                 pa_debug.g_err_stage:='Invalid Arguments Passed';
12000                 pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
12001                    pa_debug.reset_err_stack;
12002 	END IF;
12003           RAISE INVALID_ARG_EXC;
12004       ELSE
12005             IF p_context = 'FINPLAN' AND p_budget_version_id IS NULL THEN
12006                   PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
12007                                        p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
12008                                        p_token1         => l_token_name,
12009                                        p_value1         => l_mod_name);
12010 
12011 
12012                   IF P_PA_DEBUG_MODE = 'Y' THEN
12013                        pa_debug.g_err_stage:='Invalid Arguments Passed';
12014                        pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
12015                            pa_debug.reset_err_stack;
12016 		END IF;
12017                   RAISE INVALID_ARG_EXC;
12018             END IF;
12019       END IF;
12020 
12021       IF P_PA_DEBUG_MODE = 'Y' THEN
12022             pa_debug.g_err_stage:='Input Parameters validation done';
12023             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
12024       END IF;
12025 
12026       -- Getting the project currency code
12027       IF P_PA_DEBUG_MODE = 'Y' THEN
12028             pa_debug.g_err_stage:='Getting the project currency code';
12029             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
12030       END IF;
12031 
12032       SELECT project_currency_code
12033       INTO   l_currency_code
12034       FROM   pa_projects_all
12035       WHERE  project_id = p_project_id;
12036 
12037       IF P_PA_DEBUG_MODE = 'Y' THEN
12038             pa_debug.g_err_stage:='Project Currency Code: ' || l_currency_code;
12039             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level3);
12040       END IF;
12041 
12042       IF p_context = 'WORKPLAN' THEN
12043             BEGIN
12044                   SELECT   'N'
12045                   INTO     is_valid_flag
12046                   FROM     dual
12047                   WHERE    EXISTS (SELECT   1
12048                                    FROM     pa_budget_versions bv,
12049                                             pa_budget_lines    bl
12050                                    WHERE    bv.project_id = p_project_id
12051                                    AND      bv.wp_version_flag = 'Y'
12052                                    AND      bv.budget_version_id = bl.budget_version_id
12053                                    AND      bl.txn_currency_code <> l_currency_code);
12054             EXCEPTION
12055                  WHEN NO_DATA_FOUND THEN
12056                        is_valid_flag := 'Y';
12057             END;
12058       ELSIF p_context = 'FINPLAN' THEN
12059             BEGIN
12060                   SELECT   'N'
12061                   INTO     is_valid_flag
12062                   FROM     dual
12063                   WHERE    EXISTS (SELECT   1
12064                                    FROM     pa_budget_lines
12065                                    WHERE    budget_version_id = p_budget_version_id
12066                                    AND      txn_currency_code <> l_currency_code);
12067             EXCEPTION
12068                  WHEN NO_DATA_FOUND THEN
12069                        is_valid_flag := 'Y';
12070             END;
12071       END IF;
12072 
12073       IF P_PA_DEBUG_MODE = 'Y' THEN
12074             pa_debug.g_err_stage:='Value returned: ' || is_valid_flag;
12075             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level2);
12076       END IF;
12077 
12078       IF P_PA_DEBUG_MODE = 'Y' THEN
12079             pa_debug.g_err_stage:='Leaving Validate_Uncheck_MC_Flag';
12080             pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level2);
12081 	      pa_debug.reset_curr_function;
12082 	END IF;
12083       RETURN is_valid_flag;
12084 
12085  EXCEPTION
12086       WHEN OTHERS THEN
12087             FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
12088                                     ,p_procedure_name  => 'Validate_Uncheck_MC_Flag');
12089             IF P_PA_DEBUG_MODE = 'Y' THEN
12090                   pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
12091                   pa_debug.write(l_mod_name,pa_debug.g_err_stage,l_debug_level5);
12092             pa_debug.reset_curr_function;
12093 	END IF;
12094             RAISE;
12095  END Validate_Uncheck_MC_Flag;
12096 
12097 /*=============================================================================
12098  This api is called to check if a txn currency can be deleted for an fp option.
12099  For workplan case,
12100     A txn currency can not be deleted if
12101       1. the currency is project currency or
12102       2. the currency is project functional currency or
12103       3. amounts exist against the currency in any of the workplan versions
12104 
12105   For Budgets and Forecasting case,
12106     A txn currency can not be deleted if
12107       1. the currency is project currency or
12108       2. the currency is project functional currency or
12109       3. option is a version and amounts exist against the currency
12110 ==============================================================================*/
12111 
12112 FUNCTION Check_delete_txn_cur_ok(
12113           p_project_id            IN   pa_projects_all.project_id%TYPE
12114           ,p_context              IN   VARCHAR2 -- FINPLAN or WORKPLAN
12115           ,p_fin_plan_version_id  IN   pa_budget_versions.budget_version_id%TYPE
12116           ,p_txn_currency_code    IN   fnd_currencies.currency_code%TYPE
12117 ) RETURN VARCHAR2
12118 IS
12119    l_delete_ok_flag     varchar2(1);
12120 
12121    CURSOR project_info_cur IS
12122      SELECT project_currency_code
12123             ,projfunc_currency_code
12124      FROM   pa_projects_all
12125      WHERE  project_id = p_project_id;
12126    project_info_rec  project_info_cur%ROWTYPE;
12127 BEGIN
12128     -- Set curr function
12129  IF P_PA_DEBUG_MODE = 'Y' THEN
12130     pa_debug.set_curr_function(
12131                 p_function   =>'PA_FIN_PLAN_UTILS.Check_delete_txn_cur_ok'
12132                ,p_debug_mode => P_PA_DEBUG_MODE );
12133 
12134     -- Validate input parameters
12135         pa_debug.g_err_stage:='Validating input parameters';
12136         pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,3);
12137     END IF;
12138 
12139     IF (p_project_id IS NULL) OR
12140        (p_context    IS NULL) OR
12141        (p_context = 'FINPLAN' AND nvl(p_fin_plan_version_id, -99) = -99) OR
12142        (p_txn_currency_code IS NULL)
12143     THEN
12144 
12145         IF P_PA_DEBUG_MODE = 'Y' THEN
12146            pa_debug.g_err_stage:='p_project_id = '||p_project_id;
12147            pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,5);
12148 
12149            pa_debug.g_err_stage:='p_context = '||p_context;
12150            pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,5);
12151 
12152            pa_debug.g_err_stage:='p_fin_plan_version_id = '||p_fin_plan_version_id;
12153            pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,5);
12154 
12155            pa_debug.g_err_stage:='p_txn_currency_code = '||p_txn_currency_code;
12156            pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,5);
12157         END IF;
12158 
12159         PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PA',
12160                               p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
12161                               p_token1         => 'PROCEDURENAME',
12162                               p_value1         => 'PA_FIN_PLAN_UTILS.Check_delete_txn_cur_ok');
12163 
12164         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12165 
12166     END IF;
12167 
12168     -- Derive project and project funcional currencies
12169     OPEN project_info_cur;
12170     FETCH project_info_cur INTO
12171          project_info_rec;
12172     CLOSE project_info_cur;
12173 
12174     -- Initialising l_delete_ok_flag to 'Y'
12175     l_delete_ok_flag := 'Y';
12176 
12177     IF p_context = 'FINPLAN' THEN
12178 
12179          IF  p_txn_currency_code IN (project_info_rec.project_currency_code,
12180                                      project_info_rec.projfunc_currency_code)
12181          THEN
12182              l_delete_ok_flag := 'N';
12183          ELSE  -- Check if amounts exist against this currency
12184              BEGIN
12185                  SELECT 'N' INTO l_delete_ok_flag
12186                  FROM DUAL
12187                  WHERE EXISTS
12188                      ( select 1 from pa_budget_lines bl
12189                        where  bl.budget_version_id = p_fin_plan_version_id
12190                        and    bl.txn_currency_code = p_txn_currency_code
12191                      );
12192              EXCEPTION
12193                  WHEN NO_DATA_FOUND THEN
12194                       null;
12195              END;
12196          END IF;
12197     ELSIF p_context = 'WORKPLAN' THEN
12198 
12199          IF  p_txn_currency_code IN (project_info_rec.project_currency_code,
12200                                      project_info_rec.projfunc_currency_code)
12201          THEN
12202              l_delete_ok_flag := 'N';
12203          ELSE  -- Check if amounts exist against this currency
12204              BEGIN
12205                  SELECT 'N' INTO l_delete_ok_flag
12206                  FROM DUAL
12207                  WHERE EXISTS
12208                      ( select 1 from pa_budget_versions bv, pa_budget_lines bl
12209                        where  bv.project_id = p_project_id
12210                        and    bv.wp_version_flag = 'Y'
12211                        and    bl.budget_version_id = bv.budget_version_id
12212                        and    bl.txn_currency_code = p_txn_currency_code
12213                      );
12214              EXCEPTION
12215                  WHEN NO_DATA_FOUND THEN
12216                       null;
12217              END;
12218          END IF;
12219 
12220     END IF; -- p_context
12221 
12222     IF P_PA_DEBUG_MODE = 'Y' THEN
12223         pa_debug.g_err_stage:='Exiting Check_delete_txn_cur_ok';
12224         pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,3);
12225 
12226     -- reset curr function
12227     pa_debug.reset_curr_function();
12228 	END IF;
12229     RETURN l_delete_ok_flag;
12230 
12231 EXCEPTION
12232    WHEN Others THEN
12233 
12234        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
12235                                ,p_procedure_name  => 'Check_delete_txn_cur_ok');
12236 
12237        IF P_PA_DEBUG_MODE = 'Y' THEN
12238            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
12239            pa_debug.write('Check_delete_txn_cur_ok: ' || l_module_name,pa_debug.g_err_stage,5);
12240 
12241        -- reset curr function
12242           pa_debug.Reset_Curr_Function();
12243 	END IF;
12244        RAISE;
12245 END Check_delete_txn_cur_ok;
12246 
12247 /*=============================================================================
12248   This api is called to check if amounts exist for any of the workplan versions
12249   of the project in budgets data model.
12250 ==============================================================================*/
12251 
12252 FUNCTION check_if_amounts_exist_for_wp(
12253            p_project_id           IN   pa_projects_all.project_id%TYPE
12254 ) RETURN VARCHAR2
12255 IS
12256    l_amounts_exist_flag  VARCHAR2(1)  := 'N';
12257 BEGIN
12258     -- Set curr function
12259  IF P_PA_DEBUG_MODE = 'Y' THEN
12260     pa_debug.set_curr_function(
12261                 p_function   =>'PA_FIN_PLAN_UTILS.check_if_amounts_exist_for_wp'
12262                ,p_debug_mode => P_PA_DEBUG_MODE );
12263 
12264     -- Validate input parameters
12265 
12266         pa_debug.g_err_stage:='Validating input parameters';
12267         pa_debug.write('check_if_amounts_exist_for_wp: ' || l_module_name,pa_debug.g_err_stage,3);
12268     END IF;
12269 
12270     IF (p_project_id IS NULL)
12271     THEN
12272 
12273         IF P_PA_DEBUG_MODE = 'Y' THEN
12274            pa_debug.g_err_stage:='p_project_id = '||p_project_id;
12275            pa_debug.write('check_if_amounts_exist_for_wp: ' || l_module_name,pa_debug.g_err_stage,5);
12276         END IF;
12277 
12278         PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PA',
12279                               p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
12280                               p_token1         => 'PROCEDURENAME',
12281                               p_value1         => 'PA_FIN_PLAN_UTILS.check_if_amounts_exist_for_wp');
12282 
12283         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12284 
12285     END IF;
12286 
12287     -- Check if budget line exists for any of the workplan versions of the project
12288 	Begin
12289         SELECT 'Y' INTO l_amounts_exist_flag
12290         FROM dual WHERE EXISTS
12291             (SELECT 1
12292              FROM   pa_budget_lines bl,
12293                     pa_budget_versions bv
12294              WHERE  bv.project_id = p_project_id
12295              AND    bv.wp_version_flag = 'Y'
12296              AND    bl.budget_version_id = bv.budget_version_id);
12297     Exception
12298        When no_data_found Then
12299            l_amounts_exist_flag := 'N';
12300     End;
12301 
12302 
12303     IF P_PA_DEBUG_MODE = 'Y' THEN
12304         pa_debug.g_err_stage:='Exiting check_if_amounts_exist_for_wp';
12305         pa_debug.write('check_if_amounts_exist_for_wp: ' || l_module_name,pa_debug.g_err_stage,3);
12306 
12307     -- reset curr function
12308     pa_debug.reset_curr_function();
12309     END IF;
12310     RETURN l_amounts_exist_flag;
12311 
12312 EXCEPTION
12313    WHEN Others THEN
12314 
12315        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
12316                                ,p_procedure_name  => 'check_if_amounts_exist_for_wp');
12317 
12318        IF P_PA_DEBUG_MODE = 'Y' THEN
12319            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
12320            pa_debug.write('check_if_amounts_exist_for_wp: ' || l_module_name,pa_debug.g_err_stage,5);
12321 
12322        -- reset curr function
12323        pa_debug.Reset_Curr_Function();
12324 	END IF;
12325        RAISE;
12326 END check_if_amounts_exist_for_wp;
12327 
12328 /*=============================================================================
12329   This api is called to check if task assignments exist for any of the workplan
12330   versions of the given project
12331 ==============================================================================*/
12332 
12333 FUNCTION check_if_task_asgmts_exist(
12334            p_project_id           IN   pa_projects_all.project_id%TYPE
12335 ) RETURN VARCHAR2
12336 IS
12337    l_task_assignments_exist_flag  VARCHAR2(1);
12338 BEGIN
12339     -- Set curr function
12340  IF P_PA_DEBUG_MODE = 'Y' THEN
12341     pa_debug.set_curr_function(
12342                 p_function   =>'PA_FIN_PLAN_UTILS.check_if_task_asgmts_exist'
12343                ,p_debug_mode => P_PA_DEBUG_MODE );
12344 
12345     -- Validate input parameters
12346 
12347         pa_debug.g_err_stage:='Validating input parameters';
12348         pa_debug.write('check_if_task_asgmts_exist: ' || l_module_name,pa_debug.g_err_stage,3);
12349     END IF;
12350 
12351     IF (p_project_id IS NULL)
12352     THEN
12353 
12354         IF P_PA_DEBUG_MODE = 'Y' THEN
12355            pa_debug.g_err_stage:='p_project_id = '||p_project_id;
12356            pa_debug.write('check_if_task_asgmts_exist: ' || l_module_name,pa_debug.g_err_stage,5);
12357         END IF;
12358 
12359         PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PA',
12360                               p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
12361                               p_token1         => 'PROCEDURENAME',
12362                               p_value1         => 'PA_FIN_PLAN_UTILS.check_if_task_asgmts_exist');
12363 
12364         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12365 
12366     END IF;
12367 
12368     -- Check if task assignments exist for any of the workplan versions of the project
12369     Begin
12370         SELECT 'Y' INTO l_task_assignments_exist_flag
12371         FROM dual WHERE EXISTS
12372             (SELECT 1
12373              FROM   pa_budget_versions bv,
12374                     pa_resource_assignments ra
12375              WHERE  bv.project_id = p_project_id
12376              AND    bv.wp_version_flag = 'Y'
12377              AND    ra.budget_version_id = bv.budget_version_id
12378              AND    ra.ta_display_flag = 'Y');
12379     Exception
12380        When no_data_found Then
12381            l_task_assignments_exist_flag := 'N';
12382     End;
12383 
12384 
12385     IF P_PA_DEBUG_MODE = 'Y' THEN
12386         pa_debug.g_err_stage:='Exiting check_if_task_asgmts_exist';
12387         pa_debug.write('check_if_task_asgmts_exist: ' || l_module_name,pa_debug.g_err_stage,3);
12388 
12389     -- reset curr function
12390 	    pa_debug.reset_curr_function();
12391 	END IF;
12392     RETURN l_task_assignments_exist_flag;
12393 
12394 EXCEPTION
12395    WHEN Others THEN
12396 
12397        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
12398                                ,p_procedure_name  => 'check_if_task_asgmts_exist');
12399 
12400        IF P_PA_DEBUG_MODE = 'Y' THEN
12401            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
12402            pa_debug.write('check_if_task_asgmts_exist: ' || l_module_name,pa_debug.g_err_stage,5);
12403 
12404        -- reset curr function
12405        pa_debug.Reset_Curr_Function();
12406 	END IF;
12407        RAISE;
12408 END check_if_task_asgmts_exist;
12409 
12410 /*=============================================================================
12411   This api is called to check if amounts exist for any of the budget versions
12412   of the project - plan type combination. This is used as of now to restrict
12413   RBS change at plan type level.
12414 ==============================================================================*/
12415 
12416 FUNCTION check_if_amounts_exist_for_fp(
12417            p_project_id           IN   pa_projects_all.project_id%TYPE
12418            ,p_fin_plan_type_id    IN   pa_fin_plan_types_b.fin_plan_type_id %TYPE
12419 ) RETURN VARCHAR2
12420 IS
12421    l_amounts_exist_flag  VARCHAR2(1)  := 'N';
12422 BEGIN
12423     -- Set curr function
12424  IF P_PA_DEBUG_MODE = 'Y' THEN
12425     pa_debug.set_curr_function(
12426                 p_function   =>'PA_FIN_PLAN_UTILS.check_if_amounts_exist_for_fp'
12427                ,p_debug_mode => P_PA_DEBUG_MODE );
12428 
12429     -- Validate input parameters
12430 
12431         pa_debug.g_err_stage:='Validating input parameters';
12432         pa_debug.write('check_if_amounts_exist_for_fp: ' || l_module_name,pa_debug.g_err_stage,3);
12433     END IF;
12434 
12435     IF (p_project_id IS NULL) OR (p_fin_plan_type_id IS NULL)
12436     THEN
12437 
12438         IF P_PA_DEBUG_MODE = 'Y' THEN
12439            pa_debug.g_err_stage:='p_project_id = '||p_project_id;
12440            pa_debug.write('check_if_amounts_exist_for_fp: ' || l_module_name,pa_debug.g_err_stage,5);
12441 
12442            pa_debug.g_err_stage:='p_fin_plan_type_id = '||p_fin_plan_type_id;
12443            pa_debug.write('check_if_amounts_exist_for_fp: ' || l_module_name,pa_debug.g_err_stage,5);
12444         END IF;
12445 
12446         PA_UTILS.ADD_MESSAGE( p_app_short_name => 'PA',
12447                               p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
12448                               p_token1         => 'PROCEDURENAME',
12449                               p_value1         => 'PA_FIN_PLAN_UTILS.check_if_amounts_exist_for_fp');
12450 
12451         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12452 
12453     END IF;
12454 
12455     -- Check if budget line exists for any of the budget versions of the project-plan type
12456 	Begin
12457         SELECT 'Y' INTO l_amounts_exist_flag
12458         FROM dual WHERE EXISTS
12459             (SELECT 1
12460              FROM   pa_budget_lines bl,
12461                     pa_budget_versions bv
12462              WHERE  bv.project_id = p_project_id
12463              AND    bv.fin_plan_type_id = p_fin_plan_type_id
12464              AND    bl.budget_version_id = bv.budget_version_id);
12465     Exception
12466        When no_data_found Then
12467            l_amounts_exist_flag := 'N';
12468     End;
12469 
12470 
12471     IF P_PA_DEBUG_MODE = 'Y' THEN
12472         pa_debug.g_err_stage:='Exiting check_if_amounts_exist_for_fp';
12473         pa_debug.write('check_if_amounts_exist_for_fp: ' || l_module_name,pa_debug.g_err_stage,3);
12474 
12475     -- reset curr function
12476 	    pa_debug.reset_curr_function();
12477 	END IF;
12478     RETURN l_amounts_exist_flag;
12479 
12480 EXCEPTION
12481    WHEN Others THEN
12482 
12483        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_UTILS'
12484                                ,p_procedure_name  => 'check_if_amounts_exist_for_fp');
12485 
12486        IF P_PA_DEBUG_MODE = 'Y' THEN
12487            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
12488            pa_debug.write('check_if_amounts_exist_for_fp: ' || l_module_name,pa_debug.g_err_stage,5);
12489 
12490        -- reset curr function
12491           pa_debug.Reset_Curr_Function();
12492 	END IF;
12493        RAISE;
12494 END check_if_amounts_exist_for_fp;
12495 
12496 /*===================================================================================
12497  This api is used to validate the plan processing code if it passed, or to return
12498  the same for the budget version id that is passed in budget context or for the
12499  ci_id passed for the CI version context and throw an error in case they are not valid
12500 =====================================================================================*/
12501 
12502 PROCEDURE return_and_vldt_plan_prc_code
12503 (
12504        p_add_msg_to_stack             IN       VARCHAR2
12505       ,p_calling_context              IN       VARCHAR2
12506       ,p_budget_version_id            IN       pa_budget_versions.budget_version_id%TYPE
12507       ,p_source_ci_id_tbl             IN       SYSTEM.pa_num_tbl_type
12508       ,p_target_ci_id                 IN       pa_control_items.ci_id%TYPE
12509       ,p_plan_processing_code         IN       pa_budget_versions.plan_processing_code%TYPE
12510       ,x_final_plan_prc_code          OUT      NOCOPY pa_budget_versions.plan_processing_code%TYPE --File.Sql.39 bug 4440895
12511       ,x_targ_request_id              OUT      NOCOPY pa_budget_versions.request_id%TYPE --File.Sql.39 bug 4440895
12512       ,x_return_status                OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
12513       ,x_msg_count                    OUT      NOCOPY NUMBER --File.Sql.39 bug 4440895
12514       ,x_msg_data                     OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
12515 ) IS
12516 
12517     --Start of variables used for debugging
12518     l_return_status                            VARCHAR2(1);
12519     l_msg_count                                NUMBER := 0;
12520     l_msg_data                                 VARCHAR2(2000);
12521     l_data                                     VARCHAR2(2000);
12522     l_msg_index_out                            NUMBER;
12523     l_debug_mode                               VARCHAR2(30);
12524     l_debug_level3                             CONSTANT NUMBER :=3;
12525     l_debug_level5                             CONSTANT NUMBER :=5;
12526 
12527     --End of variables used for debugging
12528     l_module_name                              VARCHAR2(200) :=  'PAFPUTLB.return_and_vldt_plan_prc_code';
12529     l_plan_processing_code                     VARCHAR2(30);
12530 
12531     l_src_ci_impact_type_tbl                   SYSTEM.pa_varchar2_30_tbl_type := SYSTEM.pa_varchar2_30_tbl_type();
12532     l_no_of_targ_ci_version                    NUMBER;
12533     l_no_of_src_ci_version                     NUMBER;
12534     l_targ_ci_ver_plan_prc_code                pa_budget_versions.plan_processing_code%TYPE;
12535     l_targ_cost_ci_ver_plan_prc_cd             pa_budget_versions.plan_processing_code%TYPE;
12536     l_targ_rev_ci_ver_plan_prc_cd              pa_budget_versions.plan_processing_code%TYPE;
12537     l_targ_cost_ci_err_flag                    VARCHAR2(1)   := 'N';
12538     l_targ_rev_ci_err_flag                     VARCHAR2(1)   := 'N';
12539     l_incomp_imapact_exists                    VARCHAR2(1)   := 'N';
12540     l_targ_request_id                          pa_budget_versions.request_id%TYPE;
12541     l_targ_cost_ci_req_id                      pa_budget_versions.request_id%TYPE;
12542     l_targ_rev_ci_req_id                       pa_budget_versions.request_id%TYPE;
12543     l_targ_ci_request_id                       pa_budget_versions.request_id%TYPE;
12544 
12545 BEGIN
12546 
12547     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
12548 
12549     x_msg_count := 0;
12550     x_return_status := FND_API.G_RET_STS_SUCCESS;
12551 
12552     IF l_debug_mode = 'Y' THEN
12553           PA_DEBUG.Set_Curr_Function( p_function   => l_module_name,
12554                                       p_debug_mode => l_debug_mode );
12555     END IF;
12556     IF l_debug_mode = 'Y' THEN
12557           pa_debug.g_err_stage := 'Entering into pa.plsql.pa_fin_plan_utils.return_and_vldt_plan_prc_code';
12558           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
12559           pa_debug.g_err_stage := 'Validating input parameters';
12560           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
12561     END IF;
12562 
12563     IF p_calling_context = 'BUDGET' THEN
12564           IF p_budget_version_id IS NULL AND
12565              p_plan_processing_code IS  NULL THEN
12566 
12567               IF l_debug_mode = 'Y' THEN
12568                   pa_debug.g_err_stage:='p_budget_version_id: '|| p_budget_version_id ;
12569                   pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
12570 
12571                   pa_debug.g_err_stage:='p_plan_processing_code: '|| p_plan_processing_code ;
12572                   pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
12573               END IF;
12574 
12575               PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
12576                                    p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
12577               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12578           END IF;
12579 
12580     ELSIF p_calling_context = 'CI' THEN
12581           IF p_source_ci_id_tbl.COUNT = 0 AND
12582              p_target_ci_id IS  NULL THEN
12583 
12584               IF l_debug_mode = 'Y' THEN
12585                   pa_debug.g_err_stage:='p_source_ci_id_tbl.COUNT: '|| p_source_ci_id_tbl.COUNT ;
12586                   pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
12587 
12588                   pa_debug.g_err_stage:='p_target_ci_id: '|| p_target_ci_id ;
12589                   pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
12590               END IF;
12591 
12592               PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
12593                                    p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
12594               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12595           END IF;
12596     END IF;
12597 
12598     IF p_calling_context = 'BUDGET' THEN
12599           IF p_plan_processing_code IS NULL THEN
12600 
12601               SELECT plan_processing_code,
12602                      request_id
12603               INTO   l_plan_processing_code,
12604                      l_targ_request_id
12605               FROM   pa_budget_versions
12606               WHERE  budget_version_id = p_budget_version_id;
12607           ELSE
12608               l_plan_processing_code := p_plan_processing_code;
12609           END IF;
12610 
12611           IF p_add_msg_to_stack = 'Y' THEN
12612                 IF l_plan_processing_code = 'XLUP' THEN
12613                       PA_UTILS.ADD_MESSAGE
12614                            (p_app_short_name => 'PA',
12615                             p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
12616                       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12617                 ELSIF l_plan_processing_code = 'XLUE' THEN
12618                       PA_UTILS.ADD_MESSAGE
12619                            (p_app_short_name => 'PA',
12620                             p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
12621                       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12622                 END IF;
12623           END IF;
12624 
12625           x_final_plan_prc_code := l_plan_processing_code;
12626           x_targ_request_id := l_targ_request_id;
12627     ELSIF p_calling_context = 'CI' THEN
12628           BEGIN
12629                 SELECT COUNT(*)
12630                 INTO   l_no_of_targ_ci_version
12631                 FROM   pa_budget_versions
12632                 WHERE  ci_id = p_target_ci_id;
12633 
12634                 IF l_no_of_targ_ci_version = 1 THEN
12635                      -- irrespective of the version type for the target CI version
12636                      -- fetch the plan_processing_code
12637                      SELECT plan_processing_code,
12638                             request_id
12639                      INTO   l_targ_ci_ver_plan_prc_code,
12640                             l_targ_request_id
12641                      FROM   pa_budget_versions
12642                      WHERE  ci_id = p_target_ci_id;
12643 
12644                      IF p_add_msg_to_stack = 'Y' THEN
12645                         IF l_targ_ci_ver_plan_prc_code = 'XLUP' THEN
12646                             PA_UTILS.ADD_MESSAGE
12647                                  (p_app_short_name => 'PA',
12648                                   p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
12649                             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12650                         ELSIF l_targ_ci_ver_plan_prc_code = 'XLUE' THEN
12651                             PA_UTILS.ADD_MESSAGE
12652                                  (p_app_short_name => 'PA',
12653                                   p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
12654                             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12655                         END IF;
12656                      ELSE
12657                         x_final_plan_prc_code := l_targ_ci_ver_plan_prc_code;
12658                         x_targ_request_id := l_targ_request_id;
12659                      END IF;
12660                 -- if there are two versions for the CI
12661                 ELSE
12662                      SELECT plan_processing_code,
12663                             request_id
12664                      INTO   l_targ_cost_ci_ver_plan_prc_cd,
12665                             l_targ_cost_ci_req_id
12666                      FROM   pa_budget_versions
12667                      WHERE  ci_id = p_target_ci_id
12668                      AND    version_type = 'COST';
12669 
12670                      SELECT plan_processing_code,
12671                             request_id
12672                      INTO   l_targ_rev_ci_ver_plan_prc_cd,
12673                             l_targ_rev_ci_req_id
12674                      FROM   pa_budget_versions
12675                      WHERE  ci_id = p_target_ci_id
12676                      AND    version_type = 'REVENUE';
12677 
12678 
12679                      IF l_targ_cost_ci_ver_plan_prc_cd = 'XLUP' OR
12680                         l_targ_cost_ci_ver_plan_prc_cd = 'XLUE' THEN
12681                             l_targ_ci_ver_plan_prc_code := l_targ_cost_ci_ver_plan_prc_cd;
12682                             l_targ_ci_request_id := l_targ_cost_ci_req_id;
12683                             l_targ_cost_ci_err_flag := 'Y';
12684                      ELSE
12685                             l_targ_ci_ver_plan_prc_code := null;
12686                             l_targ_ci_request_id := null;
12687                      END IF;
12688 
12689                      IF l_targ_rev_ci_ver_plan_prc_cd = 'XLUP' OR
12690                         l_targ_rev_ci_ver_plan_prc_cd = 'XLUE' THEN
12691                             l_targ_ci_ver_plan_prc_code := l_targ_rev_ci_ver_plan_prc_cd;
12692                             l_targ_ci_request_id := l_targ_rev_ci_req_id;
12693                             l_targ_rev_ci_err_flag := 'Y';
12694                      ELSE
12695                             l_targ_ci_ver_plan_prc_code := null;
12696                             l_targ_ci_request_id := null;
12697                      END IF;
12698 
12699                      -- if both the target CI versions are not accessible, then staright away throw error
12700                      -- if both the target CI versions are valid for access, then don't return error
12701                      -- if either of the target CI version has some process lock/error,
12702                      -- loop thru the source ci id table to find out the impact type of each source CI
12703                      -- if the source has an impact type for which the target version is accessible, then
12704                      -- no need to throw error, otherwise the merge should be disallowed.
12705 
12706                      IF l_targ_cost_ci_err_flag = 'Y' AND
12707                         l_targ_rev_ci_err_flag = 'Y' THEN
12708                               IF p_add_msg_to_stack = 'Y' THEN
12709                                     IF l_targ_ci_ver_plan_prc_code = 'XLUP' THEN
12710                                         PA_UTILS.ADD_MESSAGE
12711                                              (p_app_short_name => 'PA',
12712                                               p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
12713                                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12714                                     ELSIF l_targ_ci_ver_plan_prc_code = 'XLUE' THEN
12715                                         PA_UTILS.ADD_MESSAGE
12716                                              (p_app_short_name => 'PA',
12717                                               p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
12718                                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12719                                     END IF;
12720                               ELSE
12721                                     x_final_plan_prc_code := l_targ_ci_ver_plan_prc_code;
12722                                     x_targ_request_id := l_targ_ci_request_id;
12723                               END IF;
12724                      ELSIF l_targ_cost_ci_err_flag = 'N' AND
12725                            l_targ_rev_ci_err_flag = 'N' THEN
12726                               -- do nothing just return
12727                               x_final_plan_prc_code := null;
12728                               x_targ_request_id := null;
12729                      ELSE
12730                            IF p_source_ci_id_tbl.COUNT > 0 THEN
12731                               -- call a function to get the impact type of all the source ci_ids
12732                               FOR i IN p_source_ci_id_tbl.FIRST .. p_source_ci_id_tbl.LAST LOOP
12733                                     l_src_ci_impact_type_tbl.EXTEND(1);
12734                                     l_src_ci_impact_type_tbl(l_src_ci_impact_type_tbl.COUNT) := PA_FP_CONTROL_ITEMS_UTILS.is_impact_exists(p_source_ci_id_tbl(i));
12735                               END LOOP;
12736 
12737                               IF l_targ_cost_ci_err_flag = 'Y' AND
12738                                    l_targ_rev_ci_err_flag = 'N' THEN
12739                                          -- check if atleast any of the source CI has cost impact
12740                                          IF l_src_ci_impact_type_tbl.COUNT > 0 THEN
12741                                                 FOR i IN l_src_ci_impact_type_tbl.FIRST .. l_src_ci_impact_type_tbl.LAST LOOP
12742                                                       IF l_src_ci_impact_type_tbl(i) = 'COST' THEN
12743                                                             l_incomp_imapact_exists := 'Y';
12744                                                             EXIT;
12745                                                       END IF;
12746                                                 END LOOP;
12747                                          END IF;
12748                                          IF l_incomp_imapact_exists = 'Y' THEN
12749                                                -- throw error
12750                                                IF p_add_msg_to_stack = 'Y' THEN
12751                                                      IF l_targ_ci_ver_plan_prc_code = 'XLUP' THEN
12752                                                          PA_UTILS.ADD_MESSAGE
12753                                                               (p_app_short_name => 'PA',
12754                                                                p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
12755                                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12756                                                      ELSIF l_targ_ci_ver_plan_prc_code = 'XLUE' THEN
12757                                                          PA_UTILS.ADD_MESSAGE
12758                                                               (p_app_short_name => 'PA',
12759                                                                p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
12760                                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12761                                                      END IF;
12762                                                ELSE
12763                                                      x_final_plan_prc_code := l_targ_ci_ver_plan_prc_code;
12764                                                      x_targ_request_id := l_targ_ci_request_id;
12765                                                END IF;
12766                                          ELSE
12767                                                -- do nothing just return
12768                                                x_final_plan_prc_code := null;
12769                                          END IF;
12770                               ELSIF l_targ_cost_ci_err_flag = 'N' AND
12771                                       l_targ_rev_ci_err_flag = 'Y' THEN
12772                                          -- check if atleast any of the source CI has cost impact
12773                                          IF l_src_ci_impact_type_tbl.COUNT > 0 THEN
12774                                                 FOR i IN l_src_ci_impact_type_tbl.FIRST .. l_src_ci_impact_type_tbl.LAST LOOP
12775                                                       IF l_src_ci_impact_type_tbl(i) = 'REVENUE' THEN
12776                                                             l_incomp_imapact_exists := 'Y';
12777                                                             EXIT;
12778                                                       END IF;
12779                                                 END LOOP;
12780                                          END IF;
12781                                          IF l_incomp_imapact_exists = 'Y' THEN
12782                                                -- throw error
12783                                                IF p_add_msg_to_stack = 'Y' THEN
12784                                                      IF l_targ_ci_ver_plan_prc_code = 'XLUP' THEN
12785                                                          PA_UTILS.ADD_MESSAGE
12786                                                               (p_app_short_name => 'PA',
12787                                                                p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
12788                                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12789                                                      ELSIF l_targ_ci_ver_plan_prc_code = 'XLUE' THEN
12790                                                          PA_UTILS.ADD_MESSAGE
12791                                                               (p_app_short_name => 'PA',
12792                                                                p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
12793                                                          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12794                                                      END IF;
12795                                                ELSE
12796                                                      x_final_plan_prc_code := l_targ_ci_ver_plan_prc_code;
12797                                                      x_targ_request_id := l_targ_ci_request_id;
12798                                                END IF;
12799                                          ELSE
12800                                                -- do nothing just return
12801                                                x_final_plan_prc_code := null;
12802                                                x_targ_request_id := null;
12803                                          END IF;
12804                               END IF;
12805                            ELSE -- if source ci_id tbl is null
12806                               -- raise error for the failed version
12807                               IF l_targ_cost_ci_err_flag = 'Y'  OR
12808                                  l_targ_cost_ci_err_flag = 'Y' THEN
12809                                     IF p_add_msg_to_stack = 'Y' THEN
12810                                           IF l_targ_ci_ver_plan_prc_code = 'XLUP' THEN
12811                                               PA_UTILS.ADD_MESSAGE
12812                                                    (p_app_short_name => 'PA',
12813                                                     p_msg_name       => 'PA_FP_LOCKED_BY_PROCESSING');
12814                                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12815                                           ELSIF l_targ_ci_ver_plan_prc_code = 'XLUE' THEN
12816                                               PA_UTILS.ADD_MESSAGE
12817                                                    (p_app_short_name => 'PA',
12818                                                     p_msg_name       => 'PA_FP_WA_CONC_PRC_FAILURE_MSG');
12819                                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
12820                                           END IF;
12821                                     ELSE
12822                                           x_final_plan_prc_code := l_targ_ci_ver_plan_prc_code;
12823                                           x_targ_request_id := l_targ_ci_request_id;
12824                                     END IF;
12825                               END IF;
12826                            END IF; -- if source ci_id tbl is not null
12827                      END IF; -- if either cost or rev version has error
12828                 END IF; -- 2 target ci versions
12829           EXCEPTION
12830                 WHEN NO_DATA_FOUND THEN
12831                       x_final_plan_prc_code := null;
12832           END;
12833     END IF; -- p_calling_context
12834 
12835     IF l_debug_mode = 'Y' THEN
12836           pa_debug.g_err_stage := 'Leaving into pa.plsql.pa_fin_plan_utils.return_and_vldt_plan_prc_code';
12837           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level3);
12838 
12839 	    pa_debug.reset_curr_function();
12840 	END IF;
12841 EXCEPTION
12842 
12843    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
12844        l_msg_count := FND_MSG_PUB.count_msg;
12845        IF l_msg_count = 1 THEN
12846            PA_INTERFACE_UTILS_PUB.get_messages
12847                  (p_encoded        => FND_API.G_TRUE
12848                   ,p_msg_index      => 1
12849                   ,p_msg_count      => l_msg_count
12850                   ,p_msg_data       => l_msg_data
12851                   ,p_data           => l_data
12852                   ,p_msg_index_out  => l_msg_index_out);
12853 
12854            x_msg_data := l_data;
12855            x_msg_count := l_msg_count;
12856        ELSE
12857            x_msg_count := l_msg_count;
12858        END IF;
12859        x_return_status := FND_API.G_RET_STS_ERROR;
12860 
12861        IF l_debug_mode = 'Y' THEN
12862            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
12863            pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
12864 
12865        -- reset curr function
12866 	       pa_debug.reset_curr_function();
12867 	END IF;
12868        RETURN;
12869    WHEN OTHERS THEN
12870        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
12871        x_msg_count     := 1;
12872        x_msg_data      := SQLERRM;
12873 
12874        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'pa_fin_plan_utils'
12875                                ,p_procedure_name  => 'return_and_vldt_plan_prc_code');
12876 
12877        IF l_debug_mode = 'Y' THEN
12878            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
12879            pa_debug.write( l_module_name,pa_debug.g_err_stage,l_debug_level5);
12880        -- reset curr function
12881        pa_debug.Reset_Curr_Function();
12882 	END IF;
12883        RAISE;
12884 
12885 END return_and_vldt_plan_prc_code;
12886 
12887 --Bug: 3619687 Added a function to check that the preference code the plan type is not used as a generation source for any other plan type
12888 
12889 FUNCTION Is_source_for_gen_options
12890           (p_project_id                   IN   pa_projects_all.project_id%TYPE
12891           ,p_fin_plan_type_id             IN   pa_fin_plan_types_b.fin_plan_type_id%TYPE
12892           ,p_preference_code              IN   pa_proj_fp_options.fin_plan_preference_code%TYPE
12893           ) RETURN VARCHAR2 IS
12894 
12895        l_valid_status  VARCHAR2(1)  := 'S';
12896        l_exists        VARCHAR2(1);
12897 
12898 BEGIN
12899 
12900            IF P_PA_DEBUG_MODE = 'Y' THEN
12901               pa_debug.init_err_stack ('pa_fin_plan_utils.Is_source_for_generation_options');
12902            END IF;
12903 
12904     /* Changes for FP.M, Tracking Bug No - 3619687. Making a check if the plan version/type is
12905     a source of generation fot other plan types*/
12906     BEGIN
12907     IF ( p_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY) THEN
12908          SELECT 1 INTO l_exists FROM dual WHERE EXISTS (SELECT fin_plan_type_id
12909          FROM PA_PROJ_FP_OPTIONS
12910          WHERE Project_id = p_project_id AND
12911          (GEN_SRC_REV_PLAN_TYPE_ID = p_fin_plan_type_id
12912          OR GEN_SRC_COST_PLAN_TYPE_ID= p_fin_plan_type_id
12913          OR GEN_SRC_ALL_PLAN_TYPE_ID = p_fin_plan_type_id));
12914          RETURN ('PA_FP_IS_GEN_OPTNS_SRC');
12915         END IF;
12916         EXCEPTION
12917            WHEN NO_DATA_FOUND THEN
12918               l_valid_status :='S';
12919     END;
12920     RETURN ( l_valid_status );
12921 END Is_source_for_gen_options;
12922 
12923 
12924  /* bug 4494740: The following function is included here which would return
12925     the percent complete for the financial structure version when the financial
12926     structure_version_id and the status_flags are passed as input. This function
12927     is introduced as part of performance improvement for FP.M excel download.
12928   */
12929   FUNCTION get_physical_pc_complete
12930         ( p_project_id                  IN           pa_projects_all.project_id%TYPE,
12931           p_proj_element_id             IN           pa_proj_element_versions.proj_element_id%TYPE)
12932 
12933   RETURN NUMBER IS
12934 
12935         l_msg_count            NUMBER;
12936         l_msg_data             VARCHAR2(2000);
12937         l_return_status        VARCHAR2(1);
12938 
12939         l_return_value         NUMBER;
12940         l_structure_status     VARCHAR2(30);
12941         l_physic_pc_complete   NUMBER;
12942 
12943   BEGIN
12944 
12945 --	Replaced the p_proj_element_id with nvl(p_proj_element_id,0) Bug 5335146
12946         IF g_fp_wa_task_pc_compl_tbl.EXISTS(NVL(p_proj_element_id,0)) THEN
12947             l_return_value := g_fp_wa_task_pc_compl_tbl(NVL(p_proj_element_id,0));
12948         ELSE
12949 
12950             IF g_fp_wa_struct_status_flag = 'Y' THEN
12951                 l_structure_status := 'PUBLISHED';
12952             ELSE
12953                 l_structure_status := 'WORKING';
12954             END IF;
12955 
12956             PA_PROGRESS_UTILS.REDEFAULT_BASE_PC
12957                (p_project_id             => p_project_id,
12958                 p_proj_element_id        => p_proj_element_id,
12959                 p_structure_type         => 'FINANCIAL',
12960                 p_object_type            => 'PA_TASKS',
12961                 p_as_of_date             => trunc(SYSDATE),
12962                 p_structure_version_id   => g_fp_wa_struct_ver_id,
12963                 p_structure_status       => l_structure_status,
12964                 p_calling_context        => 'FINANCIAL_PLANNING',
12965                 x_base_percent_complete  => l_physic_pc_complete,
12966                 x_return_status          => l_return_status,
12967                 x_msg_count              => l_msg_count,
12968                 x_msg_data               => l_msg_data);
12969 
12970             IF l_return_status = 'S' THEN
12971                 l_return_value := l_physic_pc_complete;
12972                 g_fp_wa_task_pc_compl_tbl(NVL(p_proj_element_id,0)) := l_physic_pc_complete;
12973             END IF;
12974         END IF;
12975 
12976         RETURN l_return_value;
12977 
12978   END get_physical_pc_complete;
12979 
12980   FUNCTION set_webadi_download_var
12981          (p_structure_version_id        IN           pa_proj_element_versions.parent_structure_version_id%TYPE,
12982           p_structure_status_flag       IN           VARCHAR2)
12983 
12984   RETURN VARCHAR2
12985   IS
12986        l_return_null_value            VARCHAR2(1);
12987   BEGIN
12988         g_fp_wa_struct_ver_id      := p_structure_version_id;
12989         g_fp_wa_struct_status_flag := p_structure_status_flag;
12990 
12991         RETURN l_return_null_value;
12992   END set_webadi_download_var;
12993 
12994   FUNCTION get_fp_wa_struct_ver_id
12995   RETURN NUMBER
12996   IS
12997   BEGIN
12998         RETURN g_fp_wa_struct_ver_id;
12999   END get_fp_wa_struct_ver_id;
13000 
13001 
13002   /* This procedure is called from FPWebadiAMImpl.java to get the structure version id
13003    * and the structure version status flag to be used as URL parameter for BNE URL
13004    */
13005   PROCEDURE return_struct_ver_info
13006         (p_budget_version_id    IN              pa_budget_versions.budget_version_id%TYPE,
13007          x_struct_version_id    OUT    NOCOPY   pa_proj_element_versions.parent_structure_version_id%TYPE,
13008          x_struct_status_flag   OUT    NOCOPY   VARCHAR2,
13009          x_return_status        OUT    NOCOPY   VARCHAR2,
13010          x_msg_count            OUT    NOCOPY   NUMBER,
13011          x_msg_data             OUT    NOCOPY   VARCHAR2)
13012 
13013   IS
13014         l_debug_mode           VARCHAR2(30);
13015         l_module_name          VARCHAR2(100) := 'PAFPWAUB.return_struct_ver_info';
13016         l_msg_count            NUMBER := 0;
13017         l_data                 VARCHAR2(2000);
13018         l_msg_data             VARCHAR2(2000);
13019         l_msg_index_out        NUMBER;
13020         l_debug_level3         CONSTANT NUMBER :=3;
13021         l_project_id           pa_projects_all.project_id%TYPE;
13022 
13023 
13024   BEGIN
13025         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
13026 
13027         x_msg_count := 0;
13028         x_return_status := FND_API.G_RET_STS_SUCCESS;
13029 
13030         IF l_debug_mode = 'Y' THEN
13031             PA_DEBUG.Set_Curr_Function(p_function   => l_module_name,
13032                                        p_debug_mode => l_debug_mode );
13033         END IF;
13034 
13035         IF l_debug_mode = 'Y' THEN
13036               pa_debug.g_err_stage:='Entering return_struct_ver_info';
13037               pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13038               pa_debug.g_err_stage:='Validating input parameters';
13039               pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13040         END IF;
13041 
13042         IF p_budget_version_id IS NULL THEN
13043             IF l_debug_mode = 'Y' THEN
13044                  pa_debug.g_err_stage := 'p_budget_version_id is passed as null';
13045                  pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13046             END IF;
13047             pa_utils.add_message(p_app_short_name   => 'PA',
13048                                  p_msg_name         => 'PA_FP_INV_PARAM_PASSED',
13049                                  p_token1           => 'PROCEDURENAME',
13050                                  p_value1           => l_module_name);
13051 
13052             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
13053         END IF;
13054 
13055         IF l_debug_mode = 'Y' THEN
13056              pa_debug.g_err_stage := 'Deriving project_id';
13057              pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13058         END IF;
13059 
13060         BEGIN
13061             SELECT project_id
13062             INTO   l_project_id
13063             FROM   pa_budget_versions
13064             WHERE  budget_version_id = p_budget_version_id;
13065         EXCEPTION
13066             WHEN NO_DATA_FOUND THEN
13067                 IF l_debug_mode = 'Y' THEN
13068                      pa_debug.g_err_stage := 'Invalid budget_version_id passed';
13069                      pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13070                 END IF;
13071                 pa_utils.add_message(p_app_short_name   => 'PA',
13072                                      p_msg_name         => 'PA_FP_INV_PARAM_PASSED',
13073                                      p_token1           => 'PROCEDURENAME',
13074                                      p_value1           => l_module_name);
13075 
13076                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
13077         END;
13078 
13079         IF l_debug_mode = 'Y' THEN
13080              pa_debug.g_err_stage := 'Getting structure version id';
13081              pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13082         END IF;
13083 
13084         x_struct_version_id := pa_planning_element_utils.get_fin_struct_id(l_project_id,p_budget_version_id);
13085         x_struct_status_flag := PA_PROJECT_STRUCTURE_UTILS.Check_Struc_Ver_Published(l_project_id, x_struct_version_id);
13086 
13087         IF l_debug_mode = 'Y' THEN
13088              pa_debug.g_err_stage := 'Values returned->';
13089              pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13090              pa_debug.g_err_stage := 'x_struct_version_id-> ' || x_struct_version_id;
13091              pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13092              pa_debug.g_err_stage := 'x_struct_status_flag-> ' || x_struct_status_flag;
13093              pa_debug.write(l_module_name, pa_debug.g_err_stage, l_debug_level3);
13094         END IF;
13095 
13096         IF l_debug_mode = 'Y' THEN
13097              pa_debug.g_err_stage:='Leaving return_struct_ver_info';
13098              pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
13099              pa_debug.reset_curr_function;
13100         END IF;
13101 
13102   EXCEPTION
13103         WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
13104              l_msg_count := FND_MSG_PUB.count_msg;
13105              IF l_msg_count = 1 THEN
13106                   PA_INTERFACE_UTILS_PUB.get_messages
13107                       (p_encoded        => FND_API.G_TRUE
13108                       ,p_msg_index      => 1
13109                       ,p_msg_count      => l_msg_count
13110                       ,p_msg_data       => l_msg_data
13111                       ,p_data           => l_data
13112                       ,p_msg_index_out  => l_msg_index_out);
13113              END IF;
13114              IF l_debug_mode = 'Y' THEN
13115                 pa_debug.reset_curr_function;
13116              END IF;
13117         WHEN OTHERS THEN
13118            FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PAFPWAUB'
13119                                    ,p_procedure_name  => 'return_struct_ver_info');
13120 
13121            IF l_debug_mode = 'Y' THEN
13122                  pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
13123                  pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
13124            END IF;
13125            IF l_debug_mode = 'Y' THEN
13126                 pa_debug.reset_curr_function;
13127            END IF;
13128            RAISE;
13129   END return_struct_ver_info;
13130 
13131   /* This fuction calls pa_fin_plan_utils.get_time_phased_code to get the time phased
13132    * code for the version and caches it in a package variable for the first time
13133    * and uses it to read and return to avoid select every time for each row
13134    * in the excel download view query
13135    */
13136   FUNCTION get_cached_time_phased_code (bv_id     IN     pa_budget_versions.budget_version_id%TYPE)
13137   RETURN VARCHAR2
13138   IS
13139         l_time_phased_code          pa_proj_fp_options.cost_time_phased_code%TYPE;
13140   BEGIN
13141         IF g_fp_wa_time_phased_code IS NOT NULL THEN
13142             l_time_phased_code := g_fp_wa_time_phased_code;
13143         ELSE
13144             l_time_phased_code := pa_fin_plan_utils.get_time_phased_code(bv_id);
13145             g_fp_wa_time_phased_code := l_time_phased_code;
13146         END IF;
13147 
13148         RETURN l_time_phased_code;
13149   END;
13150 
13151   -- 4494740 changes end here
13152 
13153   /*=============================================================================
13154  This api is used as a wrapper API to pa_budget_pub.create_draft_budget
13155 ==============================================================================*/ --4738996 Starts here
13156 
13157 PROCEDURE create_draft_budget_wrp(
13158   p_api_version_number            IN  NUMBER
13159  ,p_commit                        IN  VARCHAR2        := FND_API.G_FALSE
13160  ,p_init_msg_list                 IN  VARCHAR2        := FND_API.G_FALSE
13161  ,p_msg_count                     OUT NOCOPY NUMBER
13162  ,p_msg_data                      OUT NOCOPY VARCHAR2
13163  ,p_return_status                 OUT NOCOPY VARCHAR2
13164  ,p_pm_product_code               IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13165  ,p_pm_budget_reference           IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13166 , p_budget_version_name           IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13167  ,p_pa_project_id                 IN  NUMBER          := PA_INTERFACE_UTILS_PUB.G_PA_MISS_NUM
13168  ,p_pm_project_reference          IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13169  ,p_budget_type_code              IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13170  ,p_change_reason_code            IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13171  ,p_description                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13172  ,p_entry_method_code             IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13173  ,p_resource_list_name            IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13174  ,p_resource_list_id              IN  NUMBER          := PA_INTERFACE_UTILS_PUB.G_PA_MISS_NUM
13175  ,p_attribute_category            IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13176  ,p_attribute1                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13177  ,p_attribute2                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13178  ,p_attribute3                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13179  ,p_attribute4                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13180  ,p_attribute5                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13181  ,p_attribute6                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13182  ,p_attribute7                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13183  ,p_attribute8                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13184  ,p_attribute9                    IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13185  ,p_attribute10                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13186  ,p_attribute11                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13187  ,p_attribute12                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13188  ,p_attribute13                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13189  ,p_attribute14                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13190  ,p_attribute15                   IN  VARCHAR2        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13191  ,p_budget_lines_in               IN  PA_BUDGET_PUB.budget_line_in_tbl_type
13192  ,p_budget_lines_out              OUT NOCOPY PA_BUDGET_PUB.budget_line_out_tbl_type
13193 
13194  /*Parameters due fin plan model */
13195  ,p_fin_plan_type_id              IN   pa_fin_plan_types_b.fin_plan_type_id%TYPE          := PA_INTERFACE_UTILS_PUB.G_PA_MISS_NUM
13196  ,p_fin_plan_type_name            IN   pa_fin_plan_types_vl.name%TYPE                     := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13197  ,p_version_type                  IN   pa_budget_versions.version_type%TYPE               := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13198  ,p_fin_plan_level_code           IN   pa_proj_fp_options.cost_fin_plan_level_code%TYPE   := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13199  ,p_time_phased_code              IN   pa_proj_fp_options.cost_time_phased_code%TYPE      := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13200  ,p_plan_in_multi_curr_flag       IN   pa_proj_fp_options.plan_in_multi_curr_flag%TYPE    := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13201  ,p_projfunc_cost_rate_type       IN   pa_proj_fp_options.projfunc_cost_rate_type%TYPE    := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13202  ,p_projfunc_cost_rate_date_typ   IN   pa_proj_fp_options.projfunc_cost_rate_date_type%TYPE  := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13203  ,p_projfunc_cost_rate_date       IN   pa_proj_fp_options.projfunc_cost_rate_date%TYPE        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_DATE
13204  ,p_projfunc_rev_rate_type        IN   pa_proj_fp_options.projfunc_rev_rate_type%TYPE        := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13205  ,p_projfunc_rev_rate_date_typ    IN   pa_proj_fp_options.projfunc_rev_rate_date_type%TYPE   := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13206  ,p_projfunc_rev_rate_date        IN   pa_proj_fp_options.projfunc_rev_rate_date%TYPE       := PA_INTERFACE_UTILS_PUB.G_PA_MISS_DATE
13207  ,p_project_cost_rate_type        IN   pa_proj_fp_options.project_cost_rate_type%TYPE      := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13208  ,p_project_cost_rate_date_typ    IN   pa_proj_fp_options.project_cost_rate_date_type%TYPE   := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13209  ,p_project_cost_rate_date        IN   pa_proj_fp_options.project_cost_rate_date%TYPE  := PA_INTERFACE_UTILS_PUB.G_PA_MISS_DATE
13210  ,p_project_rev_rate_type         IN   pa_proj_fp_options.project_rev_rate_type%TYPE   := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13211  ,p_project_rev_rate_date_typ     IN   pa_proj_fp_options.project_rev_rate_date_type%TYPE := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13212  ,p_project_rev_rate_date         IN   pa_proj_fp_options.project_rev_rate_date%TYPE   := PA_INTERFACE_UTILS_PUB.G_PA_MISS_DATE
13213  ,p_raw_cost_flag                 IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13214  ,p_burdened_cost_flag            IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13215  ,p_revenue_flag                  IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13216  ,p_cost_qty_flag                 IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13217  ,p_revenue_qty_flag              IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13218  ,P_all_qty_flag                  IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13219  ,p_create_new_curr_working_flag  IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13220  ,p_replace_current_working_flag  IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13221  ,p_using_resource_lists_flag	  IN   VARCHAR2 := PA_INTERFACE_UTILS_PUB.G_PA_MISS_CHAR
13222  )
13223       IS
13224 
13225     --Start of variables used for debugging
13226 
13227     l_msg_count                      NUMBER := 0;
13228     l_data                           VARCHAR2(2000);
13229     l_msg_data                       VARCHAR2(2000);
13230     l_msg_index_out                  NUMBER;
13231     l_error_msg_code     VARCHAR2(30);
13232     l_return_status      VARCHAR2(2000);
13233     l_debug_mode         VARCHAR2(30);
13234     l_debug_level5                  CONSTANT NUMBER := 5;
13235     l_workflow_started	VARCHAR2(1);
13236     --End of variables used for debugging
13237     l_budget_lines_out_tbl          pa_budget_pub.budget_line_out_tbl_type;
13238     l_fp_preference_code    pa_proj_fp_options.fin_plan_preference_code%TYPE;
13239     l_version_type          pa_budget_versions.version_type%TYPE;
13240     l_baselined_version_id  pa_budget_versions.budget_version_id%TYPE;
13241     l_fp_options_id         pa_proj_fp_options.proj_fp_options_id%TYPE;
13242     l_approved_fin_plan_type_id      pa_fin_plan_types_b.fin_plan_type_id%TYPE;
13243 
13244 BEGIN
13245 
13246     pa_debug.set_err_stack('PA_FIN_PLAN_UTILS.create_draft_budget_wrp');
13247     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
13248     l_debug_mode := NVL(l_debug_mode, 'Y');
13249     IF P_PA_DEBUG_MODE = 'Y' THEN
13250        pa_debug.set_process('create_draft_budget_wrp: ' || 'PLSQL','LOG',l_debug_mode);
13251     END IF;
13252 
13253 
13254 
13255      -- Call the utility function that gives the id of the approved revenue plan type, if exists,
13256                   -- that is added to the project
13257      pa_fin_plan_utils.Get_Appr_Rev_Plan_Type_Info(
13258       p_project_id     => p_pa_project_id
13259      ,x_plan_type_id  =>  l_approved_fin_plan_type_id
13260      ,x_return_status =>  l_return_status
13261      ,x_msg_count     =>  l_msg_count
13262      ,x_msg_data      =>  l_msg_data) ;
13263 
13264      IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
13265 
13266                IF l_debug_mode = 'Y' THEN
13267                 pa_debug.g_err_stage := 'Get_Appr_Cost_Plan_Type_Info API returned error' ;
13268                 pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
13269                END IF;
13270      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
13271 
13272                   -- The Get_Appr_Cost_Plan_Type_Info api got executed successfully.
13273      ELSIF(  l_approved_fin_plan_type_id IS NOT NULL)  THEN
13274 
13275     --Call create_draft_budget
13276 --dbms_output.put_line('control comes here');
13277    --  dbms_output.put_line('Value of yeessssssss l_approved_fin_plan_type_id'||l_approved_fin_plan_type_id);
13278 
13279      pa_budget_pub.create_draft_budget( p_api_version_number   => p_api_version_number
13280                         ,p_commit               => FND_API.G_FALSE
13281                         ,p_init_msg_list        => FND_API.G_FALSE
13282                         ,p_msg_count            => l_msg_count
13283                         ,p_msg_data             => l_msg_data
13284                         ,p_return_status        => l_return_status
13285                         ,p_pm_product_code      => p_pm_product_code
13286                         ,p_budget_version_name  => p_budget_version_name
13287                         ,p_pa_project_id        => p_pa_project_id
13288                         ,p_pm_project_reference => p_pm_project_reference
13289                         ,p_budget_type_code     => NULL
13290                         ,p_change_reason_code   => Null
13291                         ,p_description          => 'Default Created by Projects AMG Agreement Funding'
13292                         ,p_entry_method_code    => NULL
13293                         ,p_resource_list_name   => p_resource_list_name
13294                         ,p_resource_list_id     => p_resource_list_id
13295                         ,p_attribute_category   => p_attribute_category
13296                         ,p_attribute1           => p_attribute1
13297                         ,p_attribute2           => p_attribute2
13298                         ,p_attribute3           => p_attribute3
13299                         ,p_attribute4           => p_attribute4
13300                         ,p_attribute5           => p_attribute5
13301                         ,p_attribute6           => p_attribute6
13302                         ,p_attribute7           => p_attribute7
13303                         ,p_attribute8           => p_attribute8
13304                         ,p_attribute9           => p_attribute9
13305                         ,p_attribute10          => p_attribute10
13306                         ,p_attribute11          => p_attribute11
13307                         ,p_attribute12          => p_attribute12
13308                         ,p_attribute13          => p_attribute13
13309                         ,p_attribute14          => p_attribute14
13310                         ,p_attribute15          => p_attribute15
13311                         ,p_budget_lines_in      => p_budget_lines_in
13312                         ,p_budget_lines_out     => l_budget_lines_out_tbl
13313 			,p_fin_plan_type_id     => l_approved_fin_plan_type_id
13314 			,p_version_type         => PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
13315 			,p_revenue_flag         => 'Y'
13316 			, p_revenue_qty_flag    => 'Y');
13317                        IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
13318                            THEN
13319 			     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13320 			     p_msg_count     := 1;
13321                              p_msg_data      := 'Exiting create_draft_budget_wrp';
13322                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13323 
13324                        ELSIF l_return_status = FND_API.G_RET_STS_ERROR
13325                          THEN
13326 			  p_return_status := FND_API.G_RET_STS_ERROR;
13327 			     p_msg_count     := 1;
13328                              p_msg_data      := 'Exiting create_draft_budget_wrp';
13329                         RAISE FND_API.G_EXC_ERROR;
13330                          END IF;
13331 
13332 
13333         -- dbms_output.put_line('about to call baseline_budget ... ');
13334         -- dbms_output.put_line('Before setting the value of PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB = '|| PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB);
13335         PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB := 'Y';
13336 
13337 	PA_BUDGET_PUB.BASELINE_BUDGET
13338 	( p_api_version_number                  => p_api_version_number
13339  	 ,p_commit                              => FND_API.G_FALSE
13340  	 ,p_init_msg_list                       => FND_API.G_FALSE
13341  	 ,p_msg_count                           => p_msg_count
13342  	 ,p_msg_data                            => p_msg_data
13343  	 ,p_return_status                       => l_return_status
13344  	 ,p_workflow_started                    => l_workflow_started
13345  	 ,p_pm_product_code                     => p_pm_product_code
13346  	 ,p_pa_project_id                       => p_pa_project_id
13347  	 ,p_pm_project_reference                => p_pm_project_reference
13348  	 ,p_budget_type_code                    => NULL
13349  	 ,p_mark_as_original                    => 'Y'
13350 	 ,p_fin_plan_type_id                    => l_approved_fin_plan_type_id
13351 	 ,p_version_type                        => PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
13352 	 );
13353        --  dbms_output.put_line('returned from BASELINE_BUDGET ... status = '||l_return_status);
13354 
13355 	IF (nvl(PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB,'N') = 'Y') THEN
13356 		PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB := 'N'; -- reset the value bug 3099706
13357 	END IF;
13358 
13359         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
13360         THEN
13361                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13362 
13363         ELSIF l_return_status = FND_API.G_RET_STS_ERROR
13364         THEN
13365                         RAISE FND_API.G_EXC_ERROR;
13366         END IF;
13367 
13368      ELSIF(  l_approved_fin_plan_type_id IS  NULL)  THEN
13369   --   dbms_output.put_line('Value of l_approved_fin_plan_type_id'||l_approved_fin_plan_type_id);
13370  pa_budget_pub.create_draft_budget( p_api_version_number   => p_api_version_number
13371                         ,p_commit               => FND_API.G_FALSE
13372                         ,p_init_msg_list        => FND_API.G_FALSE
13373                         ,p_msg_count            => l_msg_count
13374                         ,p_msg_data             => l_msg_data
13375                         ,p_return_status        => l_return_status
13376                         ,p_pm_product_code      => p_pm_product_code
13377                         ,p_budget_version_name  => p_budget_version_name
13378                         ,p_pa_project_id        => p_pa_project_id
13379                         ,p_pm_project_reference => p_pm_project_reference
13380                         ,p_budget_type_code     => 'AR'
13381                         ,p_change_reason_code   => Null
13382                         ,p_description          => 'Default Created by Projects AMG Agreement Funding'
13383                         ,p_entry_method_code    => p_entry_method_code
13384                         ,p_resource_list_name   => p_resource_list_name
13385                         ,p_resource_list_id     => p_resource_list_id
13386                         ,p_attribute_category   => p_attribute_category
13387                         ,p_attribute1           => p_attribute1
13388                         ,p_attribute2           => p_attribute2
13389                         ,p_attribute3           => p_attribute3
13390                         ,p_attribute4           => p_attribute4
13391                         ,p_attribute5           => p_attribute5
13392                         ,p_attribute6           => p_attribute6
13393                         ,p_attribute7           => p_attribute7
13394                         ,p_attribute8           => p_attribute8
13395                         ,p_attribute9           => p_attribute9
13396                         ,p_attribute10          => p_attribute10
13397                         ,p_attribute11          => p_attribute11
13398                         ,p_attribute12          => p_attribute12
13399                         ,p_attribute13          => p_attribute13
13400                         ,p_attribute14          => p_attribute14
13401                         ,p_attribute15          => p_attribute15
13402                         ,p_budget_lines_in      => p_budget_lines_in
13403                         ,p_budget_lines_out     => l_budget_lines_out_tbl);
13404 
13405 			  IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
13406                            THEN
13407 			     p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13408 			     p_msg_count     := 1;
13409                              p_msg_data      := 'Exiting create_draft_budget_wrp';
13410                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13411 
13412                        ELSIF l_return_status = FND_API.G_RET_STS_ERROR
13413                          THEN
13414 			  p_return_status := FND_API.G_RET_STS_ERROR;
13415 			     p_msg_count     := 1;
13416                              p_msg_data      := 'Exiting create_draft_budget_wrp';
13417                         RAISE FND_API.G_EXC_ERROR;
13418                          END IF;
13419 
13420       --   dbms_output.put_line('about to call baseline_budget ... ');
13421       --   dbms_output.put_line('Before setting the value of PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB = '|| PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB);
13422         PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB := 'Y';
13423 
13424 	PA_BUDGET_PUB.BASELINE_BUDGET
13425 	( p_api_version_number                  => p_api_version_number
13426  	 ,p_commit                              => FND_API.G_FALSE
13427  	 ,p_init_msg_list                       => FND_API.G_FALSE
13428  	 ,p_msg_count                           => p_msg_count
13429  	 ,p_msg_data                            => p_msg_data
13430  	 ,p_return_status                       => l_return_status
13431  	 ,p_workflow_started                    => l_workflow_started
13432  	 ,p_pm_product_code                     => p_pm_product_code
13433  	 ,p_pa_project_id                       => p_pa_project_id
13434  	 ,p_pm_project_reference                => p_pm_project_reference
13435  	 ,p_budget_type_code                    => 'AR'
13436  	 ,p_mark_as_original                    => 'Y'
13437 	);
13438       --   dbms_output.put_line('returned from BASELINE_BUDGET ... status = '||l_return_status);
13439 
13440 	IF (nvl(PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB,'N') = 'Y') THEN
13441 		PA_FP_CONSTANTS_PKG.G_CALLED_FROM_AGREEMENT_PUB := 'N'; -- reset the value bug 3099706
13442 	END IF;
13443 
13444         IF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR
13445         THEN
13446                         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13447 
13448         ELSIF l_return_status = FND_API.G_RET_STS_ERROR
13449         THEN
13450                         RAISE FND_API.G_EXC_ERROR;
13451         END IF;
13452 END IF;
13453 
13454     pa_debug.g_err_stage:='Exiting create_draft_budget_wrp';
13455     IF P_PA_DEBUG_MODE = 'Y' THEN
13456        pa_debug.write('create_draft_budget_wrp: ' || l_module_name,pa_debug.g_err_stage,3);
13457     END IF;
13458     pa_debug.reset_err_stack;
13459 
13460 EXCEPTION
13461 
13462      WHEN Invalid_Arg_Exc THEN
13463 
13464           l_msg_count := FND_MSG_PUB.count_msg;
13465 
13466           IF l_msg_count = 1 THEN
13467 
13468                PA_INTERFACE_UTILS_PUB.get_messages
13469                      (p_encoded        => FND_API.G_TRUE
13470                       ,p_msg_index      => 1
13471                       ,p_msg_count      => l_msg_count
13472                       ,p_msg_data       => l_msg_data
13473                       ,p_data           => l_data
13474                       ,p_msg_index_out  => l_msg_index_out);
13475 
13476                p_msg_data := l_data;
13477 
13478                p_msg_count := l_msg_count;
13479           ELSE
13480 
13481               p_msg_count := l_msg_count;
13482 
13483           END IF;
13484 
13485            p_return_status := FND_API.G_RET_STS_ERROR;
13486 
13487            pa_debug.g_err_stage:='Invalid Arguments Passed';
13488            IF P_PA_DEBUG_MODE = 'Y' THEN
13489               pa_debug.write('create_draft_budget_wrp: ' || l_module_name,pa_debug.g_err_stage,5);
13490            END IF;
13491 
13492            pa_debug.reset_err_stack;
13493 
13494            RAISE;
13495 
13496      WHEN Others THEN
13497 
13498           p_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
13499           p_msg_count     := 1;
13500           p_msg_data      := SQLERRM;
13501 
13502           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_UTILS'
13503                                   ,p_procedure_name  => 'create_draft_budget_wrp');
13504 
13505           pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
13506           IF P_PA_DEBUG_MODE = 'Y' THEN
13507              pa_debug.write('create_draft_budget_wrp: ' || l_module_name,pa_debug.g_err_stage,5);
13508           END IF;
13509 
13510           pa_debug.reset_err_stack;
13511 
13512           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
13513 
13514 
13515 END create_draft_budget_wrp;
13516 --4738996 Ends here
13517 
13518 /*
13519   API Name          : Get_NP_RA_Description
13520   API Description   : Returns the description for the Non Periodic Resource Assignment
13521   API Created By    : kchaitan
13522   API Creation Date : 07-MAY-2007
13523 */
13524 
13525 FUNCTION Get_NP_RA_Description
13526                (p_resource_assignment_id IN pa_resource_assignments.resource_assignment_id%TYPE Default Null,
13527                 p_txn_currency_code      IN pa_budget_lines.txn_currency_code%TYPE Default Null
13528      ) RETURN VARCHAR2
13529 IS
13530 l_description pa_budget_lines.description%TYPE;
13531 begin
13532 
13533     select description
13534     into   l_description
13535     from  pa_budget_lines
13536     where resource_assignment_id = p_resource_assignment_id
13537     and   txn_currency_code      = p_txn_currency_code;
13538 
13539     return l_description;
13540 
13541 exception
13542     when no_data_found then
13543         return null;
13544 
13545 end Get_NP_RA_Description;
13546 
13547 /*
13548   API Name          : Get_Change_Reason
13549   API Description   : Returns the Change Reason Meaning for the Non Periodic and Periodic Resource Assignment
13550   API Created By    : kchaitan
13551   API Creation Date : 07-MAY-2007
13552 */
13553 
13554 FUNCTION Get_Change_Reason
13555                (p_resource_assignment_id IN pa_resource_assignments.resource_assignment_id%TYPE Default Null,
13556                 p_txn_currency_code      IN pa_budget_lines.txn_currency_code%TYPE Default Null,
13557                 p_time_phased_code       IN varchar2
13558      ) RETURN VARCHAR2
13559 IS
13560 l_chg_rsn_code pa_budget_lines.change_reason_code%TYPE;
13561 l_chg_rsn      varchar2(80) := null;
13562 begin
13563 
13564     If p_time_phased_code <> 'N' Then
13565         If G_Chg_Reason is null Then
13566             select meaning
13567             into G_Chg_Reason
13568             from pa_lookups
13569             where lookup_type = 'BUDGET CHANGE REASON'
13570             and lookup_code   = 'MULTIPLE';
13571         End if;
13572         return G_Chg_Reason;
13573     End if;
13574 
13575     select change_reason_code
13576     into   l_chg_rsn_code
13577     from  pa_budget_lines
13578     where resource_assignment_id = p_resource_assignment_id
13579     and   txn_currency_code      = p_txn_currency_code;
13580 
13581     IF l_chg_rsn_code is not null Then
13582         select meaning
13583         into l_chg_rsn
13584         from pa_lookups
13585         where lookup_type = 'BUDGET CHANGE REASON'
13586         and lookup_code   = l_chg_rsn_code;
13587     End If;
13588 
13589     return l_chg_rsn;
13590     --return l_chg_rsn_code;
13591 
13592 exception
13593     when no_data_found then
13594         return null;
13595 
13596 end Get_Change_Reason;
13597 
13598 END PA_FIN_PLAN_UTILS;