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