DBA Data[Home] [Help]

PACKAGE BODY: APPS.PA_FIN_PLAN_PUB

Source


1 PACKAGE BODY pa_fin_plan_pub as
2 /* $Header: PAFPPUBB.pls 120.18.12010000.3 2008/12/03 12:40:30 jcgeorge ship $
3    Start of Comments
4    Package name     : PA_FIN_PLAN_PUB
5    Purpose          : utility API's for Org Forecast pages
6    History          :
7    NOTE             :
8    End of Comments
9 */
10 
11 /* BUG NO:- 2331201 For FINPLAN these pacakge level variables have been included */
12 
13    l_module_name VARCHAR2(100) := 'pa.plsql.pa_fin_plan_pub';
14    -- Bug Fix: 4569365. Removed MRC code.
15    -- g_mrc_exception  EXCEPTION; /* FPB2 */
16 
17 /* ------------------------------------------------------------------------- */
18 
19 
20 P_PA_DEBUG_MODE varchar2(1) := NVL(FND_PROFILE.value('PA_DEBUG_MODE'), 'N');
21 
22 --Bug 3964755. Introduced the parameter p_calling_context. Valid values are NULL and 'COPY_PROJECT'
23 procedure Submit_Current_Working
24     (p_calling_context                  IN     VARCHAR2                                         DEFAULT NULL,
25      p_project_id                       IN     pa_budget_versions.project_id%TYPE,
26      p_budget_version_id                IN     pa_budget_versions.budget_version_id%TYPE,
27      p_record_version_number            IN     pa_budget_versions.record_version_number%TYPE,
28      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
29      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
30      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
31 is
32 l_debug_mode      VARCHAR2(30);
33 l_valid_flag              VARCHAR2(1);
34 l_current_working_flag    pa_budget_versions.current_working_flag%TYPE;
35 l_budget_status_code      pa_budget_versions.budget_status_code%TYPE;
36 l_plan_processing_code    pa_budget_versions.plan_processing_code%TYPE;
37 l_locked_by_person_id   pa_budget_versions.locked_by_person_id%TYPE;
38 /* Bug# 2661650 - _vl to _b/_tl for performance changes */
39 l_fin_plan_type_code      pa_fin_plan_types_b.fin_plan_type_code%TYPE;
40 
41 l_msg_count       NUMBER := 0;
42 l_data            VARCHAR2(2000);
43 l_msg_data        VARCHAR2(2000);
44 l_error_msg_code  VARCHAR2(30);
45 l_msg_index_out   NUMBER;
46 l_return_status   VARCHAR2(2000);
47 
48 begin
49     FND_MSG_PUB.initialize;
50     IF P_PA_DEBUG_MODE = 'Y' THEN
51        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Submit_Current_Working');
52     END IF;
53     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
54     l_debug_mode := NVL(l_debug_mode, 'Y');
55     IF P_PA_DEBUG_MODE = 'Y' THEN
56        pa_debug.set_process('Submit_Current_Working: ' || 'PLSQL','LOG',l_debug_mode);
57     END IF;
58     x_msg_count := 0;
59 /* CHECK FOR BUSINESS RULES VIOLATIONS */
60     /* check for null budget_version_id */
61     if p_budget_version_id is NULL then
62         x_return_status := FND_API.G_RET_STS_ERROR;
63         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
64                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
65     end if;
66     /* check to see if the budget version we're updating is a WORKING version; */
67     /* only CURRENT WORKING versions can be submitted */
68     select
69         current_working_flag,
70         budget_status_code,
71         plan_processing_code,
72   locked_by_person_id
73     into
74         l_current_working_flag,
75         l_budget_status_code,
76         l_plan_processing_code,
77   l_locked_by_person_id
78     from
79         pa_budget_versions
80     where
81         budget_version_id = p_budget_version_id;
82       select pt.fin_plan_type_code
83         into l_fin_plan_type_code
84         from pa_proj_fp_options po,
85              pa_fin_plan_types_b pt /* Bug# 2661650 - _vl to _b/_tl for performance changes */
86         where po.project_id = p_project_id and
87               po.fin_plan_version_id = p_budget_version_id and
88               po.fin_plan_option_level_code = 'PLAN_VERSION' and
89               po.fin_plan_type_id = pt.fin_plan_type_id;
90     /* allow user to Submit a version that's already Submitted */
91     if not ((l_current_working_flag = 'Y') and (l_budget_status_code in ('W', 'S'))) then
92         IF P_PA_DEBUG_MODE = 'Y' THEN
93            pa_debug.write_file('Submit_Current_Working: ' || 'selected budget version is not a current working version');
94         END IF;
95         x_return_status := FND_API.G_RET_STS_ERROR;
96         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
97                              p_msg_name            => 'PA_FP_SUBMIT_CURRENT_WORKING');
98     end if;
99     /* check to see if the budget version is currently under regeneration.  If so, we */
100     /* cannot submit it for baselining */
101     /*** BUG FIX 2779674: check for regeneration in progress OR period profile refresh
102      *** in progress
103      */
104     -- if l_plan_processing_code <> PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G then
105     if nvl(l_plan_processing_code,'X') = PA_FP_CONSTANTS_PKG.G_PLAN_PROC_CODE_P then
106         x_return_status := FND_API.G_RET_STS_ERROR;
107         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
108                              p_msg_name            => 'PA_FP_SUBMIT_GENERATED');
109     end if;
110     if nvl(l_plan_processing_code,'X') = PA_FP_CONSTANTS_PKG.G_PLAN_PROC_CODE_PPP then
111         x_return_status := FND_API.G_RET_STS_ERROR;
112         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
113                              p_msg_name            => 'PA_FP_SUBMIT_PP_REFRESHING');
114     end if;
115 
116     /* check to see if the budget version we're updating to be current working has */
117     /* been updated by someone else already */
118     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
119             (p_unique_index             => p_budget_version_id,
120              p_record_version_number    => p_record_version_number,
121              x_valid_flag               => l_valid_flag,
122              x_return_status            => l_return_status,
123              x_error_msg_code           => l_error_msg_code);
124     if x_return_status = FND_API.G_RET_STS_ERROR then
125         IF P_PA_DEBUG_MODE = 'Y' THEN
126            pa_debug.write_file('Submit_Current_Working: ' || 'record version number error ');
127         END IF;
128         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
129                              p_msg_name            => l_error_msg_code);
130     end if;
131 
132     /* Check to see if the plan version is locked.  An unlocked plan version cannot be submitted */
133     --Bug 3964755. This check is not required in copy project flow. In this flow, the version would be created,
134     --submitted and baselined by the API that copies the project. This check would result in an error since
135     --locked_by_person_id is not copied in copy_budget_version API. Hence skipping this check
136     -- Bug 4276265: do not check for lock if plan version is Org Forecasting version
137     if (l_locked_by_person_id is null)  AND
138        NVL(p_calling_context, '-99') <> 'COPY_PROJECT' AND
139        NVL(l_fin_plan_type_code,'-99') <> 'ORG_FORECAST' then --Bug 5456482
140         x_return_status := FND_API.G_RET_STS_ERROR;
141         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
142                              p_msg_name            => 'PA_FP_SUBMIT_UNLOCKED_VER');
143     end if; --locked_by_person_id is null
144 
145 /* If There are ANY Busines Rules Violations , Then Do NOT Proceed: RETURN */
146     l_msg_count := FND_MSG_PUB.count_msg;
147     if l_msg_count > 0 then
148         if l_msg_count = 1 then
149              PA_INTERFACE_UTILS_PUB.get_messages
150                  (p_encoded        => FND_API.G_TRUE,
151                   p_msg_index      => 1,
152                   p_msg_count      => l_msg_count,
153                   p_msg_data       => l_msg_data,
154                   p_data           => l_data,
155                   p_msg_index_out  => l_msg_index_out);
156              x_msg_data := l_data;
157              x_msg_count := l_msg_count;
158             else
159              x_msg_count := l_msg_count;
160         end if;
161             pa_debug.reset_err_stack;
162             return;
163     end if;
164 
165 /* If There are NO Business Rules Violations , Then proceed with Submit Current Working */
166     IF P_PA_DEBUG_MODE = 'Y' THEN
167        pa_debug.write_file('Submit_Current_Working: ' || 'no business rules violations');
168     END IF;
169     if l_msg_count = 0 then
170       SAVEPOINT PA_FIN_PLAN_PUB_SUBMIT_WORKING;
171 
172       /* FINPLANNING PATCHSET K: If the plan type is not ORG_FORECAST, then call
173          pa_fin_plan_pvt.Submit_Current_Working_FinPlan
174        */
175       if l_fin_plan_type_code = 'ORG_FORECAST' then
176         /* set the BUDGET_STATUS_CODE from 'W' to 'S' */
177         update
178             pa_budget_versions
179         set
180             last_update_date=SYSDATE,
181             last_updated_by=FND_GLOBAL.user_id,
182             last_update_login=FND_GLOBAL.login_id,
183             budget_status_code = 'S',
184             record_version_number=record_version_number+1    /* increment record_version_number */
185         where
186             budget_version_id=p_budget_version_id;
187       -- CALL PA_FIN_PLAN_PVT.Submit_Current_Working_FinPlan for non ORG_FORECAST
188       else
189         PA_FIN_PLAN_PVT.Submit_Current_Working_FinPlan
190             (p_project_id             => p_project_id,
191              p_budget_version_id      => p_budget_version_id,
192              p_record_version_number  => p_record_version_number,
193              x_return_status          => l_return_status,
194              x_msg_count              => l_msg_count,
195              x_msg_data               => l_msg_data);
196         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
197           -- PA_FIN_PLAN_PVT.Submit_Current_Working_FinPlan RETURNED ERRORS
198           rollback to PA_FIN_PLAN_PUB_SUBMIT_WORKING;
199           x_return_status := FND_API.G_RET_STS_ERROR;
200           if l_msg_count = 1 then
201              PA_INTERFACE_UTILS_PUB.get_messages
202                  (p_encoded        => FND_API.G_TRUE,
203                   p_msg_index      => 1,
204                   p_msg_count      => l_msg_count,
205                   p_msg_data       => l_msg_data,
206                   p_data           => l_data,
207                   p_msg_index_out  => l_msg_index_out);
208              x_msg_data := l_data;
209              x_msg_count := l_msg_count;
210           else
211              x_msg_count := l_msg_count;
212           end if;
213           pa_debug.reset_err_stack;
214           return;
215         end if;
216       end if; -- l_fin_plan_type_code
217 
218     end if;
219     x_return_status := FND_API.G_RET_STS_SUCCESS;
220     pa_debug.reset_err_stack;
221 
222 exception
223     when pa_fin_plan_pub.rollback_on_error then
224       IF P_PA_DEBUG_MODE = 'Y' THEN
225          pa_debug.write_file('Submit_Current_Working: ' || 'Procedure Submit_working: rollback_on_error exception');
226       END IF;
227       rollback to PA_FIN_PLAN_PUB_SUBMIT_WORKING;
228       raise FND_API.G_EXC_UNEXPECTED_ERROR;
229 
230     when others then
231       rollback to PA_FIN_PLAN_PUB_SUBMIT_WORKING;
232       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
233       x_msg_count     := 1;
234       x_msg_data      := SQLERRM;
235       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
236                                p_procedure_name   => 'Submit_Current_Working');
237       pa_debug.reset_err_stack;
238       raise FND_API.G_EXC_UNEXPECTED_ERROR;
239 end Submit_Current_Working;
240 /* ------------------------------------------------------------------------- */
241 
242 -- ** BUG FIX 2615778: orig budget version id, record version numbers can be null **
243 --    BUG FIX 2733848: check resource_list_id to see it matches with baselined versions
244 -- FP M -- Resource list is changeable even if baselined version exists. Versions can
245 -- be set as CW, even if the resource list of the version that is being made CW doesnt
246 -- match the resource list of the baselined version.
247 procedure Set_Current_Working
248     (p_project_id                   IN     pa_budget_versions.project_id%TYPE,
249      p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE,
250      p_record_version_number        IN     pa_budget_versions.record_version_number%TYPE,
251      p_orig_budget_version_id       IN     pa_budget_versions.budget_version_id%TYPE,
252      p_orig_record_version_number   IN     pa_budget_versions.record_version_number%TYPE,
253      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
254      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
255      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
256 is
257 l_debug_mode      VARCHAR2(30);
258 l_valid1_flag     VARCHAR2(1);
259 l_valid2_flag     VARCHAR2(1);
260 l_budget_status_code    pa_budget_versions.budget_status_code%TYPE;
261 l_cur_work_bv_id        pa_budget_versions.budget_version_id%TYPE;
262 l_fin_plan_type_id      pa_budget_versions.fin_plan_type_id%TYPE;
263 l_version_type          pa_budget_versions.version_type%TYPE;
264 l_msg_count       NUMBER := 0;
265 l_data            VARCHAR2(2000);
266 l_msg_data        VARCHAR2(2000);
267 l_return_status   VARCHAR2(2000);
268 l_error_msg_code  VARCHAR2(30);
269 l_msg_index_out   NUMBER;
270 l_attributes_same_flag VARCHAR2(1);
271 l_exists          VARCHAR2(1);
272 
273 -- for BUG FIX 2733848
274 l_resource_list_id     pa_budget_versions.resource_list_id%TYPE;
275 
276 begin
277     FND_MSG_PUB.initialize;
278     IF P_PA_DEBUG_MODE = 'Y' THEN
279        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Set_Current_Working');
280     END IF;
281     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
282     l_debug_mode := NVL(l_debug_mode, 'Y');
283     IF P_PA_DEBUG_MODE = 'Y' THEN
284        pa_debug.set_process('Set_Current_Working: ' || 'PLSQL','LOG',l_debug_mode);
285     END IF;
286     x_msg_count := 0;
287 /* CHECK FOR BUSINESS RULES VIOLATIONS */
288     /* check for null budget_version_id */
289     if p_budget_version_id is NULL then
290         IF P_PA_DEBUG_MODE = 'Y' THEN
291            pa_debug.write_file('Set_Current_Working: ' || 'no budget version id entered');
292         END IF;
293         x_return_status := FND_API.G_RET_STS_ERROR;
294         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
295                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
296     end if;
297     /* check to see if the budget version we're setting to be current working has */
298     /* been updated by someone else already */
299     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
300             (p_unique_index             => p_budget_version_id,
301              p_record_version_number    => p_record_version_number,
302              x_valid_flag               => l_valid1_flag,
303              x_return_status            => l_return_status,
304              x_error_msg_code           => l_error_msg_code);
305     IF P_PA_DEBUG_MODE = 'Y' THEN
306        pa_debug.write_file('Set_Current_Working: ' || 'record version check #1: return status is ' || l_valid1_flag);
307     END IF;
308     /* check to see if the old current working budget version has been updated */
309     /* by someone else already */
310     /* BUT, need to check if there was an old current working version */
311     if p_orig_budget_version_id is not null then
312       PA_FIN_PLAN_UTILS.Check_Record_Version_Number
313               (p_unique_index             => p_orig_budget_version_id,
314                p_record_version_number    => p_orig_record_version_number,
315                x_valid_flag               => l_valid2_flag,
316                x_return_status            => x_return_status,
317                x_error_msg_code           => l_error_msg_code);
318       IF P_PA_DEBUG_MODE = 'Y' THEN
319          pa_debug.write_file('Set_Current_Working: ' || 'record version check #2: return status is ' || l_valid2_flag);
320       END IF;
321       if (not ((l_valid1_flag = 'Y')and (l_valid2_flag='Y'))) then
322           PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
323                                p_msg_name            => l_error_msg_code);
324       end if;
325       /* check to see if the old current working budget version has been submitted */
326       select
327          budget_status_code
328         ,fin_plan_type_id
329         ,version_type
330       into
331           l_budget_status_code
332          ,l_fin_plan_type_id
333          ,l_version_type
334       from
335           pa_budget_versions
336       where
337           budget_version_id=p_orig_budget_version_id;
338       if (l_budget_status_code='S') and (p_budget_version_id <> p_orig_budget_version_id) then
339           x_return_status := FND_API.G_RET_STS_ERROR;
340           PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
341                                p_msg_name            => 'PA_FP_VERSION_SUBMITTED_ERR');
342       end if;
343     end if; -- p_orig_budget_version_id is not null
344 
345 
346 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
347     l_msg_count := FND_MSG_PUB.count_msg;
348     if l_msg_count > 0 then
349         if l_msg_count = 1 then
350              PA_INTERFACE_UTILS_PUB.get_messages
351                  (p_encoded        => FND_API.G_TRUE,
352                   p_msg_index      => 1,
353                   p_msg_count      => l_msg_count,
354                   p_msg_data       => l_msg_data,
355                   p_data           => l_data,
356                   p_msg_index_out  => l_msg_index_out);
357              x_msg_data := l_data;
358              x_msg_count := l_msg_count;
359             else
360              x_msg_count := l_msg_count;
361         end if;
362             pa_debug.reset_err_stack;
363             return;
364     end if;
365 
366     /* Fix for bug 2651851:
367        When the user tries to set a new approved budget plan version to be the current
368        working version, a check must be made to see the new plan version has the same
369        time phase, planning level, and resource list as the existing current working
370        plan version:
371 
372        If it does, the new version will successfully be set to be the current working
373        version.
374 
375        If it does not, and submitted or approved but unimplemented financial impact
376        change orders do not exist, the new version will successfully be set to be the
377        current working version.
378 
379        If it does not, and submitted or approved but unimplemented financial impact
380        change orders do exist, an error message will be displayed */
381 
382        BEGIN
383          Select budget_version_id
384            into l_cur_work_bv_id
385          from pa_budget_versions bv
386         where bv.project_id       = p_project_id
387           and bv.fin_plan_type_id = l_fin_plan_type_id
388           and bv.version_type     = l_version_type
389           and bv.current_working_flag = 'Y'
390           and bv.ci_id            IS NULL
391           and ((DECODE(bv.version_type,'COST',bv.approved_cost_plan_type_flag,
392                                     'REVENUE',bv.approved_rev_plan_type_flag,
393                                     'N') = 'Y')
394               OR
395              (bv.approved_cost_plan_type_flag = 'Y' and
396               bv.approved_rev_plan_type_flag  = 'Y')) ;
397        EXCEPTION
398              WHEN NO_DATA_FOUND THEN
399                   l_cur_work_bv_id := -9999;
400        END;
401 
402        IF p_orig_budget_version_id = l_cur_work_bv_id THEN
403           /* The original current working budget is of approved plan type so additional checks */
404              Pa_Fp_Control_Items_Utils.Compare_Source_Target_Ver_Attr
405                 ( p_source_bv_id         => p_orig_budget_version_id
406                  ,p_target_bv_id         => p_budget_version_id
407                  ,x_attributes_same_flag => l_attributes_same_flag
408                  ,x_return_status        => l_return_status
409                  ,x_msg_count            => l_msg_count
410                  ,x_msg_data             => l_msg_data);
411 
412              IF l_attributes_same_flag = 'N' THEN
413                 BEGIN
414                 /*
415                 select 'Y'
416                   into l_exists
417                   from dual
418                  where exists (select 'x'
419                                  from pa_budget_versions bv
420                                      ,pa_control_items   ci
421                                      ,pa_ci_impacts      cp
422                                      --For bug 3550073
423                                      ,pa_ci_statuses_v   pcs
424                                      ,pa_pt_co_impl_statuses pcis
425                                 where bv.project_id                    = p_project_id
426                                   and bv.fin_plan_type_id              = l_fin_plan_type_id
427                                   and bv.version_type                  = l_version_type
428                                   and ci.ci_id                         = bv.ci_id
429                                   and pcs.ci_type_id                   = ci.ci_type_id
430                                   and pcs.project_status_code          = ci.status_code
431                                   and pcs.project_system_status_code   IN ('CI_APPROVED','CI_SUBMITTED')
432                                   and cp.ci_id                         = ci.ci_id
433                                   and cp.impact_type_code              <> 'FINPLAN'
434                                   and cp.impact_type_code              =  DECODE(bv.version_type,
435                                                                                 'COST','FINPLAN_COST',
436                                                                                 'REVENUE','FINPLAN_REVENUE',
437                                                                                 cp.impact_type_code)
438                                   and cp.status_code                   = 'CI_IMPACT_PENDING'
439                                   and pcis.fin_plan_type_id            = bv.fin_plan_type_id
440                                   and pcis.ci_type_id                  = ci.ci_type_id
441                                   and pcis.version_type                = bv.version_type
442                                   and pcis.status_code                 = pcs.project_status_code);
443                 */
444                 -- Bug 3828512 changed the validation to look for eligible change
445                 -- documents against the target current working version. If any
446                 -- that are not already included in baseline version, throw an
447                 -- error as such change documents can not be included in the target
448                 -- current working version. We ignore change documents that are part
449                 -- of baseline versions for the reason that we just mark them as copied
450                 -- and not actually merged
451 
452                 select 'Y'
453                   into l_exists
454                   from dual
455                  where exists
456                      (select 'x'
457                        from pa_fp_eligible_ci_v eligible
458                       where eligible.project_id = p_project_id
459                         and eligible.fin_plan_type_id = l_fin_plan_type_id
460                         and eligible.ci_version_type = l_version_type
461                         and eligible.project_system_status_code IN ('CI_APPROVED','CI_SUBMITTED')
462                         -- filter cis that are already part of target cur working version
463                         and eligible.ci_id not in (select merged.ci_id
464                                                      from pa_fp_merged_ctrl_items merged
465                                                     where merged.plan_version_id = p_budget_version_id
466                                                       and merged.project_id = p_project_id
467                                                   )
468                         -- filter cis included in current baseline version
469                         and eligible.ci_id not in (select merged.ci_id
470                                                      from pa_fp_merged_ctrl_items merged,
471                                                           pa_budget_versions cur_baseline
472                                                     where cur_baseline.project_id = p_project_id
473                                                       and cur_baseline.fin_plan_type_id = l_fin_plan_type_id
474                                                       and cur_baseline.version_type = l_version_type
475                                                       and cur_baseline.budget_status_code = 'B'
476                                                       and cur_baseline.current_flag = 'Y'
477                                                       and merged.plan_version_id = cur_baseline.budget_version_id
478                                                       and merged.project_id = cur_baseline.project_id));
479                 EXCEPTION
480                           WHEN NO_DATA_FOUND THEN
481                                l_exists := 'N';
482                 END;
483              END IF; -- l_attributes_same_flag = 'N'
484 
485              IF l_exists = 'Y' THEN
486                 pa_utils.add_message
487                         ( p_app_short_name => 'PA',
488                           p_msg_name       => 'PA_FP_SET_CURNT_WORK_NOT_ALLWD');
489                 fnd_msg_pub.count_and_get (p_count => x_msg_count,
490                                            p_data  => x_msg_data);
491                 x_return_status := FND_API.G_RET_STS_ERROR;
492                 return;
493              END IF; -- l_exists = 'Y'
494        END IF; -- Approved budget plan version
495 
496    /* Ssarma: Enhancement for Control items when the plan type is approved budget.
497 
498       Business Rule: Only approved ci's can be included in approved budget versions.
499 
500       1. The new current working version gets all the ci_links from the latest baselined
501          budget version.
502       2. The ci's linked to old current working and not present in the
503          new current working are made un-implemented. These are the ones
504          that have been implemented after the latest baselined version.
505       3. The ci's linked with the new current working and not implemented are
506          set to implemented.
507    */
508 
509    pa_fin_plan_pvt.handle_ci_links(
510       p_source_bv_id  => p_orig_budget_version_id  -- Old Current Working Version
511      ,p_target_bv_id  => p_budget_version_id       -- New Current Working Version
512      ,x_return_status => l_return_status
513      ,x_msg_count     => l_msg_count
514      ,x_msg_data      => l_msg_data);
515 
516       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
517          l_msg_count := FND_MSG_PUB.count_msg;
518          if l_msg_count > 0 then
519              if l_msg_count = 1 then
520                   PA_INTERFACE_UTILS_PUB.get_messages
521                       (p_encoded        => FND_API.G_TRUE,
522                        p_msg_index      => 1,
523                        p_msg_count      => l_msg_count,
524                        p_msg_data       => l_msg_data,
525                        p_data           => l_data,
526                        p_msg_index_out  => l_msg_index_out);
527                   x_msg_data := l_data;
528                   x_msg_count := l_msg_count;
529                  else
530                   x_msg_count := l_msg_count;
531              end if;
532                  pa_debug.reset_err_stack;
533                  return;
534          end if;
535       END IF;
536 
537 /* If There are NO Business Rules Violations , Then proceed with Set Current Working */
538     IF P_PA_DEBUG_MODE = 'Y' THEN
539        pa_debug.write_file('Set_Current_Working: ' || 'no business rules violations: beginning Set Current Working');
540     END IF;
541     if l_msg_count = 0 then
542         SAVEPOINT PA_FIN_PLAN_PUB_SET_WORKING;
543             /* remove the CURRENT_WORKING status from the old current working version */
544             /* ONLY if there is an old current working version */
545             if p_orig_budget_version_id is not null then
546                 update
547                     pa_budget_versions
548                 set
549                     last_update_date=SYSDATE,
550                     last_updated_by=FND_GLOBAL.user_id,
551                     last_update_login=FND_GLOBAL.login_id,
552                     current_working_flag='N',
553                     record_version_number=record_version_number+1    /* increment record_version_number */
554                 where
555                     budget_version_id=p_orig_budget_version_id;
556             end if;
557             /* crown the CURRENT_WORKING status to the new current working version */
558             update
559                 pa_budget_versions
560             set
561                 last_update_date=SYSDATE,
562                 last_updated_by=FND_GLOBAL.user_id,
563                 last_update_login=FND_GLOBAL.login_id,
564                 current_working_flag='Y',
565                 record_version_number=record_version_number+1    /* increment record_version_number */
566             where
567                 budget_version_id=p_budget_version_id;
568     end if;
569     x_return_status := FND_API.G_RET_STS_SUCCESS;
570     pa_debug.reset_err_stack;
571 exception
572     when pa_fin_plan_pub.rollback_on_error then
573       IF P_PA_DEBUG_MODE = 'Y' THEN
574          pa_debug.write_file('Procedure Set_Current_Working: rollback_on_error exception');
575       END IF;
576       rollback to PA_FIN_PLAN_PUB_SET_WORKING;
577       raise FND_API.G_EXC_UNEXPECTED_ERROR;
578 
579     when others then
580         rollback to PA_FIN_PLAN_PUB_SET_WORKING;
581         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
582         x_msg_count     := 1;
583         x_msg_data      := SQLERRM;
584         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
585                                p_procedure_name   => 'Set_Current_Working');
586         pa_debug.reset_err_stack;
587         raise FND_API.G_EXC_UNEXPECTED_ERROR;
588 end Set_Current_Working;
589 /* ------------------------------------------------------------------------- */
590 
591 procedure Rework_Submitted
592     (p_project_id                   IN     pa_budget_versions.project_id%TYPE,
593      p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE,
594      p_record_version_number        IN     pa_budget_versions.record_version_number%TYPE,
595      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
596      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
597      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
598 is
599 l_debug_mode      VARCHAR2(30);
600 l_valid_flag              VARCHAR2(1);
601 l_current_working_flag    pa_budget_versions.current_working_flag%TYPE;
602 l_budget_status_code      pa_budget_versions.budget_status_code%TYPE;
603 
604 l_msg_count       NUMBER := 0;
605 l_data            VARCHAR2(2000);
606 l_msg_data        VARCHAR2(2000);
607 l_error_msg_code  VARCHAR2(30);
608 l_msg_index_out   NUMBER;
609 l_return_status   VARCHAR2(2000);
610 
611 begin
612     FND_MSG_PUB.initialize;
613     IF P_PA_DEBUG_MODE = 'Y' THEN
614        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Rework_Submitted');
615     END IF;
616     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
617     l_debug_mode := NVL(l_debug_mode, 'Y');
618     IF P_PA_DEBUG_MODE = 'Y' THEN
619        pa_debug.set_process('Rework_Submitted: ' || 'PLSQL','LOG',l_debug_mode);
620     END IF;
621     x_msg_count := 0;
622 
623 /* CHECK FOR BUSINESS RULES VIOLATIONS */
624     /* check for null budget_version_id */
625     if p_budget_version_id is NULL then
626         x_return_status := FND_API.G_RET_STS_ERROR;
627         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
628                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
629     end if;
630     /* check to see if the budget version we're updating is a SUBMITTED version; */
631     /* only SUBMITTED versions can be reworked */
632     select
633         budget_status_code,
634         current_working_flag
635     into
636         l_budget_status_code,
637         l_current_working_flag
638     from
639         pa_budget_versions
640     where
641         budget_version_id = p_budget_version_id;
642     if ((l_budget_status_code <> 'S') or (l_current_working_flag <> 'Y')) then
643         IF P_PA_DEBUG_MODE = 'Y' THEN
644            pa_debug.write_file('Rework_Submitted: ' || 'version is not a submitted current working version');
645         END IF;
646         x_return_status := FND_API.G_RET_STS_ERROR;
647         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
648                              p_msg_name            => 'PA_FP_REWORK_SUBMITTED');
649     end if;
650     /* check to see if the budget version we're updating to be current working has */
651     /* been updated by someone else already */
652     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
653             (p_unique_index             => p_budget_version_id,
654              p_record_version_number    => p_record_version_number,
655              x_valid_flag               => l_valid_flag,
656              x_return_status            => l_return_status,
657              x_error_msg_code           => l_error_msg_code);
658     if x_return_status = FND_API.G_RET_STS_ERROR then
659         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
660                              p_msg_name            => l_error_msg_code);
661     end if;
662 
663 /* If There are ANY Busines Rules Violations , Then Do NOT Proceed: RETURN */
664     l_msg_count := FND_MSG_PUB.count_msg;
665     if l_msg_count > 0 then
666         if l_msg_count = 1 then
667              PA_INTERFACE_UTILS_PUB.get_messages
668                  (p_encoded        => FND_API.G_TRUE,
669                   p_msg_index      => 1,
670                   p_msg_count      => l_msg_count,
671                   p_msg_data       => l_msg_data,
672                   p_data           => l_data,
673                   p_msg_index_out  => l_msg_index_out);
674              x_msg_data := l_data;
675              x_msg_count := l_msg_count;
676             else
677              x_msg_count := l_msg_count;
678         end if;
679             pa_debug.reset_err_stack;
680             return;
681     end if;
682 
683 /* If There are NO Business Rules Violations , Then proceed with Rework Submitted */
684     if l_msg_count = 0 then
685         IF P_PA_DEBUG_MODE = 'Y' THEN
686            pa_debug.write_file('Rework_Submitted: ' || 'no business logic errors; proceeding with Rework Submitted');
687         END IF;
688         SAVEPOINT PA_FIN_PLAN_PUB_REWORK;
689         /* set the BUDGET_STATUS_CODE from 'W' to 'S' */
690         update
691             pa_budget_versions
692         set
693             last_update_date=SYSDATE,
694             last_updated_by=FND_GLOBAL.user_id,
695             last_update_login=FND_GLOBAL.login_id,
696             budget_status_code = 'W',
697             record_version_number=record_version_number+1    /* increment record_version_number */
698         where
699             budget_version_id=p_budget_version_id;
700     end if;
701     x_return_status := FND_API.G_RET_STS_SUCCESS;
702     pa_debug.reset_err_stack;
703 
704 exception
705     when pa_fin_plan_pub.rollback_on_error then
706       IF P_PA_DEBUG_MODE = 'Y' THEN
707          pa_debug.write_file('Procedure Rework_Submitted: rollback_on_error exception');
708       END IF;
709       rollback to PA_FIN_PLAN_PUB_REWORK;
710       raise FND_API.G_EXC_UNEXPECTED_ERROR;
711 
712     when others then
713       rollback to PA_FIN_PLAN_PUB_REWORK;
714       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
715       x_msg_count     := 1;
716       x_msg_data      := SQLERRM;
717       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
718                                p_procedure_name   => 'Rework_Submitted');
719       pa_debug.reset_err_stack;
720       raise FND_API.G_EXC_UNEXPECTED_ERROR;
721 end Rework_Submitted;
722 /* ------------------------------------------------------------------------- */
723 
724 procedure Mark_As_Original
725     (p_project_id                   IN     pa_budget_versions.project_id%TYPE,
726      p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE,
727      p_record_version_number        IN     pa_budget_versions.record_version_number%TYPE,
728      p_orig_budget_version_id       IN     pa_budget_versions.budget_version_id%TYPE,
729      p_orig_record_version_number   IN     pa_budget_versions.record_version_number%TYPE,
730      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
731      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
732      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
733 is
734 l_debug_mode      VARCHAR2(30);
735 l_valid1_flag     VARCHAR2(1);
736 l_valid2_flag     VARCHAR2(1);
737 l_msg_count       NUMBER := 0;
738 l_data            VARCHAR2(2000);
739 l_msg_data        VARCHAR2(2000);
740 l_return_status   VARCHAR2(2000);
741 l_error_msg_code  VARCHAR2(30);
742 l_msg_index_out   NUMBER;
743 
744 begin
745     FND_MSG_PUB.initialize;
746     IF P_PA_DEBUG_MODE = 'Y' THEN
747        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Mark_As_Original');
748     END IF;
749     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
750     l_debug_mode := NVL(l_debug_mode, 'Y');
751     IF P_PA_DEBUG_MODE = 'Y' THEN
752        pa_debug.set_process('Mark_As_Original: ' || 'PLSQL','LOG',l_debug_mode);
753     END IF;
754     x_msg_count := 0;
755 /* CHECK FOR BUSINESS RULES VIOLATIONS */
756     /* check for null budget_version_id */
757     if p_budget_version_id is NULL then
758         x_return_status := FND_API.G_RET_STS_ERROR;
759         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
760                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
761     end if;
762     /* check to see if the budget version we're setting to be original baselined has */
763     /* been updated by someone else already */
764     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
765             (p_unique_index             => p_budget_version_id,
766              p_record_version_number    => p_record_version_number,
767              x_valid_flag               => l_valid1_flag,
768              x_return_status            => l_return_status,
769              x_error_msg_code           => l_error_msg_code);
770     /* check to see if the old original baselined budget version has been updated */
771    /* by someone else already */
772 
773    /* Bug # 2639285 - Included the check for p_orig_budget_version_id is not null*/
774    IF p_orig_budget_version_id IS NOT NULL THEN
775    PA_FIN_PLAN_UTILS.Check_Record_Version_Number
776             (p_unique_index             => p_orig_budget_version_id,
777              p_record_version_number    => p_orig_record_version_number,
778              x_valid_flag               => l_valid2_flag,
779              x_return_status            => x_return_status,
780              x_error_msg_code           => l_error_msg_code);
781     if not((l_valid1_flag='Y') and (l_valid2_flag='Y')) then
782         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
783                              p_msg_name            => l_error_msg_code);
784     end if;
785     END IF;
786 
787 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
788     l_msg_count := FND_MSG_PUB.count_msg;
789     if l_msg_count > 0 then
790         if l_msg_count = 1 then
791              PA_INTERFACE_UTILS_PUB.get_messages
792                  (p_encoded        => FND_API.G_TRUE,
793                   p_msg_index      => 1,
794                   p_msg_count      => l_msg_count,
795                   p_msg_data       => l_msg_data,
796                   p_data           => l_data,
797                   p_msg_index_out  => l_msg_index_out);
798              x_msg_data := l_data;
799              x_msg_count := l_msg_count;
800             else
801              x_msg_count := l_msg_count;
802         end if;
803             pa_debug.reset_err_stack;
804             return;
805     end if;
806 
807 /* If There are NO Business Rules Violations , Then proceed with Mark As Original */
808     if l_msg_count = 0 then
809         IF P_PA_DEBUG_MODE = 'Y' THEN
810            pa_debug.write_file('Mark_As_Original: ' || 'no business violations; continuing with Mark As Original');
811         END IF;
812         SAVEPOINT PA_FIN_PLAN_PUB_MARK_ORIGINAL;
813         /* remove the ORIGINAL status from the old original version */
814 
815     /* Bug # 2639285 - Included the update in case p_orig_budget_version_id is null */
816     IF p_orig_budget_version_id is null THEN
817 
818         update pa_budget_versions a
819         set    original_flag = 'Y',
820         current_original_flag = 'N',
821         last_update_date = SYSDATE,
822         last_updated_by=FND_GLOBAL.user_id,
823         last_update_login=FND_GLOBAL.login_id,
824         record_version_number = record_version_number + 1
825         where  (a.project_id,a.fin_plan_type_id,a.version_type) =
826                 (select b.project_id,b.fin_plan_type_id,b.version_type
827                  from   pa_budget_versions b
828                  where  b.budget_version_id = p_budget_version_id)
829         and    a.budget_version_id <> p_budget_version_id
830         and    current_original_flag = 'Y';
831     ELSE
832         update
833             pa_budget_versions
834         set
835             last_update_date=SYSDATE,
836             last_updated_by=FND_GLOBAL.user_id,
837             last_update_login=FND_GLOBAL.login_id,
838             original_flag = 'Y',
839             current_original_flag='N',
840             record_version_number = record_version_number + 1 /* increment record_version_number */
841         where
842             budget_version_id=p_orig_budget_version_id;
843      END IF;
844         /* crown the ORIGINAL status to the new original version */
845         update
846             pa_budget_versions
847         set
848             last_update_date=SYSDATE,
849             last_updated_by=FND_GLOBAL.user_id,
850             last_update_login=FND_GLOBAL.login_id,
851             original_flag = 'Y',
852             current_original_flag='Y',
853             record_version_number = record_version_number + 1 /* increment record_version_number */
854         where
855             budget_version_id=p_budget_version_id;
856     end if;
857 
858     /* FP M - Reporting lines integration */
859 
860     DECLARE
861          l_budget_version_ids SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type(p_budget_version_id);
862     BEGIN
863          pa_debug.write('Mark_As_Original','Calling PJI_FM_XBS_ACCUM_MAINT.PLAN_ORIGINAL ' ,5);
864          pa_debug.write('Mark_As_Original','p_baseline_version_id  '|| p_budget_version_id,5);
865          PJI_FM_XBS_ACCUM_MAINT.PLAN_ORIGINAL (
866               p_original_version_id => p_budget_version_id,
867               x_return_status       => l_return_status,
868               x_msg_code            => l_error_msg_code);
869 
870          IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
871               PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
872                                    p_msg_name            => l_error_msg_code);
873 
874               RAISE pa_fin_plan_pub.rollback_on_error;
875          END IF;
876 
877     END;
878 
879 
880 exception
881     when pa_fin_plan_pub.rollback_on_error then
882       IF P_PA_DEBUG_MODE = 'Y' THEN
883          pa_debug.write_file('Procedure Mark_As_Original: rollback_on_error exception');
884       END IF;
885       rollback to PA_FIN_PLAN_PUB_MARK_ORIGINAL;
886       raise FND_API.G_EXC_UNEXPECTED_ERROR;
887 
888     when others then
889         rollback to PA_FIN_PLAN_PUB_MARK_ORIGINAL;
890         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
891         x_msg_count     := 1;
892         x_msg_data      := SQLERRM;
893         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
894                                  p_procedure_name   => 'Mark_As_Original');
895         pa_debug.reset_err_stack;
896         raise FND_API.G_EXC_UNEXPECTED_ERROR;
897 end Mark_As_Original;
898 /* ------------------------------------------------------------------------- */
899 
900 
901 ----------------------------------------------------------------------------------------
902 -- Delete_Version API -  Adding p_context IN parameter as part of FPM changes
903 -- The permissible values for p_context are ('BUDGET' and 'WORKPLAN'). This
904 -- parameter is added since this api shall be called also from wrokplan perspective
905 -- FPM onwards. This parameter is defaulted as 'BUGDET' is the spec. So existing calls
906 -- need not be modified
907 ----------------------------------------------------------------------------------------
908 procedure Delete_Version
909     (p_project_id                   IN     pa_budget_versions.project_id%TYPE,
910      p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE,
911      p_record_version_number        IN     pa_budget_versions.record_version_number%TYPE,
912      p_context                      IN     VARCHAR2,
913      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
914      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
915      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
916 as
917 l_debug_mode      VARCHAR2(30);
918 l_msg_count       NUMBER := 0;
919 l_data            VARCHAR2(2000);
920 l_msg_data        VARCHAR2(2000);
921 l_error_msg_code  VARCHAR2(30);
922 l_msg_index_out   NUMBER;
923 l_return_status   VARCHAR2(2000);
924 
925 l_valid_flag            VARCHAR2(1);
926 l_budget_status_code    pa_budget_versions.budget_status_code%TYPE;
927 l_version_type          pa_budget_versions.version_type%TYPE;
928 l_current_working_flag  VARCHAR2(1);
929 l_fin_plan_type_id      pa_budget_versions.fin_plan_type_id%TYPE;
930 l_max_version           NUMBER;
931 l_cur_work_bv_id        pa_budget_versions.budget_version_id%TYPE;
932 l_exists                varchar2(1);
933 l_module_name           VARCHAR2(100):='PAFPPUBB.delete_version';
934 l_wp_fin_plan_type_id   pa_fin_plan_types_b.fin_plan_type_id%TYPE;
935 l_budget_version_id_tbl SYSTEM.PA_NUM_TBL_TYPE;
936 
937 /* Bug 2688610 - Add the following two locals*/
938 l_baseline_funding_flag            pa_projects_all.baseline_funding_flag%TYPE;
939 l_approved_rev_plan_type_flag      pa_budget_versions.approved_rev_plan_type_flag%TYPE;
940 
941 -- Bug 3354518 FP M Doosan Phase 1 changes
942 l_current_original_flag         pa_budget_versions.current_original_flag%TYPE;
943 l_current_flag          pa_budget_versions.original_flag%TYPE;
944 
945 cursor l_resource_assignments_csr is
946 select unique
947     resource_assignment_id
948 from
949     pa_resource_assignments
950 where
951     budget_version_id=p_budget_version_id;
952 l_resource_assignments_rec l_resource_assignments_csr%ROWTYPE;
953 
954 begin
955     FND_MSG_PUB.initialize;
956     IF P_PA_DEBUG_MODE = 'Y' THEN
957        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Delete_Version');
958     END IF;
959     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
960     l_debug_mode := NVL(l_debug_mode, 'Y');
961     IF P_PA_DEBUG_MODE = 'Y' THEN
962        pa_debug.set_process('Delete_Version: ' || 'PLSQL','LOG',l_debug_mode);
963     END IF;
964     x_msg_count := 0;
965 
966 --------------------------------------------------------------------
967 -- Checking for ellgible values of p_context ('WORKPLAN' or 'BUDGET')
968 --------------------------------------------------------------------
969     IF NOT ((p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET) OR
970             (p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN)) THEN
971               IF l_debug_mode = 'Y' THEN
972                       pa_debug.g_err_stage:='p_context value is invalid  -  p_context :' || p_context;
973                       pa_debug.write('PA_FIN_PLAN_PUB.Delete_Version: ' || l_module_name,pa_debug.g_err_stage,5);
974               END IF;
975               PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
976                                    p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
977               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
978     END IF;
979 
980 /* CHECK FOR BUSINESS RULES VIOLATIONS */
981     /* check for null budget_version_id */
982     if p_budget_version_id is NULL AND p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET then
983         x_return_status := FND_API.G_RET_STS_ERROR;
984         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
985                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
986     end if;
987 
988     IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN AND
989        p_budget_version_id IS NULL AND
990        p_project_id IS NULL THEN
991 
992         IF l_debug_mode = 'Y' THEN
993            pa_debug.g_err_stage:= 'p_context '||p_context;
994            pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
995 
996            pa_debug.g_err_stage:= 'p_budget_version_id '||p_budget_version_id;
997            pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
998 
999            pa_debug.g_err_stage:= 'p_project_id '||p_project_id;
1000            pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
1001 
1002         END IF;
1003 
1004         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1005                            p_msg_name         => 'PA_FP_INV_PARAM_PASSED',
1006                            p_token1           => 'PROCEDURENAME',
1007                            p_value1           => l_module_name);
1008         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1009     END IF;
1010 
1011 
1012 
1013 ----------------------------------------------------------------------
1014 -- CHECK FOR BUSINESS RULES VIOLATIONS
1015 -- only for BUDGET Context   -- BUSINESS RULES CHECK FOR BUDGET STARTS
1016 ----------------------------------------------------------------------
1017     IF ((p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET) AND (p_budget_version_id IS NOT NULL)) THEN
1018     /* check to see if the budget version we're updating to be current working has */
1019     /* been updated by someone else already */
1020     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
1021             (p_unique_index             => p_budget_version_id,
1022              p_record_version_number    => p_record_version_number,
1023              x_valid_flag               => l_valid_flag,
1024              x_return_status            => l_return_status,
1025              x_error_msg_code           => l_error_msg_code);
1026     if x_return_status = FND_API.G_RET_STS_ERROR then
1027         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
1028                              p_msg_name            => l_error_msg_code);
1029     end if;
1030     /* check to make sure that budget_status_code = 'W' */
1031     /* we can delete only working versions (cannot delete submitted, baselined) */
1032     select
1033         budget_status_code,
1034         current_working_flag,
1035         fin_plan_type_id,
1036         version_type,
1037         current_original_flag,     -- Bug 3354518 FP M
1038         current_flag       -- Bug 3354518 FP M
1039     into
1040         l_budget_status_code,
1041         l_current_working_flag,
1042         l_fin_plan_type_id,
1043         l_version_type,
1044         l_current_original_flag,    -- Bug 3354518 FP M
1045         l_current_flag      -- Bug 3354518 FP M
1046     from
1047         pa_budget_versions
1048     where
1049         budget_version_id = p_budget_version_id;
1050 
1051     if  l_budget_status_code <> 'W' then
1052         if l_budget_status_code = 'S' then
1053             IF P_PA_DEBUG_MODE = 'Y' THEN
1054                pa_debug.write_file('Delete_Version: ' || 'budget status code is S');
1055             END IF;
1056             x_return_status := FND_API.G_RET_STS_ERROR;
1057             PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
1058                                  p_msg_name            => 'PA_FP_DELETE_WORKING');
1059         elsif (l_budget_status_code = 'B' and           -- Bug 3354518 FP M
1060                (l_current_original_flag = 'Y' OR l_current_flag = 'Y')) then
1061                -- baseline versions marked as current or original can not be deleted
1062                x_return_status := FND_API.G_RET_STS_ERROR;
1063                PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
1064                                     p_msg_name            => 'PA_FP_DEL_CUR_OR_ORIG_BASELINE');
1065 
1066         end if;                                        -- Bug 3354518 FP M
1067     end if;
1068 
1069     /* Begin Fix for bug 2688610 : The control item validation needs to be by passed in
1070        case of autobaselined project and approved revenue plan version.
1071     */
1072     BEGIN
1073         select p.baseline_funding_flag, v.approved_rev_plan_type_flag
1074         into l_baseline_funding_flag, l_approved_rev_plan_type_flag
1075         from pa_projects_all p, pa_budget_versions v
1076         where p.project_id = v.project_id
1077         and v.budget_version_id = p_budget_version_id;
1078     EXCEPTION
1079         WHEN NO_DATA_FOUND THEN
1080         pa_debug.write(l_module_name,'Error getting version details',3);
1081         RAISE;
1082     END;
1083 
1084     IF NOT (nvl(l_baseline_funding_flag,'N') = 'Y' AND
1085                 nvl(l_approved_rev_plan_type_flag,'N') = 'Y') THEN
1086 
1087 
1088     /* Begin Fix for bug 2651851 : Once the financial impact of a change order exists,
1089                              the current working approved budget plan version
1090                              cannot be deleted. */
1091 
1092     BEGIN
1093     Select budget_version_id
1094            into l_cur_work_bv_id
1095          from pa_budget_versions bv
1096         where bv.project_id       = p_project_id
1097           and bv.fin_plan_type_id = l_fin_plan_type_id
1098           and bv.version_type     = l_version_type
1099           and bv.current_working_flag = 'Y'
1100           and bv.ci_id            IS NULL
1101           and ((DECODE(bv.version_type,'COST',bv.approved_cost_plan_type_flag,
1102                                     'REVENUE',bv.approved_rev_plan_type_flag,
1103                                     'N') = 'Y')
1104               OR
1105              (bv.approved_cost_plan_type_flag = 'Y' and
1106               bv.approved_rev_plan_type_flag  = 'Y')) ;
1107     EXCEPTION
1108          WHEN NO_DATA_FOUND THEN
1109               l_cur_work_bv_id := -9999;
1110     END;
1111 
1112     IF p_budget_version_id = l_cur_work_bv_id THEN
1113        /* The version to be deleted is also the current working approved budget plan
1114           version. Check to see if any financial impact of a change order exists for
1115           this project plan type combination and if so then return an error and do
1116           not delete the current working version (p_budget_version_id) */
1117 
1118           BEGIN
1119 
1120                 SELECT 'Y'
1121                   INTO l_exists
1122                   FROM dual
1123                  WHERE EXISTS ( SELECT 'X' from pa_budget_versions pb,pa_control_items pci -- added pa_control_items pci for bug 3741051
1124                                  WHERE pb.project_id = p_project_id --added the alias name for bug 3741051
1125                                    AND pb.fin_plan_type_id = l_fin_plan_type_id -- added the alias name for bug 3741051
1126                                    AND pb.version_type = l_version_type -- added the alias name for bug 3741051
1127            AND pb.project_id = pci.project_id -- added for bug 3741051
1128            AND pb.ci_id = pci.ci_id -- added for bug 3741051
1129            AND pci.status_code <> 'CI_CANCELED'-- added for bug 3741051
1130             );
1131           EXCEPTION
1132                WHEN NO_DATA_FOUND THEN
1133                     l_exists := 'N';
1134           END;
1135 
1136         IF l_exists = 'Y' THEN
1137            pa_utils.add_message
1138                         ( p_app_short_name => 'PA',
1139                           p_msg_name       => 'PA_FP_BV_CI_NO_DELETE');
1140 
1141                    fnd_msg_pub.count_and_get (p_count => x_msg_count,
1142                                               p_data  => x_msg_data);
1143                    x_return_status := FND_API.G_RET_STS_ERROR;
1144                    return;
1145         END IF; -- l_exists = 'Y'
1146     END IF; -- p_budget_version_id = l_cur_work_bv_id
1147 
1148     /* End   Fix for bug 2651851 : Once the financial impact of a change order exists,
1149                              the current working approved budget plan version
1150                              cannot be deleted. */
1151     END IF;
1152     /* End Fix for bug 2688610 : The control item validation needs to be by passed in
1153        case of autobaselined project and approved revenue plan version.
1154     */
1155 
1156 END IF;
1157 --------------------------------------------------------------------------------
1158 -- End of check for p_context = 'BUDGET' -- BUSINESS RULES CHECK FOR BUDGET ENDS
1159 --------------------------------------------------------------------------------
1160 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
1161     l_msg_count := FND_MSG_PUB.count_msg;
1162     if l_msg_count > 0 then
1163         if l_msg_count = 1 then
1164              PA_INTERFACE_UTILS_PUB.get_messages
1165                  (p_encoded        => FND_API.G_TRUE,
1166                   p_msg_index      => 1,
1167                   p_msg_count      => l_msg_count,
1168                   p_msg_data       => l_msg_data,
1169                   p_data           => l_data,
1170                   p_msg_index_out  => l_msg_index_out);
1171              x_msg_data := l_data;
1172              x_msg_count := l_msg_count;
1173             else
1174              x_msg_count := l_msg_count;
1175         end if;
1176             pa_debug.reset_err_stack;
1177             return;
1178     end if;
1179 
1180 
1181 /* If There are NO Business Rules Violations , Then proceed with Delete Version */
1182     if l_msg_count = 0 then
1183         IF P_PA_DEBUG_MODE = 'Y' THEN
1184            pa_debug.write_file('Delete_Version: ' || 'no business errors: continuing with Delete Version');
1185         END IF;
1186         SAVEPOINT PA_FIN_PLAN_PUB_DELETE;
1187         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN THEN
1188 
1189             BEGIN
1190                 SELECT fin_plan_type_id
1191                 INTO   l_wp_fin_plan_type_id
1192                 FROM   pa_fin_plan_types_b
1193                 WHERE  use_for_workplan_flag='Y';
1194             EXCEPTION
1195             WHEN NO_DATA_FOUND THEN
1196                 IF l_debug_mode = 'Y' THEN
1197                    pa_debug.g_err_stage:= 'Workplan plan type does not exist';
1198                    pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
1199                 END IF;
1200                 RETURN;
1201             END;
1202 
1203         ELSE
1204 
1205             l_wp_fin_plan_type_id:=NULL;
1206 
1207         END IF;
1208         /* call Delete_Version_Helper to delete everything but the entry in PA_BUDGET_VERSIONS and PA_PROJ_FP_OPTIONS */
1209         pa_fin_plan_pub.Delete_Version_Helper
1210             (p_project_id           => p_project_id,
1211              p_context              => p_context,
1212              p_budget_version_id    => p_budget_version_id,
1213              x_return_status        => l_return_status,
1214              x_msg_count            => l_msg_count,
1215              x_msg_data             => l_msg_data);
1216         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
1217             raise pa_fin_plan_pub.rollback_on_error;
1218         end if;
1219 
1220         /* PA_PROJ_FIN_PLAN_OPTIONS: delete row (keyed on fin_plan_version_id) */
1221 
1222         -- Made changes for sql id
1223         IF p_budget_version_id IS NOT NULL THEN
1224 
1225             delete
1226             from
1227                 pa_proj_fp_options
1228             where
1229                 fin_plan_version_id= p_budget_version_id AND
1230                 project_id=nvl(p_project_id,project_id) AND
1231                 fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id);
1232 
1233         ELSIF p_project_id IS NOT NULL THEN
1234             delete
1235             from
1236                 pa_proj_fp_options
1237             where
1238             fin_plan_version_id=nvl(p_budget_version_id,fin_plan_version_id) AND
1239             project_id=p_project_id AND
1240             (fin_plan_type_id IS NULL OR
1241             fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id));
1242 
1243         END IF;
1244 
1245 
1246 
1247 
1248         /* PA_BUDGET_VERSIONS delete row */
1249         /* Bug 4873352 - Split this delete based on i/p parameter null condition
1250          * to avoid FTS - Sql id : 14903057 */
1251         if p_budget_version_id is not null then
1252              delete
1253              from
1254                  pa_budget_versions
1255              where
1256                  budget_version_id=p_budget_version_id AND
1257                  project_id=nvl(p_project_id,project_id) AND
1258                  fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id)
1259              returning budget_version_id
1260              bulk collect into l_budget_version_id_tbl    ;
1261         elsif p_project_id is not null then
1262              delete
1263              from
1264                  pa_budget_versions
1265              where
1266                  budget_version_id=nvl(p_budget_version_id,budget_version_id) AND
1267                  project_id=p_project_id AND
1268                  fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id)
1269              returning budget_version_id
1270              bulk collect into l_budget_version_id_tbl    ;
1271         else
1272         /* For budget context, bv id cannot be null;
1273          * For wp context, combination of project id and bv id cannot be null;
1274          * Given this, this else part would never get executed. Including for
1275          * future cases, in case this logic changes or addtl parameters are included.
1276          */
1277              delete
1278              from
1279                  pa_budget_versions
1280              where
1281                  budget_version_id=nvl(p_budget_version_id,budget_version_id) AND
1282                  project_id=nvl(p_project_id,project_id) AND
1283                  fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id)
1284              returning budget_version_id
1285              bulk collect into l_budget_version_id_tbl    ;
1286         end if;
1287 
1288 ----------------------------------------------------------------------
1289 -- CHECK FOR CURRENT WORKING VERSIONS
1290 -- only for BUDGET Context   -- CHECK FOR CURRENT WORKING VERSIONS STARTS
1291 ----------------------------------------------------------------------
1292     IF p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET THEN
1293 
1294         /* if the deleted version was the current working version, need to find a replacement */
1295         if l_current_working_flag='Y' then
1296             /* find next most recent version */
1297             select
1298                 nvl(max(version_number), 0)
1299             into
1300                 l_max_version
1301             from
1302                 pa_budget_versions
1303             where
1304                 project_id = p_project_id and
1305                 fin_plan_type_id = l_fin_plan_type_id and
1306                 budget_status_code = 'W' and
1307                 version_type = l_version_type and
1308 /* BUG FIX 2638356: do not accidentally select Control Items */
1309                 ci_id is null;
1310 
1311             /* make it the Current Working version */
1312             if (l_max_version <> 0) then
1313                 update
1314                     pa_budget_versions
1315                 set
1316                     current_working_flag = 'Y',
1317                     last_update_date=SYSDATE,
1318                     last_updated_by=FND_GLOBAL.user_id,
1319                     last_update_login=FND_GLOBAL.login_id,
1320                     record_version_number=record_version_number+1
1321                 where
1322                     project_id = p_project_id and
1323                     fin_plan_type_id = l_fin_plan_type_id and
1324                     budget_status_code = 'W' and
1325                     version_type       = l_version_type and
1326                     version_number = l_max_version;
1327             end if;
1328         end if;
1329 
1330     END IF;
1331 ----------------------------------------------------------------------
1332 -- CHECK FOR CURRENT WORKING VERSIONS
1333 -- only for BUDGET Context   -- CHECK FOR CURRENT WORKING VERSIONS ENDS
1334 ----------------------------------------------------------------------
1335         -- Delete attachements which are associated with the budget version
1336     fnd_attached_documents2_pkg.delete_attachments
1337                (X_entity_name             => 'PA_BUDGET_VERSIONS',
1338                 X_pk1_value               => to_char(p_budget_version_id),
1339                 X_delete_document_flag    => 'Y');
1340 
1341     /* FP M - Reporting lines integration */
1342 
1343     BEGIN
1344          IF P_PA_DEBUG_MODE = 'Y' THEN
1345              pa_debug.write(l_module_name,'Calling PJI_FM_XBS_ACCUM_MAINT.PLAN_DELETE ' ,5);
1346              pa_debug.write(l_module_name,'p_fp_version_ids  count '|| l_budget_version_id_tbl.count,5);
1347          END IF;
1348          /* Very sure that there is only one record in the plsql table. Just having the loop  */
1349          FOR I in 1..l_budget_version_id_tbl.count LOOP
1350               pa_debug.write(l_module_name,'p_fp_version_ids   ('|| i || ')' || l_budget_version_id_tbl(i),5);
1351          END LOOP;
1352          IF l_budget_version_id_tbl.COUNT>0 THEN
1353              PJI_FM_XBS_ACCUM_MAINT.PLAN_DELETE (
1354                   p_fp_version_ids   => l_budget_version_id_tbl,
1355                   x_return_status    => l_return_status,
1356                   x_msg_code         => l_error_msg_code);
1357 
1358              IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
1359                   PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
1360                                        p_msg_name            => l_error_msg_code);
1361 
1362                   RAISE pa_fin_plan_pub.rollback_on_error;
1363              END IF;
1364         END IF;
1365 
1366     END;
1367     x_return_status := FND_API.G_RET_STS_SUCCESS;
1368     pa_debug.reset_err_stack;
1369 end if;
1370 
1371 exception
1372 
1373      WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
1374            l_msg_count := FND_MSG_PUB.count_msg;
1375              IF l_msg_count = 1 THEN
1376                 PA_INTERFACE_UTILS_PUB.get_messages
1377                      (p_encoded        => FND_API.G_TRUE
1378                       ,p_msg_index      => 1
1379                       ,p_msg_count      => l_msg_count
1380                       ,p_msg_data       => l_msg_data
1381                       ,p_data           => l_data
1382                       ,p_msg_index_out  => l_msg_index_out);
1383                 x_msg_data := l_data;
1384                 x_msg_count := l_msg_count;
1385              ELSE
1386                 x_msg_count := l_msg_count;
1387              END IF;
1388            x_return_status := FND_API.G_RET_STS_ERROR;
1389            pa_debug.reset_curr_function;
1390 
1391     when pa_fin_plan_pub.rollback_on_error then
1392       IF P_PA_DEBUG_MODE = 'Y' THEN
1393          pa_debug.write_file('Procedure Delete_Version: rollback_on_error exception');
1394       END IF;
1395       rollback to PA_FIN_PLAN_PUB_DELETE;
1396       raise FND_API.G_EXC_UNEXPECTED_ERROR;
1397 
1398     when others then
1399         rollback to PA_FIN_PLAN_PUB_DELETE;
1400         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1401         x_msg_count     := 1;
1402         x_msg_data      := SQLERRM;
1403         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
1404                                  p_procedure_name   => 'Delete_Version');
1405         pa_debug.reset_err_stack;
1406         raise FND_API.G_EXC_UNEXPECTED_ERROR;
1407 end Delete_Version;
1408 /* ------------------------------------------------------------------------- */
1409 
1410 --p_context can be PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET or PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN.
1411 --p_budget_version_id is mandatory whenever p_context is PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET.
1412 --When PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN, data for the version will be deleted when p_budget_version_id
1413 --is passed. Otherwise when p_project_id is passed data for the entire project will be deleted.
1414 procedure Delete_Version_Helper
1415     (p_project_id                   IN     pa_projects_all.project_id%TYPE ,
1416      p_context                      IN     VARCHAR2 ,
1417      p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE,
1418      x_return_status                OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
1419      x_msg_count                    OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
1420      x_msg_data                     OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1421 is
1422 l_debug_mode      VARCHAR2(30);
1423 l_msg_count       NUMBER := 0;
1424 l_data            VARCHAR2(2000);
1425 l_msg_data        VARCHAR2(2000);
1426 l_msg_index_out   NUMBER;
1427 l_return_status   VARCHAR2(2000);
1428 
1429 l_valid_flag            VARCHAR2(1);
1430 l_budget_status_code    pa_budget_versions.budget_status_code%TYPE;
1431 l_max_version           NUMBER;
1432 
1433 l_project_id             pa_budget_versions.project_id%TYPE;
1434 l_ci_id                  pa_budget_versions.ci_id%TYPE;
1435 l_budget_version_id_tbl  pa_plsql_datatypes.idTabTyp;
1436 l_proj_fp_options_id_tbl pa_plsql_datatypes.idTabTyp;
1437 l_module_name            VARCHAR2(100):='PAFPPUBB.Delete_Version_Helper';
1438 i                        NUMBER;
1439 l_wp_fin_plan_type_id    pa_fin_plan_types_b.fin_plan_type_id%TYPE;
1440 
1441   -- IPM Arch Enhancement - Bug 4865563
1442    l_fp_cols_rec                   PA_FP_GEN_AMOUNT_UTILS.FP_COLS;  --This variable will be used to call pa_resource_asgn_curr maintenance api
1443    l_debug_level5                  NUMBER:=5;
1444 
1445 -- start of changes for bug 2779637
1446 /*
1447 cursor l_resource_assignments_csr is
1448 select unique
1449     resource_assignment_id
1450 from
1451     pa_resource_assignments
1452 where
1453     budget_version_id=p_budget_version_id;
1454 l_resource_assignments_rec l_resource_assignments_csr%ROWTYPE;
1455 */
1456 
1457 
1458 -- end of changes bug 2779637
1459 begin
1460     /*=================================================================
1461     BUG NO:- 2331201 for fin plan these two lines  have been modified
1462     =================================================================*/
1463     --FND_MSG_PUB.initialize;
1464     --pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Delete_Version_Helper');
1465     x_return_status := FND_API.G_RET_STS_SUCCESS;
1466     pa_debug.set_err_stack('PA_FIN_PLAN_PUB.Delete_Version_Helper');
1467 
1468     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1469     l_debug_mode := NVL(l_debug_mode, 'Y');
1470     IF P_PA_DEBUG_MODE = 'Y' THEN
1471        pa_debug.set_process('Delete_Version: ' || 'PLSQL','LOG',l_debug_mode);
1472     END IF;
1473     x_msg_count := 0;
1474 /* CHECK FOR BUSINESS RULES VIOLATIONS */
1475     /* check for null budget_version_id */
1476     IF NOT ((p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET) OR
1477             (p_context = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN)) THEN
1478               IF l_debug_mode = 'Y' THEN
1479                       pa_debug.g_err_stage:='p_context value is invalid  -  p_context :' || p_context;
1480                       pa_debug.write( l_module_name,pa_debug.g_err_stage,5);
1481               END IF;
1482               PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1483                                    p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
1484                                    p_token1         => 'PROCEDURENAME',
1485                                    p_value1         => l_module_name);
1486               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1487     END IF;
1488 
1489     if p_budget_version_id is NULL AND p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET then
1490         x_return_status := FND_API.G_RET_STS_ERROR;
1491         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
1492                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
1493     end if;
1494 
1495     IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN AND
1496        p_budget_version_id IS NULL AND
1497        p_project_id IS NULL THEN
1498 
1499         IF l_debug_mode = 'Y' THEN
1500            pa_debug.g_err_stage:= 'p_context '||p_context;
1501            pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
1502 
1503            pa_debug.g_err_stage:= 'p_budget_version_id '||p_budget_version_id;
1504            pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
1505 
1506            pa_debug.g_err_stage:= 'p_project_id '||p_project_id;
1507            pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
1508 
1509         END IF;
1510 
1511         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
1512                            p_msg_name         => 'PA_FP_INV_PARAM_PASSED',
1513                            p_token1           => 'PROCEDURENAME',
1514                            p_value1           => l_module_name);
1515         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1516     END IF;
1517 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
1518     l_msg_count := FND_MSG_PUB.count_msg;
1519     if l_msg_count > 0 then
1520         if l_msg_count = 1 then
1521              PA_INTERFACE_UTILS_PUB.get_messages
1522                  (p_encoded        => FND_API.G_TRUE,
1523                   p_msg_index      => 1,
1524                   p_msg_count      => l_msg_count,
1525                   p_msg_data       => l_msg_data,
1526                   p_data           => l_data,
1527                   p_msg_index_out  => l_msg_index_out);
1528              x_msg_data := l_data;
1529              x_msg_count := l_msg_count;
1530             else
1531              x_msg_count := l_msg_count;
1532         end if;
1533             pa_debug.reset_err_stack;
1534             return;
1535     end if;
1536 
1537 /* If There are NO Business Rules Violations , Then proceed with Delete Version Helper*/
1538     if l_msg_count = 0 then
1539         IF P_PA_DEBUG_MODE = 'Y' THEN
1540            pa_debug.write_file('Delete_Version: ' || 'no business errors: continuing with Delete Version Helper');
1541         END IF;
1542         SAVEPOINT PA_FIN_PLAN_PUB_DELETE_H;
1543         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN THEN
1544 
1545             BEGIN
1546                 SELECT fin_plan_type_id
1547                 INTO   l_wp_fin_plan_type_id
1548                 FROM   pa_fin_plan_types_b
1549                 WHERE  use_for_workplan_flag='Y';
1550             EXCEPTION
1551             WHEN NO_DATA_FOUND THEN
1552                 IF l_debug_mode = 'Y' THEN
1553                    pa_debug.g_err_stage:= 'Workplan plan type does not exist';
1554                    pa_debug.write(l_module_name,pa_debug.g_err_stage,3);
1555                 END IF;
1556                 RETURN;
1557             END;
1558 
1559         ELSE
1560 
1561             l_wp_fin_plan_type_id:=NULL;
1562 
1563         END IF;
1564 
1565         -- The budget version ids and option ids fetched with this SELECT
1566         -- will be used in deleting from other tables. The DMLs when executed with these IDs will use the indexes
1567         --improve the performence
1568         IF p_budget_version_id IS NOT NULL THEN
1569 
1570             SELECT proj_fp_options_id,
1571                    fin_plan_version_id
1572             BULK COLLECT INTO
1573                    l_proj_fp_options_id_tbl,
1574                    l_budget_Version_id_tbl
1575             FROM   pa_proj_fp_options
1576             WHERE  fin_plan_version_id=p_budget_version_id
1577             AND    fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id);
1578 
1579         ELSE
1580 
1581             SELECT proj_fp_options_id,
1582                    fin_plan_version_id
1583             BULK COLLECT INTO
1584                    l_proj_fp_options_id_tbl,
1585                    l_budget_Version_id_tbl
1586             FROM   pa_proj_fp_options
1587             WHERE  project_id=p_project_id
1588             AND    (fin_plan_type_id = nvl(l_wp_fin_plan_type_id,fin_plan_type_id) OR
1589                    fin_plan_type_id IS NULL);
1590 
1591         END IF;
1592 
1593         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET THEN
1594 
1595             FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1596 
1597                 /* PA_FIN_PLAN_ADJ_LINES: delete row (keyed on budget_version_id) */
1598                 delete
1599                 from
1600                     pa_fin_plan_adj_lines
1601                 where
1602                     budget_version_id=l_budget_Version_id_tbl(i);
1603 
1604         END IF;
1605 
1606         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET THEN
1607 
1608             FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1609 
1610                 /* PA_FP_ADJ_ELEMENTS: delete row (keyed on budget_version_id) */
1611                 delete
1612                 from
1613                     pa_fp_adj_elements
1614                 where
1615                     budget_version_id=l_budget_Version_id_tbl(i);
1616 
1617         END IF;
1618 
1619         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET THEN
1620 
1621             FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1622 
1623                 /* PA_ORG_FORECAST_LINES: delete row (keyed on budget_version_id as of 2/20/2002) */
1624                 delete
1625                 from
1626                     pa_org_forecast_lines
1627                 where
1628                     budget_version_id = l_budget_Version_id_tbl(i);
1629 
1630         END IF;
1631 
1632         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET THEN
1633             FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1634                 /* PA_ORG_FORECAST_ELEMENTS: delete row (keyed on budget_version_id) */
1635                 delete
1636                 from
1637                     pa_org_fcst_elements
1638                 where
1639                     budget_version_id=l_budget_Version_id_tbl(i);
1640         END IF;
1641 
1642         ----  start of changes for bug 2779637 ----
1643         /* for performance its better to use budget_version_id for the deletion of budget lines*/
1644 
1645         /*
1646         -- PA_BUDGET_LINES: delete row (keyed on PA_RESOURCE_ASSIGNMENTS.resource_assignment_id)
1647         open l_resource_assignments_csr;
1648         loop
1649             fetch l_resource_assignments_csr into l_resource_assignments_rec;
1650             exit when l_resource_assignments_csr%NOTFOUND;
1651             delete
1652             from
1653                 pa_budget_lines
1654             where
1655                 resource_assignment_id=l_resource_assignments_rec.resource_assignment_id;
1656         end loop;
1657         close l_resource_assignments_csr;
1658         */
1659         FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1660             delete
1661             from
1662                 pa_budget_lines
1663             where
1664                 budget_version_id=l_budget_Version_id_tbl(i);
1665          ----   end  of changes for bug 2779637  ----
1666 
1667         FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1668             /* PA_RESOURCE_ASSIGNMENTS: delete row (keyed on budget_version_id) */
1669             delete
1670             from
1671                 pa_resource_assignments
1672             where
1673                 budget_version_id=l_budget_Version_id_tbl(i);
1674         -- Bug Fix: 4569365. Removed MRC code.
1675         /*
1676         FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1677             -- FPB2: MRC : PA_MC_BUDGET_LINES: delete row (keyed on budget_Version_id)
1678             delete
1679             from
1680                 pa_mc_budget_lines
1681             where
1682                 budget_version_id = l_budget_Version_id_tbl(i);
1683         */
1684         FORALL i IN 1..l_proj_fp_options_id_tbl.COUNT
1685             delete
1686             from
1687                pa_fp_txn_currencies
1688             where
1689                proj_fp_options_id = l_proj_fp_options_id_tbl(i); -- bug 2779637
1690 
1691         --IPM Arch Enhancement Bug 4865563 Start
1692            FOR i IN 1..l_budget_Version_id_tbl.COUNT LOOP
1693            if l_budget_Version_id_tbl(i) is not null then  --bug 5441949
1694                PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DTLS
1695                    (P_BUDGET_VERSION_ID              => l_budget_Version_id_tbl(i),
1696                     X_FP_COLS_REC                    => l_fp_cols_rec,
1697                     X_RETURN_STATUS                  => l_return_status,
1698                     X_MSG_COUNT                      => l_msg_count,
1699                     X_MSG_DATA                       => l_msg_data);
1700 
1701                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
1702                        IF P_PA_debug_mode = 'Y' THEN
1703                                        pa_debug.g_err_stage:= 'Error in PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DETAILS';
1704                                        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
1705                        END IF;
1706                        RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1707                     END IF;
1708 
1709                pa_res_asg_currency_pub.maintain_data
1710                (p_fp_cols_rec        => l_fp_cols_rec,
1711                 p_calling_module     => 'UPDATE_PLAN_TRANSACTION',
1712                 p_delete_flag        => 'Y',
1713                 p_version_level_flag => 'Y',
1714                 x_return_status      => l_return_status,
1715                 x_msg_data           => l_msg_count,
1716                 x_msg_count          => l_msg_data);
1717 
1718 
1719                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
1720                        IF P_PA_debug_mode = 'Y' THEN
1721                                        pa_debug.g_err_stage:= 'Error in PA_RES_ASG_CURRENCY_PUB.MAINTAIN_DATA';
1722                                        pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
1723                        END IF;
1724                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
1725                     END IF;
1726                   end if;        --bug 5441949
1727            END LOOP;
1728            --IPM Architechture Enhancement Bug 4865563 - End
1729 
1730         /*================================================================
1731         End of changes for Bug:- 2331201
1732         =================================================================*/
1733         /*===================================================================
1734           Start of changes for Bug :- 2634900 , Control Item Changes On APIs
1735         ===================================================================*/
1736         IF p_context=PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_BUDGET THEN
1737             BEGIN
1738                SELECT project_id,
1739                       ci_id
1740                INTO   l_project_id,
1741                       l_ci_id
1742                FROM   pa_budget_versions
1743                WHERE  budget_version_id = p_budget_version_id;
1744             EXCEPTION
1745                WHEN OTHERS THEN
1746                   pa_debug.g_err_stage:= 'Error while Fetching the data for '||p_budget_version_id;
1747                   IF P_PA_DEBUG_MODE = 'Y' THEN
1748                      pa_debug.write('Delete_Version: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
1749                   END IF;
1750                   RAISE;
1751             END;
1752 
1753             IF l_ci_id IS NULL THEN
1754 
1755                 FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1756                     DELETE FROM pa_fp_merged_ctrl_items
1757                     WHERE project_id = l_project_id
1758                     AND   plan_version_id = l_budget_Version_id_tbl(i);
1759             ELSE --(l_ci_id IS NOT NULL )
1760 
1761                 FORALL i IN 1..l_budget_Version_id_tbl.COUNT
1762                     DELETE FROM pa_fp_merged_ctrl_items
1763                     WHERE project_id = l_project_id
1764                     AND   ci_plan_version_id = l_budget_Version_id_tbl(i);
1765             END IF;
1766         END IF;
1767 
1768 
1769 
1770 
1771         IF p_budget_version_id IS NOT NULL THEN
1772             -- Bug 3572548 Update all the budget versions that have the input budget version id
1773             -- gen source version id with null for that column as this is being deleted
1774 
1775             UPDATE pa_proj_fp_options
1776             SET   gen_src_cost_plan_version_id = DECODE(gen_src_cost_plan_version_id
1777                                              ,p_budget_version_id,NULL,gen_src_cost_plan_version_id)
1778                  ,gen_src_rev_plan_version_id = DECODE(gen_src_rev_plan_version_id
1779                                              ,p_budget_version_id,NULL,gen_src_rev_plan_version_id)
1780                  ,gen_src_all_plan_version_id = DECODE(gen_src_all_plan_version_id
1781                                              ,p_budget_version_id,NULL,gen_src_all_plan_version_id)
1782                  ,gen_src_cost_wp_version_id = DECODE(gen_src_cost_wp_version_id
1783                                              ,p_budget_version_id,NULL,gen_src_cost_wp_version_id)
1784                  ,gen_src_rev_wp_version_id = DECODE(gen_src_rev_wp_version_id
1785                                              ,p_budget_version_id,NULL,gen_src_rev_wp_version_id)
1786                  ,gen_src_all_wp_version_id = DECODE(gen_src_all_wp_version_id
1787                                              ,p_budget_version_id,NULL,gen_src_all_wp_version_id)
1788                  ,record_version_number       = record_version_number + 1
1789                  ,last_update_date            = SYSDATE
1790                  ,last_updated_by             = FND_GLOBAL.user_id
1791                  ,last_update_login           = FND_GLOBAL.login_id
1792             WHERE project_id = l_project_id
1793             AND   fin_plan_option_level_code = 'PLAN_VERSION'
1794             AND   (gen_src_cost_plan_version_id = p_budget_version_id OR
1795                    gen_src_rev_plan_version_id = p_budget_version_id OR
1796                    gen_src_all_plan_version_id = p_budget_version_id OR
1797                    gen_src_cost_wp_version_id  = p_budget_version_id OR
1798                    gen_src_rev_wp_version_id   = p_budget_version_id OR
1799                    gen_src_all_wp_version_id   = p_budget_version_id );
1800         END IF;
1801     end if;
1802     x_return_status := FND_API.G_RET_STS_SUCCESS;
1803     pa_debug.reset_err_stack;
1804 
1805 exception
1806     when pa_fin_plan_pub.rollback_on_error then
1807       IF P_PA_DEBUG_MODE = 'Y' THEN
1808          pa_debug.write_file('Procedure Delete_Version_Helper: rollback_on_error exception');
1809       END IF;
1810       rollback to PA_FIN_PLAN_PUB_DELETE_H;
1811       raise FND_API.G_EXC_UNEXPECTED_ERROR;
1812 
1813     when others then
1814         rollback to PA_FIN_PLAN_PUB_DELETE_H;
1815         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1816         x_msg_count     := 1;
1817         x_msg_data      := SQLERRM;
1818         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
1819                                  p_procedure_name   => 'Delete_Version_Helper');
1820         pa_debug.reset_err_stack;
1821         raise FND_API.G_EXC_UNEXPECTED_ERROR;
1822 end Delete_Version_Helper;
1823 
1824 --Bug 4290043. This is a private API called by copy version. This will return variables to indicate whether to
1825 --copy the actuals, missing rates and amounts
1826 --All the Input parameters are mandator. No validations are done since this is a private and only called by copy
1827 --version
1828 PROCEDURE get_copy_paramters
1829 (
1830       p_source_project_id               IN       pa_projects_all.project_id%TYPE,
1831       p_target_project_id               IN       pa_projects_all.project_id%TYPE,
1832       p_source_plan_class_code          IN       pa_fin_plan_types_b.plan_class_code%TYPE,
1833       p_target_plan_class_code          IN       pa_fin_plan_types_b.plan_class_code%TYPE,
1834       p_source_version_type             IN       pa_budget_versions.version_type%TYPE,
1835       p_target_version_type             IN       pa_budget_versions.version_type%TYPE,
1836       x_copy_actuals_flag               OUT      NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
1837       x_derv_rates_missing_amts_flag    OUT      NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
1838 )
1839 IS
1840 l_debug_mode                               VARCHAR2(30);
1841 l_module_name                              VARCHAR2(100);
1842 
1843 BEGIN
1844     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
1845     l_debug_mode := NVL(l_debug_mode, 'Y');
1846     l_module_name := 'PAFPPUBB.get_copy_paramters';
1847 
1848     -- Set curr function
1849     IF l_debug_mode = 'Y' THEN
1850         pa_debug.set_curr_function(
1851                     p_function   =>'PAFPPUBB.get_copy_paramters'
1852                    ,p_debug_mode => l_debug_mode );
1853 
1854         pa_debug.g_err_stage:='In get_copy_paramters';
1855         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1856 
1857         pa_debug.g_err_stage:='p_source_project_id '||p_source_project_id;
1858         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1859 
1860         pa_debug.g_err_stage:='p_target_project_id '||p_target_project_id;
1861         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1862 
1863         pa_debug.g_err_stage:='p_source_plan_class_code '||p_source_plan_class_code;
1864         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1865 
1866         pa_debug.g_err_stage:='p_target_plan_class_code '||p_target_plan_class_code;
1867         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1868 
1869         pa_debug.g_err_stage:='p_source_version_type '||p_source_version_type;
1870         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1871 
1872         pa_debug.g_err_stage:='p_target_version_type '||p_target_version_type;
1873         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1874 
1875     END IF;
1876 
1877     x_copy_actuals_flag:='Y';
1878     x_derv_rates_missing_amts_flag:='N';
1879 
1880     IF p_source_project_id <> p_target_project_id OR
1881        p_source_plan_class_code <> p_target_plan_class_code OR
1882        p_source_version_type <>  p_target_version_type THEN
1883 
1884         x_copy_actuals_flag := 'N';
1885 
1886     END IF;
1887 
1888     IF (p_source_plan_class_code='FORECAST' AND p_target_plan_class_code = 'BUDGET') OR
1889         p_source_version_type <>  p_target_version_type THEN
1890 
1891         x_derv_rates_missing_amts_flag := 'Y';
1892 
1893     END IF;
1894 
1895     IF l_debug_mode = 'Y' THEN
1896         pa_debug.reset_curr_function;
1897 
1898         pa_debug.g_err_stage:='x_copy_actuals_flag '||x_copy_actuals_flag;
1899         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1900 
1901         pa_debug.g_err_stage:='x_derv_rates_missing_amts_flag '||x_derv_rates_missing_amts_flag;
1902         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1903 
1904         pa_debug.g_err_stage:='Exiting get_copy_paramters';
1905         pa_debug.write( l_module_name,pa_debug.g_err_stage,3);
1906 
1907     END IF;
1908 
1909 
1910 END get_copy_paramters;
1911 
1912 /*===============================================================================
1913   Bug No. 2331201
1914   This is an existing api, used in ORG FORECASTING, modified completely for
1915   Financial Planning. This api has been used to copy data from one version to
1916   another during create working copy and baselining a version. Now this api
1917   would also be used to copy budgets/finplans  and would be called from
1918   pa_fp_copy_from_pkg.copy_plan.Hence this api takes care of copying one verion to
1919   an already existing version also.
1920 
1921   Bug No. 2920954
1922   When p_copy_mode is B, pa_fp_elements and pa_resource_assignments will be copied
1923   to have only planning elements and ras with plan amounts. Calls to
1924   pa_fp_elements_pub.copy_elements and pa_fp_copy_from_pkg.create_res_tasks_maps
1925   modified to include new parameters.
1926 
1927   NOTE:- Do not populate px_target_version_id till the end of the program.
1928 
1929 
1930 
1931  r11.5 FP.M Developement ----------------------------------
1932 
1933  08-JAN-2004 jwhite        Bug 3362316
1934 
1935                            Extensively rewrote Copy_Version and referenced
1936                            procedures calls.
1937 
1938  05-JUL-2004 rravipat      Bug 3731925
1939                            When a working version is created as a copy of another
1940                            version, working version should inherit rbs from parent
1941                            plan type record.
1942  04-Nov-2006 nkumbi        IPM Codde Merge: Bug 5099353: Modified the api to do the rollup when
1943                            a Pre-IPM source version is copied to a IPM level target version, before
1944                            the Optional Upgrade process is run on the source version.
1945   ================================================================================*/
1946 
1947 PROCEDURE Copy_Version
1948     (p_project_id               IN      pa_budget_versions.project_id%TYPE,
1949      p_source_version_id        IN      pa_budget_versions.budget_version_id%TYPE,
1950      p_copy_mode                IN      VARCHAR2,
1951      p_adj_percentage           IN      NUMBER DEFAULT 0,
1952      p_calling_module           IN      VARCHAR2 DEFAULT PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST,
1953      p_pji_rollup_required      IN      VARCHAR2 DEFAULT 'Y',  --Bug 4200168
1954      px_target_version_id       IN  OUT NOCOPY pa_budget_versions.budget_version_id%TYPE, --File.Sql.39 bug 4440895
1955      x_return_status                OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
1956      x_msg_count                    OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
1957      x_msg_data                     OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
1958 IS
1959 
1960 l_debug_mode      VARCHAR2(30);
1961 l_msg_count       NUMBER := 0;
1962 l_data            VARCHAR2(2000);
1963 l_msg_data        VARCHAR2(2000);
1964 l_error_msg_code  VARCHAR2(30);
1965 l_msg_index_out   NUMBER;
1966 l_return_status   VARCHAR2(2000);
1967 
1968 l_adj_percentage  NUMBER := NVL(p_adj_percentage,0);
1969         --Make adjustment percentage zero if passed as null
1970 
1971 l_source_proj_fp_options_id pa_proj_fp_options.proj_fp_options_id%TYPE;
1972 l_source_fp_preference_code pa_proj_fp_options.fin_plan_preference_code%TYPE;
1973 l_source_element_type       pa_fp_elements.element_type%TYPE;
1974 l_source_profile_id         pa_budget_versions.period_profile_id%TYPE;
1975 l_source_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
1976 
1977 
1978 l_target_proj_fp_options_id pa_proj_fp_options.proj_fp_options_id%TYPE;
1979 l_target_fin_plan_type_id   pa_proj_fp_options.fin_plan_type_id%TYPE;
1980 l_target_fp_preference_code pa_proj_fp_options.fin_plan_preference_code%TYPE;
1981 l_plan_in_multi_curr_flag   pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
1982 
1983 l_target_element_type       pa_fp_elements.element_type%TYPE;
1984 
1985 l_budget_version_id         pa_budget_versions.budget_version_id%TYPE;
1986 l_target_version_id         pa_budget_versions.budget_version_id%TYPE;
1987 l_target_profile_id         pa_budget_versions.period_profile_id%TYPE;
1988 
1989 l_project_id                pa_projects_all.project_id%TYPE;
1990 
1991   -- Bug 3362316, 08-JAN-2003: Local Vars for Populating Reporting Lines  --------------------------
1992 
1993 
1994 
1995   l_source_ver_id_tbl      SYSTEM.pa_num_tbl_type := system.pa_num_tbl_type();
1996 
1997   l_dest_ver_id_tbl        SYSTEM.pa_num_tbl_type := system.pa_num_tbl_type();
1998 
1999   l_source_ver_type_tbl  SYSTEM.pa_varchar2_30_tbl_type := system.pa_varchar2_30_tbl_type();
2000 
2001   l_dest_ver_type_tbl    SYSTEM.pa_varchar2_30_tbl_type := system.pa_varchar2_30_tbl_type();
2002 
2003 
2004   -- End, Bug 3362316, 08-JAN-2003: Local Vars for Populating Reporting Lines  --------------------------
2005 
2006     -- Local variables for 3156057
2007 
2008   l_target_appr_rev_plan_flag     pa_budget_versions.approved_rev_plan_type_flag%TYPE;
2009   l_source_appr_rev_plan_flag     pa_budget_versions.approved_rev_plan_type_flag%TYPE;
2010   l_source_plan_in_mc_flag        pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;
2011   l_source_ver_rbs_version_id     pa_proj_fp_options.rbs_version_id%TYPE;-- Bug 3731925
2012   l_target_pt_lvl_rbs_version_id  pa_proj_fp_options.rbs_version_id%TYPE;-- Bug 3731925
2013 
2014   --Declared for Bug 4290043
2015   l_source_plan_class_code        pa_fin_plan_types_b.plan_class_code%TYPE;
2016   l_target_plan_class_code        pa_fin_plan_types_b.plan_class_code%TYPE;
2017   l_copy_actuals_flag             VARCHAR2(1);
2018   l_derv_rates_missing_amts_flag  VARCHAR2(1);
2019 
2020   --IPM Architechture Enhancement Bug 4865563
2021      l_src_fp_cols_rec               PA_FP_GEN_AMOUNT_UTILS.FP_COLS;
2022      l_fp_cols_rec                   PA_FP_GEN_AMOUNT_UTILS.FP_COLS;  --This variable will be used to call pa_resource_asgn_curr maintenance api
2023      l_src_version_type              VARCHAR2(15);
2024      l_target_version_type           VARCHAR2(15);
2025      l_ra_id_tbl                     SYSTEM.pa_num_tbl_type := system.pa_num_tbl_type();
2026      l_txn_currency_code_tbl         SYSTEM.pa_varchar2_15_tbl_type := system.pa_varchar2_15_tbl_type();
2027      l_txn_raw_cost_rate_ovd_tbl     SYSTEM.pa_num_tbl_type := system.pa_num_tbl_type();
2028      l_txn_burden_cost_rate_ovd_tbl  SYSTEM.pa_num_tbl_type := system.pa_num_tbl_type();
2029      l_txn_bill_rate_ovd_tbl         SYSTEM.pa_num_tbl_type := system.pa_num_tbl_type();
2030      l_debug_level5                  NUMBER:=5;
2031 
2032    /* Bug 5099353 Start */
2033      l_is_eligible_for_rollup        VARCHAR2(1);
2034      l_chk_tgt_ver_status            VARCHAR2(1);
2035 
2036      Cursor is_eligible_for_rollup(c_project_id                IN NUMBER,
2037                                    c_source_proj_fp_options_id IN NUMBER)  is
2038      Select 'Y' from dual
2039      where EXISTS(
2040        select 1
2041        from pa_budget_versions pbv
2042        where pbv.budget_version_id = p_source_version_id and pbv.prc_generated_flag='M')
2043      and NOT EXISTS(
2044        select 1
2045        from PA_FP_UPGRADE_AUDIT pua
2046        where pua.project_id = c_project_id
2047        and pua.proj_fp_options_id_rup = c_source_proj_fp_options_id
2048        and pua.upgraded_flag = 'Y');
2049 
2050      Cursor chk_tgt_ver_status is
2051      Select 'Y' from dual
2052      where exists (select 1 from pa_budget_versions pbv where pbv.budget_version_id = px_target_version_id and pbv.budget_Status_code = 'W' )
2053      and exists (select 1 from pa_budget_lines bl, pa_budget_versions pbv
2054                  where pbv.budget_version_id = px_target_version_id
2055                  and pbv.budget_version_id = bl.budget_version_id
2056                  and (bl.cost_rejection_code  IS NOT NULL
2057                       OR bl.revenue_rejection_code IS NOT NULL
2058                       OR bl.burden_rejection_code IS NOT NULL
2059                       OR bl.pfc_cur_conv_rejection_code IS NOT NULL
2060                       OR bl.pc_cur_conv_rejection_code IS NOT NULL)
2061                  );
2062    /* Bug 5099353 End */
2063 
2064 
2065 
2066 BEGIN
2067 
2068     pa_debug.set_err_stack ('PA_FIN_PLAN_PUB.Copy_Version');
2069     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
2070     l_debug_mode := NVL(l_debug_mode, 'Y');
2071     IF P_PA_DEBUG_MODE = 'Y' THEN
2072        pa_debug.set_process('Copy_Version: ' || 'PLSQL','LOG',l_debug_mode);
2073     END IF;
2074     x_msg_count := 0;
2075     x_return_status := FND_API.G_RET_STS_SUCCESS;
2076 
2077     -- Check for business rules violations
2078 
2079     IF P_PA_DEBUG_MODE = 'Y' THEN
2080         pa_debug.g_err_stage := 'Parameter Validation';
2081         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2082     END IF;
2083 
2084     -- Check for null source_version_id
2085 
2086     IF p_source_version_id IS NULL THEN
2087         pa_debug.g_err_stage := 'Source_plan='||p_source_version_id;
2088         IF P_PA_DEBUG_MODE = 'Y' THEN
2089            pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,5);
2090         END IF;
2091 
2092         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
2093                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
2094 
2095         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2096     END IF;
2097 
2098     SAVEPOINT PA_FIN_PLAN_PUB_COPY_VERSION;
2099 
2100     --Initialise l_budget_version_id.
2101 
2102     l_budget_version_id := px_target_version_id;
2103 
2104     IF P_PA_DEBUG_MODE = 'Y' THEN
2105         pa_debug.g_err_stage := 'Source_plan='||p_source_version_id;
2106         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2107 
2108         pa_debug.g_err_stage := 'Target_plan='||l_budget_version_id;
2109         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2110     END IF;
2111 
2112     --Bug 4290043. Fire the SQLs to get the details of the source/target version ids.
2113     --Fetch proj fp options id for source version
2114     --Bug 4290043. Selected source plan class code
2115 
2116     IF P_PA_DEBUG_MODE = 'Y' THEN
2117         pa_debug.g_err_stage := 'Fetching the Source version details';
2118         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2119     END IF;
2120 
2121     SELECT  pfo.proj_fp_options_id
2122            ,pfo.fin_plan_preference_code
2123            ,pfo.project_id
2124            ,pfo.fin_plan_type_id
2125            ,DECODE(pfo.fin_plan_preference_code,
2126                    PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST,
2127                    PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY , PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE,
2128                    PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL),
2129            pfo.plan_in_multi_curr_flag,
2130            nvl(pfo.approved_rev_plan_type_flag,'N'),
2131            pfo.rbs_version_id,
2132            fin.plan_class_code
2133     INTO   l_source_proj_fp_options_id
2134            ,l_source_fp_preference_code
2135            ,l_project_id
2136            ,l_source_fin_plan_type_id
2137            ,l_source_element_type
2138            ,l_source_plan_in_mc_flag
2139            ,l_source_appr_rev_plan_flag
2140            ,l_source_ver_rbs_version_id -- Bug 3731925
2141            ,l_source_plan_class_code
2142     FROM   pa_proj_fp_options pfo,
2143            pa_fin_plan_types_b fin
2144     WHERE  pfo.fin_plan_version_id = p_source_version_id
2145     AND    fin.fin_plan_type_id=pfo.fin_plan_type_id;
2146 
2147     --Fetch proj fp options id for target version if its already existing.
2148 
2149     IF px_target_version_id IS NOT NULL THEN
2150 
2151        SELECT  pfo.proj_fp_options_id
2152               ,pfo.fin_plan_preference_code
2153               ,pfo.fin_plan_type_id
2154               ,DECODE(pfo.fin_plan_preference_code,
2155                    PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY ,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST,
2156                    PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY , PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE,
2157                    PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME,PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL),
2158                nvl(pfo.approved_rev_plan_type_flag,'N'),
2159                fin.plan_class_code
2160        INTO   l_target_proj_fp_options_id
2161               ,l_target_fp_preference_code
2162               ,l_target_fin_plan_type_id
2163               ,l_target_element_type
2164               ,l_target_appr_rev_plan_flag
2165               ,l_target_plan_class_code
2166        FROM   pa_proj_fp_options pfo,
2167               pa_fin_plan_types_b fin
2168        WHERE  pfo.fin_plan_version_id = l_budget_version_id
2169        AND    pfo.fin_plan_type_id = fin.fin_plan_type_id;
2170 
2171     ELSE
2172        --Initialise l_target_element_type to 'BOTH'
2173 
2174        l_target_element_type := 'BOTH';
2175        l_target_proj_fp_options_id := NULL;
2176        l_target_fin_plan_type_id := l_source_fin_plan_type_id;
2177        l_target_fp_preference_code := l_source_fp_preference_code;
2178        l_target_plan_class_code :=  l_source_plan_class_code;
2179 
2180     END IF;
2181 
2182     IF P_PA_DEBUG_MODE = 'Y' THEN
2183         pa_debug.g_err_stage := 'l_source_proj_fp_options_id ='||l_source_proj_fp_options_id;
2184         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2185 
2186         pa_debug.g_err_stage := 'l_source_fp_preference_code = '||l_source_fp_preference_code;
2187         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2188 
2189         pa_debug.g_err_stage := 'l_source_project_id = '||l_project_id;
2190         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2191 
2192         pa_debug.g_err_stage := 'l_source_fin_plan_type_id ='||l_source_fin_plan_type_id;
2193         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2194 
2195         pa_debug.g_err_stage := 'l_source_element_type = '||l_source_element_type;
2196         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2197     END IF;
2198 
2199 
2200 
2201     IF P_PA_DEBUG_MODE = 'Y' THEN
2202         pa_debug.g_err_stage := 'l_target_proj_fp_options_id ='||l_target_proj_fp_options_id;
2203         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2204 
2205         pa_debug.g_err_stage := 'l_target_fp_preference_code = '||l_target_fp_preference_code;
2206         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2207 
2208         pa_debug.g_err_stage := 'l_target_fin_plan_type_id = '||l_target_fin_plan_type_id;
2209         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2210 
2211         pa_debug.g_err_stage := 'l_target_element_type ='||l_target_element_type;
2212         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2213     END IF;
2214 
2215     --Call the API to decide on whether to copy actuals/rates or not. Bug 4290043
2216     get_copy_paramters
2217     (p_source_project_id             => l_project_id,
2218      p_target_project_id             => l_project_id,
2219      p_source_plan_class_code        => l_source_plan_class_code,
2220      p_target_plan_class_code        => l_target_plan_class_code,
2221      p_source_version_type           => l_source_fp_preference_code,
2222      p_target_version_type           => l_target_fp_preference_code,
2223      x_copy_actuals_flag             => l_copy_actuals_flag,
2224      x_derv_rates_missing_amts_flag  => l_derv_rates_missing_amts_flag);
2225 
2226 
2227     -- Calling copy_budget_version api.This api will update the budget version incase its
2228     -- already existing else it will create a new version.
2229 
2230     IF P_PA_DEBUG_MODE = 'Y' THEN
2231         pa_debug.g_err_stage := 'Copying budget version';
2232         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2233     END IF;
2234 
2235     --Bug 4290043 .Added the parameters for copy actuals flag and copy missing amounts/rates fla
2236     PA_FP_COPY_FROM_PKG.Copy_Budget_Version(
2237                  p_source_project_id        =>  p_project_id
2238                  ,p_target_project_id       =>  p_project_id
2239                  ,p_source_version_id       =>  p_source_version_id
2240                  ,p_copy_mode               =>  p_copy_mode
2241                  ,p_adj_percentage          =>  l_adj_percentage
2242                  ,p_calling_module          =>  p_calling_module
2243                  ,p_copy_actuals_flag       =>  l_copy_actuals_flag
2244                  ,px_target_version_id      =>  l_budget_version_id
2245                  ,x_return_status           =>  l_return_status
2246                  ,x_msg_count               =>  l_msg_count
2247                  ,x_msg_data                =>  l_msg_data );
2248 
2249     -- Start of changes for BUG :- 2634900
2250     -- Copy the links from the links for the source plan version in the
2251     -- PA_FP_MERGED_CTRL_ITEMS table  to the target version.
2252 
2253 
2254     pa_fp_ci_merge.copy_merged_ctrl_items
2255            (  p_project_id            =>  p_project_id
2256              ,p_source_version_id     =>  p_source_version_id
2257              ,p_target_version_id     =>  l_budget_version_id
2258              ,x_return_status         =>  l_return_status
2259              ,x_msg_count             =>  l_msg_count
2260              ,x_msg_data              =>  l_msg_data );
2261 
2262     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2263            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2264     END IF;
2265 
2266     -- End of changes for BUG :- 2634900
2267 
2268     --Calling create fp option api to insert  or update pa_proj_fp_options.
2269     --In case of create working copy, it will insert into pa_proj_fp_options.
2270     --In case of target version is passed in copy plan, we update pa_proj_fp_options.
2271 
2272 
2273     --Calling create fp option api
2274 
2275     IF P_PA_DEBUG_MODE = 'Y' THEN
2276         pa_debug.g_err_stage := 'Calling create_fp_option api';
2277         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2278     END IF;
2279 
2280     PA_PROJ_FP_OPTIONS_PUB.create_fp_option (
2281                px_target_proj_fp_option_id   =>  l_target_proj_fp_options_id
2282                ,p_source_proj_fp_option_id   =>  l_source_proj_fp_options_id
2283                ,p_target_fp_option_level_code => PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION
2284                ,p_target_fp_preference_code  =>  l_target_fp_preference_code
2285                ,p_target_fin_plan_version_id =>  l_budget_version_id --newly derived ot passed value
2286                ,p_target_project_id          =>  l_project_id        --project_id of source version
2287                ,p_target_plan_type_id        =>  l_target_fin_plan_type_id  --plan type id of target version
2288                ,x_return_status              =>  l_return_status
2289                ,x_msg_count                  =>  l_msg_count
2290                ,x_msg_data                   =>  l_msg_data );
2291 
2292     --Calling apis specific to FIN_PLAN
2293 
2294     --Calling api which inserts/updates elements into pa_fp_elements
2295 
2296     IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN THEN
2297 
2298 
2299           --Calling an api to copy transaction currencies selected in source version to target version
2300           --Fetch multi currency flag for the target/new budget.
2301 
2302           IF P_PA_DEBUG_MODE = 'Y' THEN
2303               pa_debug.g_err_stage := 'Fetching multi currency flag for the target/new budget';
2304               pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2305           END IF;
2306 
2307           SELECT pfo.plan_in_multi_curr_flag
2308           INTO   l_plan_in_multi_curr_flag
2309           FROM   pa_proj_fp_options pfo
2310           WHERE  pfo.fin_plan_version_id = l_budget_version_id;
2311 
2312 /*          IF l_plan_in_multi_curr_flag = 'Y' THEN     Commented for bug 2706430 */
2313 
2314                 IF P_PA_DEBUG_MODE = 'Y' THEN
2315                     pa_debug.g_err_stage := 'Calling copy_fp_txn_currencies api';
2316                     pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2317                 END IF;
2318 
2319                 PA_FP_TXN_CURRENCIES_PUB.copy_fp_txn_currencies (
2320                              p_source_fp_option_id   => l_source_proj_fp_options_id
2321                              ,p_target_fp_option_id  => l_target_proj_fp_options_id
2322                              ,p_target_fp_preference_code => NULL
2323                              ,p_plan_in_multi_curr_flag => l_plan_in_multi_curr_flag    --bug 2706430
2324                              ,x_return_status        => l_return_status
2325                              ,x_msg_count            => l_msg_count
2326                              ,x_msg_data             => l_msg_data );
2327 /*          END IF;                Commented for bug 2706430   */
2328     END IF;
2329 
2330     --Calling copy_resource_assignments to insert records in pa_resource_assignments using
2331     --pa_fp_ra_map_tmp.
2332 
2333 
2334     --If the calling module is Financial Planning then its not required to go thru te route of
2335     --create_res_Task_maps as copy_resource_assignments would take care of the mapping logic too
2336     IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN THEN
2337 
2338         IF P_PA_DEBUG_MODE = 'Y' THEN
2339             pa_debug.g_err_stage := 'Calling copy_resource_assignment';
2340             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2341         END IF;
2342 
2343         PA_FP_COPY_FROM_PKG.copy_resource_assignments(
2344                          p_source_plan_version_id   => p_source_version_id
2345                          ,p_target_plan_version_id  => l_budget_version_id
2346                          ,p_adj_percentage          => l_adj_percentage
2347                          ,x_return_status           => l_return_status
2348                          ,x_msg_count               => l_msg_count
2349                          ,x_msg_data                => l_msg_data );
2350 
2351         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2352 
2353             pa_debug.g_err_stage := 'PA_FP_COPY_FROM_PKG.copy_resource_assignments returned error';
2354             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2355             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2356 
2357         END IF;
2358 
2359         --Delete budget lines of target version if any then insert new rows for target
2360         --using source budget lines depending on adjustment percentage. If adjustment
2361         --percentage is non zero,amount columns aren't copied and also roll up records
2362         --aren't entered.
2363 
2364         IF P_PA_DEBUG_MODE = 'Y' THEN
2365             pa_debug.g_err_stage := 'Calling copy_budget_lines';
2366             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2367         END IF;
2368 
2369         /* 3156057: If source plan is mc enabled but not appr rev and the target is appr rev,
2370                       then copy copy_budget_lines_appr_rev will be called to group the source
2371                       budget lines by PFC for creating target budget lines */
2372 
2373         IF l_source_appr_rev_plan_flag = 'N' and l_source_plan_in_mc_flag = 'Y' and l_target_appr_rev_plan_flag = 'Y' THEN
2374 
2375                IF P_PA_DEBUG_MODE = 'Y' THEN
2376                     pa_debug.g_err_stage:='Copying a mc enabled version into a appr rev plan type version.';
2377                     pa_debug.write('Copy_Plan: ' ||  l_module_name,pa_debug.g_err_stage,3);
2378                END IF;
2379 
2380             --Bug 4290043. Added  p_derv_rates_missing_amts_flag. Note that actuals will never be copied in this
2381             --case since the target is always Budget
2382             PA_FP_COPY_FROM_PKG.copy_budget_lines_appr_rev (
2383                           p_source_plan_version_id        => p_source_version_id
2384                           ,p_target_plan_version_id       => l_budget_version_id
2385                           ,p_adj_percentage               => l_adj_percentage
2386                           ,p_derv_rates_missing_amts_flag => l_derv_rates_missing_amts_flag
2387                           ,x_return_status                => l_return_status
2388                           ,x_msg_count                    => l_msg_count
2389                           ,x_msg_data                     => l_msg_data );
2390 
2391             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2392                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2393             END IF;
2394 
2395         ELSE
2396 
2397             --Bug 4290043. Added p_copy_actuals_flag and p_derv_rates_missing_amts_flag
2398             PA_FP_COPY_FROM_PKG.copy_budget_lines (
2399                           p_source_plan_version_id          => p_source_version_id
2400                           ,p_target_plan_version_id         => l_budget_version_id
2401                           ,p_adj_percentage                 => l_adj_percentage
2402                           ,p_copy_actuals_flag              => l_copy_actuals_flag
2403                           ,p_derv_rates_missing_amts_flag   => l_derv_rates_missing_amts_flag
2404                           ,x_return_status                  => l_return_status
2405                           ,x_msg_count                      => l_msg_count
2406                           ,x_msg_data                       => l_msg_data );
2407 
2408             IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2409 
2410                 pa_debug.g_err_stage := 'PA_FP_COPY_FROM_PKG.copy_budget_lines returned error';
2411                 pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2412                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2413 
2414             END IF;
2415 
2416 
2417         END IF; -- 3156057
2418 
2419     --In Org Forecasting Context, create res task maps should be called to create the mapping between source
2420     --and target resource assignments. After that resource assignments and budget lines should be copied.
2421     ELSIF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST THEN
2422 
2423         --Calling create_res_task_maps api to generate new resource_assignment_ids
2424         --and store them in pa_fp_ra_map_tmp table
2425 
2426 
2427         IF P_PA_DEBUG_MODE = 'Y' THEN
2428             pa_debug.g_err_stage := 'Calling create_res_task_maps';
2429             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2430         END IF;
2431 
2432         pa_fp_org_fcst_gen_pub.create_res_task_maps(
2433                 p_source_project_id       => p_project_id
2434                 ,p_target_project_id      => p_project_id
2435                 ,p_source_plan_version_id => p_source_version_id
2436                 ,p_adj_percentage         => l_adj_percentage
2437                 ,p_copy_mode              => p_copy_mode      /* Bug 2920954 */
2438                 ,p_calling_module         => p_calling_module /* Bug 2920954 */
2439                 ,x_return_status          => l_return_status
2440                 ,x_msg_count              => l_msg_count
2441                 ,x_msg_data               => l_msg_data );
2442 
2443         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2444 
2445             pa_debug.g_err_stage := 'pa_fp_org_fcst_gen_pub.create_res_task_maps returned error';
2446             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2447             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2448 
2449         END IF;
2450 
2451 
2452         IF P_PA_DEBUG_MODE = 'Y' THEN
2453             pa_debug.g_err_stage := 'Calling copy_resource_assignment';
2454             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2455         END IF;
2456 
2457         pa_fp_org_fcst_gen_pub.copy_resource_assignments(
2458                          p_source_plan_version_id   => p_source_version_id
2459                          ,p_target_plan_version_id  => l_budget_version_id
2460                          ,p_adj_percentage          => l_adj_percentage
2461                          ,x_return_status           => l_return_status
2462                          ,x_msg_count               => l_msg_count
2463                          ,x_msg_data                => l_msg_data );
2464 
2465         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2466 
2467             pa_debug.g_err_stage := 'pa_fp_org_fcst_gen_pub.copy_resource_assignments returned error';
2468             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2469             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2470 
2471         END IF;
2472 
2473 
2474         IF P_PA_DEBUG_MODE = 'Y' THEN
2475             pa_debug.g_err_stage := 'Calling copy_budget_lines';
2476             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2477         END IF;
2478 
2479         pa_fp_org_fcst_gen_pub.copy_budget_lines (
2480                       p_source_plan_version_id   => p_source_version_id
2481                       ,p_target_plan_version_id  => l_budget_version_id
2482                       ,p_adj_percentage          => l_adj_percentage
2483                       ,x_return_status           => l_return_status
2484                       ,x_msg_count               => l_msg_count
2485                       ,x_msg_data                => l_msg_data );
2486 
2487         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2488 
2489             pa_debug.g_err_stage := 'pa_fp_org_fcst_gen_pub.copy_budget_lines returned error';
2490             pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2491             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2492 
2493         END IF;
2494 
2495 
2496     END IF;
2497 
2498     --Calling convert_txn_currency to complete pa_budget_lines by converting
2499     --txn currency'amounts into project projfunc amounts for finplan.
2500 
2501     --Bug 4290043. IF the target version can have missing amounts which will be derived during copyu
2502     --then the PC/PFC amounts should be rederived. Note that even if l_derv_rates_missing_amts_flag is Y,
2503     --only the rates will be derived(and not amounts) when l_source_fp_preference_code is same as
2504     --l_target_fp_preference_code and hence the MC api need not be called
2505     IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN
2506        AND (l_adj_percentage <> 0
2507          OR ( l_derv_rates_missing_amts_flag='Y' AND
2508               l_source_fp_preference_code <> l_target_fp_preference_code)) THEN
2509 
2510        /* 3156057 */
2511 
2512      IF l_source_appr_rev_plan_flag = 'N' and l_source_plan_in_mc_flag = 'Y' and l_target_appr_rev_plan_flag = 'Y' THEN
2513 
2514            IF P_PA_DEBUG_MODE = 'Y' THEN
2515                 pa_debug.g_err_stage:='Not calling convert_txn_currency since copying a mc enabled version into a appr rev plan type version.';
2516                 pa_debug.write('Copy_Plan: ' ||  l_module_name,pa_debug.g_err_stage,3);
2517            END IF;
2518 
2519      ELSE
2520 
2521             IF P_PA_DEBUG_MODE = 'Y' THEN
2522                 pa_debug.g_err_stage := 'Calling convert_txn_currency';
2523                 pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2524             END IF;
2525 
2526             PA_FP_MULTI_CURRENCY_PKG.convert_txn_currency (
2527                               p_budget_version_id   => l_budget_version_id
2528                               ,p_entire_version     => 'Y'
2529                               ,x_return_status      => l_return_status
2530                               ,x_msg_count          => l_msg_count
2531                               ,x_msg_data           => l_msg_data );
2532        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN /* Bug# 2644641 */
2533          raise PA_FP_CONSTANTS_PKG.INVALID_ARG_EXC;
2534        END IF;
2535      END IF; -- 3156057
2536     END IF;
2537 
2538     -- Bug Fix: 4569365. Removed MRC code.
2539     /* FPB2: MRC - Needs to done only in case of FINPLAN */
2540 
2541     /*
2542     IF P_PA_DEBUG_MODE = 'Y' THEN
2543         pa_debug.g_err_stage:='Calling mrc api ........ ';
2544         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2545     END IF;
2546 
2547     IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN THEN
2548 
2549        -- Nvl is handled because we donot want to overwrite calling_module set already ,eg., COPY_PROJECTS
2550 
2551        PA_MRC_FINPLAN.G_CALLING_MODULE := Nvl(PA_MRC_FINPLAN.G_CALLING_MODULE,PA_MRC_FINPLAN.G_COPY_VERSION);
2552 
2553        IF P_PA_DEBUG_MODE = 'Y' THEN
2554            pa_debug.g_err_stage:='before mrc api ........ MRC Calling module : ' || PA_MRC_FINPLAN.G_CALLING_MODULE;
2555            pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2556        END IF;
2557 
2558        IF PA_MRC_FINPLAN.G_MRC_ENABLED_FOR_BUDGETS IS NULL THEN
2559               PA_MRC_FINPLAN.CHECK_MRC_INSTALL
2560                         (x_return_status      => l_return_status,
2561                          x_msg_count          => l_msg_count,
2562                          x_msg_data           => l_msg_data);
2563        END IF;
2564 
2565        IF PA_MRC_FINPLAN.G_MRC_ENABLED_FOR_BUDGETS AND
2566           (PA_MRC_FINPLAN.G_FINPLAN_MRC_OPTION_CODE = 'A' OR
2567            (PA_MRC_FINPLAN.G_FINPLAN_MRC_OPTION_CODE = 'B' and p_copy_mode = 'B')) THEN
2568 
2569                 --Bug 4290043. If amounts/rates are derived in the target version then MRC lines should not be copied
2570                 --from source and they should be created by looking at budget lines of target
2571                 IF nvl(l_adj_percentage,0) = 0 AND
2572                    PA_MRC_FINPLAN.G_FINPLAN_MRC_OPTION_CODE = 'A'  AND
2573                    (l_derv_rates_missing_amts_flag = 'N'  OR
2574                     l_source_fp_preference_code = l_target_fp_preference_code ) THEN
2575 
2576                     IF P_PA_DEBUG_MODE = 'Y' THEN
2577                         pa_debug.g_err_stage:='before mrc api adj % is zero ';
2578                         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2579                     END IF;
2580 
2581                     -- 3156057: If source plan is mc enabled but not appr rev and the target is appr rev,
2582                     --                  then copy_mc_budget_lines_appr_rev will be called to group the source
2583                     --                  mc budget lines by currency for creating target mc budget lines
2584 
2585                          IF l_source_appr_rev_plan_flag = 'N'
2586                          and l_source_plan_in_mc_flag = 'Y'
2587                          and l_target_appr_rev_plan_flag = 'Y' THEN
2588 
2589 
2590                                    IF P_PA_DEBUG_MODE = 'Y' THEN
2591                                        pa_debug.g_err_stage:='calling copy_mc_budget_lines_appr_rev ';
2592                                        pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2593                                     END IF;
2594 
2595 
2596                                  PA_MRC_FINPLAN.COPY_MC_BUDGET_LINES_APPR_REV
2597                                      (p_source_fin_plan_version_id => p_source_version_id,
2598                                       p_target_fin_plan_version_id => l_budget_version_id,
2599                                       x_return_status              => l_return_status,
2600                                       x_msg_count                  => x_msg_count,
2601                                       x_msg_data                   => x_msg_data);
2602 
2603                                   IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2604 
2605                                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2606 
2607                                   END IF;
2608 
2609                          ELSE
2610 
2611                                    IF P_PA_DEBUG_MODE = 'Y' THEN
2612                                        pa_debug.g_err_stage:='calling copy_mc_budget_lines ';
2613                                        pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2614                                     END IF;
2615 
2616                                  PA_MRC_FINPLAN.COPY_MC_BUDGET_LINES
2617                                      (p_source_fin_plan_version_id => p_source_version_id,
2618                                       p_target_fin_plan_version_id => l_budget_version_id,
2619                                       x_return_status              => l_return_status,
2620                                       x_msg_count                  => x_msg_count,
2621                                       x_msg_data                   => x_msg_data);
2622 
2623                          END IF; -- 3156057
2624 
2625                 ELSE
2626 
2627                     IF P_PA_DEBUG_MODE = 'Y' THEN
2628                         pa_debug.g_err_stage:='before mrc api adj % is NOT zero ';
2629                         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2630                     END IF;
2631 
2632                         PA_MRC_FINPLAN.MAINTAIN_ALL_MC_BUDGET_LINES
2633 -- Bug# 2657812 - Commented -  (p_fin_plan_version_id => p_source_version_id, - mrc should be done for target
2634 -- Bug# 2657812
2635                         (p_fin_plan_version_id => l_budget_version_id, -- Target version should be passed
2636                                 p_entire_version      => 'Y',
2637                                 x_return_status       => l_return_status,
2638                                 x_msg_count           => x_msg_count,
2639                                 x_msg_data            => x_msg_data);
2640 
2641                 END IF;
2642        END IF;
2643 
2644        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2645          RAISE g_mrc_exception;
2646        END IF;
2647 
2648        PA_MRC_FINPLAN.G_CALLING_MODULE := NULL;
2649 
2650      END IF;
2651      */
2652 
2653     --Bug 4290043. Rollup API should be called if any of the amounts in the target version are re-derived
2654     IF l_adj_percentage = 0 AND
2655       ( l_derv_rates_missing_amts_flag='N' OR
2656         l_source_fp_preference_code = l_target_fp_preference_code)THEN
2657         --Fetch source profile id
2658         BEGIN
2659                 SELECT period_profile_id
2660                 INTO   l_source_profile_id
2661                 FROM   PA_BUDGET_VERSIONS
2662                 WHERE  budget_version_id = p_source_version_id;
2663         EXCEPTION
2664                 WHEN NO_DATA_FOUND THEN
2665                       l_source_profile_id := NULL;
2666         END;
2667 
2668         --Fetch target profile id
2669 
2670         BEGIN
2671                 SELECT period_profile_id
2672                 INTO   l_target_profile_id
2673                 FROM   PA_BUDGET_VERSIONS
2674                 WHERE  budget_version_id = l_budget_version_id;
2675         EXCEPTION
2676                 WHEN NO_DATA_FOUND THEN
2677                     l_target_profile_id := NULL;
2678         END;
2679 
2680         IF (l_source_profile_id IS NOT NULL)  AND (l_target_profile_id IS NOT NULL) THEN
2681              IF l_source_profile_id = l_target_profile_id THEN
2682 
2683 
2684               -- Bug 3362316, 08-JAN-2003: Added New IF/END IF fro ORG_FORECAST  ----------
2685 
2686               IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST
2687                   THEN
2688 
2689 
2690                   --copy the  period denorm directly from source to target
2691 
2692                   IF P_PA_DEBUG_MODE = 'Y' THEN
2693                       pa_debug.g_err_stage := 'Calling copy_periods_denorm api';
2694                       pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2695                   END IF;
2696 
2697                   PA_FP_COPY_FROM_PKG.copy_periods_denorm (
2698                                p_source_plan_version_id   => p_source_version_id
2699                                ,p_target_plan_version_id  => l_budget_version_id
2700                                ,p_calling_module          => p_calling_module
2701                                ,x_return_status           => l_return_status
2702                                ,x_msg_count               => l_msg_count
2703                                ,x_msg_data                => l_msg_data );
2704 
2705 
2706                END IF;
2707 
2708                -- End, Bug 3362316, 08-JAN-2003: Added New IF/END IF fro ORG_FORECAST  --------
2709 
2710 
2711               ELSIF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST THEN
2712 
2713                     /* Please note that in case of org forecast, we are NOT using the current
2714                        period profile id for the target version. We copy the period profile id
2715                        from the source version. This is a known bug and bug# 2521711 is logged
2716                        to track this change. When this bug is fixed, we need to comment the
2717                        below update and call to copy_period_denorm and call call_maintain_plan_matrix
2718                        instead. We also need to change call_maintain_plan_matrix and create_org_fcst_elements
2719                        to fix this issue */
2720 
2721                     --Update the new budget version/target's period profile id as that of the source version
2722 
2723                     UPDATE pa_budget_versions
2724                     SET    period_profile_id = l_source_profile_id
2725                     WHERE  budget_version_id = l_budget_version_id;
2726 
2727                     --Calling copy_periods_denorm api
2728 
2729                     pa_debug.g_err_stage := 'Calling copy_periods_denorm api';
2730                     IF P_PA_DEBUG_MODE = 'Y' THEN
2731                        pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2732                     END IF;
2733 
2734                     PA_FP_COPY_FROM_PKG.copy_periods_denorm (
2735                                p_source_plan_version_id   => p_source_version_id
2736                                ,p_target_plan_version_id  => l_budget_version_id
2737                                ,p_calling_module          => p_calling_module
2738                                ,x_return_status           => l_return_status
2739                                ,x_msg_count               => l_msg_count
2740                                ,x_msg_data                => l_msg_data );
2741 
2742                     /*--if source and target profile ids are different then call
2743                     --call_maintain_plan_matrix api
2744 
2745                     pa_debug.g_err_stage := 'Calling call_maintain_plan_matrix api';
2746                     IF P_PA_DEBUG_MODE = 'Y' THEN
2747                        pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2748                     END IF;
2749 
2750                     Call_maintain_plan_matrix(
2751                            p_budget_version_id => l_budget_version_id,
2752                            p_data_source    => PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST,
2753                            x_return_status     => l_return_status,
2754                            x_msg_count         => l_msg_count,
2755                            x_msg_data          => l_msg_data ); */
2756               END IF;
2757         END IF; --l_target_profile_id IS NOT NULL
2758        END IF; --l_adj_percentage
2759 
2760      --IPM Arch Enhancement - Start - Bug 4865563
2761        IF p_calling_module <> PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST THEN
2762 
2763                PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DTLS
2764                    (P_BUDGET_VERSION_ID              => p_source_version_id,
2765                     X_FP_COLS_REC                    => l_src_fp_cols_rec,
2766                     X_RETURN_STATUS                  => l_return_status,
2767                     X_MSG_COUNT                      => l_msg_count,
2768                     X_MSG_DATA                       => l_msg_data);
2769 
2770                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
2771                        IF P_PA_debug_mode = 'Y' THEN
2772                           pa_debug.g_err_stage:= 'Error in SRC PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DETAILS';
2773                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
2774                        END IF;
2775                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2776                     END IF;
2777 
2778                 PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DTLS
2779                    (P_BUDGET_VERSION_ID              => l_budget_version_id,
2780                     X_FP_COLS_REC                    => l_fp_cols_rec,
2781                     X_RETURN_STATUS                  => l_return_status,
2782                     X_MSG_COUNT                      => l_msg_count,
2783                     X_MSG_DATA                       => l_msg_data);
2784 
2785                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
2786                        IF P_PA_debug_mode = 'Y' THEN
2787                           pa_debug.g_err_stage:= 'Error in TARGET PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DETAILS';
2788                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
2789                        END IF;
2790                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2791                     END IF;
2792 
2793                l_src_version_type :=l_src_fp_cols_rec.x_version_type;
2794                l_target_version_type :=l_fp_cols_rec.x_version_type;
2795 
2796                    --Calling populate_display_qty for populating display_quantity in pa_budget_lines
2797                    PA_BUDGET_LINES_UTILS.populate_display_qty
2798                    (p_budget_version_id    => l_budget_version_id,
2799                     p_context              => 'FINANCIAL',
2800                     p_use_temp_table_flag  => 'N',
2801                     x_return_status        => l_return_status);
2802 
2803                 IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
2804                    IF P_PA_debug_mode = 'Y' THEN
2805                       pa_debug.g_err_stage:= 'Error in PA_BUDGET_LINES_UTILS.populate_display_qty';
2806                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
2807                    END IF;
2808                    RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2809                 END IF;
2810 
2811            IF (l_source_appr_rev_plan_flag = 'N' AND l_source_plan_in_mc_flag = 'Y') and l_target_appr_rev_plan_flag = 'Y' THEN
2812 
2813                pa_res_asg_currency_pub.maintain_data
2814                (p_fp_cols_rec        => l_fp_cols_rec,
2815                 p_calling_module     => 'COPY_PLAN',
2816                 p_rollup_flag        => 'Y',
2817                 p_version_level_flag => 'Y',
2818                 x_return_status      => l_return_status,
2819                 x_msg_data           => l_msg_count,
2820                 x_msg_count          => l_msg_data);
2821 
2822 
2823                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
2824                        IF P_PA_debug_mode = 'Y' THEN
2825                           pa_debug.g_err_stage:= 'Error in PA_RES_ASG_CURRENCY_PUB.MAINTAIN_DATA';
2826                           pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
2827                        END IF;
2828                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2829                     END IF;
2830 
2831            ELSIF (l_adj_percentage = 0 AND l_src_version_type = l_target_version_type) THEN
2832 
2833                pa_res_asg_currency_pub.maintain_data
2834                (p_fp_cols_rec        => l_fp_cols_rec,
2835                 p_calling_module     => 'COPY_PLAN',
2836                 p_copy_flag          => 'Y',
2837                 p_src_version_id     => p_source_version_id,
2838                 p_copy_mode          => 'COPY_ALL',
2839                 p_version_level_flag => 'Y',
2840                 x_return_status      => l_return_status,
2841                 x_msg_data           => l_msg_count,
2842                 x_msg_count          => l_msg_data);
2843 
2844            ELSE
2845 
2846                -- Copy only the overrides
2847                pa_res_asg_currency_pub.maintain_data
2848                (p_fp_cols_rec        => l_fp_cols_rec,
2849                 p_calling_module     => 'COPY_PLAN',
2850                 p_copy_flag          => 'Y',
2851                 p_copy_mode          => 'COPY_OVERRIDES',
2852                 p_src_version_id     => p_source_version_id,
2853                 p_version_level_flag => 'Y',
2854                 x_return_status      => l_return_status,
2855                 x_msg_data           => l_msg_data,
2856                 x_msg_count          => l_msg_count);
2857 
2858                 IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
2859                    IF P_PA_debug_mode = 'Y' THEN
2860                       pa_debug.g_err_stage:= 'Error in PA_RES_ASG_CURRENCY_PUB.MAINTAIN_DATA - Copy Overrides';
2861                       pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
2862                    END IF;
2863                    RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2864                 END IF;
2865 
2866                -- Does the rollup from budget lines
2867                 pa_res_asg_currency_pub.maintain_data
2868                 (p_fp_cols_rec        => l_fp_cols_rec,
2869                 p_calling_module     => 'COPY_PLAN',
2870                 p_rollup_flag        => 'Y',
2871                 p_src_version_id     => p_source_version_id,
2872                 p_version_level_flag => 'Y',
2873                 x_return_status      => l_return_status,
2874                 x_msg_data           => l_msg_data,
2875                 x_msg_count          => l_msg_count);
2876 
2877                 IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
2878                    IF P_PA_debug_mode = 'Y' THEN
2879                         pa_debug.g_err_stage:= 'Error in PA_RES_ASG_CURRENCY_PUB.MAINTAIN_DATA - Rollup';
2880                         pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
2881                    END IF;
2882                    RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2883                 END IF;
2884            END IF;
2885 
2886        END IF;  --p_calling_module <> PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST
2887 
2888        IF NOT (l_adj_percentage = 0 AND
2889            ( l_derv_rates_missing_amts_flag='N' OR
2890             l_source_fp_preference_code = l_target_fp_preference_code))THEN
2891            --Calling rollup_budget_versions.This api rolls up resource assignments,
2892            --creates proj denorm at entered level, creates rollup proj denorm and
2893            --updates budget version with summed up values
2894 
2895            IF P_PA_DEBUG_MODE = 'Y' THEN
2896                pa_debug.g_err_stage := 'Calling rollup_budget_versions api - 1st rollup';
2897                pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2898            END IF;
2899 
2900            PA_FP_ROLLUP_PKG.rollup_budget_version (
2901                          p_budget_version_id   => l_budget_version_id
2902                          ,p_entire_version     => 'Y'
2903                          ,x_return_status      => l_return_status
2904                          ,x_msg_count          => l_msg_count
2905                          ,x_msg_data           => l_msg_data );
2906    /* Bug 5099353 Start */
2907        ELSIF p_calling_module <> PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST then
2908 
2909            open is_eligible_for_rollup(p_project_id,l_source_proj_fp_options_id);
2910            fetch is_eligible_for_rollup into l_is_eligible_for_rollup;
2911            close is_eligible_for_rollup;
2912 
2913            If l_is_eligible_for_rollup = 'Y' then
2914                open chk_tgt_ver_status;
2915                fetch chk_tgt_ver_status into l_chk_tgt_ver_status;
2916                close chk_tgt_ver_status;
2917                If l_chk_tgt_ver_status = 'Y' then
2918 
2919                          IF P_PA_DEBUG_MODE = 'Y' THEN
2920                              pa_debug.g_err_stage := 'Calling rollup_budget_versions api - 2nd rollup';
2921                              pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
2922                          END IF;
2923 
2924 
2925            PA_FP_ROLLUP_PKG.rollup_budget_version (
2926                        p_budget_version_id   => l_budget_version_id
2927                        ,p_entire_version     => 'Y'
2928                        ,x_return_status      => l_return_status
2929                        ,x_msg_count          => l_msg_count
2930                        ,x_msg_data           => l_msg_data );
2931               End if;
2932            End if;
2933   /* Bug 5099353 End */
2934        END IF;
2935      --IPM Arch Enhancement - End - Bug 4865563
2936 
2937 
2938 
2939     -- Bug 3362316, 08-JAN-2003: Populate Reporting Lines Entity  --------------------------
2940 
2941 
2942     IF (p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN)
2943     THEN
2944         IF p_copy_mode = 'B' THEN
2945 
2946           /* We want to handle the reporting lines integration for baseline case
2947              in baseline api. Partly because start/end dates of budget lines could
2948              change during baseline and also for the reason that all api calls
2949              pertaining to baseline are in one api */
2950              null;
2951 
2952         ELSIF p_copy_mode = 'W' THEN
2953 
2954             -- bug 3731925 Check if rbs version id for the target version needs to be
2955             -- updated with plan type level value
2956             Select rbs_version_id
2957             into   l_target_pt_lvl_rbs_version_id
2958             from   pa_proj_fp_options opt
2959             where  opt.project_id = p_project_id
2960             and    opt.fin_plan_type_id = l_target_fin_plan_type_id
2961             and    opt.fin_plan_option_level_code = 'PLAN_TYPE';
2962 
2963             If nvl(l_source_ver_rbs_version_id,-99) <> nvl(l_target_pt_lvl_rbs_version_id,-99) THEN
2964                 -- rbs needs to be updated with parent plan type level record
2965                 -- do not copy summarization data
2966                 pa_fp_planning_transaction_pub.Refresh_rbs_for_versions(
2967                    p_project_id           => p_project_id
2968                   ,p_fin_plan_type_id     => l_target_fin_plan_type_id
2969                   ,p_calling_context      => 'SINGLE_VERSION'
2970                   ,p_budget_version_id    => l_budget_version_id
2971                   ,x_return_status        => l_return_status
2972                   ,x_msg_count            => l_msg_count
2973                   ,x_msg_data             => l_msg_data);
2974 
2975                IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2976                    IF l_debug_mode = 'Y' THEN
2977                         pa_debug.g_err_stage:='Called API Refresh_rbs_for_versions returned error';
2978                         pa_debug.write('Refresh_Plan_Txns:  ' || l_module_name,pa_debug.g_err_stage,5);
2979                    END IF;
2980                    RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
2981                END IF;
2982 
2983 
2984             ELSE   -- bug 3731925 rbs version is same so summarization data can be copied
2985 
2986                IF p_pji_rollup_required = 'Y' THEN --for Bug 4200168
2987                     /* 3156057: If source plan is mc enabled but not appr rev and the target is appr rev,
2988                      PJI copy api should not be called and PJI CREATE api should be called as is it could
2989                      be that PJI team is doing currency level rollups and the copying from source version
2990                      which had multiple currecnies would cause data integriy issues
2991                      (target AR version would show data in MC in view plan pages). */
2992 
2993                      --Bug 4290043. If amounts/rates are derived in the target version then PJI lines should
2994                      --not be copied from source and they should be created by looking at budget lines of target
2995                      IF (l_source_appr_rev_plan_flag = 'N' AND
2996                          l_source_plan_in_mc_flag = 'Y' AND
2997                          l_target_appr_rev_plan_flag = 'Y') OR
2998              l_source_fp_preference_code <> l_target_fp_preference_code OR --Added for bug 42344402
2999                          l_adj_percentage <> 0  OR  -- Bug 4085235: Added this condition to call plan_create
3000                          l_derv_rates_missing_amts_flag = 'Y' THEN
3001 
3002                        IF P_PA_DEBUG_MODE = 'Y' THEN
3003                           pa_debug.g_err_stage := 'Calling PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE';
3004                           pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
3005                        END IF;
3006 
3007                        l_dest_ver_id_tbl.extend(1);
3008                        l_dest_ver_id_tbl(1)   := l_budget_version_id;
3009 
3010                        --Bug 3969851. Plan_delete should be called before calling plan_create
3011                        PJI_FM_XBS_ACCUM_MAINT.PLAN_DELETE (
3012                           p_fp_version_ids   => l_dest_ver_id_tbl,
3013                           x_return_status    => l_return_status,
3014                           x_msg_code         => l_error_msg_code);
3015 
3016                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
3017                            IF P_PA_DEBUG_MODE = 'Y' THEN
3018                               pa_debug.g_err_stage := 'API PJI_FM_XBS_ACCUM_MAINT.PLAN_DELETE returned ERROR 1 '|| l_error_msg_code;
3019                               pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,5);
3020                            END IF;
3021 
3022                            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3023 
3024                        END IF;
3025 
3026 
3027                        PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE(p_fp_version_ids => l_dest_ver_id_tbl
3028                                                         , x_return_status => l_return_status
3029                                                         , x_msg_code => l_error_msg_code);
3030 
3031                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3032                                    RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3033                        END IF;
3034 
3035                      ELSE
3036 
3037                        l_source_ver_id_tbl.extend(1);
3038                        l_dest_ver_id_tbl.extend(1);
3039                        l_source_ver_type_tbl.extend(1);
3040                        l_dest_ver_type_tbl.extend(1);
3041 
3042                        l_source_ver_id_tbl(1) := p_source_version_id;
3043 
3044                        l_dest_ver_id_tbl(1)   := l_budget_version_id;
3045 
3046                        -- Fetch source IN-parameter values
3047 
3048                        BEGIN
3049 
3050                          SELECT decode(budget_status_code,'S','W',budget_status_code)
3051                          INTO   l_source_ver_type_tbl(1)
3052                          FROM   PA_BUDGET_VERSIONS
3053                          WHERE  budget_version_id = p_source_version_id;
3054                         EXCEPTION
3055                              WHEN NO_DATA_FOUND THEN
3056                                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3057                        END;
3058 
3059 
3060                        BEGIN
3061 
3062                          SELECT budget_status_code
3063                          INTO   l_dest_ver_type_tbl(1) /* This should always be W since we are inside "IF p_copy_mode = W" */
3064                          FROM   PA_BUDGET_VERSIONS
3065                          WHERE  budget_version_id = l_budget_version_id;
3066                         EXCEPTION
3067                              WHEN NO_DATA_FOUND THEN
3068                                   RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3069                        END;
3070 
3071                        IF P_PA_DEBUG_MODE = 'Y' THEN
3072                            pa_debug.g_err_stage := 'Calling PJI_FM_XBS_ACCUM_MAINT.FINPLAN_COPY';
3073                            pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
3074                        END IF;
3075 
3076 
3077                        PJI_FM_XBS_ACCUM_MAINT.FINPLAN_COPY(
3078                               p_source_fp_version_ids => l_source_ver_id_tbl
3079                               , p_dest_fp_version_ids => l_dest_ver_id_tbl
3080                               , p_source_fp_version_types => l_source_ver_type_tbl
3081                               , p_dest_fp_version_types => l_dest_ver_type_tbl
3082                               , x_return_status => l_return_status
3083                               , x_msg_code => l_error_msg_code);
3084 
3085                        -- Dev Note: Most of the other FP api calls were NOT followed by error and rollback
3086                        --           processing logic. However, in other high-level procedures in this
3087                        --           package, calls to Reporting Lines apis are followed by error and
3088                        --           rollback conditional logic.
3089                        --
3090                        --           Confirmed strategy with Sanjay Sarma, 19-MAR-2004.
3091                        --
3092 
3093                        IF l_return_status <> FND_API.G_RET_STS_SUCCESS
3094                          THEN
3095 
3096                           PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA'
3097                                                , p_msg_name            => l_error_msg_code);
3098 
3099                           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3100                        END IF;
3101 
3102                      END IF;
3103                END IF;     --  IF p_pji_rollup_required = 'Y' THEN --for Bug 4200168
3104             END If; -- If l_source_ver_rbs_version_id <> l_target_pt_lvl_rbs_version_id
3105         END IF; -- p_copy_mode
3106 
3107 
3108       -- End, Bug 3362316, 08-JAN-2003: Populate Reporting Lines Entity  ----------------------
3109      END IF;-- p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_FIN_PLAN)
3110 
3111     --IF p_calling_module is PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST then call create_org_fcst_elements to insert into
3112     --pa_fp_adj_elements,pa_fin_plan_adj_lines,pa_org_fcst_elements,pa_org_forecast_lines.
3113 
3114     IF p_calling_module = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST THEN
3115 
3116           IF P_PA_DEBUG_MODE = 'Y' THEN
3117              pa_debug.g_err_stage := 'Calling create_org_fcst_elements api';
3118              pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
3119           END IF;
3120 
3121           PA_FIN_PLAN_PUB.create_org_fcst_elements (
3122                   p_project_id          => p_project_id,
3123                   p_source_version_id   => p_source_version_id,
3124                   p_target_version_id   => l_budget_version_id,
3125                   x_return_status       => l_return_status,
3126                   x_msg_count           => l_msg_count,
3127                   x_msg_data            => l_msg_data );
3128     END IF;
3129 
3130     px_target_version_id := l_budget_version_id;
3131 
3132 
3133     IF P_PA_DEBUG_MODE = 'Y' THEN
3134         pa_debug.g_err_stage := 'Exiting Copy_Version';
3135         pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,3);
3136     END IF;
3137 
3138     pa_debug.reset_err_stack;
3139 
3140  EXCEPTION
3141    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
3142         ROLLBACK TO PA_FIN_PLAN_PUB_COPY_VERSION;
3143         -- Bug Fix: 4569365. Removed MRC code.
3144         -- PA_MRC_FINPLAN.G_CALLING_MODULE := Null;
3145         l_msg_count := FND_MSG_PUB.count_msg;
3146         IF l_msg_count = 1 THEN
3147              PA_INTERFACE_UTILS_PUB.get_messages
3148                    (p_encoded        => FND_API.G_TRUE
3149                     ,p_msg_index      => 1
3150                     ,p_msg_count      => l_msg_count
3151                     ,p_msg_data       => l_msg_data
3152                     ,p_data           => l_data
3153                     ,p_msg_index_out  => l_msg_index_out);
3154 
3155              x_msg_data := l_data;
3156              x_msg_count := l_msg_count;
3157         ELSE
3158             x_msg_count := l_msg_count;
3159         END IF;
3160 
3161         pa_debug.g_err_stage:='Invalid Arguments Passed';
3162         IF P_PA_DEBUG_MODE = 'Y' THEN
3163            pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,5);
3164         END IF;
3165 
3166         x_return_status:= FND_API.G_RET_STS_ERROR;
3167         pa_debug.reset_err_stack;
3168 
3169    WHEN Others THEN
3170         ROLLBACK TO PA_FIN_PLAN_PUB_COPY_VERSION;
3171         -- Bug Fix: 4569365. Removed MRC code.
3172         -- PA_MRC_FINPLAN.G_CALLING_MODULE := Null;
3173         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3174         x_msg_count     := 1;
3175         x_msg_data      := SQLERRM;
3176         FND_MSG_PUB.add_exc_msg( p_pkg_name       => 'PA_FIN_PLAN_PUB'
3177                                 ,p_procedure_name => 'COPY_VERSION');
3178         pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
3179         IF P_PA_DEBUG_MODE = 'Y' THEN
3180            pa_debug.write('Copy_Version: ' || l_module_name,pa_debug.g_err_stage,5);
3181         END IF;
3182         pa_debug.reset_err_stack;
3183         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3184 END Copy_Version;
3185 
3186 /* ------------------------------------------------------------------------- */
3187 
3188 procedure Baseline
3189     (p_project_id                   IN  pa_budget_versions.project_id%TYPE,
3190      p_budget_version_id            IN  pa_budget_versions.budget_version_id%TYPE,
3191      p_record_version_number        IN  pa_budget_versions.record_version_number%TYPE,
3192      p_orig_budget_version_id       IN  pa_budget_versions.budget_version_id%TYPE,
3193      p_orig_record_version_number   IN  pa_budget_versions.record_version_number%TYPE,
3194      x_fc_version_created_flag      OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3195      x_return_status                OUT NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3196      x_msg_count                    OUT NOCOPY NUMBER, --File.Sql.39 bug 4440895
3197      x_msg_data                     OUT NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
3198 is
3199 /* Bug# 2661650 - _vl to _b/_tl for performance changes */
3200 l_fin_plan_type_code    pa_fin_plan_types_b.fin_plan_type_code%TYPE;
3201 l_debug_mode      VARCHAR2(30);
3202 l_valid1_flag     VARCHAR2(1);
3203 l_valid2_flag     VARCHAR2(1);
3204 l_msg_count       NUMBER := 0;
3205 l_data            VARCHAR2(2000);
3206 l_msg_data        VARCHAR2(2000);
3207 l_error_msg_code  VARCHAR2(30);
3208 l_return_status   VARCHAR2(2000);
3209 l_msg_index_out   NUMBER;
3210 
3211 l_created_by        pa_budget_versions.created_by%TYPE;
3212 l_emp_id            NUMBER;
3213 
3214 --The following varible is added to make program consistent with the
3215 --changed copy_version procedure prototype
3216 
3217 l_target_version_id PA_BUDGET_VERSIONS.budget_version_id%TYPE;
3218 --Bug 4145705
3219 l_version_type               pa_budget_versions.version_type%TYPE;
3220 l_fin_plan_type_id           pa_budget_versions.fin_plan_type_id%TYPE;
3221 l_orig_budget_version_id     pa_budget_versions.budget_version_id%TYPE;
3222 l_orig_record_version_number pa_budget_versions.record_Version_number%TYPE;
3223 l_temp                       NUMBER;
3224 
3225 begin
3226     FND_MSG_PUB.initialize;
3227     IF P_PA_DEBUG_MODE = 'Y' THEN
3228        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Baseline');
3229     END IF;
3230     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
3231     l_debug_mode := NVL(l_debug_mode, 'Y');
3232     IF P_PA_DEBUG_MODE = 'Y' THEN
3233        pa_debug.set_process('Baseline: ' || 'PLSQL','LOG',l_debug_mode);
3234     END IF;
3235     x_msg_count := 0;
3236 /* CHECK FOR BUSINESS RULES VIOLATIONS */
3237     /* check for null budget_version_id */
3238     if p_budget_version_id is NULL then
3239         IF P_PA_DEBUG_MODE = 'Y' THEN
3240            pa_debug.write_file('Baseline: ' || 'BUSINESS RULE VIOLATION: p_budget_version_id is null');
3241         END IF;
3242         x_return_status := FND_API.G_RET_STS_ERROR;
3243         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3244                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
3245     end if;
3246     /* check to see if the current user is an EMPLOYEE; ONLY EMPLOYEES CAN BASELINE */
3247     l_created_by:=FND_GLOBAL.user_id;
3248     IF P_PA_DEBUG_MODE = 'Y' THEN
3249        pa_debug.write_file('Baseline: ' || 'created by= ' || l_created_by);
3250     END IF;
3251     l_emp_id := PA_UTILS.GetEmpIdFromUser(l_created_by);
3252     IF P_PA_DEBUG_MODE = 'Y' THEN
3253        pa_debug.write_file('Baseline: ' || 'employee id= ' || l_emp_id);
3254     END IF;
3255     if l_emp_id IS NULL then
3256         IF P_PA_DEBUG_MODE = 'Y' THEN
3257            pa_debug.write_file('Baseline: ' || 'BUSINESS RULE VIOLATION: l_emp_id is NULL');
3258         END IF;
3259         x_return_status := FND_API.G_RET_STS_ERROR;
3260         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3261                              p_msg_name            => 'PA_ALL_WARN_NO_EMPL_REC');
3262     end if;
3263     /* check to see if the budget version we're setting to be current baselined has */
3264     /* been updated by someone else already */
3265     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
3266             (p_unique_index             => p_budget_version_id,
3267              p_record_version_number    => p_record_version_number,
3268              x_valid_flag               => l_valid1_flag,
3269              x_return_status            => l_return_status,
3270              x_error_msg_code           => l_error_msg_code);
3271     /* check to see if the old current baselined budget version has */
3272     /* been updated by someone else already */
3273     /* if p_orig_budget_version_id = null then there is currently not a baselined version */
3274     /* in this case, ignore this check
3275     if p_orig_budget_version_id <> null then */
3276     if p_orig_budget_version_id is not null then
3277         PA_FIN_PLAN_UTILS.Check_Record_Version_Number
3278             (p_unique_index             => p_orig_budget_version_id,
3279              p_record_version_number    => p_orig_record_version_number,
3280              x_valid_flag               => l_valid2_flag,
3281              x_return_status            => l_return_status,
3282              x_error_msg_code           => l_error_msg_code);
3283         if not((l_valid1_flag='Y') and (l_valid2_flag='Y')) then
3284             IF P_PA_DEBUG_MODE = 'Y' THEN
3285                pa_debug.write_file('Baseline: ' || 'BUSINESS RULE VIOLATION: Check_Record_Version_Number failed');
3286             END IF;
3287             x_return_status := FND_API.G_RET_STS_ERROR;
3288             PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3289                                  p_msg_name            => l_error_msg_code);
3290         end if;
3291     end if;
3292 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
3293     l_msg_count := FND_MSG_PUB.count_msg;
3294     if l_msg_count > 0 then
3295         if l_msg_count = 1 then
3296              PA_INTERFACE_UTILS_PUB.get_messages
3297                  (p_encoded        => FND_API.G_TRUE,
3298                   p_msg_index      => 1,
3299                   p_msg_count      => l_msg_count,
3300                   p_msg_data       => l_msg_data,
3301                   p_data           => l_data,
3302                   p_msg_index_out  => l_msg_index_out);
3303              x_msg_data := l_data;
3304              x_msg_count := l_msg_count;
3305             else
3306              x_msg_count := l_msg_count;
3307         end if;
3308             pa_debug.reset_err_stack;
3309             return;
3310     end if;
3311 
3312 /* If There are NO Business Rules Violations , Then proceed with Baseline */
3313     IF P_PA_DEBUG_MODE = 'Y' THEN
3314        pa_debug.write_file('no business rules violations; proceeding with baseline');
3315     END IF;
3316     SAVEPOINT PA_FIN_PLAN_PUB_BASELINE;
3317 
3318     /* FINPLANNING PATCHSET K: If the plan type is not ORG_FORECAST, then call
3319        pa_fin_plan_pvt.Submit_Current_Working_FinPlan
3320      */
3321     --Bug 4145705.Selected version type and fin plan type id
3322     select pt.fin_plan_type_code,
3323            pbv.version_type,
3324            pbv.fin_plan_type_id
3325       into l_fin_plan_type_code,
3326            l_version_type,
3327            l_fin_plan_type_id
3328       from pa_budget_versions pbv,
3329            pa_fin_plan_types_b pt  /* Bug# 2661650 - _vl to _b/_tl for performance changes */
3330       where pbv.budget_version_id = p_budget_version_id and
3331             pbv.fin_plan_type_id = pt.fin_plan_type_id ;
3332 
3333 
3334     if l_fin_plan_type_code = 'ORG_FORECAST' then
3335 
3336       /* set the status_code back to "Working" from "Submitted" for the version we baseline */
3337       update
3338         pa_budget_versions
3339       set
3340         last_update_date = SYSDATE,
3341         last_updated_by = FND_GLOBAL.user_id,
3342         last_update_login = FND_GLOBAL.login_id,
3343         budget_status_code = 'W',
3344         record_version_number = record_version_number+1
3345       where
3346         budget_version_id = p_budget_version_id;
3347       /* remove Current Baselined status from current baselined version */
3348       update
3349           pa_budget_versions
3350       set
3351         last_update_date = SYSDATE,
3352         last_updated_by = FND_GLOBAL.user_id,
3353         last_update_login = FND_GLOBAL.login_id,
3354         current_flag = 'N',
3355         record_version_number = record_version_number+1
3356       where
3357         budget_version_id=p_orig_budget_version_id;
3358       IF P_PA_DEBUG_MODE = 'Y' THEN
3359          pa_debug.write_file('the old baselined version is no longer the current baselined version');
3360       END IF;
3361       /* create a copy, labeled as 'BASELINED' */
3362       PA_FIN_PLAN_PUB.Copy_Version
3363         (p_project_id           => p_project_id,
3364          p_source_version_id    => p_budget_version_id,
3365          p_copy_mode            => 'B',
3366          px_target_version_id   => l_target_version_id,
3367                   --added to make the call consistent with new extension
3368          x_return_status        => l_return_status,
3369          x_msg_count            => l_msg_count,
3370          x_msg_data             => l_msg_data);
3371       /* PA_FIN_PLAN_PUB.Copy_Version may have generated errors */
3372       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3373           rollback to PA_FIN_PLAN_PUB_BASELINE;
3374           if l_msg_count = 1 then
3375                PA_INTERFACE_UTILS_PUB.get_messages
3376                    (p_encoded        => FND_API.G_TRUE,
3377                     p_msg_index      => 1,
3378                     p_msg_count      => l_msg_count,
3379                     p_msg_data       => l_msg_data,
3380                     p_data           => l_data,
3381                     p_msg_index_out  => l_msg_index_out);
3382                x_msg_data := l_data;
3383                x_msg_count := l_msg_count;
3384               else
3385                x_msg_count := l_msg_count;
3386           end if;
3387         raise pa_fin_plan_pub.rollback_on_error;
3388       end if;
3389     else
3390 
3391       --Bug 4145705. The following block has been added to address the issue where in multiple current baselined versions
3392       --were getting created. This code on detecting such cases will throw an error
3393       BEGIN
3394 
3395           SELECT budget_version_id,
3396                  record_version_number
3397           INTO   l_orig_budget_version_id,
3398                  l_orig_record_version_number
3399           FROM   pa_budget_versions
3400           WHERE  project_id=p_project_id
3401           AND    fin_plan_type_id=l_fin_plan_type_id
3402           AND    version_type=l_version_type
3403           AND    current_flag='Y';
3404 
3405       EXCEPTION
3406       WHEN NO_DATA_FOUND THEN
3407         l_orig_budget_version_id:=NULL;
3408         l_orig_record_version_number:=NULL;
3409 
3410       WHEN TOO_MANY_ROWS THEN
3411 
3412           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
3413                 p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
3414                 p_token1         => 'PROCEDURENAME',
3415                 p_value1         => 'PAFPPUBB.Baseline',
3416                 p_token2         => 'STAGE',
3417                 p_value2         => 'l_orig_budget_version_id IS '||l_orig_budget_version_id
3418                                     ||' AND p_orig_budget_version_id IS '||p_orig_budget_version_id );
3419 
3420           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3421 
3422       WHEN OTHERS THEN
3423 
3424           IF P_PA_DEBUG_MODE = 'Y' THEN
3425                   pa_debug.g_err_stage := 'Unexpected error while deriving l_orig_budget_version_id '||sqlerrm;
3426                   pa_debug.write('PAFPPUBB.Baseline',pa_debug.g_err_stage,5);
3427           END IF;
3428           RAISE;
3429       END;
3430 
3431       IF NVL(l_orig_budget_version_id,-99) <>  NVL(p_orig_budget_version_id,NVL(l_orig_budget_version_id,-99)) THEN
3432 
3433           IF P_PA_DEBUG_MODE = 'Y' THEN
3434                   pa_debug.g_err_stage := 'l_orig_budget_version_id IS NOT SAME AS p_orig_budget_version_id' ;
3435                   pa_debug.write('PAFPPUBB.Baseline',pa_debug.g_err_stage,5);
3436 
3437                   pa_debug.g_err_stage := 'l_orig_budget_version_id '||l_orig_budget_version_id ;
3438                   pa_debug.write('PAFPPUBB.Baseline',pa_debug.g_err_stage,5);
3439 
3440                   pa_debug.g_err_stage := 'p_orig_budget_version_id '||p_orig_budget_version_id ;
3441                   pa_debug.write('PAFPPUBB.Baseline',pa_debug.g_err_stage,5);
3442 
3443           END IF;
3444           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
3445                 p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
3446                 p_token1         => 'PROCEDURENAME',
3447                 p_value1         => 'PAFPPUBB.Baseline',
3448                 p_token2         => 'STAGE',
3449                 p_value2         => 'l_orig_budget_version_id IS '||l_orig_budget_version_id
3450                                     ||' AND p_orig_budget_version_id IS '||p_orig_budget_version_id );
3451 
3452           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3453 
3454       ELSIF p_orig_budget_version_id IS NOT NULL THEN
3455 
3456         l_orig_budget_version_id:=p_orig_budget_version_id;
3457         l_orig_record_version_number:=p_orig_record_version_number;
3458 
3459       END IF;
3460       --End of block for bug 4145705
3461 
3462       -- call PA_FIN_PLAN_PVT.Baseline_FinPlan for NON ORG_FORECAST types
3463       PA_FIN_PLAN_PVT.Baseline_FinPlan
3464         (p_project_id                 => p_project_id,
3465          p_budget_version_id          => p_budget_version_id,
3466          p_record_version_number      => p_record_version_number,
3467          p_orig_budget_version_id     => l_orig_budget_version_id,
3468          p_orig_record_version_number => l_orig_record_version_number,
3469          p_verify_budget_rules        => 'Y',
3470          x_fc_version_created_flag    => x_fc_version_created_flag,
3471          x_return_status              => l_return_status,
3472          x_msg_count                  => l_msg_count,
3473          x_msg_data                   => l_msg_data);
3474       if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3475         -- PA_FIN_PLAN_PVT.Baseline_FinPlan RETURNED ERRORS
3476         rollback to PA_FIN_PLAN_PUB_BASELINE;
3477         x_return_status := FND_API.G_RET_STS_ERROR;
3478         /*
3479            PA_INTERFACE_UTILS_PUB.get_messages
3480                  (p_encoded        => FND_API.G_TRUE,
3481                   p_msg_index      => 1,
3482                   p_data           => x_msg_data,
3483                   p_msg_index_out  => l_msg_index_out);
3484            x_msg_count := l_msg_count;
3485         else
3486            x_msg_count := l_msg_count;
3487         end if; */
3488         if l_msg_count = 1 then
3489                PA_INTERFACE_UTILS_PUB.get_messages
3490                    (p_encoded        => FND_API.G_TRUE,
3491                     p_msg_index      => 1,
3492                     p_msg_count      => l_msg_count,
3493                     p_msg_data       => l_msg_data,
3494                     p_data           => l_data,
3495                     p_msg_index_out  => l_msg_index_out);
3496                x_msg_data := l_data;
3497                x_msg_count := l_msg_count;
3498         else
3499                x_msg_count := l_msg_count;
3500         end if;
3501         pa_debug.reset_err_stack;
3502         return;
3503       end if;
3504       --Bug 4145705
3505       SELECT COUNT(*)
3506       INTO   l_temp
3507       FROM   pa_budget_versions
3508       WHERE  project_id=p_project_id
3509       AND    fin_plan_type_id=l_fin_plan_type_id
3510       AND    version_type=l_version_type
3511       AND    current_flag='Y';
3512       IF l_temp <> 1 THEN
3513 
3514           PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
3515                 p_msg_name       => 'PA_FP_INV_PARAM_PASSED',
3516                 p_token1         => 'PROCEDURENAME',
3517                 p_value1         => 'PAFPPUBB.Baseline',
3518                 p_token2         => 'STAGE',
3519                 p_value2         => 'No. of current baselined versions '||l_temp);
3520 
3521           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
3522 
3523       END IF;
3524 
3525     end if; -- l_fin_plan_type_code = ORG_FORECAST
3526 
3527     x_return_status := FND_API.G_RET_STS_SUCCESS;
3528     pa_debug.reset_err_stack;
3529 
3530 exception
3531    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
3532            l_msg_count := FND_MSG_PUB.count_msg;
3533              IF l_msg_count = 1 THEN
3534                 PA_INTERFACE_UTILS_PUB.get_messages
3535                      (p_encoded        => FND_API.G_TRUE
3536                       ,p_msg_index      => 1
3537                       ,p_msg_count      => l_msg_count
3538                       ,p_msg_data       => l_msg_data
3539                       ,p_data           => l_data
3540                       ,p_msg_index_out  => l_msg_index_out);
3541                 x_msg_data := l_data;
3542                 x_msg_count := l_msg_count;
3543              ELSE
3544                 x_msg_count := l_msg_count;
3545              END IF;
3546            ROLLBACK TO PA_FIN_PLAN_PUB_BASELINE;
3547            x_return_status := FND_API.G_RET_STS_ERROR;
3548            pa_debug.reset_curr_function;
3549     when pa_fin_plan_pub.rollback_on_error then
3550       IF P_PA_DEBUG_MODE = 'Y' THEN
3551          pa_debug.write_file('Procedure Baseline: rollback_on_error exception');
3552       END IF;
3553       rollback to PA_FIN_PLAN_PUB_BASELINE;
3554       raise FND_API.G_EXC_UNEXPECTED_ERROR;
3555 
3556     when others then
3557         rollback to PA_FIN_PLAN_PUB_BASELINE;
3558         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3559         x_msg_count     := 1;
3560         x_msg_data      := SQLERRM;
3561         FND_MSG_PUB.add_exc_msg(p_pkg_name         => 'PA_FIN_PLAN_PUB',
3562                                 p_procedure_name   => 'Baseline');
3563         pa_debug.reset_err_stack;
3564         raise FND_API.G_EXC_UNEXPECTED_ERROR;
3565 end Baseline;
3566 /* ------------------------------------------------------------------------- */
3567 
3568 procedure Create_Version_OrgFcst
3569     (p_project_id                   IN     pa_budget_versions.project_id%TYPE,
3570      p_fin_plan_type_id             IN     pa_budget_versions.fin_plan_type_id%TYPE,
3571      p_fin_plan_options_id          IN     pa_proj_fp_options.proj_fp_options_id%TYPE,
3572      p_version_name                 IN     pa_budget_versions.version_name%TYPE,
3573      p_description                  IN     pa_budget_versions.description%TYPE,
3574      p_resource_list_id             IN     pa_budget_versions.resource_list_id%TYPE,
3575      x_budget_version_id            OUT    NOCOPY pa_budget_versions.budget_version_id%TYPE, --File.Sql.39 bug 4440895
3576      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
3577      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
3578      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
3579 is
3580 
3581 /* error handling variables */
3582 l_debug_mode      VARCHAR2(30);
3583 l_msg_count       NUMBER := 0;
3584 l_data            VARCHAR2(2000);
3585 l_msg_data        VARCHAR2(2000);
3586 l_error_msg_code  VARCHAR2(2000);
3587 l_msg_index_out   NUMBER;
3588 l_return_status   VARCHAR2(2000);
3589 
3590 l_version_type    pa_budget_versions.version_type%TYPE;
3591 l_version_type_code     pa_fin_plan_types_b.fin_plan_type_code%TYPE;
3592 l_max_version     pa_budget_versions.version_number%TYPE;
3593 l_current_working_flag  pa_budget_versions.current_working_flag%TYPE;
3594 
3595 l_budget_version_id     pa_budget_versions.budget_version_id%TYPE;         /* newly-created budget_version_id */
3596 l_proj_fin_plan_options_id   pa_proj_fp_options.proj_fp_options_id%TYPE;
3597 l_row_id            ROWID;
3598 l_resource_list_id      pa_budget_versions.resource_list_id%TYPE;
3599 l_org_fcst_period_type  pa_forecasting_options_all.org_fcst_period_type%TYPE;
3600 l_org_time_phased_code   pa_proj_fp_options.all_time_phased_code%TYPE;
3601 l_org_amount_set_id      pa_fin_plan_amount_sets.fin_plan_amount_set_id%TYPE;
3602 l_org_structure_version_id pa_implementations_all.org_structure_version_id%TYPE;
3603 l_period_set_name       pa_implementations_all.period_set_name%TYPE;
3604 l_act_period_type       gl_periods.period_type%TYPE;
3605 l_org_projfunc_currency_code    gl_sets_of_books.currency_code%TYPE;
3606 l_number_of_periods     pa_forecasting_options_all.number_of_periods%TYPE;
3607 l_request_id            pa_budget_versions.request_id%TYPE;
3608 l_weighted_or_full_code pa_forecasting_options_all.weighted_or_full_code%TYPE;
3609 l_fcst_start_date       pa_proj_fp_options.fin_plan_start_date%TYPE;
3610 l_fcst_end_date         pa_proj_fp_options.fin_plan_end_date%TYPE;
3611 l_org_project_template_id   pa_forecasting_options_all.org_fcst_project_template_id%TYPE;
3612 l_org_id                pa_forecasting_options_all.org_id%TYPE;
3613 l_period_profile_id     pa_proj_period_profiles.period_profile_id%TYPE;
3614 l_ppp_start_date        DATE;
3615 l_ppp_end_date          DATE;
3616 l_pa_period_type        pa_implementations_all.pa_period_type%TYPE;
3617 
3618 cursor amount_set_csr is
3619 select
3620     fin_plan_amount_set_id
3621 from
3622     pa_fin_plan_amount_sets
3623 where
3624     amount_set_type_code = 'ALL';
3625 amount_set_rec amount_set_csr%ROWTYPE;
3626 
3627 cursor plan_options_csr is
3628 select
3629     proj_fp_options_id
3630 from
3631     pa_proj_fp_options
3632 where
3633     project_id=p_project_id and
3634     fin_plan_type_id=p_fin_plan_type_id and
3635     fin_plan_option_level_code = 'PLAN_TYPE';
3636 plan_options_rec plan_options_csr%ROWTYPE;
3637 
3638 begin
3639     FND_MSG_PUB.initialize;
3640     IF P_PA_DEBUG_MODE = 'Y' THEN
3641        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Create_Version_OrgFcst');
3642     END IF;
3643     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
3644     l_debug_mode := NVL(l_debug_mode, 'Y');
3645     IF P_PA_DEBUG_MODE = 'Y' THEN
3646        pa_debug.set_process('Create_Version_OrgFcst: ' || 'PLSQL','LOG',l_debug_mode);
3647     END IF;
3648     x_msg_count := 0;
3649 /* CHECK FOR BUSINESS RULES VIOLATIONS */
3650     /* check for null version_name */
3651     IF P_PA_DEBUG_MODE = 'Y' THEN
3652        pa_debug.write_file('Create_Version_OrgFcst: ' || 'starting procedure: initial message count= ' || FND_MSG_PUB.count_msg);
3653     END IF;
3654     if p_version_name is NULL then
3655         x_return_status := FND_API.G_RET_STS_ERROR;
3656         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3657                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION_NAME');
3658     end if;
3659 
3660 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
3661     l_msg_count := FND_MSG_PUB.count_msg;
3662     if l_msg_count > 0 then
3663         if l_msg_count = 1 then
3664              PA_INTERFACE_UTILS_PUB.get_messages
3665                  (p_encoded        => FND_API.G_TRUE,
3666                   p_msg_index      => 1,
3667                   p_msg_count      => l_msg_count,
3668                   p_msg_data       => l_msg_data,
3669                   p_data           => l_data,
3670                   p_msg_index_out  => l_msg_index_out);
3671              x_msg_data := l_data;
3672              x_msg_count := l_msg_count;
3673             else
3674              x_msg_count := l_msg_count;
3675         end if;
3676             pa_debug.reset_err_stack;
3677             return;
3678     end if;
3679 
3680 /* If There are NO Business Rules Violations , Then proceed with Create Version Apply */
3681     if l_msg_count = 0 then
3682         IF P_PA_DEBUG_MODE = 'Y' THEN
3683            pa_debug.write_file('Create_Version_OrgFcst: ' || 'no business rules violations');
3684         END IF;
3685         SAVEPOINT PA_FP_PUB_CREATE_VER_ORGFCST;
3686         /* Get the version_type by querying pa_fin_plan_types_b using fin_plan_type_id */
3687         select
3688                 fin_plan_type_code,
3689                 name
3690             into
3691                 l_version_type_code,
3692                 l_version_type
3693             from
3694                 pa_fin_plan_types_vl
3695             where
3696                 fin_plan_type_id=p_fin_plan_type_id;
3697         if (l_version_type is null) then
3698             raise NO_DATA_FOUND;
3699         end if;
3700         IF P_PA_DEBUG_MODE = 'Y' THEN
3701            pa_debug.write_file('Create_Version_OrgFcst: ' || 'version type selected with no problems');
3702            pa_debug.write_file('Create_Version_OrgFcst: ' || 'version type code is ' || l_version_type_code);
3703            pa_debug.write_file('Create_Version_OrgFcst: ' || 'version type name is ' || l_version_type);
3704         END IF;
3705         /* Get the max version_number for working versions of this plan type */
3706         select
3707                 nvl(max(version_number), 0)
3708             into
3709                 l_max_version
3710             from
3711                 pa_budget_versions
3712             where
3713                 project_id = p_project_id and
3714                 fin_plan_type_id = p_fin_plan_type_id and
3715                 budget_status_code in ('W', 'S');
3716         IF P_PA_DEBUG_MODE = 'Y' THEN
3717            pa_debug.write_file('Create_Version_OrgFcst: ' || 'max version number is ' || l_max_version);
3718         END IF;
3719         /* Get the resource_list_id to be used; if it was not passed to this procedure, */
3720         /* we can retrieve it from FND_PROFILE if the version_type is ORG_FORECAST */
3721         if p_resource_list_id is NULL then
3722             IF P_PA_DEBUG_MODE = 'Y' THEN
3723                pa_debug.write_file('Create_Version_OrgFcst: ' || 'p_resource_list_id is null');
3724             END IF;
3725             if l_version_type_code = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST then
3726                 l_resource_list_id := FND_PROFILE.value('PA_FORECAST_RESOURCE_LIST');
3727             else
3728                 l_msg_count := l_msg_count + 1;
3729                 if x_msg_count = 1 then
3730                     PA_INTERFACE_UTILS_PUB.get_messages
3731                         (p_encoded        => FND_API.G_TRUE,
3732                          p_msg_index      => 1,
3733                          p_msg_count      => l_msg_count,
3734                          p_msg_data       => l_msg_data,
3735                          p_data           => l_data,
3736                          p_msg_index_out  => l_msg_index_out);
3737                          x_msg_data := l_data;
3738                          x_msg_count := l_msg_count;
3739                      else
3740                      x_msg_count := l_msg_count;
3741                  end if;
3742                  raise pa_fin_plan_pub.rollback_on_error;
3743              end if;
3744         else
3745             l_resource_list_id := p_resource_list_id;
3746         end if;
3747         IF P_PA_DEBUG_MODE = 'Y' THEN
3748            pa_debug.write_file('Create_Version_OrgFcst: ' || 'resource list is ' || l_resource_list_id);
3749         END IF;
3750         /* retrieve the period type, start date, and end date from pa_forecasting_options */
3751         pa_fp_org_fcst_utils.get_forecast_option_details
3752            (x_fcst_period_type      => l_org_fcst_period_type,
3753             x_period_set_name       => l_period_set_name,
3754             x_act_period_type       => l_act_period_type,
3755             x_org_projfunc_currency_code    => l_org_projfunc_currency_code,
3756             x_number_of_periods     => l_number_of_periods,
3757             x_weighted_or_full_code => l_weighted_or_full_code,
3758             x_org_proj_template_id  => l_org_project_template_id,
3759             x_org_structure_version_id => l_org_structure_version_id,
3760             x_fcst_start_date       => l_fcst_start_date,
3761             x_fcst_end_date         => l_fcst_end_date,
3762             x_org_id                => l_org_id,
3763             x_return_status         => l_return_status,
3764             x_err_code              => l_error_msg_code);
3765 /*
3766         IF P_PA_DEBUG_MODE = 'Y' THEN
3767            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_org_fcst_period_type is ' || l_org_fcst_period_type);
3768            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_period_set_name is ' || l_period_set_name);
3769            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_act_period_type is ' || l_act_period_type);
3770            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_org_projfunc_currency_code is ' || l_org_projfunc_currency_code);
3771            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_number_of_periods is ' || l_number_of_periods);
3772            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_org_project_template_id is ' || l_org_project_template_id);
3773            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_org_structure_version_id is ' || l_org_structure_version_id);
3774            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_org_id is ' || l_org_id);
3775            pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_return_status is ' || l_return_status);
3776         END IF;
3777 */
3778         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3779             IF P_PA_DEBUG_MODE = 'Y' THEN
3780                pa_debug.write_file('Create_Version_OrgFcst: ' || 'error with pa_fp_org_fcst_utils.get_forecast_option_details');
3781             END IF;
3782             PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3783                                  p_msg_name            => l_error_msg_code);
3784             raise pa_fin_plan_pub.rollback_on_error;
3785         end if;
3786 
3787         if l_org_fcst_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL then
3788             l_org_time_phased_code := PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G;
3789         elsif l_org_fcst_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA then
3790             l_org_time_phased_code := PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P;
3791         end if;
3792         /* GETTING PERIOD PROFILE ID: first check to see if we can find it; if not, then
3793          * we create one
3794          */
3795         pa_fp_org_fcst_utils.get_period_profile
3796             (p_project_id               => p_project_id,
3797              p_period_profile_type      => PA_FP_CONSTANTS_PKG.G_PD_PROFILE_FIN_PLANNING,
3798              p_plan_period_type         => l_org_fcst_period_type,
3799              p_period_set_name          => l_period_set_name,
3800              p_act_period_type          => l_act_period_type,
3801              p_start_date               => l_fcst_start_date,
3802              p_number_of_periods        => l_number_of_periods,
3803              x_period_profile_id        => l_period_profile_id,
3804              x_return_status            => l_return_status,
3805              x_err_code                 => l_error_msg_code);
3806         /* create a new PERIOD PROFILE ID if one does not exist */
3807         if l_period_profile_id < 0 then
3808             IF P_PA_DEBUG_MODE = 'Y' THEN
3809                pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_period_profile_id < 0');
3810             END IF;
3811             l_period_profile_id := NULL;
3812             if l_org_fcst_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA then
3813                 l_pa_period_type := l_act_period_type;
3814             else
3815                 l_pa_period_type := NULL;
3816             end if;
3817 /*
3818             IF P_PA_DEBUG_MODE = 'Y' THEN
3819                pa_debug.write_file('Create_Version_OrgFcst: ' || 'entering pa_prj_period_profile_utils.maintain_prj_period_profile');
3820                pa_debug.write_file('Create_Version_OrgFcst: ' || 'project id is ' || p_project_id);
3821                pa_debug.write_file('Create_Version_OrgFcst: ' || 'p_plan_period_type is ' || l_org_fcst_period_type);
3822                pa_debug.write_file('Create_Version_OrgFcst: ' || 'p_period_set_name is ' || l_period_set_name);
3823                pa_debug.write_file('Create_Version_OrgFcst: ' || 'p_gl_period_type is ' || l_act_period_type);
3824                pa_debug.write_file('Create_Version_OrgFcst: ' || 'p_pa_period_type is ' || l_pa_period_type);
3825                pa_debug.write_file('Create_Version_OrgFcst: ' || 'p_start_date is ' || l_fcst_start_date);
3826                pa_debug.write_file('Create_Version_OrgFcst: ' || 'px_end_date is ' || l_fcst_end_date);
3827                pa_debug.write_file('Create_Version_OrgFcst: ' || 'px_period_profile_id is ' || l_period_profile_id);
3828                pa_debug.write_file('Create_Version_OrgFcst: ' || 'px_number_of_periods is ' || l_number_of_periods);
3829             END IF;
3830 */
3831             pa_prj_period_profile_utils.maintain_prj_period_profile
3832                 (p_project_id               => p_project_id,
3833                  p_period_profile_type      => PA_FP_CONSTANTS_PKG.G_PD_PROFILE_FIN_PLANNING,
3834                  p_plan_period_type         => l_org_fcst_period_type,
3835                  p_period_set_name          => l_period_set_name,
3836                  p_gl_period_type           => l_act_period_type,
3837                  p_pa_period_type           => l_pa_period_type,
3838                  p_start_date               => l_fcst_start_date,
3839                  px_end_date                => l_fcst_end_date,
3840                  px_period_profile_id       => l_period_profile_id,
3841                  p_commit_flag              => 'N',
3842                  px_number_of_periods       => l_number_of_periods,
3843                  x_plan_start_date          => l_ppp_start_date,
3844                  x_plan_end_date            => l_ppp_end_date,
3845                  x_return_status            => l_return_status,
3846                  x_msg_count                => l_msg_count,
3847                  x_msg_data                 => l_msg_data);
3848         end if;
3849         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3850             IF P_PA_DEBUG_MODE = 'Y' THEN
3851                pa_debug.write_file('Create_Version_OrgFcst: ' || 'error with pa_prj_period_profile_utils.maintain_prj_period_profile');
3852             END IF;
3853             PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
3854                                  p_msg_name            => l_msg_data);
3855             raise pa_fin_plan_pub.rollback_on_error;
3856         end if;
3857         IF P_PA_DEBUG_MODE = 'Y' THEN
3858            pa_debug.write_file('Create_Version_OrgFcst: ' || ' the new period profile id is ' || l_period_profile_id);
3859         END IF;
3860         /* create the plan version */
3861         IF P_PA_DEBUG_MODE = 'Y' THEN
3862            pa_debug.write_file('Create_Version_OrgFcst: ' || 'calling pa_fp_budget_versions_pkg.Insert_Row to create a budget version');
3863         END IF;
3864         select pa_budget_versions_s.nextVal into l_budget_version_id from dual;
3865         /* the new version is the current working only if it's the ONLY working version */
3866         if (l_max_version = 0) then
3867             l_current_working_flag := 'Y';
3868         else
3869             l_current_working_flag := 'N';
3870         end if;
3871         pa_fp_budget_versions_pkg.Insert_Row
3872            (px_budget_version_id        => l_budget_version_id,  /* unique budget_version_id for new version */
3873             p_project_id                => p_project_id,          /* the ID of the project */
3874             p_budget_type_code          => NULL,
3875             p_version_number            => l_max_version+1,      /* version_number incremented */
3876             p_budget_status_code        => 'W',                  /* 'Working' version */
3877             p_current_flag              => 'N',                  /* 'Working' version */
3878             p_original_flag             => 'N',                  /* 'Working' version */
3879             p_current_original_flag     => 'N',                  /* 'Working' version */
3880             p_resource_accumulated_flag => 'N',   /* HARDCODED VALUE */
3881             p_resource_list_id          => l_resource_list_id,
3882             p_version_name              =>  p_version_name,     /* user-entered value */
3883             p_budget_entry_method_code  => NULL,
3884             p_baselined_by_person_id    => NULL,
3885             p_baselined_date            => NULL,
3886             p_change_reason_code        => NULL,
3887             p_labor_quantity            => NULL,
3888             p_labor_unit_of_measure     => NULL,
3889             p_raw_cost                  => NULL,
3890             p_burdened_cost             => NULL,
3891             p_revenue                   => NULL,
3892             p_description               => p_description,      /* user-entered value */
3893             p_attribute_category        => NULL,
3894             p_attribute1                => NULL,
3895             p_attribute2                => NULL,
3896             p_attribute3                => NULL,
3897             p_attribute4                => NULL,
3898             p_attribute5                => NULL,
3899             p_attribute6                => NULL,
3900             p_attribute7                => NULL,
3901             p_attribute8                => NULL,
3902             p_attribute9                => NULL,
3903             p_attribute10               => NULL,
3904             p_attribute11               => NULL,
3905             p_attribute12               => NULL,
3906             p_attribute13               => NULL,
3907             p_attribute14               => NULL,
3908             p_attribute15               => NULL,
3909             p_first_budget_period       => NULL,
3910             p_pm_product_code           => NULL,
3911             p_pm_budget_reference       => NULL,
3912             p_wf_status_code            => NULL,
3913             p_adw_notify_flag           => NULL,
3914             p_prc_generated_flag        => NULL,
3915             p_plan_run_date             => NULL,
3916             p_plan_processing_code      => NULL, /* plan_processing_code = null, since we're not running the generate concurrent process*/
3917             p_period_profile_id         => l_period_profile_id,     /* use newly-generated period_profile_id */
3918             p_fin_plan_type_id          => p_fin_plan_type_id,
3919             p_parent_plan_version_id    => NULL,
3920             p_project_structure_version_id => NULL,
3921             p_current_working_flag      => l_current_working_flag,
3922             p_total_borrowed_revenue    => NULL,
3923             p_total_tp_revenue_in       => NULL,
3924             p_total_tp_revenue_out      => NULL,
3925             p_total_revenue_adj         => NULL,
3926             p_total_lent_resource_cost  => NULL,
3927             p_total_tp_cost_in          => NULL,
3928             p_total_tp_cost_out         => NULL,
3929             p_total_cost_adj            => NULL,
3930             p_total_unassigned_time_cost => NULL,
3931             p_total_utilization_percent  => NULL,
3932             p_total_utilization_hours    => NULL,
3933             p_total_utilization_adj      => NULL,
3934             p_total_capacity             => NULL,
3935             p_total_head_count           => NULL,
3936             p_total_head_count_adj       => NULL,
3937             p_version_type               => l_version_type_code,
3938             p_request_id                 => NULL,           /* will be changed later in the script */
3939             x_row_id                     => l_row_id,
3940             x_return_status              => l_return_status);
3941         if l_return_status <> FND_API.G_RET_STS_SUCCESS then
3942             IF P_PA_DEBUG_MODE = 'Y' THEN
3943                pa_debug.write_file('Create_Version_OrgFcst: ' || 'error with pa_prj_period_profile_utils.maintain_prj_period_profile');
3944             END IF;
3945             /* error message added to the stack in the table handler; we don't need to do it here */
3946             raise pa_fin_plan_pub.rollback_on_error;
3947         end if;
3948         IF P_PA_DEBUG_MODE = 'Y' THEN
3949            pa_debug.write_file('Create_Version_OrgFcst: ' || 'new budget version id is ' || l_budget_version_id);
3950         END IF;
3951         x_budget_version_id := l_budget_version_id;
3952 
3953 
3954 /* create new PROJECT PLANNING OPTION for level=PLAN_TYPE, if it doesn't already exist */
3955         /* retrieve the amount_set_id for org forecast */
3956         open amount_set_csr;
3957         fetch amount_set_csr into amount_set_rec;
3958         if amount_set_csr%NOTFOUND then
3959             close amount_set_csr;
3960             IF P_PA_DEBUG_MODE = 'Y' THEN
3961                pa_debug.write_file('Create_Version_OrgFcst: ' || 'no data found in amount_sets');
3962             END IF;
3963             raise pa_fin_plan_pub.rollback_on_error;
3964         else
3965             l_org_amount_set_id := amount_set_rec.fin_plan_amount_set_id;
3966             close amount_set_csr;
3967         end if;
3968 
3969         l_proj_fin_plan_options_id := null;
3970         open plan_options_csr;
3971         fetch plan_options_csr into plan_options_rec;
3972         if plan_options_csr%NOTFOUND then
3973             close plan_options_csr;
3974              /* raise NO_DATA_FOUND; no error thrown here, because it's part of the logic */
3975         else
3976             l_proj_fin_plan_options_id := plan_options_rec.proj_fp_options_id;
3977             close plan_options_csr;
3978         end if;
3979 
3980         if l_proj_fin_plan_options_id is NULL then
3981             IF P_PA_DEBUG_MODE = 'Y' THEN
3982                pa_debug.write_file('Create_Version_OrgFcst: ' || 'no planning options for plan_type level: creating one now');
3983             END IF;
3984         /* call table handler to create planning options for plan_type */
3985             select pa_proj_fp_options_s.nextVal into l_proj_fin_plan_options_id from dual;
3986             pa_proj_fp_options_pkg.Insert_Row
3987                (px_proj_fp_options_id     =>  l_proj_fin_plan_options_id,
3988                 p_project_id                    =>  p_project_id,
3989                 p_fin_plan_option_level_code    =>  'PLAN_TYPE',
3990                 p_fin_plan_type_id              =>  p_fin_plan_type_id,
3991                 p_fin_plan_start_date           =>  NULL,               /* for PLAN_TYPE level */
3992                 p_fin_plan_end_date             =>  NULL,               /* for PLAN_TYPE level */
3993                 p_fin_plan_preference_code      =>  'COST_AND_REV_SAME',     /* for org_forecast */
3994                 p_cost_amount_set_id            =>  NULL,                    /* for org_forecast */
3995                 p_revenue_amount_set_id         =>  NULL,                    /* for org_forecast */
3996                 p_all_amount_set_id             =>  l_org_amount_set_id,     /* for org_forecast */
3997                 p_cost_fin_plan_level_code      =>  NULL,                   /* for org_forecast */
3998                 p_cost_time_phased_code         =>  NULL,                   /* for org_forecast */
3999                 p_cost_resource_list_id         =>  NULL,                   /* for org_forecast */
4000                 p_revenue_fin_plan_level_code   =>  NULL,                   /* for org_forecast */
4001                 p_revenue_time_phased_code      =>  NULL,                   /* for org_forecast */
4002                 p_revenue_resource_list_id      =>  NULL,                   /* for org_forecast */
4003                 p_all_fin_plan_level_code       =>  'L',                     /* for org_forecast */
4004                 p_all_time_phased_code          =>  l_org_time_phased_code, /* for org_forecast */
4005                 p_all_resource_list_id          =>  l_resource_list_id,
4006                 p_report_labor_hrs_from_code    =>  'COST',                 /* for org_forecast */
4007                 p_fin_plan_version_id           =>  NULL,                 /* use l_budget_version_id only at the VERSION_TYPE level */
4008                 x_row_id                        =>  l_row_id,
4009                 x_return_status                 =>  l_return_status);
4010             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4011                 IF P_PA_DEBUG_MODE = 'Y' THEN
4012                    pa_debug.write_file('Create_Version_OrgFcst: ' || 'error with pa_fp_proj_fplan_options_pkg.Insert_Row: plan_type level');
4013                 END IF;
4014                 /* error message added to the stack in the table handler; we don't need to do it here */
4015                 raise pa_fin_plan_pub.rollback_on_error;
4016             end if;
4017         end if;
4018         /* create planning option for plan VERSION */
4019         IF P_PA_DEBUG_MODE = 'Y' THEN
4020            pa_debug.write_file('Create_Version_OrgFcst: ' || 'creating planning options for PLAN_VERSION level');
4021         END IF;
4022         select pa_proj_fp_options_s.nextVal into l_proj_fin_plan_options_id from dual;
4023         pa_proj_fp_options_pkg.Insert_Row
4024                (px_proj_fp_options_id     => l_proj_fin_plan_options_id,
4025                 p_project_id                    => p_project_id,
4026                 p_fin_plan_option_level_code    => 'PLAN_VERSION',
4027                 p_fin_plan_type_id              => p_fin_plan_type_id,
4028                 p_fin_plan_start_date           => l_fcst_start_date,
4029                 p_fin_plan_end_date             => l_fcst_end_date,
4030                 p_fin_plan_preference_code      => 'COST_AND_REV_SAME',     /* for org_forecast */
4031                 p_cost_amount_set_id            => NULL,                    /* for org_forecast */
4032                 p_revenue_amount_set_id         => NULL,                    /* for org_forecast */
4033                 p_all_amount_set_id             => l_org_amount_set_id,     /* for org_forecast */
4034                 p_cost_fin_plan_level_code      => NULL,                   /* for org_forecast */
4035                 p_cost_time_phased_code         => NULL,                   /* for org_forecast */
4036                 p_cost_resource_list_id         => NULL,                   /* for org_forecast */
4037                 p_revenue_fin_plan_level_code   => NULL,                   /* for org_forecast */
4038                 p_revenue_time_phased_code      => NULL,                   /* for org_forecast */
4039                 p_revenue_resource_list_id      => NULL,                   /* for org_forecast */
4040                 p_all_fin_plan_level_code       => 'L',                     /* for org_forecast */
4041                 p_all_time_phased_code          => l_org_time_phased_code, /* for org_forecast */
4042                 p_all_resource_list_id          => l_resource_list_id,
4043                 p_report_labor_hrs_from_code    => 'COST',                 /* for org_forecast */
4044                 p_fin_plan_version_id           => l_budget_version_id,
4045                 x_row_id                        => l_row_id,
4046                 x_return_status                 => l_return_status);
4047             if l_return_status <> FND_API.G_RET_STS_SUCCESS then
4048                 IF P_PA_DEBUG_MODE = 'Y' THEN
4049                    pa_debug.write_file('Create_Version_OrgFcst: ' || 'error with pa_fp_proj_fplan_options_pkg.Insert_Row: plan_version level');
4050                 END IF;
4051                 /* error message added to the stack in the table handler; we don't need to do it here */
4052                 raise pa_fin_plan_pub.rollback_on_error;
4053             end if;
4054         l_msg_count := FND_MSG_PUB.count_msg;
4055         if l_msg_count = 0 then
4056             x_return_status := FND_API.G_RET_STS_SUCCESS;
4057             pa_debug.reset_err_stack;
4058         else
4059             IF P_PA_DEBUG_MODE = 'Y' THEN
4060                pa_debug.write_file('Create_Version_OrgFcst: ' || 'l_msg_count is ' || l_msg_count);
4061             END IF;
4062             x_return_status := FND_API.G_RET_STS_ERROR;
4063             if l_msg_count > 0 then
4064                 if l_msg_count = 1 then
4065                     PA_INTERFACE_UTILS_PUB.get_messages
4066                          (p_encoded        => FND_API.G_TRUE,
4067                           p_msg_index      => 1,
4068                           p_msg_count      => l_msg_count,
4069                           p_msg_data       => l_msg_data,
4070                           p_data           => l_data,
4071                           p_msg_index_out  => l_msg_index_out);
4072                      x_msg_data := l_data;
4073                      x_msg_count := l_msg_count;
4074                     else
4075                      x_msg_count := l_msg_count;
4076                 end if;
4077                 raise pa_fin_plan_pub.rollback_on_error;
4078             end if;
4079         end if;
4080     end if;
4081 
4082 exception
4083     when pa_fin_plan_pub.rollback_on_error then
4084       IF P_PA_DEBUG_MODE = 'Y' THEN
4085          pa_debug.write_file('Create_Version_OrgFcst: rollback_on_error exception');
4086       END IF;
4087       rollback to PA_FP_PUB_CREATE_VER_ORGFCST;
4088       raise FND_API.G_EXC_UNEXPECTED_ERROR;
4089 
4090     when others then
4091         rollback to PA_FP_PUB_CREATE_VER_ORGFCST;
4092         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4093         x_msg_count     := 1;
4094         x_msg_data      := SQLERRM;
4095         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
4096                                  p_procedure_name   => 'Create_Version_OrgFcst');
4097         pa_debug.reset_err_stack;
4098         raise FND_API.G_EXC_UNEXPECTED_ERROR;
4099 end Create_Version_OrgFcst;
4100 /* ------------------------------------------------------------------------- */
4101 
4102 procedure Regenerate
4103     (p_project_id                   IN     pa_budget_versions.project_id%TYPE,
4104      p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE,
4105      p_record_version_number        IN     pa_budget_versions.record_version_number%TYPE,
4106      x_return_status                    OUT    NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
4107      x_msg_count                        OUT    NOCOPY NUMBER, --File.Sql.39 bug 4440895
4108      x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4109 is
4110 l_debug_mode      VARCHAR2(30);
4111 l_msg_count       NUMBER := 0;
4112 l_valid_flag      VARCHAR2(1);
4113 l_budget_status_code    pa_budget_versions.budget_status_code%TYPE;
4114 l_request_id      NUMBER;
4115 l_data            VARCHAR2(2000);
4116 l_msg_data        VARCHAR2(2000);
4117 l_error_msg_code  VARCHAR2(30);
4118 l_msg_index_out   NUMBER;
4119 l_return_status   VARCHAR2(2000);
4120 /* Moac Changes */
4121 l_org_id      NUMBER;
4122 
4123 begin
4124     FND_MSG_PUB.initialize;
4125     IF P_PA_DEBUG_MODE = 'Y' THEN
4126        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Regenerate');
4127     END IF;
4128     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4129     l_debug_mode := NVL(l_debug_mode, 'Y');
4130     IF P_PA_DEBUG_MODE = 'Y' THEN
4131        pa_debug.set_process('Regenerate: ' || 'PLSQL','LOG',l_debug_mode);
4132     END IF;
4133     x_msg_count := 0;
4134 /* CHECK FOR BUSINESS RULES VIOLATIONS */
4135     /* check for null budget_version_id */
4136     if p_budget_version_id is NULL then
4137         x_return_status := FND_API.G_RET_STS_ERROR;
4138         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4139                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
4140     end if;
4141 
4142 /* check to see if the budget version we're regenerating has */
4143     /* been updated by someone else already */
4144     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
4145             (p_unique_index             => p_budget_version_id,
4146              p_record_version_number    => p_record_version_number,
4147              x_valid_flag               => l_valid_flag,
4148              x_return_status            => l_return_status,
4149              x_error_msg_code           => l_error_msg_code);
4150     if x_return_status = FND_API.G_RET_STS_ERROR then
4151         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4152                              p_msg_name            => l_error_msg_code);
4153     end if;
4154 
4155 /* check to see if the budget version is in the submitted status.  we cannot */
4156 /* regenerate the budget version if it's in the submitted status. */
4157     /** MOAC Changes:4510784
4158     select
4159         budget_status_code
4160     into
4161         l_budget_status_code
4162     from
4163         pa_budget_versions
4164     where
4165         budget_version_id=p_budget_version_id;
4166     **/
4167     -- get org id to initiaize before submit the fnd request
4168     SELECT  pp.org_id
4169         ,bv.budget_status_code
4170         INTO    l_org_id
4171         ,l_budget_status_code
4172         FROM    pa_projects_all pp
4173             ,pa_budget_versions bv
4174         WHERE   pp.project_id = bv.project_id
4175     AND     bv.budget_version_id = p_budget_version_id;
4176     /* End of bug fix:4510784 */
4177 
4178     if l_budget_status_code = 'S' then
4179         x_return_status := FND_API.G_RET_STS_ERROR;
4180         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4181                              p_msg_name            => 'PA_FP_REGEN_SUBMITTED');
4182     end if;
4183 
4184 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
4185     l_msg_count := FND_MSG_PUB.count_msg;
4186     if l_msg_count > 0 then
4187         if l_msg_count = 1 then
4188              PA_INTERFACE_UTILS_PUB.get_messages
4189                  (p_encoded        => FND_API.G_TRUE,
4190                   p_msg_index      => 1,
4191                   p_msg_count      => l_msg_count,
4192                   p_msg_data       => l_msg_data,
4193                   p_data           => l_data,
4194                   p_msg_index_out  => l_msg_index_out);
4195              x_msg_data := l_data;
4196              x_msg_count := l_msg_count;
4197             else
4198              x_msg_count := l_msg_count;
4199         end if;
4200             pa_debug.reset_err_stack;
4201             return;
4202     end if;
4203 
4204 /* If There are NO Business Rules Violations , Then proceed with Regenerate */
4205     if l_msg_count = 0 then
4206         SAVEPOINT PA_FIN_PLAN_PUB_REGENERATE;
4207         IF P_PA_DEBUG_MODE = 'Y' THEN
4208            pa_debug.write_file('no business logic errors; proceeding with regenerate');
4209         END IF;
4210         /* submit the concurrent request to generate the forecast */
4211         /* will need to FND_REQUEST.set_mode if submit_request is called from database trigger */
4212 
4213         /* SUBMIT THE REQUEST ONLY WHEN version_type = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST */
4214        /* Moac changes:4510784 initialize org id before submitting the request for single org context */
4215         MO_GLOBAL.INIT('PA');
4216             If P_PA_DEBUG_MODE = 'Y' THEN
4217             PA_DEBUG.Log_Message(p_message => 'Calling MO_GLOBAL.SET_POLICY_CONTEXT for OrgId:'||l_org_id);
4218             End If;
4219             MO_GLOBAL.SET_POLICY_CONTEXT('S',l_org_id);
4220         FND_REQUEST.SET_ORG_ID(l_org_id);
4221             l_request_id := FND_REQUEST.submit_request
4222                (application                =>   'PA',
4223                 program                    =>   'PAFPORGF',   /* refer to HLD: Generate Organization Forecast */
4224                 description                =>   NULL,
4225                 start_time                 =>   NULL,
4226                 sub_request                =>   false,
4227                 argument1                  =>   NULL,
4228                 argument2                  =>   NULL,
4229                 argument3                  =>   NULL,
4230                 argument4                  =>   NULL,
4231                 argument5                  =>   NULL,
4232                 argument6                  =>   p_budget_version_id);
4233             if l_request_id = 0 then
4234                 x_return_status := FND_API.G_RET_STS_ERROR;
4235                 IF P_PA_DEBUG_MODE = 'Y' THEN
4236                    pa_debug.write_file('Regenerate: ' || 'l_request_id=0; ERROR');
4237                 END IF;
4238                 /* FND_MESSAGE.RETRIEVE; */
4239                 l_msg_data := FND_MESSAGE.GET;
4240                 IF P_PA_DEBUG_MODE = 'Y' THEN
4241                    pa_debug.write_file('Regenerate: ' || 'the error message is ' || l_msg_data);
4242                 END IF;
4243                 /* PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4244                                      p_msg_name            => FND_MESSAGE.GET); */
4245                 l_msg_count := 1;
4246                 /*l_msg_count := FND_MSG_PUB.count_msg;*/
4247                 if l_msg_count = 1 then
4248                     PA_INTERFACE_UTILS_PUB.get_messages
4249                         (p_encoded        => FND_API.G_TRUE,
4250                          p_msg_index      => 1,
4251                          p_msg_count      => l_msg_count,
4252                          p_msg_data       => l_msg_data,
4253                          p_data           => l_data,
4254                          p_msg_index_out  => l_msg_index_out);
4255                     x_msg_data := l_data;
4256                     x_msg_count := l_msg_count;
4257                     else
4258                     x_msg_count := l_msg_count;
4259                 end if;
4260                 pa_debug.reset_err_stack;
4261                 raise FND_API.G_EXC_UNEXPECTED_ERROR;
4262             else
4263                 IF P_PA_DEBUG_MODE = 'Y' THEN
4264                    pa_debug.write_file('Regenerate: ' || 'concurrent process submitted successfully; stamping request_id');
4265                 END IF;
4266                 update
4267                     pa_budget_versions
4268                 set
4269                     request_id = l_request_id,
4270                     plan_processing_code = PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P,     /* "Generation in process" */
4271                     record_version_number = record_version_number + 1
4272                 where
4273                     budget_version_id = p_budget_version_id;
4274             end if;
4275             x_return_status := FND_API.G_RET_STS_SUCCESS;
4276             pa_debug.reset_err_stack;
4277     end if;
4278 
4279 exception
4280     when pa_fin_plan_pub.rollback_on_error then
4281       IF P_PA_DEBUG_MODE = 'Y' THEN
4282          pa_debug.write_file('Regenerate: rollback_on_error exception');
4283       END IF;
4284       rollback to PA_FIN_PLAN_PUB_REGENERATE;
4285       raise FND_API.G_EXC_UNEXPECTED_ERROR;
4286 
4287     when others then
4288         rollback to PA_FIN_PLAN_PUB_REGENERATE;
4289         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4290         x_msg_count     := 1;
4291         x_msg_data      := SQLERRM;
4292         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
4293                                  p_procedure_name   => 'Regenerate');
4294         pa_debug.reset_err_stack;
4295         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4296 end Regenerate;
4297 
4298 /* ------------------------------------------------------------------------- */
4299 
4300 procedure Update_Version
4301     (p_project_id                   IN     pa_budget_versions.project_id%TYPE
4302      ,p_budget_version_id            IN     pa_budget_versions.budget_version_id%TYPE
4303      ,p_record_version_number        IN     pa_budget_versions.record_version_number%TYPE
4304      ,p_version_name                 IN     pa_budget_versions.version_name%TYPE
4305      ,p_description                  IN     pa_budget_versions.description%TYPE
4306      ,p_change_reason_code           IN     pa_budget_versions.change_reason_code%TYPE
4307     -- Start of additional columns for Bug :- 3088010
4308     ,p_attribute_category               IN     pa_budget_versions.attribute_category%TYPE
4309     ,p_attribute1                       IN     pa_budget_versions.attribute1%TYPE
4310     ,p_attribute2                       IN     pa_budget_versions.attribute2%TYPE
4311     ,p_attribute3                       IN     pa_budget_versions.attribute3%TYPE
4312     ,p_attribute4                       IN     pa_budget_versions.attribute4%TYPE
4313     ,p_attribute5                       IN     pa_budget_versions.attribute5%TYPE
4314     ,p_attribute6                       IN     pa_budget_versions.attribute6%TYPE
4315     ,p_attribute7                       IN     pa_budget_versions.attribute7%TYPE
4316     ,p_attribute8                       IN     pa_budget_versions.attribute8%TYPE
4317     ,p_attribute9                       IN     pa_budget_versions.attribute9%TYPE
4318     ,p_attribute10                      IN     pa_budget_versions.attribute10%TYPE
4319     ,p_attribute11                      IN     pa_budget_versions.attribute11%TYPE
4320     ,p_attribute12                      IN     pa_budget_versions.attribute12%TYPE
4321     ,p_attribute13                      IN     pa_budget_versions.attribute13%TYPE
4322     ,p_attribute14                      IN     pa_budget_versions.attribute14%TYPE
4323     ,p_attribute15                      IN     pa_budget_versions.attribute15%TYPE
4324     -- End of additional columns for Bug :- 3088010
4325      ,x_return_status                    OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
4326      ,x_msg_count                        OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
4327      ,x_msg_data                         OUT    NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4328 is
4329 l_debug_mode      VARCHAR2(30);
4330 l_valid_flag              VARCHAR2(1);
4331 l_current_working_flag    pa_budget_versions.current_working_flag%TYPE;
4332 l_record_version_number   pa_budget_versions.record_version_number%TYPE;
4333 
4334 l_msg_count       NUMBER := 0;
4335 l_data            VARCHAR2(2000);
4336 l_msg_data        VARCHAR2(2000);
4337 l_error_msg_code  VARCHAR2(30);
4338 l_msg_index_out   NUMBER;
4339 l_return_status   VARCHAR2(2000);
4340 
4341 begin
4342     FND_MSG_PUB.initialize;
4343     IF P_PA_DEBUG_MODE = 'Y' THEN
4344        pa_debug.init_err_stack('PA_FIN_PLAN_PUB.Update_Version');
4345     END IF;
4346     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4347     l_debug_mode := NVL(l_debug_mode, 'Y');
4348     IF P_PA_DEBUG_MODE = 'Y' THEN
4349        pa_debug.set_process('Update_Version: ' || 'PLSQL','LOG',l_debug_mode);
4350     END IF;
4351     x_msg_count := 0;
4352 /* CHECK FOR BUSINESS RULES VIOLATIONS */
4353     /* check for null budget_version_id */
4354     if p_budget_version_id is NULL then
4355         x_return_status := FND_API.G_RET_STS_ERROR;
4356         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4357                              p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
4358     end if;
4359     /* check to see if the budget version we're updating to be current working has */
4360     /* been updated by someone else already */
4361     PA_FIN_PLAN_UTILS.Check_Record_Version_Number
4362             (p_unique_index             => p_budget_version_id,
4363              p_record_version_number    => p_record_version_number,
4364              x_valid_flag               => l_valid_flag,
4365              x_return_status            => l_return_status,
4366              x_error_msg_code           => l_error_msg_code);
4367     if x_return_status = FND_API.G_RET_STS_ERROR then
4368         PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4369                              p_msg_name            => l_error_msg_code);
4370     end if;
4371 
4372 /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
4373     l_msg_count := FND_MSG_PUB.count_msg;
4374     if l_msg_count > 0 then
4375         if l_msg_count = 1 then
4376              PA_INTERFACE_UTILS_PUB.get_messages
4377                  (p_encoded        => FND_API.G_TRUE,
4378                   p_msg_index      => 1,
4379                   p_msg_count      => l_msg_count,
4380                   p_msg_data       => l_msg_data,
4381                   p_data           => l_data,
4382                   p_msg_index_out  => l_msg_index_out);
4383              x_msg_data := l_data;
4384              x_msg_count := l_msg_count;
4385             else
4386              x_msg_count := l_msg_count;
4387         end if;
4388             pa_debug.reset_err_stack;
4389             return;
4390     end if;
4391 
4392 /* If There are NO Business Rules Violations , Then proceed with Update Version */
4393     if l_msg_count = 0 then
4394         SAVEPOINT PA_FIN_PLAN_PUB_UPDATE_VERSION;
4395         l_record_version_number := p_record_version_number + 1;
4396 
4397         UPDATE
4398             PA_BUDGET_VERSIONS
4399         SET
4400             record_version_number       = l_record_version_number,
4401             version_name                = p_version_name,
4402             description                 = p_description,
4403             change_reason_code          = p_change_reason_code,
4404             last_update_date            = SYSDATE,
4405             last_updated_by             = FND_GLOBAL.user_id,
4406             last_update_login           = FND_GLOBAL.login_id,
4407             /* Code addition for bug 3088010 starts */
4408             attribute_category          = p_attribute_category,
4409             attribute1                  = p_attribute1,
4410             attribute2                  = p_attribute2,
4411             attribute3                  = p_attribute3,
4412             attribute4                  = p_attribute4,
4413             attribute5                  = p_attribute5,
4414             attribute6                  = p_attribute6,
4415             attribute7                  = p_attribute7,
4416             attribute8                  = p_attribute8,
4417             attribute9                  = p_attribute9,
4418             attribute10                 = p_attribute10,
4419             attribute11                 = p_attribute11,
4420             attribute12                 = p_attribute12,
4421             attribute13                 = p_attribute13,
4422             attribute14                 = p_attribute14,
4423             attribute15                 = p_attribute15
4424             /* Code addition for bug 3088010 ends */
4425         WHERE
4426             budget_version_id           = p_budget_version_id;
4427     end if;
4428     x_return_status := FND_API.G_RET_STS_SUCCESS;
4429     pa_debug.reset_err_stack;
4430 
4431 exception
4432     when others then
4433       rollback to PA_FIN_PLAN_PUB_UPDATE_VERSION;
4434       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4435       x_msg_count     := 1;
4436       x_msg_data      := SQLERRM;
4437       FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
4438                                p_procedure_name   => 'Update_Version');
4439       pa_debug.reset_err_stack;
4440       raise FND_API.G_EXC_UNEXPECTED_ERROR;
4441 end Update_Version;
4442 /*========================================================================
4443   Bug no.:- 2331201
4444   This api is called from Copy_version and called only in the case of '
4445   ORG_FORECAST'
4446 =========================================================================*/
4447 procedure Create_Org_Fcst_Elements (
4448     p_project_id               IN      pa_projects_all.project_id%TYPE,
4449     p_source_version_id        IN      pa_budget_versions.budget_version_id%TYPE,
4450     p_target_version_id        IN      pa_budget_versions.budget_version_id%TYPE,
4451     x_return_status               OUT  NOCOPY VARCHAR2, --File.Sql.39 bug 4440895
4452     x_msg_count                   OUT  NOCOPY NUMBER, --File.Sql.39 bug 4440895
4453     x_msg_data                    OUT  NOCOPY VARCHAR2) --File.Sql.39 bug 4440895
4454 
4455 AS
4456 
4457 l_debug_mode      VARCHAR2(30);
4458 l_msg_count       NUMBER := 0;
4459 l_data            VARCHAR2(2000);
4460 l_msg_data        VARCHAR2(2000);
4461 l_error_msg_code  VARCHAR2(30);
4462 l_msg_index_out   NUMBER;
4463 
4464 
4465 l_resource_assignment_id    pa_resource_assignments.resource_assignment_id%TYPE;
4466 l_forecast_element_id       pa_org_forecast_lines.forecast_element_id%TYPE;
4467 l_adj_element_id            pa_fp_adj_elements.adj_element_id%TYPE;
4468 
4469 cursor l_ra_csr is
4470     select
4471         resource_assignment_id
4472     from
4473         pa_resource_assignments
4474     where
4475         budget_version_id=p_source_version_id;
4476 
4477 l_ra_rec l_ra_csr%ROWTYPE;
4478 
4479 cursor l_fe_csr is
4480     select
4481         forecast_element_id
4482     from
4483         pa_org_fcst_elements
4484     where
4485         budget_version_id=p_source_version_id;
4486 
4487 l_fe_rec l_fe_csr%ROWTYPE;
4488 
4489 cursor l_fl_csr is
4490     select
4491         forecast_line_id
4492     from
4493         pa_org_forecast_lines
4494     where
4495         forecast_element_id=l_fe_rec.forecast_element_id;
4496 
4497 l_fl1_rec l_fl_csr%ROWTYPE;
4498 
4499 cursor l_ae_csr is
4500     select
4501         adj_element_id
4502     from
4503         pa_fp_adj_elements
4504     where
4505         budget_version_id = p_source_version_id and
4506         resource_assignment_id = l_ra_rec.resource_assignment_id;
4507 
4508 l_ae_rec l_ae_csr%ROWTYPE;
4509 
4510 begin
4511 
4512      pa_debug.set_err_stack ('PA_FIN_PLAN_PUB.Create_Org_Fcst_Elements');
4513      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
4514      l_debug_mode := NVL(l_debug_mode, 'Y');
4515      IF P_PA_DEBUG_MODE = 'Y' THEN
4516         pa_debug.set_process('Create_Org_Fcst_Elements: ' || 'PLSQL','LOG',l_debug_mode);
4517      END IF;
4518      x_msg_count := 0;
4519 
4520      /* CHECK FOR BUSINESS RULES VIOLATIONS */
4521          /* check for null budget_version_id */
4522          if (p_source_version_id is null)  or (p_target_version_id is null) then
4523              x_return_status := FND_API.G_RET_STS_ERROR;
4524              PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
4525                                   p_msg_name            => 'PA_FP_NO_PLAN_VERSION');
4526          end if;
4527 
4528      /* If There are ANY Business Rules Violations , Then Do NOT Proceed: RETURN */
4529          l_msg_count := FND_MSG_PUB.count_msg;
4530          if l_msg_count > 0 then
4531              if l_msg_count = 1 then
4532                   PA_INTERFACE_UTILS_PUB.get_messages
4533                       (p_encoded        => FND_API.G_TRUE,
4534                        p_msg_index      => 1,
4535                        p_msg_count      => l_msg_count,
4536                        p_msg_data       => l_msg_data,
4537                        p_data           => l_data,
4538                        p_msg_index_out  => l_msg_index_out);
4539                   x_msg_data := l_data;
4540                   x_msg_count := l_msg_count;
4541              else
4542                   x_msg_count := l_msg_count;
4543              end if;
4544              pa_debug.reset_err_stack;
4545              return;
4546          end if;
4547 
4548      /* If there are no Business Rules violations, then continue with Create_Org_Fcst_Elements */
4549      if l_msg_count = 0 then
4550            IF P_PA_DEBUG_MODE = 'Y' THEN
4551               pa_debug.write_file('Create_Org_Fcst_Elements: ' || 'no business violations; continuing with create org fcst elements');
4552            END IF;
4553 
4554            x_return_status    := FND_API.G_RET_STS_SUCCESS;
4555 
4556            open l_ra_csr;
4557            loop
4558                fetch l_ra_csr into l_ra_rec;
4559                exit when l_ra_csr%NOTFOUND;
4560 
4561                --Fetch corresponding target resource assignment id from pa_fp_ra_map_tmp
4562 
4563                select target_res_assignment_id
4564                into   l_resource_assignment_id
4565                from   pa_fp_ra_map_tmp
4566                where  source_res_assignment_id = l_ra_rec.resource_assignment_id;
4567 
4568                /* PA_FP_ADJ_ELEMENTS: Insert new row for all existing rows with the same budget_version_id */
4569                open l_ae_csr;
4570                loop
4571                        fetch l_ae_csr into l_ae_rec;
4572                        exit when l_ae_csr%NOTFOUND;
4573                        select pa_fp_adj_elements_s.nextVal into l_adj_element_id from dual;
4574                        insert into pa_fp_adj_elements(
4575                            adj_element_id,
4576                            project_id,
4577                            budget_version_id,
4578                            resource_assignment_id,
4579                            task_id,
4580                             adjustment_reason_code,
4581                             adjustment_comments,
4582                             creation_date,
4583                             created_by,
4584                             last_update_login,
4585                             last_updated_by,
4586                             last_update_date)
4587                       select
4588                             l_adj_element_id,       /* use newly-created adj_element_id */
4589                             ae.project_id,
4590                             p_target_version_id,    /* use newly-created budget_version_id */
4591                             l_resource_assignment_id,   /* use newly-created resource_assignment_id */
4592                             ae.task_id,
4593                             ae.adjustment_reason_code,
4594                             ae.adjustment_comments,
4595                             SYSDATE,                /* creation_date */
4596                             FND_GLOBAL.user_id,     /* created_by */
4597                             FND_GLOBAL.login_id,    /* last_update_login */
4598                             FND_GLOBAL.user_id,     /* last_updated_by */
4599                             SYSDATE                 /* last_update_date */
4600                       from
4601                                pa_fp_adj_elements ae
4602                       where
4603                                ae.adj_element_id = l_ae_rec.adj_element_id;
4604                       /* PA_FIN_PLAN_ADJ_LINES: Insert new row for all rows with the old adj_element_id*/
4605                       insert into pa_fin_plan_adj_lines (
4606                                     adj_element_id,
4607                                     creation_date,
4608                                     created_by,
4609                                     last_update_login,
4610                                     last_updated_by,
4611                                     last_update_date,
4612                                     fin_plan_adj_line_id,
4613                                     project_id,
4614                                     task_id,
4615                                     budget_version_id,
4616                                     resource_assignment_id,
4617                                     period_name,
4618                                     start_date,
4619                                     end_date,
4620                                     raw_cost_adjustment,
4621                                     burdened_cost_adjustment,
4622                                     revenue_adjustment,
4623                                     utilization_adjustment,
4624                                     head_count_adjustment)
4625                       select
4626                                     l_adj_element_id,               /* use newly-created adj_element_id */
4627                                     SYSDATE,                        /* creation_date */
4628                                     FND_GLOBAL.user_id,             /* created_by */
4629                                     FND_GLOBAL.login_id,            /* last_update_login */
4630                                     FND_GLOBAL.user_id,             /* last_updated_by */
4631                                     SYSDATE,                        /* last_update_date */
4632                                     pa_fin_plan_adj_lines_s.nextVal, /* use nextVal for fin_plan_adj_line_id */
4633                                     al.project_id,
4634                                     al.task_id,
4635                                     p_target_version_id,            /* use newly-created budget_version_id */
4636                                     l_resource_assignment_id,       /* use newly-created resource assignment id */
4637                                     al.period_name,
4638                                     al.start_date,
4639                                     al.end_date,
4640                                     al.raw_cost_adjustment,
4641                                     al.burdened_cost_adjustment,
4642                                     al.revenue_adjustment,
4643                                     al.utilization_adjustment,
4644                                     al.head_count_adjustment
4645                       from
4646                                     pa_fin_plan_adj_lines al
4647                       where
4648                                    al.adj_element_id=l_ae_rec.adj_element_id;
4649 
4650 
4651 
4652                       /* PA_PROJECT_PERIODS_DENORM: Insert a new row for every record whose budget_version_id and
4653                          resource_assignment_id match our old ones */
4654                       /* key on OBJECT_ID = adj_element_id of the original version */
4655                       IF P_PA_DEBUG_MODE = 'Y' THEN
4656                          pa_debug.write_file('Create_Org_Fcst_Elements: ' || 'inserting into pa_project_periods_denorm');
4657                       END IF;
4658                       insert into pa_proj_periods_denorm (
4659                           creation_date,
4660                           created_by,
4661                           last_update_login,
4662                           last_updated_by,
4663                           last_update_date,
4664                           budget_version_id,
4665                           resource_assignment_id,
4666                           object_id,
4667                           object_type_code,
4668                           period_profile_id,
4669                           amount_type_code,
4670                           amount_subtype_code,
4671                           amount_type_id,
4672                           amount_subtype_id,
4673                           currency_type,
4674                           currency_code,
4675                           preceding_periods_amount,
4676                           succeeding_periods_amount,
4677                           prior_period_amount,
4678                           period_amount1,
4679                           period_amount2,
4680                           period_amount3,
4681                           period_amount4,
4682                           period_amount5,
4683                           period_amount6,
4684                           period_amount7,
4685                           period_amount8,
4686                           period_amount9,
4687                           period_amount10,
4688                           period_amount11,
4689                           period_amount12,
4690                           period_amount13,
4691                           period_amount14,
4692                           period_amount15,
4693                           period_amount16,
4694                           period_amount17,
4695                           period_amount18,
4696                           period_amount19,
4697                           period_amount20,
4698                           period_amount21,
4699                           period_amount22,
4700                           period_amount23,
4701                           period_amount24,
4702                           period_amount25,
4703                           period_amount26,
4704                           period_amount27,
4705                           period_amount28,
4706                           period_amount29,
4707                           period_amount30,
4708                           period_amount31,
4709                           period_amount32,
4710                           period_amount33,
4711                           period_amount34,
4712                           period_amount35,
4713                           period_amount36,
4714                           period_amount37,
4715                           period_amount38,
4716                           period_amount39,
4717                           period_amount40,
4718                           period_amount41,
4719                           period_amount42,
4720                           period_amount43,
4721                           period_amount44,
4722                           period_amount45,
4723                           period_amount46,
4724                           period_amount47,
4725                           period_amount48,
4726                           period_amount49,
4727                           period_amount50,
4728                           period_amount51,
4729                           period_amount52,
4730                           project_id,
4731                           parent_assignment_id)
4732                       select
4733                           SYSDATE,                            /* creation_date */
4734                           FND_GLOBAL.user_id,                 /* created_by */
4735                           FND_GLOBAL.login_id,                /* last_update_login */
4736                           FND_GLOBAL.user_id,                 /* last_updated_by */
4737                           SYSDATE,                            /* last_update_date */
4738                           p_target_version_id,                /* use newly-created budget_version_id */
4739                           ppd.resource_assignment_id,   /* copy over resource_assignment_id */
4740                           l_adj_element_id,           /* object_id is the newly-created adj_element_id */
4741                           ppd.object_type_code,
4742                           ppd.period_profile_id,
4743                           ppd.amount_type_code,
4744                           ppd.amount_subtype_code,
4745                           ppd.amount_type_id,
4746                           ppd.amount_subtype_id,
4747                           ppd.currency_type,
4748                           ppd.currency_code,
4749                           ppd.preceding_periods_amount,
4750                           ppd.succeeding_periods_amount,
4751                           ppd.prior_period_amount,
4752                           ppd.period_amount1,
4753                           ppd.period_amount2,
4754                           ppd.period_amount3,
4755                           ppd.period_amount4,
4756                           ppd.period_amount5,
4757                           ppd.period_amount6,
4758                           ppd.period_amount7,
4759                           ppd.period_amount8,
4760                           ppd.period_amount9,
4761                           ppd.period_amount10,
4762                           ppd.period_amount11,
4763                           ppd.period_amount12,
4764                           ppd.period_amount13,
4765                           ppd.period_amount14,
4766                           ppd.period_amount15,
4767                           ppd.period_amount16,
4768                           ppd.period_amount17,
4769                           ppd.period_amount18,
4770                           ppd.period_amount19,
4771                           ppd.period_amount20,
4772                           ppd.period_amount21,
4773                           ppd.period_amount22,
4774                           ppd.period_amount23,
4775                           ppd.period_amount24,
4776                           ppd.period_amount25,
4777                           ppd.period_amount26,
4778                           ppd.period_amount27,
4779                           ppd.period_amount28,
4780                           ppd.period_amount29,
4781                           ppd.period_amount30,
4782                           ppd.period_amount31,
4783                           ppd.period_amount32,
4784                           ppd.period_amount33,
4785                           ppd.period_amount34,
4786                           ppd.period_amount35,
4787                           ppd.period_amount36,
4788                           ppd.period_amount37,
4789                           ppd.period_amount38,
4790                           ppd.period_amount39,
4791                           ppd.period_amount40,
4792                           ppd.period_amount41,
4793                           ppd.period_amount42,
4794                           ppd.period_amount43,
4795                           ppd.period_amount44,
4796                           ppd.period_amount45,
4797                           ppd.period_amount46,
4798                           ppd.period_amount47,
4799                           ppd.period_amount48,
4800                           ppd.period_amount49,
4801                           ppd.period_amount50,
4802                           ppd.period_amount51,
4803                           ppd.period_amount52,
4804                           p_project_id, --passed value
4805                           NULL    --Org_Forecast doesn't have rollup
4806                       from
4807                           pa_proj_periods_denorm ppd
4808                       where
4809                           ppd.budget_version_id = p_source_version_id and
4810                           ppd.object_id = l_ae_rec.adj_element_id;
4811 
4812                end loop; -- l_ae_csr
4813                close l_ae_csr;
4814 
4815            end loop; -- l_ra_csr
4816            close l_ra_csr;
4817 
4818 
4819 
4820           /* PA_ORG_FCST_ELEMENTS: Insert a new row for each row that contained the old budget version */
4821            IF P_PA_DEBUG_MODE = 'Y' THEN
4822               pa_debug.write_file('Create_Org_Fcst_Elements: ' || 'insert into pa_org_forecast_elements');
4823            END IF;
4824            open l_fe_csr;
4825            loop
4826                   /* generate next forecast_element_id into local variable */
4827                   select pa_org_fcst_elements_s.nextVal into l_forecast_element_id from dual;
4828                   fetch l_fe_csr into l_fe_rec;
4829                   exit when l_fe_csr%NOTFOUND;
4830                   insert into pa_org_fcst_elements (
4831                       creation_date,
4832                       created_by,
4833                       last_update_login,
4834                       last_updated_by,
4835                       last_update_date,
4836                       forecast_element_id,
4837                       organization_id,
4838                       org_id,
4839                       budget_version_id,
4840                       project_id,
4841                       task_id,
4842                       provider_receiver_code,
4843                       other_organization_id,
4844                       other_org_id,
4845                       txn_project_id,
4846                       assignment_id,
4847                       resource_id,
4848                       record_version_number)
4849                   select
4850                       SYSDATE,                        /* creation_date */
4851                       FND_GLOBAL.user_id,             /* created_by */
4852                       FND_GLOBAL.login_id,            /* last_update_login */
4853                       FND_GLOBAL.user_id,             /* last_updated_by */
4854                       SYSDATE,                        /* last_update_date */
4855                       l_forecast_element_id,  /* use newly-generated forecast_element_id */
4856                       fe.organization_id,
4857                       fe.org_id,
4858                       p_target_version_id,            /* use newly-generated budget_version_id */
4859                       fe.project_id,
4860                       fe.task_id,
4861                       fe.provider_receiver_code,
4862                       fe.other_organization_id,
4863                       fe.other_org_id,
4864                       fe.txn_project_id,
4865                       fe.assignment_id,
4866                       fe.resource_id,
4867                       1                               /* record_version_number = 1 */
4868                   from
4869                       pa_org_fcst_elements fe
4870                   where
4871                       forecast_element_id=l_fe_rec.forecast_element_id;
4872                   /* PA_ORG_FORECAST_LINES: create a new row for every row whose forecast_element_id matches
4873                      the one we're using */
4874                   open l_fl_csr;
4875                   loop
4876                       fetch l_fl_csr into l_fl1_rec;
4877                       exit when l_fl_csr%NOTFOUND;
4878                       insert into pa_org_forecast_lines (
4879                           creation_date,
4880                           created_by,
4881                           last_update_login,
4882                           last_updated_by,
4883                           last_update_date,
4884                           forecast_line_id,
4885                           forecast_element_id,
4886                           project_id,
4887                           task_id,
4888                           period_name,
4889                           start_date,
4890                           end_date,
4891                           quantity,
4892                           raw_cost,
4893                           burdened_cost,
4894                           tp_cost_in,
4895                           tp_cost_out,
4896                           revenue,
4897                           tp_revenue_in,
4898                           tp_revenue_out,
4899                           record_version_number,
4900                           borrowed_revenue,
4901                           lent_resource_cost,
4902                           unassigned_time_cost,
4903                           budget_version_id)
4904                       select
4905                           SYSDATE,                                /* creation_date */
4906                           FND_GLOBAL.user_id,                     /* created_by */
4907                           FND_GLOBAL.login_id,                    /* last_update_login */
4908                           FND_GLOBAL.user_id,                     /* last_updated_by */
4909                           SYSDATE,                                /* last_update_date */
4910                           pa_org_forecast_lines_s.nextVal,        /* use nextVal to generate next forecast_line_id */
4911                           l_forecast_element_id,                  /* use newly-created forecast_element_id */
4912                           fl.project_id,
4913                           fl.task_id,
4914                           fl.period_name,
4915                           fl.start_date,
4916                           fl.end_date,
4917                           fl.quantity,
4918                           fl.raw_cost,
4919                           fl.burdened_cost,
4920                           fl.tp_cost_in,
4921                           fl.tp_cost_out,
4922                           fl.revenue,
4923                           fl.tp_revenue_in,
4924                           fl.tp_revenue_out,
4925                           1,                                              /* record_version_number */
4926                           fl.borrowed_revenue,
4927                           fl.lent_resource_cost,
4928                           fl.unassigned_time_cost,
4929                           p_target_version_id
4930                       from
4931                           pa_org_forecast_lines fl
4932                       where
4933                           fl.forecast_line_id=l_fl1_rec.forecast_line_id;
4934                   end loop; /* PA_ORG_FORECAST_LINES */
4935                   close l_fl_csr;
4936                   /* PA_PROJECT_PERIODS_DENORM: Insert a new row for every record whose budget_version_id and resource_assignment_id match our old ones */
4937                   /* key on OBJECT_ID = forecast_element_id of the original version */
4938                   IF P_PA_DEBUG_MODE = 'Y' THEN
4939                      pa_debug.write_file('Create_Org_Fcst_Elements: ' || 'inserting into pa_project_periods_denorm');
4940                   END IF;
4941                   insert into pa_proj_periods_denorm (
4942                       creation_date,
4943                       created_by,
4944                       last_update_login,
4945                       last_updated_by,
4946                       last_update_date,
4947                       budget_version_id,
4948                       resource_assignment_id,
4949                       object_id,
4950                       object_type_code,
4951                       period_profile_id,
4952                       amount_type_code,
4953                       amount_subtype_code,
4954                       amount_type_id,
4955                       amount_subtype_id,
4956                       currency_type,
4957                       currency_code,
4958                       preceding_periods_amount,
4959                       succeeding_periods_amount,
4960                       prior_period_amount,
4961                       period_amount1,
4962                       period_amount2,
4963                       period_amount3,
4964                       period_amount4,
4965                       period_amount5,
4966                       period_amount6,
4967                       period_amount7,
4968                       period_amount8,
4969                       period_amount9,
4970                       period_amount10,
4971                       period_amount11,
4972                       period_amount12,
4973                       period_amount13,
4974                       period_amount14,
4975                       period_amount15,
4976                       period_amount16,
4977                       period_amount17,
4978                       period_amount18,
4979                       period_amount19,
4980                       period_amount20,
4981                       period_amount21,
4982                       period_amount22,
4983                       period_amount23,
4984                       period_amount24,
4985                       period_amount25,
4986                       period_amount26,
4987                       period_amount27,
4988                       period_amount28,
4989                       period_amount29,
4990                       period_amount30,
4991                       period_amount31,
4992                       period_amount32,
4993                       period_amount33,
4994                       period_amount34,
4995                       period_amount35,
4996                       period_amount36,
4997                       period_amount37,
4998                       period_amount38,
4999                       period_amount39,
5000                       period_amount40,
5001                       period_amount41,
5002                       period_amount42,
5003                       period_amount43,
5004                       period_amount44,
5005                       period_amount45,
5006                       period_amount46,
5007                       period_amount47,
5008                       period_amount48,
5009                       period_amount49,
5010                       period_amount50,
5011                       period_amount51,
5012                       period_amount52,
5013                       project_id,
5014                       parent_assignment_id)
5015                   select
5016                       SYSDATE,                            /* creation_date */
5017                       FND_GLOBAL.user_id,                 /* created_by */
5018                       FND_GLOBAL.login_id,                /* last_update_login */
5019                       FND_GLOBAL.user_id,                 /* last_updated_by */
5020                       SYSDATE,                            /* last_update_date */
5021                       p_target_version_id,                /* use newly-created budget_version_id */
5022                       ppd.resource_assignment_id,         /* use the existing resource_assignment_id */
5023                       l_forecast_element_id,              /* object_id is the newly-created forecast_element_id */
5024                       ppd.object_type_code,
5025                       ppd.period_profile_id,
5026                       ppd.amount_type_code,
5027                       ppd.amount_subtype_code,
5028                       ppd.amount_type_id,
5029                       ppd.amount_subtype_id,
5030                       ppd.currency_type,
5031                       ppd.currency_code,
5032                       ppd.preceding_periods_amount,
5033                       ppd.succeeding_periods_amount,
5034                       ppd.prior_period_amount,
5035                       ppd.period_amount1,
5036                       ppd.period_amount2,
5037                       ppd.period_amount3,
5038                       ppd.period_amount4,
5039                       ppd.period_amount5,
5040                       ppd.period_amount6,
5041                       ppd.period_amount7,
5042                       ppd.period_amount8,
5043                       ppd.period_amount9,
5044                       ppd.period_amount10,
5045                       ppd.period_amount11,
5046                       ppd.period_amount12,
5047                       ppd.period_amount13,
5048                       ppd.period_amount14,
5049                       ppd.period_amount15,
5050                       ppd.period_amount16,
5051                       ppd.period_amount17,
5052                       ppd.period_amount18,
5053                       ppd.period_amount19,
5054                       ppd.period_amount20,
5055                       ppd.period_amount21,
5056                       ppd.period_amount22,
5057                       ppd.period_amount23,
5058                       ppd.period_amount24,
5059                       ppd.period_amount25,
5060                       ppd.period_amount26,
5061                       ppd.period_amount27,
5062                       ppd.period_amount28,
5063                       ppd.period_amount29,
5064                       ppd.period_amount30,
5065                       ppd.period_amount31,
5066                       ppd.period_amount32,
5067                       ppd.period_amount33,
5068                       ppd.period_amount34,
5069                       ppd.period_amount35,
5070                       ppd.period_amount36,
5071                       ppd.period_amount37,
5072                       ppd.period_amount38,
5073                       ppd.period_amount39,
5074                       ppd.period_amount40,
5075                       ppd.period_amount41,
5076                       ppd.period_amount42,
5077                       ppd.period_amount43,
5078                       ppd.period_amount44,
5079                       ppd.period_amount45,
5080                       ppd.period_amount46,
5081                       ppd.period_amount47,
5082                       ppd.period_amount48,
5083                       ppd.period_amount49,
5084                       ppd.period_amount50,
5085                       ppd.period_amount51,
5086                       ppd.period_amount52,
5087                       p_project_id, /* project_id */
5088                       NULL --as Org_Fcst doen't have rollup
5089                   from
5090                       pa_proj_periods_denorm ppd
5091                   where
5092                       ppd.budget_version_id = p_source_version_id and
5093                       ppd.object_id = l_fe_rec.forecast_element_id;
5094 
5095            end loop; /* l_fe_csr*/
5096            close l_fe_csr;
5097 
5098         pa_debug.reset_err_stack;
5099     end if;
5100 
5101 exception
5102 
5103     when others then
5104         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5105         x_msg_count     := 1;
5106         x_msg_data      := SQLERRM;
5107         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
5108                                  p_procedure_name   => 'Create_Org_Fcst_Elements');
5109         pa_debug.reset_err_stack;
5110         raise FND_API.G_EXC_UNEXPECTED_ERROR;
5111 
5112 end create_org_fcst_elements;
5113 
5114 
5115 /*=====================================================================
5116   Bug No. 2331201 For finplan this api has been added
5117   This api Creates a working fin plan version.The version inherits its
5118   properties from plan type.This api creates records in
5119   pa_proj_fp_options , pa_resource_assignments,
5120   pa_fp_txn_currencies and pa_budget_versions.
5121   If px_budget_version_id is passed it is assumed that new version
5122   id is created and passed to the api, else new value is created.
5123 
5124 --    26-JUN-2003 jwhite        - Plannable Task Dev Effort:
5125 --                                Make code changes to Create_Version procedure to
5126 --                                enable population of new parameters on
5127 --                                PA_FP_BUDGET_VERSIONS_PKG.Insert_Row table handler.
5128 
5129 --
5130 --    01-JUL-2003 jwhite        - bug 2989874
5131 --                                For Create_Version procedure, default ci from the current
5132 --                                working version.
5133 
5134 --    07-JUL-2003 jwhite        - bug 2989874:
5135 --                                As per IDC UT, made numerous modifications to the
5136 --                                Create_Version procedure.
5137 
5138 --    30-SEP-2003 rravipat      - bug 3165956
5139 --                                if p_time_phased_code is passed as
5140 --                                'P', l_time_phased_code should be 'PA'
5141 --                                'G', l_time_phased_code should be 'GL'
5142 --                                as this value is used to fetch period_profile_id
5143 
5144 --    31-MAY-2004 rravipat      - bug 3658232
5145 --                                For finplan, when new version is being created all
5146 --                                the setup should be inherited from parent
5147 --                                plan type. Resource assignments data should be
5148 --                                copied from current working version if planning
5149 --                                level and resource list match. Else default
5150 --                                planning elements are created
5151 
5152 --    21-SEP-2004 rravipat      - bug 3867302
5153 --                                For ci versions reporting data is not maintained
5154 --    06-DEC-2004 dlai    - bug 3831449
5155 --          do not create records in pa_resource_assignments
5156 --          if p_calling_context is GENERATE
5157 
5158 --    07-Nov-2005 dbora           Bug 4724017: Redone the fix done for bug 4534591
5159 --                                in 11.5 as CDM Enhancement.
5160 --                                Changes made to avoid creating default planning
5161 --                                transactions, if the version being created uses
5162 --                                a categorized resource list. This is a deviation
5163 --                                from the update 1-MAY-2004 rravipat - bug 3658232
5164 --                                Please look at the bug for details.
5165 =====================================================================*/
5166 
5167 PROCEDURE Create_Version (
5168     p_project_id                        IN     NUMBER
5169     ,p_fin_plan_type_id                 IN     NUMBER
5170     ,p_element_type                     IN     VARCHAR2
5171     ,p_version_name                     IN     VARCHAR2
5172     ,p_description                      IN     VARCHAR2
5173     -- Start of additional columns for Bug :- 2634900
5174     ,p_ci_id                            IN     pa_budget_versions.ci_id%TYPE                    --:= NULL
5175     ,p_est_proj_raw_cost                IN     pa_budget_versions.est_project_raw_cost%TYPE     --:= NULL
5176     ,p_est_proj_bd_cost                 IN     pa_budget_versions.est_project_burdened_cost%TYPE--:= NULL
5177     ,p_est_proj_revenue                 IN     pa_budget_versions.est_project_revenue%TYPE      --:= NULL
5178     ,p_est_qty                          IN     pa_budget_versions.est_quantity%TYPE             --:= NULL
5179     ,p_est_equip_qty                    IN     pa_budget_versions.est_equipment_quantity%TYPE   --:= NULL FP.M
5180     ,p_impacted_task_id                 IN     pa_tasks.task_id%TYPE                            --:= NULL
5181     ,p_agreement_id                     IN     pa_budget_versions.agreement_id%TYPE             --:= NULL
5182     ,p_calling_context                  IN     VARCHAR2                                         --:= NULL
5183     -- End of additional columns for Bug :- 2634900
5184     -- Start of additional columns for Bug :- 2649474
5185     ,p_resource_list_id                 IN     pa_budget_versions.resource_list_id%TYPE         --:= NULL
5186     ,p_time_phased_code                 IN     pa_proj_fp_options.cost_time_phased_code%TYPE    --:= NULL
5187     ,p_fin_plan_level_code              IN     pa_proj_fp_options.cost_fin_plan_level_code%TYPE --:= NULL
5188     ,p_plan_in_multi_curr_flag          IN     pa_proj_fp_options.plan_in_multi_curr_flag%TYPE  --:= NULL
5189     ,p_amount_set_id                    IN     pa_proj_fp_options.cost_amount_set_id%TYPE       --:= NULL
5190     -- End of additional columns for Bug :- 2649474
5191     -- Start of additional columns for Bug :- 3088010
5192     ,p_attribute_category               IN     pa_budget_versions.attribute_category%TYPE
5193     ,p_attribute1                       IN     pa_budget_versions.attribute1%TYPE
5194     ,p_attribute2                       IN     pa_budget_versions.attribute2%TYPE
5195     ,p_attribute3                       IN     pa_budget_versions.attribute3%TYPE
5196     ,p_attribute4                       IN     pa_budget_versions.attribute4%TYPE
5197     ,p_attribute5                       IN     pa_budget_versions.attribute5%TYPE
5198     ,p_attribute6                       IN     pa_budget_versions.attribute6%TYPE
5199     ,p_attribute7                       IN     pa_budget_versions.attribute7%TYPE
5200     ,p_attribute8                       IN     pa_budget_versions.attribute8%TYPE
5201     ,p_attribute9                       IN     pa_budget_versions.attribute9%TYPE
5202     ,p_attribute10                      IN     pa_budget_versions.attribute10%TYPE
5203     ,p_attribute11                      IN     pa_budget_versions.attribute11%TYPE
5204     ,p_attribute12                      IN     pa_budget_versions.attribute12%TYPE
5205     ,p_attribute13                      IN     pa_budget_versions.attribute13%TYPE
5206     ,p_attribute14                      IN     pa_budget_versions.attribute14%TYPE
5207     ,p_attribute15                      IN     pa_budget_versions.attribute15%TYPE
5208     -- End of additional columns for Bug :- 3088010
5209     ,px_budget_version_id               IN OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
5210     ,p_struct_elem_version_id           IN     pa_proj_element_versions.element_version_id%TYPE --For Bug 3354518
5211     ,p_pm_product_code                  IN pa_budget_versions.pm_product_code%TYPE DEFAULT NULL
5212     ,p_finplan_reference                IN pa_budget_versions.pm_budget_reference%TYPE DEFAULT NULL
5213     ,p_change_reason_code               IN pa_budget_versions.change_reason_code%TYPE DEFAULT NULL
5214     ,p_pji_rollup_required              IN VARCHAR2                                   DEFAULT 'Y'  --Bug 4200168
5215     ,x_proj_fp_option_id                   OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
5216     ,x_return_status                       OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
5217     ,x_msg_count                           OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
5218     ,x_msg_data                            OUT NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
5219 AS
5220 
5221     l_module_name varchar2(30):= 'pa.plsql.pa_fin_plan_pub';
5222 
5223      -- Start of variables used for debugging purpose
5224 
5225      l_msg_count          NUMBER :=0;
5226      l_data               VARCHAR2(2000);
5227      l_msg_data           VARCHAR2(2000);
5228      l_error_msg_code     VARCHAR2(30);
5229      l_msg_index_out      NUMBER;
5230      l_return_status      VARCHAR2(2000);
5231      l_debug_mode         VARCHAR2(30);
5232 
5233      -- End of variables used for debugging purpose
5234 
5235      l_max_version_number               pa_budget_versions.version_number%TYPE;
5236      l_new_budget_version_id            pa_budget_versions.budget_version_id%TYPE;
5237      l_current_working_flag             pa_budget_versions.current_working_flag%TYPE;
5238      l_dummy_version_id                 pa_budget_versions.budget_version_id%TYPE;
5239      l_new_proj_fp_options_id           pa_proj_fp_options.proj_fp_options_id%TYPE;
5240      l_project_currency_code            pa_projects_all.project_currency_code%TYPE;
5241      l_projfunc_currency_code           pa_projects_all.projfunc_currency_code%TYPE;
5242      l_dummy_currency_code              pa_projects_all.projfunc_currency_code%TYPE;
5243      l_est_projfunc_raw_cost            pa_budget_versions.est_projfunc_raw_cost%TYPE;
5244      l_est_projfunc_bd_cost             pa_budget_versions.est_projfunc_burdened_cost%TYPE;
5245      l_est_projfunc_revenue             pa_budget_versions.est_projfunc_revenue%TYPE;
5246      l_est_project_raw_cost             pa_budget_versions.est_project_raw_cost%TYPE;
5247      l_est_project_bd_cost              pa_budget_versions.est_project_burdened_cost%TYPE;
5248      l_est_project_revenue              pa_budget_versions.est_project_revenue%TYPE;
5249      l_agreement_num                    pa_agreements_all.agreement_num%TYPE;
5250      l_agreement_amount                 pa_agreements_all.amount%TYPE;
5251      l_agreement_currency_code          pa_agreements_all.agreement_currency_code%TYPE;
5252 
5253      --l_mixed_resource_planned_flag      VARCHAR2(1); --Added for Bug:-2625872
5254 
5255      l_plan_type_mc_flag          pa_proj_fp_options.plan_in_multi_curr_flag%TYPE;  --Bug 2661237
5256 
5257      /* Project level conversion attributes var for bug 2661237 */
5258 
5259         l_multi_currency_billing_flag           pa_projects_all.multi_currency_billing_flag%TYPE;
5260         l_baseline_funding_flag                 pa_projects_all.baseline_funding_flag%TYPE;
5261         l_revproc_currency_code                 pa_projects_all.revproc_currency_code%TYPE;
5262         l_invproc_currency_type                 pa_projects_all.invproc_currency_type%TYPE;
5263         l_invproc_currency_code                 pa_projects_all.revproc_currency_code%TYPE;
5264         l_project_bil_rate_date_code            pa_projects_all.project_bil_rate_date_code%TYPE;
5265         l_project_bil_rate_type                 pa_projects_all.project_bil_rate_type%TYPE;
5266         l_project_bil_rate_date                 pa_projects_all.project_bil_rate_date%TYPE;
5267         l_project_bil_exchange_rate             pa_projects_all.project_bil_exchange_rate%TYPE;
5268         l_projfunc_bil_rate_date_code           pa_projects_all.projfunc_bil_rate_date_code%TYPE;
5269         l_projfunc_bil_rate_type                pa_projects_all.projfunc_bil_rate_type%TYPE;
5270         l_projfunc_bil_rate_date                pa_projects_all.projfunc_bil_rate_date%TYPE;
5271         l_projfunc_bil_exchange_rate            pa_projects_all.projfunc_bil_exchange_rate%TYPE;
5272         l_funding_rate_date_code                pa_projects_all.funding_rate_date_code%TYPE;
5273         l_funding_rate_type                     pa_projects_all.funding_rate_type%TYPE;
5274         l_funding_rate_date                     pa_projects_all.funding_rate_date%TYPE;
5275         l_funding_exchange_rate                 pa_projects_all.funding_exchange_rate%TYPE;
5276 
5277       /* End of variable definition for bug 2661237 */
5278 
5279      l_res_list_uncategorized_flag   VARCHAR2(1);
5280      l_res_list_control_flag         VARCHAR2(1);
5281      l_gl_start_period               gl_periods.period_name%TYPE;
5282      l_gl_end_period                 gl_periods.period_name%TYPE;
5283      l_gl_start_Date                 VARCHAR2(100);
5284      l_pa_start_period               pa_periods_all.period_name%TYPE;
5285      l_pa_end_period                 pa_periods_all.period_name%TYPE;
5286      l_pa_start_date                 VARCHAR2(100);
5287      l_plan_version_exists_flag      VARCHAR2(1);
5288      l_prj_start_date                VARCHAR2(100);
5289      l_prj_end_date                  VARCHAR2(100);
5290      l_budget_version_ids            SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
5291 
5292      -- 01-JUL-2003 Default ci from current working version (bug 2989874)
5293        l_ci_apprv_cw_bv_id              pa_budget_versions.budget_version_id%TYPE  :=NULL;
5294 
5295       -- Added by pseethar. Used for workplan flag, derivered from pa_fin_plan_types_b
5296        l_WP_VERSION_FLAG   pa_fin_plan_types_b.USE_FOR_WORKPLAN_FLAG%TYPE;
5297 
5298 CURSOR  plan_type_info_cur (
5299   c_project_id       NUMBER,
5300   c_fin_plan_type_id NUMBER )IS
5301 SELECT  proj_fp_options_id
5302        ,project_id
5303        ,fin_plan_option_level_code
5304        ,fin_plan_preference_code
5305        ,plan_in_multi_curr_flag
5306        ,approved_cost_plan_type_flag
5307        ,approved_rev_plan_type_flag
5308        ,all_fin_plan_level_code
5309        ,all_time_phased_code
5310        ,all_resource_list_id
5311        ,all_amount_set_id
5312        ,all_current_planning_period
5313        ,all_period_mask_id
5314        ,RBS_VERSION_ID
5315        ,select_all_res_auto_flag
5316        ,cost_fin_plan_level_code
5317        ,cost_time_phased_code
5318        ,cost_resource_list_id
5319        ,cost_amount_set_id
5320        ,select_cost_res_auto_flag
5321        ,cost_current_planning_period
5322        ,cost_period_mask_id
5323        ,revenue_fin_plan_level_code
5324        ,revenue_resource_list_id
5325        ,revenue_time_phased_code
5326        ,revenue_amount_set_id
5327        ,select_rev_res_auto_flag
5328        ,primary_cost_forecast_flag
5329        ,primary_rev_forecast_flag
5330        ,rev_current_planning_period
5331        ,rev_period_mask_id
5332 FROM   pa_proj_fp_options
5333 WHERE  project_id = c_project_id
5334   AND  fin_plan_type_id = c_fin_plan_type_id
5335   AND  fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_TYPE;
5336 
5337 plan_type_info_rec plan_type_info_cur%ROWTYPE;
5338 
5339 CURSOR  plan_version_info_cur (
5340   c_project_id       NUMBER,
5341   c_fin_plan_type_id NUMBER,
5342   c_fin_plan_version_id NUMBER  )IS
5343 SELECT   pfo.proj_fp_options_id
5344        , pfo.project_id
5345        , pfo.fin_plan_option_level_code
5346        , pfo.fin_plan_preference_code
5347        , pfo.plan_in_multi_curr_flag
5348        , pfo.approved_cost_plan_type_flag
5349        , pfo.approved_rev_plan_type_flag
5350        , pfo.all_fin_plan_level_code
5351        , pfo.all_time_phased_code
5352        , pfo.all_resource_list_id
5353        , pfo.all_amount_set_id
5354        , pfo.all_current_planning_period
5355        , pfo.all_period_mask_id
5356        , pfo.rbs_version_id
5357        , pfo.select_all_res_auto_flag
5358        , pfo.cost_fin_plan_level_code
5359        , pfo.cost_time_phased_code
5360        , pfo.cost_resource_list_id
5361        , pfo.cost_amount_set_id
5362        , pfo.select_cost_res_auto_flag
5363        , pfo.cost_current_planning_period
5364        , pfo.cost_period_mask_id
5365        , pfo.revenue_fin_plan_level_code
5366        , pfo.revenue_resource_list_id
5367        , pfo.revenue_time_phased_code
5368        , pfo.revenue_amount_set_id
5369        , pfo.select_rev_res_auto_flag
5370        , pfo.rev_current_planning_period
5371        , pfo.rev_period_mask_id
5372        , pfo.primary_cost_forecast_flag
5373        , pfo.primary_rev_forecast_flag
5374        , bv.actual_amts_thru_period
5375        , bv.project_structure_version_id
5376 FROM   pa_proj_fp_options pfo, pa_budget_versions bv
5377 WHERE  pfo.project_id = c_project_id
5378   AND  pfo.fin_plan_type_id = c_fin_plan_type_id
5379   AND  pfo.fin_plan_version_id = c_fin_plan_version_id
5380   AND  bv.budget_version_id =  c_fin_plan_version_id
5381   AND  fin_plan_option_level_code = PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION;
5382 
5383 plan_version_info_rec  plan_version_info_cur%ROWTYPE;
5384 
5385 TYPE new_version_rec_type IS RECORD (
5386    fin_plan_option_level_code     PA_PROJ_FP_OPTIONS.fin_plan_option_level_code%TYPE
5387   ,fin_plan_preference_code       PA_PROJ_FP_OPTIONS.fin_plan_preference_code%TYPE
5388   ,fin_plan_level_code            PA_PROJ_FP_OPTIONS.cost_fin_plan_level_code%TYPE
5389   ,time_phased_code               PA_PROJ_FP_OPTIONS.cost_time_phased_code%TYPE
5390   ,resource_list_id               PA_PROJ_FP_OPTIONS.cost_resource_list_id%TYPE
5391   ,amount_set_id                  PA_PROJ_FP_OPTIONS.cost_amount_set_id%TYPE
5392   ,current_planning_period        PA_PROJ_FP_OPTIONS.cost_CURRENT_PLANNING_PERIOD%TYPE
5393   ,period_mask_id                 PA_PROJ_FP_OPTIONS.cost_PERIOD_MASK_ID%TYPE
5394   ,plan_in_multi_curr_flag        PA_PROJ_FP_OPTIONS.plan_in_multi_curr_flag%TYPE
5395   ,approved_cost_plan_type_flag   PA_PROJ_FP_OPTIONS.approved_cost_plan_type_flag%TYPE
5396   ,approved_rev_plan_type_flag    PA_PROJ_FP_OPTIONS.approved_rev_plan_type_flag%TYPE
5397   ,select_res_auto_flag           PA_PROJ_FP_OPTIONS.select_cost_res_auto_flag%TYPE
5398   ,source_fp_options_id           PA_PROJ_FP_OPTIONS.PROJ_FP_OPTIONS_ID%TYPE
5399   ,version_type                   PA_BUDGET_VERSIONS.version_type%TYPE
5400   ,project_structure_version_id   PA_BUDGET_VERSIONS.project_structure_version_id%TYPE
5401   ,rbs_version_id                 PA_PROJ_FP_OPTIONS.rbs_version_id%TYPE
5402   ,primary_cost_forecast_flag     PA_BUDGET_VERSIONS.primary_cost_forecast_flag%TYPE
5403   ,primary_rev_forecast_flag      PA_BUDGET_VERSIONS.primary_rev_forecast_flag%TYPE
5404   ,actual_amts_thru_period        PA_BUDGET_VERSIONS.actual_amts_thru_period%TYPE := NULL
5405   );
5406 
5407 new_version_info_rec    new_version_rec_type;
5408 
5409 l_curr_work_ver_exists_flag         VARCHAR2(1);
5410 l_fp_options_id                     pa_proj_fp_options.proj_fp_options_id%TYPE;
5411 l_fin_plan_version_id               pa_proj_fp_options.fin_plan_version_id%TYPE;
5412 l_cw_fin_plan_level_code            VARCHAR2(30);
5413 l_cw_ver_res_list_id                NUMBER;
5414 l_row_id                            rowid;
5415 l_copy_res_assmt_from_cwv_flag      VARCHAR2(1);
5416      -- ---------------------------------------------------------------------------
5417 l_src_bv_id_for_copying_ra          pa_budget_versions.budget_version_id%TYPE;
5418 
5419 l_txn_source_id_tbl          SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
5420 l_res_list_member_id_tbl     SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
5421 l_rbs_element_id_tbl         SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
5422 l_txn_accum_header_id_tbl    SYSTEM.pa_num_tbl_type := SYSTEM.pa_num_tbl_type();
5423 l_pji_rollup_required       VARCHAR2(1);
5424 
5425 
5426 
5427 BEGIN
5428 
5429      FND_MSG_PUB.initialize;
5430      fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
5431      l_debug_mode := NVL(l_debug_mode, 'Y');
5432      pa_debug.set_curr_function( p_function => 'Create_Version',
5433                                  p_debug_mode => l_debug_mode );
5434      x_msg_count := 0;
5435      x_return_status := FND_API.G_RET_STS_SUCCESS;
5436      -- Check for business rules violations
5437 
5438 --Added this if for the bug 4200168
5439     IF p_pji_rollup_required = 'Y' THEN
5440         l_pji_rollup_required := 'Y';
5441     ELSE
5442         l_pji_rollup_required := 'N';
5443     END IF;
5444 
5445 
5446      IF P_PA_DEBUG_MODE = 'Y' THEN
5447          pa_debug.g_err_stage:='Validating input parameters';
5448          pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5449      END IF;
5450 
5451      -- Check if source and target fp option ids are null
5452 
5453      IF (p_project_id       IS NULL) OR
5454         (p_fin_plan_type_id IS NULL) OR
5455         (p_version_name IS NULL)
5456      THEN
5457 
5458          IF P_PA_DEBUG_MODE = 'Y' THEN
5459              pa_debug.g_err_stage:='Project_id = '||p_project_id;
5460              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5461              pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
5462              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5463              pa_debug.g_err_stage:='Version_name = '||p_version_name;
5464              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5465              pa_debug.g_err_stage:='Description = '||p_description;
5466              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5467          END IF;
5468 
5469          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
5470                               p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
5471 
5472 
5473          IF P_PA_DEBUG_MODE = 'Y' THEN
5474              pa_debug.g_err_stage:='Invalid Arguments Passed';
5475              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5476          END IF;
5477          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5478 
5479      END IF;
5480 
5481      --Bug 3354518. If the calling context is WORKPLAN and if p_struct_elem_version_id IS NULL then
5482      --Throw error
5483      IF (p_calling_context =PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN AND
5484          p_struct_elem_version_id IS NULL) THEN
5485 
5486          IF P_PA_DEBUG_MODE = 'Y' THEN
5487              pa_debug.g_err_stage:='p_struct_elem_version_id in workplan context is'||p_struct_elem_version_id;
5488              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5489          END IF;
5490          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
5491                                p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
5492          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5493 
5494      END IF;
5495 
5496      --Fetch plan type values
5497 
5498      IF P_PA_DEBUG_MODE = 'Y' THEN
5499          pa_debug.g_err_stage:='Fetching plan type properties';
5500          pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5501      END IF;
5502 
5503      OPEN plan_type_info_cur(p_project_id,p_fin_plan_type_id);
5504      FETCH plan_type_info_cur INTO plan_type_info_rec;
5505      CLOSE plan_type_info_cur;
5506 
5507      --Raise an error if element type isn't passed for cost and rev separate plan type
5508 
5509      IF ( plan_type_info_rec.fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) AND
5510         (p_element_type IS NULL )
5511      THEN
5512 
5513          IF P_PA_DEBUG_MODE = 'Y' THEN
5514              pa_debug.g_err_stage:='Element_type input can not be null for this plan type';
5515              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
5516          END IF;
5517 
5518          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
5519                               p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
5520          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5521 
5522      END IF;
5523 
5524      IF (p_element_type IS NULL) THEN
5525 
5526           IF    plan_type_info_rec.fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY THEN
5527 
5528                 new_version_info_rec.version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST;
5529 
5530           ELSIF plan_type_info_rec.fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY THEN
5531 
5532                 new_version_info_rec.version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE;
5533 
5534           ELSIF plan_type_info_rec.fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SAME THEN
5535 
5536                 new_version_info_rec.version_type := PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL;
5537 
5538           END IF;
5539      ELSE
5540           new_version_info_rec.version_type := p_element_type;
5541      END IF;
5542 
5543      IF P_PA_DEBUG_MODE = 'Y' THEN
5544         pa_debug.g_err_stage:='Element_type = '||new_version_info_rec.version_type;
5545         pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5546      END IF;
5547 
5548      IF ( p_ci_id IS NOT NULL )
5549      THEN
5550          -- Fetch current working approved budget version id
5551          Pa_Fp_Control_Items_Utils.CHK_APRV_CUR_WORKING_BV_EXISTS(
5552                   p_project_id       => p_project_id,
5553                   p_fin_plan_type_id => p_fin_plan_type_id,
5554                   p_version_type     => new_version_info_rec.version_type,
5555                   x_cur_work_bv_id   => l_ci_apprv_cw_bv_id,
5556                   x_return_status    => l_return_status,
5557                   x_msg_count        => l_msg_count,
5558                   x_msg_data         => l_msg_data );
5559          IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5560              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5561          END IF;
5562 
5563          OPEN plan_version_info_cur (p_project_id,p_fin_plan_type_id, l_ci_apprv_cw_bv_id);
5564          FETCH plan_version_info_cur INTO plan_version_info_rec;
5565          CLOSE plan_version_info_cur;
5566 
5567          l_curr_work_ver_exists_flag :='Y';
5568 
5569      ELSE
5570          pa_fin_plan_utils. Get_Curr_Working_Version_Info(
5571                    p_project_id          => p_project_id
5572                   ,p_fin_plan_type_id    => p_fin_plan_type_id
5573                   ,p_version_type        => p_element_type
5574                   ,x_fp_options_id       => l_fp_options_id
5575                   ,x_fin_plan_version_id => l_fin_plan_version_id
5576                   ,x_return_status       => l_return_status
5577                   ,x_msg_count           => l_msg_count
5578                   ,x_msg_data            => l_msg_data );
5579 
5580          IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5581              RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5582          END IF;
5583 
5584          IF ( l_fin_plan_version_id IS NOT NULL )
5585          THEN
5586              OPEN plan_version_info_cur (p_project_id,p_fin_plan_type_id, l_fin_plan_version_id);
5587              FETCH plan_version_info_cur INTO plan_version_info_rec;
5588              CLOSE plan_version_info_cur;
5589 
5590              l_curr_work_ver_exists_flag := 'Y';
5591 
5592          ELSE
5593              l_curr_work_ver_exists_flag := 'N';
5594          END IF;
5595      END IF;
5596 
5597      IF P_PA_DEBUG_MODE = 'Y' THEN
5598          pa_debug.g_err_stage:='Parameter validation complete';
5599          pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5600      END IF;
5601 
5602      -- Derive fin_plan_level_code, resource list id plan version based on element type
5603      IF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST )
5604      THEN
5605          l_cw_fin_plan_level_code  :=  plan_version_info_rec.cost_fin_plan_level_code;
5606          l_cw_ver_res_list_id      :=  plan_version_info_rec.cost_resource_list_id;
5607      ELSIF new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE
5608      THEN
5609          l_cw_fin_plan_level_code  :=  plan_version_info_rec.revenue_fin_plan_level_code;
5610          l_cw_ver_res_list_id      :=  plan_version_info_rec.revenue_resource_list_id;
5611      ELSE
5612          l_cw_fin_plan_level_code  :=  plan_version_info_rec.all_fin_plan_level_code;
5613          l_cw_ver_res_list_id      :=  plan_version_info_rec.all_resource_list_id;
5614      END IF;
5615 
5616      -- Bug 3658080 copy options info from current working version only for ci versions
5617      IF (p_ci_id is not null)   AND
5618         (NVL(p_calling_context,'-99') <> PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN)
5619      THEN
5620          IF P_PA_DEBUG_MODE = 'Y' THEN
5621              pa_debug.g_err_stage:='Assigning values to new_version_info_rec' ;
5622              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5623          END IF;
5624 
5625         -- bug 3658080 l_copy_res_assmt_from_cwv_flag := 'Y';
5626 
5627          new_version_info_rec.fin_plan_option_level_code := plan_version_info_rec.fin_plan_option_level_code;
5628          new_version_info_rec.fin_plan_preference_code := plan_version_info_rec.fin_plan_preference_code;
5629          new_version_info_rec.plan_in_multi_curr_flag := plan_version_info_rec.plan_in_multi_curr_flag;
5630          new_version_info_rec.approved_cost_plan_type_flag := plan_version_info_rec.approved_cost_plan_type_flag;
5631          new_version_info_rec.approved_rev_plan_type_flag := plan_version_info_rec.approved_rev_plan_type_flag;
5632          new_version_info_rec.primary_cost_forecast_flag := plan_version_info_rec.primary_cost_forecast_flag;
5633          new_version_info_rec.primary_rev_forecast_flag := plan_version_info_rec.primary_rev_forecast_flag;
5634          new_version_info_rec.project_structure_version_id := plan_version_info_rec.project_structure_version_id;
5635          new_version_info_rec.source_fp_options_id := plan_version_info_rec.proj_fp_options_id;
5636 
5637          IF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST )
5638          THEN
5639 
5640              IF P_PA_DEBUG_MODE = 'Y' THEN
5641                  pa_debug.g_err_stage:='Element type is COST'||new_version_info_rec.version_type ;
5642                  pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5643              END IF;
5644 
5645            new_version_info_rec.fin_plan_level_code :=  plan_version_info_rec.cost_fin_plan_level_code;
5646              new_version_info_rec.time_phased_code    :=  plan_version_info_rec.cost_time_phased_code;
5647              new_version_info_rec.resource_list_id    :=  plan_version_info_rec.cost_resource_list_id;
5648              new_version_info_rec.amount_set_id       :=  plan_version_info_rec.cost_amount_set_id;
5649              new_version_info_rec.select_res_auto_flag := plan_version_info_rec.select_cost_res_auto_flag;
5650              new_version_info_rec.CURRENT_PLANNING_PERIOD := plan_version_info_rec.cost_CURRENT_PLANNING_PERIOD;
5651              new_version_info_rec.PERIOD_MASK_ID      :=  plan_version_info_rec.cost_PERIOD_MASK_ID;
5652 
5653          ELSIF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE ) THEN
5654 
5655              IF P_PA_DEBUG_MODE = 'Y' THEN
5656                  pa_debug.g_err_stage:='Element type is REVENUE'||new_version_info_rec.version_type ;
5657                  pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5658              END IF;
5659 
5660              new_version_info_rec.fin_plan_level_code := plan_version_info_rec.revenue_fin_plan_level_code;
5661              new_version_info_rec.time_phased_code := plan_version_info_rec.revenue_time_phased_code;
5662              new_version_info_rec.resource_list_id  :=   plan_version_info_rec.revenue_resource_list_id;
5663              new_version_info_rec.amount_set_id :=   plan_version_info_rec.revenue_amount_set_id;
5664              new_version_info_rec.select_res_auto_flag := plan_version_info_rec.select_rev_res_auto_flag;
5665              new_version_info_rec.CURRENT_PLANNING_PERIOD := plan_version_info_rec.rev_CURRENT_PLANNING_PERIOD;
5666              new_version_info_rec.PERIOD_MASK_ID :=  plan_version_info_rec.rev_PERIOD_MASK_ID;
5667 
5668          ELSE
5669 
5670              IF P_PA_DEBUG_MODE = 'Y' THEN
5671                  pa_debug.g_err_stage:='Element type is ALL'||new_version_info_rec.version_type ;
5672                  pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5673              END IF;
5674 
5675              new_version_info_rec.fin_plan_level_code := plan_version_info_rec.all_fin_plan_level_code;
5676              new_version_info_rec.time_phased_code := plan_version_info_rec.all_time_phased_code;
5677              new_version_info_rec.resource_list_id  :=   plan_version_info_rec.all_resource_list_id;
5678              new_version_info_rec.amount_set_id :=   plan_version_info_rec.all_amount_set_id;
5679              new_version_info_rec.select_res_auto_flag := plan_version_info_rec.select_all_res_auto_flag;
5680              new_version_info_rec.CURRENT_PLANNING_PERIOD := plan_version_info_rec.all_CURRENT_PLANNING_PERIOD;
5681              new_version_info_rec.PERIOD_MASK_ID :=  plan_version_info_rec.all_PERIOD_MASK_ID;
5682 
5683          END IF;
5684 
5685      ELSE
5686          new_version_info_rec.fin_plan_option_level_code := PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION ;
5687 
5688          -- bug 3658080 l_copy_res_assmt_from_cwv_flag:='N';
5689 
5690          -- If plan type's preference code is cost and rev sep then use
5691          -- new_version_info_rec.version_type to derive preference code
5692          -- for plan version.
5693 
5694          IF ( plan_type_info_rec.fin_plan_preference_code = PA_FP_CONSTANTS_PKG.G_PREF_COST_AND_REV_SEP) THEN
5695              IF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST ) THEN
5696                  new_version_info_rec.fin_plan_preference_code := PA_FP_CONSTANTS_PKG.G_PREF_COST_ONLY;
5697              ELSIF new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE THEN
5698                  new_version_info_rec.fin_plan_preference_code := PA_FP_CONSTANTS_PKG.G_PREF_REVENUE_ONLY;
5699              END IF;
5700          ELSE
5701              --If not then plan version preference code would be same as
5702              --plan type's preference code
5703 
5704              new_version_info_rec.fin_plan_preference_code := plan_type_info_rec.fin_plan_preference_code;
5705          END IF;
5706 
5707          new_version_info_rec.plan_in_multi_curr_flag := plan_type_info_rec.plan_in_multi_curr_flag;
5708          new_version_info_rec.approved_cost_plan_type_flag := plan_type_info_rec.approved_cost_plan_type_flag;
5709          new_version_info_rec.approved_rev_plan_type_flag := plan_type_info_rec.approved_rev_plan_type_flag;
5710          new_version_info_rec.primary_cost_forecast_flag := plan_type_info_rec.primary_cost_forecast_flag;
5711          new_version_info_rec.primary_rev_forecast_flag := plan_type_info_rec.primary_rev_forecast_flag;
5712          new_version_info_rec.project_structure_version_id := NULL;
5713          new_version_info_rec.source_fp_options_id := plan_type_info_rec.proj_fp_options_id;
5714 
5715          IF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST )
5716          THEN
5717              new_version_info_rec.fin_plan_level_code := plan_type_info_rec.cost_fin_plan_level_code;
5718              new_version_info_rec.time_phased_code := plan_type_info_rec.cost_time_phased_code;
5719              new_version_info_rec.resource_list_id  :=   plan_type_info_rec.cost_resource_list_id;
5720              new_version_info_rec.amount_set_id :=   plan_type_info_rec.cost_amount_set_id;
5721              new_version_info_rec.select_res_auto_flag := plan_type_info_rec.select_cost_res_auto_flag;
5722              new_version_info_rec.CURRENT_PLANNING_PERIOD := plan_type_info_rec.cost_CURRENT_PLANNING_PERIOD;
5723              new_version_info_rec.PERIOD_MASK_ID :=  plan_type_info_rec.cost_PERIOD_MASK_ID;
5724          ELSIF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE ) THEN
5725              new_version_info_rec.fin_plan_level_code := plan_type_info_rec.revenue_fin_plan_level_code;
5726              new_version_info_rec.time_phased_code := plan_type_info_rec.revenue_time_phased_code;
5727              new_version_info_rec.resource_list_id  :=   plan_type_info_rec.revenue_resource_list_id;
5728              new_version_info_rec.amount_set_id :=   plan_type_info_rec.revenue_amount_set_id;
5729              new_version_info_rec.select_res_auto_flag := plan_type_info_rec.select_rev_res_auto_flag;
5730              new_version_info_rec.CURRENT_PLANNING_PERIOD := plan_type_info_rec.rev_CURRENT_PLANNING_PERIOD;
5731              new_version_info_rec.PERIOD_MASK_ID :=  plan_type_info_rec.rev_PERIOD_MASK_ID;
5732          ELSE
5733              new_version_info_rec.fin_plan_level_code := plan_type_info_rec.all_fin_plan_level_code;
5734              new_version_info_rec.time_phased_code := plan_type_info_rec.all_time_phased_code;
5735              new_version_info_rec.resource_list_id  :=   plan_type_info_rec.all_resource_list_id;
5736              new_version_info_rec.amount_set_id :=   plan_type_info_rec.all_amount_set_id;
5737              new_version_info_rec.select_res_auto_flag := plan_type_info_rec.select_all_res_auto_flag;
5738              new_version_info_rec.CURRENT_PLANNING_PERIOD := plan_type_info_rec.all_CURRENT_PLANNING_PERIOD;
5739              new_version_info_rec.PERIOD_MASK_ID :=  plan_type_info_rec.all_PERIOD_MASK_ID;
5740          END IF;
5741 
5742      END IF;
5743 
5744      /* Bug# 2637789 */
5745      IF ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST ) THEN
5746          new_version_info_rec.approved_rev_plan_type_flag := 'N';
5747          new_version_info_rec.primary_rev_forecast_flag := 'N';
5748      ELSIF  ( new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE ) THEN
5749          new_version_info_rec.approved_cost_plan_type_flag := 'N';
5750          new_version_info_rec.primary_cost_forecast_flag := 'N';
5751      END IF;
5752 
5753      IF ( p_fin_plan_level_code IS NOT NULL )
5754      THEN
5755          new_version_info_rec.fin_plan_level_code := p_fin_plan_level_code;
5756      END IF;
5757 
5758      l_plan_type_mc_flag := new_version_info_rec.plan_in_multi_curr_flag;
5759 
5760      IF ( p_plan_in_multi_curr_flag IS NOT NULL )
5761      THEN
5762          new_version_info_rec.plan_in_multi_curr_flag := p_plan_in_multi_curr_flag;
5763      END IF;
5764 
5765      IF ( p_resource_list_id IS NOT NULL )
5766      THEN
5767 
5768          new_version_info_rec.resource_list_id := p_resource_list_id;
5769 
5770          IF P_PA_DEBUG_MODE = 'Y' THEN
5771              pa_debug.g_err_stage:='p_resource_list_id = '|| p_resource_list_id;
5772              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5773 
5774              pa_debug.g_err_stage:='fetching control flag and uncategorized flag for res list: '|| p_resource_list_id;
5775              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5776          END IF;
5777 
5778          -- Add resources automatically flag should be y only for project specific res lists
5779          SELECT nvl(control_flag,'Y'),
5780                 nvl(uncategorized_flag,'N')
5781          INTO   l_res_list_control_flag,
5782                 l_res_list_uncategorized_flag
5783          FROM   pa_resource_lists_all_bg
5784          WHERE  resource_list_id = p_resource_list_id;
5785 
5786          IF (l_res_list_control_flag = 'Y' OR l_res_list_uncategorized_flag = 'Y')
5787          THEN
5788 
5789              new_version_info_rec.select_res_auto_flag  := 'N';
5790 
5791          ELSE
5792              IF P_PA_DEBUG_MODE = 'Y' THEN
5793                  pa_debug.g_err_stage:='Calling PA_CREATE_RESOURCE.CREATE_PROJ_RESOURCE_LIST for res list: '|| p_resource_list_id;
5794                  pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5795              END IF;
5796              -- Call create_proj_resource_list api for this resource  list
5797 
5798              PA_CREATE_RESOURCE.CREATE_PROJ_RESOURCE_LIST (
5799                     p_project_id            =>   p_project_id
5800                    ,p_resource_list_id      =>   p_resource_list_id
5801                    ,x_return_status         =>   x_return_status
5802                    ,x_msg_count             =>   x_msg_count
5803                    ,x_error_msg_data        =>   x_msg_data );
5804 
5805              IF  x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5806                  RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5807              END IF;
5808          END IF;
5809      ELSE -- added for bug 4724017
5810          -- this code block would be executed for all the flows other than the AMG flow.
5811          -- the uncategorized info is required to call pa_fp_planning_transaction_pub.create_default_task_plan_txns
5812          -- conditionally only for uncategorized resource lists only.
5813          IF P_PA_DEBUG_MODE = 'Y' THEN
5814              pa_debug.g_err_stage:='Fetching uncategorized flag when resource list id is not passed';
5815              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5816          END IF;
5817 
5818          BEGIN
5819             SELECT nvl(uncategorized_flag,'N')
5820             INTO   l_res_list_uncategorized_flag
5821             FROM   pa_resource_lists_all_bg
5822             WHERE  resource_list_id = new_version_info_rec.resource_list_id;
5823 
5824             IF P_PA_DEBUG_MODE = 'Y' THEN
5825                 pa_debug.g_err_stage:='l_res_list_uncategorized_flag: ' || l_res_list_uncategorized_flag;
5826                 pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5827             END IF;
5828          EXCEPTION
5829             WHEN NO_DATA_FOUND THEN
5830                 IF P_PA_DEBUG_MODE = 'Y' THEN
5831                     pa_debug.g_err_stage:='No uncategorized flag found for the resource list id passed';
5832                     pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5833                 END IF;
5834                 RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5835          END; -- bug 4724017 ends.
5836      END IF;
5837 
5838      IF p_time_phased_code IS NOT NULL
5839      THEN
5840          -- If input time phase code is different from time phased code value of new_version_info_rec
5841          -- time phased code , period mask id and current_planning_period should not be defaulted
5842 
5843          IF  p_time_phased_code <> new_version_info_rec.time_phased_code
5844          THEN
5845              new_version_info_rec.time_phased_code :=  p_time_phased_code;
5846              IF p_time_phased_code IN ('N')
5847              THEN
5848                  -- Current planning period and period mask id should be null
5849                  new_version_info_rec.current_planning_period := NULL;
5850                  new_version_info_rec.period_mask_id :=  NULL;
5851 
5852              ELSIF p_time_phased_code IN ('G', 'P')
5853              THEN
5854                  -- Derive default current planning period and current period mask
5855                  Pa_Prj_Period_Profile_Utils.Get_Prj_Defaults(
5856                        p_project_id                 => p_project_id
5857                       ,p_info_flag                  => 'ALL'
5858                       ,p_create_defaults            => 'N'
5859                       ,x_gl_start_period            => l_gl_start_period
5860                       ,x_gl_end_period              => l_gl_end_period
5861                       ,x_gl_start_Date              => l_gl_start_Date
5862                       ,x_pa_start_period            => l_pa_start_period
5863                       ,x_pa_end_period              => l_pa_end_period
5864                       ,x_pa_start_date              => l_pa_start_date
5865                       ,x_plan_version_exists_flag   => l_plan_version_exists_flag
5866                       ,x_prj_start_date             => l_prj_start_date
5867                       ,x_prj_end_date               => l_prj_end_date);
5868 
5869                  IF  p_time_phased_code = 'P'
5870                  THEN
5871                      new_version_info_rec.period_mask_id :=  2;
5872                      new_version_info_rec.current_planning_period := l_pa_start_period;
5873                  ELSIF p_time_phased_code = 'G'
5874                  THEN
5875                      new_version_info_rec.period_mask_id :=  1;
5876                      new_version_info_rec.current_planning_period := l_gl_start_period;
5877                  END IF;
5878              END IF;
5879          END IF;
5880      END IF; -- p_time_phased_code is not null
5881 
5882      IF P_PA_DEBUG_MODE = 'Y' THEN
5883         pa_debug.g_err_stage:='new_version_info_rec.resource_list_id = '|| new_version_info_rec.resource_list_id || 'new_version_info_rec.time_phased_code = ' || new_version_info_rec.time_phased_code;
5884         pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5885      END IF;
5886 
5887      --Start of changes for Bug :- 2570250
5888 
5889      --Fetch the MAX  working version for this plan type
5890 
5891      IF P_PA_DEBUG_MODE = 'Y' THEN
5892         pa_debug.g_err_stage:='Fetching max working version number for this plan type';
5893         pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5894      END IF;
5895 
5896      PA_FIN_PLAN_UTILS.Get_Max_Budget_Version_Number
5897         (p_project_id          =>   p_project_id
5898         ,p_fin_plan_type_id    =>   p_fin_plan_type_id
5899         ,p_version_type        =>   new_version_info_rec.version_type
5900         ,p_copy_mode           =>   PA_FP_CONSTANTS_PKG.G_BUDGET_STATUS_WORKING
5901         ,p_ci_id               =>   p_ci_id
5902         ,p_lock_required_flag  =>   'Y'
5903         ,x_version_number      =>   l_max_version_number
5904         ,x_return_status       =>   x_return_status
5905         ,x_msg_count           =>   x_msg_count
5906         ,x_msg_data            =>   x_msg_data );
5907 
5908      IF   x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5909        RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
5910      END IF;
5911 
5912      --End of changes for Bug :- 2570250
5913 
5914      IF P_PA_DEBUG_MODE = 'Y' THEN
5915         pa_debug.g_err_stage:='l_max_version_number = '|| l_max_version_number ;
5916         pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5917      END IF;
5918 
5919 -- Start of changes for Bug :- 2650427
5920 
5921      /* For control item versions current_working_flag is always set to 'N'. Now
5922         there is a possibility that max_version is > 0 and still there is no current
5923         working version. Hence a non-ci version should be set to current working in
5924         case there is no current working already available.
5925      */
5926      /*
5927      --Populate l_current_working_flag
5928      IF l_max_version_number = 0 THEN l_current_working_flag := 'Y';
5929      ELSE l_current_working_flag := 'N';
5930      END IF;
5931      */
5932 
5933      IF p_ci_id IS NOT NULL THEN
5934           l_current_working_flag := 'N';
5935      ELSE
5936          BEGIN
5937               /* Bug 2668667 , in the following select clause, version type condition is necessary
5938                  in the following sceniaro. If the plan type is attached is cost_and_rev_sep and
5939                  a cost version is created and then a revenue version is created. Now , the revenue
5940                  version should also be set as current_working_version. */
5941 
5942                SELECT budget_version_id
5943                INTO   l_dummy_version_id
5944                FROM   pa_budget_versions
5945                WHERE  project_id = p_project_id
5946                AND    fin_plan_type_id = p_fin_plan_type_id
5947                AND    version_type = new_version_info_rec.version_type   -- Bug :- 2668667
5948                AND    current_working_flag = 'Y';
5949 
5950                -- If a current_working_version already exists then set
5951                -- current_working flag to 'N'
5952 
5953                l_current_working_flag := 'N';
5954          EXCEPTION
5955                WHEN NO_DATA_FOUND THEN
5956                      -- If no current_working_version already exists,make the
5957                      -- current_version as current_working_version
5958                      l_current_working_flag := 'Y';
5959                WHEN OTHERS THEN
5960 
5961                     IF P_PA_DEBUG_MODE = 'Y' THEN
5962                         pa_debug.g_err_stage:='Error while fetching current_working budget_version_id';
5963                         pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5964                      END IF;
5965                      RAISE;
5966          END;
5967      END IF;
5968 
5969      -- End of changes for Bug :- 2650427
5970 
5971 
5972      --Fetch new budget version id if budget version id isn't passed
5973 
5974      IF (px_budget_version_id IS NULL) THEN
5975 
5976          IF P_PA_DEBUG_MODE = 'Y' THEN
5977             pa_debug.g_err_stage:='Fetching new budget_version_id';
5978             pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5979          END IF;
5980 
5981          SELECT pa_budget_versions_s.NEXTVAL
5982          INTO   l_new_budget_version_id
5983          FROM   DUAL;
5984      ELSE
5985          l_new_budget_version_id := px_budget_version_id;
5986      END IF;
5987 
5988 
5989       --Create a new record in pa_budget_versions using plan type properties
5990 
5991      IF P_PA_DEBUG_MODE = 'Y' THEN
5992          pa_debug.g_err_stage:='Calling budget_versions table handler to insert new row ';
5993          pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
5994      END IF;
5995 
5996      -- Getting the Use for workplan flag to populate WP_VERSION_FLAG in pa_budget_versions
5997      SELECT use_for_workplan_flag
5998      INTO   l_wp_version_flag
5999      FROM   pa_fin_plan_types_b
6000      WHERE  fin_plan_type_id = p_fin_plan_type_id;
6001 
6002      /* This fix is done during IB1 testing of FP M. There are some flows, which
6003       * are creation more than one budget version for the same workplan version. To
6004       * identify such flows, the following check is being made so that dev can fix
6005       * such issues */
6006 
6007      Declare
6008           l_exists varchar2(1);
6009      Begin
6010           Select 'Y'
6011           Into   l_exists
6012           From   pa_budget_versions
6013           Where  project_structure_version_id =
6014                   nvl(p_struct_elem_version_id,new_version_info_rec.project_structure_version_id)
6015           And    wp_version_flag = 'Y'
6016           And    exists (select 'x' from pa_budget_versions b
6017                          where b.budget_version_id =
6018                                  nvl(l_ci_apprv_cw_bv_id,l_fin_plan_version_id)
6019                          and b.wp_version_flag = 'Y') ;
6020 
6021           IF P_PA_DEBUG_MODE = 'Y' THEN
6022              pa_debug.g_err_stage:='Project_id = '||p_project_id;
6023              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6024              pa_debug.g_err_stage:='Fin_plan_type_id = '||p_fin_plan_type_id;
6025              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6026              pa_debug.g_err_stage:='Version_name = '||p_version_name;
6027              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6028              pa_debug.g_err_stage:='Description = '||p_description;
6029              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6030              pa_debug.g_err_stage:='proj sv id = ' || nvl(p_struct_elem_version_id,new_version_info_rec.project_structure_version_id);
6031              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6032              pa_debug.g_err_stage:='calling context = ' || p_calling_context;
6033              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6034          END IF;
6035 
6036          PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
6037                               p_msg_name      => 'DUPLICATE_WP_BEING_CREATED');
6038 
6039          IF P_PA_DEBUG_MODE = 'Y' THEN
6040              pa_debug.g_err_stage:='Invalid Arguments Passed';
6041              pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6042          END IF;
6043          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6044      Exception
6045           When No_Data_Found Then
6046                Null;
6047      End;
6048 
6049      pa_fp_budget_versions_pkg.Insert_Row
6050            (px_budget_version_id                =>      l_new_budget_version_id, -- unique budget_version_id for new version
6051             p_project_id                        =>      p_project_id,          -- the ID of the project
6052             p_budget_type_code                  =>      NULL,
6053             p_version_number                    =>      l_max_version_number+1,      -- version_number incremented
6054             p_budget_status_code                =>      'W',                  -- 'Working' version
6055             p_current_flag                      =>      'N',                  -- 'Working' version
6056             p_original_flag                     =>      'N',                  -- 'Working' version
6057             p_current_original_flag             =>      'N',                  -- 'Working' version
6058             p_resource_accumulated_flag         =>      'N',   -- HARDCODED VALUE
6059             p_resource_list_id                  =>      new_version_info_rec.resource_list_id,
6060             p_version_name                      =>      p_version_name,     -- user-entered value
6061             p_budget_entry_method_code          =>      NULL,
6062             p_baselined_by_person_id            =>      NULL,
6063             p_baselined_date                    =>      NULL,
6064             p_change_reason_code                =>      NULL,
6065             p_labor_quantity                    =>      NULL,
6066             p_labor_unit_of_measure             =>      'HOURS',
6067             p_raw_cost                          =>      NULL,
6068             p_burdened_cost                     =>      NULL,
6069             p_revenue                           =>      NULL,
6070             p_description                       =>      p_description,      -- user-entered value
6071             --Bug3088010 start: Changed NULL to the parameters passed in to this api
6072             p_attribute_category                =>      p_attribute_category,   --NULL,
6073             p_attribute1                        =>      p_attribute1,           --NULL,
6074             p_attribute2                        =>      p_attribute2,           --NULL,
6075             p_attribute3                        =>      p_attribute3,           --NULL,
6076             p_attribute4                        =>      p_attribute4,           --NULL,
6077             p_attribute5                        =>      p_attribute5,           --NULL,
6078             p_attribute6                        =>      p_attribute6,           --NULL,
6079             p_attribute7                        =>      p_attribute7,           --NULL,
6080             p_attribute8                        =>      p_attribute8,           --NULL,
6081             p_attribute9                        =>      p_attribute9,           --NULL,
6082             p_attribute10                       =>      p_attribute10,          --NULL,
6083             p_attribute11                       =>      p_attribute11,          --NULL,
6084             p_attribute12                       =>      p_attribute12,          --NULL,
6085             p_attribute13                       =>      p_attribute13,          --NULL,
6086             p_attribute14                       =>      p_attribute14,          --NULL,
6087             p_attribute15                       =>      p_attribute15,          --NULL,
6088             --Bug3088010 end: Changed NULL to the parameters passed in to this api
6089             p_first_budget_period               =>      NULL,
6090             p_pm_product_code                   =>      p_pm_product_code,  --NULL, --Bug 5403751
6091             p_pm_budget_reference               =>      NULL,
6092             p_wf_status_code                    =>      NULL,
6093             p_adw_notify_flag                   =>      NULL,
6094             p_prc_generated_flag                =>      NULL,
6095             p_plan_run_date                     =>      NULL,
6096             p_plan_processing_code              =>      NULL,
6097             p_fin_plan_type_id                  =>      p_fin_plan_type_id,
6098             p_parent_plan_version_id            =>      NULL,
6099             p_project_structure_version_id      =>      nvl(p_struct_elem_version_id,new_version_info_rec.project_structure_version_id),
6100             p_current_working_flag              =>      l_current_working_flag,
6101             p_total_borrowed_revenue            =>      NULL,
6102             p_total_tp_revenue_in               =>      NULL,
6103             p_total_tp_revenue_out              =>      NULL,
6104             p_total_revenue_adj                 =>      NULL,
6105             p_total_lent_resource_cost          =>      NULL,
6106             p_total_tp_cost_in                  =>      NULL,
6107             p_total_tp_cost_out                 =>      NULL,
6108             p_total_cost_adj                    =>      NULL,
6109             p_total_unassigned_time_cost        =>      NULL,
6110             p_total_utilization_percent         =>      NULL,
6111             p_total_utilization_hours           =>      NULL,
6112             p_total_utilization_adj             =>      NULL,
6113             p_total_capacity                    =>      NULL,
6114             p_total_head_count                  =>      NULL,
6115             p_total_head_count_adj              =>      NULL,
6116             p_version_type                      =>      new_version_info_rec.version_type,
6117             p_request_id                        =>      FND_GLOBAL.conc_request_id,
6118             p_total_project_raw_cost            =>      NULL,
6119             p_total_project_burdened_cost       =>      NULL,
6120             p_total_project_revenue             =>      NULL,
6121             p_locked_by_person_id               =>      NULL,
6122             p_approved_cost_plan_type_flag      =>      new_version_info_rec.approved_cost_plan_type_flag,
6123             p_approved_rev_plan_type_flag       =>      new_version_info_rec.approved_rev_plan_type_flag,
6124             p_est_project_raw_cost              =>      p_est_proj_raw_cost,
6125             p_est_project_burdened_cost         =>      p_est_proj_bd_cost,
6126             p_est_project_revenue               =>      p_est_proj_revenue,
6127             p_est_quantity                      =>      p_est_qty,
6128             p_est_equip_qty                     =>      p_est_equip_qty,
6129             p_est_projfunc_raw_cost             =>      NULL,
6130             p_est_projfunc_burdened_cost        =>      NULL,
6131             p_est_projfunc_revenue              =>      NULL,
6132             p_ci_id                             =>      p_ci_id,
6133             p_agreement_id                      =>      p_agreement_id,
6134             p_refresh_required_flag             =>      NULL, -- redundant in patchset M
6135             p_object_type_code                  =>      'PROJECT',
6136             p_object_id                         =>      p_project_id,
6137             p_primary_cost_forecast_flag        =>      new_version_info_rec.primary_cost_forecast_flag,
6138             p_primary_rev_forecast_flag         =>      new_version_info_rec.PRIMARY_REV_FORECAST_FLAG,
6139             p_rev_partially_impl_flag           =>      'N',
6140             p_equipment_quantity                =>      NULL,
6141             p_pji_summarized_flag               =>      'N',
6142             p_wp_version_flag                   =>      l_WP_VERSION_FLAG,
6143             p_current_planning_period           =>      new_version_info_rec.CURRENT_PLANNING_PERIOD,
6144             p_period_mask_id                    =>      new_version_info_rec.PERIOD_MASK_ID,
6145             p_actual_amts_thru_period           =>      new_version_info_rec.actual_amts_thru_period,
6146             p_last_amt_gen_date                 =>      NULL,
6147             x_row_id                            =>      l_row_id,
6148             x_return_status                     =>      x_return_status);
6149 
6150      -- End, jwhite, 26-JUN-2003: Plannable Task Effort --------------------------------
6151 
6152      IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6153 
6154           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6155 
6156      END IF;
6157 
6158      pa_budget_utils.Get_Project_Currency_Info  -- Bug # 2634900
6159      (
6160         p_project_id                    => p_project_id
6161       , x_projfunc_currency_code        => l_projfunc_currency_code
6162       , x_project_currency_code         => l_project_currency_code
6163       , x_txn_currency_code             => l_dummy_currency_code
6164       , x_msg_count                     => x_msg_count
6165       , x_msg_data                      => x_msg_data
6166       , x_return_status                 => x_return_status
6167      );
6168 
6169     IF (x_return_status <>  FND_API.G_RET_STS_SUCCESS) THEN
6170 
6171          IF P_PA_DEBUG_MODE = 'Y' THEN
6172             pa_debug.g_err_stage:= 'Could not obtain currency info for the project';
6173             pa_debug.write('Create_Version: ' || l_module_name,
6174                                pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
6175          END IF;
6176          RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6177     END IF;
6178 
6179     -- for a control item budget version, the only plannable currency should be
6180     -- agreement currency.
6181 
6182     IF (p_ci_id IS NOT NULL) AND (p_agreement_id IS NOT NULL) THEN   -- Bug # 2634900
6183         -- Fetch the project and project functional currency codes of the project
6184 
6185         -- Obtain the agreement currency code.
6186         Pa_Fp_Control_Items_Utils.get_fp_ci_agreement_dtls(
6187                     p_project_id                    =>  p_project_id
6188                    ,p_ci_id                         =>  p_ci_id
6189                    ,x_agreement_num                 =>  l_agreement_num
6190                    ,x_agreement_amount              =>  l_agreement_amount
6191                    ,x_agreement_currency_code       =>  l_agreement_currency_code
6192                    ,x_msg_data                      =>  x_msg_data
6193                    ,x_msg_count                     =>  x_msg_count
6194                    ,x_return_status                 =>  x_return_status );
6195 
6196         IF  (l_agreement_currency_code IS NULL) OR
6197             (x_return_status <> FND_API.G_RET_STS_SUCCESS)
6198         THEN
6199             IF P_PA_DEBUG_MODE = 'Y' THEN
6200                pa_debug.g_err_stage:='Agreement_currency_code is null';
6201                pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
6202             END IF;
6203             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6204         END IF;
6205 
6206         IF (l_agreement_currency_code NOT IN (l_projfunc_currency_code,l_project_currency_code)) THEN
6207                 new_version_info_rec.plan_in_multi_curr_flag := 'Y';
6208         END IF;
6209     END IF;
6210     -- end of changes for Bug :- 2634900
6211 
6212     -- Create record in PA_PROJ_FP_OPTIONS
6213     -- Calling create_fp_options api to create new record for the created plan version
6214 
6215 
6216     IF P_PA_DEBUG_MODE = 'Y' THEN
6217         pa_debug.g_err_stage:='Calling create_fp_option api';
6218         pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
6219     END IF;
6220 
6221     PA_PROJ_FP_OPTIONS_PUB.create_fp_option (
6222                px_target_proj_fp_option_id   =>  l_new_proj_fp_options_id
6223                ,p_source_proj_fp_option_id   =>  new_version_info_rec.source_fp_options_id
6224                ,p_target_fp_option_level_code => PA_FP_CONSTANTS_PKG.G_OPTION_LEVEL_PLAN_VERSION
6225                ,p_target_fp_preference_code  =>  new_version_info_rec.fin_plan_preference_code
6226                ,p_target_fin_plan_version_id =>  l_new_budget_version_id
6227                ,p_target_project_id          =>  p_project_id
6228                ,p_target_plan_type_id        =>  p_fin_plan_type_id
6229                ,x_return_status              =>  x_return_status
6230                ,x_msg_count                  =>  x_msg_count
6231                ,x_msg_data                   =>  x_msg_data );
6232 
6233     IF   x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6234       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6235     END IF;
6236 
6237     --Update the fp option created for plan version with the passed  i/p parameters if they are not null
6238 
6239     IF new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_COST THEN
6240          UPDATE pa_proj_fp_options
6241          SET    cost_amount_set_id           =   NVL(p_amount_set_id,new_version_info_rec.amount_set_id),
6242                 plan_in_multi_curr_flag      =   new_version_info_rec.plan_in_multi_curr_flag,
6243                 cost_fin_plan_level_code     =   new_version_info_rec.fin_plan_level_code,
6244                 cost_time_phased_code        =   new_version_info_rec.time_phased_code,
6245                 cost_resource_list_id        =   new_version_info_rec.resource_list_id,
6246                 select_cost_res_auto_flag    =   new_version_info_rec.select_res_auto_flag,
6247                 cost_current_planning_period =   new_version_info_rec.current_planning_period,
6248                 cost_period_mask_id          =   new_version_info_rec.period_mask_id,
6249                 rbs_version_id               =   Decode(p_ci_id, null, rbs_version_id, null) -- bug 3867302
6250          WHERE  proj_fp_options_id = l_new_proj_fp_options_id;
6251     ELSIF new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_REVENUE THEN
6252          UPDATE pa_proj_fp_options
6253          SET    revenue_amount_set_id         =   NVL(p_amount_set_id,new_version_info_rec.amount_set_id),
6254                 plan_in_multi_curr_flag       =   new_version_info_rec.plan_in_multi_curr_flag,
6255                 revenue_fin_plan_level_code   =   new_version_info_rec.fin_plan_level_code,
6256                 revenue_time_phased_code      =   new_version_info_rec.time_phased_code,
6257                 revenue_resource_list_id      =   new_version_info_rec.resource_list_id,
6258                 select_rev_res_auto_flag      =   new_version_info_rec.select_res_auto_flag,
6259                 rev_current_planning_period   =   new_version_info_rec.current_planning_period,
6260                 rev_period_mask_id            =   new_version_info_rec.period_mask_id,
6261                 rbs_version_id                =   Decode(p_ci_id, null, rbs_version_id, null) -- bug 3867302
6262          WHERE  proj_fp_options_id = l_new_proj_fp_options_id;
6263     ELSIF new_version_info_rec.version_type = PA_FP_CONSTANTS_PKG.G_ELEMENT_TYPE_ALL THEN
6264          UPDATE pa_proj_fp_options
6265          SET    all_amount_set_id             =   NVL(p_amount_set_id,new_version_info_rec.amount_set_id),
6266                 plan_in_multi_curr_flag       =   new_version_info_rec.plan_in_multi_curr_flag,
6267                 all_fin_plan_level_code       =   new_version_info_rec.fin_plan_level_code,
6268                 all_time_phased_code          =   new_version_info_rec.time_phased_code,
6269                 all_resource_list_id          =   new_version_info_rec.resource_list_id,
6270                 select_all_res_auto_flag      =   new_version_info_rec.select_res_auto_flag,
6271                 all_current_planning_period   =   new_version_info_rec.current_planning_period,
6272                 all_period_mask_id            =   new_version_info_rec.period_mask_id,
6273                 rbs_version_id                =   Decode(p_ci_id, null, rbs_version_id, null) -- bug 3867302
6274          WHERE  proj_fp_options_id = l_new_proj_fp_options_id;
6275     END IF;
6276 
6277     -- Start of bug changes :- 2649474 (Baseline funding without budget Changes)
6278     --Calling copy_fp_txn_currencies api
6279 
6280     IF p_agreement_id IS NOT NULL THEN
6281 
6282         -- Insert the agreement_currency into pa_fp_txn_currencies table
6283 
6284         PA_FP_TXN_CURRENCIES_PUB.enter_agreement_curr_for_ci
6285              (  p_project_id              =>  p_project_id
6286                ,p_fin_plan_version_id     =>  l_new_budget_version_id
6287                ,p_ci_id                   =>  p_ci_id
6288                ,p_project_currency_code   =>  l_project_currency_code
6289                ,p_projfunc_currency_code  =>  l_projfunc_currency_code
6290                ,x_return_status           =>  x_return_status
6291                ,x_msg_count               =>  x_msg_count
6292                ,x_msg_data                =>  x_msg_data );
6293 
6294         IF   x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6295             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6296         END IF;
6297 
6298     ELSE
6299 
6300         IF P_PA_DEBUG_MODE = 'Y' THEN
6301            pa_debug.g_err_stage:='Calling COPY_FP_TXN_CURRENCIES api';
6302            pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
6303         END IF;
6304 
6305         PA_FP_TXN_CURRENCIES_PUB.COPY_FP_TXN_CURRENCIES (
6306              p_source_fp_option_id           =>   new_version_info_rec.source_fp_options_id
6307              ,p_target_fp_option_id          =>   l_new_proj_fp_options_id
6308              ,p_target_fp_preference_code    =>   null
6309              ,p_plan_in_multi_curr_flag      =>   new_version_info_rec.plan_in_multi_curr_flag
6310              ,x_return_status                =>   x_return_status
6311              ,x_msg_count                    =>   x_msg_count
6312              ,x_msg_data                     =>   x_msg_data );
6313 
6314         IF   x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6315           RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6316         END IF;
6317 
6318     END IF;
6319 
6320     --Bug 3867302  For ci versions reporting data is not maintained
6321     IF l_pji_rollup_required = 'Y' THEN --for Bug 4200168
6322         IF p_ci_id IS NULL THEN
6323             /* FP M - Reporting lines integration */
6324             l_budget_version_ids.delete;
6325             l_budget_version_ids   := SYSTEM.pa_num_tbl_type(l_new_budget_version_id);
6326 
6327             IF P_PA_DEBUG_MODE = 'Y' THEN
6328                 pa_debug.write('Create_Version: ' || l_module_name,'Calling PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE ' ,5);
6329                 pa_debug.write('Create_Version: ' || l_module_name,'p_fp_version_ids count '|| l_budget_version_ids.count(),5);
6330             END IF;
6331 
6332             /* We are sure that there is only one record. But just looping the std way */
6333             FOR I in l_budget_version_ids.first..l_budget_version_ids.last LOOP
6334                pa_debug.write('Create_Version: ' || l_module_name,''|| l_budget_version_ids(i),5);
6335             END LOOP;
6336 
6337               PJI_FM_XBS_ACCUM_MAINT.PLAN_CREATE (
6338                      p_fp_version_ids   => l_budget_version_ids,
6339                      x_return_status    => l_return_status,
6340                      x_msg_code         => l_msg_data);
6341 
6342               IF l_return_status <> FND_API.G_RET_STS_SUCCESS then
6343                    PA_UTILS.ADD_MESSAGE(p_app_short_name      => 'PA',
6344                                         p_msg_name            => l_msg_data);
6345                    RAISE  PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6346               END IF;
6347 
6348         END IF;
6349     END IF;
6350     /*
6351        In control item context for a project enabled for auto baseline funding
6352        and PC=PFC get the conversion attributes from the project. Bug 2661237.
6353     */
6354     IF ( nvl(l_plan_type_mc_flag,'N') = 'N' and
6355          nvl(new_version_info_rec.plan_in_multi_curr_flag,'N') = 'Y' AND
6356          p_agreement_id IS NOT NULL
6357        ) OR
6358        (
6359          nvl(l_plan_type_mc_flag,'N') = 'Y' and
6360          nvl(new_version_info_rec.plan_in_multi_curr_flag,'N') = 'Y' AND
6361          p_agreement_id IS NOT NULL AND
6362          l_project_currency_code = l_projfunc_currency_code
6363        )
6364     THEN
6365 
6366         pa_multi_currency_billing.get_project_defaults (
6367             p_project_id                       => p_project_id
6368            ,x_multi_currency_billing_flag      => l_multi_currency_billing_flag
6369            ,x_baseline_funding_flag            => l_baseline_funding_flag
6370            ,x_revproc_currency_code            => l_revproc_currency_code
6371            ,x_invproc_currency_type            => l_invproc_currency_type
6372            ,x_invproc_currency_code            => l_invproc_currency_code
6373            ,x_project_currency_code            => l_project_currency_code
6374            ,x_project_bil_rate_date_code       => l_project_bil_rate_date_code
6375            ,x_project_bil_rate_type            => l_project_bil_rate_type
6376            ,x_project_bil_rate_date            => l_project_bil_rate_date
6377            ,x_project_bil_exchange_rate        => l_project_bil_exchange_rate
6378            ,x_projfunc_currency_code           => l_projfunc_currency_code
6379            ,x_projfunc_bil_rate_date_code      => l_projfunc_bil_rate_date_code
6380            ,x_projfunc_bil_rate_type           => l_projfunc_bil_rate_type
6381            ,x_projfunc_bil_rate_date           => l_projfunc_bil_rate_date
6382            ,x_projfunc_bil_exchange_rate       => l_projfunc_bil_exchange_rate
6383            ,x_funding_rate_date_code           => l_funding_rate_date_code
6384            ,x_funding_rate_type                => l_funding_rate_type
6385            ,x_funding_rate_date                => l_funding_rate_date
6386            ,x_funding_exchange_rate            => l_funding_exchange_rate
6387            ,x_return_status                    => x_return_status
6388            ,x_msg_count                        => x_msg_count
6389            ,x_msg_data                         => x_msg_data   );
6390 
6391 /*     IF nvl(l_multi_currency_billing_flag,'N') = 'N' THEN
6392          -- This will never occur as the required validation is done in the agreement form.
6393                PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
6394                                     p_msg_name      => 'PAFP_NO_PROJ_CONV_ATTR');
6395                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6396        ELSE
6397 */             /* check for FP compatible rate date type - PC*/
6398                IF l_project_bil_rate_date is not null THEN  -- Takes care of FIXED_DATE
6399 
6400                        -- Rate Date will be as it is.
6401                        -- Rate Date type is FIXED_DATE.
6402                        l_project_bil_rate_date_code  := PA_FP_CONSTANTS_PKG.G_RATE_DATE_TYPE_FIXED_DATE;
6403 
6404                ELSIF l_project_bil_rate_date_code IS NOT NULL AND
6405                      l_project_bil_rate_type <> PA_FP_CONSTANTS_PKG.G_RATE_TYPE_USER THEN  --Takes care of PA_INVOICE_DATE
6406 
6407                        l_project_bil_rate_date_code  := PA_FP_CONSTANTS_PKG.G_RATE_DATE_TYPE_START_DATE;
6408 
6409                ELSIF l_project_bil_rate_type = PA_FP_CONSTANTS_PKG.G_RATE_TYPE_USER THEN    --USER rate type.
6410 
6411                        update pa_fp_txn_currencies
6412                        set project_rev_exchange_rate = l_project_bil_exchange_rate
6413                        where proj_fp_options_id = l_new_proj_fp_options_id
6414                        and txn_currency_code = l_agreement_currency_code;
6415 
6416                END IF;
6417 
6418                /* check for FP compatible rate date type - PFC*/
6419                IF l_projfunc_bil_rate_date IS NOT NULL THEN  -- Takes care of FIXED_DATE
6420                     -- Rate Date will be as it is.
6421                      l_projfunc_bil_rate_date_code  := PA_FP_CONSTANTS_PKG.G_RATE_DATE_TYPE_FIXED_DATE;
6422 
6423                ELSIF l_projfunc_bil_rate_date_code IS NOT NULL AND
6424                      l_projfunc_bil_rate_type <> PA_FP_CONSTANTS_PKG.G_RATE_TYPE_USER THEN  --Takes care of PA_INVOICE_DATE
6425 
6426                       l_projfunc_bil_rate_date_code  := PA_FP_CONSTANTS_PKG.G_RATE_DATE_TYPE_START_DATE;
6427 
6428                ELSIF l_projfunc_bil_rate_type = PA_FP_CONSTANTS_PKG.G_RATE_TYPE_USER then    --USER rate type.
6429 
6430                      update pa_fp_txn_currencies
6431                      set projfunc_rev_exchange_rate = l_projfunc_bil_exchange_rate
6432                      where proj_fp_options_id = l_new_proj_fp_options_id
6433                      and txn_currency_code = l_agreement_currency_code;
6434 
6435                END IF;
6436 
6437                UPDATE   pa_proj_fp_options
6438                SET      PROJECT_REV_RATE_TYPE         = l_project_bil_rate_type
6439                        ,PROJECT_REV_RATE_DATE_TYPE    = l_project_bil_rate_date_code
6440                        ,PROJECT_REV_RATE_DATE         = l_project_bil_rate_date
6441                        ,PROJFUNC_REV_RATE_TYPE        = l_projfunc_bil_rate_type
6442                        ,PROJFUNC_REV_RATE_DATE_TYPE   = l_projfunc_bil_rate_date_code
6443                        ,PROJFUNC_REV_RATE_DATE        = l_projfunc_bil_rate_date
6444                 WHERE  proj_fp_options_id = l_new_proj_fp_options_id;
6445 /*     END IF;*/
6446     END IF;
6447 
6448     -- End of bug changes :- 2649474
6449 
6450     /*
6451         Bug 2678651 - The API get_converted_amounts should be called after the option
6452         for the version in case of CI is created - Moved the code to after the option
6453         is created.
6454     */
6455     IF p_ci_id IS NOT NULL THEN
6456 
6457          PA_FIN_PLAN_UTILS.get_converted_amounts
6458                (  p_budget_version_id       =>  l_new_budget_version_id
6459                  ,p_txn_raw_cost            =>  p_est_proj_raw_cost
6460                  ,p_txn_burdened_cost       =>  p_est_proj_bd_cost
6461                  ,p_txn_revenue             =>  p_est_proj_revenue
6462                  ,p_txn_currency_Code       =>  l_project_currency_Code
6463                  ,p_project_currency_Code   =>  l_project_currency_Code
6464                  ,p_projfunc_currency_code  =>  l_projfunc_currency_code
6465                  ,x_project_raw_cost        =>  l_est_project_raw_cost
6466                  ,x_project_burdened_cost   =>  l_est_project_bd_cost
6467                  ,x_project_revenue         =>  l_est_project_revenue
6468                  ,x_projfunc_raw_cost       =>  l_est_projfunc_raw_cost
6469                  ,x_projfunc_burdened_cost  =>  l_est_projfunc_bd_cost
6470                  ,x_projfunc_revenue        =>  l_est_projfunc_revenue
6471                  ,x_return_status           =>  x_return_status
6472                  ,x_msg_count               =>  x_msg_count
6473                  ,x_msg_data                =>  x_msg_data );
6474 
6475          IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6476                RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6477          END IF;
6478 
6479          -- Update the budget versions table with the converted estimated amounts in projfunc currency
6480 
6481          UPDATE Pa_Budget_Versions SET
6482              est_projfunc_raw_cost      =  l_est_projfunc_raw_cost,
6483              est_projfunc_burdened_cost =  l_est_projfunc_bd_cost,
6484              est_projfunc_revenue       =  l_est_projfunc_revenue
6485          WHERE  Budget_Version_Id       =  l_new_budget_version_id;
6486      END IF;
6487      /* End of changes for Bug 2678651 */
6488 
6489      -- Bug 3658080 Logic to derive if res assignments data should be
6490      -- copied from current working version or create defaults
6491 
6492      IF  p_ci_id IS NOT NULL
6493      THEN
6494          l_copy_res_assmt_from_cwv_flag:='Y';
6495      ELSIF ( l_curr_work_ver_exists_flag = 'Y' ) AND
6496            ( new_version_info_rec.fin_plan_level_code = l_cw_fin_plan_level_code) AND
6497            ( new_version_info_rec.resource_list_id = l_cw_ver_res_list_id )
6498      THEN
6499          l_copy_res_assmt_from_cwv_flag:='Y';
6500      ELSE
6501          l_copy_res_assmt_from_cwv_flag:='N';
6502      END IF;
6503 
6504      -- If calling context is create_draft or automatic baseline this api need not
6505      -- create resource assignments for the budget version
6506 
6507      /* Did null handing for bug 2663313 */
6508      /* 3831449: do not create records in pa_resource_assignments if p_calling_context is GENERATE */
6509      IF nvl(p_calling_context,'-99') NOT IN  (PA_FP_CONSTANTS_PKG.G_CREATE_DRAFT,
6510                                               -- Bug Fix: 4569365. Removed MRC code.
6511                                               -- PA_MRC_FINPLAN.G_AUTOMATIC_BASELINE,
6512                                               'AUTOMATIC_BASELINE', --Bug 5700400: Autobaseline case was commented earlier by mistake.
6513                                               PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_WORKPLAN,
6514                 'GENERATE', PA_FP_CONSTANTS_PKG.G_AMG_API) --Added this for bug 4224464
6515      THEN
6516 
6517          --Start of changes for Bug :- 2634900
6518 
6519          IF p_impacted_task_id IS NULL THEN
6520 
6521               --  If current working version doesn't exist, call
6522               --  create_default_task_plan_txns to create default
6523               --  planning transactions
6524               IF l_copy_res_assmt_from_cwv_flag = 'N'
6525               THEN
6526               -- added for bug 4724017:
6527               -- Creation of default planning transaction is not done for versions
6528               -- being created with categorized resource list.
6529                   IF l_res_list_uncategorized_flag = 'Y' THEN
6530                       IF P_PA_DEBUG_MODE = 'Y' THEN
6531                           pa_debug.g_err_stage:='Calling create_default_task_plan_txns api';
6532                           pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
6533                       END IF;
6534 
6535                       pa_fp_planning_transaction_pub.create_default_task_plan_txns
6536                       (    p_budget_version_id       => l_new_budget_version_id
6537                          , p_version_plan_level_code => new_version_info_rec.fin_plan_level_code
6538                          , x_return_status           => x_return_status
6539                          , x_msg_count               => l_msg_count
6540                          , x_msg_data                => l_msg_data
6541                       );
6542 
6543                       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6544                            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6545                       END IF;
6546                   END IF; -- bug 4724017 ends
6547               ELSE
6548 
6549 
6550                   IF p_ci_id IS NULL THEN
6551                       l_src_bv_id_for_copying_ra :=  l_fin_plan_version_id ;
6552                   ELSE
6553                       l_src_bv_id_for_copying_ra :=  l_ci_apprv_cw_bv_id ;
6554                   END IF;
6555 
6556                   IF P_PA_DEBUG_MODE = 'Y' THEN
6557                       pa_debug.g_err_stage:='Calling copy_resource_assignments api';
6558                       pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
6559                   END IF;
6560 
6561                   pa_fp_copy_from_pkg.copy_resource_assignments
6562                   (     p_source_plan_version_id => l_src_bv_id_for_copying_ra
6563                       , p_target_plan_version_id => l_new_budget_version_id
6564                       , p_adj_percentage         => -99
6565                       , p_calling_context        => 'CREATE_VERSION'
6566                       , x_return_status          => x_return_status
6567                       , x_msg_count              => l_msg_count
6568                       , x_msg_data               => l_msg_data
6569                   );
6570 
6571                   IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6572                       RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6573                   END IF;
6574 
6575                   -- For normal budget versions when res assignments data is copied
6576                   -- from current working version, if rbs of versions is not same as
6577                   -- that of plan type's rbs, rbs refresh is necessary
6578                   IF  (nvl(plan_type_info_rec.rbs_version_id, -99)  <>
6579                               nvl(plan_version_info_rec.rbs_version_id, -99)) AND
6580                       p_ci_id IS NULL
6581                   THEN
6582                       -- RBS refresh is necessary for the resource assignments data
6583                       IF  plan_type_info_rec.rbs_version_id IS NOT NULL THEN
6584                           -- Call RBS mapping api for the entire version
6585                           PA_RLMI_RBS_MAP_PUB.Map_Rlmi_Rbs(
6586                                p_budget_version_id         =>   l_new_budget_version_id
6587                               ,p_resource_list_id          =>   new_version_info_rec.resource_list_id
6588                               ,p_rbs_version_id            =>   plan_type_info_rec.rbs_version_id
6589                               ,p_calling_process           =>   'RBS_REFRESH'
6590                               ,p_calling_context           =>   'PLSQL'
6591                               ,p_process_code              =>   'RBS_MAP'
6592                               ,p_calling_mode              =>   'BUDGET_VERSION'
6593                               ,p_init_msg_list_flag        =>   'N'
6594                               ,p_commit_flag               =>   'N'
6595                               ,x_txn_source_id_tab         =>   l_txn_source_id_tbl
6596                               ,x_res_list_member_id_tab    =>   l_res_list_member_id_tbl
6597                               ,x_rbs_element_id_tab        =>   l_rbs_element_id_tbl
6598                               ,x_txn_accum_header_id_tab   =>   l_txn_accum_header_id_tbl
6599                               ,x_return_status             =>   x_return_status
6600                               ,x_msg_count                 =>   x_msg_count
6601                               ,x_msg_data                  =>   x_msg_data);
6602 
6603                           -- Check return status
6604                           IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6605                               RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6606                           END IF;
6607 
6608                           -- Check if out table has any records first
6609                           IF nvl(l_txn_source_id_tbl.last,0) >= 1 THEN
6610                               -- Update resource assignments data for the version
6611                               FORALL j IN l_txn_source_id_tbl.first .. l_txn_source_id_tbl.last
6612                                   UPDATE pa_resource_assignments
6613                                   SET     rbs_element_id          =  l_rbs_element_id_tbl(j)
6614                                          ,txn_accum_header_id     =  l_txn_accum_header_id_tbl(j)
6615                                          ,record_version_number   =  record_version_number + 1
6616                                          ,last_update_date        =  SYSDATE
6617                                          ,last_updated_by         =  FND_GLOBAL.user_id
6618                                          ,last_update_login       =  FND_GLOBAL.login_id
6619                                   WHERE  budget_version_id = l_new_budget_version_id
6620                                   AND    resource_assignment_id = l_txn_source_id_tbl(j);
6621                           END IF;
6622                       ELSE -- rbs version id is null
6623 
6624                             -- Update all the resource assigments with null for rbs _element_id
6625                             UPDATE pa_resource_assignments
6626                             SET     rbs_element_id          =  null
6627                                    ,txn_accum_header_id     =  null
6628                                    ,record_version_number   =  record_version_number + 1
6629                                    ,last_update_date        =  SYSDATE
6630                                    ,last_updated_by         =  FND_GLOBAL.user_id
6631                                    ,last_update_login       =  FND_GLOBAL.login_id
6632                             WHERE  budget_version_id = l_new_budget_version_id;
6633 
6634                       END IF;
6635                   END IF;
6636               END IF;  -- ( l_copy_res_assmt_from_cwv_flag = 'N'  )
6637 
6638          ELSE
6639                -- Create resource assignments for the budget version and the impacted task id
6640 
6641                IF P_PA_DEBUG_MODE = 'Y' THEN
6642                   pa_debug.g_err_stage:='Calling Create_CI_Resource_Assignments';
6643                   pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
6644                END IF;
6645 
6646                PA_FP_ELEMENTS_PUB.Create_CI_Resource_Assignments
6647                              (  p_project_id              =>    p_project_id
6648                                ,p_budget_version_id       =>    l_new_budget_version_id
6649                                ,p_version_type            =>    new_version_info_rec.version_type
6650                                ,p_impacted_task_id        =>    p_impacted_task_id
6651                                ,x_return_status           =>    x_return_status
6652                                ,x_msg_count               =>    x_msg_count
6653                                ,x_msg_data                =>    x_msg_data );
6654 
6655                IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
6656                  Raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
6657                END IF;
6658 
6659          END IF;
6660 
6661          --End of changes for Bug :- 2634900
6662     END IF; -- calling context
6663 
6664       -- IPM Architecture Enhancement Bug 4865563
6665        /* If there is no budget lines for some resource assignments of the current budget versions
6666         * then, the maintenance api would not create data in the new entity. In that scenario, we have
6667         * to insert those resource assignment with default applicable currency
6668         */
6669        PA_FIN_PLAN_PUB.create_default_plan_txn_rec
6670            (p_budget_version_id => l_new_budget_version_id,
6671             p_calling_module    => 'COPY_PLAN',
6672             x_return_status     => l_return_status,
6673             x_msg_count         => l_msg_count,
6674             x_msg_data          => l_msg_data);
6675 
6676         IF l_return_status <> FND_API.G_RET_STS_SUCCESS
6677                   THEN
6678                      IF p_pa_debug_mode = 'Y' THEN
6679                          pa_debug.write_file('Failed due to error in PA_FIN_PLAN_PUB.create_default_plan_txn_rec',5);
6680                      END IF;
6681                      raise PA_FP_CONSTANTS_PKG.INVALID_ARG_EXC;
6682                 END IF;
6683     -- End of changes for Bug :- 2649474
6684 
6685 
6686     IF P_PA_DEBUG_MODE = 'Y' THEN
6687        pa_debug.g_err_stage:='Exiting Create_Version';
6688        pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,3);
6689     END IF;
6690 
6691     --Return the out parameters
6692 
6693     px_budget_version_id := l_new_budget_version_id;
6694 
6695     x_proj_fp_option_id  := l_new_proj_fp_options_id;
6696 
6697     --Reset the error stack
6698 
6699     pa_debug.reset_curr_function;
6700 
6701 EXCEPTION
6702 
6703    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
6704           l_msg_count := FND_MSG_PUB.count_msg;
6705           IF l_msg_count = 1 THEN
6706                PA_INTERFACE_UTILS_PUB.get_messages
6707                      (p_encoded        => FND_API.G_TRUE
6708                       ,p_msg_index      => 1
6709                       ,p_msg_count      => l_msg_count
6710                       ,p_msg_data       => l_msg_data
6711                       ,p_data           => l_data
6712                       ,p_msg_index_out  => l_msg_index_out);
6713                x_msg_data := l_data;
6714                x_msg_count := l_msg_count;
6715           ELSE
6716               x_msg_count := l_msg_count;
6717           END IF;
6718            x_return_status := FND_API.G_RET_STS_ERROR;
6719 --           pa_debug.g_err_stage:='Invalid Arguments Passed';
6720 --           pa_debug.write(l_module_name,pa_debug.g_err_stage,5);
6721            pa_debug.reset_curr_function;
6722            RETURN;
6723 
6724      WHEN Others THEN
6725           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6726           x_msg_count     := 1;
6727           x_msg_data      := SQLERRM;
6728           FND_MSG_PUB.add_exc_msg( p_pkg_name      => 'PA_FIN_PLAN_PUB'
6729                                   ,p_procedure_name  => 'CREATE_VERSION');
6730           IF P_PA_DEBUG_MODE = 'Y' THEN
6731               pa_debug.g_err_stage:='Unexpected Error ' || SQLERRM;
6732               pa_debug.write('Create_Version: ' || l_module_name,pa_debug.g_err_stage,5);
6733           END IF;
6734           pa_debug.reset_curr_function;
6735           RAISE;
6736 
6737 END Create_Version;
6738 
6739 /*===================================================================
6740 This private procedure fetches start date of the period into which the
6741 profile start period falls and end date of period into which period
6742 end date falls.
6743 ===================================================================*/
6744 PROCEDURE Get_start_and_end_dates(
6745             p_period_type              IN  VARCHAR2
6746             ,p_profile_start_date      IN  DATE
6747             ,p_profile_end_date        IN  DATE
6748             ,x_start_period_start_date OUT NOCOPY DATE --File.Sql.39 bug 4440895
6749             ,x_end_period_end_date     OUT NOCOPY DATE) --File.Sql.39 bug 4440895
6750 AS
6751 BEGIN
6752 
6753    IF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA THEN
6754 
6755       BEGIN
6756           SELECT start_date
6757           INTO   x_start_period_start_date
6758           FROM   PA_PERIODS
6759           WHERE  p_profile_start_date BETWEEN start_date AND end_date;
6760       EXCEPTION
6761           WHEN NO_DATA_FOUND THEN
6762                 SELECT MIN(START_DATE)
6763                 INTO   x_start_period_start_date
6764                 FROM   PA_PERIODS;
6765       END;
6766 
6767       BEGIN
6768           SELECT end_date
6769           INTO   x_end_period_end_date
6770           FROM   PA_PERIODS
6771           WHERE  p_profile_end_date BETWEEN start_date AND end_date;
6772       EXCEPTION
6773           WHEN NO_DATA_FOUND THEN
6774                  SELECT MAX(end_date)
6775                  INTO   x_end_period_end_date
6776                  FROM   PA_PERIODS;
6777       END;
6778 
6779    ELSIF P_PERIOD_TYPE = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL THEN
6780 
6781           BEGIN
6782                --Fetch the start date of the period in to which p_profile_start_date falls
6783                SELECT start_date
6784                INTO   x_start_period_start_date
6785                FROM   GL_PERIOD_STATUSES g
6786                      ,PA_IMPLEMENTATIONS i
6787                WHERE  adjustment_period_flag = 'N'
6788                AND    g.application_id = pa_period_process_pkg.application_id
6789                AND    g.set_of_books_id = i.set_of_books_id
6790                AND    p_profile_start_date BETWEEN g.start_date AND g.end_date;
6791           EXCEPTION
6792                WHEN NO_DATA_FOUND THEN
6793                         SELECT MIN(start_date)
6794                         INTO   x_start_period_start_date
6795                         FROM   GL_PERIOD_STATUSES g
6796                               ,PA_IMPLEMENTATIONS i
6797                         WHERE  adjustment_period_flag = 'N'
6798                         AND    g.application_id = pa_period_process_pkg.application_id
6799                         AND    g.set_of_books_id = i.set_of_books_id;
6800           END;
6801 
6802           BEGIN
6803                --Fetch the end date of the period in to which p_profile_end_date falls
6804                SELECT end_date
6805                INTO   x_end_period_end_date
6806                FROM   GL_PERIOD_STATUSES g
6807                      ,PA_IMPLEMENTATIONS i
6808                WHERE  adjustment_period_flag = 'N'
6809                AND    g.application_id = pa_period_process_pkg.application_id
6810                AND    g.set_of_books_id = i.set_of_books_id
6811                AND    p_profile_end_date BETWEEN g.start_date AND g.end_date;
6812           EXCEPTION
6813                WHEN NO_DATA_FOUND THEN
6814                         SELECT MAX(end_date)
6815                         INTO   x_end_period_end_date--Selected the max(end_date) into x_end_period_end_date. Bug 3329002.
6816                         FROM   GL_PERIOD_STATUSES g
6817                               ,PA_IMPLEMENTATIONS i
6818                         WHERE  adjustment_period_flag = 'N'
6819                         AND    g.application_id = pa_period_process_pkg.application_id
6820                         AND    g.set_of_books_id = i.set_of_books_id;
6821           END;
6822    END IF;
6823 END Get_start_and_end_dates;
6824 
6825 /*===================================================================
6826   This procedure is called from Create_Fresh_Period_Profile api
6827 ===================================================================*/
6828 
6829 PROCEDURE Get_Profile_Start_Date(
6830     p_profile_end_date    IN DATE
6831     ,p_period_type        IN VARCHAR2
6832     ,x_profile_start_date     OUT NOCOPY DATE --File.Sql.39 bug 4440895
6833     ,x_return_status          OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6834     ,x_msg_count              OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
6835     ,x_msg_data               OUT NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
6836 AS
6837     l_profile_start_date     pa_periods.start_date%TYPE;
6838    -- l_end_period_start_date  pa_periods.end_date%TYPE;
6839 
6840 BEGIN
6841 /*
6842      pa_debug.g_err_stage := 'Entered get_profile_start_date';
6843      IF P_PA_DEBUG_MODE = 'Y' THEN
6844         pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6845      END IF;
6846      pa_debug.g_err_stage := 'p_profile_end_date = ' || p_profile_end_date;
6847      IF P_PA_DEBUG_MODE = 'Y' THEN
6848         pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6849      END IF;
6850      pa_debug.g_err_stage := 'p_period_type = ' || p_period_type;
6851      IF P_PA_DEBUG_MODE = 'Y' THEN
6852         pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6853      END IF;
6854 */
6855      IF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA THEN
6856 
6857           BEGIN
6858                --Select start date of period which is 51 periods before end period
6859                SELECT start_date
6860                INTO   l_profile_start_date
6861                FROM   pa_periods a
6862                WHERE  51= (SELECT COUNT(*) FROM pa_periods b
6863                            WHERE  a.start_date < b.start_date
6864                            AND    b.start_date <= p_profile_end_date );
6865           EXCEPTION
6866                WHEN NO_DATA_FOUND THEN
6867                     --IF no such period existing select the first available period
6868 /*
6869                     pa_debug.g_err_stage := 'Fetching MIN of start date as profile start date';
6870                     IF P_PA_DEBUG_MODE = 'Y' THEN
6871                        pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6872                     END IF;
6873 */
6874                     SELECT MIN(start_date)
6875                     INTO   l_profile_start_date
6876                     FROM   pa_periods;
6877           END;
6878 
6879      ELSIF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL THEN
6880 
6881           BEGIN
6882                --Fetch the start date of the period in to which p_profile_end_date falls
6883                --Select start date of periods which is 51 periods before end period
6884 
6885                SELECT start_date
6886                INTO   l_profile_start_date
6887                FROM   GL_PERIOD_STATUSES a
6888                      ,PA_IMPLEMENTATIONS i
6889                WHERE  a.application_id = pa_period_process_pkg.application_id
6890                AND    a.set_of_books_id = i.set_of_books_id
6891                AND    a.adjustment_period_flag = 'N'
6892                AND    51= (SELECT COUNT(*) FROM GL_PERIOD_STATUSES b
6893                                                ,PA_IMPLEMENTATIONS i2
6894                            WHERE  b.adjustment_period_flag = 'N'
6895                            AND    b.application_id = pa_period_process_pkg.application_id
6896                            AND    b.set_of_books_id = i2.set_of_books_id
6897                            AND    a.start_date < b.start_date
6898                            AND    b.start_date <= p_profile_end_date);
6899           EXCEPTION
6900                WHEN NO_DATA_FOUND THEN
6901                     --IF no such period existing select the first available period
6902 /*
6903                     pa_debug.g_err_stage := 'Fetching the first available period';
6904                     IF P_PA_DEBUG_MODE = 'Y' THEN
6905                        pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6906                     END IF;
6907 */
6908                     SELECT MIN(start_date)
6909                     INTO   l_profile_start_date
6910                     FROM   GL_PERIOD_STATUSES a
6911                           ,PA_IMPLEMENTATIONS i
6912                     WHERE  a.application_id = pa_period_process_pkg.application_id
6913                     AND    a.set_of_books_id = i.set_of_books_id
6914                     AND    a.adjustment_period_flag = 'N';
6915           END;
6916      END IF;
6917 
6918     x_profile_start_date := l_profile_start_date;
6919 
6920     pa_debug.g_err_stage := 'Exiting Get_Profile_Start_Date';
6921     IF P_PA_DEBUG_MODE = 'Y' THEN
6922        pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6923     END IF;
6924 
6925 EXCEPTION
6926 
6927    WHEN Others THEN
6928         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6929         x_msg_count     := 1;
6930         x_msg_data      := SQLERRM;
6931         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_PUB'
6932                         ,p_procedure_name  => 'Get_Profile_Start_Date');
6933         pa_debug.g_err_stage:='Unexpected Error' || SQLERRM;
6934         IF P_PA_DEBUG_MODE = 'Y' THEN
6935            pa_debug.write('Get_Profile_Start_Date: ' || l_module_name,pa_debug.g_err_stage,5);
6936         END IF;
6937         RAISE;
6938 
6939 END Get_Profile_Start_Date;
6940 
6941 /*===================================================================
6942   This procedure is called from Create_Fresh_Period_Profile api
6943 ===================================================================*/
6944 PROCEDURE Get_Profile_End_Date(
6945     p_profile_start_date    IN DATE
6946     ,p_period_type          IN VARCHAR2
6947     ,x_profile_end_date     OUT NOCOPY DATE --File.Sql.39 bug 4440895
6948     ,x_return_status        OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
6949     ,x_msg_count            OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
6950     ,x_msg_data             OUT NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
6951 AS
6952     l_profile_end_date           pa_periods.end_date%TYPE;
6953 BEGIN
6954 /*
6955      pa_debug.g_err_stage := 'Entered get_profile_end_date';
6956      IF P_PA_DEBUG_MODE = 'Y' THEN
6957         pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6958      END IF;
6959      pa_debug.g_err_stage := 'p_profile_start_date = ' || p_profile_start_date;
6960      IF P_PA_DEBUG_MODE = 'Y' THEN
6961         pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6962      END IF;
6963      pa_debug.g_err_stage := 'p_period_type = ' || p_period_type;
6964      IF P_PA_DEBUG_MODE = 'Y' THEN
6965         pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6966      END IF;
6967 */
6968      IF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL THEN
6969 
6970              BEGIN
6971                   --Fetch the start date of the period in to which p_profile_start_date falls
6972                   --Select the 51st period's start date  from start period as profile end date
6973 
6974                   SELECT end_date
6975                   INTO   l_profile_end_date
6976                   FROM   GL_PERIOD_STATUSES a
6977                         ,PA_IMPLEMENTATIONS i
6978                   WHERE  a.application_id = pa_period_process_pkg.application_id
6979                   AND    a.set_of_books_id = i.set_of_books_id
6980                   AND    a.adjustment_period_flag = 'N'
6981                   AND    51= (SELECT COUNT(*) FROM GL_PERIOD_STATUSES b
6982                                                   ,PA_IMPLEMENTATIONS i2
6983                               WHERE  b.adjustment_period_flag = 'N'
6984                               AND    b.application_id = pa_period_process_pkg.application_id
6985                               AND    b.set_of_books_id = i2.set_of_books_id
6986                               AND    a.start_date > b.start_date
6987                               AND    b.start_date >= p_profile_start_date);
6988              EXCEPTION
6989                   WHEN NO_DATA_FOUND THEN
6990                        --IF no such period existing select the last available period
6991 /*
6992                        pa_debug.g_err_stage := 'Fetching last period available';
6993                        IF P_PA_DEBUG_MODE = 'Y' THEN
6994                           pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
6995                        END IF;
6996 */
6997                        SELECT MAX(end_date)
6998                        INTO   l_profile_end_date
6999                        FROM   GL_PERIOD_STATUSES a
7000                              ,PA_IMPLEMENTATIONS i
7001                        WHERE  a.application_id = pa_period_process_pkg.application_id
7002                        AND    a.set_of_books_id = i.set_of_books_id
7003                        AND    a.adjustment_period_flag = 'N';
7004              END;
7005 
7006     ELSIF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA THEN
7007 
7008              BEGIN
7009                   --Fetch the start date of the period in to which p_profile_start_date falls
7010                   --Select the 51st period's start date  from start period as profile end date
7011 
7012                   pa_debug.g_err_stage := 'Fetching profile end date';
7013                   IF P_PA_DEBUG_MODE = 'Y' THEN
7014                      pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
7015                   END IF;
7016 
7017                   SELECT end_date
7018                   INTO   l_profile_end_date
7019                   FROM   pa_periods a
7020                   WHERE  51= (SELECT COUNT(*) FROM pa_periods b
7021                               WHERE  a.start_date > b.start_date
7022                               AND    b.start_date >= p_profile_start_date );
7023 
7024              EXCEPTION
7025                        WHEN NO_DATA_FOUND THEN
7026 
7027                        --IF no such period existing select the last available period
7028 /*
7029                        pa_debug.g_err_stage := 'Fetching last available period';
7030                        IF P_PA_DEBUG_MODE = 'Y' THEN
7031                           pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
7032                        END IF;
7033  */
7034                        SELECT MAX(end_date)
7035                        INTO   l_profile_end_date
7036                        FROM   pa_periods;
7037              END;
7038     END IF;
7039 
7040     x_profile_end_date := l_profile_end_date;
7041 
7042     pa_debug.g_err_stage := 'Exiting Get_Profile_End_Date';
7043     IF P_PA_DEBUG_MODE = 'Y' THEN
7044        pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,3);
7045     END IF;
7046 
7047 EXCEPTION
7048 
7049    WHEN Others THEN
7050         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7051         x_msg_count     := 1;
7052         x_msg_data      := SQLERRM;
7053         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_PUB'
7054                         ,p_procedure_name  => 'Get_Profile_End_Date');
7055         pa_debug.g_err_stage:='Unexpected Error' ||SQLERRM;
7056         IF P_PA_DEBUG_MODE = 'Y' THEN
7057            pa_debug.write('Get_Profile_End_Date: ' || l_module_name,pa_debug.g_err_stage,5);
7058         END IF;
7059         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7060 
7061 END Get_Profile_End_Date;
7062 
7063 
7064 /*===================================================================
7065   This api creates records in pa_proj_period_profiles for a project
7066   and plan period type based on
7067   1)project start and end dates if both are available
7068   2)The  budget lines period distribution in the target project id
7069 ===================================================================*/
7070 
7071 PROCEDURE Create_Fresh_Period_Profile(
7072     p_project_id           IN     NUMBER
7073     ,p_period_type         IN     VARCHAR2
7074     ,x_period_profile_id   OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
7075     ,x_return_status       OUT    NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7076     ,x_msg_count           OUT    NOCOPY NUMBER --File.Sql.39 bug 4440895
7077     ,x_msg_data            OUT    NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
7078 AS
7079 
7080     l_return_status      VARCHAR2(2000);
7081     l_msg_count          NUMBER :=0;
7082     l_msg_data           VARCHAR2(2000);
7083     l_data               VARCHAR2(2000);
7084     l_msg_index_out      NUMBER;
7085     l_debug_mode         VARCHAR2(30);
7086 
7087 
7088     l_start_date         gl_periods.start_date%TYPE;
7089     l_end_date           gl_periods.end_date%TYPE;
7090     l_profile_start_date gl_periods.start_date%TYPE;
7091     l_profile_end_date   gl_periods.end_date%TYPE;
7092 
7093     l_project_start_date       gl_periods.start_date%TYPE;
7094     l_project_completion_date  gl_periods.end_date%TYPE;
7095 
7096     l_number_of_periods     NUMBER;
7097     l_period_set_name       gl_sets_of_books.period_set_name%TYPE;
7098     l_accounted_period_type gl_sets_of_books.accounted_period_type%TYPE;
7099     l_pa_period_type        pa_implementations.pa_period_type%TYPE;
7100 
7101     l_plan_start_date       gl_periods.start_date%TYPE;
7102     l_plan_end_date         gl_periods.end_date%TYPE;
7103 
7104     l_period_profile_id     pa_budget_versions.period_profile_id%TYPE;
7105 
7106 BEGIN
7107 
7108     x_msg_count := 0;
7109     x_return_status := FND_API.G_RET_STS_SUCCESS;
7110 
7111     pa_debug.set_err_stack('Create_Fresh_Period_Profile');
7112     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
7113     l_debug_mode := NVL(l_debug_mode, 'Y');
7114     IF P_PA_DEBUG_MODE = 'Y' THEN
7115        pa_debug.set_process('Create_Fresh_Period_Profile: ' || 'PLSQL','LOG',l_debug_mode);
7116     END IF;
7117 
7118     -- Check if  source project id is  NULL,if so throw an error message
7119 
7120     pa_debug.g_err_stage := 'Checking for valid parameters:';
7121     IF P_PA_DEBUG_MODE = 'Y' THEN
7122        pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7123     END IF;
7124 
7125     IF (p_project_id IS NULL)  OR (p_period_type IS NULL) THEN
7126 
7127         pa_debug.g_err_stage := 'Project='||p_project_id;
7128         IF P_PA_DEBUG_MODE = 'Y' THEN
7129            pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,5);
7130         END IF;
7131         pa_debug.g_err_stage := 'Period_type = '||p_period_type;
7132         IF P_PA_DEBUG_MODE = 'Y' THEN
7133            pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,5);
7134         END IF;
7135         PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
7136                              p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
7137         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
7138     END IF;
7139 
7140     pa_debug.g_err_stage := 'Parameter validation complete';
7141     IF P_PA_DEBUG_MODE = 'Y' THEN
7142        pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7143     END IF;
7144 
7145     --Fetch project start and completion dates
7146 
7147     SELECT start_date
7148            ,completion_date
7149     INTO   l_project_start_date
7150            ,l_project_completion_date
7151     FROM   pa_projects_all
7152     WHERE  project_id = p_project_id;
7153 
7154     -- IF both start and completion dates are not null choose them as
7155     -- period profiles' start date and end dates
7156 
7157     IF (l_project_start_date IS NOT NULL) AND
7158        (l_project_completion_date IS NOT NULL)
7159     THEN
7160          pa_debug.g_err_stage := 'Calling get_start_and_end_dates procedure';
7161          IF P_PA_DEBUG_MODE = 'Y' THEN
7162             pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7163          END IF;
7164 
7165          Get_start_and_end_dates( p_period_type              => p_period_type
7166                                   ,p_profile_start_date      => l_project_start_date
7167                                   ,p_profile_end_date        => l_project_completion_date
7168                                   ,x_start_period_start_date => l_start_date
7169                                   ,x_end_period_end_date     => l_end_date);
7170 
7171     ELSE
7172          --Fetch MIN and MAX of start dates of pa_budget_lines
7173 
7174          pa_debug.g_err_stage := 'Selecting start and end dates from pa_budget_lines of the project';
7175          IF P_PA_DEBUG_MODE = 'Y' THEN
7176             pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7177          END IF;
7178 
7179          SELECT MIN (pbl.start_date)
7180                 ,MAX(pbl.end_date)
7181          INTO   l_start_date
7182                 ,l_end_date
7183          FROM   pa_budget_versions pbv             --bug#2708524 pa_resource_assignments pra
7184                 ,pa_budget_lines pbl
7185          WHERE  pbv.project_id = p_project_id
7186          AND    pbl.budget_version_id = pbv.budget_version_id
7187          AND    PA_FIN_PLAN_UTILS.GET_TIME_PHASED_CODE(pbv.budget_version_id)
7188                           = DECODE(p_period_type,PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_G,
7189                                                  PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA,PA_FP_CONSTANTS_PKG.G_TIME_PHASED_CODE_P) ;
7190 
7191 
7192     END IF;
7193 
7194     -- IF l_start_date or l_end_date is null at this step, we would just return to
7195     -- the calling program as there is no way we can create period profile for  this project.
7196 
7197     IF l_start_date IS NULL OR l_end_date IS NULL THEN
7198 
7199             pa_debug.g_err_stage := 'Profile id cant be created';
7200             IF P_PA_DEBUG_MODE = 'Y' THEN
7201                pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7202             END IF;
7203             x_period_profile_id := NULL;
7204             pa_debug.reset_err_stack;
7205             RETURN;
7206      END IF;
7207 
7208     -- Fetch period_set_name, accounted_period_type and pa_period_type
7209     -- required for creation of period profiles as follows
7210 
7211     pa_debug.g_err_stage := 'Fetching accounted_period_type, pa_period_type';
7212     IF P_PA_DEBUG_MODE = 'Y' THEN
7213        pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7214     END IF;
7215 
7216     SELECT b.period_set_name
7217            ,DECODE(p_period_type,
7218                    PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA ,pa_period_type,
7219                    PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL ,accounted_period_type) --accounted_period_type
7220            ,DECODE(p_period_type,
7221                    PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA ,pa_period_type,
7222                    PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL ,NULL) --pa_period_type
7223     INTO  l_period_set_name
7224           ,l_accounted_period_type
7225           ,l_pa_period_type
7226     FROM  pa_implementations  a
7227           ,gl_sets_of_books  b
7228     WHERE  a.set_of_books_id = b.set_of_books_id;
7229 
7230     IF TRUNC(SYSDATE) BETWEEN l_start_date AND l_end_date THEN /*2690087*/
7231 
7232          --Select the number of periods between start and end date
7233 /*
7234          pa_debug.g_err_stage := 'Fetching number of periods';
7235          IF P_PA_DEBUG_MODE = 'Y' THEN
7236             pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7237          END IF;
7238 */
7239          IF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA THEN
7240 
7241                SELECT count(*)
7242                INTO   l_number_of_periods
7243                FROM   PA_PERIODS
7244                WHERE  start_date BETWEEN TRUNC(SYSDATE) AND l_end_date; /*2690087*/
7245 
7246          ELSIF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL THEN
7247 
7248                SELECT count(*)
7249                INTO   l_number_of_periods
7250                FROM   GL_PERIOD_STATUSES a
7251                      ,PA_IMPLEMENTATIONS i
7252                WHERE  a.application_id = pa_period_process_pkg.application_id
7253                AND    a.set_of_books_id = i.set_of_books_id
7254                AND    a.adjustment_period_flag = 'N'
7255                AND    start_date BETWEEN TRUNC(SYSDATE) AND l_end_date; -- Bug :- 2623941, last condition has been put for 2623941
7256                /* Bug:- 2690087, sysdate has been changed to trunc(sysdate) */
7257 
7258          END IF;
7259 
7260          IF l_number_of_periods < 52 THEN
7261 
7262               --Select end date as start date of end period
7263 
7264               l_profile_end_date := l_end_date;
7265 
7266               pa_debug.g_err_stage := 'Calling get_profile_start_date';
7267               IF P_PA_DEBUG_MODE = 'Y' THEN
7268                  pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7269               END IF;
7270 
7271               Get_Profile_Start_Date (
7272                        p_profile_end_date    => l_profile_end_date
7273                        ,p_period_type        => p_period_type
7274                        ,x_profile_start_date => l_profile_start_date
7275                        ,x_return_status      => l_return_status
7276                        ,x_msg_count          => l_msg_count
7277                        ,x_msg_data           => l_msg_data );
7278 
7279                --If fetched l_profile_start_date is less than l_start_date then
7280                --choose l_start_date as l_profile_start_date
7281 
7282                IF l_profile_start_date < l_start_date THEN
7283 
7284                    l_profile_start_date := l_start_date;
7285 
7286                END IF;
7287 
7288          ELSIF l_number_of_periods >= 52 THEN
7289 
7290             /* Start of changes for the bug :- 2623941 */
7291 
7292               /* l_profile_start_date := SYSDATE; */  --commented out for bug :- 2623941
7293 
7294               BEGIN
7295                    --Fetch the start date of the period in to which sysdate falls
7296 
7297                    IF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_GL THEN
7298 
7299                            SELECT start_date
7300                            INTO   l_profile_start_date
7301                            FROM   GL_PERIOD_STATUSES g
7302                                  ,PA_IMPLEMENTATIONS i
7303                            WHERE  adjustment_period_flag = 'N'
7304                            AND    g.application_id = pa_period_process_pkg.application_id
7305                            AND    g.set_of_books_id = i.set_of_books_id
7306                            AND    TRUNC(SYSDATE) BETWEEN g.start_date AND g.end_date; /* Bug:- 2690087 */
7307 
7308                    ELSIF p_period_type = PA_FP_CONSTANTS_PKG.G_PERIOD_TYPE_PA THEN
7309 
7310                           SELECT start_date
7311                           INTO   l_profile_start_date
7312                           FROM   PA_PERIODS
7313                           WHERE  TRUNC(SYSDATE) BETWEEN start_date AND end_date; /* Bug:- 2690087 */
7314 
7315                    END IF;
7316 
7317               EXCEPTION
7318                    WHEN Others THEN
7319                         pa_debug.g_err_stage:='Fetching start date of the period into which sysdate falls'||SQLERRM;
7320                         IF P_PA_DEBUG_MODE = 'Y' THEN
7321                            pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,5);
7322                         END IF;
7323                         RAISE;
7324               END;
7325 
7326             /* End of changes for the bug :- 2623941 */
7327 
7328               pa_debug.g_err_stage := 'Calling get_profile_end_date';
7329               IF P_PA_DEBUG_MODE = 'Y' THEN
7330                  pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7331               END IF;
7332 
7333               Get_Profile_End_Date (
7334                        p_profile_start_date =>  l_profile_start_date
7335                        ,p_period_type       =>  p_period_type
7336                        ,x_profile_end_date  =>  l_profile_end_date
7337                        ,x_return_status     =>  l_return_status
7338                        ,x_msg_count         =>  l_msg_count
7339                        ,x_msg_data          =>  l_msg_data );
7340 
7341                --If l_end_date is less than fetched l_profile_end_date then
7342                --choose l_end_date as l_profile_end_date
7343 
7344                IF l_end_date < l_profile_end_date THEN
7345 
7346                    l_profile_end_date := l_end_date;
7347 
7348                END IF;
7349 
7350          END IF; --l_number_of_periods
7351 
7352     ELSIF (l_start_date > TRUNC(SYSDATE))   THEN  /* Bug:- 2690087 */
7353 
7354          --Select start_date as start date of start period
7355 
7356          l_profile_start_date := l_start_date;
7357 
7358          pa_debug.g_err_stage := 'Calling get_profile_end_date';
7359          IF P_PA_DEBUG_MODE = 'Y' THEN
7360             pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7361          END IF;
7362 
7363          Get_Profile_End_Date (
7364                   p_profile_start_date =>  l_profile_start_date
7365                   ,p_period_type       =>  p_period_type
7366                   --,p_period_set_name   =>  l_period_set_name
7367                   ,x_profile_end_date  => l_profile_end_date
7368                   ,x_return_status     => l_return_status
7369                   ,x_msg_count         => l_msg_count
7370                   ,x_msg_data          => l_msg_data );
7371 
7372          --If l_end_date is less than fetched l_profile_end_date then
7373          --choose l_end_date as l_profile_end_date
7374 
7375          IF l_end_date < l_profile_end_date THEN
7376 
7377              l_profile_end_date := l_end_date;
7378 
7379          END IF;
7380 
7381     ELSIF (l_end_date < TRUNC(SYSDATE)) THEN /* Bug:- 2690087 */
7382 
7383          --Select end date as profile periods last period start date
7384 
7385          l_profile_end_date := l_end_date;
7386 
7387          pa_debug.g_err_stage := 'Calling get_profile_start_date';
7388          IF P_PA_DEBUG_MODE = 'Y' THEN
7389             pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7390          END IF;
7391 
7392          Get_Profile_Start_Date (
7393                   p_profile_end_date   =>  l_profile_end_date
7394                   ,p_period_type       =>  p_period_type
7395                   --,p_period_set_name   =>  l_period_set_name
7396                   ,x_profile_start_date => l_profile_start_date
7397                   ,x_return_status      => l_return_status
7398                   ,x_msg_count          => l_msg_count
7399                   ,x_msg_data           => l_msg_data );
7400 
7401           --If fetched l_profile_start_date is less than l_start_date then
7402           --choose l_start_date as l_profile_start_date
7403 
7404           IF l_profile_start_date < l_start_date THEN
7405 
7406               l_profile_start_date := l_start_date;
7407 
7408           END IF;
7409 
7410     END IF; --SYSDATE BETWEEN l_start_date AND l_end_date
7411 
7412     --Call maintain_prj_period_profile to create fresh period profile id
7413 
7414     --Null out  number of periods
7415 
7416     l_number_of_periods:= NULL;
7417 
7418     pa_debug.g_err_stage := 'Calling Maintain_Prj_Period_Profile api';
7419     IF P_PA_DEBUG_MODE = 'Y' THEN
7420        pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7421     END IF;
7422 
7423     Pa_Prj_Period_Profile_Utils.Maintain_Prj_Period_Profile(
7424                    p_project_id           => p_project_id
7425                    ,p_period_profile_type => PA_FP_CONSTANTS_PKG.G_PD_PROFILE_FIN_PLANNING
7426                    ,p_plan_period_type    => p_period_type
7427                    ,p_period_set_name     => l_period_set_name
7428                    ,p_gl_period_type      => l_accounted_period_type
7429                    ,p_pa_period_type      => l_pa_period_type
7430                    ,p_start_date          => l_profile_start_date
7431                    ,px_end_date           => l_profile_end_date
7432                    ,px_period_profile_id  => l_period_profile_id
7433                    ,p_commit_flag         => 'N'
7434                    ,px_number_of_periods  => l_number_of_periods
7435                    ,x_plan_start_date     => l_plan_start_date
7436                    ,x_plan_end_date       => l_plan_end_date
7437                    ,x_return_status       => l_return_status
7438                    ,x_msg_count           => l_msg_count
7439                    ,x_msg_data            => l_msg_data );
7440 
7441     --Return the newly fetched profile id
7442 
7443     x_period_profile_id := l_period_profile_id;
7444     pa_debug.g_err_stage := ' exiting Create_Fresh_Period_Profile';
7445     IF P_PA_DEBUG_MODE = 'Y' THEN
7446        pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,3);
7447     END IF;
7448     pa_debug.reset_err_stack;
7449 
7450 EXCEPTION
7451 
7452    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
7453         l_msg_count := FND_MSG_PUB.count_msg;
7454         IF l_msg_count = 1 THEN
7455              PA_INTERFACE_UTILS_PUB.get_messages
7456                    (p_encoded        => FND_API.G_TRUE
7457                     ,p_msg_index      => 1
7458                     ,p_msg_count      => l_msg_count
7459                     ,p_msg_data       => l_msg_data
7460                     ,p_data           => l_data
7461                     ,p_msg_index_out  => l_msg_index_out);
7462              x_msg_data := l_data;
7463              x_msg_count := l_msg_count;
7464         ELSE
7465             x_msg_count := l_msg_count;
7466         END IF;
7467         pa_debug.g_err_stage:='Invalid Arguments Passed';
7468         IF P_PA_DEBUG_MODE = 'Y' THEN
7469            pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,5);
7470         END IF;
7471         x_return_status:= FND_API.G_RET_STS_ERROR;
7472         pa_debug.reset_err_stack;
7473         RAISE;
7474 
7475    WHEN Others THEN
7476         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7477         x_msg_count     := 1;
7478         x_msg_data      := SQLERRM;
7479         FND_MSG_PUB.add_exc_msg( p_pkg_name=> 'PA_FIN_PLAN_PUB'
7480                         ,p_procedure_name  => 'Create_Fresh_Period_Profile');
7481         pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
7482         IF P_PA_DEBUG_MODE = 'Y' THEN
7483            pa_debug.write('Create_Fresh_Period_Profile: ' || l_module_name,pa_debug.g_err_stage,5);
7484         END IF;
7485         pa_debug.reset_err_stack;
7486         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7487 
7488 END Create_Fresh_Period_Profile;
7489 
7490 /*=======================================================================================*/
7491 
7492  PROCEDURE INSERT_PLAN_LINES_TMP_BULK
7493                                   (p_res_assignment_tbl        IN   p_res_assignment_tbl_typ
7494                                   ,p_period_name_tbl           IN   p_period_name_tbl_typ
7495                                   ,p_start_date_tbl            IN   p_start_date_tbl_typ
7496                                   ,p_end_date_tbl              IN   p_end_date_tbl_typ
7497                                   ,p_currency_type             IN   pa_proj_periods_denorm.currency_type%TYPE
7498                                   ,p_currency_code_tbl         IN   p_currency_code_tbl_typ
7499                                   ,p_quantity_tbl              IN   p_quantity_tbl_typ
7500                                   ,p_raw_cost_tbl              IN   p_cost_tbl_typ
7501                                   ,p_burdened_cost_tbl         IN   p_cost_tbl_typ
7502                                   ,p_revenue_tbl               IN   p_cost_tbl_typ
7503                                   ,p_old_quantity_tbl          IN   p_quantity_tbl_typ
7504                                   ,p_old_raw_cost_tbl          IN   p_cost_tbl_typ
7505                                   ,p_old_burdened_cost_tbl     IN   p_cost_tbl_typ
7506                                   ,p_old_revenue_tbl           IN   p_cost_tbl_typ
7507                                   ,p_margin_tbl                IN   p_cost_tbl_typ
7508                                   ,p_margin_percent_tbl        IN   p_cost_tbl_typ
7509                                   ,p_old_margin_tbl            IN   p_cost_tbl_typ
7510                                   ,p_old_margin_percent_tbl    IN   p_cost_tbl_typ
7511                                   ,p_buck_period_code_tbl      IN   p_buck_period_code_tbl_typ
7512                                   ,p_parent_assignment_id_tbl  IN   p_res_assignment_tbl_typ
7513                                   ,p_delete_flag_tbl           IN   p_delete_flag_tbl_typ
7514                                   ,p_source_txn_curr_code_tbl  IN   p_currency_code_tbl_typ
7515                                   ,x_return_status             OUT  NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7516                                   ,x_msg_count                 OUT  NOCOPY NUMBER --File.Sql.39 bug 4440895
7517                                   ,x_msg_data                  OUT  NOCOPY VARCHAR2  ) IS --File.Sql.39 bug 4440895
7518 
7519   l_stage NUMBER :=100 ;
7520   l_debug_mode VARCHAR2(1) ;
7521 
7522  BEGIN
7523 
7524      -- Set the error stack.
7525         pa_debug.set_err_stack('PA_FIN_PLAN_PUB.INSERT_PLAN_LINES_TMP_BULK');
7526 
7527      -- Get the Debug mode into local variable and set it to 'Y' if its NULL
7528         fnd_profile.get('pa_debug_MODE',l_debug_mode);
7529         l_debug_mode := NVL(l_debug_mode, 'Y');
7530 
7531      -- Initialize the return status to success
7532         x_return_status := FND_API.G_RET_STS_SUCCESS;
7533 
7534         IF P_PA_DEBUG_MODE = 'Y' THEN
7535            pa_debug.set_process('INSERT_PLAN_LINES_TMP_BULK: ' || 'PLSQL','LOG',l_debug_mode);
7536         END IF;
7537 
7538         pa_debug.g_err_stage := TO_CHAR(l_stage)||':In PA_FIN_PLAN_PUB.INSERT_PLAN_LINES_TMP_BULK ';
7539         IF P_PA_DEBUG_MODE = 'Y' THEN
7540            pa_debug.write('INSERT_PLAN_LINES_TMP_BULK: ' || l_module_name,pa_debug.g_err_stage,2);
7541         END IF;
7542 
7543 
7544          /*
7545           * Bulk Insert records into PA_FP_ELEMENTS table for the records fetched
7546           * from cursor top_task_cur.
7547           */
7548        pa_debug.g_err_stage := TO_CHAR(l_stage)||': INSERT into fin plan lines tmp';
7549        IF P_PA_DEBUG_MODE = 'Y' THEN
7550           pa_debug.write('INSERT_PLAN_LINES_TMP_BULK: ' || l_module_name,pa_debug.g_err_stage,2);
7551        END IF;
7552 
7553 
7554    FORALL i in p_res_assignment_tbl.first..p_res_assignment_tbl.last
7555                  INSERT INTO PA_FIN_PLAN_LINES_TMP (
7556                           OBJECT_ID
7557                          ,OBJECT_TYPE_CODE
7558                          ,RESOURCE_ASSIGNMENT_ID
7559                          ,PERIOD_NAME
7560                          ,START_DATE
7561                          ,END_DATE
7562                          ,CURRENCY_TYPE
7563                          ,CURRENCY_CODE
7564                          ,QUANTITY
7565                          ,RAW_COST
7566                          ,BURDENED_COST
7567                          ,REVENUE
7568                          ,OLD_QUANTITY
7569                          ,OLD_RAW_COST
7570                          ,OLD_BURDENED_COST
7571                          ,OLD_REVENUE
7572                          ,MARGIN
7573                          ,MARGIN_PERCENTAGE
7574                          ,OLD_MARGIN
7575                          ,OLD_MARGIN_PERCENTAGE
7576                          ,BUCKETING_PERIOD_CODE
7577                          ,PARENT_ASSIGNMENT_ID
7578                          ,DELETE_FLAG
7579                          ,SOURCE_TXN_CURRENCY_CODE
7580                          )
7581                VALUES  (  p_res_assignment_tbl(i) /* Bug#  2677867-Object id should not be -1 even for FP */
7582                          ,PA_FP_CONSTANTS_PKG.G_OBJECT_TYPE_RES_ASSIGNMENT
7583                          ,p_res_assignment_tbl(i)
7584                          ,p_period_name_tbl(i)
7585                          ,p_start_date_tbl(i)
7586                          ,p_end_date_tbl(i)
7587                          ,p_currency_type
7588                          ,p_currency_code_tbl(i)
7589                          ,p_quantity_tbl(i)
7590                          ,p_raw_cost_tbl(i)
7591                          ,p_burdened_cost_tbl(i)
7592                          ,p_revenue_tbl(i)
7593                          ,p_old_quantity_tbl(i) /* Bug # 2738047 : Corrected the order of the table */
7594                          ,p_old_raw_cost_tbl(i)
7595                          ,p_old_burdened_cost_tbl(i)
7596                          ,p_old_revenue_tbl(i)
7597                          ,p_margin_tbl(i)
7598                          ,p_margin_percent_tbl(i)
7599                          ,p_old_margin_tbl(i)
7600                          ,p_old_margin_percent_tbl(i)
7601                          ,p_buck_period_code_tbl(i)
7602                          ,p_parent_assignment_id_tbl(i)
7603                          ,p_delete_flag_tbl(i)
7604                          ,p_source_txn_curr_code_tbl(i)) ;
7605 
7606                  pa_debug.g_err_stage := TO_CHAR(l_stage)||': INSERTED ' || sql%rowcount || ' recs into fin plan lines tmp';
7607                  IF P_PA_DEBUG_MODE = 'Y' THEN
7608                     pa_debug.write('INSERT_PLAN_LINES_TMP_BULK: ' || l_module_name,pa_debug.g_err_stage,2);
7609                  END IF;
7610 
7611           pa_debug.reset_err_stack;  -- bug 2815593
7612   EXCEPTION
7613     WHEN OTHERS THEN
7614           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7615           x_msg_count     := 1;
7616           x_msg_data      := SQLERRM;
7617           FND_MSG_PUB.add_exc_msg
7618              ( p_pkg_name       => 'PA_FP_COPY_FROM_PKG'
7619               ,p_procedure_name =>  pa_debug.G_Err_Stack );
7620           IF P_PA_DEBUG_MODE = 'Y' THEN
7621              pa_debug.write('INSERT_PLAN_LINES_TMP_BULK: ' || l_module_name,SQLERRM,4);
7622              pa_debug.write('INSERT_PLAN_LINES_TMP_BULK: ' || l_module_name,pa_debug.G_Err_Stack,4);
7623           END IF;
7624           pa_debug.reset_err_stack;
7625 
7626           raise FND_API.G_EXC_UNEXPECTED_ERROR ;
7627 
7628  END INSERT_PLAN_LINES_TMP_BULK ;
7629 
7630 
7631 /*---------------------------------------------------------------------------------------------
7632   This procedure will populate the Lines Temp table pa_fin_plan_lines_tmp, with records from
7633   Budget Lines with appropriate values and call Maintain Matrix API to update the
7634   USER_ENTERED level records into pa_proj_periods_denorm table.
7635 ---------------------------------------------------------------------------------------------*/
7636 PROCEDURE Call_Maintain_Plan_Matrix (
7637     p_budget_version_id    IN     pa_budget_versions.budget_version_id%TYPE
7638     ,p_data_source         IN     VARCHAR2
7639     ,x_return_status          OUT NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
7640     ,x_msg_count              OUT NOCOPY NUMBER --File.Sql.39 bug 4440895
7641     ,x_msg_data               OUT NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
7642 AS
7643 
7644      l_msg_count       NUMBER := 0;
7645      l_data            VARCHAR2(2000);
7646      l_msg_data        VARCHAR2(2000);
7647      l_msg_index_out   NUMBER;
7648      l_return_status   VARCHAR2(2000);
7649      l_debug_mode      VARCHAR2(30);
7650 
7651      l_project_id               pa_budget_versions.PROJECT_ID%TYPE;
7652      l_period_profile_id        pa_budget_versions.PERIOD_PROFILE_ID%TYPE;
7653      l_budget_version_type      pa_budget_versions.VERSION_TYPE%TYPE;
7654      l_fp_preference_code       pa_proj_fp_options.FIN_PLAN_PREFERENCE_CODE%TYPE;
7655      l_margin_derived_from_code pa_proj_fp_options.MARGIN_DERIVED_FROM_CODE%TYPE;
7656 
7657      l_plsql_max_array_size     NUMBER := 200;
7658      l_tbl_index                NUMBER := 1;
7659 
7660      /* Record Definitions */
7661      amt_rec pa_plan_matrix.amount_type_tabtyp;
7662 
7663      /* all table types */
7664      l_res_assignment_tbl      p_res_assignment_tbl_typ;
7665      l_period_name_tbl         p_period_name_tbl_typ ;
7666      l_start_date_tbl          p_start_date_tbl_typ;
7667      l_end_date_tbl            p_end_date_tbl_typ;
7668 
7669      l_txn_raw_cost_tbl        p_cost_tbl_typ;
7670      l_txn_burdened_cost_tbl   p_cost_tbl_typ;
7671      l_txn_revenue_tbl         p_cost_tbl_typ;
7672      l_txn_margin_tbl          p_cost_tbl_typ;
7673      l_txn_margin_percent_tbl  p_cost_tbl_typ;
7674 
7675      l_proj_raw_cost_tbl       p_cost_tbl_typ;
7676      l_proj_burdened_cost_tbl  p_cost_tbl_typ;
7677      l_proj_revenue_tbl        p_cost_tbl_typ;
7678      l_proj_margin_tbl         p_cost_tbl_typ;
7679      l_proj_margin_percent_tbl p_cost_tbl_typ;
7680 
7681      l_projfunc_raw_cost_tbl       p_cost_tbl_typ;
7682      l_projfunc_burd_cost_tbl      p_cost_tbl_typ;
7683      l_projfunc_revenue_tbl        p_cost_tbl_typ;
7684      l_projfunc_margin_tbl         p_cost_tbl_typ;
7685      l_projfunc_margin_percent_tbl p_cost_tbl_typ;
7686 
7687      l_quantity_tbl                p_quantity_tbl_typ;
7688 
7689      l_old_txn_raw_cost_tbl              p_cost_tbl_typ;
7690      l_old_txn_burdened_cost_tbl         p_cost_tbl_typ;
7691      l_old_txn_revenue_tbl               p_cost_tbl_typ;
7692      l_old_txn_margin_tbl                p_cost_tbl_typ;
7693      l_old_txn_margin_percent_tbl        p_cost_tbl_typ;
7694 
7695 
7696      l_old_proj_raw_cost_tbl             p_cost_tbl_typ;
7697      l_old_proj_burd_cost_tbl            p_cost_tbl_typ;
7698      l_old_proj_revenue_tbl              p_cost_tbl_typ;
7699      l_old_proj_margin_tbl               p_cost_tbl_typ;
7700      l_old_proj_margin_percent_tbl       p_cost_tbl_typ;
7701 
7702      l_old_projfunc_raw_cost_tbl         p_cost_tbl_typ;
7703      l_old_projfunc_burd_cost_tbl        p_cost_tbl_typ;
7704      l_old_projfunc_revenue_tbl          p_cost_tbl_typ;
7705      l_old_projfunc_margin_tbl           p_cost_tbl_typ;
7706      l_old_projfunc_margin_pct_tbl       p_cost_tbl_typ;
7707 
7708      l_old_quantity_tbl                  p_quantity_tbl_typ;
7709 
7710      l_txn_curr_code_tbl       p_currency_code_tbl_typ;
7711      l_proj_curr_code_tbl      p_currency_code_tbl_typ;
7712      l_projfunc_curr_code_tbl  p_currency_code_tbl_typ;
7713 
7714      l_buck_period_code_tbl    p_buck_period_code_tbl_typ;
7715      l_delete_flag_tbl         p_delete_flag_tbl_typ;
7716      l_parent_assignment_tbl   p_res_assignment_tbl_typ;
7717 
7718      /* 2602869: In the below cursors when populating the margin percentage,
7719         if the divisor, i.e. revenue is 0 then making it NULL, so that the
7720         margin % becomes NULL and there is no divide by 0 error. */
7721 
7722      CURSOR budget_lines_cur IS
7723      SELECT  pbl.resource_assignment_id
7724             ,period_name
7725             ,start_date
7726             ,end_date
7727             ,txn_currency_code
7728             ,project_currency_code
7729             ,projfunc_currency_code
7730             ,quantity
7731             ,txn_raw_cost
7732             ,txn_burdened_cost
7733             ,txn_revenue
7734             ,null txn_margin
7735             ,null txn_margin_percent
7736             ,project_raw_cost
7737             ,project_burdened_cost
7738             ,project_revenue
7739             ,(project_revenue - decode(l_margin_derived_from_code,'R',project_raw_cost
7740                                                                  ,'B',project_burdened_cost)) project_margin
7741             ,((project_revenue - decode(l_margin_derived_from_code,'R',project_raw_cost
7742                                                                   ,'B',project_burdened_cost))/
7743                                                                   decode(project_revenue,0,NULL,project_revenue))*100
7744                project_margin_percentage
7745             ,raw_cost
7746             ,burdened_cost
7747             ,revenue
7748             ,(revenue - decode(l_margin_derived_from_code,'R',raw_cost
7749                                                          ,'B',burdened_cost)) projfunc_margin
7750             ,((revenue - decode(l_margin_derived_from_code,'R',raw_cost
7751                                                           ,'B',burdened_cost))/
7752                                                            decode(revenue,0,NULL,revenue))*100 projfunc_margin_percentage
7753             ,null old_quantity
7754             ,null old_txn_raw_cost
7755             ,null old_txn_burdened_cost
7756             ,null old_txn_revenue
7757             ,null old_txn_margin
7758             ,null old_txn_margin_percent
7759             ,null old_proj_raw_cost
7760             ,null old_proj_burdened_cost
7761             ,null old_proj_revenue
7762             ,null old_proj_margin
7763             ,null old_proj_margin_percent
7764             ,null old_projfunc_raw_cost
7765             ,null old_projfunc_burdened_cost
7766             ,null old_projfunc_revenue
7767             ,null old_projfunc_margin
7768             ,null old_projfunc_margin_percent
7769             ,bucketing_period_code
7770             ,pra.parent_assignment_id
7771             ,null delete_flag
7772        FROM pa_resource_assignments pra, pa_budget_lines pbl
7773       WHERE pra.budget_version_id = p_budget_version_id
7774         AND pra.resource_assignment_id = pbl.resource_assignment_id;
7775 
7776      CURSOR rollup_tmp_cur IS
7777      SELECT  frt.resource_assignment_id
7778             ,period_name
7779             ,start_date
7780             ,end_date
7781             ,txn_currency_code
7782             ,project_currency_code
7783             ,projfunc_currency_code
7784             ,quantity
7785             ,txn_raw_cost
7786             ,txn_burdened_cost
7787             ,txn_revenue
7788             ,null txn_margin
7789             ,null txn_margin_percent
7790             ,project_raw_cost
7791             ,project_burdened_cost
7792             ,project_revenue
7793             ,(project_revenue - decode(l_margin_derived_from_code,'R',project_raw_cost
7794                                                                  ,'B',project_burdened_cost)) project_margin
7795             ,((project_revenue - decode(l_margin_derived_from_code,'R',project_raw_cost
7796                                                                   ,'B',project_burdened_cost))/
7797                                                                   decode(project_revenue,0,NULL,project_revenue))*100
7798                project_margin_percentage
7799             ,projfunc_raw_cost
7800             ,projfunc_burdened_cost
7801             ,projfunc_revenue
7802             ,(projfunc_revenue - decode(l_margin_derived_from_code,'R',projfunc_raw_cost
7803                                                                   ,'B',projfunc_burdened_cost)) projfunc_margin
7804             ,((projfunc_revenue - decode(l_margin_derived_from_code,'R',projfunc_raw_cost
7805                                                                    ,'B',projfunc_burdened_cost))/
7806                                                                    decode(projfunc_revenue,0,NULL,projfunc_revenue))*100
7807                projfunc_margin_percentage
7808             ,old_quantity
7809             ,NULL old_txn_raw_cost
7810             ,NULL old_txn_burdened_cost
7811             ,null old_txn_revenue
7812             ,null old_txn_margin
7813             ,null old_txn_margin_percent
7814             ,old_proj_raw_cost
7815             ,old_proj_burdened_cost
7816             ,old_proj_revenue
7817             ,(old_proj_revenue - decode(l_margin_derived_from_code,'R',old_proj_raw_cost
7818                                                                    ,'B',old_proj_burdened_cost)) old_project_margin
7819             ,((old_proj_revenue - decode(l_margin_derived_from_code,'R',old_proj_raw_cost
7820                                                                    ,'B',old_proj_burdened_cost))/
7821                                                                    decode(old_proj_revenue,0,NULL,old_proj_revenue))*100
7822                old_project_margin_percentage
7823             ,old_projfunc_raw_cost
7824             ,old_projfunc_burdened_cost
7825             ,old_projfunc_revenue
7826             ,(old_projfunc_revenue - decode(l_margin_derived_from_code,'R',old_projfunc_raw_cost
7827                                                                    ,'B',old_projfunc_burdened_cost)) projfunc_margin
7828             ,((old_projfunc_revenue - decode(l_margin_derived_from_code,'R',old_projfunc_raw_cost
7829                                                                     ,'B',old_projfunc_burdened_cost))/
7830                                                                  decode(old_projfunc_revenue,0,NULL,old_projfunc_revenue))*100
7831                old_projfunc_margin_percentage
7832             ,bucketing_period_code
7833             ,pra.parent_assignment_id
7834             ,delete_flag
7835        FROM pa_resource_assignments pra, pa_fp_rollup_tmp frt
7836       WHERE pra.budget_version_id = p_budget_version_id
7837         AND pra.resource_assignment_id = frt.resource_assignment_id;
7838 
7839 BEGIN
7840 
7841 
7842      -- Set the error stack.
7843         pa_debug.set_err_stack('PA_FIN_PLAN_PUB.Call_Maintain_Plan_Matrix');
7844 
7845      -- Get the Debug mode into local variable and set it to 'Y'if its NULL
7846         fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
7847         l_debug_mode := NVL(l_debug_mode, 'Y');
7848 
7849      -- Initialize the return status to success
7850          x_return_status := FND_API.G_RET_STS_SUCCESS;
7851 
7852          IF P_PA_DEBUG_MODE = 'Y' THEN
7853             pa_debug.set_process('Call_Maintain_Plan_Matrix: ' || 'PLSQL','LOG',l_debug_mode);
7854          END IF;
7855 
7856          pa_debug.g_err_stage := 'Getting the project and profile id';
7857          IF P_PA_DEBUG_MODE = 'Y' THEN
7858             pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
7859          END IF;
7860 
7861      /* Get the value of the Project_ID and the Period Profile ID for the
7862         Budget Version. */
7863 
7864            SELECT project_id,
7865                   period_profile_id
7866              INTO l_project_id,
7867                   l_period_profile_id
7868              FROM pa_budget_versions
7869             WHERE budget_version_id = p_budget_version_id;
7870 
7871      /* Get the value of the Preference code of the Budget Version. Margin and
7872         Margin % have to be populated only if the Preference code is
7873         'COST_AND_REV_SAME'. */
7874          pa_debug.g_err_stage := 'Getting the fin plan preference code';
7875          IF P_PA_DEBUG_MODE = 'Y' THEN
7876             pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
7877          END IF;
7878 
7879 
7880          SELECT  fin_plan_preference_code, margin_derived_from_code
7881            INTO  l_fp_preference_code, l_margin_derived_from_code
7882            FROM  pa_proj_fp_options
7883           WHERE  fin_plan_version_id = p_budget_version_id;
7884 
7885 
7886           IF (p_data_source = PA_FP_CONSTANTS_PKG.G_DATA_SOURCE_BUDGET_LINE) THEN
7887               pa_debug.g_err_stage := 'opening budget_lines_cur';
7888               IF P_PA_DEBUG_MODE = 'Y' THEN
7889                  pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
7890               END IF;
7891 
7892               OPEN budget_lines_cur;
7893           ELSIF (p_data_source =  PA_FP_CONSTANTS_PKG.G_DATA_SOURCE_ROLLUP_TMP) THEN
7894               pa_debug.g_err_stage := 'opening rollup_tmp_cur';
7895               IF P_PA_DEBUG_MODE = 'Y' THEN
7896                  pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
7897               END IF;
7898 
7899               OPEN rollup_tmp_cur;
7900           END IF;
7901 
7902           delete from PA_FIN_PLAN_LINES_TMP;
7903           LOOP
7904                IF (p_data_source = PA_FP_CONSTANTS_PKG.G_DATA_SOURCE_BUDGET_LINE) THEN
7905 
7906                       pa_debug.g_err_stage := 'fetching from budget_lines_cur';
7907                       IF P_PA_DEBUG_MODE = 'Y' THEN
7908                          pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
7909                       END IF;
7910 
7911                        FETCH budget_lines_cur
7912                        BULK COLLECT INTO
7913                               l_res_assignment_tbl
7914                              ,l_period_name_tbl
7915                              ,l_start_date_tbl
7916                              ,l_end_date_tbl
7917                              ,l_txn_curr_code_tbl
7918                              ,l_proj_curr_code_tbl
7919                              ,l_projfunc_curr_code_tbl
7920                              ,l_quantity_tbl
7921                              ,l_txn_raw_cost_tbl
7922                              ,l_txn_burdened_cost_tbl
7923                              ,l_txn_revenue_tbl
7924                              ,l_txn_margin_tbl
7925                              ,l_txn_margin_percent_tbl
7926                              ,l_proj_raw_cost_tbl
7927                              ,l_proj_burdened_cost_tbl
7928                              ,l_proj_revenue_tbl
7929                              ,l_proj_margin_tbl
7930                              ,l_proj_margin_percent_tbl
7931                              ,l_projfunc_raw_cost_tbl
7932                              ,l_projfunc_burd_cost_tbl
7933                              ,l_projfunc_revenue_tbl
7934                              ,l_projfunc_margin_tbl
7935                              ,l_projfunc_margin_percent_tbl
7936                              ,l_old_quantity_tbl
7937                              ,l_old_txn_raw_cost_tbl
7938                              ,l_old_txn_burdened_cost_tbl
7939                              ,l_old_txn_revenue_tbl
7940                              ,l_old_txn_margin_tbl
7941                              ,l_old_txn_margin_percent_tbl
7942                              ,l_old_proj_raw_cost_tbl
7943                              ,l_old_proj_burd_cost_tbl
7944                              ,l_old_proj_revenue_tbl
7945                              ,l_old_proj_margin_tbl
7946                              ,l_old_proj_margin_percent_tbl
7947                              ,l_old_projfunc_raw_cost_tbl
7948                              ,l_old_projfunc_burd_cost_tbl
7949                              ,l_old_projfunc_revenue_tbl
7950                              ,l_old_projfunc_margin_tbl
7951                              ,l_old_projfunc_margin_pct_tbl
7952                              ,l_buck_period_code_tbl
7953                              ,l_parent_assignment_tbl
7954                              ,l_delete_flag_tbl
7955                          LIMIT l_plsql_max_array_size;
7956 
7957                ELSIF (p_data_source =  PA_FP_CONSTANTS_PKG.G_DATA_SOURCE_ROLLUP_TMP) THEN
7958 
7959                       pa_debug.g_err_stage := 'fetching from rollup_tmp_cur';
7960                       IF P_PA_DEBUG_MODE = 'Y' THEN
7961                          pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
7962                       END IF;
7963 
7964                        FETCH rollup_tmp_cur
7965                        BULK COLLECT INTO
7966                               l_res_assignment_tbl
7967                              ,l_period_name_tbl
7968                              ,l_start_date_tbl
7969                              ,l_end_date_tbl
7970                              ,l_txn_curr_code_tbl
7971                              ,l_proj_curr_code_tbl
7972                              ,l_projfunc_curr_code_tbl
7973                              ,l_quantity_tbl
7974                              ,l_txn_raw_cost_tbl
7975                              ,l_txn_burdened_cost_tbl
7976                              ,l_txn_revenue_tbl
7977                              ,l_txn_margin_tbl
7978                              ,l_txn_margin_percent_tbl
7979                              ,l_proj_raw_cost_tbl
7980                              ,l_proj_burdened_cost_tbl
7981                              ,l_proj_revenue_tbl
7982                              ,l_proj_margin_tbl
7983                              ,l_proj_margin_percent_tbl
7984                              ,l_projfunc_raw_cost_tbl
7985                              ,l_projfunc_burd_cost_tbl
7986                              ,l_projfunc_revenue_tbl
7987                              ,l_projfunc_margin_tbl
7988                              ,l_projfunc_margin_percent_tbl
7989                              ,l_old_quantity_tbl
7990                              ,l_old_txn_raw_cost_tbl
7991                              ,l_old_txn_burdened_cost_tbl
7992                              ,l_old_txn_revenue_tbl
7993                              ,l_old_txn_margin_tbl
7994                              ,l_old_txn_margin_percent_tbl
7995                              ,l_old_proj_raw_cost_tbl
7996                              ,l_old_proj_burd_cost_tbl
7997                              ,l_old_proj_revenue_tbl
7998                              ,l_old_proj_margin_tbl
7999                              ,l_old_proj_margin_percent_tbl
8000                              ,l_old_projfunc_raw_cost_tbl
8001                              ,l_old_projfunc_burd_cost_tbl
8002                              ,l_old_projfunc_revenue_tbl
8003                              ,l_old_projfunc_margin_tbl
8004                              ,l_old_projfunc_margin_pct_tbl
8005                              ,l_buck_period_code_tbl
8006                              ,l_parent_assignment_tbl
8007                              ,l_delete_flag_tbl
8008                          LIMIT l_plsql_max_array_size;
8009                END IF;
8010 
8011                  pa_debug.g_err_stage := 'Data Source is Budget Lines';
8012                  IF P_PA_DEBUG_MODE = 'Y' THEN
8013                     pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8014                  END IF;
8015 
8016                 /* Insert the Transaction, Project and Project Functional currency
8017                    columns as rows into the Lines Temp table one after the other selecting
8018                    from the Budget Lines. So, three inserts statements are required.   */
8019 
8020                 /* Inserting the Transaction Currency records into the Lines Temp table with
8021                    the Amount Type code as 'TRANSACTION'. */
8022 
8023                  pa_debug.g_err_stage := 'Inserting Transaction Currency Records';
8024                  IF P_PA_DEBUG_MODE = 'Y' THEN
8025                     pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8026                  END IF;
8027 
8028                IF NVL(l_start_date_tbl.last,0) >= 1 THEN
8029                        /* insert txn amounts */
8030                        pa_debug.g_err_stage := 'calling insert_plan_lines_tmp_bulk for txn curr';
8031                        IF P_PA_DEBUG_MODE = 'Y' THEN
8032                           pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8033                        END IF;
8034 
8035                        insert_plan_lines_tmp_bulk(
8036                                p_res_assignment_tbl         =>   l_res_assignment_tbl
8037                               ,p_period_name_tbl            =>   l_period_name_tbl
8038                               ,p_start_date_tbl             =>   l_start_date_tbl
8039                               ,p_end_date_tbl               =>   l_end_date_tbl
8040                               ,p_currency_type              =>   PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_TRANSACTION
8041                               ,p_currency_code_tbl          =>   l_txn_curr_code_tbl
8042                               ,p_quantity_tbl               =>   l_quantity_tbl
8043                               ,p_raw_cost_tbl               =>   l_txn_raw_cost_tbl
8044                               ,p_burdened_cost_tbl          =>   l_txn_burdened_cost_tbl
8045                               ,p_revenue_tbl                =>   l_txn_revenue_tbl
8046                               ,p_old_quantity_tbl           =>   l_old_quantity_tbl
8047                               ,p_old_raw_cost_tbl           =>   l_old_txn_raw_cost_tbl
8048                               ,p_old_burdened_cost_tbl      =>   l_old_txn_burdened_cost_tbl
8049                               ,p_old_revenue_tbl            =>   l_old_txn_revenue_tbl
8050                               ,p_margin_tbl                 =>   l_txn_margin_tbl
8051                               ,p_margin_percent_tbl         =>   l_txn_margin_percent_tbl
8052                               ,p_old_margin_tbl             =>   l_old_txn_margin_tbl
8053                               ,p_old_margin_percent_tbl     =>   l_old_txn_margin_percent_tbl
8054                               ,p_buck_period_code_tbl       =>   l_buck_period_code_tbl
8055                               ,p_parent_assignment_id_tbl   =>   l_parent_assignment_tbl
8056                               ,p_delete_flag_tbl            =>   l_delete_flag_tbl
8057                               ,p_source_txn_curr_code_tbl   =>   l_txn_curr_code_tbl
8058                               ,x_return_status              =>   l_return_status
8059                               ,x_msg_count                  =>   l_msg_count
8060                               ,x_msg_data                   =>   l_msg_data );
8061 
8062                        /* insert project amounts */
8063                        pa_debug.g_err_stage := 'calling insert_plan_lines_tmp_bulk for proj curr';
8064                        IF P_PA_DEBUG_MODE = 'Y' THEN
8065                           pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8066                        END IF;
8067 
8068                        insert_plan_lines_tmp_bulk(
8069                                p_res_assignment_tbl        =>   l_res_assignment_tbl
8070                               ,p_period_name_tbl           =>   l_period_name_tbl
8071                               ,p_start_date_tbl            =>   l_start_date_tbl
8072                               ,p_end_date_tbl              =>   l_end_date_tbl
8073                               ,p_currency_type             =>   PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_PROJECT
8074                               ,p_currency_code_tbl         =>   l_proj_curr_code_tbl
8075                               ,p_quantity_tbl              =>   l_quantity_tbl
8076                               ,p_raw_cost_tbl              =>   l_proj_raw_cost_tbl
8077                               ,p_burdened_cost_tbl         =>   l_proj_burdened_cost_tbl
8078                               ,p_revenue_tbl               =>   l_proj_revenue_tbl
8079                               ,p_old_quantity_tbl          =>   l_old_quantity_tbl
8080                               ,p_old_raw_cost_tbl          =>   l_old_proj_raw_cost_tbl
8081                               ,p_old_burdened_cost_tbl     =>   l_old_proj_burd_cost_tbl
8082                               ,p_old_revenue_tbl           =>   l_old_proj_revenue_tbl
8083                               ,p_margin_tbl                =>   l_proj_margin_tbl
8084                               ,p_margin_percent_tbl        =>   l_proj_margin_percent_tbl
8085                               ,p_old_margin_tbl            =>   l_old_proj_margin_tbl
8086                               ,p_old_margin_percent_tbl    =>   l_old_proj_margin_percent_tbl
8087                               ,p_buck_period_code_tbl      =>   l_buck_period_code_tbl
8088                               ,p_parent_assignment_id_tbl  =>   l_parent_assignment_tbl
8089                               ,p_delete_flag_tbl           =>   l_delete_flag_tbl
8090                               ,p_source_txn_curr_code_tbl  =>   l_txn_curr_code_tbl
8091                               ,x_return_status             =>   l_return_status
8092                               ,x_msg_count                 =>   l_msg_count
8093                               ,x_msg_data                  =>   l_msg_data );
8094 
8095                        /* insert project functional amounts */
8096                        pa_debug.g_err_stage := 'calling insert_plan_lines_tmp_bulk for projfunc curr';
8097                        IF P_PA_DEBUG_MODE = 'Y' THEN
8098                           pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8099                        END IF;
8100 
8101 
8102                        insert_plan_lines_tmp_bulk(
8103                                p_res_assignment_tbl         =>   l_res_assignment_tbl
8104                               ,p_period_name_tbl            =>   l_period_name_tbl
8105                               ,p_start_date_tbl             =>   l_start_date_tbl
8106                               ,p_end_date_tbl               =>   l_end_date_tbl
8107                               ,p_currency_type              =>   PA_FP_CONSTANTS_PKG.G_CURRENCY_TYPE_PROJFUNC
8108                               ,p_currency_code_tbl          =>   l_projfunc_curr_code_tbl
8109                               ,p_quantity_tbl               =>   l_quantity_tbl
8110                               ,p_raw_cost_tbl               =>   l_projfunc_raw_cost_tbl
8111                               ,p_burdened_cost_tbl          =>   l_projfunc_burd_cost_tbl
8112                               ,p_revenue_tbl                =>   l_projfunc_revenue_tbl
8113                               ,p_old_quantity_tbl           =>   l_old_quantity_tbl
8114                               ,p_old_raw_cost_tbl           =>   l_old_projfunc_raw_cost_tbl
8115                               ,p_old_burdened_cost_tbl      =>   l_old_projfunc_burd_cost_tbl
8116                               ,p_old_revenue_tbl            =>   l_old_projfunc_revenue_tbl
8117                               ,p_margin_tbl                 =>   l_projfunc_margin_tbl
8118                               ,p_margin_percent_tbl         =>   l_projfunc_margin_percent_tbl
8119                               ,p_old_margin_tbl             =>   l_old_projfunc_margin_tbl
8120                               ,p_old_margin_percent_tbl     =>   l_old_projfunc_margin_pct_tbl
8121                               ,p_buck_period_code_tbl       =>   l_buck_period_code_tbl
8122                               ,p_parent_assignment_id_tbl   =>   l_parent_assignment_tbl
8123                               ,p_delete_flag_tbl            =>   l_delete_flag_tbl
8124                               ,p_source_txn_curr_code_tbl   =>   l_txn_curr_code_tbl
8125                               ,x_return_status              =>   l_return_status
8126                               ,x_msg_count                  =>   l_msg_count
8127                               ,x_msg_data                   =>   l_msg_data );
8128 
8129                END IF; /* end of only if something is fetched */
8130 
8131                EXIT WHEN nvl(l_start_date_tbl.last,0) < l_plsql_max_array_size;
8132 
8133         END LOOP; -- loop for bulk insert
8134 
8135      IF (p_data_source = PA_FP_CONSTANTS_PKG.G_DATA_SOURCE_BUDGET_LINE) THEN
8136          pa_debug.g_err_stage := 'closing budget_lines_cur';
8137          IF P_PA_DEBUG_MODE = 'Y' THEN
8138             pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8139          END IF;
8140 
8141          CLOSE budget_lines_cur;
8142      ELSIF (p_data_source =  PA_FP_CONSTANTS_PKG.G_DATA_SOURCE_ROLLUP_TMP) THEN
8143          pa_debug.g_err_stage := 'closing rollup_tmp_cur';
8144          IF P_PA_DEBUG_MODE = 'Y' THEN
8145             pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8146          END IF;
8147 
8148          CLOSE rollup_tmp_cur;
8149      END IF;
8150 
8151      IF (p_data_source = PA_FP_CONSTANTS_PKG.G_CALLING_MODULE_ORG_FORECAST) THEN
8152 
8153           -- This portion to be filled up by the HQ team.
8154 
8155           null;
8156 
8157      END IF; /* End IF of check for the Data Source. */
8158 
8159 
8160      /* Call PA_PLAN_MATRIX.MAINTAIN_PLAN_MATRIX to populate the Denorm table
8161         'PA_PROJ_PERIODS_DENORM' in order to report in a matrix format if the
8162         Plan version is tim phased by PA or GL periods. */
8163 
8164      /* First populate the amt_rec PL/SQL table with the Amount Type and the
8165         Amount Sub Type codes based on the Budget Version Type
8166         to pass to the Maintain Plan Matrix procedure. */
8167 
8168           pa_debug.g_err_stage := 'Populating the Amount Types and Amount Sub Types';
8169           IF P_PA_DEBUG_MODE = 'Y' THEN
8170              pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8171           END IF;
8172 
8173           SELECT version_type
8174             INTO l_budget_version_type
8175             FROM PA_BUDGET_VERSIONS
8176            WHERE pa_budget_versions.budget_version_id = p_budget_version_id;
8177 
8178           amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_QUANTITY;
8179           amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_QUANTITY;
8180           amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_QUANTITY);
8181           amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_QUANTITY);
8182 
8183           IF (l_budget_version_type = 'ALL') THEN
8184 
8185                   l_tbl_index := l_tbl_index + 1;
8186                   amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST;
8187                   amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_RAW_COST;
8188                   amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST);
8189                   amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_RAW_COST);
8190 
8191                   l_tbl_index := l_tbl_index + 1;
8192                   amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST;
8193                   amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_BURD_COST;
8194                   amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST);
8195                   amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_BURD_COST);
8196 
8197                   l_tbl_index := l_tbl_index + 1;
8198                   amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE;
8199                   amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE;
8200                   amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE);
8201                   amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE);
8202 
8203           ELSIF (l_budget_version_type = 'COST') THEN
8204 
8205                   l_tbl_index := l_tbl_index + 1;
8206                   amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST;
8207                   amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_RAW_COST;
8208                   amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST);
8209                   amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_RAW_COST);
8210 
8211                   l_tbl_index := l_tbl_index + 1;
8212                   amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST;
8213                   amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_BURD_COST;
8214                   amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_COST);
8215                   amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_BURD_COST);
8216 
8217           ELSIF (l_budget_version_type = 'REVENUE') THEN
8218 
8219                   l_tbl_index := l_tbl_index + 1;
8220                   amt_rec(l_tbl_index).amount_type_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE;
8221                   amt_rec(l_tbl_index).amount_subtype_code := PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE;
8222                   amt_rec(l_tbl_index).amount_type_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE);
8223                   amt_rec(l_tbl_index).amount_subtype_id := pa_fin_plan_utils.get_amttype_id(p_amt_typ_code => PA_FP_CONSTANTS_PKG.G_AMOUNT_TYPE_REVENUE);
8224 
8225           END IF;
8226 
8227           /* Calling the Maintain_Plan_Matrix API to recalculate profile period amounts as well
8228              as preceding and succeeding period amounts in the Denorm Table. */
8229 
8230           pa_debug.g_err_stage := 'Calling the Maintain Plan Matrix procedure';
8231           IF P_PA_DEBUG_MODE = 'Y' THEN
8232              pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,3);
8233           END IF;
8234 
8235           PA_PLAN_MATRIX.Maintain_Plan_Matrix(
8236                              p_amount_type_tab   => amt_rec,
8237                              p_period_profile_id => l_period_profile_id,
8238                              p_prior_period_flag => 'N',
8239                              p_commit_flag       => 'N',
8240                              p_budget_version_id => p_budget_version_id,
8241                              p_project_id        => l_project_id,
8242                              p_debug_mode        => l_debug_mode,
8243                              p_add_msg_in_stack  => 'Y',
8244                              p_calling_module    => PA_FP_CONSTANTS_PKG.G_PD_PROFILE_FIN_PLANNING,
8245                              x_return_status     => x_return_status,
8246                              x_msg_count         => x_msg_count,
8247                              x_msg_data          => x_msg_data);
8248 
8249           IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
8250             Raise PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8251           END IF;
8252 
8253           pa_debug.reset_err_stack;
8254 EXCEPTION
8255     WHEN OTHERS THEN
8256         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8257         x_msg_count     := 1;
8258         x_msg_data      := SQLERRM;
8259         FND_MSG_PUB.add_exc_msg( p_pkg_name         => 'PA_FIN_PLAN_PUB',
8260                                  p_procedure_name   => 'Call_Maintain_Plan_Matrix');
8261 
8262         pa_debug.g_err_stage:='Unexpected Error';
8263         IF P_PA_DEBUG_MODE = 'Y' THEN
8264            pa_debug.write('Call_Maintain_Plan_Matrix: ' || l_module_name,pa_debug.g_err_stage,5);
8265         END IF;
8266         pa_debug.reset_err_stack;
8267         raise FND_API.G_EXC_UNEXPECTED_ERROR;
8268 
8269 END Call_Maintain_Plan_Matrix;
8270 
8271 /*=============================================================================
8272  This api would be called in the context of workplan. This procedure deletes
8273  any exsiting res list assignment and creates new assignment for the input
8274  resource list.
8275 ==============================================================================*/
8276 
8277 PROCEDURE Refresh_res_list_assignment (
8278      p_project_id             IN    pa_budget_versions.project_id%TYPE
8279     ,p_resource_list_id       IN    pa_budget_versions.resource_list_id%TYPE
8280     ,x_return_status          OUT   NOCOPY VARCHAR2 --File.Sql.39 bug 4440895
8281     ,x_msg_count              OUT   NOCOPY NUMBER --File.Sql.39 bug 4440895
8282     ,x_msg_data               OUT   NOCOPY VARCHAR2 ) --File.Sql.39 bug 4440895
8283 AS
8284 
8285     --Start of variables used for debugging
8286     l_return_status      VARCHAR2(1);
8287     l_msg_count          NUMBER := 0;
8288     l_msg_index_out      NUMBER;
8289     l_data               VARCHAR2(2000);
8290     l_msg_data           VARCHAR2(2000);
8291     l_debug_mode         VARCHAR2(30);
8292 
8293     -- Variables declared for calling plsql apis
8294     l_err_code           NUMBER;
8295     l_err_stage          VARCHAR2(2000);
8296     l_err_stack          VARCHAR2(2000);
8297 
8298     -- Other variables
8299     l_existing_rl_assignment_id     pa_resource_list_assignments.resource_list_assignment_id%TYPE;
8300     l_new_rl_assignment_id          pa_resource_list_assignments.resource_list_assignment_id%TYPE;
8301 
8302 BEGIN
8303 
8304     x_msg_count := 0;
8305     x_return_status := FND_API.G_RET_STS_SUCCESS;
8306 
8307     fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
8308     l_debug_mode := NVL(l_debug_mode, 'Y');
8309 
8310     -- Set curr function
8311     pa_debug.set_curr_function(
8312                 p_function   =>'PA_FIN_PLAN_PUB.Refresh_res_list_assignment'
8313                ,p_debug_mode => l_debug_mode );
8314 
8315 
8316     -- Check for business rule violations
8317 
8318     IF l_debug_mode = 'Y' THEN
8319         pa_debug.g_err_stage:='Validating input parameters';
8320         pa_debug.write('Refresh_res_list_assignment: ' || l_module_name,pa_debug.g_err_stage,3);
8321     END IF;
8322 
8323     IF (p_project_id       IS NULL) OR
8324        (p_resource_list_id IS NULL)
8325     THEN
8326 
8327         IF l_debug_mode = 'Y' THEN
8328            pa_debug.g_err_stage:='p_project_id = '|| p_project_id;
8329            pa_debug.write('Refresh_res_list_assignment: ' || l_module_name,pa_debug.g_err_stage,5);
8330 
8331            pa_debug.g_err_stage:='p_resource_list_id = '|| p_resource_list_id;
8332            pa_debug.write('Refresh_res_list_assignment: ' || l_module_name,pa_debug.g_err_stage,5);
8333         END IF;
8334 
8335         PA_UTILS.ADD_MESSAGE(p_app_short_name => 'PA',
8336                              p_msg_name       => 'PA_FP_INV_PARAM_PASSED');
8337 
8338         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8339 
8340     END IF;
8341 
8342     -- Check if there already exists a resource list assignment
8343 
8344     BEGIN
8345         SELECT resource_list_assignment_id
8346         INTO   l_existing_rl_assignment_id
8347         FROM   pa_resource_list_assignments
8348         WHERE  project_id = p_project_id
8349         AND    resource_list_id = p_resource_list_id
8350         AND    used_in_wp_flag  = 'Y' ;
8351 
8352         -- If resource list assignment exists it needs to be deleted first
8353         l_err_code        :=    0;
8354         l_err_stage       :=    null;
8355         l_err_stack       :=    null;
8356 
8357         PA_RES_LIST_ASSIGNMENTS.Delete_Rl_Assgmt(
8358                  X_Resource_list_Assgmt_id   =>  l_existing_rl_assignment_id
8359                 ,X_err_code                  =>  l_err_code
8360                 ,X_err_stage                 =>  l_err_stage
8361                 ,x_err_stack                 =>  l_err_stack );
8362         IF  l_err_code <> 0 THEN
8363             -- Add the error message
8364             IF l_err_stage IS NOT NULL THEN
8365                 PA_UTILS.ADD_MESSAGE(
8366                          p_app_short_name => 'PA'
8367                         ,p_msg_name       => l_err_stage);
8368             END IF;
8369             RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8370         END IF;
8371     EXCEPTION
8372         When No_Data_Found Then
8373             null;    -- do nothing
8374     END;
8375 
8376     -- Create new res list assignment for the passed resource list
8377 
8378     PA_RES_LIST_ASSIGNMENTS.Create_Rl_Assgmt (
8379                X_project_id               =>  p_project_id
8380               ,X_resource_list_id         =>  p_resource_list_id
8381               ,X_resource_list_Assgmt_id  =>  l_new_rl_assignment_id
8382               ,X_err_code                 =>  l_err_code
8383               ,X_err_stage                =>  l_err_stage
8384               ,X_err_stack                =>  l_err_stack );
8385 
8386     IF  l_err_code <> 0 THEN
8387         -- Add the error message
8388         IF l_err_stage IS NOT NULL THEN
8389             PA_UTILS.ADD_MESSAGE(
8390                      p_app_short_name => 'PA'
8391                     ,p_msg_name       => l_err_stage);
8392         END IF;
8393         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8394     END IF;
8395 
8396     IF l_debug_mode = 'Y' THEN
8397         pa_debug.g_err_stage:='Exiting Refresh_res_list_assignment';
8398         pa_debug.write('Refresh_res_list_assignment: ' || l_module_name,pa_debug.g_err_stage,3);
8399 
8400         -- reset curr function
8401         pa_debug.reset_curr_function;
8402     END IF;
8403 
8404 EXCEPTION
8405 
8406    WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8407        l_msg_count := FND_MSG_PUB.count_msg;
8408        IF l_msg_count = 1 THEN
8409            PA_INTERFACE_UTILS_PUB.get_messages
8410                  (p_encoded        => FND_API.G_TRUE
8411                   ,p_msg_index      => 1
8412                   ,p_msg_count      => l_msg_count
8413                   ,p_msg_data       => l_msg_data
8414                   ,p_data           => l_data
8415                   ,p_msg_index_out  => l_msg_index_out);
8416 
8417            x_msg_data := l_data;
8418            x_msg_count := l_msg_count;
8419        ELSE
8420            x_msg_count := l_msg_count;
8421        END IF;
8422 
8423        x_return_status := FND_API.G_RET_STS_ERROR;
8424 
8425        IF l_debug_mode = 'Y' THEN
8426            pa_debug.g_err_stage:='Invalid Arguments Passed Or called api raised an error';
8427            pa_debug.write('Refresh_res_list_assignment: ' || l_module_name,pa_debug.g_err_stage,5);
8428 
8429            -- reset curr function
8430            pa_debug.reset_curr_function;
8431        END IF;
8432        RETURN;
8433    WHEN Others THEN
8434        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8435        x_msg_count     := 1;
8436        x_msg_data      := SQLERRM;
8437 
8438        FND_MSG_PUB.add_exc_msg( p_pkg_name        => 'PA_FIN_PLAN_PUB'
8439                                ,p_procedure_name  => 'Refresh_res_list_assignment');
8440 
8441        IF l_debug_mode = 'Y' THEN
8442            pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
8443            pa_debug.write('Refresh_res_list_assignment: ' || l_module_name,pa_debug.g_err_stage,5);
8444 
8445            -- reset curr function
8446            pa_debug.reset_curr_function;
8447        END IF;
8448        RAISE;
8449 END Refresh_res_list_assignment;
8450 
8451  /* bug 4865563: Added the following procedure which accepts a
8452     * budget version id and inserts records in the new IPM table PA_RESOURCE_ASGN_CURR
8453     * for the resource assignments which do not have budget lines and hence
8454     * not taken care by the new plannig transaction level entiy maintenance API.
8455     * These resource assignments are inserted with default currency as applicable.
8456     *
8457     * This API is called from upgrade_budget_versions API[PAFPUPGB.pls].
8458     */
8459 
8460 
8461    PROCEDURE create_default_plan_txn_rec
8462              (p_budget_version_id          IN    pa_budget_versions.budget_version_id%TYPE,
8463               p_calling_module             IN    VARCHAR2,
8464 	      p_ra_id_tbl                  IN    SYSTEM.PA_NUM_TBL_TYPE         DEFAULT SYSTEM.PA_NUM_TBL_TYPE(),  /* 7161809 */
8465 	      p_curr_code_tbl              IN    SYSTEM.PA_VARCHAR2_15_TBL_TYPE DEFAULT SYSTEM.PA_VARCHAR2_15_TBL_TYPE(),  /* 7161809 */
8466               x_return_status              OUT NOCOPY  VARCHAR2,
8467               x_msg_count                  OUT NOCOPY  NUMBER,
8468               x_msg_data                   OUT NOCOPY  VARCHAR2)
8469    IS
8470 
8471    l_return_status                 VARCHAR2(2000);
8472    l_msg_count                     NUMBER :=0;
8473    l_msg_data                      VARCHAR2(2000);
8474    l_data                          VARCHAR2(2000);
8475    l_msg_index_out                 NUMBER;
8476    l_debug_mode                    VARCHAR2(30);
8477 
8478    CURSOR def_plan_txn_to_ins_csr
8479    IS
8480    SELECT ra.resource_assignment_id
8481    FROM   pa_resource_assignments ra
8482    WHERE  ra.budget_version_id = p_budget_version_id
8483    AND    NOT EXISTS (SELECT 'X'
8484                       FROM   pa_resource_asgn_curr rac
8485                       WHERE  rac.resource_assignment_id = ra.resource_assignment_id
8486                       AND    rac.budget_version_id = ra.budget_version_id);
8487 
8488    l_def_plan_txn_ra_id_tbl        SYSTEM.pa_num_tbl_type:=SYSTEM.pa_num_tbl_type();
8489    l_def_txn_curr_code             pa_budget_lines.txn_currency_code%TYPE;
8490    l_curr_code_temp_tbl            SYSTEM.PA_VARCHAR2_15_TBL_TYPE := SYSTEM.PA_VARCHAR2_15_TBL_TYPE();   /* 7161809 */
8491 
8492    l_agr_curr_code                 pa_agreements_all.agreement_currency_code%TYPE;
8493    l_proj_curr_code                pa_projects_all.project_currency_code%TYPE;
8494    l_pfunc_curr_code               pa_projects_all.projfunc_currency_code%TYPE;
8495 
8496    l_ci_id                         pa_budget_versions.ci_id%TYPE;
8497    l_agreement_id                  pa_budget_versions.agreement_id%TYPE;
8498    l_app_rev_flag                  pa_budget_versions.approved_rev_plan_type_flag%TYPE;
8499 
8500    -- IPM Arch Enhancement - Bug 4865563
8501    l_fp_cols_rec                   PA_FP_GEN_AMOUNT_UTILS.FP_COLS;  --This variable will be used to call pa_resource_asgn_curr maintenance api
8502    l_debug_level5                  NUMBER:=5;
8503 
8504    BEGIN
8505        x_msg_count := 0;
8506        x_msg_data  := NULL;
8507        x_return_status := FND_API.G_RET_STS_SUCCESS;
8508 
8509        fnd_profile.get('PA_DEBUG_MODE',l_debug_mode);
8510        l_debug_mode := NVL(l_debug_mode, 'Y');
8511 
8512        IF p_pa_debug_mode = 'Y' THEN
8513             pa_debug.set_err_stack('PA_FIN_PLAN_PUB.create_default_plan_txn_rec');
8514             pa_debug.set_process('PLSQL','LOG',l_debug_mode);
8515 
8516             pa_debug.g_err_stage := 'Entered create_default_plan_txn_rec';
8517             pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8518 
8519             -- Check for not null parameters
8520 
8521             pa_debug.g_err_stage := 'Checking for valid parameters:';
8522             pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8523        END IF;
8524 
8525        IF p_budget_version_id       IS NULL OR
8526           p_calling_module          IS NULL THEN
8527            IF p_pa_debug_mode = 'Y' THEN
8528                 pa_debug.g_err_stage := 'p_budget_version_id = '||p_budget_version_id;
8529                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8530                 pa_debug.g_err_stage := 'p_calling_module = '||p_calling_module;
8531                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8532            END IF;
8533 
8534            PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
8535                                 p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
8536 
8537            RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8538 
8539        END IF;
8540        IF p_pa_debug_mode = 'Y' THEN
8541             pa_debug.g_err_stage := 'Parameter validation complete';
8542             pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8543        END IF;
8544 
8545         if (p_calling_module = 'UPDATE_PLAN_TRANSACTION') then  /* janani */
8546 	    l_def_plan_txn_ra_id_tbl := p_ra_id_tbl;
8547         else
8548 	    OPEN def_plan_txn_to_ins_csr;
8549 	    FETCH def_plan_txn_to_ins_csr
8550 	    BULK COLLECT INTO l_def_plan_txn_ra_id_tbl;
8551 	    CLOSE def_plan_txn_to_ins_csr;
8552          END IF;     /* janani */
8553 
8554 	 l_curr_code_temp_tbl := p_curr_code_tbl;  /* janani */
8555     l_curr_code_temp_tbl.extend(l_def_plan_txn_ra_id_tbl.COUNT);  /* janani */
8556 
8557 
8558        IF l_def_plan_txn_ra_id_tbl.COUNT = 0 THEN
8559            IF p_pa_debug_mode = 'Y' THEN
8560                pa_debug.g_err_stage := 'No resource assignment to default, returning';
8561                pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8562                pa_debug.reset_err_stack;
8563            END IF;
8564            RETURN;
8565        END IF;
8566 
8567        IF l_def_plan_txn_ra_id_tbl.COUNT > 0 THEN
8568              IF p_pa_debug_mode = 'Y' THEN
8569                 pa_debug.g_err_stage := 'Getting currency information';
8570                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8571              END IF;
8572 
8573            BEGIN
8574                  SELECT pbv.ci_id,
8575                         pbv.agreement_id,
8576                         pbv.approved_rev_plan_type_flag,
8577                         ppa.project_currency_code,
8578                         ppa.projfunc_currency_code
8579                  INTO   l_ci_id,
8580                         l_agreement_id,
8581                         l_app_rev_flag,
8582                         l_proj_curr_code,
8583                         l_pfunc_curr_code
8584                  FROM   pa_budget_versions pbv,
8585                         pa_projects_all ppa
8586                  WHERE  pbv.budget_version_id = p_budget_version_id
8587                  AND    ppa.project_id = pbv.project_id;
8588            EXCEPTION
8589                  WHEN NO_DATA_FOUND THEN
8590                      IF p_pa_debug_mode = 'Y' THEN
8591                         pa_debug.g_err_stage := 'Invalid budget version id';
8592                         pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8593                      END IF;
8594                      PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
8595                                           p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
8596 
8597                      RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8598            END;
8599 
8600            -- bug 5007734: for upgrade context, when this API is called to upgrade forms based
8601            -- budget versions to sswa plan versions, the txn currency should always be PFC
8602            IF p_calling_module = 'UPGRADE' THEN
8603                l_def_txn_curr_code := l_pfunc_curr_code;
8604            ELSE
8605                -- create version flow: proceed with the usual currency defaulting logic
8606                IF l_ci_id IS NOT NULL THEN
8607                     /* ci version context */
8608                          IF p_pa_debug_mode = 'Y' THEN
8609                             pa_debug.g_err_stage := 'Ci_id is not null';
8610                             pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8611                          END IF;
8612                     IF l_app_rev_flag = 'Y' THEN
8613                         IF l_agreement_id IS NOT NULL THEN
8614                             /* txn currency should be the agreement currency */
8615                             BEGIN
8616                                 SELECT agreement_currency_code
8617                                 INTO   l_agr_curr_code
8618                                 FROM   pa_agreements_all
8619                                 WHERE  agreement_id = l_agreement_id;
8620                             EXCEPTION
8621                                 WHEN NO_DATA_FOUND THEN
8622                                     IF p_pa_debug_mode = 'Y' THEN
8623                                        pa_debug.g_err_stage := 'Invalid agreement id';
8624                                        pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8625                                     END IF;
8626                                     PA_UTILS.ADD_MESSAGE(p_app_short_name=> 'PA',
8627                                                          p_msg_name      => 'PA_FP_INV_PARAM_PASSED');
8628                                     RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8629                             END;
8630                             l_def_txn_curr_code := l_agr_curr_code;
8631                         ELSE
8632                            /* it is possible, that for upgraded data, the agreement information may be missing.
8633                             * in this case, when user visits the change order page, he has to select a valid
8634                             * agreement before proceeding. In that case, all the existing budget lines for the revenue/all
8635                             * change order is deleted and new budget lines would be created with the agreement currency
8636                             * as the txn currency. But, to create the planning transaction with the default currency,
8637                             * in the new entity for change orders, which do not have an agreement yet, we are defaulting
8638                             * PFC as the txn currency. Anyway, this would be deleted, when user selects a valid agreement.
8639                             */
8640                            l_def_txn_curr_code := l_pfunc_curr_code;
8641                         END IF;
8642                     ELSE
8643                         /* txn currency should be the project currency */
8644                         l_def_txn_curr_code := l_proj_curr_code;
8645                     END IF;
8646                ELSE
8647                     IF l_app_rev_flag = 'Y' THEN
8648                         /* txn currency should be the project functional currency */
8649                         l_def_txn_curr_code := l_pfunc_curr_code;
8650                     ELSE
8651                         /* txn currency should be the project currency */
8652                         l_def_txn_curr_code := l_proj_curr_code;
8653                     END IF;
8654                END IF; -- if ci_id not null
8655            END IF; -- p_calling_module
8656 
8657            IF p_pa_debug_mode = 'Y' THEN
8658               pa_debug.g_err_stage := 'l_def_txn_curr_code: ' || l_def_txn_curr_code;
8659               pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL3);
8660            END IF;
8661 
8662            DELETE pa_resource_asgn_curr_tmp;
8663            /* bulk insert the left over resource assignments into the new table with default txn currency
8664             * derived above */
8665            FORALL k IN l_def_plan_txn_ra_id_tbl.FIRST .. l_def_plan_txn_ra_id_tbl.LAST
8666                INSERT INTO pa_resource_asgn_curr_tmp
8667                    (RA_TXN_ID,
8668                     BUDGET_VERSION_ID,
8669                     RESOURCE_ASSIGNMENT_ID,
8670                     TXN_CURRENCY_CODE)
8671                    VALUES
8672                    (pa_resource_asgn_curr_s.nextval,
8673                     p_budget_version_id,
8674                     l_def_plan_txn_ra_id_tbl(k),
8675                     /*l_def_txn_curr_code);    */
8676 		    nvl(l_curr_code_temp_tbl(k),l_def_txn_curr_code));   /* janani */
8677 
8678                    --IPM Arch Enhancement Bug 4865563 Start
8679                    PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DTLS
8680                    (P_BUDGET_VERSION_ID              => p_budget_version_id,
8681                     X_FP_COLS_REC                    => l_fp_cols_rec,
8682                     X_RETURN_STATUS                  => l_return_status,
8683                     X_MSG_COUNT                      => l_msg_count,
8684                     X_MSG_DATA                       => l_msg_data);
8685 
8686                     IF l_return_status <>  FND_API.G_RET_STS_SUCCESS THEN
8687                         IF P_PA_debug_mode = 'Y' THEN
8688                            pa_debug.g_err_stage:= 'Error in TARGET PA_FP_GEN_AMOUNT_UTILS.GET_PLAN_VERSION_DETAILS';
8689                            pa_debug.write(l_module_name,pa_debug.g_err_stage,l_debug_level5);
8690                         END IF;
8691                         RAISE PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc;
8692                     END IF;
8693 
8694 
8695                 /* calling the maintenance api to insert data into the new planning transaction level table */
8696                 if (p_calling_module = 'UPDATE_PLAN_TRANSACTION') then  /* janani */
8697 		pa_res_asg_currency_pub.maintain_data(
8698 				p_fp_cols_rec                  => l_fp_cols_rec,
8699 				p_calling_module               => p_calling_module,
8700 				p_delete_flag                  => 'N',
8701 				p_copy_flag                    => 'N',
8702 				p_src_version_id               => NULL,
8703 				p_copy_mode                    => NULL,
8704 				p_rollup_flag                  => 'Y',
8705 				p_version_level_flag           => 'N',
8706 				p_called_mode                  => 'SELF_SERVICE',
8707 				x_return_status                => l_return_status,
8708 				x_msg_count                    => l_msg_count,
8709 				x_msg_data                     => l_msg_data
8710 				);
8711 	     else
8712 		PA_RES_ASG_CURRENCY_PUB.maintain_data
8713                            (p_fp_cols_rec          => l_fp_cols_rec,
8714                             p_calling_module       => p_calling_module,
8715                             p_version_level_flag   => 'N',     --Calling in temp table mode
8716                             x_return_status        => l_return_status,
8717                             x_msg_count            => l_msg_count,
8718                             x_msg_data             => l_msg_data);
8719               END IF; /* janani */
8720 
8721                      IF l_return_status <> FND_API.G_RET_STS_SUCCESS
8722                         THEN
8723                            IF p_pa_debug_mode = 'Y' THEN
8724                                pa_debug.write_file('Failed due to error in PA_RES_ASG_CURRENCY_PUB.maintain_data',5);
8725                            END IF;
8726                            raise PA_FP_CONSTANTS_PKG.INVALID_ARG_EXC;
8727                      END IF;
8728            --IPM Arch Enhancement Bug 4865563 End
8729 
8730 
8731        END IF; -- if there is ra_id to be inserted
8732        IF p_pa_debug_mode = 'Y' THEN
8733            pa_debug.reset_err_stack;
8734        END IF;
8735 
8736    EXCEPTION
8737        WHEN PA_FP_CONSTANTS_PKG.Invalid_Arg_Exc THEN
8738 
8739            IF def_plan_txn_to_ins_csr%ISOPEN THEN
8740                CLOSE def_plan_txn_to_ins_csr;
8741            END IF;
8742            l_msg_count := FND_MSG_PUB.count_msg;
8743            IF l_msg_count = 1 THEN
8744                 PA_INTERFACE_UTILS_PUB.get_messages
8745                       ( p_encoded        => FND_API.G_TRUE
8746                        ,p_msg_index      => 1
8747                        ,p_msg_count      => l_msg_count
8748                        ,p_msg_data       => l_msg_data
8749                        ,p_data           => l_data
8750                        ,p_msg_index_out  => l_msg_index_out);
8751                 x_msg_data := l_data;
8752                 x_msg_count := l_msg_count;
8753            ELSE
8754                x_msg_count := l_msg_count;
8755                x_msg_data := l_msg_data;
8756            END IF;
8757            IF p_pa_debug_mode = 'Y' THEN
8758                 pa_debug.g_err_stage:='Invalid Arguments Passed';
8759                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8760                 pa_debug.write_file('create_default_plan_txn_rec ' || x_msg_data,5);
8761            END IF;
8762 
8763            x_return_status:= FND_API.G_RET_STS_ERROR;
8764            IF p_pa_debug_mode = 'Y' THEN
8765                pa_debug.reset_err_stack;
8766            END IF;
8767            RAISE;
8768 
8769       WHEN Others THEN
8770 
8771            IF def_plan_txn_to_ins_csr%ISOPEN THEN
8772                CLOSE def_plan_txn_to_ins_csr;
8773            END IF;
8774            x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8775            x_msg_count     := 1;
8776            x_msg_data      := SQLERRM;
8777            FND_MSG_PUB.add_exc_msg( p_pkg_name => 'PA_FIN_PLAN_PUB'
8778                             ,p_procedure_name  => 'create_default_plan_txn_rec');
8779            IF p_pa_debug_mode = 'Y' THEN
8780                 pa_debug.g_err_stage:='Unexpected Error'||SQLERRM;
8781                 pa_debug.write(l_module_name,pa_debug.g_err_stage,PA_FP_CONSTANTS_PKG.G_DEBUG_LEVEL5);
8782                 pa_debug.write_file('create_default_plan_txn_rec '  || pa_debug.G_Err_Stack,5);
8783            END IF;
8784            IF p_pa_debug_mode = 'Y' THEN
8785                pa_debug.reset_err_stack;
8786            END IF;
8787            RAISE;
8788    END create_default_plan_txn_rec;
8789 
8790 END pa_fin_plan_pub;