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